Changeset 21363 for trunk/psModules/src/camera/pmFPAWrite.c
- Timestamp:
- Feb 5, 2009, 4:31:25 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/camera/pmFPAWrite.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/camera/pmFPAWrite.c
r21279 r21363 9 9 10 10 #include "pmConfig.h" 11 #include "pmConfigMask.h" 11 12 #include "pmHDU.h" 12 13 #include "pmFPA.h" … … 29 30 FPA_WRITE_TYPE_IMAGE, // Write image 30 31 FPA_WRITE_TYPE_MASK, // Write mask 31 FPA_WRITE_TYPE_ WEIGHT // Write weightmap32 FPA_WRITE_TYPE_VARIANCE // Write variance map 32 33 } fpaWriteType; 33 34 … … 46 47 case FPA_WRITE_TYPE_MASK: 47 48 return &hdu->masks; 48 case FPA_WRITE_TYPE_ WEIGHT:49 return &hdu-> weights;49 case FPA_WRITE_TYPE_VARIANCE: 50 return &hdu->variances; 50 51 default: 51 52 psAbort("Unknown write type: %x\n", type); … … 66 67 case FPA_WRITE_TYPE_MASK: 67 68 return pmHDUWriteMask(hdu, fits, config); 68 case FPA_WRITE_TYPE_ WEIGHT:69 return pmHDUWrite Weight(hdu, fits, config);69 case FPA_WRITE_TYPE_VARIANCE: 70 return pmHDUWriteVariance(hdu, fits, config); 70 71 default: 71 72 psAbort("Unknown write type: %x\n", type); … … 74 75 } 75 76 76 // Write a cell image/mask/weight 77 // Indicate whether a covariance matrix is defined 78 static 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) \ 85 static 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 100 SEARCH_COVARIANCES(cellSearchCovariances, pmCell, pmReadout, readouts, readoutSearchCovariances); 101 SEARCH_COVARIANCES(chipSearchCovariances, pmChip, pmCell, cells, cellSearchCovariances); 102 SEARCH_COVARIANCES(fpaSearchCovariances, pmFPA, pmChip, chips, chipSearchCovariances); 103 104 // Some type-specific additions to the header 105 static 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 77 143 static bool cellWrite(pmCell *cell, // Cell to write 78 144 psFits *fits, // FITS file to which to write … … 97 163 // generate the HDU, but only copies the structure. 98 164 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."); 100 166 return false; 101 167 } … … 106 172 107 173 if (writeBlank || writeImage) { 108 109 174 pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | 110 175 PM_CONCEPT_SOURCE_DEFAULTS | PM_CONCEPT_SOURCE_DATABASE; 111 176 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"); 113 182 return false; 114 183 } … … 123 192 } 124 193 125 // Write a chip image/mask/ weight194 // Write a chip image/mask/variance 126 195 static bool chipWrite(pmChip *chip, // Chip to write 127 196 psFits *fits, // FITS file to which to write … … 162 231 return false; 163 232 } 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 164 239 if (!appropriateWriteFunc(hdu, fits, config, type)) { 165 240 psError(PS_ERR_IO, false, "Unable to write HDU for chip.\n"); … … 186 261 187 262 188 // Write an FPA image/mask/ weight263 // Write an FPA image/mask/variance 189 264 static bool fpaWrite(pmFPA *fpa, // FPA to write 190 265 psFits *fits, // FITS file to which to write … … 223 298 if (!pmConceptsWriteFPA(fpa, source, true, config)) { 224 299 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"); 225 304 return false; 226 305 } … … 433 512 434 513 435 bool pmCellWrite Weight(pmCell *cell, psFits *fits, pmConfig *config, bool blank)514 bool pmCellWriteVariance(pmCell *cell, psFits *fits, pmConfig *config, bool blank) 436 515 { 437 516 PS_ASSERT_PTR_NON_NULL(cell, false); 438 517 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 527 bool pmChipWriteVariance(pmChip *chip, psFits *fits, pmConfig *config, bool blank, bool recurse) 443 528 { 444 529 PS_ASSERT_PTR_NON_NULL(chip, false); 445 530 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 540 bool pmFPAWriteVariance(pmFPA *fpa, psFits *fits, pmConfig *config, bool blank, bool recurse) 450 541 { 451 542 PS_ASSERT_PTR_NON_NULL(fpa, false); 452 543 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; 454 551 } 455 552 … … 526 623 return numWrite; 527 624 } 625 626 bool 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 711 bool 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 728 bool 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.
