IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:45:22 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simmosaic_branches
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/simmosaic_branches

  • branches/simmosaic_branches/magic

    • Property svn:ignore
      •  

        old new  
        11magic
        22ssa-core-cpp
        3 Makefile
        43Makefile.bak
  • branches/simmosaic_branches/magic/remove

    • Property svn:ignore set to
      configure
      Makefile.in
      Doxyfile
      config.log
      depcomp
      config.status
      config.guess
      ltmain.sh
      config.sub
      autom4te.cache
      libtool
      missing
      Makefile
      aclocal.m4
      install-sh

  • branches/simmosaic_branches/magic/remove/src

    • Property svn:ignore
      •  

        old new  
        44streakscompare
        55streaksrelease
        6 makefile
         6Makefile
         7Makefile.in
         8config.h
         9.deps
         10streaksVersionDefinitions.h
         11config.h.in
         12stamp-h1
  • branches/simmosaic_branches/magic/remove/src/streaksremove.c

    r24853 r27839  
    1212static pmConfig *parseArguments(int argc, char **argv);
    1313static bool readAndCopyToOutput(streakFiles *sf, bool exciseAll);
    14 static void exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue);
    15 static bool warpedPixel(streakFiles *sfiles, PixelPos *cellCoord);
    16 static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue);
     14static long exciseNonDiffedPixels(streakFiles *sfiles, psImageMaskType newMaskValue);
     15static bool diffedPixel(streakFiles *sfiles, int x, int y);
     16static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue);
    1717static void writeImages(streakFiles *sf, bool exciseImageCube);
    1818static void updateAstrometry(streakFiles *sfiles);
    19 static void censorSources(streakFiles *sfiles, psU32 maskStreak);
    20 
     19static void censorSources(streakFiles *sfiles, psImageMaskType maskStreak);
     20static long censorPixels(streakFiles *sfiles, psImage * pixels, bool checkNonDiffedPixels, psU16 maskStreak);
     21
     22char *streaksProgram = "streaksremove";
     23
     24// Note: For clarity the flow of this program is in main().
     25// There is not a lot of error checking is done in main.
     26// Until the end, where we might be doing Nebulous operations, called functions exit when an error
     27// is encountered.
    2128int
    2229main(int argc, char *argv[])
     
    2532
    2633    psLibInit(NULL);
    27     psTimerStart("STREAKSREMOVE");
     34    psTimerStart("TOTAL_TIME");
    2835
    2936    pmConfig *config = parseArguments(argc, argv);
     
    3340    }
    3441
    35     // Values to set for masked pixels
    36     psU32 maskStreak = 0;           // for the image and weight (usually NAN, MAXINT for integer images)
    37     psU32 maskMask = 0;             // value looked up for MASK.STREAK
    38 
    3942    psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS");
    4043
     
    4245    Streaks *streaks = readStreaksFile(streaksFileName);
    4346    if (!streaks) {
    44         psError(PS_ERR_UNKNOWN, "failed to read streaks file: %s", streaksFileName);
     47        psError(PS_ERR_UNKNOWN, false, "failed to read streaks file: %s", streaksFileName);
    4548        streaksExit("", PS_EXIT_PROG_ERROR);
    4649    }
     
    4952    streakFiles *sfiles = openFiles(config, true, argv[0]);
    5053    setupAstrometry(sfiles);
     54    sfiles->stats = psMetadataAlloc();
    5155
    5256    // Optionally we can set pixels that are masked to NAN since they couldn't have been
     
    6064
    6165    bool exciseAll = false;
    62     // --keepnonwarped is a test and debug mode
    63     bool keepNonWarpedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_WARPED");
    64 
    65     // we need to check for non warped pixels unless we've been asked not to or the stage is diff
    66     // (By definition pixels in diff images were included in a diff)
    67     bool checkNonWarpedPixels = ! (keepNonWarpedPixels || (sfiles->stage == IPP_STAGE_DIFF) );
    68 
    69     if (checkNonWarpedPixels ) {
     66    // --keepnondiffed is a test and debug mode
     67    bool keepNonDiffedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_DIFFED");
     68
     69    // we need to check for non diffed pixels unless we've been asked not to or the stage is diff
     70    // (By definition pixels in diff images were included in the difference images)
     71    bool checkNonDiffedPixels = ! (keepNonDiffedPixels || (sfiles->stage == IPP_STAGE_DIFF) );
     72
     73    if (checkNonDiffedPixels ) {
    7074        // From magic ICD:
    7175        // In the raw and detrended images, the pixels which were not
     
    7579        // if no skycells are provided sfiles->exciseAll is set to true
    7680
    77         psTimerStart("COMPUTE_WARPED_PIXELS");
    78         if (! computeWarpedPixels(sfiles) ) {
     81        psTimerStart("COMPUTE_DIFFED_PIXELS");
     82        if (! computeDiffedPixels(sfiles) ) {
    7983            // we have no choice to excise all pixels
    8084            exciseAll = true;
    8185        }
    82         psF64 cwp_t = psTimerClear("COMPUTE_WARPED_PIXELS");
    83         psLogMsg("streaksremove", PS_LOG_INFO, "time to compute warped pixels: %f\n", cwp_t);
    84     }
    85    
     86        psF64 cdp_t = psTimerClear("COMPUTE_DIFFED_PIXELS");
     87        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "COMPUTE_UNDIFFED_PIXELS", PS_META_REPLACE, "time to compute non-diffedpixels", cdp_t);
     88        psLogMsg("streaksremove", PS_LOG_INFO, "time to compute diffed pixels: %f\n", cdp_t);
     89    }
     90
    8691    if (sfiles->stage == IPP_STAGE_RAW) {
    8792        // Except for raw stage, all of our (GPC1) files have one image extension.
     
    97102    }
    98103
    99     int totalPixels = 0;
    100     int totalStreakPixels = 0;
    101 
     104    long totalPixels = 0;
     105    long totalStreakPixels = 0;
     106    long nonDiffedPixels = 0;
     107
     108    // accumulators for the various timers
     109    psF64 gsp_t = 0;
     110    psF64 enw_t = 0;
     111    psF64 rms_t = 0;
     112    psF64 cs_t = 0;
     113    psF64 wi_t = 0;
     114    psF64 ua_t = 0;
    102115    // Iterate through each component of the input (except for raw images there is only one)
    103116    do {
     
    117130
    118131        // now that we've read the input files, lookup the mask values
    119         if (maskStreak == 0) {
    120             strkGetMaskValues(sfiles, &maskStreak, &maskMask);
    121         }
     132        strkGetMaskValues(sfiles);
    122133
    123134        totalPixels += sfiles->inImage->numRows * sfiles->inImage->numCols;
     
    131142            StreakPixels *pixels = streak_on_component(streaks, sfiles->astrom, sfiles->inImage->numCols,
    132143                                                        sfiles->inImage->numRows);
     144            gsp_t +=  psTimerClear("GET_STREAK_PIXELS");
     145            psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "GET_STREAK_PIXELS", PS_META_REPLACE, "", gsp_t);
    133146            psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS"));
    134            
     147
    135148            // if this extension contained an image, excise the streaked pixels.
    136149            // otherwise it contained an image cube (video cell) which is handled in the if block
    137150            if (sfiles->inImage->image) {
    138                 if (checkNonWarpedPixels) {
    139                     psTimerStart("EXCISE_NON_WARPED");
    140 
    141                     // set non-warped pixels and variance to NAN, mask to maskStreak (since the pixel
     151                if (checkNonDiffedPixels) {
     152                    psTimerStart("EXCISE_NON_DIFFED");
     153
     154                    // set non-diffed pixels and variance to NAN, mask to maskStreak (since the pixel
    142155                    // is excised as part of the destreaking process)
    143                     exciseNonWarpedPixels(sfiles, maskStreak);
    144 
    145                     psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED"));
     156                    nonDiffedPixels += exciseNonDiffedPixels(sfiles, sfiles->maskStreak);
     157
     158                    enw_t +=  psTimerClear("EXCISE_NON_DIFFED");
     159                    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "EXCISE_NON_DIFFED", PS_META_REPLACE, "", enw_t);
     160                    psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non diffed pixels: %f\n", enw_t);
    146161                }
    147162
    148                 totalStreakPixels +=  psArrayLength(pixels);
    149 
    150163                psTimerStart("REMOVE_STREAKS");
    151164
    152                 // for each pixel covered by the streak
    153                 for (int i = 0; i < psArrayLength (pixels); ++i) {
    154                     PixelPos *pixelPos = psArrayGet (pixels, i);
    155 
    156                     // if this pixel was not part of the warp, skip because it has already been
    157                     // excised (unless we weren't asked to check)
    158                     if (!checkNonWarpedPixels || warpedPixel(sfiles, pixelPos)) {
    159 
    160                         excisePixel(sfiles, pixelPos->x, pixelPos->y, true, maskStreak);
    161 
    162                     } else {
    163                         // This pixel was not included in any warp and has thus already excised
    164                         // by exciseNonWarpedPixels
    165                     }
    166                 }
    167 
    168                 psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS"));
     165                totalStreakPixels += censorPixels(sfiles, pixels, checkNonDiffedPixels, sfiles->maskStreak);
     166
     167                rms_t += psTimerClear("REMOVE_STREAKS");
     168                psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "REMOVE_STREAKS", PS_META_REPLACE, "", enw_t);
     169                psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", rms_t);
    169170
    170171                if (nanForRelease) {
    171172                    // set any pixels that were masked, to NAN (unless they are already NAN)
    172                     setMaskedToNAN(sfiles, maskMask, true);
     173                    setMaskedToNAN(sfiles, sfiles->maskMask, true);
    173174                }
    174175
    175             } else { 
     176            } else {
    176177                // this component contains an image cube
    177178                // For now excise it completely
    178179                exciseImageCube = true;
    179180            }
    180             psArrayElementsFree (pixels);
    181181            psFree(pixels);
    182182        }
     
    186186            // chip processed files with the data calcuated by psastro at the camera stage
    187187            // (actually we use a linear approximation)
     188            psTimerStart("UPDATE_ASTROMETRY");
    188189            updateAstrometry(sfiles);
    189         }
    190 
    191         censorSources(sfiles, maskStreak);
     190            ua_t += psTimerClear("UPDATE_ASTROMETRY");
     191            psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "UPDATE_ASTROMETRY", PS_META_REPLACE, "", ua_t);
     192            psLogMsg("streaksremove", PS_LOG_INFO, "time to update astrometry: %f\n", ua_t);
     193        }
     194
     195        psTimerStart("CENSOR_SOURCES");
     196        censorSources(sfiles, sfiles->maskStreak);
     197        cs_t += psTimerClear("CENSOR_SOURCES");
     198        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "CENSOR_SOURCES", PS_META_REPLACE, "", cs_t);
    192199
    193200        // write the destreaked "temporary" images and the recovery images
     201        psTimerStart("WRITE_IMAGES");
    194202        writeImages(sfiles, exciseImageCube);
    195 
     203        wi_t += psTimerClear("WRITE_IMAGES");
     204        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "WRITE_IMAGES", PS_META_REPLACE, "", wi_t);
    196205
    197206        psLogMsg("streaksremove", PS_LOG_INFO, "time to process component %d: %f\n", sfiles->extnum, psTimerClear("PROCESS_COMPONENT"));
     207
    198208    } while (streakFilesNextExtension(sfiles));
    199209
     
    201211    psFree(streaks);
    202212
    203     psLogMsg("streaksremove", PS_LOG_INFO, "pixels: %ld streak pixels: %ld %4.2f%%\n", totalPixels, totalStreakPixels, 100. * totalStreakPixels / totalPixels);
     213    if (exciseAll) {
     214        totalStreakPixels = totalPixels;
     215    }
     216
     217    psF64 streakFraction = (double) totalStreakPixels / totalPixels;
     218    psLogMsg("streaksremove", PS_LOG_INFO, "   total pixels:  %8ld\n", totalPixels);
     219    psLogMsg("streaksremove", PS_LOG_INFO, "  streak pixels:  %8ld %4.2f%%\n", totalStreakPixels, streakFraction * 100);
     220    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "STREAK_FRACTION", PS_META_REPLACE, "", streakFraction);
     221
     222    psF64 nonDiffedFraction = (double) nonDiffedPixels / totalPixels;
     223    psLogMsg("streaksremove", PS_LOG_INFO, "nondiffed pixels:  %8ld %4.2f%%\n", nonDiffedPixels, nonDiffedFraction * 100);
     224    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "NONDIFFED_FRACTION", PS_META_REPLACE, "", nonDiffedFraction);
    204225
    205226    // check the weight and mask files for extra extensions that might be in files
     
    210231
    211232    psTimerStart("CLOSE_IMAGES");
    212 
    213233    closeImages(sfiles);
    214 
    215     psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", psTimerClear("CLOSE_IMAGES"));
    216 
    217 
     234    psF64 ci_t = psTimerClear("CLOSE_IMAGES");
     235    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "CLOSE_IMAGES", PS_META_REPLACE, "", ci_t);
     236
     237    psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", ci_t);
     238
     239#ifdef DO_REPLICATE
     240    psTimerStart("REPLICATE_OUTPUTS");
    218241    if (!replicateOutputs(sfiles)) {
    219         psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
     242        psErrorStackPrint(stderr, "failed to replicate output files");
    220243        deleteTemps(sfiles);
    221         psErrorStackPrint(stderr, "");
    222244        exit(PS_EXIT_UNKNOWN_ERROR);
    223245    }
     246    psF64 ro_t = psTimerClear("REPLICATE_OUTPUTS");
     247    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "REPLICATE_OUTPUTS", PS_META_REPLACE, "", ro_t);
     248#endif
    224249
    225250    // NOTE: from here on we can't just quit if something goes wrong.
     
    231256        //     swap the instances for the input and output
    232257        //     Note this is a nebulous database operation. No file I/O is performed
     258        psTimerStart("SWAP_INSTANCES");
    233259        if (!swapOutputsToInputs(sfiles)) {
    234             // XXX: Now what?
    235260            // It is up to the program that reverts failed destreak runs to insure that
    236             // any input files that have been swapped are restored and that the de-streaked
    237             // versions are deleted
     261            // any original non-destreaked input files that have been swapped are restored and that the de-streaked
     262            // versions are deleted.
    238263
    239264            psErrorStackPrint(stderr, "failed to swap files");
     
    241266            // XXX: pick a specific error code for this failure
    242267            exit(PS_EXIT_UNKNOWN_ERROR);
     268        }
     269        psF64 si_t = psTimerClear("SWAP_INSTANCES");
     270        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "SWAP_INSTANCES", PS_META_REPLACE, "", si_t);
     271    }
     272
     273    psF64 total_time = psTimerClear("TOTAL_TIME");
     274    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "TOTAL_TIME", PS_META_REPLACE, "", total_time);
     275    psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", total_time);
     276
     277    if (sfiles->statsFile) {
     278        const char *statsMDC = psMetadataConfigFormat(sfiles->stats);
     279        if (!statsMDC || strlen(statsMDC) == 0) {
     280            psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
     281        } else {
     282            fprintf(sfiles->statsFile, "%s", statsMDC);
     283            psFree(statsMDC);
     284            fclose(sfiles->statsFile);
     285            sfiles->statsFile = NULL;
     286            psFree(sfiles->stats);
     287            sfiles->stats = NULL;
    243288        }
    244289    }
     
    249294    pmConceptsDone();
    250295    pmModelClassCleanup();
    251     streaksNebulousCleanup(); 
     296    streaksNebulousCleanup();
    252297    pmConfigDone();
    253     psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", psTimerClear("STREAKSREMOVE"));
    254298    psLibFinalize();
    255299
     
    257301
    258302    return 0;
     303}
     304
     305static long
     306censorPixels(streakFiles *sfiles, psImage *pixels, bool checkNonDiffedPixels, psU16 maskStreak)
     307{
     308    long streakPixels = 0;
     309
     310    for (int y=0 ; y < sfiles->inImage->numRows; y++) {
     311        for (int x = 0; x < sfiles->inImage->numCols; x++) {
     312            if (psImageGet(pixels, x, y)) {
     313                if (!checkNonDiffedPixels || diffedPixel(sfiles, x, y)) {
     314                    ++streakPixels;
     315
     316                    excisePixel(sfiles, x, y, true, maskStreak);
     317
     318                } else {
     319                    // This pixel was not included in any warp and has thus already excised
     320                    // by exciseNonDiffedPixels
     321                }
     322            }
     323        }
     324    }
     325    return streakPixels;
    259326}
    260327
     
    268335    fprintf(stderr, "\t-weight WEIGHT.fits: weight file to de-streak\n");
    269336    fprintf(stderr, "\t-replace: replace the input images with the output\n");
    270     fprintf(stderr, "\t-keepnonwarped: do not exise pixels that were not part of difference processing\n");
     337    fprintf(stderr, "\t-keepnondiffed: do not exise pixels that were not part of difference processing\n");
    271338    fprintf(stderr, "\t-transparent val: instead of setting excicsed pixel to NAN add val\n");
    272339    fprintf(stderr, "\t-chip_mask MASK.fits: name of mask for chip stage (camera stage mask is passed tih -mask)\n");
     
    358425            true);
    359426    }
    360    
    361     if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) {
    362         psArgumentRemove(argnum, &argc, argv);
    363         psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_WARPED", 0,
    364             "skip excising of non warped pixels", true);
     427
     428    if ((argnum = psArgumentGet(argc, argv, "-keepnondiffed"))) {
     429        psArgumentRemove(argnum, &argc, argv);
     430        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_DIFFED", 0,
     431            "skip excising of non diffed pixels", true);
    365432    }
    366433
     
    372439        psArgumentRemove(argnum, &argc, argv);
    373440        double transparentStreaks = atof(argv[argnum]);
    374         psMetadataAddF64(config->arguments, PS_LIST_TAIL, "TRANSPARENT_STREAKS", 0,
     441        psMetadataAddF32(config->arguments, PS_LIST_TAIL, "TRANSPARENT_STREAKS", 0,
    375442            "value to adjust excised pixels", transparentStreaks);
    376443        psArgumentRemove(argnum, &argc, argv);
    377444    }
    378        
    379     // if skycells are not provided then we have to execise all pixels  unless -keepnonwarped
     445
     446    // if skycells are not provided then we have to execise all pixels  unless -keepnondiffed
    380447    pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist");
    381448
     
    443510    }
    444511
     512    if ((argnum = psArgumentGet(argc, argv, "-stats"))) {
     513        psArgumentRemove(argnum, &argc, argv);
     514        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "STATS", 0,
     515                "name of input stats file", argv[argnum]);
     516        psArgumentRemove(argnum, &argc, argv);
     517    }
     518
    445519    if ((argnum = psArgumentGet(argc, argv, "-tmproot"))) {
    446520        psArgumentRemove(argnum, &argc, argv);
     
    485559updateAstrometry(streakFiles *sf)
    486560{
    487     // XXX: why do I check this here? Shouldn't it be just around the call to linearizeTransforms?
    488561    if (sf->bilevelAstrometry) {
    489 
    490562        if (!linearizeTransforms(sf->astrom)) {
    491             // fit failed, leave the astrometry unchanged
     563            // fit failed, leave the transform in the file unchanged
    492564            return;
    493565        }
    494 
    495         if (!pmAstromWriteWCS(sf->outImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
    496             psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
    497             streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    498         }
    499         if (sf->outMask) {
    500             pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
    501         }
    502         if (sf->outWeight) {
    503             pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     566    }
     567    if (!pmAstromWriteWCS(sf->outImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
     568        psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
     569        streaksExit("", PS_EXIT_UNKNOWN_ERROR);
     570    }
     571    if (sf->outMask) {
     572        pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
     573    }
     574    if (sf->outWeight) {
     575        pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     576    }
     577}
     578
     579static void
     580setStreakBits(psImage *maskImage, psU32 maskStreak)
     581{
     582    for (int y=0 ; y < maskImage->numRows; y++) {
     583        for (int x=0 ; x < maskImage->numCols; x++) {
     584            maskImage->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskStreak;
    504585        }
    505586    }
     
    510591readAndCopyToOutput(streakFiles *sf, bool exciseAll)
    511592{
    512     bool    updateAstrometry = false;
    513593    if (sf->inImage->pmfile) {
    514594        // image data from pmFPAfile (diff or warp file)
     
    528608        streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    529609    }
    530     // For the chip level files, copy the WCS from the astrometry file to the header
    531     // XXX: do we want to do this for raw images as well?
    532     if (sf->stage == IPP_STAGE_CHIP) {
    533         if (!sf->bilevelAstrometry) {
    534             updateAstrometry = true;
    535             if (!pmAstromWriteWCS(sf->inImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
    536                 psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
    537                 streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    538             }
    539         }
    540     }
    541610    sf->outImage->header =  psMemIncrRefCounter(sf->inImage->header);
    542611    if (sf->recImage) {
     
    571640            }
    572641            addDestreakKeyword(sf->outMask->header);
    573             if (updateAstrometry) {
    574                 pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
    575             }
    576             setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
     642            // Note: we don't excise the mask pixels even if exciseAll is true.
     643            setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, false);
     644            if (exciseAll) {
     645                strkGetMaskValues(sf);
     646               
     647                // add the STREAK bit to the mask image pixels
     648                setStreakBits(sf->inMask->image, sf->maskStreak);
     649            }
    577650            if (sf->outChMask) {
    578651                sf->outChMask->header = (psMetadata *) psMemIncrRefCounter(sf->outMask->header);
     
    599672        }
    600673        addDestreakKeyword(sf->outWeight->header);
    601         if (updateAstrometry) {
    602             pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
    603         }
    604674        setupImageRefs(sf->outWeight, sf->recWeight, sf->inWeight, sf->extnum, exciseAll);
    605675
     
    624694    } else {
    625695        // we have an image cube
    626         double initValue;
    627696        if (exciseImageCube) {
    628697            // copy the entire input image to the recovery image
    629698            writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
    630             initValue = NAN;
    631699        } else {
    632             // otherwise write it to the output 
     700            // otherwise write it to the output
    633701            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    634             initValue = 0;
    635         }
    636 
    637         // borrow one of the images from the imagecube and set it to init value
    638         psImage *image = psArrayGet (sf->inImage->imagecube, 0);
    639         psMemIncrRefCounter(image);
    640         psImageInit(image, initValue);
     702        }
     703
     704        // Now deal with the other output image
    641705        if (exciseImageCube) {
    642             sf->outImage->image = image;
    643             writeImage(sf->outImage, extname, sf->extnum);
     706            // Set the values in the imagecube images to NAN and write them to the output image
     707            for (int i = 0; i < psArrayLength(sf->inImage->imagecube); i++) {
     708                psImage *image = psArrayGet (sf->inImage->imagecube, i);
     709                // XXX: NAN isn't right. It should be the integer equivalent. Should use exciseValue
     710                // but it isn't set with this code path. Fix that.
     711                psImageInit(image, 65535);
     712            }
     713            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    644714        } else {
    645             // write zero valued image to reccovery
    646715            if (sf->recImage) {
    647                 sf->recImage->image = image;
    648                 writeImage(sf->recImage, extname, sf->extnum);
     716                // Set the values in the imagecube images to zero
     717                for (int i = 0; i < psArrayLength(sf->inImage->imagecube); i++) {
     718                    psImage *image = psArrayGet (sf->inImage->imagecube, i);
     719                    psImageInit(image, 0);
     720                }
     721                // copy the entire zeroed image to the recovery image
     722                writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
    649723            }
    650724        }
     
    668742
    669743static void
    670 excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue)
     744excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue)
    671745{
    672746    double exciseValue = sfiles->inImage->exciseValue;
     
    677751    }
    678752
    679     double imageValue  = psImageGet (sfiles->inImage->image,  x, y);
    680     if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
    681         psImageSet (sfiles->recImage->image,  x, y, imageValue);
    682     }
    683 
    684     if (sfiles->transparentStreaks == 0) {
    685         psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     753    if (sfiles->inImage->image->type.type == PS_TYPE_U16) {
     754        psU16 imageValue  = sfiles->inImage->image->data.U16[y][x];
     755        if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
     756            sfiles->recImage->image->data.U16[y][x] = imageValue;
     757        }
     758
     759        if (sfiles->transparentStreaks == 0) {
     760            sfiles->outImage->image->data.U16[y][x] = exciseValue;
     761        } else {
     762            if (streak) {
     763                // as a visualization aid don't mask the pixel, just change the intensity
     764                sfiles->outImage->image->data.U16[y][x] = imageValue + sfiles->transparentStreaks;
     765            } else {
     766                sfiles->outImage->image->data.U16[y][x] = exciseValue;
     767            }
     768        }
    686769    } else {
    687         if (streak) {
    688             // as a visualization aid don't mask the pixel, just change the intensity
    689             psImageSet (sfiles->outImage->image,  x, y, imageValue + sfiles->transparentStreaks);
     770        float imageValue  = sfiles->inImage->image->data.F32[y][x];
     771        if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
     772            sfiles->recImage->image->data.F32[y][x] = imageValue;
     773        }
     774
     775        if (sfiles->transparentStreaks == 0) {
     776            sfiles->outImage->image->data.F32[y][x] = exciseValue;
    690777        } else {
    691             psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     778            if (streak) {
     779                // as a visualization aid don't mask the pixel, just change the intensity
     780                sfiles->outImage->image->data.F32[y][x] = imageValue + sfiles->transparentStreaks;
     781            } else {
     782                sfiles->outImage->image->data.F32[y][x] = exciseValue;
     783            }
    692784        }
    693785    }
     
    695787    if (sfiles->outWeight) {
    696788        if (sfiles->recWeight) {
    697             double weightValue = psImageGet (sfiles->inWeight->image, x, y);
    698             psImageSet (sfiles->recWeight->image, x, y, weightValue);
     789            sfiles->recWeight->image->data.F32[y][x] = sfiles->inWeight->image->data.F32[y][x];
    699790        }
    700791        // Assume that weight images are always a floating point type
    701         psImageSet (sfiles->outWeight->image, x, y, NAN);
     792        sfiles->outWeight->image->data.F32[y][x] = NAN;
    702793    }
    703794    if (sfiles->outMask) {
    704795        if (sfiles->recMask) {
    705             double maskValue   = psImageGet (sfiles->inMask->image,   x, y);
    706             psImageSet (sfiles->recMask->image,   x, y, maskValue);
    707         }
    708         psImageSet (sfiles->outMask->image,   x, y, newMaskValue);
    709     }
    710 }
    711 
    712 static void
    713 exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue)
     796            sfiles->recMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] =
     797                sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x];
     798        }
     799        sfiles->outMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= newMaskValue;
     800    }
     801}
     802
     803static long
     804exciseNonDiffedPixels(streakFiles *sfiles, psImageMaskType newMaskValue)
    714805{
    715806    int cell_x0 = sfiles->astrom->cell_x0;
     
    719810    int numCols = sfiles->inImage->numCols; // for raw images this was calculated from the width of datasec
    720811    int numRows = sfiles->inImage->numRows; // for raw images this was calculated from the height of datasec
     812
     813    long excisedPixels = 0;
    721814
    722815//    printf("%2d x0: %4d y0: %4d xpar: %d ypar: %d\n", sfiles->extnum, cell_x0, cell_y0, xParity, yParity);
     
    733826        }
    734827
    735         psU8 *pixels = sfiles->warpedPixels->data.U8[yChip];
     828        psU8 *pixels = sfiles->diffedPixels->data.U8[yChip];
    736829
    737830        if (xParity == 1) {
     
    740833                if (! *pixels ) {
    741834                    excisePixel(sfiles, xCell, yCell, false, newMaskValue);
     835                    excisedPixels++;
    742836                }
    743837            }
     
    749843                if (!*pixels) {
    750844                    excisePixel(sfiles, xCell, yCell, false, newMaskValue);
     845                    excisedPixels++;
    751846                }
    752847            }
    753848        }
    754849    }
     850    return excisedPixels;
    755851}
    756852
    757853static bool
    758 warpedPixel(streakFiles *sfiles, PixelPos *cellCoord)
     854diffedPixel(streakFiles *sfiles, int x, int y)
    759855{
    760856    PixelPos chipCoord;
    761857
    762858    if (!CHIP_LEVEL_INPUT(sfiles->stage)) {
    763         // if we're here on a skycell image by definition this pixel was warped
     859        // if we're here on a skycell image by definition this pixel was diffed
    764860        return true;
    765861    }
     
    767863    // we clip so that the streak calculation code doesn't have to
    768864    // clipping here insures that we don't touch the overscan regions
    769     if ((cellCoord->x < 0) || (cellCoord->x >= sfiles->inImage->numCols) ||
    770         (cellCoord->y < 0) || (cellCoord->y >= sfiles->inImage->numRows)) {
     865    if ((x < 0) || (x >= sfiles->inImage->numCols) ||
     866        (y < 0) || (y >= sfiles->inImage->numRows)) {
    771867        return false;
    772868    }
    773869
    774     cellToChipInt(&chipCoord.x, &chipCoord.y, sfiles->astrom, cellCoord->x, cellCoord->y);
    775 
    776     if (chipCoord.x < 0 || chipCoord.x >= sfiles->warpedPixels->numCols) {
     870    cellToChipInt(&chipCoord.x, &chipCoord.y, sfiles->astrom, x, y);
     871
     872    if (chipCoord.x < 0 || chipCoord.x >= sfiles->diffedPixels->numCols) {
    777873        return false;
    778874    }
    779     if (chipCoord.y < 0 || chipCoord.y >= sfiles->warpedPixels->numRows) {
     875    if (chipCoord.y < 0 || chipCoord.y >= sfiles->diffedPixels->numRows) {
    780876        return false;
    781877    }
    782878
    783     return psImageGet(sfiles->warpedPixels, chipCoord.x, chipCoord.y) ? true : false;
     879    return psImageGet(sfiles->diffedPixels, chipCoord.x, chipCoord.y) ? true : false;
    784880}
    785881
     
    787883// streak mask
    788884static void
    789 censorSources(streakFiles *sfiles, psU32 maskStreak)
     885censorSources(streakFiles *sfiles, psImageMaskType maskStreak)
    790886{
    791887    if ((!sfiles->inSources) || (!sfiles->outMask)) {
     
    801897    sFile *out = sfiles->outSources;
    802898
    803     in->header = psFitsReadHeader(NULL, in->fits);
    804     if (!in->header) {
    805         psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
     899
     900    // Primary header, should be "something.hdr"
     901    {
     902        psMetadata *header = psFitsReadHeader(NULL, in->fits);
     903        if (!header) {
     904            psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
     905            streaksExit("", PS_EXIT_DATA_ERROR);
     906        }
     907
     908        bool status;
     909        psString extname = psMetadataLookupStr(&status, header, "EXTNAME");
     910        if (!extname) {
     911            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
     912            streaksExit("", PS_EXIT_DATA_ERROR);
     913        }
     914        addDestreakKeyword(header);
     915
     916        if (!psFitsWriteBlank(out->fits, header, extname)) {
     917            psError(PS_ERR_IO, false, "failed to write blank in header of %s", in->resolved_name);
     918            streaksExit("", PS_EXIT_DATA_ERROR);
     919        }
     920        psFree(header);
     921    }
     922
     923    // Extension with PSF fits, should be "something.psf"
     924    {
     925        if (!psFitsMoveExtNum(in->fits, 1, true)) {
     926            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
     927            streaksExit("", PS_EXIT_DATA_ERROR);
     928        }
     929
     930        psMetadata *header = psFitsReadHeader(NULL, in->fits);
     931        if (!header) {
     932            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
     933            streaksExit("", PS_EXIT_DATA_ERROR);
     934        }
     935        psString extname = psMetadataLookupStr(NULL, header, "EXTNAME");
     936        if (!extname) {
     937            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
     938            streaksExit("", PS_EXIT_DATA_ERROR);
     939        }
     940
     941        psArray *inTable = psFitsReadTable(in->fits);
     942        if (!inTable->n) {
     943            psErrorStackPrint(stderr, "table in %s is empty", in->resolved_name);
     944            streaksExit("", PS_EXIT_DATA_ERROR);
     945        }
     946
     947        psArray *outTable = psArrayAllocEmpty(inTable->n);
     948        int j = 0;
     949        int numCensored = 0;
     950        for (int i = 0 ; i < inTable->n; i++) {
     951            psMetadata *row = inTable->data[i];
     952
     953            psF32 x = psMetadataLookupF32(NULL, row, "X_PSF");
     954            psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF");
     955
     956            psImageMaskType mask;
     957            if ((x >= maskImage->numCols) || (y >= maskImage->numRows) || (x <  0) || (y < 0)) {
     958                mask = maskStreak;
     959            } else {
     960                mask = maskImage->data.PS_TYPE_IMAGE_MASK_DATA[(int)y][(int)x];
     961            }
     962
     963            // Key the source if the center pixel is not masked with maskStreak
     964            if (!(mask & maskStreak) ) {
     965                psArraySet(outTable, j++, row);
     966            } else {
     967                numCensored++;
     968            }
     969        }
     970
     971        // get rid of unused elements (don't know if this is necessary)
     972        psArrayRealloc(outTable, j);
     973
     974        addDestreakKeyword(header);
     975        if (psArrayLength(outTable) > 0) {
     976            printf("Censored %d sources\n", numCensored);
     977            if (! psFitsWriteTable(out->fits, header, outTable, extname)) {
     978                psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
     979                streaksExit("", PS_EXIT_DATA_ERROR);
     980            }
     981        } else {
     982            printf("Censored ALL %d sources\n", numCensored);
     983            if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) {
     984                psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name);
     985                streaksExit("", PS_EXIT_DATA_ERROR);
     986            }
     987        }
     988        psFree(header);
     989        psFree(outTable);
     990        psFree(inTable);
     991    }
     992
     993    // XXX Will need to update to handle extension with extended sources, etc.
     994
     995    if (!psFitsClose(out->fits)) {
     996        psErrorStackPrint(stderr, "failed to close table %s", out->resolved_name);
    806997        streaksExit("", PS_EXIT_DATA_ERROR);
    807998    }
    808 
    809     bool status;
    810     psString extname = psMetadataLookupStr(&status, in->header, "EXTNAME");
    811     if (!extname) {
    812         psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
    813         streaksExit("", PS_EXIT_DATA_ERROR);
    814     }
    815 
    816     psArray *inTable = psFitsReadTable(in->fits);
    817     if (!inTable->n) {
    818         psError(PS_ERR_IO, false, "table in %s is empty", in->resolved_name);
    819         streaksExit("", PS_EXIT_DATA_ERROR);
    820     }
    821 
    822     psArray *outTable = psArrayAllocEmpty(inTable->n);
    823     int j = 0;
    824     int numCensored = 0;
    825     for (int i = 0 ; i < inTable->n; i++) {
    826         psMetadata *row = inTable->data[i];
    827 
    828         psF32 x = psMetadataLookupF32 (&status, row, "X_PSF");
    829         psF32 y = psMetadataLookupF32 (&status, row, "Y_PSF");
    830        
    831         psU32 mask = psImageGet(maskImage, x, y);
    832 
    833         // Key the source if the center pixel is not masked with maskStreak
    834         if (! (mask & maskStreak) ) {
    835             psArraySet(outTable, j++, row);
    836         } else {
    837             numCensored++;
    838         }
    839     }
    840 
    841     printf("Censored %d sources\n", numCensored);
    842 
    843     // get rid of unused elements (don't know if this is necessary)
    844     psArrayRealloc(outTable, j);
    845 
    846     addDestreakKeyword(in->header);
    847     if (! psFitsWriteTable(out->fits, in->header, outTable, extname)) {
    848         psError(PS_ERR_IO, false, "failed to write table to %s", out->resolved_name);
    849         streaksExit("", PS_EXIT_DATA_ERROR);
    850     }
    851 
    852     if (!psFitsClose(out->fits)) {
    853         psError(PS_ERR_IO, false, "failed to close table %s", out->resolved_name);
    854         streaksExit("", PS_EXIT_DATA_ERROR);
    855     }
    856 }
     999}
Note: See TracChangeset for help on using the changeset viewer.