IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

updates from trunk

Location:
branches/tap_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/tap_branches

  • branches/tap_branches/ppStack/src

    • Property svn:ignore
      •  

        old new  
        1010stamp-h1
        1111ppStackVersionDefinitions.h
         12ppStackErrorCodes.c
         13ppStackErrorCodes.h
  • branches/tap_branches/ppStack/src/ppStackCombineFinal.c

    r25096 r27838  
    1010#include "ppStackLoop.h"
    1111
    12 bool ppStackCombineFinal(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config)
     12#define WCS_TOLERANCE 0.001             // Tolerance for WCS
     13
     14//#define TESTING                         // Enable test output
     15
     16bool ppStackCombineFinal(ppStackThreadData *stack, psArray *covariances, ppStackOptions *options,
     17                         pmConfig *config, bool safe, bool normalise, bool grow)
    1318{
    1419    psAssert(stack, "Require stack");
    1520    psAssert(options, "Require options");
    1621    psAssert(config, "Require configuration");
     22
     23    pmReadout *outRO = options->outRO;                                      // Output readout
     24    pmReadout *expRO = options->expRO;                                      // Exposure readout
     25    int numCols = outRO->image->numCols, numRows = outRO->image->numRows; // Size of image
     26
     27    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
     28    psAssert(recipe, "We've thrown an error on this before.");
     29    float poorFrac = psMetadataLookupF32(NULL, recipe, "POOR.FRACTION"); // Fraction for "poor"
     30
     31    // Grow the list of rejected pixels, if desired
     32    psArray *reject = psArrayAlloc(options->num); // Pixels rejected for each image
     33    for (int i = 0; i < options->num; i++) {
     34        if (options->inputMask->data.U8[i]) {
     35            continue;
     36        }
     37        if (grow) {
     38            reject->data[i] = pmStackRejectGrow(options->rejected->data[i], numCols, numRows, poorFrac,
     39                                                options->regions->data[i], options->kernels->data[i]);
     40            if (!reject->data[i]) {
     41                psError(psErrorCodeLast(), false, "Unable to grow rejected pixels for image %d", i);
     42                psFree(reject);
     43                return false;
     44            }
     45        } else {
     46            reject->data[i] = psMemIncrRefCounter(options->rejected->data[i]);
     47        }
     48    }
     49
     50    if (!outRO->mask) {
     51        outRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_IMAGE_MASK);
     52    }
     53    if (!expRO->mask) {
     54        expRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_IMAGE_MASK);
     55    }
    1756
    1857    stack->lastScan = 0;            // Reset read
     
    2261        if (!status) {
    2362            // Something went wrong
    24             psError(PS_ERR_UNKNOWN, false, "Unable to read chunk %d", numChunk);
     63            psError(psErrorCodeLast(), false, "Unable to read chunk %d", numChunk);
     64            psFree(reject);
    2565            return false;
    2666        }
     
    3373        psThreadJob *job = psThreadJobAlloc("PPSTACK_FINAL_COMBINE"); // Job to start
    3474        psArrayAdd(job->args, 1, thread);
     75        psArrayAdd(job->args, 1, reject);
    3576        psArrayAdd(job->args, 1, options);
    3677        psArrayAdd(job->args, 1, config);
     78        PS_ARRAY_ADD_SCALAR(job->args, safe, PS_TYPE_U8);
     79        PS_ARRAY_ADD_SCALAR(job->args, normalise, PS_TYPE_U8);
    3780        if (!psThreadJobAddPending(job)) {
    3881            psFree(job);
     82            psFree(reject);
    3983            return false;
    4084        }
     
    4387
    4488    if (!psThreadPoolWait(true)) {
    45         psError(PS_ERR_UNKNOWN, false, "Unable to do final combination.");
     89        psError(psErrorCodeLast(), false, "Unable to do final combination.");
     90        psFree(reject);
    4691        return false;
    4792    }
    4893
     94    psFree(reject);
     95
    4996    // Sum covariance matrices
    50     double sumWeights = 0.0;            // Sum of weights
    51     for (int i = 0; i < options->num; i++) {
    52         if (options->inputMask->data.U8[i]) {
    53             psFree(options->covariances->data[i]);
    54             options->covariances->data[i] = NULL;
     97    if (covariances) {
     98        outRO->covariance = psImageCovarianceAverageWeighted(covariances, options->weightings);
     99    } else {
     100        outRO->covariance = psImageCovarianceNone();
     101    }
     102
     103    // Propagate WCS
     104    bool wcsDone = false;           // Have we done the WCS?
     105    for (int i = 0; i < options->num && !wcsDone; i++) {
     106        if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
    55107            continue;
    56108        }
    57         psKernel *covar = options->covariances->data[i]; // Covariance matrix
    58         if (!covar) {
    59             continue;
     109
     110        ppStackThread *thread = stack->threads->data[0]; // Representative stack
     111        pmReadout *inRO = thread->readouts->data[i]; // Template readout
     112        if (inRO && !wcsDone) {
     113            // Copy astrometry over
     114            wcsDone = true;
     115            pmHDU *inHDU = pmHDUFromCell(inRO->parent); // Template HDU
     116            pmHDU *outHDU = pmHDUFromCell(outRO->parent); // Output HDU
     117            pmChip *outChip = outRO->parent->parent; // Output chip
     118            pmFPA *outFPA = outChip->parent; // Output FPA
     119            if (!outHDU || !inHDU) {
     120                psWarning("Unable to find HDU at FPA level to copy astrometry.");
     121            } else {
     122                if (!pmAstromReadWCS(outFPA, outChip, inHDU->header, 1.0)) {
     123                    psErrorClear();
     124                    psWarning("Unable to read WCS astrometry from input FPA.");
     125                    wcsDone = false;
     126                } else {
     127                    if (!outHDU->header) {
     128                        outHDU->header = psMetadataAlloc();
     129                    }
     130                    if (!pmAstromWriteWCS(outHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
     131                        psErrorClear();
     132                        psWarning("Unable to write WCS astrometry to output FPA.");
     133                        wcsDone = false;
     134                    }
     135                }
     136            }
    60137        }
    61         float weight = options->weightings->data.F32[i]; // Weight to apply
    62         psBinaryOp(covar->image, covar->image, "*", psScalarAlloc(weight, PS_TYPE_F32));
    63         sumWeights += weight;
    64     }
    65     if (sumWeights > 0.0) {
    66         pmReadout *outRO = options->outRO;  // Output readout
    67         outRO->covariance = psImageCovarianceSum(options->covariances);
    68         psBinaryOp(outRO->covariance->image, outRO->covariance->image, "/",
    69                    psScalarAlloc(sumWeights, PS_TYPE_F32));
    70         psFree(options->covariances); options->covariances = NULL;
    71         psImageCovarianceTransfer(outRO->variance, outRO->covariance);
    72138    }
    73139
     140    // Set exposure time correctly
     141    {
     142        float exptime = 0.0;            // Summed exposure time
     143        for (int i = 0; i < options->num; i++) {
     144            if (options->inputMask->data.U8[i]) {
     145                continue;
     146            }
     147            exptime += options->exposures->data.F32[i];
     148        }
     149
     150        {
     151            psMetadataItem *item = psMetadataLookup(outRO->parent->concepts, "CELL.EXPOSURE");
     152            item->data.F32 = exptime;
     153        }
     154        {
     155            psMetadataItem *item = psMetadataLookup(outRO->parent->parent->parent->concepts, "FPA.EXPOSURE");
     156            item->data.F32 = exptime;
     157        }
     158    }
     159
     160    // Put version information into the header
     161    pmHDU *hdu = pmHDUFromCell(outRO->parent);
     162    if (!hdu) {
     163        psError(PPSTACK_ERR_PROG, false, "Unable to find HDU for output.");
     164        return false;
     165    }
     166    if (!hdu->header) {
     167        hdu->header = psMetadataAlloc();
     168    }
     169    ppStackVersionHeader(hdu->header);
     170
     171
    74172#ifdef TESTING
    75     pmStackVisualPlotTestImage(outRO->image, "combined_initial.fits");
    76     ppStackWriteImage("combined_final.fits", NULL, outRO->image, config);
     173    static int pass = 0;                // Pass through
     174    psString name = NULL;               // Name of file
     175    psStringAppend(&name, "combined_image_final_%d.fits", pass);
     176    pass++;
     177    ppStackWriteImage(name, NULL, outRO->image, config);
     178    psStringSubstitute(&name, "mask", "image");
     179    ppStackWriteImage(name, NULL, outRO->mask, config);
     180    psStringSubstitute(&name, "variance", "mask");
     181    ppStackWriteImage(name, NULL, outRO->variance, config);
     182    psFree(name);
     183
     184    pmStackVisualPlotTestImage(outRO->image, "combined_image_final.fits");
    77185#endif
    78186
Note: See TracChangeset for help on using the changeset viewer.