Changeset 21363 for trunk/psModules/src/camera/pmFPARead.c
- Timestamp:
- Feb 5, 2009, 4:31:25 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/camera/pmFPARead.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/camera/pmFPARead.c
r21183 r21363 9 9 #include <pslib.h> 10 10 11 #include "pmConfig.h" 12 #include "pmConfigMask.h" 11 13 #include "pmHDU.h" 12 14 #include "pmFPA.h" … … 28 30 FPA_READ_TYPE_IMAGE, // Read image 29 31 FPA_READ_TYPE_MASK, // Read mask 30 FPA_READ_TYPE_ WEIGHT, // Read weightmap32 FPA_READ_TYPE_VARIANCE, // Read variance map 31 33 FPA_READ_TYPE_HEADER // Read header 32 34 } fpaReadType; … … 54 56 case FPA_READ_TYPE_MASK: 55 57 return readout->thisMaskScan; 56 case FPA_READ_TYPE_ WEIGHT:57 return readout->this WeightScan;58 case FPA_READ_TYPE_VARIANCE: 59 return readout->thisVarianceScan; 58 60 default: 59 61 psAbort("Unknown read type: %x\n", type); … … 74 76 readout->thisMaskScan = thisScan; 75 77 return readout->lastMaskScan; 76 case FPA_READ_TYPE_ WEIGHT:77 readout->this WeightScan = thisScan;78 return readout->last WeightScan;78 case FPA_READ_TYPE_VARIANCE: 79 readout->thisVarianceScan = thisScan; 80 return readout->lastVarianceScan; 79 81 default: 80 82 psAbort("Unknown read type: %x\n", type); … … 93 95 case FPA_READ_TYPE_MASK: 94 96 return readout->lastMaskScan; 95 case FPA_READ_TYPE_ WEIGHT:96 return readout->last WeightScan;97 case FPA_READ_TYPE_VARIANCE: 98 return readout->lastVarianceScan; 97 99 default: 98 100 psAbort("Unknown read type: %x\n", type); … … 113 115 readout->lastMaskScan = lastScan; 114 116 return readout->lastMaskScan; 115 case FPA_READ_TYPE_ WEIGHT:116 readout->last WeightScan = lastScan;117 return readout->last WeightScan;117 case FPA_READ_TYPE_VARIANCE: 118 readout->lastVarianceScan = lastScan; 119 return readout->lastVarianceScan; 118 120 default: 119 121 psAbort("Unknown read type: %x\n", type); … … 132 134 case FPA_READ_TYPE_MASK: 133 135 return &readout->mask; 134 case FPA_READ_TYPE_ WEIGHT:135 return &readout-> weight;136 case FPA_READ_TYPE_VARIANCE: 137 return &readout->variance; 136 138 default: 137 139 psAbort("Unknown read type: %x\n", type); … … 193 195 194 196 return naxis3; 197 } 198 199 // Determine whether a FITS file contains covariance matrices 200 static 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; 195 218 } 196 219 … … 350 373 *target = psImageSubset(image, region); 351 374 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) 353 376 if (type == FPA_READ_TYPE_IMAGE) { 354 377 if (readout->bias->n != 0) { … … 515 538 } 516 539 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); 524 565 } 525 566 … … 582 623 *image = readoutReadComponent(*image, fits, trimsec, readdir, thisScan, lastScan, z, bad, pixelTypes[type]); 583 624 584 // Read overscans only for "image" type --- weights and masks shouldn't record overscans625 // Read overscans only for "image" type --- variances and masks shouldn't record overscans 585 626 if (type == FPA_READ_TYPE_IMAGE) { 586 627 // Blow away existing data … … 608 649 } 609 650 610 // Read into an cell; this is the engine for pmCellRead, pmCellReadMask, pmCellRead Weight651 // Read into an cell; this is the engine for pmCellRead, pmCellReadMask, pmCellReadVariance 611 652 // Does most of the work for the reading --- reads the HDU, and portions the HDU into readouts. 612 653 static bool cellRead(pmCell *cell, // Cell into which to read … … 640 681 dataPointer = hdu->masks; 641 682 break; 642 case FPA_READ_TYPE_ WEIGHT:643 hduReadFunc = pmHDURead Weight;644 dataPointer = hdu-> weights;683 case FPA_READ_TYPE_VARIANCE: 684 hduReadFunc = pmHDUReadVariance; 685 dataPointer = hdu->variances; 645 686 break; 646 687 default: … … 680 721 imageArray = hdu->masks; 681 722 break; 682 case FPA_READ_TYPE_ WEIGHT:683 imageArray = hdu-> weights;723 case FPA_READ_TYPE_VARIANCE: 724 imageArray = hdu->variances; 684 725 break; 685 726 default: … … 729 770 730 771 731 // Read into an chip; this is the engine for pmChipRead, pmChipReadMask, pmChipRead Weight772 // Read into an chip; this is the engine for pmChipRead, pmChipReadMask, pmChipReadVariance 732 773 // Iterates over component cells, reading each 733 774 static bool chipRead(pmChip *chip, // Chip into which to read … … 760 801 761 802 762 // Read into an FPA; this is the engine for pmFPARead, pmFPAReadMask, pmFPARead Weight803 // Read into an FPA; this is the engine for pmFPARead, pmFPAReadMask, pmFPAReadVariance 763 804 // Iterates over component chips, reading each 764 805 static bool fpaRead(pmFPA *fpa, // FPA into which to read … … 852 893 float bad = psMetadataLookupF32(&mdok, cell->concepts, "CELL.BAD"); // Bad level 853 894 if (!mdok) { 854 ps LogMsg(__func__, PS_LOG_WARN,"CELL.BAD is not set --- assuming zero.\n");895 psWarning("CELL.BAD is not set --- assuming zero.\n"); 855 896 bad = 0.0; 856 897 } … … 1091 1132 1092 1133 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1093 // Reading the weightmap1094 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1095 1096 bool pmReadoutMore Weight(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config)1134 // Reading the variance map 1135 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1136 1137 bool pmReadoutMoreVariance(pmReadout *readout, psFits *fits, int z, int numScans, pmConfig *config) 1097 1138 { 1098 1139 PS_ASSERT_PTR_NON_NULL(readout, false); 1099 1140 PS_ASSERT_FITS_NON_NULL(fits, false); 1100 1141 1101 return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_ WEIGHT, config);1102 } 1103 1104 bool pmReadoutReadChunk Weight(pmReadout *readout, psFits *fits, int z, int numScans, int overlap,1142 return readoutMore(readout, fits, z, numScans, FPA_READ_TYPE_VARIANCE, config); 1143 } 1144 1145 bool pmReadoutReadChunkVariance(pmReadout *readout, psFits *fits, int z, int numScans, int overlap, 1105 1146 pmConfig *config) 1106 1147 { … … 1110 1151 PS_ASSERT_INT_NONNEGATIVE(numScans, false); 1111 1152 1112 return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_ WEIGHT, config);1113 } 1114 1115 bool pmReadoutRead Weight(pmReadout *readout, psFits *fits, int z, pmConfig *config)1153 return readoutReadChunk(readout, fits, z, numScans, overlap, FPA_READ_TYPE_VARIANCE, config); 1154 } 1155 1156 bool pmReadoutReadVariance(pmReadout *readout, psFits *fits, int z, pmConfig *config) 1116 1157 { 1117 1158 PS_ASSERT_PTR_NON_NULL(readout, false); 1118 1159 PS_ASSERT_FITS_NON_NULL(fits, false); 1119 1160 1120 return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_ WEIGHT, config);1121 } 1122 1123 bool pmCellRead Weight(pmCell *cell, psFits *fits, pmConfig *config)1161 return readoutReadChunk(readout, fits, z, 0, 0, FPA_READ_TYPE_VARIANCE, config); 1162 } 1163 1164 bool pmCellReadVariance(pmCell *cell, psFits *fits, pmConfig *config) 1124 1165 { 1125 1166 PS_ASSERT_PTR_NON_NULL(cell, false); 1126 1167 PS_ASSERT_FITS_NON_NULL(fits, false); 1127 1168 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 1179 bool pmChipReadVariance(pmChip *chip, psFits *fits, pmConfig *config) 1132 1180 { 1133 1181 PS_ASSERT_PTR_NON_NULL(chip, false); 1134 1182 PS_ASSERT_FITS_NON_NULL(fits, false); 1135 1183 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 1194 bool pmFPAReadVariance(pmFPA *fpa, psFits *fits, pmConfig *config) 1140 1195 { 1141 1196 PS_ASSERT_PTR_NON_NULL(fpa, false); 1142 1197 PS_ASSERT_FITS_NON_NULL(fits, false); 1143 1198 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; 1145 1207 } 1146 1208 … … 1268 1330 return numRead; 1269 1331 } 1332 1333 1334 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1335 // Reading covariance matrices 1336 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1337 1338 bool 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 1409 bool 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 1426 bool 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.
