IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 15, 2009, 4:02:42 PM (17 years ago)
Author:
eugene
Message:

updates from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090715/magic/remove/src/streaksremove.c

    r25022 r25406  
    1212static pmConfig *parseArguments(int argc, char **argv);
    1313static bool readAndCopyToOutput(streakFiles *sf, bool exciseAll);
    14 static void exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue);
     14static void exciseNonWarpedPixels(streakFiles *sfiles, psImageMaskType newMaskValue);
    1515static bool warpedPixel(streakFiles *sfiles, int x, int y);
    16 static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue);
     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);
     19static void censorSources(streakFiles *sfiles, psImageMaskType maskStreak);
     20static long censorPixels(streakFiles *sfiles, psImage * pixels, bool checkNonWarpedPixels, psU16 maskStreak);
    2021
    2122int
     
    3536    // Values to set for masked pixels
    3637    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    psU32 maskMask = 0;             // value looked up for MASK.STREAK
    3839
    3940    psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS");
     
    4243    Streaks *streaks = readStreaksFile(streaksFileName);
    4344    if (!streaks) {
    44         psError(PS_ERR_UNKNOWN, "failed to read streaks file: %s", streaksFileName);
     45        psError(PS_ERR_UNKNOWN, false, "failed to read streaks file: %s", streaksFileName);
    4546        streaksExit("", PS_EXIT_PROG_ERROR);
    4647    }
     
    8384        psLogMsg("streaksremove", PS_LOG_INFO, "time to compute warped pixels: %f\n", cwp_t);
    8485    }
    85    
     86
    8687    if (sfiles->stage == IPP_STAGE_RAW) {
    8788        // Except for raw stage, all of our (GPC1) files have one image extension.
     
    9798    }
    9899
    99     int totalPixels = 0;
    100     int totalStreakPixels = 0;
     100    long totalPixels = 0;
     101    long totalStreakPixels = 0;
    101102
    102103    // Iterate through each component of the input (except for raw images there is only one)
     
    132133                                                        sfiles->inImage->numRows);
    133134            psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS"));
    134            
     135
    135136            // if this extension contained an image, excise the streaked pixels.
    136137            // otherwise it contained an image cube (video cell) which is handled in the if block
     
    146147                }
    147148
    148 
    149149                psTimerStart("REMOVE_STREAKS");
    150150
    151                 for (int y=0 ; y < sfiles->inImage->numRows; y++) {
    152                     for (int x = 0; x < sfiles->inImage->numCols; x++) {
    153                         if (psImageGet(pixels, x, y)) {
    154                             ++totalStreakPixels;
    155                             if (!checkNonWarpedPixels || warpedPixel(sfiles, x, y)) {
    156 
    157                                 excisePixel(sfiles, x, y, true, maskStreak);
    158 
    159                             } else {
    160                                 // This pixel was not included in any warp and has thus already excised
    161                                 // by exciseNonWarpedPixels
    162                             }
    163                         }
    164                     }
    165                 }
     151                totalStreakPixels += censorPixels(sfiles, pixels, checkNonWarpedPixels, maskStreak);
    166152
    167153                psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS"));
     
    172158                }
    173159
    174             } else { 
     160            } else {
    175161                // this component contains an image cube
    176162                // For now excise it completely
     
    215201
    216202    if (!replicateOutputs(sfiles)) {
    217         psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
     203        psErrorStackPrint(stderr, "failed to replicate output files");
    218204        deleteTemps(sfiles);
    219         psErrorStackPrint(stderr, "");
    220205        exit(PS_EXIT_UNKNOWN_ERROR);
    221206    }
     
    247232    pmConceptsDone();
    248233    pmModelClassCleanup();
    249     streaksNebulousCleanup(); 
     234    streaksNebulousCleanup();
    250235    pmConfigDone();
    251236    psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", psTimerClear("STREAKSREMOVE"));
     
    255240
    256241    return 0;
     242}
     243
     244static long
     245censorPixels(streakFiles *sfiles, psImage *pixels, bool checkNonWarpedPixels, psU16 maskStreak)
     246{
     247    long streakPixels = 0;
     248
     249    for (int y=0 ; y < sfiles->inImage->numRows; y++) {
     250        for (int x = 0; x < sfiles->inImage->numCols; x++) {
     251            if (psImageGet(pixels, x, y)) {
     252                ++streakPixels;
     253                if (!checkNonWarpedPixels || warpedPixel(sfiles, x, y)) {
     254
     255                    excisePixel(sfiles, x, y, true, maskStreak);
     256
     257                } else {
     258                    // This pixel was not included in any warp and has thus already excised
     259                    // by exciseNonWarpedPixels
     260                }
     261            }
     262        }
     263    }
     264    return streakPixels;
    257265}
    258266
     
    356364            true);
    357365    }
    358    
     366
    359367    if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) {
    360368        psArgumentRemove(argnum, &argc, argv);
     
    374382        psArgumentRemove(argnum, &argc, argv);
    375383    }
    376        
     384
    377385    // if skycells are not provided then we have to execise all pixels  unless -keepnonwarped
    378386    pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist");
     
    628636            initValue = NAN;
    629637        } else {
    630             // otherwise write it to the output 
     638            // otherwise write it to the output
    631639            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    632640            initValue = 0;
     
    666674
    667675static void
    668 excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue)
     676excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue)
    669677{
    670678    double exciseValue = sfiles->inImage->exciseValue;
     
    675683    }
    676684
    677     double imageValue  = psImageGet (sfiles->inImage->image,  x, y);
     685    float imageValue  = sfiles->inImage->image->data.F32[y][x];
    678686    if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
    679         psImageSet (sfiles->recImage->image,  x, y, imageValue);
     687        sfiles->recImage->image->data.F32[y][x] = imageValue;
    680688    }
    681689
    682690    if (sfiles->transparentStreaks == 0) {
    683         psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     691        sfiles->outImage->image->data.F32[y][x] = exciseValue;
    684692    } else {
    685693        if (streak) {
    686694            // as a visualization aid don't mask the pixel, just change the intensity
    687             psImageSet (sfiles->outImage->image,  x, y, imageValue + sfiles->transparentStreaks);
     695            sfiles->outImage->image->data.F32[y][x] = imageValue + sfiles->transparentStreaks;
    688696        } else {
    689             psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     697            sfiles->outImage->image->data.F32[y][x] = exciseValue;
    690698        }
    691699    }
     
    693701    if (sfiles->outWeight) {
    694702        if (sfiles->recWeight) {
    695             double weightValue = psImageGet (sfiles->inWeight->image, x, y);
    696             psImageSet (sfiles->recWeight->image, x, y, weightValue);
     703            sfiles->recWeight->image->data.F32[y][x] = sfiles->inWeight->image->data.F32[y][x];
    697704        }
    698705        // Assume that weight images are always a floating point type
    699         psImageSet (sfiles->outWeight->image, x, y, NAN);
     706        sfiles->outWeight->image->data.F32[y][x] = NAN;
    700707    }
    701708    if (sfiles->outMask) {
    702709        if (sfiles->recMask) {
    703             double maskValue   = psImageGet (sfiles->inMask->image,   x, y);
    704             psImageSet (sfiles->recMask->image,   x, y, maskValue);
    705         }
    706         psImageSet (sfiles->outMask->image,   x, y, newMaskValue);
     710            sfiles->recMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] =
     711                sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x];
     712        }
     713        sfiles->outMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] =
     714            sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] | newMaskValue;
    707715    }
    708716}
    709717
    710718static void
    711 exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue)
     719exciseNonWarpedPixels(streakFiles *sfiles, psImageMaskType newMaskValue)
    712720{
    713721    int cell_x0 = sfiles->astrom->cell_x0;
     
    785793// streak mask
    786794static void
    787 censorSources(streakFiles *sfiles, psU32 maskStreak)
     795censorSources(streakFiles *sfiles, psImageMaskType maskStreak)
    788796{
    789797    if ((!sfiles->inSources) || (!sfiles->outMask)) {
     
    799807    sFile *out = sfiles->outSources;
    800808
    801     in->header = psFitsReadHeader(NULL, in->fits);
    802     if (!in->header) {
    803         psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
     809
     810    // Primary header, should be "something.hdr"
     811    {
     812        psMetadata *header = psFitsReadHeader(NULL, in->fits);
     813        if (!header) {
     814            psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
     815            streaksExit("", PS_EXIT_DATA_ERROR);
     816        }
     817
     818        bool status;
     819        psString extname = psMetadataLookupStr(&status, header, "EXTNAME");
     820        if (!extname) {
     821            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
     822            streaksExit("", PS_EXIT_DATA_ERROR);
     823        }
     824        addDestreakKeyword(header);
     825
     826        if (!psFitsWriteBlank(out->fits, header, extname)) {
     827            psError(PS_ERR_IO, false, "failed to write blank in header of %s", in->resolved_name);
     828            streaksExit("", PS_EXIT_DATA_ERROR);
     829        }
     830        psFree(header);
     831    }
     832
     833    // Extension with PSF fits, should be "something.psf"
     834    {
     835        if (!psFitsMoveExtNum(in->fits, 1, true)) {
     836            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
     837            streaksExit("", PS_EXIT_DATA_ERROR);
     838        }
     839
     840        psMetadata *header = psFitsReadHeader(NULL, in->fits);
     841        if (!header) {
     842            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
     843            streaksExit("", PS_EXIT_DATA_ERROR);
     844        }
     845        psString extname = psMetadataLookupStr(NULL, header, "EXTNAME");
     846        if (!extname) {
     847            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
     848            streaksExit("", PS_EXIT_DATA_ERROR);
     849        }
     850
     851        psArray *inTable = psFitsReadTable(in->fits);
     852        if (!inTable->n) {
     853            psErrorStackPrint(stderr, "table in %s is empty", in->resolved_name);
     854            streaksExit("", PS_EXIT_DATA_ERROR);
     855        }
     856
     857        psArray *outTable = psArrayAllocEmpty(inTable->n);
     858        int j = 0;
     859        int numCensored = 0;
     860        for (int i = 0 ; i < inTable->n; i++) {
     861            psMetadata *row = inTable->data[i];
     862
     863            psF32 x = psMetadataLookupF32(NULL, row, "X_PSF");
     864            psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF");
     865
     866            psImageMaskType mask = maskImage->data.PS_TYPE_IMAGE_MASK_DATA[(int)y][(int)x];
     867
     868            // Key the source if the center pixel is not masked with maskStreak
     869            if (!(mask & maskStreak) ) {
     870                psArraySet(outTable, j++, row);
     871            } else {
     872                numCensored++;
     873            }
     874        }
     875
     876        // get rid of unused elements (don't know if this is necessary)
     877        psArrayRealloc(outTable, j);
     878
     879        addDestreakKeyword(header);
     880        if (psArrayLength(outTable) > 0) {
     881            printf("Censored %d sources\n", numCensored);
     882            if (! psFitsWriteTable(out->fits, header, outTable, extname)) {
     883                psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
     884                streaksExit("", PS_EXIT_DATA_ERROR);
     885            }
     886        } else {
     887            printf("Censored ALL %d sources\n", numCensored);
     888            if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) {
     889                psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name);
     890                streaksExit("", PS_EXIT_DATA_ERROR);
     891            }
     892        }
     893        psFree(header);
     894        psFree(outTable);
     895        psFree(inTable);
     896    }
     897
     898    // XXX Will need to update to handle extension with extended sources, etc.
     899
     900    if (!psFitsClose(out->fits)) {
     901        psErrorStackPrint(stderr, "failed to close table %s", out->resolved_name);
    804902        streaksExit("", PS_EXIT_DATA_ERROR);
    805903    }
    806 
    807     bool status;
    808     psString extname = psMetadataLookupStr(&status, in->header, "EXTNAME");
    809     if (!extname) {
    810         psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
    811         streaksExit("", PS_EXIT_DATA_ERROR);
    812     }
    813 
    814     psArray *inTable = psFitsReadTable(in->fits);
    815     if (!inTable->n) {
    816         psError(PS_ERR_IO, false, "table in %s is empty", in->resolved_name);
    817         streaksExit("", PS_EXIT_DATA_ERROR);
    818     }
    819 
    820     psArray *outTable = psArrayAllocEmpty(inTable->n);
    821     int j = 0;
    822     int numCensored = 0;
    823     for (int i = 0 ; i < inTable->n; i++) {
    824         psMetadata *row = inTable->data[i];
    825 
    826         psF32 x = psMetadataLookupF32 (&status, row, "X_PSF");
    827         psF32 y = psMetadataLookupF32 (&status, row, "Y_PSF");
    828        
    829         psU32 mask = psImageGet(maskImage, x, y);
    830 
    831         // Key the source if the center pixel is not masked with maskStreak
    832         if (! (mask & maskStreak) ) {
    833             psArraySet(outTable, j++, row);
    834         } else {
    835             numCensored++;
    836         }
    837     }
    838 
    839     // get rid of unused elements (don't know if this is necessary)
    840     psArrayRealloc(outTable, j);
    841 
    842     addDestreakKeyword(in->header);
    843     if (psArrayLength(outTable) > 0) {
    844         printf("Censored %d sources\n", numCensored);
    845         if (! psFitsWriteTable(out->fits, in->header, outTable, extname)) {
    846             psError(PS_ERR_IO, false, "failed to write table to %s", out->resolved_name);
    847             streaksExit("", PS_EXIT_DATA_ERROR);
    848         }
    849     } else {
    850         printf("Censored ALL %d sources\n", numCensored);
    851         if (! psFitsWriteTableEmpty(out->fits, in->header, inTable->data[0], extname)) {
    852             psError(PS_ERR_IO, false, "failed to write empty table to %s", out->resolved_name);
    853             streaksExit("", PS_EXIT_DATA_ERROR);
    854         }
    855     }
    856 
    857     if (!psFitsClose(out->fits)) {
    858         psError(PS_ERR_IO, false, "failed to close table %s", out->resolved_name);
    859         streaksExit("", PS_EXIT_DATA_ERROR);
    860     }
    861 }
     904}
Note: See TracChangeset for help on using the changeset viewer.