IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 21264


Ignore:
Timestamp:
Feb 2, 2009, 8:05:38 AM (17 years ago)
Author:
Paul Price
Message:

Adding work on integrating covariances into the read/write system. Not sure I like this framework (putting covariances for an HDU into a common table); might rework to write a covariance image for each cell. I think I started doing that and ran into problems, but I can't remember what they were and how they stopped me.

Location:
branches/pap_branch_20090128/psModules/src/camera
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/pap_branch_20090128/psModules/src/camera/pmFPARead.c

    r21211 r21264  
    2828    FPA_READ_TYPE_IMAGE,                // Read image
    2929    FPA_READ_TYPE_MASK,                 // Read mask
    30     FPA_READ_TYPE_VARIANCE,               // Read variance map
     30    FPA_READ_TYPE_VARIANCE,             // Read variance map
    3131    FPA_READ_TYPE_HEADER                // Read header
    3232} fpaReadType;
     
    515515    }
    516516
    517     // XXX for IMAGE, we need the CELL.BAD value, but for MASK, we need the BAD mask value
    518 
    519     float bad = 0;
    520     if (type == FPA_READ_TYPE_MASK) {
    521       bad = 1.0;
    522     } else {
    523       bad = psMetadataLookupF32(&mdok, cell->concepts, "CELL.BAD"); // Bad level
     517    // Need to set the invalid (unread) pixels appropriately, and to read the mask bits
     518    float bad = 0;                      // Bad level
     519    switch (type) {
     520      case FPA_READ_TYPE_MASK: {
     521          pmHDU *phu = pmHDUGetHighest(cell->parent->parent, cell->parent, cell); // Primary header
     522          if (!pmConfigMaskReadHeader(config, phu->header)) {
     523              psError(PS_ERR_IO, false, "Unable to determine mask bits");
     524              return false;
     525          }
     526          bad = pmConfigMaskGet("BAD", config);
     527          break;
     528      }
     529      case FPA_READ_TYPE_IMAGE:
     530      case FPA_READ_TYPE_VARIANCE:
     531        bad = psMetadataLookupF32(&mdok, cell->concepts, "CELL.BAD");
     532        break;
     533      default:
     534        psAbort("Unrecognised type: %x", type);
    524535    }
    525536
     
    721732            return NULL;
    722733        }
     734
     735        if (type == FPA_READ_TYPE_VARIANCE && hdu->covariances) {
     736            psArray *covariances = hdu->covariances; // Covariances in HDU
     737            for (int i = 0; i < covariances->n; i++) {
     738                pmHDUCovariances *covar = covariances->data[i]; // Covariance information
     739
    723740        psFree(readout);                // Drop reference
    724741    }
     
    852869    float bad = psMetadataLookupF32(&mdok, cell->concepts, "CELL.BAD"); // Bad level
    853870    if (!mdok) {
    854         psLogMsg(__func__, PS_LOG_WARN, "CELL.BAD is not set --- assuming zero.\n");
     871        psWarning("CELL.BAD is not set --- assuming zero.\n");
    855872        bad = 0.0;
    856873    }
  • branches/pap_branch_20090128/psModules/src/camera/pmFPAWrite.c

    r21211 r21264  
    2929    FPA_WRITE_TYPE_IMAGE,               // Write image
    3030    FPA_WRITE_TYPE_MASK,                // Write mask
    31     FPA_WRITE_TYPE_VARIANCE               // Write variance map
     31    FPA_WRITE_TYPE_VARIANCE             // Write variance map
    3232} fpaWriteType;
    3333
     
    7474}
    7575
     76
     77
     78
     79// Some type-specific additions to the header
     80static bool writeUpdateHeader(pmFPA *fpa, // FPA of interest
     81                              pmChip *chip, // Chip of interest, or NULL
     82                              pmCell *cell, // Cell of interest, or NULL
     83                              fpaWriteType type // Type to write
     84    )
     85{
     86    switch (type) {
     87      case FPA_WRITE_TYPE_MASK: {
     88          pmHDU *phu = pmHDUGetHighest(fpa, chip, cell); // Primary header
     89          if (!pmConfigMaskWriteHeader(config, phu->header)) {
     90              psError(PS_ERR_UNKNOWN, false, "Unable to set the mask names in the PHU header");
     91              return false;
     92          }
     93          break;
     94      }
     95      case FPA_WRITE_TYPE_VARIANCE: {
     96          pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // Header being written
     97
     98
     99
    76100// Write a cell image/mask/variance
    77101static bool cellWrite(pmCell *cell,     // Cell to write
     
    97121    // generate the HDU, but only copies the structure.
    98122    if (!blank && !hdu->blankPHU && !*imageArray && (!pmHDUGenerateForCell(cell) || !*imageArray)) {
    99         psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for cell --- likely programming error.\n");
     123        psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for cell --- likely programming error.");
    100124        return false;
    101125    }
     
    106130
    107131    if (writeBlank || writeImage) {
    108 
    109132        pmFPAUpdateNames(cell->parent->parent, cell->parent, cell);
    110133        pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS |
    111134                                 PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE;
    112135        if (!pmConceptsWriteCell(cell, source, true, config)) {
    113             psError(PS_ERR_IO, false, "Unable to write concepts for cell.\n");
     136            psError(PS_ERR_IO, false, "Unable to write concepts for cell.");
    114137            return false;
    115138        }
     139
     140
    116141        if (!appropriateWriteFunc(hdu, fits, config, type)) {
    117142            psError(PS_ERR_IO, false, "Unable to write HDU for cell.\n");
  • branches/pap_branch_20090128/psModules/src/camera/pmFPAfileIO.c

    r21211 r21264  
    771771          }
    772772
    773           // XXX if we have a mask file, then we need to read the mask bit names
    774           // defined for this file
    775           if (file->type == PM_FPA_FILE_MASK) {
    776             if (!pmConfigMaskReadHeader (config, phu)) {
    777                 psError(PS_ERR_IO, false, "error in mask bits");
    778                 return false;
    779             }
    780           }
    781 
    782773          // determine the current format from the header
    783774          // determine camera if not specified already
     
    786777          psString formatName = NULL;
    787778          psString cameraName = NULL;
    788           file->format = pmConfigCameraFormatFromHeader (&camera, &cameraName, &formatName, config, phu, true);
     779          file->format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true);
    789780          if (!file->format) {
    790781            psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", file->filename);
  • branches/pap_branch_20090128/psModules/src/camera/pmHDU.c

    r21211 r21264  
    1212#include "pmHDU.h"
    1313#include "pmFPA.h"
     14
     15#define COVARIANCE_INDICATOR "PS_COVAR" // FITS keyword to indicate if covariance matrices are present
    1416
    1517//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    160162    PS_ASSERT_PTR_NON_NULL(fits, false);
    161163
    162     return hduRead(hdu, &hdu->variances, fits);
     164    if (!hduRead(hdu, &hdu->variances, fits)) {
     165        return false;
     166    }
     167
     168    return true;
    163169}
    164170
     
    240246    PS_ASSERT_PTR_NON_NULL(fits, false);
    241247
     248    pmHDUCovariancesWriteHeader(hdu);
     249
    242250    psImageMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config); // Value to mask
    243     return hduWrite(hdu, hdu->variances, hdu->masks, maskVal, fits);
    244 }
     251    if (!hduWrite(hdu, hdu->variances, hdu->masks, maskVal, fits)) {
     252        return false;
     253    }
     254    pmHDUCovarianceClearHeader(hdu);
     255
     256    if (!pmHDUCovarianceWrite(hdu, fits)) {
     257        psError(PS_ERR_UNKNOWN, false, "Unable to write covariance");
     258        return false;
     259    }
     260
     261    return true;
     262}
  • branches/pap_branch_20090128/psModules/src/camera/pmHDU.h

    r21211 r21264  
    44 * @author Paul Price, IfA
    55 *
    6  * @version $Revision: 1.9.22.1 $ $Name: not supported by cvs2svn $
    7  * @date $Date: 2009-01-29 00:33:51 $
     6 * @version $Revision: 1.9.22.2 $ $Name: not supported by cvs2svn $
     7 * @date $Date: 2009-02-02 18:05:38 $
    88 * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
    99 */
     
    2222/// Of course, it is not an exact replica of a FITS HDU --- they have no mask and variance data, but these are
    2323/// stored here for convenience --- it keeps all the relevant data about the image in one place.
    24 typedef struct
    25 {
     24typedef struct {
    2625    psString extname;                   ///< The extension name
    2726    bool blankPHU;                      ///< Is this a blank FITS Primary Header Unit, i.e., no data?
    2827    psMetadata *format;                 ///< The camera format
    2928    psMetadata *header;                 ///< The FITS header, or NULL if primary for FITS; or section info
    30     psArray *images;                    ///< The pixel data
    31     psArray *variances;                   ///< The pixel data
    32     psArray *masks;                     ///< The pixel data
    33 }
    34 pmHDU;
     29    psArray *images;                    ///< Pixel data
     30    psArray *variances;                 ///< Variance in the pixel data, or NULL
     31    psArray *masks;                     ///< Mask for the pixel data, or NULL
     32    psArray *covariances;               ///< Covariance matrices (pmHDUCovariance), or NULL
     33} pmHDU;
    3534
    3635
  • branches/pap_branch_20090128/psModules/src/camera/pmHDUGenerate.c

    r21211 r21264  
    355355    psElemType imageType = 0;           // Type of readout images
    356356    psElemType maskType = 0;            // Type of readout masks
    357     psElemType varianceType = 0;          // Type of readout variances
     357    psElemType varianceType = 0;        // Type of readout variances
     358    psElemType covarianceType = 0;      // Type of readout covariances
    358359    {
    359360        psListIterator *iter = psListIteratorAlloc(cells, PS_LIST_HEAD, false); // Iterator for cells
     
    384385                    varianceType = checkTypes(varianceType, readout->variance->type.type);
    385386                }
     387                if (!hdu->covariances && readout->covariance) {
     388                    covarianceType = checkTypes(covarianceType, PS_TYPE_F32);
     389                }
    386390            }
    387391        }
    388392        psFree(iter);
    389393    }
    390     if (numReadouts == 0 || (imageType == 0 && maskType == 0 && varianceType == 0)) {
     394    if (numReadouts == 0 || (imageType == 0 && maskType == 0 && varianceType == 0 && covarianceType == 0)) {
    391395        // Nothing from which to create an HDU
    392396        psFree(cells);
     
    428432            hdu->variances->data[i] = variance;
    429433        }
     434    }
     435    if (covarianceType) {
     436        hdu->covariances = psHashAlloc(psListLength(cells));
    430437    }
    431438
     
    448455
    449456            psArray *readouts = cell->readouts; // Array of readouts
     457
     458            // Check size of covariances
     459            int xMinCovar = INT_MAX, xMaxCovar = INT_MIN, yMinCovar = INT_MAX, yMaxCovar = INT_MIN; // Size
     460            if (covarianceType) {
     461                for (int i = 0; i < readouts->n; i++) {
     462                    pmReadout *readout = readouts->data[i]; // The readout of interest
     463                    if (!readout) {
     464                        continue;
     465                    }
     466                    psKernel *covar = readout->covariance; // Covariance matrix
     467                    xMinCovar = PS_MIN(xMinCovar, covar->xMin);
     468                    xMaxCovar = PS_MAX(xMaxCovar, covar->xMax);
     469                    yMinCovar = PS_MIN(yMinCovar, covar->yMin);
     470                    yMaxCovar = PS_MAX(yMaxCovar, covar->yMax);
     471                }
     472            }
     473
    450474            psArray *hduImages = hdu->images; // Array of images in the HDU
    451475            psArray *hduMasks = hdu->masks; // Array of masks in the HDU
    452476            psArray *hduVariances = hdu->variances; // Array of variances in the HDU
     477            psArray *covariances = (covarianceType ? psArrayAlloc(readouts->n) : NULL; // Covariance images
    453478            for (int i = 0; i < readouts->n; i++) {
    454479                pmReadout *readout = readouts->data[i]; // The readout of interest
     
    471496                    psFree(readout->variance);
    472497                    readout->variance = new;
     498                }
     499                if (readout->covariance) {
     500                    psKernel *covar = readout->covariance; // Covariance matrix
     501                    int xMin = covar->xMin, xMax = covar->xMax, yMin = covar->yMin, yMax = covar->yMax;// Size
     502                    if (xMin == xMinCovar && xMax == xMaxCovar && yMin == yMinCovar && yMax == yMaxCovar) {
     503                        variances->data[i] = psMemIncrRefCounter(readout->covariance);
     504                    } else {
     505                        psKernel *new = psKernelAlloc(xMinCovar, xMaxCovar, yMinCovar, yMaxCovar);// New covar
     506                        psImageInit(covar->image, 0);
     507                        psImageOverlaySection(new->image, covar->image,
     508                                              xMinCovar - xMin, yMinCovar - yMin, "=");
     509                        variances->data[i] = new;
     510                    }
    473511                }
    474512
     
    495533            }
    496534            psFree(biassecsIter);
     535
     536            if (covariances) {
     537                const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // Name of cell
     538                pmChip *chip = cell->parent; // Parent chip
     539                const char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME"); // Name of chip
     540                if (!cellName || !chipName) {
     541                    psError(PS_ERR_UNKNOWN, false, "Unable to find cell or chip name.");
     542                    return false;
     543                }
     544                psString name = NULL; // Name for covariance extension
     545                psStringAppend(&name, "COVAR_%s_%s", chipName, cellName);
     546                if (psHashLookup(hdu->covariances, name)) {
     547                    psHashRemove(hash, name);
     548                }
     549                psHashAdd(hdu->covariances, name, covariance);
     550                psFree(name);
     551            }
    497552        } // Iterating over cells within the HDU
    498553        psFree(iter);
     
    504559
    505560// Return the level that an extension applies to
    506 static pmFPALevel extensionLevel(pmHDU *hdu // HDU to check
     561static pmFPALevel extensionLevel(const pmHDU *hdu // HDU to check
    507562                                )
    508563{
     
    512567    }
    513568    bool mdok = true;                   // Status of MD lookup
    514     psMetadata *file = psMetadataLookupMetadata(&mdok, hdu->format, "FILE"); // File information for camera format
     569    psMetadata *file = psMetadataLookupMetadata(&mdok, hdu->format, "FILE"); // File info for camera format
    515570    if (!mdok || !file) {
    516571        psError(PS_ERR_UNEXPECTED_NULL, true, "Can't file FILE information for camera format "
     
    581636        return false;
    582637    }
    583     if (hdu->images && hdu->masks && hdu->variances) {
     638    if (hdu->images && hdu->masks && hdu->variances && hdu->covariances) {
    584639        // It's already here!
    585640        return true;
     
    631686        return generateForCells(chip);
    632687    }
    633     if (hdu->images && hdu->masks && hdu->variances) {
     688    if (hdu->images && hdu->masks && hdu->variances && hdu->covariances) {
    634689        // It's already here!
    635690        return true;
     
    680735        return generateForChips(fpa);
    681736    }
    682     if (hdu->images && hdu->masks && hdu->variances) {
     737    if (hdu->images && hdu->masks && hdu->variances && hdu->covariances) {
    683738        // It's already here!
    684739        return true;
Note: See TracChangeset for help on using the changeset viewer.