IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 17, 2008, 2:44:52 PM (17 years ago)
Author:
bills
Message:

if no skycells files are provided, excise the entire image unless operating
at diff stage.
Don't force creation of recovery image unless stage is raw and -replace
was specified. Require recovery file if we're being asked to overwrite
a raw file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/magic/remove/src/streaksremove.c

    r20730 r20780  
    290290    char *inputBasename = basename(sf->inImage->name);
    291291    sf->outImage = sFileOpen(config, stage, "OUTPUT", inputBasename, true);
    292     sf->recImage = sFileOpen(config, stage, "RECOVERY", inputBasename, true);
     292    sf->recImage = sFileOpen(config, stage, "RECOVERY", inputBasename, false);
    293293
    294294    sf->inMask = sFileOpen(config, stage, "INPUT.MASK", NULL, false);
     
    490490    }
    491491   
    492    
    493     if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist")) { ;
    494         if (CHIP_LEVEL_INPUT(stage)) {
    495             psError(PS_ERR_UNKNOWN, true, "-skycelllist is required for raw and chip stages\n");
    496             return NULL;
    497         }
    498     }
     492    if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) {
     493        psArgumentRemove(argnum, &argc, argv);
     494        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_WARPED", 0,
     495            "skip excising of non warped pixels", true);
     496    }
     497       
     498    // if skycells are not provided then we have to execise all pixels  unless -keepnonwarped
     499    pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist");
    499500
    500501    if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "ASTROM", "-astrom", NULL)) { ;
     
    504505        }
    505506    }
     507
    506508    if ((argnum = psArgumentGet(argc, argv, "-mask"))) {
    507509        psArgumentRemove(argnum, &argc, argv);
     
    516518        psArgumentRemove(argnum, &argc, argv);
    517519    }
     520
    518521    if ((argnum = psArgumentGet(argc, argv, "-weight"))) {
    519522        psArgumentRemove(argnum, &argc, argv);
     
    528531        psArgumentRemove(argnum, &argc, argv);
    529532    }
     533
    530534    if ((argnum = psArgumentGet(argc, argv, "-tmproot"))) {
    531535        psArgumentRemove(argnum, &argc, argv);
     
    544548        return NULL;
    545549    }
     550
    546551    if ((argnum = psArgumentGet(argc, argv, "-recovery"))) {
    547552        psArgumentRemove(argnum, &argc, argv);
     
    550555            dir);
    551556        psArgumentRemove(argnum, &argc, argv);
    552     } else if (stage == IPP_STAGE_RAW) {
    553         psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw\n");
     557    } else if ((stage == IPP_STAGE_RAW) && gotReplace) {
     558        psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw with -replace\n");
    554559        return NULL;
    555560    }
     561
    556562    if ((argnum = psArgumentGet(argc, argv, "-remove"))) {
    557563        if (!gotReplace) {
     
    589595    }
    590596
    591    
    592597    // TODO: add keyword indicating that streaks have been removed
    593598    if (!psFitsWriteBlank(sfiles->outImage->fits, imageHeader, NULL)) {
     
    597602    }
    598603    // TODO: add keyword indicating that this is the recovery image
    599     if (!psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) {
     604    if (sfiles->recImage && !psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) {
    600605        psError(PS_ERR_IO, false, "failed to write primary header to %s",
    601606            sfiles->recImage->resolved_name);
     
    619624        }
    620625        // TODO: add keyword indicating that this is the recovery image
    621         if (!psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) {
     626        if (sfiles->recMask && !psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) {
    622627            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    623628                sfiles->recMask->resolved_name);
     
    641646        }
    642647        // TODO: add keyword indicating that this is a recovery image
    643         if (!psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) {
     648        if (sfiles->recWeight && !psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) {
    644649            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    645650                sfiles->recWeight->resolved_name);
     
    764769            streaksremoveExit("", PS_EXIT_DATA_ERROR);
    765770        }
    766         in->numRows = in->image->numRows;
    767771    }  else {
    768772        if (stage != IPP_STAGE_RAW) {
     
    784788setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero)
    785789{
     790    if (!sfile) {
     791        return;
     792    }
    786793    if (sfile->fits->options) {
    787794        psFree(sfile->fits->options);
     
    822829    }
    823830
    824 //    setFitsOptions(out, bitpix, bscale, bzero);
    825 //    setFitsOptions(rec, bitpix, bscale, bzero);
     831#ifdef COMPRESS_OUTPUT
     832    setFitsOptions(out, bitpix, bscale, bzero);
     833    setFitsOptions(rec, bitpix, bscale, bzero);
     834#endif
    826835}
    827836
     
    859868
    860869static void
    861 createRecoveryImage(sFile *rec, sFile *in, int extnum)
    862 {
    863     rec->image = psImageRecycle(rec->image, in->image->numCols, in->image->numRows, in->image->type.type);
    864     if (!rec->image) {
    865         psError(PS_ERR_UNKNOWN, false, "failed to allocate recoveyr image for extnsion %d", extnum);
     870createNewImage(sFile *out, sFile *in, int extnum, double initValue)
     871{
     872    out->image = psImageRecycle(out->image, in->image->numCols, in->image->numRows, in->image->type.type);
     873    if (!out->image) {
     874        psError(PS_ERR_UNKNOWN, false, "failed to allocate image for extnsion %d", extnum);
    866875        streaksremoveExit("", PS_EXIT_UNKNOWN_ERROR);
    867876    }
    868     psImageInit(rec->image, 0);
     877    psImageInit(out->image, initValue);
     878}
     879
     880static void
     881setupImageRefs(sFile *out, sFile *recovery, sFile *in, int extnum, bool exciseAll)
     882{
     883    if (exciseAll) {
     884        // set the output to an image all NANs
     885        createNewImage(out, in, extnum, NAN);
     886        if (recovery) {
     887            // save the whole input as the recovery image
     888            recovery->image = (psImage*) psMemIncrRefCounter(in->image);
     889        }
     890    } else {
     891        // output is the input image
     892        out->image = (psImage*) psMemIncrRefCounter(in->image);
     893        if (recovery) {
     894            // create a recovery image initialized to zero
     895            createNewImage(recovery, in, extnum, 0);
     896        }
     897    }
    869898}
    870899
    871900// set the image for the output files to the contents of the corresponding input file.
    872901static bool
    873 readAndCopyToOutput(streakFiles *sf)
     902readAndCopyToOutput(streakFiles *sf, bool exciseAll)
    874903{
    875904    bool    updateAstrometry = false;
     
    893922    if (sf->stage == IPP_STAGE_CHIP) {
    894923        // For the chip level files, copy the WCS from the astrometry file to the header
    895         updateAstrometry = true;
     924        // updateAstrometry = true;
    896925        if (!pmAstromWriteWCS(sf->inImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
    897926            psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
     
    900929    }
    901930    sf->outImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header);
    902     sf->recImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header);
     931    if (sf->recImage) {
     932        sf->recImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header);
     933    }
    903934
    904935    if (!SFILE_IS_IMAGE(sf->inImage)) {
     
    910941
    911942    if (sf->inImage->image) {
    912         sf->outImage->image = (psImage*) psMemIncrRefCounter(sf->inImage->image);
    913         createRecoveryImage(sf->recImage, sf->inImage, sf->extnum);
     943        setupImageRefs(sf->outImage, sf->recImage, sf->inImage, sf->extnum, exciseAll);
    914944    }  else {
    915         // we don't do a reference if we have an imagecube
     945        // Image cubes are handeled specially
    916946    }
    917947
    918948    // set up the compression parameters
    919     //  copyFitsOptions(sf->outImage, sf->recImage, sf->inImage);
     949#ifdef COMPRESS_OUTPUT
     950    // compression of the image pixels is disabled for now. Some consortium members
     951    // have problems reading them
     952    copyFitsOptions(sf->outImage, sf->recImage, sf->inImage);
    920953
    921954    // XXX: TODO: can we derive these values from the input header?
    922955    // psFitsCompressionGet(sf->inImage->image) gives compression none
    923956    // perhaps we should just use the definition of COMP_IMG in the configuration
    924     //    psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    925 
    926     psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
     957    psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
     958#endif
     959    if (sf->recImage) {
     960        psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
     961    }
    927962
    928963    if (sf->inMask) {
    929964        readImage(sf->inMask, sf->extnum, sf->stage);
    930         sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
    931965        if (updateAstrometry) {
    932966            pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
    933967        }
    934         sf->outMask->image = (psImage*) psMemIncrRefCounter(sf->inMask->image);
    935         sf->recMask->image = (psImage*) psMemIncrRefCounter(sf->inMask->image);
    936 
    937         createRecoveryImage(sf->recMask, sf->inMask, sf->extnum);
     968        sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
     969        if (sf->recMask) {
     970            sf->recMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
     971        }
     972
     973        setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
     974
     975        // XXX: see note above
    938976        copyFitsOptions(sf->outMask, sf->recMask, sf->inMask);
    939 
    940         // XXX: see note above
    941977        psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    942978        psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
     
    945981    if (sf->inWeight) {
    946982        readImage(sf->inWeight, sf->extnum, sf->stage);
     983        if (updateAstrometry) {
     984            pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     985        }
    947986        sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
    948         if (updateAstrometry) {
    949             pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
    950         }
    951         sf->outWeight->image = (psImage*) psMemIncrRefCounter(sf->inWeight->image);
    952 
    953         sf->recWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
    954         createRecoveryImage(sf->recWeight, sf->inWeight, sf->extnum);
     987        if (sf->recWeight) {
     988            sf->recWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
     989        }
     990        setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
    955991
    956992        copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight);
     
    9701006writeImage(sFile *sfile, psString extname, int extnum)
    9711007{
    972     if (!sfile->image) {
     1008    if (!sfile || !sfile->image) {
    9731009        return;
    9741010    }
     
    9871023writeImageCube(sFile *sfile, psArray *imagecube, psString extname, int extnum)
    9881024{
     1025    if (!imagecube) {
     1026        return;
     1027    }
    9891028    if (!psFitsWriteImageCube(sfile->fits, sfile->header, imagecube, extname)) {
    9901029        psError(PS_ERR_IO, false, "failed to write image to %s extnum: %d",
     
    9971036
    9981037static void
    999 writeImages(streakFiles *sf, bool exciseAll)
     1038writeImages(streakFiles *sf, bool exciseImageCube)
    10001039{
    10011040    psString extname = NULL;
     
    10051044    }
    10061045    if (sf->inImage->numZPlanes == 0)  {
     1046        // note exciseing complete images is handled in readAndCopyToOutput
    10071047        writeImage(sf->outImage, extname, sf->extnum);
    10081048        writeImage(sf->recImage, extname, sf->extnum);
    10091049    } else {
     1050        // we have an image cube
    10101051        double initValue;
    1011         if (exciseAll) {
     1052        if (exciseImageCube) {
     1053            // copy the entire input image to the recovery image
    10121054            writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
    10131055            initValue = NAN;
    10141056        } else {
     1057            // otherwise write it to the output
    10151058            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    10161059            initValue = 0;
    10171060        }
    10181061
    1019         // borrow one of the images from the imagecube and set it to NAN
     1062        // borrow one of the images from the imagecube and set it to init value
    10201063        psImage *image = psArrayGet (sf->inImage->imagecube, 0);
    10211064        psMemIncrRefCounter(image);
    10221065        psImageInit(image, initValue);
    1023         if (exciseAll) {
     1066        if (exciseImageCube) {
    10241067            sf->outImage->image = image;
    10251068            writeImage(sf->outImage, extname, sf->extnum);
    10261069        } else {
    1027             sf->recImage->image = image;
    1028             writeImage(sf->recImage, extname, sf->extnum);
    1029         }
    1030         // Assumption there are no mask and weight images for video cells
     1070            // write zero valued image to reccovery
     1071            if (sf->recImage) {
     1072                sf->recImage->image = image;
     1073                writeImage(sf->recImage, extname, sf->extnum);
     1074            }
     1075        }
     1076        // Assumption: there are no mask and weight images for video cells
    10311077        return;
    10321078    }
     
    10441090closeImage(sFile *sfile)
    10451091{
    1046     if (!sfile->fits) {
     1092    if (!sfile) {
    10471093        return;
    10481094    }
    1049     if (!psFitsClose(sfile->fits)) {
     1095    if (sfile->fits && !psFitsClose(sfile->fits)) {
    10501096        psError(PS_ERR_IO, false, "failed to close image to %s",
    10511097            sfile->resolved_name);
     
    12531299
    12541300    double imageValue  = psImageGet (sfiles->inImage->image,  x, y);
    1255     psImageSet (sfiles->recImage->image,  x, y, imageValue);
     1301    if (sfiles->recImage) {
     1302        psImageSet (sfiles->recImage->image,  x, y, imageValue);
     1303    }
    12561304
    12571305#define ACTUALLY_MASK_PIXEL
     
    14071455}
    14081456
     1457static StreakPixels *
     1458getStreakPixels(streakFiles *sfiles, Streaks *streaks)
     1459{
     1460    StreakPixels *pixels;
     1461#ifdef notyet
     1462    Streaks *componentStreaks = NULL;
     1463    Streaks *streaksToProcess;
     1464
     1465    bool only_component_streaks = false; // XXX flesh this out and make it an option
     1466    if (only_component_streaks) {
     1467        componentStreaks = restrictStreaksToComponent(sfiles, streaks);
     1468        streaksToProcess = componentStreaks;
     1469    } else {
     1470        streaksToProcess = streaks;
     1471    }
     1472
     1473    pixels = streak_on_component (streaksToProcess, sfiles->astrom,
     1474                                  sfiles->inImage->numCols, sfiles->inImage->numRows);
     1475    if (componentStreaks) {
     1476        psFree(componentStreaks);
     1477    }
     1478#else
     1479        pixels = streak_on_component (streaks, sfiles->astrom,
     1480                                      sfiles->inImage->numCols, sfiles->inImage->numRows);
     1481#endif
     1482    return pixels;
     1483}
    14091484int
    14101485main(int argc, char *argv[])
     
    14331508    streakFiles *sfiles = openFiles(config);
    14341509
     1510    bool exciseAll = false;
     1511    // --keepnonwarped is a test and debug mode
    14351512    bool status;
    1436     bool checkWarpedPixels = ! psMetadataLookupBool(&status, config->arguments, "INCLUDE_NON_WARPED");
    1437     if (checkWarpedPixels) {
     1513    bool keepNonWarpedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_WARPED");
     1514
     1515    // we need to check for non warped pixels unless we've been asked not to or the stage is diff
     1516    // (By definition pixels in diff images were included in a diff)
     1517    bool checkNonWarpedPixels = ! (keepNonWarpedPixels || (sfiles->stage == IPP_STAGE_DIFF) );
     1518
     1519    if (checkNonWarpedPixels ) {
    14381520        // From ICD:
    14391521        // In the raw and detrended images, the pixels which were not
     
    14411523        // Note that the warp and difference skycells are only generated if more
    14421524        // than a small fraction of the pixels are lit by the input image.
    1443 
    1444         computeWarpedPixels(sfiles);
     1525        // if no skycells are provided sfiles->exciseAll is set to true
     1526
     1527        if (! computeWarpedPixels(sfiles) ) {
     1528            // we have no choice to excise all pixels
     1529            exciseAll = true;
     1530        }
    14451531    }
    14461532   
     
    14571543    }
    14581544
    1459     // Iterate through components of image
     1545    // Iterate through components of input files
    14601546    do {
    1461         bool exciseAll = false;
    1462 
    1463         // read the images and copy the pixels image from the inputs to the outputs
    1464         // This also sets up the astrometry and initializes the recovery images
    1465         if (!readAndCopyToOutput(sfiles)) {
    1466             // this extension is not an image skip (video descriptor?)
    1467             // XXX is there anything else that could be in an input file that we need to handle?
     1547        bool exciseImageCube = false;
     1548
     1549        // read the images and copy the data from the inputs to the outputs
     1550        // This also sets up the astrometry and initializes the recovery image(s)
     1551
     1552        if (!readAndCopyToOutput(sfiles, exciseAll)) {
     1553            // false return value indicates that this extension is not an image and
     1554            // the contents has already been copied to the output file.
     1555            // we don't need to process this extesion for streaks.
    14681556            continue;
    14691557        }
    14701558
    1471         // Identify pixels to mask because of streaks
    1472 
    1473 #ifdef notyet
    1474         Streaks *componentStreaks = NULL;
    1475         Streaks *streaksToProcess;
    1476 
    1477         bool only_component_streaks = false; // XXX flesh this out and make it an option
    1478         if (only_component_streaks) {
    1479             componentStreaks = restrictStreaksToComponent(sfiles, streaks);
    1480             streaksToProcess = componentStreaks;
    1481         } else {
    1482             streaksToProcess = streaks;
    1483         }
    1484 
    1485         pixels = streak_on_component (streaksToProcess, sfiles->astrom,
    1486                                       sfiles->inImage->numCols, sfiles->inImage->numRows);
    1487         if (componentStreaks) {
    1488             psFree(componentStreaks);
    1489         }
    1490 #else
    1491         pixels = streak_on_component (streaks, sfiles->astrom,
    1492                                       sfiles->inImage->numCols, sfiles->inImage->numRows);
    1493 #endif
    1494         // XXX:
    1495         //
    1496         // PFS: Need to get mask weight for PM_MASK_DETECTOR.  Is this stored
    1497         // in a config somewhere?  Not sure how to properly set the maskValue.
    1498         // WES just pick some numbers
    1499 
    1500         if (checkWarpedPixels) {
    1501             exciseNonWarpedPixels(sfiles, MASK_BAD_WARP);
    1502         }
    1503 
    1504         if (sfiles->inImage->image) {
    1505             for (i = 0; i < psArrayLength (pixels); ++i) {
    1506                 pixelPos = psArrayGet (pixels, i);
    1507 
    1508                 if (!checkWarpedPixels || warpedPixel(sfiles, pixelPos)) {
    1509 
    1510                     excisePixel(sfiles, pixelPos->x, pixelPos->y, MASK_STREAK);
    1511 
    1512                 } else {
    1513                     // This pixel was not included in any warp and has thus already excised above
     1559        if (!exciseAll) {
     1560            // Identify pixels to mask because of streaks
     1561
     1562            pixels = getStreakPixels(sfiles, streaks);
     1563
     1564            // XXX:
     1565            //
     1566            // PFS: Need to get mask weight for PM_MASK_DETECTOR.  Is this stored
     1567            // in a config somewhere?  Not sure how to properly set the maskValue.
     1568
     1569
     1570            if (checkNonWarpedPixels) {
     1571                exciseNonWarpedPixels(sfiles, MASK_BAD_WARP);
     1572            }
     1573           
     1574            if (sfiles->inImage->image) {
     1575                for (i = 0; i < psArrayLength (pixels); ++i) {
     1576                    pixelPos = psArrayGet (pixels, i);
     1577
     1578                    if (!checkNonWarpedPixels || warpedPixel(sfiles, pixelPos)) {
     1579
     1580                        excisePixel(sfiles, pixelPos->x, pixelPos->y, MASK_STREAK);
     1581
     1582                    } else {
     1583                        // This pixel was not included in any warp and has thus already excised above
     1584                    }
    15141585                }
     1586            } else {
     1587                // this component contains an image cube, excise it completely
     1588                exciseImageCube = true;
    15151589            }
    1516         } else {
    1517             // This video cell is touched by a streak excise
    1518             // For now we excise all video cells.
    1519             // if (psArrayLength(pixels))
    1520             exciseAll = true;
    1521         }
    1522         psArrayElementsFree (pixels);
     1590            psArrayElementsFree (pixels);
     1591        }
    15231592
    15241593        // write out the destreaked temporary images and the recovery images
    1525         writeImages(sfiles, exciseAll);
     1594        writeImages(sfiles, exciseImageCube);
    15261595
    15271596    } while (streakFilesNextExtension(sfiles));
Note: See TracChangeset for help on using the changeset viewer.