IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
1 deleted
18 edited
4 copied

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/magic

    • Property svn:ignore
      •  

        old new  
        11magic
        22ssa-core-cpp
        3 Makefile
        43Makefile.bak
  • branches/simtest_nebulous_branches/magic/remove

    • Property svn:ignore set to
      configure
      Makefile.in
      Doxyfile
      config.log
      depcomp
      config.status
      config.guess
      ltmain.sh
      config.sub
      autom4te.cache
      libtool
      missing
      Makefile
      aclocal.m4
      install-sh

  • branches/simtest_nebulous_branches/magic/remove/src

    • Property svn:ignore
      •  

        old new  
        44streakscompare
        55streaksrelease
        6 makefile
         6Makefile
         7Makefile.in
         8config.h
         9.deps
         10streaksVersionDefinitions.h
         11config.h.in
         12stamp-h1
  • branches/simtest_nebulous_branches/magic/remove/src/Line.c

    r25001 r27840  
    3636    // If the current line is not vertical, check to see if the point
    3737    // is inside the two endpoints independent of direction
    38    
     38
    3939    if (line->begin.x != line->end.x)
    4040    {
     
    5959    ux = line->end.x - line->begin.x;
    6060    uy = line->end.y - line->begin.y;
    61    
     61
    6262    vx = x - line->begin.x;
    6363    vy = y - line->begin.y;
    64    
     64
    6565    double u_u = ux * ux + uy * uy;                         // (u . u)
    6666    double u_v = ux * vx + uy * vy;                         // (u . v)
    67    
     67
    6868    if (u_v <= 0) return vx * vx + vy * vy;                 // (v . v)
    6969    if (u_u <= u_v)
     
    7373        return wx * wx + wy * wy;                           // (w . w)
    7474    }
    75    
     75
    7676    // Compute P(b) is the base of the perpendicular dropped from tuple to
    7777    // the line
    78    
     78
    7979    b = u_v / u_u;
    8080    px = vx - b * ux;
     
    109109    double wx  = first->begin.x - second->begin.x;
    110110    double wy  = first->begin.y - second->begin.y;
    111    
     111
    112112    // Calculate the perpendicular product, dt
    113    
     113
    114114    double dt = dx1 * dy2 - dy1 * dx2;
    115    
     115
    116116    // Are the lines parallel?
    117    
     117
    118118    if (fabs (dt) <= 1e-8)
    119119    {
    120120        // Check to see if they are overlap
    121        
     121
    122122        if (dx1 * wy - dy1 * wx != 0 || dx2 * wy - dy2 * wx != 0) return 0;
    123        
     123
    124124        // The line are coplanar, so check to see if they are degenerate
    125        
     125
    126126        len1 = dx1 * dx1 + dy1 * dy1;
    127127        len2 = dx2 * dx2 + dy2 * dy2;
    128        
     128
    129129        // First, check to see if both lines are points and if they are
    130130        // distinct tuples
    131        
     131
    132132        if (len1 == 0 && len2 == 0)
    133133        {
     
    137137            return 1;
    138138        }
    139        
     139
    140140        // Check to see if the first line is a point and is inside the
    141141        // second line
    142        
     142
    143143        if (len1 == 0)
    144144        {
     
    148148                return 0;
    149149        }
    150        
     150
    151151        // Check to see if the second line is a point and is inside the
    152152        // first line
    153        
     153
    154154        if (len2 == 0)
    155155        {
     
    159159                return 0;
    160160        }
    161        
     161
    162162        // Since both lines are coplanar and have length, search for
    163163        // overlap tuples
    164        
     164
    165165        w2x = first->end.x - second->begin.x;
    166166        w2y = first->end.y - second->begin.y;
    167        
     167
    168168        t0 = (dx2 != 0) ? wx  / dx2 : wy  / dy2;
    169169        t1 = (dx2 != 0) ? w2x / dx2 : w2y / dy2;
    170        
     170
    171171        if (t0 > t1) SwapDouble (&t0, &t1);
    172172        if ((inFirstSegment || inSecondSegment) && (t0 > 1 || t1 < 0))
     
    176176        t0 = (t0 < 0) ? 0 : t0;             // Clip to min 0
    177177        t1 = (t1 > 1) ? 1 : t1;             // Clip to max 1
    178        
     178
    179179        // Set the first tuple of intersection
    180        
     180
    181181        tuple1->x = second->begin.x + t0 * dx2;
    182182        tuple1->y = second->begin.y + t0 * dy2;
    183        
     183
    184184        // Check to see if the intersection is a single tuple
    185        
     185
    186186        if (t0 == t1) return 1;
    187        
     187
    188188        // Otherwise, set the second tuple of intersection as well
    189        
     189
    190190        tuple2->x = second->begin.x + t1 * dx2;
    191191        tuple2->y = second->begin.y + t1 * dy2;
    192192        return 2;
    193193    }
    194    
     194
    195195    // The segments are skew and may intersect in a tuple.
    196196    // Get the intersect parameter for the first line
    197    
     197
    198198    double i1 = (dx2 * wy - dy2 * wx) / dt;
    199199    if (inFirstSegment && (i1 < 0 || i1 > 1)) return 0;
    200    
     200
    201201    // Get the intersect parameter for the second line
    202202
     
    226226    vertices[3].x = 0;       vertices[3].y = numRows;
    227227
     228    clipLine.begin.x = 0;
     229    clipLine.begin.y = 0;
     230    clipLine.end.x = 0;
     231    clipLine.end.y = 0;
     232
    228233    for (i = 0; i < 4 && found < 2; ++i)
    229234    {
     
    237242                ++found;
    238243            }
    239             else if (tuple1.x != clipLine.begin.x || 
     244            else if (tuple1.x != clipLine.begin.x ||
    240245                     tuple1.y != clipLine.begin.y)
    241246            {
     
    245250        }
    246251    }
    247    
     252
    248253    // If two endpoints are found, clip the line
    249    
     254
    250255    if (found > 1)
    251256    {
     
    283288    vertices[3].x = minX; vertices[3].y = maxY;
    284289
     290    clipLine.begin.x = 0;
     291    clipLine.begin.y = 0;
     292    clipLine.end.x = 0;
     293    clipLine.end.y = 0;
     294
    285295    for (i = 0; i < 4 && found < 2; ++i)
    286296    {
     
    294304                ++found;
    295305            }
    296             else if (tuple1.x != clipLine.begin.x || 
     306            else if (tuple1.x != clipLine.begin.x ||
    297307                     tuple1.y != clipLine.begin.y)
    298308            {
     
    302312        }
    303313    }
    304    
     314
    305315    // If two endpoints are found, clip the line
    306    
     316
    307317    if (found > 1)
    308318    {
     
    360370    @param[out] pixels list of PixelPos pointers corresponding
    361371                       based on the line settings
    362     @param[in] line Line to map to pixels   
     372    @param[in] line Line to map to pixels
    363373    @param[in] numCols maximum X (columns) for the line
    364374    @param[in] numRows maximum Y (rows) for the line            */
     
    367377{
    368378    Line offsetLine;
    369     PixelPos *pixel;
    370379    double slope, xOffset, yOffset, xMid, yMid;
    371380    int x, y, xBegin = numCols, yBegin = numRows, xEnd = 0, yEnd = 0;
    372381
    373382    // Extract the endpoints
    374    
     383
    375384    double x1 = line->begin.x;
    376385    double y1 = line->begin.y;
    377386    double x2 = line->end.x;
    378387    double y2 = line->end.y;
    379    
     388
    380389    // Determine the width and height of the line
    381    
     390
    382391    double dx = x2 - x1;
    383392    double dy = y2 - y1;
     
    403412
    404413    // Step point by point based on the dominate axis
    405    
     414
    406415    if (fabs (dx) > fabs (dy))
    407416    {
     
    411420        // If line is back to front, reorder endpoints and recompute
    412421        // the line width and height
    413        
     422
    414423        if (x1 > x2)
    415424        {
     
    421430
    422431        // Compute the slope of the line
    423        
     432
    424433        slope = dy / dx;
    425        
     434
    426435        // Compute the x and y offsets for the line width extent
    427436
    428         if (xBegin > xEnd) 
     437        if (xBegin > xEnd)
    429438            SwapInt (&xBegin, &xEnd);
    430439        else
     
    451460    }
    452461    else
    453     {       
     462    {
    454463        // -------------------
    455464        // vertical(ish) lines
     
    457466        // If line is back to front, reorder endpoints and recompute
    458467        // the line width and height
    459        
     468
    460469        if (y1 > y2)
    461470        {
     
    465474            dy = y2 - y1;
    466475        }
    467        
     476
    468477        // Compute the slope of the line
    469        
     478
    470479        slope = dx / dy;
    471        
     480
    472481        // Compute the x and y offsets for the line width extent
    473482
     
    481490        xMid = x1 + slope * (yBegin - y1);
    482491        xOffset = fabs (halfWidth * dr / dy);
    483        
     492
    484493        for (y = yBegin; y != yEnd; ++y)
    485494        {
  • branches/simtest_nebulous_branches/magic/remove/src/Makefile.simple

    r24761 r27840  
    1010
    1111REMOVE_OBJECTS=    \
    12     ${COMMON_OBJECTS} \
    1312    streaksremove.o \
    1413    streaksextern.o \
    15     warpedpixels.o \
     14    diffedpixels.o \
    1615    Line.o
    1716
    1817REPLACE_OBJECTS=      \
    19     ${COMMON_OBJECTS} \
    2018    streaksreplace.o
    2119
    2220COMPARE_OBJECTS=      \
    23     ${COMMON_OBJECTS} \
    2421    streakscompare.o
    2522
    2623RELEASE_OBJECTS=      \
    27     ${COMMON_OBJECTS} \
    2824    streaksrelease.o
    2925
    3026ISDESTREAKED_OBJECTS=      \
    31     ${COMMON_OBJECTS} \
    3227    isdestreaked.o
    3328
     
    3631
    3732STREAKSFLAGS=-DSTREAKS_COMPRESS_OUTPUT=1
    38 OPTFLAGS= -g -O2
     33OPTFLAGS= -g -O2 -Wall -Werror
    3934#OPTFLAGS= -g
    40 CFLAGS=`psmodules-config --cflags` -std=gnu99 ${OPTFLAGS} ${STREAKSFLAGS}
    41 #CFLAGS=`psmodules-config --cflags` -std=gnu99 ${OPTFLAGS}
    42 LDFLAGS=`psmodules-config --libs`
     35CFLAGS=`psmodules-config --cflags` `pslib-config --cflags` -std=gnu99 ${OPTFLAGS} ${STREAKSFLAGS}
     36#CFLAGS=`psmodules-config --cflags` `pslib-config --cflags` -std=gnu99 ${OPTFLAGS}
     37LDFLAGS=`psmodules-config --libs` `pslib-config --libs`
    4338
    4439PROGRAMS= streaksremove streaksreplace streakscompare streaksrelease isdestreaked
    4540HEADERS=Line.h streaksastrom.h streaksextern.h streaksio.h streaksremove.h
    4641
    47 all:    ${PROGRAMS}
     42all: ${PROGRAMS}
    4843
    49 ${REMOVE_OBJECTS}:      ${HEADERS}
    50 streaksremove:  ${REMOVE_OBJECTS}
     44# programs all depend on the common objects and the common headers
     45PROGRAM_OBJECTS = \
     46 ${REMOVE_OBJECTS} \
     47 ${REPLACE_OBJECTS} \
     48 ${COMPARE_OBJECTS} \
     49 ${RELEASE_OBJECTS} \
     50 ${ISDESTREAKED_OBJECTS}
    5151
    52 streaksreplace:  ${REPLACE_OBJECTS}
     52${COMMON_OBJECTS}: ${HEADERS}
    5353
    54 streakscompare:  ${COMPARE_OBJECTS}
     54${PROGRAM_OBJECTS}: ${HEADERS} ${COMMON_OBJECTS}
    5555
    56 streaksrelease:  ${RELEASE_OBJECTS}
     56streaksremove:  ${REMOVE_OBJECTS} ${COMMON_OBJECTS}
    5757
    58 isdestreaked:   ${ISDESTREAKED_OBJECTS}
     58streaksreplace:  ${REPLACE_OBJECTS} ${COMMON_OBJECTS}
     59
     60streakscompare:  ${COMPARE_OBJECTS} ${COMMON_OBJECTS}
     61
     62streaksrelease:  ${RELEASE_OBJECTS} ${COMMON_OBJECTS}
     63
     64isdestreaked:   ${ISDESTREAKED_OBJECTS} ${COMMON_OBJECTS}
    5965
    6066
  • branches/simtest_nebulous_branches/magic/remove/src/isdestreaked.c

    r24715 r27840  
    11#include "streaksremove.h"
     2
     3char *streaksProgram = "isdestreaked";
    24
    35int
     
    1820    psFits *fits = psFitsOpen(fileName, "r");
    1921    if (!fits) {
    20         psError(PS_ERR_UNKNOWN, false, "failed to open fits file: %s\n", fileName);
    21         psErrorStackPrint(stderr, "");
     22        psErrorStackPrint(stderr, "failed to open fits file: %s\n", fileName);
    2223        exit(PS_EXIT_DATA_ERROR);
    2324    }
     
    2526    psMetadata *header = psFitsReadHeader(NULL, fits);
    2627    if (!header) {
    27         psError(PS_ERR_IO, false, "failed to read header for: %s", fileName);
    28         psErrorStackPrint(stderr, "");
     28        psErrorStackPrint(stderr, "failed to read header for: %s", fileName);
    2929        exit(PS_EXIT_DATA_ERROR);
    3030    }
  • branches/simtest_nebulous_branches/magic/remove/src/streaksastrom.c

    r24716 r27840  
    9393    // since the transform already accounts for the chip parity we just use the cell parity
    9494    astrom->xParity = (double) xParityCell;
    95     astrom->yParity = (double) yParityCell; 
     95    astrom->yParity = (double) yParityCell;
    9696
    9797    astrom->numCols = numCols;
     
    135135
    136136void
    137 cellToChipInt(int *xChip, int *yChip, strkAstrom *astrom, int xCell, int yCell)
     137cellToChipInt(unsigned int *xChip, unsigned int *yChip, strkAstrom *astrom, int xCell, int yCell)
    138138{
    139139    if (astrom->xParity > 0) {
     
    148148    }
    149149}
    150  
     150
    151151bool
    152152SkyToLocal(strkPt *outPt, strkAstrom *astrom, double ra, double dec)
     
    156156
    157157    pmFPA *fpa   = (pmFPA *) astrom->fpa;
    158     pmChip *chip = (pmChip *) astrom->chip;
    159158
    160159    // find the RA,DEC coords of the 0,0 pixel for this chip:
     
    183182
    184183    pmFPA *fpa   = (pmFPA *) astrom->fpa;
    185     pmChip *chip = (pmChip *) astrom->chip;
    186184
    187185    // find the RA,DEC coords of the 0,0 pixel for this chip:
     
    251249    pmChip *chip = (pmChip *) astrom->chip;
    252250    pmAstromObj *pt = (pmAstromObj *) astrom->pt;
    253    
     251
    254252    pt->sky->r = ra;
    255253    pt->sky->d = dec;
     
    276274    pmChip *chip = (pmChip *) astrom->chip;
    277275    pmAstromObj *pt = (pmAstromObj *) astrom->pt;
    278    
     276
    279277    cellToChip(&pt->chip->x, &pt->chip->y, astrom, x, y);
    280278
     
    294292}
    295293
    296  
     294
    297295static bool
    298296readAstrometry(streakFiles *sf)
     
    362360            break;
    363361        }
    364     } 
     362    }
    365363    if (!sf->chip) {
    366364        psError(PS_ERR_UNKNOWN, true, "Failed to find chip with data.");
     
    380378linearizeTransforms(strkAstrom *astrom)
    381379{
    382     if (!pmAstromLinearizeTransforms((pmFPA *) astrom->fpa, (pmChip *) astrom->chip)) {
    383         // streaksExit("linear fit to astrometry failed\n", PS_EXIT_UNKNOWN_ERROR);
    384         psError(PS_ERR_UNKNOWN, false, "linear fit to astrometry failed ignoring");
     380    if (!pmAstromLinearizeTransforms((pmFPA *) astrom->fpa, (pmChip *) astrom->chip, NULL, NULL, NULL, 0, 0)) {
     381        psErrorStackPrint(stderr, "linear fit to astrometry failed. ignoring\n");
    385382        return false;
    386383    }
  • branches/simtest_nebulous_branches/magic/remove/src/streakscompare.c

    r21156 r27840  
    11#include "streaksremove.h"
     2
     3char *streaksProgram = "streakscompare";
    24
    35static pmConfig * parseArguments(int, char **);
     
    57int main(int argc, char *argv[])
    68{
    7     long i;
    89    bool status;
    910
     
    3536    int numErrors = 0;
    3637    for (int component = 0; component < ncomponents; component++) {
    37         if (component && !(psFitsMoveExtNum(file1->fits, 1, true) 
     38        if (component && !(psFitsMoveExtNum(file1->fits, 1, true)
    3839                           && psFitsMoveExtNum(file2->fits, 1, true))) {
    3940            streaksExit("failed to advance to next extesion\n", PS_EXIT_DATA_ERROR);
     
    4546        psImage *image2 = file2->image;
    4647
    47         // TODO: do more sanity checking. For example check that extname's  (if any) match 
     48        // TODO: do more sanity checking. For example check that extname's  (if any) match
    4849        // check for matching image cubes
    4950        if (image1 && image2) {
     
    6869                        error = ! isnan(value1) && isnan(value2);
    6970                    }
    70                    
     71
    7172                    if (error) {
    7273                        numErrors++;
  • branches/simtest_nebulous_branches/magic/remove/src/streaksextern.c

    r25001 r27840  
    7878        }
    7979    }
    80     printf("%d streaks on this component\n", streaksOnComponent);
     80    fprintf(stderr, "%d streaks on this component\n", streaksOnComponent);
    8181    return pixels;
    8282}
  • branches/simtest_nebulous_branches/magic/remove/src/streaksextern.h

    r25001 r27840  
    5454            pixels
    5555*/
    56    
     56
    5757extern StreakPixels *streak_on_component(Streaks *streaks, strkAstrom *astrom,
    5858                                         int numCols, int numRows);
  • branches/simtest_nebulous_branches/magic/remove/src/streaksio.c

    r24853 r27840  
    1212// Assumptions about the file structure of non-raw files
    1313// The 'image' for each file (image, mask weight) is contained in the first
    14 // extension. 
     14// extension.
    1515
    1616
     
    121121    sf->transparentStreaks = psMetadataLookupF64(&status, config->arguments, "TRANSPARENT_STREAKS");
    122122
     123    sf->stats = psMetadataAlloc();
     124    psString statsFileName= psMetadataLookupStr(&status, config->arguments, "STATS");
     125    if (statsFileName) {
     126        sf->statsFile = fopen(statsFileName, "w");
     127        if (!sf->statsFile) {
     128            psError(PS_ERR_IO, true, "failed to open stats file %s", statsFileName);
     129            streaksExit("", PS_EXIT_CONFIG_ERROR);
     130        }
     131    }
     132
    123133    return sf;
    124134}
     
    128138{
    129139    freeImages(sf);
    130     psFree(sf->warpedPixels);
     140    psFree(sf->diffedPixels);
    131141    psFree(sf->tiles);
    132142    psFree(sf->view);
     
    288298        sfile->resolved_name = psStringCopy(sfile->pmfile->filename);
    289299
     300        sfile->nHDU = psFitsGetSize(sfile->pmfile->fits);
     301
    290302        // copy header from fpu
    291303        sfile->header = (psMetadata*) psMemIncrRefCounter(sfile->pmfile->fpa->hdu->header);
    292 
    293         sfile->nHDU = psFitsGetSize(sfile->pmfile->fits);
    294304
    295305        return sfile;
     
    625635        streaksExit("", PS_EXIT_DATA_ERROR);
    626636    }
    627  
     637
    628638    bool status;
    629639    in->extname = psMetadataLookupStr(&status, in->header, "EXTNAME");
     
    654664            streaksExit("", PS_EXIT_DATA_ERROR);
    655665        }
    656         psImage *image = (psImage *) (in->imagecube->data[0]);
     666
     667        // Ensure input is of the expected type
     668        psDataType expected = isMask ? PS_TYPE_IMAGE_MASK : PS_TYPE_F32; // Expected type for image
     669        for (int i = 0; i < in->imagecube->n; i++) {
     670            psImage *image = in->imagecube->data[i]; // Image of interest
     671            if (image->type.type != expected) {
     672                psImage *temp = psImageCopy(NULL, image, expected);
     673                psFree(image);
     674                in->imagecube->data[i] = temp;
     675            }
     676        }
    657677    }
    658678    setDataExtent(stage, in, (stage == IPP_STAGE_RAW) && !isMask);
     
    671691    sfile->fits->options = psFitsOptionsAlloc();
    672692    sfile->fits->options->scaling = PS_FITS_SCALE_MANUAL;
     693    sfile->fits->options->fuzz = false;
    673694    sfile->fits->options->bitpix = bitpix;
    674695    sfile->fits->options->bscale = bscale;
     
    789810        return;
    790811    }
     812    streaksVersionHeaderFull(sfile->header);
     813
    791814    if (!psFitsWriteImage(sfile->fits, sfile->header, sfile->image, 0, extname)) {
    792815        psError(PS_ERR_IO, false, "failed to write image to %s extnum: %d",
     
    806829        return;
    807830    }
     831    streaksVersionHeaderFull(sfile->header);
    808832    if (!psFitsWriteImageCube(sfile->fits, sfile->header, imagecube, extname)) {
    809833        psError(PS_ERR_IO, false, "failed to write image to %s extnum: %d",
     
    840864    sfile->header = NULL;
    841865    psFree(sfile->image);
    842     sfile->header = NULL;
     866    sfile->image = NULL;
    843867    psFree(sfile->imagecube);
    844     sfile->header = NULL;
     868    sfile->imagecube = NULL;
    845869}
    846870
     
    848872closeImages(streakFiles *sf)
    849873{
     874    if (sf->stage == IPP_STAGE_RAW) {
     875        if (sf->view) {
     876            sf->view->readout = -1;
     877            pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
     878            sf->view->cell = -1;
     879            pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
     880            sf->view->chip = -1;
     881            pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
     882        }
     883    }
    850884    closeImage(sf->inImage);
    851885    closeImage(sf->outImage);
     
    902936    bool free_user_copies = true;
    903937    if (user_copies == NULL) {
    904         user_copies = "2";
    905         free_user_copies = false;
     938        // input does not have replication requested
     939        return true;
    906940    }
    907941    if (!nebSetXattr(server, outFile->name, "user.copies", user_copies,  NEB_REPLACE)) {
     
    909943        return false;
    910944    }
    911     if (!nebReplicate(server, outFile->name, "any", NULL)) {
     945    if (!nebReplicate(server, outFile->name, NULL, NULL)) {
    912946        psError(PM_ERR_UNKNOWN, true, "nebReplicate failed for %s\n%s", outFile->name, nebErr(server));
    913947        return false;
     
    925959replicateOutputs(streakFiles *sfiles)
    926960{
    927     bool status = false;
    928 
    929961    if (!replicate(sfiles->outImage, sfiles->inImage)) {
    930962        psError(PM_ERR_SYS, false, "failed to replicate outImage.");
     
    9731005
    9741006        if (!nebSwap(server, in->name, out->name)) {
    975             psError(PM_ERR_SYS, true, "failed to swap files for: %s.",
     1007            psError(PM_ERR_SYS, true, "failed to swap files for %s: %s.",
    9761008                in->name, nebErr(server));
    9771009            return false;
     
    10971129setMaskedToNAN(streakFiles *sfiles, psU32 maskMask, bool printCounts)
    10981130{
    1099         int maskedPixels = 0;
    1100         int nandPixels = 0;
    1101         int nandWeights = 0;
     1131        long maskedPixels = 0;
     1132        long nandPixels = 0;
     1133        long nandWeights = 0;
    11021134
    11031135        psImage *image = sfiles->outImage->image;
     1136        int imageType = image->type.type;
     1137        double exciseValue = sfiles->inImage->exciseValue;
     1138
    11041139        psImage *mask = sfiles->inMask->image;
    11051140        psImage *weight = NULL;
     1141        int weightType = 0;
     1142        double weightExciseValue = NAN;
    11061143        if (sfiles->outWeight) {
    11071144            weight = sfiles->outWeight->image;
    1108         }
    1109         double exciseValue = sfiles->inImage->exciseValue;
     1145            weightType = weight->type.type;
     1146            weightExciseValue = sfiles->inWeight->exciseValue;
     1147        }
    11101148
    11111149        if (printCounts) {
     
    11171155                // these gets are not necessary, we could just set the pixels to nan
    11181156                // but I want to get the counts
    1119                 double imageVal  = psImageGet(image, x, y);
    1120                 psU32 maskVal;
     1157                double imageVal = 0; // avoid compiler warning
     1158                if (imageType == PS_TYPE_F32) {
     1159                    imageVal  = image->data.F32[y][x];
     1160                } else if (imageType == PS_TYPE_S16) {
     1161                    imageVal = image->data.S16[y][x];
     1162                } else if (imageType == PS_TYPE_U16) {
     1163                    imageVal = image->data.U16[y][x];
     1164                } else {
     1165                    psError(PS_ERR_PROGRAMMING, true, "unexpected image type found: %d\n",
     1166                                imageType);
     1167                    streaksExit("", PS_EXIT_PROG_ERROR);
     1168                }
     1169                psU32 maskVal = 0;
    11211170                if (sfiles->stage == IPP_STAGE_RAW) {
    1122                     int xChip, yChip;
     1171                    unsigned int xChip, yChip;
    11231172                    cellToChipInt(&xChip, &yChip, sfiles->astrom, x, y);
    1124                     maskVal = psImageGet(mask, xChip, yChip);
     1173                    maskVal = mask->data.PS_TYPE_IMAGE_MASK_DATA[yChip][xChip];
    11251174                } else {
    1126                     maskVal = psImageGet(mask, x, y);
     1175                    maskVal = mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x];
    11271176                }
    11281177                if (maskVal & maskMask) {
    11291178                    ++maskedPixels;
    1130                     if (!isExciseValue(imageVal, sfiles->inImage->exciseValue)) {
     1179                    if (!isExciseValue(imageVal, exciseValue)) {
    11311180                        ++nandPixels;
    1132                         psImageSet(image, x, y, exciseValue);
     1181                        if (imageType == PS_TYPE_F32) {
     1182                            image->data.F32[y][x] = exciseValue;
     1183                        } else if (imageType == PS_TYPE_S16) {
     1184                            image->data.S16[y][x] = exciseValue;
     1185                        } else if (imageType == PS_TYPE_U16) {
     1186                            image->data.U16[y][x] = exciseValue;
     1187                        } else {
     1188                            psError(PS_ERR_PROGRAMMING, true, "unexpected image type found: %d\n",
     1189                                imageType);
     1190                            streaksExit("", PS_EXIT_PROG_ERROR);
     1191                        }
    11331192                    }
    11341193                    if (weight) {
    1135                         double weightVal = weight ? psImageGet(weight, x, y) : 0;
    1136                         if (!isnan(weightVal)) {
    1137                             ++nandWeights;
    1138                             psImageSet(weight, x, y, NAN);
     1194                        if (weightType == PS_TYPE_F32) {
     1195                            double weightVal = weight->data.F32[y][x];
     1196                            if (!isnan(weightVal)) {
     1197                                ++nandWeights;
     1198                                weight->data.F32[y][x] = NAN;
     1199                            }
     1200                        } else if(weightType == PS_TYPE_S16) {
     1201                            double weightVal = weight->data.S16[y][x];
     1202                            if (!isExciseValue(weightVal, weightExciseValue)) {
     1203                                ++nandWeights;
     1204                                image->data.S16[y][x] = weightExciseValue;
     1205                            }
     1206                        } else if (weightType == PS_TYPE_U16) {
     1207                            double weightVal = weight->data.U16[y][x];
     1208                            if (!isExciseValue(weightVal, weightExciseValue)) {
     1209                                ++nandWeights;
     1210                                image->data.U16[y][x] = weightExciseValue;
     1211                            }
     1212                        } else {
     1213                            psError(PS_ERR_PROGRAMMING, true, "unexpected image type found: %d\n",
     1214                                    weightType);
     1215                                streaksExit("", PS_EXIT_PROG_ERROR);
    11391216                        }
    11401217                    }
     
    11441221        if (printCounts) {
    11451222            psLogMsg(sfiles->program_name, PS_LOG_INFO, "time to NAN mask pixels: %f\n", psTimerClear("NAN_MASKED"));
    1146             int totalPixels = image->numRows * image->numCols;
     1223            long totalPixels = image->numRows * image->numCols;
    11471224            psLogMsg(sfiles->program_name, PS_LOG_INFO, "pixels:        %10ld\n", totalPixels);
    11481225            psLogMsg(sfiles->program_name, PS_LOG_INFO, "masked pixels: %10ld %4.2f%%\n", maskedPixels, 100. * maskedPixels / totalPixels);
     
    11671244        psMetadataAddU16(in->header, PS_LIST_TAIL, "BLANK", 0, "", 65535);
    11681245        psMetadataAddU16(in->header, PS_LIST_TAIL, "ZBLANK", 0, "", 65535);
    1169     } else {
     1246    } else if (in->image->type.type == PS_TYPE_S16) {
     1247        in->exciseValue = 32767;
     1248        psMetadataAddU16(in->header, PS_LIST_TAIL, "BLANK", 0, "", 32767);
     1249        psMetadataAddU16(in->header, PS_LIST_TAIL, "ZBLANK", 0, "", 32767);
     1250    } else if (in->image->type.type == PS_TYPE_F32) {
    11701251        in->exciseValue = NAN;
    1171     }
    1172 }
    1173 
    1174 void
    1175 strkGetMaskValues(streakFiles *sfiles, psU32 *maskStreak, psU32 *maskMask)
    1176 {
     1252    } else {
     1253        psError(PS_ERR_PROGRAMMING, true, "unexpected image type found: %d\n", in->image->type.type);
     1254        streaksExit("", PS_EXIT_PROG_ERROR);
     1255    }
     1256}
     1257
     1258// Get the mask values that we need.
     1259// If an input mask file is provided get them from there.
     1260// Otherwise get them from the recipe MASKS
     1261void
     1262strkGetMaskValues(streakFiles *sfiles)
     1263{
     1264    if (sfiles->maskStreak) {
     1265        // already done
     1266        return;
     1267    }
    11771268    if (sfiles->inMask && sfiles->inMask->header) {
    11781269        if (!pmConfigMaskReadHeader(sfiles->config, sfiles->inMask->header)) {
     
    11871278        streaksExit("", PS_EXIT_CONFIG_ERROR);
    11881279    }
    1189     *maskStreak = psMetadataLookupU32(&status, masks, "STREAK");
     1280    sfiles->maskStreak = psMetadataLookupU32(&status, masks, "STREAK");
    11901281    if (!status) {
    11911282        psError(PM_ERR_CONFIG, false, "failed to lookup mask value for STREAK in recipes\n");
     
    12031294        }
    12041295    }
    1205     *maskMask = ~convPoor;
     1296    sfiles->maskMask = ~convPoor;
    12061297}
    12071298
     
    12151306        if (SFILE_IS_IMAGE(in)) {
    12161307            // Turn off compression (code adapted from pmFPAWrite)
    1217 #ifdef SAVE_AND_RESTORE_COMPRESSION
    12181308            int bitpix = out->fits->options ? out->fits->options->bitpix : 0; // Desired bits per pixel
    12191309            psFitsCompression *compress = psFitsCompressionGet(out->fits); // Current compression options
    1220 #endif
    12211310            if (!psFitsSetCompression(out->fits, PS_FITS_COMPRESS_NONE, NULL, 0, 0, 0)) {
    12221311                psError(PM_ERR_UNKNOWN, false, "failed to turn off compression for extension %d\n", extnum);
    12231312                streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    12241313            }
    1225 #ifdef SAVE_AND_RESTORE_COMPRESSION
    12261314            if (out->fits->options) {
    12271315                out->fits->options->bitpix = 0;
     
    12381326                streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    12391327            }
    1240 #endif
    12411328        } else {
    12421329            copyTable(out, in, extnum);
  • branches/simtest_nebulous_branches/magic/remove/src/streaksio.h

    r24853 r27840  
    1616void copyFitsOptions(sFile *out, sFile *rec, sFile *in, psVector *tiles);
    1717void setupImageRefs(sFile *out, sFile *recoveryOut, sFile *in, int extnum, bool exciseAll);
    18 void strkGetMaskValues(streakFiles *sfiles, psU32 *maskStreak, psU32 *maskMask);
     18void strkGetMaskValues(streakFiles *sfiles);
    1919void copyExtraExtensions(streakFiles *sfiles);
    2020
  • branches/simtest_nebulous_branches/magic/remove/src/streaksrelease.c

    r24853 r27840  
    33static pmConfig *parseArguments(int argc, char **argv);
    44static bool readAndCopyToOutput(streakFiles *sf, bool exciseAll);
    5 static StreakPixels * getStreakPixels(streakFiles *sfiles, Streaks *streaks);
    6 static void exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue);
    7 static bool warpedPixel(streakFiles *sfiles, PixelPos *cellCoord);
    8 static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue);
    95static void writeImages(streakFiles *sf, bool exciseImageCube);
     6
     7char *streaksProgram = "streaksrelease";
    108
    119int
    1210main(int argc, char *argv[])
    1311{
    14     bool status;
    15 
    1612    psLibInit(NULL);
    1713    psTimerStart("STREAKSREMOVE");
     
    2218        return PS_EXIT_CONFIG_ERROR;
    2319    }
    24 
    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
    2820
    2921    // Does true work here?
     
    5648
    5749        // 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 
    62         setMaskedToNAN(sfiles, maskMask, true);
     50        strkGetMaskValues(sfiles);
     51
     52        if (!sfiles->inImage->imagecube) {
     53            setMaskedToNAN(sfiles, sfiles->maskMask, true);
     54        }
    6355
    6456        // write out the destreaked temporary images and the recovery images
     
    216208        // image data directly from psFits
    217209        readImage(sf->inImage, sf->extnum, sf->stage, false);
    218        
     210
    219211        // astrom struct is only needed for raw stage, and only the chip to cell parameters are used
    220212        sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, NULL, NULL, false,
     
    232224    if (sf->inImage->image) {
    233225        setupImageRefs(sf->outImage, sf->recImage, sf->inImage, sf->extnum, exciseAll);
    234     } else if (sf->inImage->imagecube) {
    235         // Image cubes should have been excised in the destreaking process
    236         streaksExit("unexpected imagecube found", PS_EXIT_CONFIG_ERROR);
    237226    }  else {
    238         return false;
     227        // image cubes are handled specially
    239228    }
    240229
     
    284273
    285274
     275// XXXX: why isn't this in streaksio.c ?? See also streakremove.c
    286276static void
    287277writeImages(streakFiles *sf, bool exciseImageCube)
     
    304294            initValue = NAN;
    305295        } else {
    306             // otherwise write it to the output 
     296            // otherwise write it to the output
    307297            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    308298            initValue = 0;
    309299        }
    310300
    311         // borrow one of the images from the imagecube and set it to init value
    312         psImage *image = psArrayGet (sf->inImage->imagecube, 0);
    313         psMemIncrRefCounter(image);
    314         psImageInit(image, initValue);
    315         if (exciseImageCube) {
    316             sf->outImage->image = image;
    317             writeImage(sf->outImage, extname, sf->extnum);
    318         } else {
    319             // write zero valued image to reccovery
    320             if (sf->recImage) {
    321                 sf->recImage->image = image;
    322                 writeImage(sf->recImage, extname, sf->extnum);
    323             }
    324         }
    325301        // Assumption: there are no mask and weight images for video cells
    326302        return;
  • branches/simtest_nebulous_branches/magic/remove/src/streaksremove.c

    r25001 r27840  
    1212static pmConfig *parseArguments(int argc, char **argv);
    1313static bool readAndCopyToOutput(streakFiles *sf, bool exciseAll);
    14 static void exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue);
    15 static bool warpedPixel(streakFiles *sfiles, int x, int y);
    16 static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue);
     14static long exciseNonDiffedPixels(streakFiles *sfiles, psImageMaskType newMaskValue);
     15static bool diffedPixel(streakFiles *sfiles, int x, int y);
     16static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue);
    1717static void writeImages(streakFiles *sf, bool exciseImageCube);
    1818static void updateAstrometry(streakFiles *sfiles);
    19 static void censorSources(streakFiles *sfiles, psU32 maskStreak);
    20 
     19static void censorSources(streakFiles *sfiles, psImageMaskType maskStreak);
     20static long censorPixels(streakFiles *sfiles, psImage * pixels, bool checkNonDiffedPixels, psU16 maskStreak);
     21
     22char *streaksProgram = "streaksremove";
     23
     24// Note: For clarity the flow of this program is in main().
     25// There is not a lot of error checking is done in main.
     26// Until the end, where we might be doing Nebulous operations, called functions exit when an error
     27// is encountered.
    2128int
    2229main(int argc, char *argv[])
     
    2532
    2633    psLibInit(NULL);
    27     psTimerStart("STREAKSREMOVE");
     34    psTimerStart("TOTAL_TIME");
    2835
    2936    pmConfig *config = parseArguments(argc, argv);
     
    3340    }
    3441
    35     // Values to set for masked pixels
    36     psU32 maskStreak = 0;           // for the image and weight (usually NAN, MAXINT for integer images)
    37     psU32 maskMask = 0;             // value looked up for MASK.STREAK
    38 
    3942    psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS");
    4043
     
    4245    Streaks *streaks = readStreaksFile(streaksFileName);
    4346    if (!streaks) {
    44         psError(PS_ERR_UNKNOWN, "failed to read streaks file: %s", streaksFileName);
     47        psError(PS_ERR_UNKNOWN, false, "failed to read streaks file: %s", streaksFileName);
    4548        streaksExit("", PS_EXIT_PROG_ERROR);
    4649    }
     
    4952    streakFiles *sfiles = openFiles(config, true, argv[0]);
    5053    setupAstrometry(sfiles);
     54    sfiles->stats = psMetadataAlloc();
    5155
    5256    // Optionally we can set pixels that are masked to NAN since they couldn't have been
     
    6064
    6165    bool exciseAll = false;
    62     // --keepnonwarped is a test and debug mode
    63     bool keepNonWarpedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_WARPED");
    64 
    65     // we need to check for non warped pixels unless we've been asked not to or the stage is diff
    66     // (By definition pixels in diff images were included in a diff)
    67     bool checkNonWarpedPixels = ! (keepNonWarpedPixels || (sfiles->stage == IPP_STAGE_DIFF) );
    68 
    69     if (checkNonWarpedPixels ) {
     66    // --keepnondiffed is a test and debug mode
     67    bool keepNonDiffedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_DIFFED");
     68
     69    // we need to check for non diffed pixels unless we've been asked not to or the stage is diff
     70    // (By definition pixels in diff images were included in the difference images)
     71    bool checkNonDiffedPixels = ! (keepNonDiffedPixels || (sfiles->stage == IPP_STAGE_DIFF) );
     72
     73    if (checkNonDiffedPixels ) {
    7074        // From magic ICD:
    7175        // In the raw and detrended images, the pixels which were not
     
    7579        // if no skycells are provided sfiles->exciseAll is set to true
    7680
    77         psTimerStart("COMPUTE_WARPED_PIXELS");
    78         if (! computeWarpedPixels(sfiles) ) {
     81        psTimerStart("COMPUTE_DIFFED_PIXELS");
     82        if (! computeDiffedPixels(sfiles) ) {
    7983            // we have no choice to excise all pixels
    8084            exciseAll = true;
    8185        }
    82         psF64 cwp_t = psTimerClear("COMPUTE_WARPED_PIXELS");
    83         psLogMsg("streaksremove", PS_LOG_INFO, "time to compute warped pixels: %f\n", cwp_t);
    84     }
    85    
     86        psF64 cdp_t = psTimerClear("COMPUTE_DIFFED_PIXELS");
     87        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "COMPUTE_UNDIFFED_PIXELS", PS_META_REPLACE, "time to compute non-diffedpixels", cdp_t);
     88        psLogMsg("streaksremove", PS_LOG_INFO, "time to compute diffed pixels: %f\n", cdp_t);
     89    }
     90
    8691    if (sfiles->stage == IPP_STAGE_RAW) {
    8792        // Except for raw stage, all of our (GPC1) files have one image extension.
     
    97102    }
    98103
    99     int totalPixels = 0;
    100     int totalStreakPixels = 0;
    101 
     104    long totalPixels = 0;
     105    long totalStreakPixels = 0;
     106    long nonDiffedPixels = 0;
     107
     108    // accumulators for the various timers
     109    psF64 gsp_t = 0;
     110    psF64 enw_t = 0;
     111    psF64 rms_t = 0;
     112    psF64 cs_t = 0;
     113    psF64 wi_t = 0;
     114    psF64 ua_t = 0;
    102115    // Iterate through each component of the input (except for raw images there is only one)
    103116    do {
     
    117130
    118131        // now that we've read the input files, lookup the mask values
    119         if (maskStreak == 0) {
    120             strkGetMaskValues(sfiles, &maskStreak, &maskMask);
    121         }
     132        strkGetMaskValues(sfiles);
    122133
    123134        totalPixels += sfiles->inImage->numRows * sfiles->inImage->numCols;
     
    131142            StreakPixels *pixels = streak_on_component(streaks, sfiles->astrom, sfiles->inImage->numCols,
    132143                                                        sfiles->inImage->numRows);
     144            gsp_t +=  psTimerClear("GET_STREAK_PIXELS");
     145            psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "GET_STREAK_PIXELS", PS_META_REPLACE, "", gsp_t);
    133146            psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS"));
    134            
     147
    135148            // if this extension contained an image, excise the streaked pixels.
    136149            // otherwise it contained an image cube (video cell) which is handled in the if block
    137150            if (sfiles->inImage->image) {
    138                 if (checkNonWarpedPixels) {
    139                     psTimerStart("EXCISE_NON_WARPED");
    140 
    141                     // set non-warped pixels and variance to NAN, mask to maskStreak (since the pixel
     151                if (checkNonDiffedPixels) {
     152                    psTimerStart("EXCISE_NON_DIFFED");
     153
     154                    // set non-diffed pixels and variance to NAN, mask to maskStreak (since the pixel
    142155                    // is excised as part of the destreaking process)
    143                     exciseNonWarpedPixels(sfiles, maskStreak);
    144 
    145                     psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED"));
     156                    nonDiffedPixels += exciseNonDiffedPixels(sfiles, sfiles->maskStreak);
     157
     158                    enw_t +=  psTimerClear("EXCISE_NON_DIFFED");
     159                    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "EXCISE_NON_DIFFED", PS_META_REPLACE, "", enw_t);
     160                    psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non diffed pixels: %f\n", enw_t);
    146161                }
    147162
    148 
    149163                psTimerStart("REMOVE_STREAKS");
    150164
    151                 for (int y=0 ; y < sfiles->inImage->numRows; y++) {
    152                     for (int x = 0; x < sfiles->inImage->numCols; x++) {
    153                         if (psImageGet(pixels, x, y)) {
    154                             ++totalStreakPixels;
    155                             if (!checkNonWarpedPixels || warpedPixel(sfiles, x, y)) {
    156 
    157                                 excisePixel(sfiles, x, y, true, maskStreak);
    158 
    159                             } else {
    160                                 // This pixel was not included in any warp and has thus already excised
    161                                 // by exciseNonWarpedPixels
    162                             }
    163                         }
    164                     }
    165                 }
    166 
    167                 psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS"));
     165                totalStreakPixels += censorPixels(sfiles, pixels, checkNonDiffedPixels, sfiles->maskStreak);
     166
     167                rms_t += psTimerClear("REMOVE_STREAKS");
     168                psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "REMOVE_STREAKS", PS_META_REPLACE, "", enw_t);
     169                psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", rms_t);
    168170
    169171                if (nanForRelease) {
    170172                    // set any pixels that were masked, to NAN (unless they are already NAN)
    171                     setMaskedToNAN(sfiles, maskMask, true);
     173                    setMaskedToNAN(sfiles, sfiles->maskMask, true);
    172174                }
    173175
    174             } else { 
     176            } else {
    175177                // this component contains an image cube
    176178                // For now excise it completely
     
    184186            // chip processed files with the data calcuated by psastro at the camera stage
    185187            // (actually we use a linear approximation)
     188            psTimerStart("UPDATE_ASTROMETRY");
    186189            updateAstrometry(sfiles);
    187         }
    188 
    189         censorSources(sfiles, maskStreak);
     190            ua_t += psTimerClear("UPDATE_ASTROMETRY");
     191            psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "UPDATE_ASTROMETRY", PS_META_REPLACE, "", ua_t);
     192            psLogMsg("streaksremove", PS_LOG_INFO, "time to update astrometry: %f\n", ua_t);
     193        }
     194
     195        psTimerStart("CENSOR_SOURCES");
     196        censorSources(sfiles, sfiles->maskStreak);
     197        cs_t += psTimerClear("CENSOR_SOURCES");
     198        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "CENSOR_SOURCES", PS_META_REPLACE, "", cs_t);
    190199
    191200        // write the destreaked "temporary" images and the recovery images
     201        psTimerStart("WRITE_IMAGES");
    192202        writeImages(sfiles, exciseImageCube);
    193 
     203        wi_t += psTimerClear("WRITE_IMAGES");
     204        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "WRITE_IMAGES", PS_META_REPLACE, "", wi_t);
    194205
    195206        psLogMsg("streaksremove", PS_LOG_INFO, "time to process component %d: %f\n", sfiles->extnum, psTimerClear("PROCESS_COMPONENT"));
     207
    196208    } while (streakFilesNextExtension(sfiles));
    197209
     
    199211    psFree(streaks);
    200212
    201     psLogMsg("streaksremove", PS_LOG_INFO, "pixels: %ld streak pixels: %ld %4.2f%%\n", totalPixels, totalStreakPixels, 100. * totalStreakPixels / totalPixels);
     213    if (exciseAll) {
     214        totalStreakPixels = totalPixels;
     215    }
     216
     217    psF64 streakFraction = (double) totalStreakPixels / totalPixels;
     218    psLogMsg("streaksremove", PS_LOG_INFO, "   total pixels:  %8ld\n", totalPixels);
     219    psLogMsg("streaksremove", PS_LOG_INFO, "  streak pixels:  %8ld %4.2f%%\n", totalStreakPixels, streakFraction * 100);
     220    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "STREAK_FRACTION", PS_META_REPLACE, "", streakFraction);
     221
     222    psF64 nonDiffedFraction = (double) nonDiffedPixels / totalPixels;
     223    psLogMsg("streaksremove", PS_LOG_INFO, "nondiffed pixels:  %8ld %4.2f%%\n", nonDiffedPixels, nonDiffedFraction * 100);
     224    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "NONDIFFED_FRACTION", PS_META_REPLACE, "", nonDiffedFraction);
    202225
    203226    // check the weight and mask files for extra extensions that might be in files
     
    208231
    209232    psTimerStart("CLOSE_IMAGES");
    210 
    211233    closeImages(sfiles);
    212 
    213     psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", psTimerClear("CLOSE_IMAGES"));
    214 
    215 
     234    psF64 ci_t = psTimerClear("CLOSE_IMAGES");
     235    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "CLOSE_IMAGES", PS_META_REPLACE, "", ci_t);
     236
     237    psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", ci_t);
     238
     239#ifdef DO_REPLICATE
     240    psTimerStart("REPLICATE_OUTPUTS");
    216241    if (!replicateOutputs(sfiles)) {
    217         psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
     242        psErrorStackPrint(stderr, "failed to replicate output files");
    218243        deleteTemps(sfiles);
    219         psErrorStackPrint(stderr, "");
    220244        exit(PS_EXIT_UNKNOWN_ERROR);
    221245    }
     246    psF64 ro_t = psTimerClear("REPLICATE_OUTPUTS");
     247    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "REPLICATE_OUTPUTS", PS_META_REPLACE, "", ro_t);
     248#endif
    222249
    223250    // NOTE: from here on we can't just quit if something goes wrong.
     
    229256        //     swap the instances for the input and output
    230257        //     Note this is a nebulous database operation. No file I/O is performed
     258        psTimerStart("SWAP_INSTANCES");
    231259        if (!swapOutputsToInputs(sfiles)) {
    232             // XXX: Now what?
    233260            // It is up to the program that reverts failed destreak runs to insure that
    234             // any input files that have been swapped are restored and that the de-streaked
    235             // versions are deleted
     261            // any original non-destreaked input files that have been swapped are restored and that the de-streaked
     262            // versions are deleted.
    236263
    237264            psErrorStackPrint(stderr, "failed to swap files");
     
    239266            // XXX: pick a specific error code for this failure
    240267            exit(PS_EXIT_UNKNOWN_ERROR);
     268        }
     269        psF64 si_t = psTimerClear("SWAP_INSTANCES");
     270        psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "SWAP_INSTANCES", PS_META_REPLACE, "", si_t);
     271    }
     272
     273    psF64 total_time = psTimerClear("TOTAL_TIME");
     274    psMetadataAddF32(sfiles->stats, PS_LIST_TAIL, "TOTAL_TIME", PS_META_REPLACE, "", total_time);
     275    psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", total_time);
     276
     277    if (sfiles->statsFile) {
     278        const char *statsMDC = psMetadataConfigFormat(sfiles->stats);
     279        if (!statsMDC || strlen(statsMDC) == 0) {
     280            psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
     281        } else {
     282            fprintf(sfiles->statsFile, "%s", statsMDC);
     283            psFree(statsMDC);
     284            fclose(sfiles->statsFile);
     285            sfiles->statsFile = NULL;
     286            psFree(sfiles->stats);
     287            sfiles->stats = NULL;
    241288        }
    242289    }
     
    247294    pmConceptsDone();
    248295    pmModelClassCleanup();
    249     streaksNebulousCleanup(); 
     296    streaksNebulousCleanup();
    250297    pmConfigDone();
    251     psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", psTimerClear("STREAKSREMOVE"));
    252298    psLibFinalize();
    253299
     
    255301
    256302    return 0;
     303}
     304
     305static long
     306censorPixels(streakFiles *sfiles, psImage *pixels, bool checkNonDiffedPixels, psU16 maskStreak)
     307{
     308    long streakPixels = 0;
     309
     310    for (int y=0 ; y < sfiles->inImage->numRows; y++) {
     311        for (int x = 0; x < sfiles->inImage->numCols; x++) {
     312            if (psImageGet(pixels, x, y)) {
     313                if (!checkNonDiffedPixels || diffedPixel(sfiles, x, y)) {
     314                    ++streakPixels;
     315
     316                    excisePixel(sfiles, x, y, true, maskStreak);
     317
     318                } else {
     319                    // This pixel was not included in any warp and has thus already excised
     320                    // by exciseNonDiffedPixels
     321                }
     322            }
     323        }
     324    }
     325    return streakPixels;
    257326}
    258327
     
    266335    fprintf(stderr, "\t-weight WEIGHT.fits: weight file to de-streak\n");
    267336    fprintf(stderr, "\t-replace: replace the input images with the output\n");
    268     fprintf(stderr, "\t-keepnonwarped: do not exise pixels that were not part of difference processing\n");
     337    fprintf(stderr, "\t-keepnondiffed: do not exise pixels that were not part of difference processing\n");
    269338    fprintf(stderr, "\t-transparent val: instead of setting excicsed pixel to NAN add val\n");
    270339    fprintf(stderr, "\t-chip_mask MASK.fits: name of mask for chip stage (camera stage mask is passed tih -mask)\n");
     
    356425            true);
    357426    }
    358    
    359     if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) {
    360         psArgumentRemove(argnum, &argc, argv);
    361         psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_WARPED", 0,
    362             "skip excising of non warped pixels", true);
     427
     428    if ((argnum = psArgumentGet(argc, argv, "-keepnondiffed"))) {
     429        psArgumentRemove(argnum, &argc, argv);
     430        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_DIFFED", 0,
     431            "skip excising of non diffed pixels", true);
    363432    }
    364433
     
    370439        psArgumentRemove(argnum, &argc, argv);
    371440        double transparentStreaks = atof(argv[argnum]);
    372         psMetadataAddF64(config->arguments, PS_LIST_TAIL, "TRANSPARENT_STREAKS", 0,
     441        psMetadataAddF32(config->arguments, PS_LIST_TAIL, "TRANSPARENT_STREAKS", 0,
    373442            "value to adjust excised pixels", transparentStreaks);
    374443        psArgumentRemove(argnum, &argc, argv);
    375444    }
    376        
    377     // if skycells are not provided then we have to execise all pixels  unless -keepnonwarped
     445
     446    // if skycells are not provided then we have to execise all pixels  unless -keepnondiffed
    378447    pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist");
    379448
     
    441510    }
    442511
     512    if ((argnum = psArgumentGet(argc, argv, "-stats"))) {
     513        psArgumentRemove(argnum, &argc, argv);
     514        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "STATS", 0,
     515                "name of input stats file", argv[argnum]);
     516        psArgumentRemove(argnum, &argc, argv);
     517    }
     518
    443519    if ((argnum = psArgumentGet(argc, argv, "-tmproot"))) {
    444520        psArgumentRemove(argnum, &argc, argv);
     
    483559updateAstrometry(streakFiles *sf)
    484560{
    485     // XXX: why do I check this here? Shouldn't it be just around the call to linearizeTransforms?
    486561    if (sf->bilevelAstrometry) {
    487 
    488562        if (!linearizeTransforms(sf->astrom)) {
    489             // fit failed, leave the astrometry unchanged
     563            // fit failed, leave the transform in the file unchanged
    490564            return;
    491565        }
    492 
    493         if (!pmAstromWriteWCS(sf->outImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
    494             psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
    495             streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    496         }
    497         if (sf->outMask) {
    498             pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
    499         }
    500         if (sf->outWeight) {
    501             pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     566    }
     567    if (!pmAstromWriteWCS(sf->outImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
     568        psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
     569        streaksExit("", PS_EXIT_UNKNOWN_ERROR);
     570    }
     571    if (sf->outMask) {
     572        pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
     573    }
     574    if (sf->outWeight) {
     575        pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
     576    }
     577}
     578
     579static void
     580setStreakBits(psImage *maskImage, psU32 maskStreak)
     581{
     582    for (int y=0 ; y < maskImage->numRows; y++) {
     583        for (int x=0 ; x < maskImage->numCols; x++) {
     584            maskImage->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskStreak;
    502585        }
    503586    }
     
    508591readAndCopyToOutput(streakFiles *sf, bool exciseAll)
    509592{
    510     bool    updateAstrometry = false;
    511593    if (sf->inImage->pmfile) {
    512594        // image data from pmFPAfile (diff or warp file)
     
    526608        streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    527609    }
    528     // For the chip level files, copy the WCS from the astrometry file to the header
    529     // XXX: do we want to do this for raw images as well?
    530     if (sf->stage == IPP_STAGE_CHIP) {
    531         if (!sf->bilevelAstrometry) {
    532             updateAstrometry = true;
    533             if (!pmAstromWriteWCS(sf->inImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) {
    534                 psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum);
    535                 streaksExit("", PS_EXIT_UNKNOWN_ERROR);
    536             }
    537         }
    538     }
    539610    sf->outImage->header =  psMemIncrRefCounter(sf->inImage->header);
    540611    if (sf->recImage) {
     
    569640            }
    570641            addDestreakKeyword(sf->outMask->header);
    571             if (updateAstrometry) {
    572                 pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001);
    573             }
    574             setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll);
     642            // Note: we don't excise the mask pixels even if exciseAll is true.
     643            setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, false);
     644            if (exciseAll) {
     645                strkGetMaskValues(sf);
     646               
     647                // add the STREAK bit to the mask image pixels
     648                setStreakBits(sf->inMask->image, sf->maskStreak);
     649            }
    575650            if (sf->outChMask) {
    576651                sf->outChMask->header = (psMetadata *) psMemIncrRefCounter(sf->outMask->header);
     
    597672        }
    598673        addDestreakKeyword(sf->outWeight->header);
    599         if (updateAstrometry) {
    600             pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001);
    601         }
    602674        setupImageRefs(sf->outWeight, sf->recWeight, sf->inWeight, sf->extnum, exciseAll);
    603675
     
    622694    } else {
    623695        // we have an image cube
    624         double initValue;
    625696        if (exciseImageCube) {
    626697            // copy the entire input image to the recovery image
    627698            writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
    628             initValue = NAN;
    629699        } else {
    630             // otherwise write it to the output 
     700            // otherwise write it to the output
    631701            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    632             initValue = 0;
    633         }
    634 
    635         // borrow one of the images from the imagecube and set it to init value
    636         psImage *image = psArrayGet (sf->inImage->imagecube, 0);
    637         psMemIncrRefCounter(image);
    638         psImageInit(image, initValue);
     702        }
     703
     704        // Now deal with the other output image
    639705        if (exciseImageCube) {
    640             sf->outImage->image = image;
    641             writeImage(sf->outImage, extname, sf->extnum);
     706            // Set the values in the imagecube images to NAN and write them to the output image
     707            for (int i = 0; i < psArrayLength(sf->inImage->imagecube); i++) {
     708                psImage *image = psArrayGet (sf->inImage->imagecube, i);
     709                // XXX: NAN isn't right. It should be the integer equivalent. Should use exciseValue
     710                // but it isn't set with this code path. Fix that.
     711                psImageInit(image, 65535);
     712            }
     713            writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum);
    642714        } else {
    643             // write zero valued image to reccovery
    644715            if (sf->recImage) {
    645                 sf->recImage->image = image;
    646                 writeImage(sf->recImage, extname, sf->extnum);
     716                // Set the values in the imagecube images to zero
     717                for (int i = 0; i < psArrayLength(sf->inImage->imagecube); i++) {
     718                    psImage *image = psArrayGet (sf->inImage->imagecube, i);
     719                    psImageInit(image, 0);
     720                }
     721                // copy the entire zeroed image to the recovery image
     722                writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum);
    647723            }
    648724        }
     
    666742
    667743static void
    668 excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue)
     744excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue)
    669745{
    670746    double exciseValue = sfiles->inImage->exciseValue;
     
    675751    }
    676752
    677     double imageValue  = psImageGet (sfiles->inImage->image,  x, y);
    678     if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
    679         psImageSet (sfiles->recImage->image,  x, y, imageValue);
    680     }
    681 
    682     if (sfiles->transparentStreaks == 0) {
    683         psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     753    if (sfiles->inImage->image->type.type == PS_TYPE_U16) {
     754        psU16 imageValue  = sfiles->inImage->image->data.U16[y][x];
     755        if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
     756            sfiles->recImage->image->data.U16[y][x] = imageValue;
     757        }
     758
     759        if (sfiles->transparentStreaks == 0) {
     760            sfiles->outImage->image->data.U16[y][x] = exciseValue;
     761        } else {
     762            if (streak) {
     763                // as a visualization aid don't mask the pixel, just change the intensity
     764                sfiles->outImage->image->data.U16[y][x] = imageValue + sfiles->transparentStreaks;
     765            } else {
     766                sfiles->outImage->image->data.U16[y][x] = exciseValue;
     767            }
     768        }
    684769    } else {
    685         if (streak) {
    686             // as a visualization aid don't mask the pixel, just change the intensity
    687             psImageSet (sfiles->outImage->image,  x, y, imageValue + sfiles->transparentStreaks);
     770        float imageValue  = sfiles->inImage->image->data.F32[y][x];
     771        if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) {
     772            sfiles->recImage->image->data.F32[y][x] = imageValue;
     773        }
     774
     775        if (sfiles->transparentStreaks == 0) {
     776            sfiles->outImage->image->data.F32[y][x] = exciseValue;
    688777        } else {
    689             psImageSet (sfiles->outImage->image,  x, y, exciseValue);
     778            if (streak) {
     779                // as a visualization aid don't mask the pixel, just change the intensity
     780                sfiles->outImage->image->data.F32[y][x] = imageValue + sfiles->transparentStreaks;
     781            } else {
     782                sfiles->outImage->image->data.F32[y][x] = exciseValue;
     783            }
    690784        }
    691785    }
     
    693787    if (sfiles->outWeight) {
    694788        if (sfiles->recWeight) {
    695             double weightValue = psImageGet (sfiles->inWeight->image, x, y);
    696             psImageSet (sfiles->recWeight->image, x, y, weightValue);
     789            sfiles->recWeight->image->data.F32[y][x] = sfiles->inWeight->image->data.F32[y][x];
    697790        }
    698791        // Assume that weight images are always a floating point type
    699         psImageSet (sfiles->outWeight->image, x, y, NAN);
     792        sfiles->outWeight->image->data.F32[y][x] = NAN;
    700793    }
    701794    if (sfiles->outMask) {
    702795        if (sfiles->recMask) {
    703             double maskValue   = psImageGet (sfiles->inMask->image,   x, y);
    704             psImageSet (sfiles->recMask->image,   x, y, maskValue);
    705         }
    706         psImageSet (sfiles->outMask->image,   x, y, newMaskValue);
    707     }
    708 }
    709 
    710 static void
    711 exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue)
     796            sfiles->recMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] =
     797                sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x];
     798        }
     799        sfiles->outMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= newMaskValue;
     800    }
     801}
     802
     803static long
     804exciseNonDiffedPixels(streakFiles *sfiles, psImageMaskType newMaskValue)
    712805{
    713806    int cell_x0 = sfiles->astrom->cell_x0;
     
    717810    int numCols = sfiles->inImage->numCols; // for raw images this was calculated from the width of datasec
    718811    int numRows = sfiles->inImage->numRows; // for raw images this was calculated from the height of datasec
     812
     813    long excisedPixels = 0;
    719814
    720815//    printf("%2d x0: %4d y0: %4d xpar: %d ypar: %d\n", sfiles->extnum, cell_x0, cell_y0, xParity, yParity);
     
    731826        }
    732827
    733         psU8 *pixels = sfiles->warpedPixels->data.U8[yChip];
     828        psU8 *pixels = sfiles->diffedPixels->data.U8[yChip];
    734829
    735830        if (xParity == 1) {
     
    738833                if (! *pixels ) {
    739834                    excisePixel(sfiles, xCell, yCell, false, newMaskValue);
     835                    excisedPixels++;
    740836                }
    741837            }
     
    747843                if (!*pixels) {
    748844                    excisePixel(sfiles, xCell, yCell, false, newMaskValue);
     845                    excisedPixels++;
    749846                }
    750847            }
    751848        }
    752849    }
     850    return excisedPixels;
    753851}
    754852
    755853static bool
    756 warpedPixel(streakFiles *sfiles, int x, int y)
     854diffedPixel(streakFiles *sfiles, int x, int y)
    757855{
    758856    PixelPos chipCoord;
    759857
    760858    if (!CHIP_LEVEL_INPUT(sfiles->stage)) {
    761         // if we're here on a skycell image by definition this pixel was warped
     859        // if we're here on a skycell image by definition this pixel was diffed
    762860        return true;
    763861    }
     
    772870    cellToChipInt(&chipCoord.x, &chipCoord.y, sfiles->astrom, x, y);
    773871
    774     if (chipCoord.x < 0 || chipCoord.x >= sfiles->warpedPixels->numCols) {
     872    if (chipCoord.x < 0 || chipCoord.x >= sfiles->diffedPixels->numCols) {
    775873        return false;
    776874    }
    777     if (chipCoord.y < 0 || chipCoord.y >= sfiles->warpedPixels->numRows) {
     875    if (chipCoord.y < 0 || chipCoord.y >= sfiles->diffedPixels->numRows) {
    778876        return false;
    779877    }
    780878
    781     return psImageGet(sfiles->warpedPixels, chipCoord.x, chipCoord.y) ? true : false;
     879    return psImageGet(sfiles->diffedPixels, chipCoord.x, chipCoord.y) ? true : false;
    782880}
    783881
     
    785883// streak mask
    786884static void
    787 censorSources(streakFiles *sfiles, psU32 maskStreak)
     885censorSources(streakFiles *sfiles, psImageMaskType maskStreak)
    788886{
    789887    if ((!sfiles->inSources) || (!sfiles->outMask)) {
     
    799897    sFile *out = sfiles->outSources;
    800898
    801     in->header = psFitsReadHeader(NULL, in->fits);
    802     if (!in->header) {
    803         psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
     899
     900    // Primary header, should be "something.hdr"
     901    {
     902        psMetadata *header = psFitsReadHeader(NULL, in->fits);
     903        if (!header) {
     904            psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name);
     905            streaksExit("", PS_EXIT_DATA_ERROR);
     906        }
     907
     908        bool status;
     909        psString extname = psMetadataLookupStr(&status, header, "EXTNAME");
     910        if (!extname) {
     911            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
     912            streaksExit("", PS_EXIT_DATA_ERROR);
     913        }
     914        addDestreakKeyword(header);
     915
     916        if (!psFitsWriteBlank(out->fits, header, extname)) {
     917            psError(PS_ERR_IO, false, "failed to write blank in header of %s", in->resolved_name);
     918            streaksExit("", PS_EXIT_DATA_ERROR);
     919        }
     920        psFree(header);
     921    }
     922
     923    // Extension with PSF fits, should be "something.psf"
     924    {
     925        if (!psFitsMoveExtNum(in->fits, 1, true)) {
     926            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
     927            streaksExit("", PS_EXIT_DATA_ERROR);
     928        }
     929
     930        psMetadata *header = psFitsReadHeader(NULL, in->fits);
     931        if (!header) {
     932            psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name);
     933            streaksExit("", PS_EXIT_DATA_ERROR);
     934        }
     935        psString extname = psMetadataLookupStr(NULL, header, "EXTNAME");
     936        if (!extname) {
     937            psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
     938            streaksExit("", PS_EXIT_DATA_ERROR);
     939        }
     940
     941        psArray *inTable = psFitsReadTable(in->fits);
     942        if (!inTable->n) {
     943            psErrorStackPrint(stderr, "table in %s is empty", in->resolved_name);
     944            streaksExit("", PS_EXIT_DATA_ERROR);
     945        }
     946
     947        psArray *outTable = psArrayAllocEmpty(inTable->n);
     948        int j = 0;
     949        int numCensored = 0;
     950        for (int i = 0 ; i < inTable->n; i++) {
     951            psMetadata *row = inTable->data[i];
     952
     953            psF32 x = psMetadataLookupF32(NULL, row, "X_PSF");
     954            psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF");
     955
     956            psImageMaskType mask;
     957            if ((x >= maskImage->numCols) || (y >= maskImage->numRows) || (x <  0) || (y < 0)) {
     958                mask = maskStreak;
     959            } else {
     960                mask = maskImage->data.PS_TYPE_IMAGE_MASK_DATA[(int)y][(int)x];
     961            }
     962
     963            // Key the source if the center pixel is not masked with maskStreak
     964            if (!(mask & maskStreak) ) {
     965                psArraySet(outTable, j++, row);
     966            } else {
     967                numCensored++;
     968            }
     969        }
     970
     971        // get rid of unused elements (don't know if this is necessary)
     972        psArrayRealloc(outTable, j);
     973
     974        addDestreakKeyword(header);
     975        if (psArrayLength(outTable) > 0) {
     976            printf("Censored %d sources\n", numCensored);
     977            if (! psFitsWriteTable(out->fits, header, outTable, extname)) {
     978                psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
     979                streaksExit("", PS_EXIT_DATA_ERROR);
     980            }
     981        } else {
     982            printf("Censored ALL %d sources\n", numCensored);
     983            if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) {
     984                psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name);
     985                streaksExit("", PS_EXIT_DATA_ERROR);
     986            }
     987        }
     988        psFree(header);
     989        psFree(outTable);
     990        psFree(inTable);
     991    }
     992
     993    // XXX Will need to update to handle extension with extended sources, etc.
     994
     995    if (!psFitsClose(out->fits)) {
     996        psErrorStackPrint(stderr, "failed to close table %s", out->resolved_name);
    804997        streaksExit("", PS_EXIT_DATA_ERROR);
    805998    }
    806 
    807     bool status;
    808     psString extname = psMetadataLookupStr(&status, in->header, "EXTNAME");
    809     if (!extname) {
    810         psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name);
    811         streaksExit("", PS_EXIT_DATA_ERROR);
    812     }
    813 
    814     psArray *inTable = psFitsReadTable(in->fits);
    815     if (!inTable->n) {
    816         psError(PS_ERR_IO, false, "table in %s is empty", in->resolved_name);
    817         streaksExit("", PS_EXIT_DATA_ERROR);
    818     }
    819 
    820     psArray *outTable = psArrayAllocEmpty(inTable->n);
    821     int j = 0;
    822     int numCensored = 0;
    823     for (int i = 0 ; i < inTable->n; i++) {
    824         psMetadata *row = inTable->data[i];
    825 
    826         psF32 x = psMetadataLookupF32 (&status, row, "X_PSF");
    827         psF32 y = psMetadataLookupF32 (&status, row, "Y_PSF");
    828        
    829         psU32 mask = psImageGet(maskImage, x, y);
    830 
    831         // Key the source if the center pixel is not masked with maskStreak
    832         if (! (mask & maskStreak) ) {
    833             psArraySet(outTable, j++, row);
    834         } else {
    835             numCensored++;
    836         }
    837     }
    838 
    839     // get rid of unused elements (don't know if this is necessary)
    840     psArrayRealloc(outTable, j);
    841 
    842     addDestreakKeyword(in->header);
    843     if (psArrayLength(outTable) > 0) {
    844         printf("Censored %d sources\n", numCensored);
    845         if (! psFitsWriteTable(out->fits, in->header, outTable, extname)) {
    846             psError(PS_ERR_IO, false, "failed to write table to %s", out->resolved_name);
    847             streaksExit("", PS_EXIT_DATA_ERROR);
    848         }
    849     } else {
    850         printf("Censored ALL %d sources\n", numCensored);
    851         if (! psFitsWriteTableEmpty(out->fits, in->header, inTable->data[0], extname)) {
    852             psError(PS_ERR_IO, false, "failed to write empty table to %s", out->resolved_name);
    853             streaksExit("", PS_EXIT_DATA_ERROR);
    854         }
    855     }
    856 
    857     if (!psFitsClose(out->fits)) {
    858         psError(PS_ERR_IO, false, "failed to close table %s", out->resolved_name);
    859         streaksExit("", PS_EXIT_DATA_ERROR);
    860     }
    861 }
     999}
  • branches/simtest_nebulous_branches/magic/remove/src/streaksremove.h

    r24691 r27840  
    3333    int         yParity;
    3434    double      exciseValue;
     35    psString    program;
    3536} sFile;
    3637
    37 
    38 typedef enum {
    39     IPP_STAGE_NONE = 0,
    40     IPP_STAGE_RAW,
    41     IPP_STAGE_CHIP,
    42     IPP_STAGE_WARP,
    43     IPP_STAGE_DIFF
    44 } ippStage;
    45 
     38// used for error messages
     39extern char * streaksProgram;
    4640
    4741typedef struct {
     
    6458    sFile *inSources;
    6559    sFile *outSources;
     60    psMetadata *stats;
     61    FILE *statsFile;
    6662    psString class_id;
    6763    pmFPAfile  *inAstrom;
     
    7167    pmChip  *chip;  // current chip
    7268    pmCell  *cell;  // current cell
    73     psImage *warpedPixels;
     69    psImage *diffedPixels;
    7470    psVector    *tiles;
    7571    double  transparentStreaks;
    7672    psString    program_name;
     73    psU32   maskStreak;
     74    psU32   maskMask;
    7775} streakFiles;
    7876
     
    8482// can't declare this in streaksastrom due to header file ordering
    8583extern void cellToChip(double *xChip, double *yChip, strkAstrom *astrom, double xCell, double yCell);
    86 extern void cellToChipInt(int *xChip, int *yChip, strkAstrom *astrom, int xCell, int yCell);
     84extern void cellToChipInt(unsigned int *xChip, unsigned int *yChip, strkAstrom *astrom, int xCell, int yCell);
    8785
    88 extern bool computeWarpedPixels(streakFiles *sf);
     86extern bool computeDiffedPixels(streakFiles *sf);
    8987extern void streaksExit(psString, int);
    9088extern ippStage parseStage(psString);
    9189extern psString pathToDirectory(char *path);
    9290extern void setStreakFiles( streakFiles *);
     91
     92extern bool streaksVersionHeaderFull(psMetadata *header);
     93extern psString streaksVersionLong(void);
    9394
    9495#define CHIP_LEVEL_INPUT(_stage) ((_stage == IPP_STAGE_RAW) || (_stage == IPP_STAGE_CHIP))
  • branches/simtest_nebulous_branches/magic/remove/src/streaksreplace.c

    r24556 r27840  
    66static bool readAndCopyToOutput(streakFiles *sf, bool restoreImageCube);
    77
     8char *streaksProgram = "streaksreplace";
     9
    810int main(int argc, char *argv[])
    911{
    10     long i;
    1112    bool status;
    12     StreakPixels *pixels;
    13     PixelPos *pixelPos;
    1413
    1514    psLibInit(NULL);
     
    9291
    9392    if (!replicateOutputs(sfiles)) {
    94         psError(PS_ERR_UNKNOWN, false, "failed to replicate output files");
    95         psErrorStackPrint(stderr, "");
     93        psErrorStackPrint(stderr, "failed to replicate output files");
    9694        exit(PS_EXIT_UNKNOWN_ERROR);
    9795    }
     
    172170            true);
    173171    }
    174    
     172
    175173    bool gotMask = false;
    176174    if ((argnum = psArgumentGet(argc, argv, "-mask"))) {
     
    353351        // we have an image cube
    354352        double initValue;
    355         // otherwise write it to the output 
     353        // otherwise write it to the output
    356354        writeImageCube(sf->outImage, sf->recImage->imagecube, extname, sf->extnum);
    357355
  • branches/simtest_nebulous_branches/magic/remove/src/streaksutil.c

    r24556 r27840  
    4444// we just bail out
    4545void streaksExit(psString str, int exitCode) {
    46     psErrorStackPrint(stderr, str);
     46    psErrorStackPrint(stderr, "%s", str);
    4747    if (ourStreakFiles) {
    4848        deleteTemps(ourStreakFiles);
Note: See TracChangeset for help on using the changeset viewer.