IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 23, 2009, 3:05:43 PM (17 years ago)
Author:
bills
Message:

Fixes to destreaking of raw images

Location:
trunk/magic/remove/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/magic/remove/src

    • Property svn:ignore
      •  

        old new  
        22streaksreplace
        33streakscompare
         4streaksrelease
  • trunk/magic/remove/src/streaksremove.c

    r21085 r21156  
    99static void writeImages(streakFiles *sf, bool exciseImageCube);
    1010static bool replicateOutputs(streakFiles *sfiles);
     11static void updateAstrometry(streakFiles *sfiles);
    1112
    1213int
     
    2122    if (!config) {
    2223        psError(PS_ERR_UNKNOWN, false, "failed to parse arguments\n");
    23         return PS_EXIT_CONFIG_ERROR;
     24        streaksExit("", PS_EXIT_CONFIG_ERROR);
    2425    }
    2526
     
    2728    if (!status) {
    2829        psError(PM_ERR_CONFIG, false, "failed to lookup MASKS in recipes\n");
    29         return PS_EXIT_CONFIG_ERROR;
     30        streaksExit("", PS_EXIT_CONFIG_ERROR);
    3031    }
    3132    double maskStreak = (double) psMetadataLookupU8(&status, masks, "STREAK");
    3233    if (!status) {
    3334        psError(PM_ERR_CONFIG, false, "failed to lookup mask value for STREAK in recipes\n");
    34         return PS_EXIT_CONFIG_ERROR;
    35     }
     35        streaksExit("", PS_EXIT_CONFIG_ERROR);
     36    }
     37
     38    // optionally setting pixels with any mask bits execpt POOR.WARP to NAN
     39    psU8 poorWarp = (double) psMetadataLookupU8(&status, masks, "POOR.WARP");
     40    if (!status) {
     41        psError(PM_ERR_CONFIG, false, "failed to lookup mask value for POOR.WARP in recipes\n");
     42        streaksExit("", PS_EXIT_CONFIG_ERROR);
     43    }
     44    psU8 maskMask = ~poorWarp;
    3645
    3746    psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS");
     
    4049    if (!streaks) {
    4150        psErrorStackPrint(stderr, "failed to read streaks file: %s", streaksFileName);
    42         exit (PS_EXIT_PROG_ERROR);
     51        streaksExit("", PS_EXIT_PROG_ERROR);
    4352    }
    4453
    4554    streakFiles *sfiles = openFiles(config, true);
    4655    setupAstrometry(sfiles);
     56
     57    bool nanForRelease = psMetadataLookupBool(&status, config->arguments, "NAN_FOR_RELEASE");
     58    if (nanForRelease && (sfiles->inMask == NULL)) {
     59        psError(PS_ERR_UNKNOWN, true, "mask image must be supplied with -release.");
     60        streaksExit("", PS_EXIT_PROG_ERROR);
     61    }
    4762
    4863    bool exciseAll = false;
     
    113128            printf("time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS"));
    114129
    115             if (checkNonWarpedPixels) {
    116                 psTimerStart("EXCISE_NON_WARPED");
    117 
    118                 exciseNonWarpedPixels(sfiles, maskStreak);
    119 
    120                 printf("time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED"));
    121             }
    122130           
    123131            if (sfiles->inImage->image) {
     132                if (checkNonWarpedPixels) {
     133                    psTimerStart("EXCISE_NON_WARPED");
     134
     135                    exciseNonWarpedPixels(sfiles, maskStreak);
     136
     137                    printf("time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED"));
     138                }
    124139                totalStreakPixels +=  psArrayLength(pixels);
    125140                psTimerStart("REMOVE_STREAKS");
     
    137152                }
    138153                printf("time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS"));
     154
     155                if (nanForRelease) {
     156                    setMaskedToNAN(sfiles, maskMask, true);
     157                }
     158
    139159            } else {
    140160                // this component contains an image cube, excise it completely
     
    142162            }
    143163            psArrayElementsFree (pixels);
     164        }
     165
     166        if (sfiles->stage == IPP_STAGE_CHIP) {
     167        // if (CHIP_LEVEL_INPUT(sfiles->stage)) {
     168            updateAstrometry(sfiles);
    144169        }
    145170
     
    302327            true);
    303328    }
     329
     330    if ((argnum = psArgumentGet(argc, argv, "-release"))) {
     331        psArgumentRemove(argnum, &argc, argv);
     332        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "NAN_FOR_RELEASE", 0, "set masked pixels to NAN",
     333            true);
     334    }
    304335   
    305336    if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) {
     
    424455
    425456    return config;
     457}
     458
     459static void
     460updateAstrometry(streakFiles *sf)
     461{
     462    if (sf->bilevelAstrometry) {
     463        linearizeTransforms(sf->astrom);
     464        // XXX: pmAstrometry.c should set the correct CTYPE values
     465        if (!pmAstromWriteWCS(sf->outImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
     466            psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
     467            streaksExit("", PS_EXIT_UNKNOWN_ERROR);
     468        }
     469        if (sf->outMask) {
     470            pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
     471        }
     472        if (sf->outWeight) {
     473            pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     474        }
     475    }
    426476}
    427477
     
    438488    } else {
    439489        // image data directly from psFits
    440         readImage(sf->inImage, sf->extnum, sf->stage);
     490        readImage(sf->inImage, sf->extnum, sf->stage, false);
    441491        if (SFILE_IS_IMAGE(sf->inImage)) {
    442492            sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa, sf->chip, false,
     
    493543
    494544    if (sf->inMask) {
    495         readImage(sf->inMask, sf->extnum, sf->stage);
    496         sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
    497         if (sf->recMask) {
    498             sf->recMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
    499         }
    500         if (updateAstrometry) {
    501             pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
    502         }
    503         setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
    504         if (sf->outChMask) {
    505             sf->outChMask->header = (psMetadata *) psMemIncrRefCounter(sf->outMask->header);
    506             sf->outChMask->image = (psImage *) psMemIncrRefCounter(sf->outMask->image);
    507             if (sf->recChMask) {
    508                 sf->recChMask->header = (psMetadata *) psMemIncrRefCounter(sf->recMask->header);
    509                 sf->recChMask->image = (psImage *) psMemIncrRefCounter(sf->recMask->image);
    510             }
    511         }
     545        readImage(sf->inMask, sf->extnum, sf->stage, true);
     546        if (sf->outMask) {
     547            sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
     548            if (sf->recMask) {
     549                sf->recMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
     550            }
     551            if (updateAstrometry) {
     552                pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
     553            }
     554            setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
     555            if (sf->outChMask) {
     556                sf->outChMask->header = (psMetadata *) psMemIncrRefCounter(sf->outMask->header);
     557                sf->outChMask->image = (psImage *) psMemIncrRefCounter(sf->outMask->image);
     558                if (sf->recChMask) {
     559                    sf->recChMask->header = (psMetadata *) psMemIncrRefCounter(sf->recMask->header);
     560                    sf->recChMask->image = (psImage *) psMemIncrRefCounter(sf->recMask->image);
     561                }
     562            }
    512563
    513564#ifdef STREAKS_COMPRESS_OUTPUT
    514         // XXX: see note above
    515         copyFitsOptions(sf->outMask, sf->recMask, sf->inMask);
    516         psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    517         if (sf->recMask) {
    518             psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    519         }
    520         if (sf->outChMask) {
    521             copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask);
    522             psFitsSetCompression(sf->outChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    523             if (sf->recChMask) {
    524                 psFitsSetCompression(sf->recChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    525             }
    526         }
     565            // XXX: see note above
     566            copyFitsOptions(sf->outMask, sf->recMask, sf->inMask);
     567            psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
     568            if (sf->recMask) {
     569                psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
     570            }
     571            if (sf->outChMask) {
     572                copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask);
     573                psFitsSetCompression(sf->outChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
     574                if (sf->recChMask) {
     575                    psFitsSetCompression(sf->recChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
     576                }
     577            }
    527578#endif
     579        }
    528580    }
    529581
    530582    if (sf->inWeight) {
    531         readImage(sf->inWeight, sf->extnum, sf->stage);
     583        readImage(sf->inWeight, sf->extnum, sf->stage, false);
    532584        sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
    533585        if (sf->recWeight) {
     
    661713excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue)
    662714{
     715    double exciseValue = sfiles->inImage->exciseValue;
     716
    663717    // we clip so that the streak calculation code doesn't have to
    664718    if ((x < 0) || (x >= sfiles->inImage->numCols) || (y < 0) || (y >= sfiles->inImage->numRows)) {
     
    667721
    668722    double imageValue  = psImageGet (sfiles->inImage->image,  x, y);
    669     if (sfiles->recImage && !isnan(imageValue) ) {
     723    if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
    670724        psImageSet (sfiles->recImage->image,  x, y, imageValue);
    671725    }
    672726
    673727    if (sfiles->transparentStreaks == 0) {
    674         psImageSet (sfiles->outImage->image,  x, y, NAN);
     728        psImageSet (sfiles->outImage->image,  x, y, exciseValue);
    675729    } else {
    676730        if (streak) {
     
    678732            psImageSet (sfiles->outImage->image,  x, y, imageValue + sfiles->transparentStreaks);
    679733        } else {
    680             psImageSet (sfiles->outImage->image,  x, y, NAN);
    681         }
    682     }
    683 
    684     if (sfiles->inWeight) {
     734            psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     735        }
     736    }
     737
     738    if (sfiles->outWeight) {
    685739        if (sfiles->recWeight) {
    686740            double weightValue = psImageGet (sfiles->inWeight->image, x, y);
    687741            psImageSet (sfiles->recWeight->image, x, y, weightValue);
    688742        }
     743        // Assume that weight images are always a floating point type
    689744        psImageSet (sfiles->outWeight->image, x, y, NAN);
    690745    }
    691     if (sfiles->inMask) {
     746    if (sfiles->outMask) {
    692747        if (sfiles->recMask) {
    693748            double maskValue   = psImageGet (sfiles->inMask->image,   x, y);
     
    705760    int xParity = sfiles->astrom->xParity;
    706761    int yParity = sfiles->astrom->yParity;
    707     int numCols = sfiles->inImage->numCols;
    708     int numRows = sfiles->inImage->numRows;
     762    int numCols = sfiles->inImage->numCols; // for raw images this was calculated from the width of datasec
     763    int numRows = sfiles->inImage->numRows; // for raw images this was calculated from the height of datasec
    709764
    710765//    printf("%2d x0: %4d y0: %4d xpar: %d ypar: %d\n", sfiles->extnum, cell_x0, cell_y0, xParity, yParity);
    711766
     767    // XXX: in the case of raw images we are making an implicit assumption here
     768    // that the DATASEC begins at (x, y) = (0, 0) in the array for this image
     769    // While this is true for GPC1 it isn't necessarily true
    712770    for (int yCell = 0; yCell < numRows; yCell++) {
    713771        int yChip;
     
    751809
    752810    // we clip so that the streak calculation code doesn't have to
     811    // clipping here insures that we don't touch the overscan regions
    753812    if ((cellCoord->x < 0) || (cellCoord->x >= sfiles->inImage->numCols) ||
    754813        (cellCoord->y < 0) || (cellCoord->y >= sfiles->inImage->numRows)) {
     
    756815    }
    757816
    758     cellToChip(&chipCoord, sfiles->astrom, cellCoord);
    759 
    760 #ifdef notdef
    761     // Shouldn't need this but for raw images I'm getting
    762     // x = warpedPixels->numCols for cell xy70 when cell.x = 0
    763     // Perhaps cell_x0 should be IMNPIX-1 since IMNPIX is in the 1 based system of the fits
    764     // headers
     817    cellToChipInt(&chipCoord.x, &chipCoord.y, sfiles->astrom, cellCoord->x, cellCoord->y);
     818
    765819    if (chipCoord.x < 0 || chipCoord.x >= sfiles->warpedPixels->numCols) {
    766820        return false;
     
    769823        return false;
    770824    }
    771 #endif
    772825
    773826    return psImageGet(sfiles->warpedPixels, chipCoord.x, chipCoord.y) ? true : false;
Note: See TracChangeset for help on using the changeset viewer.