IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 25, 2009, 2:00:56 PM (17 years ago)
Author:
eugene
Message:

merging changes from head

Location:
branches/eam_branches/20090522
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090522

  • branches/eam_branches/20090522/magic/remove/src

    • Property svn:ignore
      •  

        old new  
        33streakscompare
        44streaksrelease
         5makefile
  • branches/eam_branches/20090522/magic/remove/src/streaksremove.c

    r23965 r24557  
     1/*
     2 * streaksremove
     3 *
     4 * Convert satellite streak detctions into masks and remove the covered pixels from the
     5 * input images.
     6 * Optionally swap the inputs with the outputs so that subsequent references to the original
     7 * images access the destreaked versions.
     8 */
     9
    110#include "streaksremove.h"
    211
     
    2332    }
    2433
    25     psU32 maskStreak = 0;
    26     psU32 maskMask = 0;
     34    // Values to set for masked pixels
     35    psU32 maskStreak = 0;           // for the image and weight (usually NAN, MAXINT for integer images)
     36    psU32 maskMask = 0;             // value looked up for MASK.STREAK
    2737
    2838    psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS");
    2939
     40    // call Paul Sydney's code to parse the streaks file that DetectStreaks produced
    3041    Streaks *streaks = readStreaksFile(streaksFileName);
    3142    if (!streaks) {
    32         psErrorStackPrint(stderr, "failed to read streaks file: %s", streaksFileName);
     43        psError(PS_ERR_UNKNOWN, "failed to read streaks file: %s", streaksFileName);
    3344        streaksExit("", PS_EXIT_PROG_ERROR);
    3445    }
    3546
     47    // open all of the input and output files, save their descriptions in the streakFiles struct
    3648    streakFiles *sfiles = openFiles(config, true, argv[0]);
    3749    setupAstrometry(sfiles);
    3850
     51    // Optionally we can set pixels that are masked to NAN since they couldn't have been
     52    // examined for streaks. Usually this is done by the distribution system just prior
     53    // to release
    3954    bool nanForRelease = psMetadataLookupBool(&status, config->arguments, "NAN_FOR_RELEASE");
    4055    if (nanForRelease && (sfiles->inMask == NULL)) {
     
    5267
    5368    if (checkNonWarpedPixels ) {
    54         // From ICD:
     69        // From magic ICD:
    5570        // In the raw and detrended images, the pixels which were not
    5671        // included in any of the streak-processed warps must also be masked.
     
    6984   
    7085    if (sfiles->stage == IPP_STAGE_RAW) {
    71         // copy PHU to output files
     86        // Except for raw stage, all of our (GPC1) files have one image extension.
     87        // Raw files have a phu and multiple extensions, one per chip
     88        // Since this is a raw file, copy it's PHU to output files
    7289        copyPHU(sfiles, true);
    7390
     
    8299    int totalStreakPixels = 0;
    83100
    84     // Iterate through each component of the input (there is only one except for raw images)
     101    // Iterate through each component of the input (except for raw images there is only one)
    85102    do {
    86103        bool exciseImageCube = false;
     
    110127            psTimerStart("GET_STREAK_PIXELS");
    111128
    112             StreakPixels *pixels = streak_on_component (streaks, sfiles->astrom,
    113                                       sfiles->inImage->numCols, sfiles->inImage->numRows);
    114 
     129            // call Paul Sydney's code to compute the set of pixels that are covered by the detected streaks
     130            StreakPixels *pixels = streak_on_component(streaks, sfiles->astrom, sfiles->inImage->numCols,
     131                                                        sfiles->inImage->numRows);
    115132            psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS"));
    116 
    117133           
     134            // if this extension contained an image, excise the streaked pixels.
     135            // otherwise it contained an image cube (video cell) which is handled in the if block
    118136            if (sfiles->inImage->image) {
    119137                if (checkNonWarpedPixels) {
     
    126144                    psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED"));
    127145                }
     146
    128147                totalStreakPixels +=  psArrayLength(pixels);
     148
    129149                psTimerStart("REMOVE_STREAKS");
     150
     151                // for each pixel covered by the streak
    130152                for (int i = 0; i < psArrayLength (pixels); ++i) {
    131153                    PixelPos *pixelPos = psArrayGet (pixels, i);
    132154
     155                    // if this pixel was not part of the warp, skip because it has already been
     156                    // excised (unless we weren't asked to check)
    133157                    if (!checkNonWarpedPixels || warpedPixel(sfiles, pixelPos)) {
    134158
     
    140164                    }
    141165                }
     166
    142167                psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS"));
    143168
    144169                if (nanForRelease) {
     170                    // set any pixels that were masked, to NAN (unless they are already NAN)
    145171                    setMaskedToNAN(sfiles, maskMask, true);
    146172                }
    147173
    148174            } else {
    149                 // this component contains an image cube, excise it completely
     175                // this component contains an image cube
     176                // For now excise it completely
    150177                exciseImageCube = true;
    151178            }
     
    155182
    156183        if (sfiles->stage == IPP_STAGE_CHIP) {
     184            // as a convience to the user of the output, replace the bogus WCS transform in the
     185            // chip processed files with the data calcuated by psastro at the camera stage
     186            // (actually we use a linear approximation)
    157187            updateAstrometry(sfiles);
    158188        }
    159189
    160         // write out the destreaked temporary images and the recovery images
     190        // write the destreaked "temporary" images and the recovery images
    161191        writeImages(sfiles, exciseImageCube);
    162192
     
    168198    psLogMsg("streaksremove", PS_LOG_INFO, "pixels: %ld streak pixels: %ld %4.2f%%\n", totalPixels, totalStreakPixels, 100. * totalStreakPixels / totalPixels);
    169199
     200    // all done close the files. This is where the files are written so it can take a long time.
     201
    170202    psTimerStart("CLOSE_IMAGES");
    171     // close all files
     203
    172204    closeImages(sfiles);
     205
    173206    psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", psTimerClear("CLOSE_IMAGES"));
     207
     208
     209    if (!replicateOutputs(sfiles)) {
     210        psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
     211        deleteTemps(sfiles);
     212        psErrorStackPrint(stderr, "");
     213        exit(PS_EXIT_UNKNOWN_ERROR);
     214    }
    174215
    175216    // NOTE: from here on we can't just quit if something goes wrong.
    176217    // especially if we're working at the raw stage
    177 
    178     if (!replicateOutputs(sfiles)) {
    179         psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
    180         psErrorStackPrint(stderr, "");
    181         exit(PS_EXIT_UNKNOWN_ERROR);
    182     }
     218    // turn off automatic deletion of output files by streaksExit
     219    setStreakFiles(NULL);
    183220
    184221    if (psMetadataLookupBool(&status, config->arguments, "REPLACE")) {
    185222        //     swap the instances for the input and output
    186         //     Note this is a database operation. No file I/O is performed
     223        //     Note this is a nebulous database operation. No file I/O is performed
    187224        if (!swapOutputsToInputs(sfiles)) {
    188             psError(PS_ERR_UNKNOWN, false, "failed to swap files");
    189 
    190             // XXX: Now what? I guess swapOutputsToInputs will need to undo anything that
    191             // it has done and give a detailed report of what happened
    192 
    193             psErrorStackPrint(stderr, "");
     225            // XXX: Now what?
     226            // It is up to the program that reverts failed destreak runs to insure that
     227            // any input files that have been swapped are restored
     228
     229            psErrorStackPrint(stderr, "failed to swap files");
     230
     231            // XXX: pick a specific error code for this failure
    194232            exit(PS_EXIT_UNKNOWN_ERROR);
    195233        }
    196 
    197         if (psMetadataLookupBool(&status, config->arguments, "REMOVE")) {
    198             //      delete the temporary storage objects (which now points to the original image(s)
    199             if (!deleteTemps(sfiles)) {
    200                 psError(PS_ERR_UNKNOWN, false, "failed to delete temporary files");
    201                 // XXX: Now what? At this point the output files have been swapped, so we can't
    202                 // repeat the operation.
    203 
    204                 // Returning error status here is problematic. The inputs have been streak removed
    205                 // but they're still lying around
    206                 // Maybe just print an error message and
    207                 // let other system tools clean up
    208                 psErrorStackPrint(stderr, "");
    209                 exit(PS_EXIT_UNKNOWN_ERROR);
    210             }
    211         }
    212     }
     234    }
     235    // all done. Clean up to look for memory leaks.
    213236
    214237    psFree(sfiles);
     
    403426            usage();
    404427        }
    405         psString dir = pathToDirectory(argv[argnum]);
    406         psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "directory for temporary files",
    407             dir);
    408         psFree(dir);
     428        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "path for (temporary if replae) output files",
     429            argv[argnum]);
    409430        psArgumentRemove(argnum, &argc, argv);
    410431    } else {
     
    415436    if ((argnum = psArgumentGet(argc, argv, "-recovery"))) {
    416437        psArgumentRemove(argnum, &argc, argv);
    417         psString dir = pathToDirectory(argv[argnum]);
    418         psMetadataAddStr(config->arguments, PS_LIST_TAIL, "RECOVERY", 0, "directory for recovery files",
    419             dir);
    420         psFree(dir);
     438        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "RECOVERY", 0, "path for recovery files",
     439            argv[argnum]);
    421440        psArgumentRemove(argnum, &argc, argv);
    422441    } else if ((stage == IPP_STAGE_RAW) && gotReplace) {
    423442        psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw with -replace\n");
    424443        usage();
    425     }
    426 
    427     if ((argnum = psArgumentGet(argc, argv, "-remove"))) {
    428         if (!gotReplace) {
    429             psError(PS_ERR_UNKNOWN, true, "-replace is required with -remove\n");
    430             usage();
    431         }
    432         psArgumentRemove(argnum, &argc, argv);
    433         psMetadataAddBool(config->arguments, PS_LIST_TAIL, "REMOVE", 0, "remove original files",
    434             true);
    435444    }
    436445
     
    451460updateAstrometry(streakFiles *sf)
    452461{
     462    // XXX: why do I check this here? Shouldn't it be just around the call to linearizeTransforms?
    453463    if (sf->bilevelAstrometry) {
    454464
     
    501511        }
    502512    }
    503     sf->outImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header);
     513    sf->outImage->header = psMemIncrRefCounter(sf->inImage->header);
    504514    if (sf->recImage) {
    505         sf->recImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header);
    506     }
     515        sf->recImage->header = psMetadataCopy(NULL, sf->inImage->header);
     516        addRecoveryKeyword(sf->recImage->header);
     517    }
     518    addDestreakKeyword(sf->outImage->header);
    507519
    508520    if (!SFILE_IS_IMAGE(sf->inImage)) {
     
    520532
    521533    // set up the compression parameters
    522 #ifdef STREAKS_COMPRESS_OUTPUT
    523     // compression of the image pixels is disabled for now. Some consortium members
    524     // have problems reading them
    525     copyFitsOptions(sf->outImage, sf->recImage, sf->inImage);
    526 
    527     // XXX: TODO: can we derive these values from the input header?
    528     // psFitsCompressionGet(sf->inImage->image) gives compression none
    529     // perhaps we should just use the definition of COMP_IMG in the configuration
    530     psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    531     if (sf->recImage) {
    532         psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    533     }
    534 #endif
     534    copyFitsOptions(sf->outImage, sf->recImage, sf->inImage, sf->tiles);
    535535
    536536    if (sf->inMask) {
     
    539539            sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
    540540            if (sf->recMask) {
    541                 sf->recMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
    542             }
     541                sf->recMask->header = psMetadataCopy(NULL, sf->outMask->header);
     542                addRecoveryKeyword(sf->recMask->header);
     543            }
     544            addDestreakKeyword(sf->outMask->header);
    543545            if (updateAstrometry) {
    544546                pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
     
    554556            }
    555557
    556 #ifdef STREAKS_COMPRESS_OUTPUT
    557             // XXX: see note above
    558             copyFitsOptions(sf->outMask, sf->recMask, sf->inMask);
    559             psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    560             if (sf->recMask) {
    561                 psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    562             }
     558            copyFitsOptions(sf->outMask, sf->recMask, sf->inMask, sf->tiles);
    563559            if (sf->outChMask) {
    564                 copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask);
    565                 psFitsSetCompression(sf->outChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    566                 if (sf->recChMask) {
    567                     psFitsSetCompression(sf->recChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    568                 }
    569             }
    570 #endif
     560                copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask, sf->tiles);
     561            }
    571562        }
    572563    }
     
    576567        sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
    577568        if (sf->recWeight) {
    578             sf->recWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
    579         }
     569            sf->recWeight->header = psMetadataCopy(NULL, sf->outWeight->header);
     570            addRecoveryKeyword(sf->recWeight->header);
     571        }
     572        addDestreakKeyword(sf->outWeight->header);
    580573        if (updateAstrometry) {
    581574            pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     
    583576        setupImageRefs(sf->outWeight, sf->recWeight, sf->inWeight, sf->extnum, exciseAll);
    584577
    585 #ifdef STREAKS_COMPRESS_OUTPUT
    586         copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight);
    587         // XXX: see note above
    588         psFitsSetCompression(sf->outWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    589         if (sf->recWeight) {
    590             psFitsSetCompression(sf->recWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    591         }
    592 #endif
    593     }
    594     // and for raw images, create sub images that represent the actual image
    595     // area (no overscan)
     578        copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight, sf->tiles);
     579    }
    596580
    597581    return true;
    598582}
    599 
    600 
    601583
    602584static void
Note: See TracChangeset for help on using the changeset viewer.