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/streaksio.c

    r21085 r21156  
    4141
    4242    sf->inMask = sFileOpen(config, stage, "INPUT.MASK", NULL, false);
    43     if (sf->inMask) {
     43    if (sf->inMask && (sf->stage != IPP_STAGE_RAW)) {
    4444        inputBasename = basename(sf->inMask->name);
    4545        sf->outMask = sFileOpen(config, stage, "OUTPUT", inputBasename, true);
     
    295295streakFilesNextExtension(streakFiles *sf)
    296296{
    297     // unless stage is raw or chip when we get here we're done
     297    // unless stage is raw when we get here we're done
    298298    if (sf->stage != IPP_STAGE_RAW) {
    299299        if (sf->view) {
     
    315315    if (sf->extnum < sf->nHDU) {
    316316        moveExt(sf->inImage, sf->extnum);
    317         if (sf->inMask) {
    318             moveExt(sf->inMask, sf->extnum);
    319         }
    320         if (sf->inWeight) {
    321             moveExt(sf->inWeight, sf->extnum);
    322         }
     317        // No mask and weight images for raw stage
    323318        return true;
    324319    } else {
     320        // we're all done
    325321        return false;
    326322    }
     
    354350
    355351    sFile *inMask = sfiles->inMask;
    356     if (inMask) {
     352    if (inMask && sfiles->outMask) {
    357353        psMetadata *maskHeader = psFitsReadHeader(NULL, inMask->fits);
    358354        if (!maskHeader) {
     
    469465}
    470466
    471 void
    472 setDataExtent(ippStage stage, sFile *in)
    473 {
    474     if (stage == IPP_STAGE_RAW) {
     467static void
     468setDataExtent(ippStage stage, sFile *in, bool rawImage)
     469{
     470    if (rawImage) {
    475471        psString datasec = psMetadataLookupStr(NULL, in->header, "DATASEC");
    476472        if (!datasec) {
     
    480476        int xmin, xmax, ymin, ymax;
    481477        sscanf(datasec, "[%d:%d,%d:%d]", &xmin, &xmax, &ymin, &ymax);
     478
     479        // I've inadvertantly introduced an implicit assumption that cell coordinates
     480        // are the same as image coordinates. This is true for GPC1
     481        // Test for this condition not being true
     482        if (xmin != 1) {
     483            psError(PS_ERR_IO, true, "can't handle datasec with minimum x != 1: %s", datasec);
     484            streaksExit("", PS_EXIT_PROG_ERROR);
     485        }
     486        if (ymin != 1) {
     487            psError(PS_ERR_IO, true, "can't handle datasec with minimum y != 1: %s", datasec);
     488            streaksExit("", PS_EXIT_PROG_ERROR);
     489        }
     490
     491        // These values are different than the size of the actual image which includes
     492        // the bias area.
    482493        in->numCols = xmax - xmin + 1;
    483494        in->numRows = ymax - ymin + 1;
     
    489500
    490501void
    491 readImage(sFile *in, int extnum, ippStage stage)
     502readImage(sFile *in, int extnum, ippStage stage, bool isMask)
    492503{
    493504    psRegion region = {0, 0, 0, 0};
     
    526537            streaksExit("", PS_EXIT_DATA_ERROR);
    527538        }
     539        if (in->image->type.type == PS_TYPE_U16) {
     540            in->exciseValue = 65535;
     541            psMetadataAddU16(in->header, PS_LIST_TAIL, "BLANK", 0, "", 65535);
     542            psMetadataAddU16(in->header, PS_LIST_TAIL, "ZBLANK", 0, "", 65535);
     543        } else {
     544            in->exciseValue = NAN;
     545        }
    528546    }  else {
    529547        if (stage != IPP_STAGE_RAW) {
     
    539557        psImage *image = (psImage *) (in->imagecube->data[0]);
    540558    }
    541     setDataExtent(stage, in);
     559    setDataExtent(stage, in, (stage == IPP_STAGE_RAW) && !isMask);
    542560}
    543561
     
    676694writeImageCube(sFile *sfile, psArray *imagecube, psString extname, int extnum)
    677695{
    678     if (!imagecube) {
     696    if (!sfile || !imagecube) {
    679697        return;
    680698    }
     
    687705    sfile->header = NULL;
    688706}
    689 
    690 #ifdef notdef
    691 void
    692 writeImages(streakFiles *sf, bool exciseImageCube)
    693 {
    694     psString extname = NULL;
    695     if (sf->nHDU > 1) {
    696         bool mdok;
    697         extname = psMetadataLookupStr(&mdok, sf->inImage->header, "EXTNAME");
    698     }
    699     if (sf->inImage->numZPlanes == 0)  {
    700         // note exciseing complete images is handled in readAndCopyToOutput
    701         writeImage(sf->outImage, extname, sf->extnum);
    702         writeImage(sf->recImage, extname, sf->extnum);
    703     } else {
    704         // we have an image cube
    705         double initValue;
    706         if (exciseImageCube) {
    707             // copy the entire input image to the recovery image
    708             writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
    709             initValue = NAN;
    710         } else {
    711             // otherwise write it to the output
    712             writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    713             initValue = 0;
    714         }
    715 
    716         // borrow one of the images from the imagecube and set it to init value
    717         psImage *image = psArrayGet (sf->inImage->imagecube, 0);
    718         psMemIncrRefCounter(image);
    719         psImageInit(image, initValue);
    720         if (exciseImageCube) {
    721             sf->outImage->image = image;
    722             writeImage(sf->outImage, extname, sf->extnum);
    723         } else {
    724             // write zero valued image to reccovery
    725             if (sf->recImage) {
    726                 sf->recImage->image = image;
    727                 writeImage(sf->recImage, extname, sf->extnum);
    728             }
    729         }
    730         // Assumption: there are no mask and weight images for video cells
    731         return;
    732     }
    733     if (sf->outMask) {
    734         writeImage(sf->outMask, extname, sf->extnum);
    735         writeImage(sf->recMask, extname, sf->extnum);
    736     }
    737     if (sf->outWeight) {
    738         writeImage(sf->outWeight, extname, sf->extnum);
    739         writeImage(sf->recWeight, extname, sf->extnum);
    740     }
    741 }
    742 #endif
    743707
    744708void
     
    952916}
    953917
     918bool
     919isExciseValue(double value, double exciseValue)
     920{
     921    if (isnan(exciseValue)) {
     922        return isnan(value);
     923    } else {
     924        return value == exciseValue;
     925    }
     926}
     927
     928void
     929setMaskedToNAN(streakFiles *sfiles, psU8 maskMask, bool printCounts)
     930{
     931        int maskedPixels = 0;
     932        int nandPixels = 0;
     933        int nandWeights = 0;
     934
     935        psImage *image = sfiles->outImage->image;
     936        psImage *mask = sfiles->inMask->image;
     937        psImage *weight = NULL;
     938        if (sfiles->outWeight) {
     939            weight = sfiles->outWeight->image;
     940        }
     941        double exciseValue = sfiles->inImage->exciseValue;
     942
     943        if (printCounts) {
     944            psTimerStart("NAN_MASKED");
     945        }
     946
     947        for (int y=0; y < sfiles->inImage->numRows; y++) {
     948            for (int x=0; x < sfiles->inImage->numCols; x++) {
     949                // these gets are not necessary, we could just set the pixels to nan
     950                // but I want to get the counts
     951                double imageVal  = psImageGet(image, x, y);
     952                psU8 maskVal;
     953                if (sfiles->stage == IPP_STAGE_RAW) {
     954                    int xChip, yChip;
     955                    cellToChipInt(&xChip, &yChip, sfiles->astrom, x, y);
     956                    maskVal = psImageGet(mask, xChip, yChip);
     957                } else {
     958                    maskVal = psImageGet(mask, x, y);
     959                }
     960                if (maskVal & maskMask) {
     961                    ++maskedPixels;
     962                    if (!isExciseValue(imageVal, sfiles->inImage->exciseValue)) {
     963                        ++nandPixels;
     964                        psImageSet(image, x, y, exciseValue);
     965                    }
     966                    if (weight) {
     967                        double weightVal = weight ? psImageGet(weight, x, y) : 0;
     968                        if (!isnan(weightVal)) {
     969                            ++nandWeights;
     970                            psImageSet(weight, x, y, NAN);
     971                        }
     972                    }
     973                }
     974            }
     975        }
     976        if (printCounts) {
     977            printf("time to NAN mask pixels: %f\n", psTimerClear("NAN_MASKED"));
     978            int totalPixels = image->numRows * image->numCols;
     979            printf("pixels:        %10ld\n", totalPixels);
     980            printf("masked pixels: %10ld %4.2f%%\n", maskedPixels, 100. * maskedPixels / totalPixels);
     981            printf("nand pixels:   %10ld %4.2f%%\n", nandPixels, 100. * nandPixels / totalPixels);
     982            if (weight) {
     983                printf("nand weights:  %10ld %4.2f%%\n", nandWeights, 100. * nandWeights / totalPixels);
     984            }
     985        }
     986}
Note: See TracChangeset for help on using the changeset viewer.