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:
15 edited
1 copied

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

    r21156 r24557  
    1616{
    1717    double temp = *first;
     18    *first = *second;
     19    *second = temp;
     20}
     21
     22/** Internal routine to swap integer values */
     23
     24void SwapInt (int* first, int* second)
     25{
     26    int temp = *first;
    1827    *first = *second;
    1928    *second = temp;
     
    255264}
    256265
    257 /** Map a line to an image for its specified width and store as a list
    258     of pixel positions
     266/** Clip the line between (minX,minY) and (maxX,maxY)
     267
     268    @param[in,out] line line to be clipped within the bounds
     269    @param[in] minX minimum X (columns) for the line
     270    @param[in] minY minimum Y (rows) for the line
     271    @param[in] maxX maximum X (columns) for the line
     272    @param[in] maxY maximum Y (rows) for the line
     273    @return true if line overlaps the clip boundaries           */
     274
     275bool LineClipFull (Line *line, int minX, int minY, int maxX, int maxY)
     276{
     277    unsigned int i, found = 0;
     278    Line boundLine, clipLine;
     279    strkPt tuple1, tuple2, vertices[4];
     280    vertices[0].x = minX; vertices[0].y = minY;
     281    vertices[1].x = maxX; vertices[1].y = minY;
     282    vertices[2].x = maxX; vertices[2].y = maxY;
     283    vertices[3].x = minX; vertices[3].y = maxY;
     284
     285    for (i = 0; i < 4 && found < 2; ++i)
     286    {
     287        boundLine.begin = vertices[i];
     288        boundLine.end   = vertices[(i + 1) % 4];
     289        if (LineIntercept (line, &boundLine, &tuple1, &tuple2, false, true))
     290        {
     291            if (found == 0)
     292            {
     293                clipLine.begin = tuple1;
     294                ++found;
     295            }
     296            else if (tuple1.x != clipLine.begin.x ||
     297                     tuple1.y != clipLine.begin.y)
     298            {
     299                clipLine.end = tuple1;
     300                ++found;
     301            }
     302        }
     303    }
     304   
     305    // If two endpoints are found, clip the line
     306   
     307    if (found > 1)
     308    {
     309        if (clipLine.begin.x <= clipLine.end.x)
     310        {
     311            line->begin = clipLine.begin;
     312            line->end   = clipLine.end;
     313        }
     314        else
     315        {
     316            line->begin = clipLine.end;
     317            line->end   = clipLine.begin;
     318        }
     319    }
     320    return found > 1;
     321}
     322
     323/** Move a line by the specified X and Y offsets
     324    @param[in,out] line Line to move by X and Y offsets
     325    @param[in] xOffset X shift applied to both endpoints
     326    @param[in] yOffset Y shift applied to both endpoints        */
     327
     328void LineMove (Line *line, double xOffset, double yOffset)
     329{
     330    line->begin.x += xOffset;
     331    line->begin.y += yOffset;
     332    line->end.x   += xOffset;
     333    line->end.y   += yOffset;
     334}
     335
     336/** Return the maximum bounds between the line endpoints and
     337    current bounds
     338    @param[in] line Line endpoints to compare
     339    @param[in,out] xMin minimum X value to update
     340    @param[in,out] xMax maximum X value to update
     341    @param[in,out] yMin minimum Y value to update
     342    @param[in,out] yMax maximum Y value to update               */
     343
     344void MaxBounds (Line *line, int *xMin, int *xMax,  int *yMin, int *yMax)
     345{
     346    if (line->begin.x < *xMin) *xMin = (int) floor (line->begin.x);
     347    if (line->end.x   < *xMin) *xMin = (int) floor (line->end.x);
     348    if (line->begin.y < *yMin) *yMin = (int) floor (line->begin.y);
     349    if (line->end.y   < *yMin) *yMin = (int) floor (line->end.y);
     350
     351    if (line->begin.x > *xMax) *xMax = (int) ceil (line->begin.x);
     352    if (line->end.x   > *xMax) *xMax = (int) ceil (line->end.x);
     353    if (line->begin.y > *yMax) *yMax = (int) ceil (line->begin.y);
     354    if (line->end.y   > *yMax) *yMax = (int) ceil (line->end.y);
     355}
     356
     357/** Map a line to an image for its specified width and store as
     358    a list of pixel positions
    259359
    260360    @param[out] pixels list of PixelPos pointers corresponding
    261361                       based on the line settings
    262     @param[in] line Line to map to pixels                       */
    263 
    264 void PixelsFromLine (StreakPixels* pixels, Line *line)
    265 {
     362    @param[in] line Line to map to pixels   
     363    @param[in] numCols maximum X (columns) for the line
     364    @param[in] numRows maximum Y (rows) for the line            */
     365
     366void PixelsFromLine (StreakPixels* pixels, Line *line, int numCols, int numRows)
     367{
     368    Line offsetLine;
    266369    PixelPos *pixel;
    267     double slope, xOffset, yOffset, xMid, yMid, xBegin, yBegin, xEnd, yEnd, x, y;
     370    double slope, xOffset, yOffset, xMid, yMid;
     371    int x, y, xBegin = numCols, yBegin = numRows, xEnd = 0, yEnd = 0;
    268372
    269373    // Extract the endpoints
     
    280384    double dr = sqrt (dx * dx + dy * dy);
    281385    double halfWidth  = line->width / 2.0;
    282     double halfWidth2 = halfWidth * halfWidth;
    283386    if (!dr) return;
    284    
     387
     388    // Compute the intercepts of line width bounds and determine maximum
     389    // bounds in each axis
     390
     391    xOffset = -halfWidth * dy / dr;
     392    yOffset =  halfWidth * dx / dr;
     393
     394    offsetLine = *line;
     395    LineMove (&offsetLine, xOffset, yOffset);
     396    if (LineClip (&offsetLine, numCols, numRows))
     397        MaxBounds (&offsetLine, &xBegin, &xEnd, &yBegin, &yEnd);
     398
     399    offsetLine = *line;
     400    LineMove (&offsetLine, -xOffset, -yOffset);
     401    if (LineClip (&offsetLine, numCols, numRows))
     402        MaxBounds (&offsetLine, &xBegin, &xEnd, &yBegin, &yEnd);
     403
    285404    // Step point by point based on the dominate axis
    286405   
     
    307426        // Compute the x and y offsets for the line width extent
    308427
    309         xOffset = halfWidth * dy / dr;
    310         yOffset = halfWidth * dr / dx;
    311         yMid   = y1 + slope * (floor (x1 - xOffset) - x1);
    312         xBegin = floor (x1 - xOffset);
    313         xEnd   = ceil  (x2 + xOffset) + 1.0;
    314 
    315         for (x = xBegin; x < xEnd; ++x)
    316         {
    317             yBegin = floor (yMid - yOffset);
    318             yEnd   = ceil  (yMid + yOffset) + 1.0;
    319             for (y = yBegin; y < yEnd; ++y)
     428        if (xBegin > xEnd)
     429            SwapInt (&xBegin, &xEnd);
     430        else
     431            ++xEnd;
     432        if (xBegin < 0) xBegin = 0;
     433        if (xEnd > numCols) xEnd = numCols;
     434
     435        yMid = y1 + slope * (xBegin - x1);
     436        yOffset = fabs (halfWidth * dr / dx);
     437
     438        for (x = xBegin; x != xEnd; ++x)
     439        {
     440            yBegin = (int) floor (yMid - yOffset);
     441            yEnd   = (int) ceil  (yMid + yOffset) + 1;
     442            for (y = yBegin; y != yEnd; ++y)
    320443            {
    321                 if (DistanceSquared (line, x, y) <= halfWidth2)
     444                if (y >= 0 && y < numRows)
    322445                {
    323                     if (x >=0 && y >= 0) {
    324                         pixel = psAlloc (sizeof(PixelPos));
    325                         pixel->x = (unsigned int) x;
    326                         pixel->y = (unsigned int) y;
    327                         psArrayAdd (pixels, 1024, pixel);
    328                         psFree (pixel);
    329                     }
     446                    pixel = psAlloc (sizeof(PixelPos));
     447                    pixel->x = (unsigned int) x;
     448                    pixel->y = (unsigned int) y;
     449                    psArrayAdd (pixels, 1024, pixel);
     450                    psFree (pixel);
    330451                }
    331452            }
     
    354475       
    355476        // Compute the x and y offsets for the line width extent
    356        
    357         xOffset = halfWidth * dr / dy;
    358         yOffset = halfWidth * dx / dr;
    359 
    360         xMid   = x1 + slope * (floor (y1 - yOffset) - y1);
    361         yBegin = floor (y1 - yOffset);
    362         yEnd   = ceil  (y2 + yOffset) + 1.0;
    363        
    364         for (y = yBegin; y < yEnd; ++y)
    365         {
    366             xBegin = floor (xMid - xOffset);
    367             xEnd   = ceil  (xMid + xOffset) + 1.0;
    368             for (x = xBegin; x < xEnd; ++x)
     477
     478        if (yBegin > yEnd)
     479            SwapInt (&yBegin, &yEnd);
     480        else
     481            ++yEnd;
     482        if (yBegin < 0) yBegin = 0;
     483        if (yEnd > numRows) yEnd = numRows;
     484
     485        xMid = x1 + slope * (yBegin - y1);
     486        xOffset = fabs (halfWidth * dr / dy);
     487       
     488        for (y = yBegin; y != yEnd; ++y)
     489        {
     490            xBegin = (int) floor (xMid - xOffset);
     491            xEnd   = (int) ceil  (xMid + xOffset) + 1;
     492            for (x = xBegin; x != xEnd; ++x)
    369493            {
    370                 if (DistanceSquared (line, x, y) <= halfWidth2)
     494                if (x >=0 && x < numCols)
    371495                {
    372                     if (x >=0 && y >= 0) {
    373                         pixel = psAlloc (sizeof(PixelPos));
    374                         pixel->x = (unsigned int) x;
    375                         pixel->y = (unsigned int) y;
    376                         psArrayAdd (pixels, 1024, pixel);
    377                         psFree (pixel);
    378                     }
     496                    pixel = psAlloc (sizeof(PixelPos));
     497                    pixel->x = (unsigned int) x;
     498                    pixel->y = (unsigned int) y;
     499                    psArrayAdd (pixels, 1024, pixel);
     500                    psFree (pixel);
    379501                }
    380502            }
  • branches/eam_branches/20090522/magic/remove/src/Line.h

    r20308 r24557  
    2121extern bool LineClip (Line *line, int numCols, int numRows);
    2222
     23/** Clip the line between (minX,minY) and (maxX,maxY)
     24
     25    @param[in,out] line line to be clipped within the bounds
     26    @param[in] minX minimum X (columns) for the line
     27    @param[in] minY minimum Y (rows) for the line
     28    @param[in] maxX maximum X (columns) for the line
     29    @param[in] maxY maximum Y (rows) for the line
     30    @return true if line overlaps the clip boundaries           */
     31
     32extern bool LineClipFull (Line *line, int minX, int minY, int maxX, int maxY);
     33
    2334/** Map a line to an image for its specified width and append as
    2435    a list of pixel positions
     
    2637    @param[out] pixels list of PixelPos pointers corresponding
    2738                       based on the line settings
    28     @param[in] line Line to map to pixels                       */
     39    @param[in] line Line to map to pixels   
     40    @param[in] numCols maximum X (columns) for the line
     41    @param[in] numRows maximum Y (rows) for the line            */
    2942
    30 extern void PixelsFromLine (StreakPixels* pixels, Line *line);
     43extern void PixelsFromLine (StreakPixels* pixels, Line *line,
     44                            int numCols, int numRows);
    3145
    3246#endif /* STREAK_LINE_H */
  • branches/eam_branches/20090522/magic/remove/src/Makefile.simple

    r23910 r24557  
    2828    streaksrelease.o
    2929
    30 # STREAKSFLAGS=-DSTREAKS_COMPRESS_OUTPUT=1
     30ISDESTREAKED_OBJECTS=      \
     31    ${COMMON_OBJECTS} \
     32    isdestreaked.o
     33
     34
     35HEADERS= Line.h  streaksastrom.h  streaksextern.h  streaksio.h  streaksremove.h
     36
     37STREAKSFLAGS=-DSTREAKS_COMPRESS_OUTPUT=1
    3138OPTFLAGS= -g -O2
    3239OPTFLAGS= -g
     
    3542LDFLAGS=`psmodules-config --libs`
    3643
    37 PROGRAMS= streaksremove streaksreplace streakscompare streaksrelease
     44PROGRAMS= streaksremove streaksreplace streakscompare streaksrelease isdestreaked
     45HEADERS=Line.h streaksastrom.h streaksextern.h streaksio.h streaksremove.h
    3846
    3947all:    ${PROGRAMS}
    4048
     49${REMOVE_OBJECTS}:      ${HEADERS}
    4150streaksremove:  ${REMOVE_OBJECTS}
    4251
     
    4756streaksrelease:  ${RELEASE_OBJECTS}
    4857
     58isdestreaked:   ${ISDESTREAKED_OBJECTS}
     59
     60
    4961install:        ${PROGRAMS}
    5062        install -t  $(PSCONFDIR)/$(PSCONFIG)/bin streaksremove
     
    5264        install -t  $(PSCONFDIR)/$(PSCONFIG)/bin streakscompare
    5365        install -t  $(PSCONFDIR)/$(PSCONFIG)/bin streaksrelease
     66        install -t  $(PSCONFDIR)/$(PSCONFIG)/bin isdestreaked
    5467
    5568clean:
  • branches/eam_branches/20090522/magic/remove/src/streaksastrom.c

    r21437 r24557  
    150150 
    151151bool
     152SkyToLocal(strkPt *outPt, strkAstrom *astrom, double ra, double dec)
     153{
     154    // generate a local project using the RA, DEC of the 0,0 pixel of the chip as the
     155    // projection center with the same plate scale as the nominal TP->Sky astrometry.
     156
     157    pmFPA *fpa   = (pmFPA *) astrom->fpa;
     158    pmChip *chip = (pmChip *) astrom->chip;
     159
     160    // find the RA,DEC coords of the 0,0 pixel for this chip:
     161
     162    psPlane ptTP;
     163    psSphere ptSky;
     164
     165    ptSky.r = ra;
     166    ptSky.d = dec;
     167    ptSky.rErr = 0.0;
     168    ptSky.dErr = 0.0;
     169
     170    psProject(&ptTP, &ptSky, fpa->toSky);
     171
     172    outPt->x = ptTP.x;
     173    outPt->y = ptTP.y;
     174
     175    return true;
     176}
     177
     178bool
     179LocalToSky(strkPt *outPt, strkAstrom *astrom, strkPt *inPt)
     180{
     181    // generate a local project using the RA, DEC of the 0,0 pixel of the chip as the
     182    // projection center with the same plate scale as the nominal TP->Sky astrometry.
     183
     184    pmFPA *fpa   = (pmFPA *) astrom->fpa;
     185    pmChip *chip = (pmChip *) astrom->chip;
     186
     187    // find the RA,DEC coords of the 0,0 pixel for this chip:
     188
     189    psPlane ptTP;
     190    psSphere ptSky;
     191
     192    ptTP.x = inPt->x;
     193    ptTP.y = inPt->y;
     194    ptTP.xErr = 0.0;
     195    ptTP.yErr = 0.0;
     196
     197    psDeproject(&ptSky, &ptTP, fpa->toSky);
     198
     199    outPt->x = ptSky.r;
     200    outPt->y = ptSky.d;
     201
     202    return true;
     203}
     204
     205bool
     206componentBounds(int *minX, int *minY, int *maxX, int *maxY, strkAstrom *astrom, int numCols, int numRows)
     207{
     208    // find the bounds of the (padded) chip region in tangent-plane coordinates
     209
     210    pmFPA *fpa   = (pmFPA *) astrom->fpa;
     211    pmChip *chip = (pmChip *) astrom->chip;
     212
     213    psPlane ptCH, ptFP, TPo, TPx, TPy;
     214
     215    // coordinate of the chip center:
     216    ptCH.x = 0.5*numCols;
     217    ptCH.y = 0.5*numRows;
     218    psPlaneTransformApply(&ptFP, chip->toFPA, &ptCH);
     219    psPlaneTransformApply(&TPo, fpa->toTPA,  &ptFP);
     220
     221    // coordinate of the chip center + dX/2:
     222    ptCH.x = numCols;
     223    ptCH.y = 0.5*numRows;
     224    psPlaneTransformApply(&ptFP, chip->toFPA, &ptCH);
     225    psPlaneTransformApply(&TPx, fpa->toTPA,  &ptFP);
     226
     227    // coordinate of the chip center + dY/2:
     228    ptCH.x = 0.5*numCols;
     229    ptCH.y = numRows;
     230    psPlaneTransformApply(&ptFP, chip->toFPA, &ptCH);
     231    psPlaneTransformApply(&TPy, fpa->toTPA,  &ptFP);
     232
     233    // half-lengths of the two sides in tangent-plane coords:
     234    double xSize = hypot (TPx.x - TPo.x, TPx.y - TPo.y);
     235    double ySize = hypot (TPy.x - TPo.x, TPy.y - TPo.y);
     236    double radius = hypot (xSize, ySize);
     237
     238    // define the region encompassed by the radius with some padding:
     239    *minX = TPo.x - 1.1*radius;
     240    *minY = TPo.y - 1.1*radius;
     241    *maxX = TPo.x + 1.1*radius;
     242    *maxY = TPo.y + 1.1*radius;
     243
     244    return true;
     245}
     246
     247bool
    152248skyToCell(strkPt *outPt, strkAstrom *astrom, double ra, double dec)
    153249{
  • branches/eam_branches/20090522/magic/remove/src/streaksastrom.h

    r21155 r24557  
    3232extern void linearizeTransforms(strkAstrom *astrom);
    3333
     34extern bool SkyToLocal(strkPt *outPt, strkAstrom *astrom, double ra, double dec);
     35extern bool LocalToSky(strkPt *outPt, strkAstrom *astrom, strkPt *inPt);
     36extern bool componentBounds(int *minX, int *minY, int *maxX, int *maxY, strkAstrom *astrom, int numCols, int numRows);
     37
    3438#endif // STREAKS_ASTROM_H
  • branches/eam_branches/20090522/magic/remove/src/streaksextern.c

    r21439 r24557  
    3636    StreakPixels *pixels = psArrayAllocEmpty (1024);
    3737    int streaksOnComponent = 0;
     38
     39    int minX, minY, maxX, maxY;
     40
     41    // find the chip dimensions in the tangent-plane coordinates (length of hypotenuse)
     42    componentBounds (&minX, &minY, &maxX, &maxY, astrom, numCols, numRows);
     43
    3844    for (i = 0; i != streaks->size; ++i)
    3945    {
     
    4147
    4248        line.width = streaks->list[i].width;
    43         if (skyToCell (&line.begin, astrom,
    44                        streaks->list[i].ra1, streaks->list[i].dec1) &&
    45             skyToCell (&line.end, astrom,
    46                        streaks->list[i].ra2, streaks->list[i].dec2) &&
     49
     50        /* Use tangent plane coordinates to narrow down the ra,dec range of the line closer to
     51         * the chip boundaries.  Use these new ra,dec positions to generate the line on the
     52         * chip using the full non-linear astrometry */
     53           
     54        // project the ends of the line using a linear projection centered on the chip center:
     55        Line full;
     56        SkyToLocal (&full.begin, astrom, streaks->list[i].ra1, streaks->list[i].dec1);
     57        SkyToLocal (&full.end,   astrom, streaks->list[i].ra2, streaks->list[i].dec2);
     58
     59        // clip the line to a square box with diameter = hypotenuse of the chip image centerd
     60        // on the chip center in tangent-plane coordinates.  skip the rest of this streak if
     61        // the line does not intersect this region
     62        if (!LineClipFull (&full, minX, minY, maxX, maxY)) {
     63            continue;
     64        }
     65
     66        // convert the end points back into ra, dec pairs:
     67        strkPt sky1, sky2;
     68        LocalToSky (&sky1, astrom, &full.begin);
     69        LocalToSky (&sky2, astrom, &full.end);
     70
     71        if (skyToCell (&line.begin, astrom, sky1.x, sky1.y) &&
     72            skyToCell (&line.end,   astrom, sky2.x, sky2.y) &&
    4773            LineClip (&line, numCols, numRows))
    4874        {
    49             PixelsFromLine (pixels, &line);
     75            PixelsFromLine (pixels, &line, numCols, numRows);
    5076            streaksOnComponent++;
    5177        }
  • branches/eam_branches/20090522/magic/remove/src/streaksio.c

    r23965 r24557  
    1919    psMemSetDeallocator(sf, (psFreeFunc) streakFilesFree);
    2020    memset(sf, 0, sizeof(*sf));
     21
     22    if (remove) {
     23        // remember pointer so that streaksExit can delete temps
     24        setStreakFiles(sf);
     25    }
    2126
    2227    sf->config = config;
     
    160165}
    161166
     167// figure out if a nebulous instance is a non-destreaked file
     168static bool
     169nebFileIsDestreaked(sFile *sfile)
     170{
     171    if (!sfile->resolved_name) {
     172        psError(PS_ERR_PROGRAMMING, true, "resolved name is null");
     173        return false;
     174    }
     175    psFits *fits = psFitsOpen(sfile->resolved_name, "r");
     176    if (!fits) {
     177        psError(PS_ERR_IO, true, "failed open %s", sfile->name);
     178        // can't tell if it is a destreaked file
     179        return false;
     180    }
     181    psMetadata *header = psFitsReadHeader(NULL, fits);
     182    if (!header) {
     183        psError(PS_ERR_IO, true, "failed to read header for: %s", sfile->name);
     184        return false;
     185    }
     186    bool mdok;
     187    bool isDestreaked = psMetadataLookupBool(&mdok, header, "PSDESTRK");
     188    if (mdok && isDestreaked) {
     189        return true;
     190    } else {
     191        psError(PS_ERR_IO, false, "output file already exists and may not be de-streaked: %s", sfile->name);
     192        return false;
     193    }
     194}
     195
    162196static psString
    163197resolveFilename(pmConfig *config, sFile *sfile, bool create)
     
    171205            // delete the existing file, since there may be more than one
    172206            // instance. It will get created below in pmConfigConvertFilename
    173             if (nebFind(server, sfile->name)) {
     207            if ((sfile->resolved_name = nebFind(server, sfile->name)) != NULL) {
     208                if (!nebFileIsDestreaked(sfile)) {
     209                    psError(PS_ERR_IO, false, "attempting to delete file that has not been destreaked %s", sfile->name);
     210                    return NULL;
     211                }
     212                nebFree(sfile->resolved_name);
     213                sfile->resolved_name = NULL;
    174214                nebDelete(server, sfile->name);
    175215            }
     
    193233    // all of the keywords in the raw image files written to the output destreaked files
    194234
    195     if (!CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
     235    if (!outputFilename && !CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
    196236        // stage is warp or diff AND fileSelect eq "INPUT"
    197237        // get data from pmFPAfile.
     
    250290    }
    251291
    252     // if outputFilename is not null name it contains the "directory"
     292    // if outputFilename is not null name it contains the "directory" (perhaps with a prefix like SR_)
    253293    // and outputFilename is the basename name of the file (or nebulous key)
    254294    // and the file is to be opened for writing
     
    334374
    335375void
     376addDestreakKeyword(psMetadata *header)
     377{
     378    psMetadataAddBool(header, PS_LIST_TAIL, "PSDESTRK", PS_META_REPLACE,
     379        "Have streaks been removed from image?", true);
     380}
     381
     382void
     383addRecoveryKeyword(psMetadata *header)
     384{
     385    psMetadataAddBool(header, PS_LIST_TAIL, "PSRECOVR", PS_META_REPLACE,
     386        "Does this image contain excised streak pixels?", true);
     387}
     388
     389void
    336390copyPHU(streakFiles *sfiles, bool remove)
    337391{
     
    344398        streaksExit("", PS_EXIT_DATA_ERROR);
    345399    }
    346 
    347     // TODO: add keyword indicating that streaks have been removed
     400    psMetadata *recHeader = NULL;
     401    if (remove && sfiles->recImage) {
     402       recHeader = psMetadataCopy(NULL, imageHeader);
     403       addRecoveryKeyword(recHeader);
     404    }
     405
     406    // add keyword indicating that streaks have been removed
     407    addDestreakKeyword(imageHeader);
     408
    348409    if (!psFitsWriteBlank(sfiles->outImage->fits, imageHeader, NULL)) {
    349410        psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    351412        streaksExit("", PS_EXIT_DATA_ERROR);
    352413    }
    353     // TODO: add keyword indicating that this is the recovery image
    354     if (remove && sfiles->recImage && !psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) {
     414    if (recHeader && !psFitsWriteBlank(sfiles->recImage->fits, recHeader, NULL)) {
    355415        psError(PS_ERR_IO, false, "failed to write primary header to %s",
    356416            sfiles->recImage->resolved_name);
    357417        streaksExit("", PS_EXIT_DATA_ERROR);
    358418    }
     419    psFree(recHeader);
     420    recHeader = NULL;
    359421    psFree(imageHeader);
    360422
     
    367429            streaksExit("", 1);
    368430        }
    369         // TODO: add keyword indicating that streaks have been removed
     431        if (remove && sfiles->recMask) {
     432            recHeader = psMetadataCopy(NULL, maskHeader);
     433            // add keyword indicating that this is the recovery image
     434           addRecoveryKeyword(recHeader);
     435        }
     436        // add keyword indicating that streaks have been removed
     437        addDestreakKeyword(maskHeader);
    370438        if (!psFitsWriteBlank(sfiles->outMask->fits, maskHeader, NULL)) {
    371439            psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    373441            streaksExit("", PS_EXIT_DATA_ERROR);
    374442        }
    375         // TODO: add keyword indicating that this is the recovery image
    376         if (remove && sfiles->recMask && !psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) {
     443        if (recHeader && !psFitsWriteBlank(sfiles->recMask->fits, recHeader, NULL)) {
    377444            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    378445                sfiles->recMask->resolved_name);
    379446            streaksExit("", PS_EXIT_DATA_ERROR);
    380447        }
     448        psFree(recHeader);
     449        recHeader = NULL;
    381450        psFree(maskHeader);
    382451    }
     
    389458            streaksExit("", 1);
    390459        }
    391         // TODO: add keyword indicating that streaks have been removed
     460        if (remove && sfiles->recWeight) {
     461            recHeader = psMetadataCopy(NULL, weightHeader);
     462            // add keyword indicating that this is a recovery image
     463           addRecoveryKeyword(recHeader);
     464        }
     465
     466        // add keyword indicating that streaks have been removed
     467        addDestreakKeyword(weightHeader);
    392468        if (!psFitsWriteBlank(sfiles->outWeight->fits, weightHeader, NULL)) {
    393469            psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    395471            streaksExit("", PS_EXIT_DATA_ERROR);
    396472        }
    397         // TODO: add keyword indicating that this is a recovery image
    398         if (remove && sfiles->recWeight && !psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) {
     473        if (recHeader && !psFitsWriteBlank(sfiles->recWeight->fits, recHeader, NULL)) {
    399474            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    400475                sfiles->recWeight->resolved_name);
     
    402477        }
    403478        psFree(weightHeader);
     479        psFree(recHeader);
    404480    }
    405481}
     
    566642
    567643static void
    568 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero)
     644setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero, psFitsCompressionType compType,
     645    psVector *tiles)
    569646{
    570647    if (!sfile) {
     
    579656    sfile->fits->options->bscale = bscale;
    580657    sfile->fits->options->bzero = bzero;
    581 }
    582 
    583 void
    584 copyFitsOptions(sFile *out, sFile *rec, sFile *in)
    585 {
     658
     659    psFitsSetCompression(sfile->fits, compType, tiles, 8, 0, 0);
     660}
     661
     662void
     663copyFitsOptions(sFile *out, sFile *rec, sFile *in, psVector *tiles)
     664{
     665    bool mdok;
     666    psString compTypeStr = psMetadataLookupStr(&mdok, in->header, "ZCMPTYPE");
     667    psFitsCompressionType compType = psFitsCompressionTypeFromString(compTypeStr);
     668    if (compType == PS_FITS_COMPRESS_NONE) {
     669        return;
     670    }
    586671    // Get current BITPIX, BSCALE, BZERO, EXTNAME
    587672    // Probably not necessary to look the numerical values up in this
     
    610695
    611696#ifdef STREAKS_COMPRESS_OUTPUT
    612     // Paul says that I should be able to leave this blank
    613     bitpix = 0;
    614     setFitsOptions(out, bitpix, bscale, bzero);
    615     setFitsOptions(rec, bitpix, bscale, bzero);
     697    // printf("%d %f %f\n", bitpix, bscale, bzero);
     698    setFitsOptions(out, bitpix, bscale, bzero, compType, tiles);
     699    setFitsOptions(rec, bitpix, bscale, bzero, compType, tiles);
    616700#endif
    617701}
     
    790874
    791875bool
    792 replicate(sFile *sfile, void *xattr)
    793 {
    794     if (!sfile->inNebulous) {
     876replicate(sFile *outFile, sFile *inFile)
     877{
     878    if (!outFile->inNebulous) {
    795879        return true;
    796880    }
    797881    nebServer *server = getNebServer(NULL);
    798882
    799     // for now just set "user.copies" to 2
    800     if (!nebSetXattr(server, sfile->name, "user.copies", "2",  NEB_REPLACE)) {
    801         psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", sfile->name, nebErr(server));
     883    char *user_copies = nebGetXattr(server, inFile->name, "user.copies");
     884    bool free_user_copies = true;
     885    if (user_copies == NULL) {
     886        user_copies = "2";
     887        free_user_copies = false;
     888    }
     889    if (!nebSetXattr(server, outFile->name, "user.copies", user_copies,  NEB_REPLACE)) {
     890        psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", outFile->name, nebErr(server));
    802891        return false;
    803892    }
    804     if (!nebReplicate(server, sfile->name, NULL, NULL)) {
    805         psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", sfile->name, nebErr(server));
     893    if (!nebReplicate(server, outFile->name, "any", NULL)) {
     894        psError(PM_ERR_UNKNOWN, true, "nebReplicate failed for %s\n%s", outFile->name, nebErr(server));
    806895        return false;
     896    }
     897    if (free_user_copies) {
     898        nebFree(user_copies);
    807899    }
    808900    return true;
     
    817909    bool status = false;
    818910
    819     // XXX: TODO: need a nebGetXatrr function, but there isn't one
    820     // another option would be to take the number of copies to be
    821     // created as an option. That way the system could decide
    822     // whether to replicate anything other than raw Image files
    823     void *xattr = NULL;
    824 
    825     if (!replicate(sfiles->outImage, xattr)) {
     911    if (!replicate(sfiles->outImage, sfiles->inImage)) {
    826912        psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    827913        return false;
    828914    }
    829915
    830 #ifdef notyet
    831     // XXX: don't replicate mask and weight images until we can look up
    832     // the input's xattr. There may be a perl program that can getXattr
    833916    if (sfiles->outMask) {
    834         // get xattr from input to see if we need to replicate
    835         if (!replicate(sfiles->outMask, xattr)) {
     917        if (!replicate(sfiles->outMask, sfiles->inMask)) {
    836918            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    837919            return false;
    838920        }
    839921    }
    840     if (sfiles->outWeight) {
    841         // get xattr from input to see if we need to replicate
    842         if (!replicate(sfiles->outWeight, xattr)) {
     922    if (sfiles->outChMask) {
     923        if (!replicate(sfiles->outChMask, sfiles->inChMask)) {
    843924            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    844925            return false;
    845926        }
    846927    }
    847 #endif
    848 
    849 //      replicate the recovery images (if in nebulous)
     928    if (sfiles->outWeight) {
     929        if (!replicate(sfiles->outWeight, sfiles->inWeight)) {
     930            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
     931            return false;
     932        }
     933    }
     934
     935//      XXX: replicate the recovery images (if in nebulous)
    850936//      perhaps whether we do that or not should be configurable.
    851937//      Sounds like we need a recipe
     
    904990    }
    905991
     992    if (sfiles->outChMask) {
     993        if (!swapOutputToInput(sfiles->inChMask, sfiles->outChMask)) {
     994            psError(PM_ERR_SYS, false, "failed to swap instances for chip mask.");
     995            return false;
     996        }
     997    }
     998
    906999    if (!swapOutputToInput(sfiles->inImage, sfiles->outImage)) {
    9071000        psError(PM_ERR_SYS, false, "failed to swap instances for Image.");
     
    9361029{
    9371030    if (sfiles->outMask) {
    938         if (!deleteFile(sfiles->outMask)) {
    939             psError(PM_ERR_SYS, false, "failed to delete Mask.");
    940             return false;
    941         }
     1031        deleteFile(sfiles->outMask);
     1032    }
     1033
     1034    if (sfiles->outChMask) {
     1035        deleteFile(sfiles->outChMask);
    9421036    }
    9431037
    9441038    if (sfiles->outWeight) {
    945         if (!deleteFile(sfiles->outWeight)) {
    946             psError(PM_ERR_SYS, false, "failed to delete Weight.");
    947             return false;
    948         }
    949     }
    950 
    951     if (!deleteFile(sfiles->outImage)) {
    952         psError(PM_ERR_SYS, false, "failed to delete Image.");
    953         return false;
     1039        deleteFile(sfiles->outWeight);
     1040    }
     1041
     1042    if (sfiles->outImage) {
     1043        deleteFile(sfiles->outImage);
    9541044    }
    9551045
  • branches/eam_branches/20090522/magic/remove/src/streaksio.h

    r23964 r24557  
    1414void copyPHU(streakFiles *sfiles, bool remove);
    1515void copyTable(sFile *out, sFile *in, int extnum);
    16 void copyFitsOptions(sFile *out, sFile *rec, sFile *in);
     16void copyFitsOptions(sFile *out, sFile *rec, sFile *in, psVector *tiles);
    1717void setupImageRefs(sFile *out, sFile *recoveryOut, sFile *in, int extnum, bool exciseAll);
    1818void strkGetMaskValues(streakFiles *sfiles, psU32 *maskStreak, psU32 *maskMask);
     
    2020void writeImage(sFile *sfile, psString extname, int extnum);
    2121void writeImageCube(sFile *sfile, psArray *imagecube, psString extname, int extnum);
    22 bool replicate(sFile *sfile, void *xattr);
     22bool replicate(sFile *outFile, sFile *inFile);
    2323void readImageFrom_pmFile(streakFiles *sf);
     24
     25void addDestreakKeyword(psMetadata *);
     26void addRecoveryKeyword(psMetadata *);
    2427
    2528bool streakFilesNextExtension(streakFiles *sf);
  • branches/eam_branches/20090522/magic/remove/src/streaksrelease.c

    r23965 r24557  
    2323    }
    2424
    25     psMetadata *masks = psMetadataLookupMetadata(&status, config->recipes, "MASKS");
    26     if (!status) {
    27         psError(PM_ERR_CONFIG, false, "failed to lookup MASKS in recipes\n");
    28         return PS_EXIT_CONFIG_ERROR;
    29     }
    30     psU8 poorWarp = (double) psMetadataLookupU8(&status, masks, "POOR.WARP");
    31     if (!status) {
    32         psError(PM_ERR_CONFIG, false, "failed to lookup mask value for POOR.WARP in recipes\n");
    33         return PS_EXIT_CONFIG_ERROR;
    34     }
    35     // we're setting pixels with any mask bits execpt POOR.WARP to NAN
    36     psU8 maskMask = ~poorWarp;
     25    // Values to set for masked pixels
     26    psU32 maskStreak = 0;           // for the image and weight (usually NAN, MAXINT for integer images)
     27    psU32 maskMask = 0;             // value looked up for MASK.STREAK
    3728
    3829    // Does true work here?
     
    6455        }
    6556
     57        // now that we've read the input files, lookup the mask values that we read
     58        if (maskStreak == 0) {
     59            strkGetMaskValues(sfiles, &maskStreak, &maskMask);
     60        }
     61
    6662        setMaskedToNAN(sfiles, maskMask, true);
    6763
     
    7773    printf("time to close images: %f\n", psTimerClear("CLOSE_IMAGES"));
    7874
    79 #ifdef NOTYET
    80     if (!replicateOutputs(sfiles)) {
    81         psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
    82         psErrorStackPrint(stderr, "");
    83         exit(PS_EXIT_UNKNOWN_ERROR);
    84     }
    85 
    86     if (psMetadataLookupBool(&status, config->arguments, "REPLACE")) {
    87         //     swap the instances for the input and output
    88         //     Note this is a database operation. No file I/O is performed
    89         if (!swapOutputsToInputs(sfiles)) {
    90             psError(PS_ERR_UNKNOWN, false, "failed to swap files");
    91 
    92             // XXX: Now what? I guess swapOutputsToInputs will need to undo anything that
    93             // it has done and give a detailed report of what happened
    94 
    95             psErrorStackPrint(stderr, "");
    96             exit(PS_EXIT_UNKNOWN_ERROR);
    97         }
    98 
    99         if (psMetadataLookupBool(&status, config->arguments, "REMOVE")) {
    100             //      delete the temporary storage objects (which now points to the original image(s)
    101             if (!deleteTemps(sfiles)) {
    102                 psError(PS_ERR_UNKNOWN, false, "failed to delete temporary files");
    103                 // XXX: Now what? At this point the output files have been swapped, so we can't
    104                 // repeat the operation.
    105 
    106                 // Returning error status here is problematic. The inputs have been streak removed
    107                 // but they're still lying around
    108                 // Maybe just print an error message and
    109                 // let other system tools clean up
    110                 psErrorStackPrint(stderr, "");
    111                 exit(PS_EXIT_UNKNOWN_ERROR);
    112             }
    113         }
    114     }
    115 #endif  // REPLACE, REMOVE
    11675    printf("time to run streaksrelease: %f\n", psTimerClear("STREAKSREMOVE"));
    11776
     
    275234
    276235    // set up the compression parameters
    277 #ifdef STREAKS_COMPRESS_OUTPUT
    278     // compression of the image pixels is disabled for now. Some consortium members
    279     // have problems reading them
    280     copyFitsOptions(sf->outImage, sf->recImage, sf->inImage);
    281 
    282     // XXX: TODO: can we derive these values from the input header?
    283     // psFitsCompressionGet(sf->inImage->image) gives compression none
    284     // perhaps we should just use the definition of COMP_IMG in the configuration
    285     psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    286     if (sf->recImage) {
    287         psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    288     }
    289 #endif
     236    copyFitsOptions(sf->outImage, sf->recImage, sf->inImage, sf->tiles);
    290237
    291238    if (sf->inMask) {
     
    306253            }
    307254
    308 #ifdef STREAKS_COMPRESS_OUTPUT
    309             // XXX: see note above
    310             copyFitsOptions(sf->outMask, sf->recMask, sf->inMask);
    311             psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    312             if (sf->recMask) {
    313                 psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    314             }
     255            copyFitsOptions(sf->outMask, sf->recMask, sf->inMask, sf->tiles);
    315256            if (sf->outChMask) {
    316                 copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask);
    317                 psFitsSetCompression(sf->outChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    318                 if (sf->recChMask) {
    319                     psFitsSetCompression(sf->recChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
    320                 }
    321             }
    322 #endif
     257                copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask, sf->tiles);
     258            }
    323259        }
    324260    }
     
    332268        setupImageRefs(sf->outWeight, sf->recWeight, sf->inWeight, sf->extnum, exciseAll);
    333269
    334 #ifdef STREAKS_COMPRESS_OUTPUT
    335         copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight);
    336         // XXX: see note above
    337         psFitsSetCompression(sf->outWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    338         if (sf->recWeight) {
    339             psFitsSetCompression(sf->recWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    340         }
    341 #endif
     270        copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight, sf->tiles);
    342271    }
    343272    // and for raw images, create sub images that represent the actual image
  • 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
  • branches/eam_branches/20090522/magic/remove/src/streaksremove.h

    r23963 r24557  
    8888extern ippStage parseStage(psString);
    8989extern psString pathToDirectory(char *path);
     90extern void setStreakFiles( streakFiles *);
    9091
    9192#define CHIP_LEVEL_INPUT(_stage) ((_stage == IPP_STAGE_RAW) || (_stage == IPP_STAGE_CHIP))
     
    9596#define IN_NEBULOUS(_filename) (!strncasecmp(_filename, "neb://", strlen("neb://")))
    9697
     98
    9799#endif // STREAKS_H
  • branches/eam_branches/20090522/magic/remove/src/streaksreplace.c

    r23965 r24557  
    9797    }
    9898
    99 #ifdef NOTYET
    100     if (psMetadataLookupBool(&status, config->arguments, "REPLACE")) {
    101         //     swap the instances for the input and output
    102         //     Note this is a database operation. No file I/O is performed
    103         if (!swapOutputsToInputs(sfiles)) {
    104             psError(PS_ERR_UNKNOWN, false, "failed to swap files");
    105 
    106             // XXX: Now what? I guess swapOutputsToInputs will need to undo anything that
    107             // it has done and give a detailed report of what happened
    108 
    109             psErrorStackPrint(stderr, "");
    110             exit(PS_EXIT_UNKNOWN_ERROR);
    111         }
    112 
    113         if (psMetadataLookupBool(&status, config->arguments, "REMOVE")) {
    114             //      delete the temporary storage objects (which now points to the original image(s)
    115             if (!deleteTemps(sfiles)) {
    116                 psError(PS_ERR_UNKNOWN, false, "failed to delete temporary files");
    117                 // XXX: Now what? At this point the output files have been swapped, so we can't
    118                 // repeat the operation.
    119 
    120                 // Returning error status here is problematic. The inputs have been streak removed
    121                 // but they're still lying around
    122                 // Maybe just print an error message and
    123                 // let other system tools clean up
    124                 psErrorStackPrint(stderr, "");
    125                 exit(PS_EXIT_UNKNOWN_ERROR);
    126             }
    127         }
    128     }
    129 #endif  // REPLACE, REMOVE
    13099//    nebServerFree(ourNebServer);
    131100    psFree(config);
     
    344313
    345314    // set up the compression parameters
    346 #ifdef STREAKS_COMPRESS_OUTPUT
    347     // compression of the image pixels is disabled for now. Some consortium members
    348     // have problems reading compressed images.
    349     copyFitsOptions(sf->outImage, sf->recImage, sf->inImage);
    350 
    351     // XXX: TODO: can we derive these values from the input header?
    352     // psFitsCompressionGet(sf->inImage->image) gives compression none
    353     // perhaps we should just use the definition of COMP_IMG in the configuration
    354     psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
    355 #endif
     315    copyFitsOptions(sf->outImage, sf->recImage, sf->inImage, sf->tiles);
     316
    356317    if (sf->inMask) {
    357318        readImage(sf->inMask, sf->extnum, sf->stage, true);
     
    362323        setupImageRefs(sf->outMask, NULL, sf->inMask, sf->extnum, false);
    363324
    364         // XXX: see note above
    365         copyFitsOptions(sf->outMask, NULL, sf->inMask);
    366         psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0);
     325        copyFitsOptions(sf->outMask, NULL, sf->inMask, sf->tiles);
    367326    }
    368327
     
    374333        setupImageRefs(sf->outMask, NULL, sf->inMask, sf->extnum, false);
    375334
    376         copyFitsOptions(sf->outWeight, NULL, sf->inWeight);
    377         // XXX: see note above
    378         psFitsSetCompression(sf->outWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0);
     335        copyFitsOptions(sf->outWeight, NULL, sf->inWeight, sf->tiles);
    379336    }
    380337
  • branches/eam_branches/20090522/magic/remove/src/streaksutil.c

    r20816 r24557  
    3333}
    3434
     35streakFiles *ourStreakFiles = NULL;
     36
     37void
     38setStreakFiles(streakFiles *sfiles)
     39{
     40    ourStreakFiles = sfiles;
     41}
     42
    3543// to enhance clarity in these programs we don't propagate errors up the stack
    3644// we just bail out
    3745void streaksExit(psString str, int exitCode) {
    3846    psErrorStackPrint(stderr, str);
     47    if (ourStreakFiles) {
     48        deleteTemps(ourStreakFiles);
     49    }
    3950    exit(exitCode);
    4051}
Note: See TracChangeset for help on using the changeset viewer.