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

    r23602 r27838  
    99#include "ppStack.h"
    1010#include "ppStackLoop.h"
     11
     12//#define TESTING
     13
    1114
    1215// Update the value of a concept
     
    3942    options->weightings = psVectorAlloc(num, PS_TYPE_F32); // Combination weightings for images (1/noise^2)
    4043    psVectorInit(options->weightings, 0.0);
    41     options->covariances = psArrayAlloc(num); // Covariance matrices
     44    options->origCovars = psArrayAlloc(num);
     45    options->convCovars = psArrayAlloc(num); // Covariance matrices
     46
     47    psVector *renorms = psVectorAlloc(num, PS_TYPE_F32); // Renormalisation values for variances
     48    psVectorInit(renorms, NAN);
    4249
    4350    psList *fpaList = psListAlloc(NULL); // List of input FPAs, for concept averaging
     
    5259        pmFPAfileActivate(config->files, false, NULL);
    5360        ppStackFileActivationSingle(config, PPSTACK_FILES_CONVOLVE, true, i);
     61        if (options->convolve) {
     62            // XXX PPSTACK.CONV.KERNEL not defined unless convolve
     63            // pmFPAfileActivate(config->files, true, "PPSTACK.CONV.KERNEL");
     64            pmFPAfileActivateSingle(config->files, true, "PPSTACK.CONV.KERNEL", i); // Activated file
     65        }
     66
    5467        pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPSTACK.INPUT", i); // File of interest
    5568        pmFPAview *view = ppStackFilesIterateDown(config);
     
    6982        } else if (options->numCols != readout->image->numCols ||
    7083                   options->numRows != readout->image->numRows) {
    71             psError(PS_ERR_UNKNOWN, true, "Sizes of input images don't match: %dx%d vs %dx%d",
     84            psError(PPSTACK_ERR_ARGUMENTS, true, "Sizes of input images don't match: %dx%d vs %dx%d",
    7285                    readout->image->numCols, readout->image->numRows, options->numCols, options->numRows);
    7386            psFree(rng);
     
    7992        // Background subtraction, scaling and normalisation is performed automatically by the image matching
    8093        psTimerStart("PPSTACK_MATCH");
     94        options->origCovars->data[i] = psMemIncrRefCounter(readout->covariance);
    8195        if (!ppStackMatch(readout, options, i, config)) {
    82             psErrorStackPrint(stderr, "Unable to match image %d --- ignoring.", i);
    83             options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] |= PPSTACK_MASK_MATCH;
    84             psErrorClear();
    85             continue;
    86         }
    87         options->covariances->data[i] = psMemIncrRefCounter(readout->covariance);
     96            // XXX many things can cause a failure of ppStackMatch -- should some be handled differently?
     97            psErrorCode error = psErrorCodeLast(); // Error code
     98            switch (error) {
     99                // Fatal errors
     100              case PM_ERR_CONFIG:
     101              case PPSTACK_ERR_CONFIG:
     102              case PPSTACK_ERR_IO:
     103                psError(error, false, "Unable to match image %d due to fatal error.", i);
     104                return false;
     105                // Non-fatal errors
     106              case PM_ERR_STAMPS:
     107              case PM_ERR_SMALL_AREA:
     108              case PPSTACK_ERR_DATA:
     109              default:
     110                psErrorStackPrint(stderr, "Unable to match image %d --- ignoring.", i);
     111                options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] |= PPSTACK_MASK_MATCH;
     112                psErrorClear();
     113                continue;
     114            }
     115        }
     116        options->convCovars->data[i] = psMemIncrRefCounter(readout->covariance);
     117
     118        float renorm = psMetadataLookupF32(NULL, readout->analysis, PM_READOUT_ANALYSIS_RENORM);
     119        if (!isfinite(renorm)) {
     120            renorm = 1.0;
     121        }
     122        renorms->data.F32[i] = renorm;
    88123
    89124        if (options->stats) {
     
    114149        pmHDU *hdu = readout->parent->parent->parent->hdu; // HDU for convolved image
    115150        assert(hdu);
    116         ppStackWriteImage(options->imageNames->data[i], hdu->header, readout->image, config);
     151        if (!ppStackWriteImage(options->convImages->data[i], hdu->header, readout->image, config)) {
     152            psError(PPSTACK_ERR_IO, false, "Unable to write convolved image %d", i);
     153            psFree(fpaList);
     154            psFree(cellList);
     155            psFree(rng);
     156            return false;
     157        }
    117158        psMetadata *maskHeader = psMetadataCopy(NULL, hdu->header); // Copy of header, for mask
    118159        pmConfigMaskWriteHeader(config, maskHeader);
    119         ppStackWriteImage(options->maskNames->data[i], maskHeader, readout->mask, config);
     160        if (!ppStackWriteImage(options->convMasks->data[i], maskHeader, readout->mask, config)) {
     161            psError(PPSTACK_ERR_IO, false, "Unable to write convolved mask %d", i);
     162            psFree(fpaList);
     163            psFree(cellList);
     164            psFree(rng);
     165            psFree(maskHeader);
     166            return false;
     167        }
    120168        psFree(maskHeader);
    121         psImageCovarianceTransfer(readout->variance, readout->covariance);
    122         ppStackWriteImage(options->varianceNames->data[i], hdu->header, readout->variance, config);
     169        if (!ppStackWriteImage(options->convVariances->data[i], hdu->header, readout->variance, config)) {
     170            psError(PPSTACK_ERR_IO, false, "Unable to write convolved variance %d", i);
     171            psFree(fpaList);
     172            psFree(cellList);
     173            psFree(rng);
     174            return false;
     175        }
    123176#ifdef TESTING
    124177        {
     
    129182            psFree(name);
    130183        }
     184        {
     185            int numCols = readout->image->numCols, numRows = readout->image->numRows;
     186            psImage *sn = psImageAlloc(numCols, numRows, PS_TYPE_F32);
     187            for (int y = 0; y < numRows; y++) {
     188                for (int x = 0; x < numCols; x++) {
     189                    sn->data.F32[y][x] = readout->image->data.F32[y][x] /
     190                        sqrtf(readout->variance->data.F32[y][x]);
     191                }
     192            }
     193            psString name = NULL;
     194            psStringAppend(&name, "signoise_%d.fits", i);
     195            ppStackWriteImage(name, hdu->header, sn, config);
     196            psFree(name);
     197            psFree(sn);
     198        }
    131199#endif
    132200
     
    136204        psListAdd(fpaList, PS_LIST_TAIL, inCell->parent->parent);
    137205
     206        // Correct ZP
     207        if (options->matchZPs) {
     208            // I think I need to take off the exposure time because we're going to set the new exposure time
     209            psMetadataItem *zpItem = psMetadataLookup(inCell->parent->parent->concepts, "FPA.ZP");
     210            zpItem->data.F32 += options->norm->data.F32[i] + 2.5*log10(options->sumExposure);
     211
     212            psMetadataItem *expItem = psMetadataLookup(inCell->parent->parent->concepts, "FPA.EXPOSURE");
     213            expItem->data.F32 = options->sumExposure;
     214
     215            expItem = psMetadataLookup(inCell->concepts, "CELL.EXPOSURE");
     216            expItem->data.F32 = options->sumExposure;
     217        }
     218
    138219        options->cells->data[i] = psMemIncrRefCounter(inCell);
    139220        if (!ppStackFilesIterateUp(config)) {
     
    149230    psFree(rng);
    150231
    151     psFree(options->norm); options->norm = NULL;
    152232    psFree(options->sourceLists); options->sourceLists = NULL;
    153233    psFree(options->psf); options->psf = NULL;
     
    155235
    156236    if (numGood == 0) {
    157         psError(PS_ERR_UNKNOWN, false, "No good images to combine.");
     237        psError(PPSTACK_ERR_REJECTED, false, "No good images to combine.");
    158238        psFree(fpaList);
    159239        psFree(cellList);
     
    167247        pmFPAview view;                 // View for output
    168248        view.chip = view.cell = view.readout = 0;
     249
    169250        pmCell *outCell = pmFPAfileThisCell(config->files, &view, "PPSTACK.OUTPUT"); // Output cell
    170251        pmFPA *outFPA = outCell->parent->parent; // Output FPA
     252
     253        pmCell *unconvCell = pmFPAfileThisCell(config->files, &view, "PPSTACK.UNCONV"); // Unconvolved cell
     254        pmFPA *unconvFPA = unconvCell->parent->parent;                                  // Unconvolved FPA
     255
    171256        pmConceptsAverageFPAs(outFPA, fpaList);
    172257        pmConceptsAverageCells(outCell, cellList, NULL, NULL, true);
     258
     259        pmConceptsAverageFPAs(unconvFPA, fpaList);
     260        pmConceptsAverageCells(unconvCell, cellList, NULL, NULL, true);
     261
    173262        psFree(fpaList);
    174263        psFree(cellList);
     
    177266        UPDATE_CONCEPT(outCell, "CELL.EXPOSURE", options->sumExposure);
    178267        UPDATE_CONCEPT(outCell, "CELL.DARKTIME", NAN);
     268        UPDATE_CONCEPT(outFPA,  "FPA.ZP",        options->zp);
     269
     270        UPDATE_CONCEPT(unconvFPA,  "FPA.EXPOSURE",  options->sumExposure);
     271        UPDATE_CONCEPT(unconvCell, "CELL.EXPOSURE", options->sumExposure);
     272        UPDATE_CONCEPT(unconvCell, "CELL.DARKTIME", NAN);
     273        UPDATE_CONCEPT(unconvFPA,  "FPA.ZP",        options->zp);
    179274    }
    180275
     
    191286        assert(values->n == numGood);
    192287        if (!psVectorSortInPlace(values)) {
    193             psError(PS_ERR_UNKNOWN, false, "Unable to sort vector.");
     288            psError(PPSTACK_ERR_PROG, false, "Unable to sort vector.");
    194289            psFree(values);
    195290            return false;
     
    211306            numGood = 0;                    // Number of good images
    212307            for (int i = 0; i < num; i++) {
    213               if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] & PPSTACK_MASK_ALL) {
     308                if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] & PPSTACK_MASK_ALL) {
    214309                    continue;
    215310                }
     
    229324
    230325    if (numGood == 0) {
    231         psError(PS_ERR_UNKNOWN, false, "No good images to combine.");
     326        psError(PPSTACK_ERR_REJECTED, false, "No good images to combine.");
    232327        return false;
    233328    }
     329
     330    // Correct chi^2 for renormalisation
     331    psBinaryOp(options->matchChi2, options->matchChi2, "/", renorms);
     332    for (int i = 0; i < num; i++) {
     333        psLogMsg("ppStack", PS_LOG_INFO, "Additional variance for image %d: %f\n",
     334                 i, options->matchChi2->data.F32[i]);
     335    }
     336    psFree(renorms);
    234337
    235338    return true;
Note: See TracChangeset for help on using the changeset viewer.