IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 6, 2008, 6:42:41 PM (18 years ago)
Author:
bills
Message:

completed excising of non-wapred pixels and various other changes.

File:
1 edited

Legend:

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

    r20520 r20573  
    1 #include "streaks.h"
    2 #include "streaksextern.h"
     1#include "streaksremove.h"
    32#include "libgen.h"
    43#include "unistd.h"
    54
     5// XXX: I don't think we need the lock and unlock functions
    66extern bool  sFileLock(sFile * sfile);
    77extern bool  sFileUnlock(sFile * sfile);
     
    1010static nebServer *ourNebServer = NULL;
    1111
    12 // for clarity in this program we don't propagate errors up the stack
     12// to enhance clarity we don't propagate errors up the stack
    1313// we just bail out
    1414void streaksremoveExit(psString str, int exitCode) {
     
    7070}
    7171
    72 psString
     72static psString
    7373resolveFilename(pmConfig *config, sFile *sfile, bool create)
    7474{
     
    166166        sfile->resolved_name = resolveFilename(config, sfile, true);
    167167        if (!sfile->resolved_name) {
    168             psError(PS_ERR_IO, false, "Failed to create file %s", sfile->name);
     168            psError(PS_ERR_IO, false, "Failed to resolve filename for %s", sfile->name);
    169169            sFileFree(sfile);
    170170            streaksremoveExit("", 1);
    171171        }
    172172        sfile->fits = psFitsOpen(sfile->resolved_name, "w");
    173         sfile->fits->options = psFitsOptionsAlloc();
     173        if (sfile->fits) {
     174            sfile->fits->options = psFitsOptionsAlloc();
     175        }
    174176    } else {
    175177        sfile->name = psStringCopy(name);
     
    197199
    198200
    199 bool
    200 astrometry_read(streakFiles *sf)
     201static bool
     202readAstrometry(streakFiles *sf)
    201203{
    202204    pmHDU *phu = pmFPAviewThisPHU(sf->view, sf->inAstrom->fpa);
    203205    if (phu) {
    204         char *ctype = psMetadataLookupStr(NULL, phu->header, "CTYPE1");
     206        bool status;
     207        char *ctype = psMetadataLookupStr(&status, phu->header, "CTYPE1");
    205208        if (ctype) {
    206209            sf->bilevelAstrometry = !strcmp (&ctype[4], "-DIS");
     
    229232}
    230233
     234// load the fpa containing the astrometry, find the appropriate chip and process the data
    231235static void
    232236setupAstromFromFPA(streakFiles *sf)
     
    259263    }
    260264
    261     // read in the astrometry
    262     astrometry_read(sf);
     265    readAstrometry(sf);
    263266}
    264267
     
    312315    }
    313316    setupAstromFromFPA(sf);
    314     if (CHIP_LEVEL_INPUT(sf->stage)) {
    315         computeWarpedPixels(sf);
    316     }
    317317     
    318318    psElemType tileType;                // Type corresponding to "long"
     
    373373{
    374374    // unless stage is raw or chip when we get here we're done
    375     if (!CHIP_LEVEL_INPUT(sf->stage)) {
     375    if (sf->stage != IPP_STAGE_RAW) {
    376376        sf->view->readout = -1;
    377377        pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
     
    442442    }
    443443
    444     // bool status;
    445444    int argnum;
    446445    ippStage stage = IPP_STAGE_NONE;
     
    667666
    668667// Determine whether the current header for this file is an image or not
     668// Find a cleaner way to do this
    669669static bool
    670670isImage(sFile *in)
     
    686686            }
    687687        }
     688    } else if (psMetadataLookupBool(&status, in->header, "ZIMAGE")) {
     689        return true;
    688690    } else if (in->nHDU == 1) {
    689691        // no extensions in the file, can just return true? For now require
     
    721723}
    722724
     725void
     726setDataExtent(ippStage stage, sFile *in)
     727{
     728    if (stage == IPP_STAGE_RAW) {
     729        psString datasec = psMetadataLookupStr(NULL, in->header, "DATASEC");
     730        if (!datasec) {
     731            psError(PS_ERR_IO, false, "failed to find DATASEC in header");
     732            streaksremoveExit("", PS_EXIT_DATA_ERROR);
     733        }
     734        int xmin, xmax, ymin, ymax;
     735        sscanf(datasec, "[%d:%d,%d:%d]", &xmin, &xmax, &ymin, &ymax);
     736        in->numCols = xmax - xmin + 1;
     737        in->numRows = ymax - ymin + 1;
     738    } else {
     739        in->numCols = in->image->numCols;
     740        in->numRows = in->image->numRows;
     741    }
     742}
     743
    723744static void
    724 readImage(sFile *in, int extnum)
     745readImage(sFile *in, int extnum, ippStage stage)
    725746{
    726747    psRegion region = {0, 0, 0, 0};
     
    756777            streaksremoveExit("", PS_EXIT_DATA_ERROR);
    757778        }
    758         in->numCols = in->image->numCols;
    759779        in->numRows = in->image->numRows;
    760780    }  else {
     781        if (stage != IPP_STAGE_RAW) {
     782            psError(PS_ERR_PROGRAMMING, true, "unexpected image cube found in non-raw image");
     783            streaksremoveExit("", PS_EXIT_PROG_ERROR);
     784        }
    761785        in->imagecube = psFitsReadImageCube(in->fits, region);
    762786        if (!in->imagecube) {
     
    766790        }
    767791        psImage *image = (psImage *) (in->imagecube->data[0]);
    768         in->numCols = image->numCols;
    769         in->numRows = image->numRows;
    770     }
    771 
     792    }
     793    setDataExtent(stage, in);
    772794}
    773795
    774796static void
    775 setFitsOptions(sFile *sfile, psString extname, int bitpix, float bscale, float bzero)
     797setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero)
    776798{
    777799    if (sfile->fits->options) {
     
    812834        bzero = psMetadataItemParseF32(bzeroItem);
    813835    }
    814     bool mdok;
    815 
    816     psString extname = psMetadataLookupStr(&mdok, in->header, "EXTNAME");
    817     setFitsOptions(out, extname, bitpix, bscale, bzero);
    818     setFitsOptions(rec, extname, bitpix, bscale, bzero);
     836
     837//    setFitsOptions(out, bitpix, bscale, bzero);
     838//    setFitsOptions(rec, bitpix, bscale, bzero);
    819839}
    820840
     
    869889        // image data from pmFPAfile (diff or warp file)
    870890        readImageFrom_pmFile(sf);
    871         sf->astrom = streakSetAstrometry(sf->astrom, sf->inAstrom->fpa, sf->chip, true, sf->cell->concepts,
    872             sf->inImage->numCols, sf->inImage->numRows);
     891        sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa,
     892                sf->chip, true, sf->cell->concepts, sf->inImage->numCols, sf->inImage->numRows);
    873893    } else {
    874894        // image data directly from psFits
    875         readImage(sf->inImage, sf->extnum);
     895        readImage(sf->inImage, sf->extnum, sf->stage);
    876896        if (SFILE_IS_IMAGE(sf->inImage)) {
    877             sf->astrom = streakSetAstrometry(sf->astrom, sf->inAstrom->fpa, sf->chip, false,
     897            sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa, sf->chip, false,
    878898                sf->inImage->header, sf->inImage->numCols, sf->inImage->numRows);
    879899        }
     900    }
     901    if (!sf->astrom) {
     902        psError(PS_ERR_UNKNOWN, false, "failed to set up astrometry for extnsion %d", sf->extnum);
     903        streaksremoveExit("", PS_EXIT_UNKNOWN_ERROR);
    880904    }
    881905    sf->outImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header);
     
    906930
    907931    if (sf->inMask) {
    908         readImage(sf->inMask, sf->extnum);
     932        readImage(sf->inMask, sf->extnum, sf->stage);
    909933        sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);
    910934        sf->outMask->image = (psImage*) psMemIncrRefCounter(sf->inMask->image);
     
    920944
    921945    if (sf->inWeight) {
    922         readImage(sf->inWeight, sf->extnum);
     946        readImage(sf->inWeight, sf->extnum, sf->stage);
    923947        sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header);
    924948        sf->outWeight->image = (psImage*) psMemIncrRefCounter(sf->inWeight->image);
     
    943967writeImage(sFile *sfile, psString extname, int extnum)
    944968{
     969    if (!sfile->image) {
     970        return;
     971    }
    945972    if (!psFitsWriteImage(sfile->fits, sfile->header, sfile->image, 0, extname)) {
    946973        psError(PS_ERR_IO, false, "failed to write image to %s extnum: %d",
     
    953980    sfile->header = NULL;
    954981}
     982
    955983static void
    956984writeImageCube(sFile *sfile, psArray *imagecube, psString extname, int extnum)
     
    966994
    967995static void
    968 writeImages(streakFiles *sf)
     996writeImages(streakFiles *sf, bool exciseAll)
    969997{
    970998    psString extname = NULL;
    971999    if (sf->nHDU > 1) {
    972         extname = psMetadataLookupStr(NULL, sf->inImage->header, "EXTNAME");
     1000        bool mdok;
     1001        extname = psMetadataLookupStr(&mdok, sf->inImage->header, "EXTNAME");
    9731002    }
    9741003    if (sf->inImage->numZPlanes == 0)  {
     
    9761005        writeImage(sf->recImage, extname, sf->extnum);
    9771006    } else {
    978         // XXX: TODO: if a streak touched this cell then
    979         // write the image cube to the recovery image if no streaks write it
    980         // to the output image
    981         writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
     1007        double initValue;
     1008        if (exciseAll) {
     1009            writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
     1010            initValue = NAN;
     1011        } else {
     1012            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
     1013            initValue = 0;
     1014        }
     1015
     1016        // borrow one of the images from the imagecube and set it to NAN
     1017        psImage *image = psArrayGet (sf->inImage->imagecube, 0);
     1018        psMemIncrRefCounter(image);
     1019        psImageInit(image, initValue);
     1020        if (exciseAll) {
     1021            sf->outImage->image = image;
     1022            writeImage(sf->outImage, extname, sf->extnum);
     1023        } else {
     1024            sf->recImage->image = image;
     1025            writeImage(sf->recImage, extname, sf->extnum);
     1026        }
     1027        // Assumption there are no mask and weight images for video cells
     1028        return;
    9821029    }
    9831030    if (sf->outMask) {
     
    11941241}
    11951242
     1243static void
     1244excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, double newMaskValue)
     1245{
     1246    double imageValue  = psImageGet (sfiles->inImage->image,  x, y);
     1247    psImageSet (sfiles->recImage->image,  x, y, imageValue);
     1248    psImageSet (sfiles->outImage->image,  x, y, NAN);
     1249
     1250    // TODO:
     1251    // Need to get mask weight for PM_MASK_DETECTOR.  Is this stored
     1252    // in a config somewhere?  Not sure how to properly set the maskValue.
     1253 
     1254    if (sfiles->inWeight) {
     1255        double weightValue = psImageGet (sfiles->inWeight->image, x, y);
     1256        psImageSet (sfiles->recWeight->image, x, y, weightValue);
     1257        psImageSet (sfiles->outWeight->image, x, y, NAN);
     1258    }
     1259    if (sfiles->inMask) {
     1260        double maskValue   = psImageGet (sfiles->inMask->image,   x, y);
     1261        psImageSet (sfiles->recMask->image,   x, y, maskValue);
     1262        psImageSet (sfiles->outMask->image,   x, y, newMaskValue);
     1263    }
     1264}
     1265
     1266static void
     1267exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue)
     1268{
     1269    int cell_x0 = sfiles->astrom->cell_x0;
     1270    int cell_y0 = sfiles->astrom->cell_y0;
     1271    int xParity = sfiles->astrom->xParity;
     1272    int yParity = sfiles->astrom->yParity;
     1273    int numCols = sfiles->inImage->numCols;
     1274    int numRows = sfiles->inImage->numRows;
     1275
     1276//    printf("%2d x0: %4d y0: %4d xpar: %d ypar: %d\n", sfiles->extnum, cell_x0, cell_y0, xParity, yParity);
     1277
     1278    for (int yCell = 0; yCell < numRows; yCell++) {
     1279        int yChip;
     1280        if (yParity == 1) {
     1281            yChip = cell_y0 + yCell;
     1282        } else {
     1283            yChip = cell_y0 - yCell;
     1284        }
     1285
     1286        psU8 *pixels = sfiles->warpedPixels->data.U8[yChip];
     1287
     1288        if (xParity == 1) {
     1289            pixels += cell_x0;
     1290            for (int xCell = 0; xCell < numCols; xCell++, pixels++) {
     1291                if (! *pixels ) {
     1292                    excisePixel(sfiles, xCell, yCell, newMaskValue);
     1293                }
     1294            }
     1295        } else {
     1296            // negative parity
     1297            // point to the right most pixel in chip coordinates (lowest in cell coords)
     1298            pixels += cell_x0 - numCols;
     1299            for (int xCell = numCols - 1; xCell >= 0 ; xCell--, pixels++) {
     1300                if (!*pixels) {
     1301                    excisePixel(sfiles, xCell, yCell, newMaskValue);
     1302                }
     1303            }
     1304        }
     1305    }
     1306}
     1307
     1308bool
     1309warpedPixel(streakFiles *sfiles, PixelPos *cellCoord)
     1310{
     1311    PixelPos chipCoord;
     1312
     1313    if (!CHIP_LEVEL_INPUT(sfiles->stage)) {
     1314        // if we're here on a skycell image by definition this pixel was warped
     1315        return true;
     1316    }
     1317
     1318    cellToChip(&chipCoord, sfiles->astrom, cellCoord);
     1319
     1320    return psImageGet(sfiles->warpedPixels, chipCoord.x, chipCoord.y) ? true : false;
     1321}
     1322
    11961323int
    11971324main(int argc, char *argv[])
     
    12201347    streakFiles *sfiles = openFiles(config);
    12211348
    1222     if (sfiles->stage == IPP_STAGE_RAW) {
    1223         // copy PHU to output files
    1224         copyPHU(sfiles);
    1225         // advance to the first image extension
    1226         if (!streakFilesNextExtension(sfiles)) {
    1227             psErrorStackPrint(stderr, "failed to advance to first extension of input images");
    1228             exit (PS_EXIT_PROG_ERROR);
    1229         }
    1230     }
    1231 
    1232     // Iterate through components of image
    1233     do {
    1234 
    1235         // read the images and copy the pixels image from the inputs to the outputs
    1236         // This also sets up the astrometry and initializes the recovery images
    1237         if (!readAndCopyToOutput(sfiles)) {
    1238             // this extension is not an image skip (video descriptor?
    1239             // XXX is there anything else that could be in an input file that we need to handle?
    1240             continue;
    1241         }
    1242 
    1243         // Identify pixels to mask because of streaks
    1244 
    1245         pixels = streak_on_component (streaks, sfiles->astrom,
    1246                                       sfiles->inImage->numCols, sfiles->inImage->numRows);
    1247 
    1248 #ifdef notyet
    1249         // XXX: How is this formatted?
    1250         // PFS: Aren't these chips that are never warped?  They should be cleared
    1251         //      entirely or copy them to archive location.  I don't know how
    1252         //      to find these chips.  The fact that a warp is not created for
    1253         //      a chip must be recorded somewhere.  I don't like this approach
    1254         //      of including everything (ALL) and then removing them if they
    1255         //      are warped.  I don't know if it is practical.
     1349    if (CHIP_LEVEL_INPUT(sfiles->stage)) {
    12561350        // From ICD:
    12571351        // In the raw and detrended images, the pixels which were not
     
    12601354        // than a small fraction of the pixels are lit by the input image.
    12611355
    1262         // Identify pixels to mask because not in a warp
    1263         warp_pixels = pixel_list_initialise(ALL); // List of pixels to be excised because not in a warp
    1264         foreach warp (warps) {
    1265             // Calculate warp region on image
    1266             image_warp = warp_on_component(warp, astrom, numCols, numRows);
    1267             foreach pixel specified by image_warp {
    1268                 remove_pixel(warp_pixels, pixel);
     1356        computeWarpedPixels(sfiles);
     1357    }
     1358
     1359    if (sfiles->stage == IPP_STAGE_RAW) {
     1360        // copy PHU to output files
     1361        copyPHU(sfiles);
     1362
     1363        // advance to the first image extension
     1364        if (!streakFilesNextExtension(sfiles)) {
     1365            psErrorStackPrint(stderr, "failed to advance to first extension of input images");
     1366            exit (PS_EXIT_PROG_ERROR);
     1367        }
     1368    }
     1369
     1370    // Iterate through components of image
     1371    do {
     1372        bool exciseAll = false;
     1373
     1374        // read the images and copy the pixels image from the inputs to the outputs
     1375        // This also sets up the astrometry and initializes the recovery images
     1376        if (!readAndCopyToOutput(sfiles)) {
     1377            // this extension is not an image skip (video descriptor?)
     1378            // XXX is there anything else that could be in an input file that we need to handle?
     1379            continue;
     1380        }
     1381
     1382        // Identify pixels to mask because of streaks
     1383
     1384        pixels = streak_on_component (streaks, sfiles->astrom,
     1385                                      sfiles->inImage->numCols, sfiles->inImage->numRows);
     1386        // XXX:
     1387        //
     1388        // PFS: Need to get mask weight for PM_MASK_DETECTOR.  Is this stored
     1389        // in a config somewhere?  Not sure how to properly set the maskValue.
     1390        // WES just pick some numbers
     1391#define MASK_STREAK     42
     1392#define MASK_NOT_WARPED 51
     1393
     1394        exciseNonWarpedPixels(sfiles, MASK_NOT_WARPED);
     1395
     1396        if (sfiles->inImage->image) {
     1397            for (i = 0; i < psArrayLength (pixels); ++i) {
     1398                pixelPos = psArrayGet (pixels, i);
     1399
     1400                if (warpedPixel(sfiles, pixelPos)) {
     1401
     1402                    excisePixel(sfiles, pixelPos->x, pixelPos->y, MASK_STREAK);
     1403
     1404                } else {
     1405                    // This pixel was not included in any warp and has thus already excised above
     1406                }
    12691407            }
    1270         }
    1271 #endif
    1272 
    1273         // XXX: This loop following will SEGV for raw video Cells becuase the image is null
    1274         for (i = 0; i < psArrayLength (pixels); ++i) {
    1275             pixelPos = psArrayGet (pixels, i);
    1276             imageValue  = psImageGet (sfiles->inImage->image,  pixelPos->x, pixelPos->y);
    1277             psImageSet (sfiles->recImage->image,  pixelPos->x, pixelPos->y, imageValue);
    1278             psImageSet (sfiles->outImage->image,  pixelPos->x, pixelPos->y, NAN);
    1279             if (sfiles->inWeight) {
    1280                 weightValue = psImageGet (sfiles->inWeight->image, pixelPos->x, pixelPos->y);
    1281                 psImageSet (sfiles->recWeight->image, pixelPos->x, pixelPos->y, weightValue);
    1282                 psImageSet (sfiles->outWeight->image, pixelPos->x, pixelPos->y, NAN);
    1283             }
    1284             if (sfiles->inMask) {
    1285                 maskValue   = psImageGet (sfiles->inMask->image,   pixelPos->x, pixelPos->y);
    1286                 psImageSet (sfiles->recMask->image,   pixelPos->x, pixelPos->y, maskValue);
    1287                 // TODO:
    1288                 // Need to get mask weight for PM_MASK_DETECTOR.  Is this stored
    1289                 // in a config somewhere?  Not sure how to properly set the maskValue.
    1290                 psImageSet (sfiles->outMask->image,   pixelPos->x, pixelPos->y, maskValue);
    1291             }
     1408        } else {
     1409            // This video cell is touched by a streak excise
     1410            // For now we excise all video cells.
     1411            // if (psArrayLength(pixels))
     1412            exciseAll = true;
    12921413        }
    12931414        psArrayElementsFree (pixels);
    12941415
    12951416        // write out the destreaked temporary images and the recovery images
    1296         writeImages(sfiles);
     1417        writeImages(sfiles, exciseAll);
     1418
    12971419    } while (streakFilesNextExtension(sfiles));
    12981420
Note: See TracChangeset for help on using the changeset viewer.