IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 5, 2009, 4:31:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging pap_branch_20090128. Resolved a small number of conflicts. Compiles, but not tested in detail.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/camera/pmFPAWrite.c

    r21279 r21363  
    99
    1010#include "pmConfig.h"
     11#include "pmConfigMask.h"
    1112#include "pmHDU.h"
    1213#include "pmFPA.h"
     
    2930    FPA_WRITE_TYPE_IMAGE,               // Write image
    3031    FPA_WRITE_TYPE_MASK,                // Write mask
    31     FPA_WRITE_TYPE_WEIGHT               // Write weight map
     32    FPA_WRITE_TYPE_VARIANCE             // Write variance map
    3233} fpaWriteType;
    3334
     
    4647    case FPA_WRITE_TYPE_MASK:
    4748        return &hdu->masks;
    48     case FPA_WRITE_TYPE_WEIGHT:
    49         return &hdu->weights;
     49    case FPA_WRITE_TYPE_VARIANCE:
     50        return &hdu->variances;
    5051    default:
    5152        psAbort("Unknown write type: %x\n", type);
     
    6667    case FPA_WRITE_TYPE_MASK:
    6768        return pmHDUWriteMask(hdu, fits, config);
    68     case FPA_WRITE_TYPE_WEIGHT:
    69         return pmHDUWriteWeight(hdu, fits, config);
     69    case FPA_WRITE_TYPE_VARIANCE:
     70        return pmHDUWriteVariance(hdu, fits, config);
    7071    default:
    7172        psAbort("Unknown write type: %x\n", type);
     
    7475}
    7576
    76 // Write a cell image/mask/weight
     77// Indicate whether a covariance matrix is defined
     78static bool readoutSearchCovariances(pmReadout *ro)
     79{
     80    return ro->covariance ? true : false;
     81}
     82
     83// Search for a covariance matrix
     84#define SEARCH_COVARIANCES(NAME, PARENT, CHILD, CHILDREN, TESTFUNC) \
     85static bool NAME(PARENT *parent) \
     86{ \
     87    if (!parent || !parent->CHILDREN) { \
     88        return false; \
     89    } \
     90    psArray *children = parent->CHILDREN; /* Array of children */ \
     91    for (int i = 0; i < children->n; i++) { \
     92        CHILD *child = children->data[i]; /* Child of interest */ \
     93        if (child && TESTFUNC(child)) { \
     94            return true; \
     95        } \
     96    } \
     97    return false; \
     98}
     99
     100SEARCH_COVARIANCES(cellSearchCovariances, pmCell, pmReadout, readouts, readoutSearchCovariances);
     101SEARCH_COVARIANCES(chipSearchCovariances, pmChip, pmCell,    cells,    cellSearchCovariances);
     102SEARCH_COVARIANCES(fpaSearchCovariances,  pmFPA,  pmChip,    chips,    chipSearchCovariances);
     103
     104// Some type-specific additions to the header
     105static bool writeUpdateHeader(pmFPA *fpa, // FPA of interest
     106                              pmChip *chip, // Chip of interest, or NULL
     107                              pmCell *cell, // Cell of interest, or NULL
     108                              fpaWriteType type, // Type to write
     109                              pmConfig *config // Configuration
     110                              )
     111{
     112    switch (type) {
     113      case FPA_WRITE_TYPE_MASK: {
     114          pmHDU *phu = pmHDUGetHighest(fpa, chip, cell); // Primary header
     115          if (!pmConfigMaskWriteHeader(config, phu->header)) {
     116              psError(PS_ERR_UNKNOWN, false, "Unable to set the mask names in the PHU header");
     117              return false;
     118          }
     119          break;
     120      }
     121      case FPA_WRITE_TYPE_VARIANCE: {
     122          bool covar = false;           // Are covariances present?
     123          if ((cell && cellSearchCovariances(cell)) ||
     124              (!cell && ((chip && chipSearchCovariances(chip)) ||
     125                         (!chip && fpa && fpaSearchCovariances(fpa))))) {
     126              covar = true;
     127          }
     128
     129          pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // Header being written
     130          psMetadataAddBool(hdu->header, PS_LIST_TAIL, PM_HDU_COVARIANCE_KEYWORD, PS_META_REPLACE,
     131                            "Is a covariance matrix present?", covar);
     132          break;
     133      }
     134      default:
     135        break;
     136    }
     137
     138    return true;
     139}
     140
     141
     142// Write a cell image/mask/variance
    77143static bool cellWrite(pmCell *cell,     // Cell to write
    78144                      psFits *fits,     // FITS file to which to write
     
    97163    // generate the HDU, but only copies the structure.
    98164    if (!blank && !hdu->blankPHU && !*imageArray && (!pmHDUGenerateForCell(cell) || !*imageArray)) {
    99         psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for cell --- likely programming error.\n");
     165        psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for cell --- likely programming error.");
    100166        return false;
    101167    }
     
    106172
    107173    if (writeBlank || writeImage) {
    108 
    109174        pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
    110175                                 PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE;
    111176        if (!pmConceptsWriteCell(cell, source, true, config)) {
    112             psError(PS_ERR_IO, false, "Unable to write concepts for cell.\n");
     177            psError(PS_ERR_IO, false, "Unable to write concepts for cell.");
     178            return false;
     179        }
     180        if (!writeUpdateHeader(NULL, NULL, cell, type, config)) {
     181            psError(PS_ERR_UNKNOWN, false, "Unable to update header for writing");
    113182            return false;
    114183        }
     
    123192}
    124193
    125 // Write a chip image/mask/weight
     194// Write a chip image/mask/variance
    126195static bool chipWrite(pmChip *chip,     // Chip to write
    127196                      psFits *fits,     // FITS file to which to write
     
    162231                return false;
    163232            }
     233
     234            if (!writeUpdateHeader(NULL, chip, NULL, type, config)) {
     235                psError(PS_ERR_UNKNOWN, false, "Unable to update header for writing");
     236                return false;
     237            }
     238
    164239            if (!appropriateWriteFunc(hdu, fits, config, type)) {
    165240                psError(PS_ERR_IO, false, "Unable to write HDU for chip.\n");
     
    186261
    187262
    188 // Write an FPA image/mask/weight
     263// Write an FPA image/mask/variance
    189264static bool fpaWrite(pmFPA *fpa,        // FPA to write
    190265                     psFits *fits,      // FITS file to which to write
     
    223298            if (!pmConceptsWriteFPA(fpa, source, true, config)) {
    224299                psError(PS_ERR_IO, false, "Unable to write concepts for FPA.\n");
     300                return false;
     301            }
     302            if (!writeUpdateHeader(fpa, NULL, NULL, type, config)) {
     303                psError(PS_ERR_UNKNOWN, false, "Unable to update header for writing");
    225304                return false;
    226305            }
     
    433512
    434513
    435 bool pmCellWriteWeight(pmCell *cell, psFits *fits, pmConfig *config, bool blank)
     514bool pmCellWriteVariance(pmCell *cell, psFits *fits, pmConfig *config, bool blank)
    436515{
    437516    PS_ASSERT_PTR_NON_NULL(cell, false);
    438517    PS_ASSERT_PTR_NON_NULL(fits, false);
    439     return cellWrite(cell, fits, config, blank, FPA_WRITE_TYPE_WEIGHT);
    440 }
    441 
    442 bool pmChipWriteWeight(pmChip *chip, psFits *fits, pmConfig *config, bool blank, bool recurse)
     518    if (!cellWrite(cell, fits, config, blank, FPA_WRITE_TYPE_VARIANCE)) {
     519        return false;
     520    }
     521    if (!pmCellWriteCovariance(fits, cell)) {
     522        return false;
     523    }
     524    return true;
     525}
     526
     527bool pmChipWriteVariance(pmChip *chip, psFits *fits, pmConfig *config, bool blank, bool recurse)
    443528{
    444529    PS_ASSERT_PTR_NON_NULL(chip, false);
    445530    PS_ASSERT_PTR_NON_NULL(fits, false);
    446     return chipWrite(chip, fits, config, blank, recurse, FPA_WRITE_TYPE_WEIGHT);
    447 }
    448 
    449 bool pmFPAWriteWeight(pmFPA *fpa, psFits *fits, pmConfig *config, bool blank, bool recurse)
     531    if (!chipWrite(chip, fits, config, blank, recurse, FPA_WRITE_TYPE_VARIANCE)) {
     532        return false;
     533    }
     534    if (!pmChipWriteCovariance(fits, chip)) {
     535        return false;
     536    }
     537    return true;
     538}
     539
     540bool pmFPAWriteVariance(pmFPA *fpa, psFits *fits, pmConfig *config, bool blank, bool recurse)
    450541{
    451542    PS_ASSERT_PTR_NON_NULL(fpa, false);
    452543    PS_ASSERT_PTR_NON_NULL(fits, false);
    453     return fpaWrite(fpa, fits, config, blank, recurse, FPA_WRITE_TYPE_WEIGHT);
     544    if (!fpaWrite(fpa, fits, config, blank, recurse, FPA_WRITE_TYPE_VARIANCE)) {
     545        return false;
     546    }
     547    if (!pmFPAWriteCovariance(fits, fpa)) {
     548        return false;
     549    }
     550    return true;
    454551}
    455552
     
    526623    return numWrite;
    527624}
     625
     626bool pmCellWriteCovariance(psFits *fits, const pmCell *cell)
     627{
     628    PS_ASSERT_PTR_NON_NULL(cell, false);
     629    PS_ASSERT_PTR_NON_NULL(fits, false);
     630
     631    int numCovar = 0;
     632    psArray *readouts = cell->readouts; // Array of readouts
     633    for (int i = 0; i < readouts->n; i++) {
     634        pmReadout *readout = readouts->data[i]; // The readout of interest
     635        if (readout && readout->covariance) {
     636            numCovar++;
     637        }
     638    }
     639    if (numCovar == 0) {
     640        return true;
     641    }
     642    if (numCovar != readouts->n) {
     643        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     644                "Number of covariances (%d) doesn't match number of readouts (%ld)",
     645                numCovar, readouts->n);
     646        return false;
     647    }
     648
     649    // Check size of covariances
     650    int xMinCovar = INT_MAX, xMaxCovar = INT_MIN, yMinCovar = INT_MAX, yMaxCovar = INT_MIN; // Size
     651    for (int i = 0; i < readouts->n; i++) {
     652        pmReadout *readout = readouts->data[i]; // The readout of interest
     653        psAssert(readout, "Should be defined.");
     654        psKernel *covar = readout->covariance; // Covariance matrix
     655        psAssert(covar, "Should be defined.");
     656        xMinCovar = PS_MIN(xMinCovar, covar->xMin);
     657        xMaxCovar = PS_MAX(xMaxCovar, covar->xMax);
     658        yMinCovar = PS_MIN(yMinCovar, covar->yMin);
     659        yMaxCovar = PS_MAX(yMaxCovar, covar->yMax);
     660    }
     661
     662    // Correct covariances to common size
     663    psArray *images = psArrayAlloc(numCovar); // Array of images
     664    for (int i = 0; i < readouts->n; i++) {
     665        pmReadout *readout = readouts->data[i]; // The readout of interest
     666        psAssert(readout, "Should be defined.");
     667        psKernel *covar = readout->covariance; // Covariance matrix
     668        psAssert(covar, "Should be defined.");
     669        int xMin = covar->xMin, xMax = covar->xMax, yMin = covar->yMin, yMax = covar->yMax;// Size
     670        if (xMin == xMinCovar && xMax == xMaxCovar && yMin == yMinCovar && yMax == yMaxCovar) {
     671            images->data[i] = psMemIncrRefCounter(covar->image);
     672        } else {
     673            psImage *new = psImageAlloc(xMaxCovar - xMinCovar + 1, yMaxCovar - yMinCovar + 1, PS_TYPE_F32);
     674            psImageInit(new, 0);
     675            psImageOverlaySection(new, covar->image, xMinCovar - xMin, yMinCovar - yMin, "=");
     676            images->data[i] = new;
     677        }
     678    }
     679
     680    // Determine extension name
     681    const char *chipName = psMetadataLookupStr(NULL, cell->parent->concepts, "CHIP.NAME"); // Name of chip
     682    const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // Name of cell
     683    psString extname = NULL;            // Extension name
     684    psStringAppend(&extname, "COVAR_%s_%s", chipName, cellName);
     685
     686    // Generate header
     687    pmHDU *hdu = pmHDUFromCell(cell);   // HDU for cell
     688    psMetadata *header = psMetadataCopy(NULL, hdu->header); // Header to write
     689    psMetadataAddS32(header, PS_LIST_TAIL, "COVARIANCE.CENTRE.X", PS_META_REPLACE,
     690                     "Centre of covariance matrix in x", -xMinCovar);
     691    psMetadataAddS32(header, PS_LIST_TAIL, "COVARIANCE.CENTRE.Y", PS_META_REPLACE,
     692                     "Centre of covariance matrix in y", -yMinCovar);
     693
     694    // Write images
     695    if (!psFitsWriteImageCube(fits, header, images, extname)) {
     696        psError(PS_ERR_UNKNOWN, false, "Unable to write covariances from chip %s, cell %s to extension %s",
     697                chipName, cellName, extname);
     698        psFree(extname);
     699        psFree(header);
     700        psFree(images);
     701        return 0;
     702    }
     703    psFree(extname);
     704    psFree(header);
     705    psFree(images);
     706
     707    return true;
     708}
     709
     710
     711bool pmChipWriteCovariance(psFits *fits, const pmChip *chip)
     712{
     713    PS_ASSERT_PTR_NON_NULL(chip, false);
     714    PS_ASSERT_PTR_NON_NULL(fits, false);
     715
     716    psArray *cells = chip->cells;       // Array of cells
     717    for (int i = 0; i < cells->n; i++) {
     718        pmCell *cell = cells->data[i];  // Cell of interest
     719        if (!pmCellWriteCovariance(fits, cell)) {
     720            return false;
     721        }
     722    }
     723
     724    return true;
     725}
     726
     727
     728bool pmFPAWriteCovariance(psFits *fits, const pmFPA *fpa)
     729{
     730    PS_ASSERT_PTR_NON_NULL(fpa, false);
     731    PS_ASSERT_PTR_NON_NULL(fits, false);
     732
     733    psArray *chips = fpa->chips;        // Array of chips
     734    for (int i = 0; i < chips->n; i++) {
     735        pmChip *chip = chips->data[i];  // Chip of interest
     736        if (!pmChipWriteCovariance(fits, chip)) {
     737            return false;
     738        }
     739    }
     740
     741    return true;
     742}
Note: See TracChangeset for help on using the changeset viewer.