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

    r21183 r21363  
    99#include <pslib.h>
    1010
     11#include "pmConfig.h"
     12#include "pmConfigMask.h"
    1113#include "pmHDU.h"
    1214#include "pmFPA.h"
     
    2830    FPA_READ_TYPE_IMAGE,                // Read image
    2931    FPA_READ_TYPE_MASK,                 // Read mask
    30     FPA_READ_TYPE_WEIGHT,               // Read weight map
     32    FPA_READ_TYPE_VARIANCE,             // Read variance map
    3133    FPA_READ_TYPE_HEADER                // Read header
    3234} fpaReadType;
     
    5456      case FPA_READ_TYPE_MASK:
    5557        return readout->thisMaskScan;
    56       case FPA_READ_TYPE_WEIGHT:
    57         return readout->thisWeightScan;
     58      case FPA_READ_TYPE_VARIANCE:
     59        return readout->thisVarianceScan;
    5860      default:
    5961        psAbort("Unknown read type: %x\n", type);
     
    7476        readout->thisMaskScan = thisScan;
    7577        return readout->lastMaskScan;
    76       case FPA_READ_TYPE_WEIGHT:
    77         readout->thisWeightScan = thisScan;
    78         return readout->lastWeightScan;
     78      case FPA_READ_TYPE_VARIANCE:
     79        readout->thisVarianceScan = thisScan;
     80        return readout->lastVarianceScan;
    7981      default:
    8082        psAbort("Unknown read type: %x\n", type);
     
    9395      case FPA_READ_TYPE_MASK:
    9496        return readout->lastMaskScan;
    95       case FPA_READ_TYPE_WEIGHT:
    96         return readout->lastWeightScan;
     97      case FPA_READ_TYPE_VARIANCE:
     98        return readout->lastVarianceScan;
    9799      default:
    98100        psAbort("Unknown read type: %x\n", type);
     
    113115        readout->lastMaskScan = lastScan;
    114116        return readout->lastMaskScan;
    115       case FPA_READ_TYPE_WEIGHT:
    116         readout->lastWeightScan = lastScan;
    117         return readout->lastWeightScan;
     117      case FPA_READ_TYPE_VARIANCE:
     118        readout->lastVarianceScan = lastScan;
     119        return readout->lastVarianceScan;
    118120      default:
    119121        psAbort("Unknown read type: %x\n", type);
     
    132134      case FPA_READ_TYPE_MASK:
    133135        return &readout->mask;
    134       case FPA_READ_TYPE_WEIGHT:
    135         return &readout->weight;
     136      case FPA_READ_TYPE_VARIANCE:
     137        return &readout->variance;
    136138      default:
    137139        psAbort("Unknown read type: %x\n", type);
     
    193195
    194196    return naxis3;
     197}
     198
     199// Determine whether a FITS file contains covariance matrices
     200static bool hduCovariance(pmHDU *hdu,   // Header data unit
     201                          psFits *fits  // FITS file
     202    )
     203{
     204    if (hdu->extname && !psFitsMoveExtName(fits, hdu->extname)) {
     205        psError(PS_ERR_IO, false, "Unable to move to extension %s", hdu->extname);
     206        return false;
     207    }
     208    // Need to explicitly read the header, since the HDU may not contain the variance header
     209    psMetadata *header = psFitsReadHeader(NULL, fits); // Header
     210    if (!header) {
     211        psError(PS_ERR_IO, false, "Unable to read variance header.");
     212        return false;
     213    }
     214    bool mdok;                          // Status of MD lookup
     215    bool covar = psMetadataLookupBool(&mdok, header, PM_HDU_COVARIANCE_KEYWORD); // Got covariance?
     216    psFree(header);
     217    return covar;
    195218}
    196219
     
    350373    *target = psImageSubset(image, region);
    351374
    352     // Get the list of overscans: only for IMAGE types (no overscan for MASK and WEIGHT)
     375    // Get the list of overscans: only for IMAGE types (no overscan for MASK and VARIANCE)
    353376    if (type == FPA_READ_TYPE_IMAGE) {
    354377        if (readout->bias->n != 0) {
     
    515538    }
    516539
    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
     540    // Need to set the invalid (unread) pixels appropriately, and to read the mask bits
     541    float bad = 0;                      // Bad level
     542    switch (type) {
     543      case FPA_READ_TYPE_MASK: {
     544          // Need to explicitly read the header, since what's in the pmHDU may not correspond to the mask
     545          psMetadata *header = psFitsReadHeader(NULL, fits);
     546          if (!header) {
     547              psError(PS_ERR_IO, false, "Unable to read mask header.");
     548              return false;
     549          }
     550          if (!pmConfigMaskReadHeader(config, header)) {
     551              psError(PS_ERR_IO, false, "Unable to determine mask bits");
     552              psFree(header);
     553              return false;
     554          }
     555          psFree(header);
     556          bad = pmConfigMaskGet("BAD", config);
     557          break;
     558      }
     559      case FPA_READ_TYPE_IMAGE:
     560      case FPA_READ_TYPE_VARIANCE:
     561        bad = psMetadataLookupF32(&mdok, cell->concepts, "CELL.BAD");
     562        break;
     563      default:
     564        psAbort("Unrecognised type: %x", type);
    524565    }
    525566
     
    582623    *image = readoutReadComponent(*image, fits, trimsec, readdir, thisScan, lastScan, z, bad, pixelTypes[type]);
    583624
    584     // Read overscans only for "image" type --- weights and masks shouldn't record overscans
     625    // Read overscans only for "image" type --- variances and masks shouldn't record overscans
    585626    if (type == FPA_READ_TYPE_IMAGE) {
    586627        // Blow away existing data
     
    608649}
    609650
    610 // Read into an cell; this is the engine for pmCellRead, pmCellReadMask, pmCellReadWeight
     651// Read into an cell; this is the engine for pmCellRead, pmCellReadMask, pmCellReadVariance
    611652// Does most of the work for the reading --- reads the HDU, and portions the HDU into readouts.
    612653static bool cellRead(pmCell *cell,      // Cell into which to read
     
    640681        dataPointer = hdu->masks;
    641682        break;
    642       case FPA_READ_TYPE_WEIGHT:
    643         hduReadFunc = pmHDUReadWeight;
    644         dataPointer = hdu->weights;
     683      case FPA_READ_TYPE_VARIANCE:
     684        hduReadFunc = pmHDUReadVariance;
     685        dataPointer = hdu->variances;
    645686        break;
    646687      default:
     
    680721        imageArray = hdu->masks;
    681722        break;
    682       case FPA_READ_TYPE_WEIGHT:
    683         imageArray = hdu->weights;
     723      case FPA_READ_TYPE_VARIANCE:
     724        imageArray = hdu->variances;
    684725        break;
    685726      default:
     
    729770
    730771
    731 // Read into an chip; this is the engine for pmChipRead, pmChipReadMask, pmChipReadWeight
     772// Read into an chip; this is the engine for pmChipRead, pmChipReadMask, pmChipReadVariance
    732773// Iterates over component cells, reading each
    733774static bool chipRead(pmChip *chip,      // Chip into which to read
     
    760801
    761802
    762 // Read into an FPA; this is the engine for pmFPARead, pmFPAReadMask, pmFPAReadWeight
     803// Read into an FPA; this is the engine for pmFPARead, pmFPAReadMask, pmFPAReadVariance
    763804// Iterates over component chips, reading each
    764805static bool fpaRead(pmFPA *fpa,         // FPA into which to read
     
    852893    float bad = psMetadataLookupF32(&mdok, cell->concepts, "CELL.BAD"); // Bad level
    853894    if (!mdok) {
    854         psLogMsg(__func__, PS_LOG_WARN, "CELL.BAD is not set --- assuming zero.\n");
     895        psWarning("CELL.BAD is not set --- assuming zero.\n");
    855896        bad = 0.0;
    856897    }
     
    10911132
    10921133//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1093 // Reading the weight map
    1094 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1095 
    1096 bool pmReadoutMoreWeight(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config)
     1134// Reading the variance map
     1135//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     1136
     1137bool pmReadoutMoreVariance(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config)
    10971138{
    10981139    PS_ASSERT_PTR_NON_NULL(readout, false);
    10991140    PS_ASSERT_FITS_NON_NULL(fits, false);
    11001141
    1101     return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_WEIGHT, config);
    1102 }
    1103 
    1104 bool pmReadoutReadChunkWeight(pmReadout *readout, psFits *fits, int z, int numScans, int overlap,
     1142    return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_VARIANCE, config);
     1143}
     1144
     1145bool pmReadoutReadChunkVariance(pmReadout *readout, psFits *fits, int z, int numScans, int overlap,
    11051146                              pmConfig *config)
    11061147{
     
    11101151    PS_ASSERT_INT_NONNEGATIVE(numScans, false);
    11111152
    1112     return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_WEIGHT, config);
    1113 }
    1114 
    1115 bool pmReadoutReadWeight(pmReadout *readout, psFits *fits, int z, pmConfig *config)
     1153    return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_VARIANCE, config);
     1154}
     1155
     1156bool pmReadoutReadVariance(pmReadout *readout, psFits *fits, int z, pmConfig *config)
    11161157{
    11171158    PS_ASSERT_PTR_NON_NULL(readout, false);
    11181159    PS_ASSERT_FITS_NON_NULL(fits, false);
    11191160
    1120     return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_WEIGHT, config);
    1121 }
    1122 
    1123 bool pmCellReadWeight(pmCell *cell, psFits *fits, pmConfig *config)
     1161    return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_VARIANCE, config);
     1162}
     1163
     1164bool pmCellReadVariance(pmCell *cell, psFits *fits, pmConfig *config)
    11241165{
    11251166    PS_ASSERT_PTR_NON_NULL(cell, false);
    11261167    PS_ASSERT_FITS_NON_NULL(fits, false);
    11271168
    1128     return cellRead(cell, fits, config, FPA_READ_TYPE_WEIGHT);
    1129 }
    1130 
    1131 bool pmChipReadWeight(pmChip *chip, psFits *fits, pmConfig *config)
     1169    if (!cellRead(cell, fits, config, FPA_READ_TYPE_VARIANCE)) {
     1170        return false;
     1171    }
     1172    pmHDU *hdu = pmHDUFromCell(cell);   // Header data unit
     1173    if (hduCovariance(hdu, fits)) {
     1174        return pmCellReadCovariance(cell, fits);
     1175    }
     1176    return true;
     1177}
     1178
     1179bool pmChipReadVariance(pmChip *chip, psFits *fits, pmConfig *config)
    11321180{
    11331181    PS_ASSERT_PTR_NON_NULL(chip, false);
    11341182    PS_ASSERT_FITS_NON_NULL(fits, false);
    11351183
    1136     return chipRead(chip, fits, config, FPA_READ_TYPE_WEIGHT);
    1137 }
    1138 
    1139 bool pmFPAReadWeight(pmFPA *fpa, psFits *fits, pmConfig *config)
     1184    if (!chipRead(chip, fits, config, FPA_READ_TYPE_VARIANCE)) {
     1185        return false;
     1186    }
     1187    pmHDU *hdu = pmHDUFromChip(chip);   // Header data unit
     1188    if (hduCovariance(hdu, fits)) {
     1189        return pmChipReadCovariance(chip, fits);
     1190    }
     1191    return true;
     1192}
     1193
     1194bool pmFPAReadVariance(pmFPA *fpa, psFits *fits, pmConfig *config)
    11401195{
    11411196    PS_ASSERT_PTR_NON_NULL(fpa, false);
    11421197    PS_ASSERT_FITS_NON_NULL(fits, false);
    11431198
    1144     return fpaRead(fpa, fits, config, FPA_READ_TYPE_WEIGHT);
     1199    if (!fpaRead(fpa, fits, config, FPA_READ_TYPE_VARIANCE)) {
     1200        return false;
     1201    }
     1202    pmHDU *hdu = pmHDUFromFPA(fpa);     // Header data unit
     1203    if (hduCovariance(hdu, fits)) {
     1204        return pmFPAReadCovariance(fpa, fits);
     1205    }
     1206    return true;
    11451207}
    11461208
     
    12681330    return numRead;
    12691331}
     1332
     1333
     1334//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     1335// Reading covariance matrices
     1336//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     1337
     1338bool pmCellReadCovariance(pmCell *cell, psFits *fits)
     1339{
     1340    PS_ASSERT_PTR_NON_NULL(cell, false);
     1341    PS_ASSERT_FITS_NON_NULL(fits, false);
     1342
     1343    const char *chipName = psMetadataLookupStr(NULL, cell->parent->concepts, "CHIP.NAME"); // Name of chip
     1344    const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // Name of cell
     1345    psString extname = NULL;            // Extension name
     1346    psStringAppend(&extname, "COVAR_%s_%s", chipName, cellName);
     1347
     1348    if (!psFitsMoveExtName(fits, extname)) {
     1349        psError(PS_ERR_IO, false, "Unable to move to extension %s\n", extname);
     1350        psFree(extname);
     1351        return false;
     1352    }
     1353    psFree(extname);
     1354
     1355    psMetadata *header = psFitsReadHeader(NULL, fits); // The FITS header
     1356    if (!header) {
     1357        psError(PS_ERR_IO, false, "Unable to read header for extension %s\n", extname);
     1358        psFree(header);
     1359        return false;
     1360    }
     1361
     1362    bool mdok;                          // Status of MD lookup
     1363    int x0 = psMetadataLookupS32(&mdok, header, "COVARIANCE.CENTRE.X"); // Centre of matrix in x
     1364    if (!mdok) {
     1365        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to read covariance centre");
     1366        psFree(header);
     1367        return false;
     1368    }
     1369    int y0 = psMetadataLookupS32(&mdok, header, "COVARIANCE.CENTRE.Y"); // Centre of matrix in y
     1370    if (!mdok) {
     1371        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to read covariance centre");
     1372        psFree(header);
     1373        return false;
     1374    }
     1375    psFree(header);
     1376
     1377    psArray *images = psFitsReadImageCube(fits, psRegionSet(0,0,0,0)); // Covariance matrices
     1378    if (!images) {
     1379        psError(PS_ERR_IO, false, "Unable to read covariance matrices for chip %s, cell %s",
     1380                chipName, cellName);
     1381        return false;
     1382    }
     1383
     1384    psArray *readouts = cell->readouts; // Readouts of cell
     1385    if (images->n != readouts->n) {
     1386        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     1387                "Number of covariance matrices (%ld) doesn't match number of readouts (%ld)",
     1388                images->n, readouts->n);
     1389        psFree(images);
     1390        return false;
     1391    }
     1392
     1393    for (int i = 0; i < readouts->n; i++) {
     1394        pmReadout *ro = readouts->data[i]; // Readout of interest
     1395        psImage *image = images->data[i]; // Image of interest
     1396        if (ro->covariance) {
     1397            psWarning("Clobbering extant covariance matrix in chip %s, cell %s, readout %d",
     1398                      chipName, cellName, i);
     1399            psFree(ro->covariance);
     1400        }
     1401        ro->covariance = psKernelAllocFromImage(image, x0, y0);
     1402    }
     1403    psFree(images);
     1404
     1405    return true;
     1406}
     1407
     1408
     1409bool pmChipReadCovariance(pmChip *chip, psFits *fits)
     1410{
     1411    PS_ASSERT_PTR_NON_NULL(chip, false);
     1412    PS_ASSERT_FITS_NON_NULL(fits, false);
     1413
     1414    psArray *cells = chip->cells;       // Array of cells
     1415    for (int i = 0; i < cells->n; i++) {
     1416        pmCell *cell = cells->data[i];  // Cell of interest
     1417        if (!pmCellReadCovariance(cell, fits)) {
     1418            return false;
     1419        }
     1420    }
     1421
     1422    return true;
     1423}
     1424
     1425
     1426bool pmFPAReadCovariance(pmFPA *fpa, psFits *fits)
     1427{
     1428    PS_ASSERT_PTR_NON_NULL(fpa, false);
     1429    PS_ASSERT_FITS_NON_NULL(fits, false);
     1430
     1431    psArray *chips = fpa->chips;        // Array of chips
     1432    for (int i = 0; i < chips->n; i++) {
     1433        pmChip *chip = chips->data[i];  // Chip of interest
     1434        if (!pmChipReadCovariance(chip, fits)) {
     1435            return false;
     1436        }
     1437    }
     1438
     1439    return true;
     1440}
Note: See TracChangeset for help on using the changeset viewer.