IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5743 for trunk/stac/src/stac.c


Ignore:
Timestamp:
Dec 7, 2005, 2:57:14 PM (21 years ago)
Author:
Paul Price
Message:

Importing from PAP cvs tree again. This will now be the working tree.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/stac/src/stac.c

    r3673 r5743  
    1818    // Set trace levels
    1919    psTraceSetLevel(".",0);
     20    psTraceSetLevel("stac",10);
    2021    psTraceSetLevel("stac.checkMemory",10);
    2122    psTraceSetLevel("stac.config",10);
     
    3839
    3940    // Read input files
    40     psArray *inputs = stacReadImages(config->inputs);
     41    psArray *headers = NULL;            // Array of headers
     42    psArray *inputs = stacReadImages(&headers, config->inputs);
     43
     44    // Generate masks
     45    psArray *masks = psArrayAlloc(inputs->n);
     46    for (int i = 0; i < inputs->n; i++) {
     47        psImage *image = inputs->data[i]; // Image for which to get mask
     48        psImage *mask = psImageAlloc(image->numCols, image->numRows, PS_TYPE_U8);
     49        for (int y = 0; y < image->numRows; y++) {
     50            for (int x = 0; x < image->numCols; x++) {
     51                if (image->data.F32[y][x] <= config->bad) {
     52                    mask->data.U8[y][x] = 1;
     53                } else {
     54                    mask->data.U8[y][x] = 0;
     55                }
     56            }
     57        }
     58        masks->data[i] = mask;
     59    }
    4160
    4261    // Read maps
     
    4766    // Get size, if not input
    4867    if (config->outnx == 0 || config->outny == 0) {
    49         stacSize(&config->outnx, &config->outny, &config->xMapDiff, &config->yMapDiff, inputs, maps);
     68        stacSize(&config->outnx, &config->outny, &config->xMapDiff, &config->yMapDiff, inputs, maps);
     69    }
     70
     71    // Fix up the headers
     72    for (int i = 0; i < headers->n; i++) {
     73        psMetadata *header = headers->data[i];
     74        psMetadataAddS32(header, PS_LIST_TAIL, "NAXIS1", PS_META_REPLACE, "Number of pixels in x",
     75                         config->outnx);
     76        psMetadataAddS32(header, PS_LIST_TAIL, "NAXIS2", PS_META_REPLACE, "Number of pixels in y",
     77                         (int)config->outny);
     78        psMetadataAddS32(header, PS_LIST_TAIL, "BITPIX", PS_META_REPLACE, "Bits per pixel", -32);
     79#if 0
     80        bool mdok = true;               // Result of MD lookup
     81        float crpix1 = psMetadataLookupF32(&mdok, header, "CRPIX1");
     82        if (mdok && !isnan(crpix1)) {
     83            psMetadataAddF32(header, PS_LIST_TAIL, "CRPIX1", PS_META_REPLACE,
     84                             "WCS Coordinate reference pixel", crpix1 -
     85#endif
    5086    }
    5187
     
    5894    // Write error images out to check
    5995    for (int i = 0; i < inputs->n; i++) {
    60         char errName[MAXCHAR];          // Filename of error image
    61         sprintf(errName,"%s.err",config->inputs->data[i]);
    62         psFits *errorFile = psFitsAlloc(errName);
    63         if (!psFitsWriteImage(errorFile, NULL, errors->data[i], 0, NULL)) {
    64             psErrorStackPrint(stderr, "Unable to write image: %s\n", errName);
    65         }
    66         psTrace("stac", 1, "Error image written to %s\n", errName);
    67         psFree(errorFile);
     96        char errName[MAXCHAR];          // Filename of error image
     97        sprintf(errName,"%s.err",config->inputs->data[i]);
     98        psFits *errorFile = psFitsOpen(errName, "w");
     99        if (!psFitsWriteImage(errorFile, NULL, errors->data[i], 0)) {
     100            psErrorStackPrint(stderr, "Unable to write image: %s\n", errName);
     101        }
     102        psTrace("stac", 1, "Error image written to %s\n", errName);
     103        psFitsClose(errorFile);
    68104    }
    69105#endif
     
    72108    psArray *transformed = NULL;
    73109    psArray *transformedErrors = NULL;
    74     (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, NULL, NULL, NULL, NULL,
    75                         config->outnx, config->outny);
     110    (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, masks, NULL, NULL, NULL,
     111                        config->outnx, config->outny);
    76112    psTrace("stac.time",1,"Transformation completed at %f seconds\n", getTime() - startTime);
    77113
     
    79115    // Write transformed images out to check
    80116    for (int i = 0; i < inputs->n; i++) {
    81         char shiftName[MAXCHAR];        // Filename of shift image
    82         char errName[MAXCHAR];          // Filename of error image
    83         sprintf(shiftName,"%s.shift1",config->inputs->data[i]);
    84         sprintf(errName,"%s.shifterr1",config->inputs->data[i]);
    85         psFits *shiftFile = psFitsAlloc(shiftName);
    86         psFits *errFile = psFitsAlloc(errName);
    87         psImage *trans = transformed->data[i]; // Transformed image
    88         psImage *transErr = transformedErrors->data[i]; // Transformed error image
    89         if (!psFitsWriteImage(shiftFile, NULL, trans, 0, NULL)) {
    90             psErrorStackPrint(stderr, "Unable to write image: %s\n", shiftName);
    91         }
    92         psTrace("stac", 1, "Shifted image written to %s\n", shiftName);
    93         if (!psFitsWriteImage(errFile, NULL, transErr, 0, NULL)) {
    94             psErrorStackPrint(stderr, "Unable to write image: %s\n", errName);
    95         }
    96         psTrace("stac", 1, "Shifted error image written to %s\n", errName);
    97         psFree(shiftFile);
    98         psFree(errFile);
    99     }
    100 #endif
    101 
    102 
    103 
    104     psVector *scales = NULL;            // Relative scales between images
    105     psVector *offsets = NULL;           // Offsets between images
     117        char shiftName[MAXCHAR];        // Filename of shift image
     118        char errName[MAXCHAR];          // Filename of error image
     119        sprintf(shiftName,"%s.shift1",config->inputs->data[i]);
     120        sprintf(errName,"%s.shifterr1",config->inputs->data[i]);
     121        psFits *shiftFile = psFitsOpen(shiftName, "w");
     122        psFits *errFile = psFitsOpen(errName, "w");
     123        psImage *trans = transformed->data[i]; // Transformed image
     124        psImage *transErr = transformedErrors->data[i]; // Transformed error image
     125        if (!psFitsWriteImage(shiftFile, NULL, trans, 0)) {
     126            psErrorStackPrint(stderr, "Unable to write image: %s\n", shiftName);
     127        }
     128        psTrace("stac", 1, "Shifted image written to %s\n", shiftName);
     129        if (!psFitsWriteImage(errFile, NULL, transErr, 0)) {
     130            psErrorStackPrint(stderr, "Unable to write image: %s\n", errName);
     131        }
     132        psTrace("stac", 1, "Shifted error image written to %s\n", errName);
     133        psFitsClose(shiftFile);
     134        psFitsClose(errFile);
     135    }
     136#endif
     137
     138    psVector *scales = NULL;            // Relative scales between images
     139    psVector *offsets = NULL;           // Offsets between images
    106140    (void)stacScales(&scales, &offsets, transformed, config->starFile, config->starMapFile, config->xMapDiff,
    107                      config->yMapDiff, config->aper);
     141                     config->yMapDiff, config->aper);
    108142    // Rescale the images
    109143    (void)stacRescale(transformed, transformedErrors, NULL, scales, offsets);
    110144    // Set the saturation and bad values
    111145    psVector *saturated = psVectorAlloc(transformed->n, PS_TYPE_F32); // Saturation limits
    112     psVector *bad = psVectorAlloc(transformed->n, PS_TYPE_F32); // Bad limits
     146    psVector *bad = psVectorAlloc(transformed->n, PS_TYPE_F32); // Bad limits
    113147    for (int i = 0; i < transformed->n; i++) {
    114         saturated->data.F32[i] = (config->saturated - offsets->data.F32[i]) / scales->data.F32[i];
    115         bad->data.F32[i] = (config->bad - offsets->data.F32[i]) / scales->data.F32[i];
     148        saturated->data.F32[i] = (config->saturated - offsets->data.F32[i]) / scales->data.F32[i];
     149        bad->data.F32[i] = (config->bad - offsets->data.F32[i]) / scales->data.F32[i];
     150    }
     151
     152    // Save shifted images
     153    if (config->saveShifts) {
     154        psImage *image = NULL;          // Copy of image, to remove NAN for output
     155        for (int i = 0; i < transformed->n; i++) {
     156            char shiftName[MAXCHAR];    // Filename of shifted image
     157            sprintf(shiftName, "%s.shift", config->inputs->data[i]);
     158            psFits *shiftFile = psFitsOpen(shiftName, "w");
     159            image = psImageCopy(NULL, transformed->data[i], PS_TYPE_F32);
     160            (void)psImageClipNaN(image, 0.0);
     161            if (!psFitsWriteImage(shiftFile, headers->data[i], image, 0)) {
     162                psErrorStackPrint(stderr, "Unable to write image: %s\n", shiftName);
     163            }
     164            psTrace("stac", 1, "Shifted image %d written to %s\n", i, shiftName);
     165            psFitsClose(shiftFile);
     166            psFree(image);
     167        }
    116168    }
    117169
     
    120172    psImage *combined = NULL;
    121173    (void)stacCombine(&combined, &rejected, transformed, transformedErrors, config->nReject, NULL, saturated,
    122                       bad, config->reject);
     174                      bad, config->reject);
    123175
    124176    psTrace("stac.time",1,"First combination completed at %f seconds\n", getTime() - startTime);
     
    128180    // Write rejection images out to check
    129181    for (int i = 0; i < rejected->n; i++) {
    130         char rejName[MAXCHAR];  // Filename of rejection image
    131         sprintf(rejName, "%s.shiftrej", config->inputs->data[i]);
    132        
    133         psFits *rejFile = psFitsAlloc(rejName);
    134         if (!psFitsWriteImage(rejFile, NULL, rejected->data[i], 0, NULL)) {
    135             psErrorStackPrint(stderr, "Unable to write image: %s\n", rejName);
    136         }
    137         psTrace("stac", 1, "Rejection image written to %s\n", rejName);
    138         psFree(rejFile);
     182        char rejName[MAXCHAR];  // Filename of rejection image
     183        sprintf(rejName, "%s.shiftrej", config->inputs->data[i]);
     184
     185        psFits *rejFile = psFitsOpen(rejName, "w");
     186        if (!psFitsWriteImage(rejFile, NULL, rejected->data[i], 0)) {
     187            psErrorStackPrint(stderr, "Unable to write image: %s\n", rejName);
     188        }
     189        psTrace("stac", 1, "Rejection image written to %s\n", rejName);
     190        psFitsClose(rejFile);
    139191    }
    140192
    141193    // Write out pre-combined image
    142     char preName[MAXCHAR];              // Filename of precombined image
     194    char preName[MAXCHAR];              // Filename of precombined image
    143195    sprintf(preName, "%s.pre", config->output);
    144     psFits *preFile = psFitsAlloc(preName);
    145     if (!psFitsWriteImage(preFile, NULL, combined, 0, NULL)) {
    146         psErrorStackPrint(stderr, "Unable to write image: %s\n", preName);
     196    psFits *preFile = psFitsOpen(preName, "w");
     197    if (!psFitsWriteImage(preFile, NULL, combined, 0)) {
     198        psErrorStackPrint(stderr, "Unable to write image: %s\n", preName);
    147199    }
    148200    psTrace("stac", 1, "Pre-combined image written to %s\n", preName);
    149     psFree(preFile);
     201    psFitsClose(preFile);
    150202#endif
    151203
     
    153205    psArray *regions = psArrayAlloc(inputs->n); // Array of images denoting regions of interest
    154206    for (int i = 0; i < inputs->n; i++) {
    155         regions->data[i] = stacAreaOfInterest(rejected->data[i], inverseMaps->data[i],
    156                                               ((psImage*)(inputs->data[i]))->numCols,
    157                                               ((psImage*)(inputs->data[i]))->numRows);
    158 #ifdef TESTING
    159         char regionName[MAXCHAR];       // Filename of region image
    160         sprintf(regionName,"%s.region",config->inputs->data[i]);
    161         psFits *regionFile = psFitsAlloc(regionName);
    162         if (!psFitsWriteImage(regionFile, NULL, regions->data[i], 0, NULL)) {
    163             psErrorStackPrint(stderr, "Unable to write image: %s\n", regionName);
    164         }
    165         psTrace("stac", 1, "Region image written to %s\n", regionName);
    166         psFree(regionFile);
     207        regions->data[i] = stacAreaOfInterest(rejected->data[i], inverseMaps->data[i],
     208                                              ((psImage*)(inputs->data[i]))->numCols,
     209                                              ((psImage*)(inputs->data[i]))->numRows);
     210#ifdef TESTING
     211        char regionName[MAXCHAR];       // Filename of region image
     212        sprintf(regionName,"%s.region",config->inputs->data[i]);
     213        psFits *regionFile = psFitsOpen(regionName, "w");
     214        if (!psFitsWriteImage(regionFile, NULL, regions->data[i], 0)) {
     215            psErrorStackPrint(stderr, "Unable to write image: %s\n", regionName);
     216        }
     217        psTrace("stac", 1, "Region image written to %s\n", regionName);
     218        psFitsClose(regionFile);
    167219#endif
    168220    }
     
    170222    // Transform rejected pixels to source frame
    171223    psArray *rejectedSource = stacRejection(inputs, rejected, regions, maps, inverseMaps, config->frac,
    172                                             config->grad, config->inputs);
     224                                            config->grad, config->inputs);
    173225
    174226    // Get regions of interest in the output frame
    175227    psImage *combineRegion = psImageAlloc(config->outnx, config->outny, PS_TYPE_U8);
    176228    for (int y = 0; y < config->outny; y++) {
    177         for (int x = 0; x < config->outnx; x++) {
    178             combineRegion->data.U8[y][x] = 0;
    179         }
    180     }
    181     for (int i = 0; i < inputs->n; i++) {
    182         psImage *region = stacAreaOfInterest(rejectedSource->data[i], maps->data[i], config->outnx,
    183                                              config->outny);
    184         psBinaryOp(combineRegion, combineRegion, "+", region);
    185         psFree(region);
     229        for (int x = 0; x < config->outnx; x++) {
     230            combineRegion->data.U8[y][x] = 0;
     231        }
     232    }
     233    for (int i = 0; i < inputs->n; i++) {
     234        psImage *region = stacAreaOfInterest(rejectedSource->data[i], maps->data[i], config->outnx,
     235                                             config->outny);
     236        psBinaryOp(combineRegion, combineRegion, "+", region);
     237        psFree(region);
     238
     239        // Do OR of masks
     240        psImage *mask = masks->data[i]; // Mask of input image (bad columns etc)
     241        psImage *cr = rejectedSource->data[i]; // Mask of CRs
     242        for (int y = 0; y < mask->numRows; y++) {
     243            for (int x = 0; x < mask->numCols; x++) {
     244                if (cr->data.U8[y][x] > 0) {
     245                    mask->data.U8[y][x] = 1;
     246                }
     247            }
     248        }
    186249    }
    187250
    188251    // Redo transformation with the masks and scales/offsets
    189     (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, rejectedSource,
    190                         combineRegion, scales, offsets, config->outnx, config->outny);
     252    (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, masks,
     253                        combineRegion, scales, offsets, config->outnx, config->outny);
    191254
    192255    // Combine the newly-transformed CR-free images, no rejection
     
    194257    rejected = NULL;
    195258    (void)stacCombine(&combined, &rejected, transformed, transformedErrors, 0, combineRegion, saturated,
    196                       bad, config->reject);
     259                      bad, config->reject);
    197260
    198261    // Write output image
    199     psFits *outFile = psFitsAlloc(config->output);
    200     if (!psFitsWriteImage(outFile, NULL, combined, 0, NULL)) {
    201         psErrorStackPrint(stderr, "Unable to write image: %s\n", config->output);
     262    psFits *outFile = psFitsOpen(config->output, "w");
     263    int numPix = psImageClipNaN(combined, 0.0);
     264    if (numPix > 0) {
     265        psTrace("stac", 3, "Clipping %d NaN pixels to zero.\n", numPix);
     266    }
     267    if (!psFitsWriteImage(outFile, headers->data[0], combined, 0)) {
     268        psErrorStackPrint(stderr, "Unable to write image: %s\n", config->output);
    202269    }
    203270    psTrace("stac", 1, "Combined image written to %s\n", config->output);
    204     psFree(outFile);
    205 
    206     // Save shifted images
    207     if (config->saveShifts) {
    208         for (int i = 0; i < transformed->n; i++) {
    209             char shiftName[MAXCHAR];    // Filename of shifted image
    210             sprintf(shiftName, "%s.shift", config->inputs->data[i]);
    211             psFits *shiftFile = psFitsAlloc(shiftName);
    212             if (!psFitsWriteImage(shiftFile, NULL, transformed->data[i], 0, NULL)) {
    213                 psErrorStackPrint(stderr, "Unable to write image: %s\n", shiftName);
    214             }
    215             psTrace("stac", 1, "Shifted image %d written to %s\n", i, shiftName);
    216             psFree(shiftFile);
    217         }
    218     }
     271    psFitsClose(outFile);
    219272
    220273    // Free everything I've used
     
    228281    psFree(inverseMaps);
    229282    psFree(errors);
     283    psFree(masks);
    230284    psFree(transformedErrors);
    231285    psFree(transformed);
    232286    psFree(rejectedSource);
    233287    psFree(combined);
     288    psFree(saturated);
     289    psFree(bad);
    234290
    235291    psTrace("stac.time",1,"Final combination completed at %f seconds\n", getTime() - startTime);
Note: See TracChangeset for help on using the changeset viewer.