Changeset 5564
- Timestamp:
- Nov 21, 2005, 5:00:14 PM (20 years ago)
- Location:
- trunk/archive/scripts/src/phase2
- Files:
-
- 14 edited
-
Makefile (modified) (1 diff)
-
ipprc.config (modified) (1 diff)
-
megacam_raw.config (modified) (1 diff)
-
papPhase2.c (modified) (4 diffs)
-
phase2.config (modified) (1 diff)
-
pmFPA.c (modified) (12 diffs)
-
pmFPA.h (modified) (4 diffs)
-
pmFPAConceptsGet.c (modified) (12 diffs)
-
pmFPAConceptsSet.c (modified) (10 diffs)
-
pmFPAConstruct.c (modified) (8 diffs)
-
pmFPARead.c (modified) (10 diffs)
-
pmFPARead.h (modified) (1 diff)
-
pmFPAWrite.c (modified) (3 diffs)
-
pmFPAWrite.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/scripts/src/phase2/Makefile
r5104 r5564 3 3 4 4 #DEVFLAGS += -DPRODUCTION # Produce binary as for production (don't use old APIs) 5 DEVFLAGS += -DTESTING # Testing version 5 DEVFLAGS += -DTESTING # Testing version 6 #DEVFLAGS += -DPS_NO_TRACE # No trace messages 6 7 7 CFLAGS += -O0 -g -std=c99 -Werror -I$(JHBUILD)/include/ -I$(JHBUILD)/include/libxml2/ -D PS_NO_TRACE -D_GNU_SOURCE $(DEVFLAGS)8 CFLAGS += -O0 -g -std=c99 -Werror -I$(JHBUILD)/include/ -I$(JHBUILD)/include/libxml2/ -D_GNU_SOURCE $(DEVFLAGS) 8 9 PSLIB += -L$(JHBUILD)/lib/ -lpslib -lgsl -lgslcblas -lfftw3f -lsla -lcfitsio -lm -L$(JHBUILD)/lib/libxml2/ -L$(JHBUILD)/lib/mysql/ -lxml2 -lmysqlclient 9 10 PSMODULE += -L$(JHBUILD)/lib/ -lpsmodule -
trunk/archive/scripts/src/phase2/ipprc.config
r5467 r5564 22 22 TRACE METADATA # Trace levels 23 23 pmConfigRead S32 10 24 pmFPARead S32 1025 24 pap S32 10 25 pmFPAWriteMask S32 10 26 pmFPAWriteWeight S32 10 27 p_pmFPATranslateName S32 10 26 28 END -
trunk/archive/scripts/src/phase2/megacam_raw.config
r5105 r5564 159 159 PHASE2 STR phase2.config # Phase 2 recipe details 160 160 END 161 162 163 # How to get the supplementary stuff: mask and weight 164 SUPPLEMENTARY METADATA 165 MASK.SOURCE STR FILE # Source type for mask: EXT | FILE 166 MASK.NAME STR %a_mask.fits # Name for mask extension or filename 167 WEIGHT.SOURCE STR EXT # Source type for weight: EXT | FILE 168 WEIGHT.NAME STR %f-weight # Name for weight extension or filename 169 END -
trunk/archive/scripts/src/phase2/papPhase2.c
r5467 r5564 29 29 // * Write calibrated image 30 30 // * Write source catalogue, astrometry, etc. 31 31 32 32 // Currently neglecting different input types (F32, S32, U16, etc) 33 33 … … 302 302 #else 303 303 psFree(inputFile); 304 #endif 305 306 307 #if 1 308 { 309 // Generate mask and weight frame 310 p_pmHDU *hdu = NULL; 311 if (input->hdu) { 312 hdu = input->hdu; 313 } 314 psArray *chips = input->chips; 315 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 316 pmChip *chip = chips->data[chipNum]; 317 if (chip->valid) { 318 if (chip->hdu) { 319 hdu = chip->hdu; 320 } 321 psArray *cells = chip->cells; 322 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 323 pmCell *cell = cells->data[cellNum]; 324 if (cell->valid) { 325 if (cell->hdu) { 326 hdu = cell->hdu; 327 } 328 329 hdu->masks = psArrayAlloc(hdu->images->n); 330 hdu->weights = psArrayAlloc(hdu->images->n); 331 psArray *readouts = cell->readouts; 332 for (int readNum = 0; readNum < hdu->images->n; readNum++) { 333 psImage *image = hdu->images->data[readNum]; 334 psImage *mask = psImageAlloc(image->numCols, image->numRows, PS_TYPE_U8); 335 psImage *weight = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32); 336 pmReadout *readout = readouts->data[readNum]; 337 for (int j = 0; j < image->numRows; j++) { 338 for (int i = 0; i < image->numCols; i++) { 339 mask->data.U8[j][i] = j + i; 340 weight->data.F32[j][i] = sqrtf(image->data.F32[j][i]); 341 } 342 } 343 hdu->masks->data[readNum] = mask; 344 hdu->weights->data[readNum] = weight; 345 readout->mask = psMemIncrRefCounter(mask); 346 readout->weight = psMemIncrRefCounter(weight); 347 } 348 } 349 } 350 } 351 } 352 } 304 353 #endif 305 354 … … 741 790 inputReadout->image->numRows, 742 791 PS_TYPE_U8); 743 pmReadout *dummyMask = pmReadoutAlloc(NULL, dummyImage, NULL, 0, 0, 1, 1 , 1, 1);792 pmReadout *dummyMask = pmReadoutAlloc(NULL, dummyImage, NULL, 0, 0, 1, 1); 744 793 (void)pmFlatField(inputReadout, dummyMask, flatReadout); 745 794 psFree(dummyMask); … … 771 820 // Write the output 772 821 pmFPAWrite(outputFile, input, database); 773 // pmFPAWriteMask(outputMaskFile, input); 822 pmFPAWriteMask(input, outputFile); 823 pmFPAWriteWeight(input, outputFile); 824 774 825 #ifdef PRODUCTION 775 826 psFitsClose(outputFile); -
trunk/archive/scripts/src/phase2/phase2.config
r5467 r5564 3 3 # List of tasks to perform 4 4 MASK BOOL FALSE # Mask bad pixels 5 NONLIN BOOL TRUE # Non-linearity correction5 NONLIN BOOL FALSE # Non-linearity correction 6 6 BIAS BOOL FALSE # Bias subtraction 7 7 DARK BOOL FALSE # Dark subtraction -
trunk/archive/scripts/src/phase2/pmFPA.c
r5462 r5564 40 40 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 41 41 42 p mPixelData *pmPixelDataAlloc(const char *extname)43 { 44 p mPixelData *pd = psAlloc(sizeof(pmPixelData));45 psMemSetDeallocator(pd, (psFreeFunc)p_pm PixelDataFree);42 p_pmHDU *p_pmHDUAlloc(const char *extname) 43 { 44 p_pmHDU *pd = psAlloc(sizeof(p_pmHDU)); 45 psMemSetDeallocator(pd, (psFreeFunc)p_pmHDUFree); 46 46 47 47 pd->extname = extname; … … 54 54 } 55 55 56 void p_pm PixelDataFree(pmPixelData *pd)57 { 58 psFree( pd->header);59 psFree( pd->images);60 psFree( pd->masks);61 psFree( pd->weights);56 void p_pmHDUFree(p_pmHDU *hdu) 57 { 58 psFree(hdu->header); 59 psFree(hdu->images); 60 psFree(hdu->masks); 61 psFree(hdu->weights); 62 62 } 63 63 … … 77 77 fpa->chips = psArrayAlloc(0); 78 78 79 fpa-> data= NULL;79 fpa->hdu = NULL; 80 80 81 81 return fpa; … … 92 92 psFree(fpa->chips); 93 93 94 psFree(fpa-> data);94 psFree(fpa->hdu); 95 95 } 96 96 … … 119 119 chip->valid = true; 120 120 121 chip-> data= NULL;121 chip->hdu = NULL; 122 122 123 123 psMetadataAddStr(chip->concepts, PS_LIST_HEAD, "CHIP.NAME", 0, "Chip name added at pmChipAlloc", name); … … 134 134 psFree(chip->cells); 135 135 136 psFree(chip-> data);136 psFree(chip->hdu); 137 137 138 138 // We don't free the parent member, since that would generate a circular call. We don't increment the … … 156 156 157 157 cell->toChip = NULL; 158 cell->fromChip = NULL;159 158 cell->toFPA = NULL; 160 cell->toTP = NULL;161 159 cell->toSky = NULL; 162 160 … … 171 169 cell->valid = true; 172 170 173 cell-> data= NULL;171 cell->hdu = NULL; 174 172 175 173 return cell; … … 179 177 { 180 178 psFree(cell->toChip); 181 psFree(cell->fromChip);182 179 psFree(cell->toFPA); 183 psFree(cell->toTP);184 180 psFree(cell->toSky); 185 181 … … 188 184 psFree(cell->readouts); 189 185 190 psFree(cell-> data);186 psFree(cell->hdu); 191 187 192 188 // We don't free the parent member, since that would generate a circular call. We don't increment the … … 197 193 psImage *image, // The pixels 198 194 psImage *mask,// The mask pixels 199 int col0, int row0, int col Parity, int rowParity, int colBin, int rowBin // Data195 int col0, int row0, int colBin, int rowBin // Data 200 196 ) 201 197 { … … 215 211 *(int*)&readout->col0 = col0; 216 212 *(int*)&readout->row0 = row0; 217 *(int*)&readout->colParity = colParity;218 *(int*)&readout->rowParity = rowParity;219 213 *(int*)&readout->colBins = colBin; 220 214 *(int*)&readout->rowBins = rowBin; -
trunk/archive/scripts/src/phase2/pmFPA.h
r5462 r5564 15 15 psArray *masks; // The mask data, if it corresponds to this level 16 16 psArray *weights; // The weight data, if it corresponds to this level 17 } p mPixelData;17 } p_pmHDU; 18 18 19 19 typedef struct { 20 20 // Astrometric transformations 21 psPlaneDistort* fromTangentPlane; // Transformation from tangent plane to focal plane22 psPlaneDistort* toTangentPlane; // Transformation from focal plane to tangent plane23 psProjection *projection; // Projection from tangent plane to sky21 psPlaneDistort* fromTangentPlane; // Transformation from tangent plane to focal plane 22 psPlaneDistort* toTangentPlane; // Transformation from focal plane to tangent plane 23 psProjection *projection; // Projection from tangent plane to sky 24 24 // Information 25 psMetadata *camera; // Camera configuration 26 psMetadata *concepts; // Values for concepts 27 psMetadata *phu; // Primary Header 28 psArray *chips; // The chips 29 pmPixelData *data; // FITS data 25 psMetadata *concepts; // Cache for PS concepts 26 psMetadata *analysis; // FPA-level analysis metadata 27 const psMetadata *camera; // Camera configuration 28 psArray *chips; // The chips 29 p_pmHDU *hdu; // FITS data 30 psMetadata *phu; // Primary Header 30 31 } pmFPA; 31 32 32 33 typedef struct { 33 34 // Offset specifying position on focal plane 34 const int col0; // Offset from the left of FPA35 const int row0; // Offset from the bottom of FPA35 int col0; // Offset from the left of FPA. 36 int row0; // Offset from the bottom of FPA. 36 37 // Astrometric transformations 37 psPlaneTransform* toFPA; // Transformation from chip to FPA coordinates38 psPlaneTransform* fromFPA; // Transformation from FPA to chip coordinates38 psPlaneTransform* toFPA; // Transformation from chip to FPA coordinates 39 psPlaneTransform* fromFPA; // Transformation from FPA to chip coordinates 39 40 // Information 40 psMetadata *concepts; // Values for concepts 41 psArray *cells; // The cells (referred to by name) 42 pmFPA *parent; // Parent FPA 43 bool valid; // Valid for reading in? 44 pmPixelData *data; // FITS data 41 psMetadata *concepts; // Cache for PS concepts 42 psMetadata *analysis; // Chip-level analysis metadata 43 psArray *cells; // The cells (referred to by name) 44 pmFPA *parent; // Parent FPA 45 bool valid; // Do we bother about reading and working with this chip? 46 p_pmHDU *hdu; // FITS data 45 47 } pmChip; 46 48 47 49 typedef struct { 48 50 // Offset specifying position on chip 49 const int col0; // Offset from the left of chip50 const int row0; // Offset from the bottom of chip51 int col0; // Offset from the left of chip. 52 int row0; // Offset from the bottom of chip. 51 53 // Astrometric transformations 52 psPlaneTransform* toChip; // Transformations from cell to chip coordinates 53 psPlaneTransform* fromChip; // Transformations from cell to chip coordinates 54 psPlaneTransform* toFPA; // Transformations from cell to FPA coordinates 55 psPlaneTransform* toTP; // Transformations from cell to FPA coordinates 56 psPlaneTransform* toSky; // Transformations from cell to tangent plane coordinates 54 psPlaneTransform* toChip; // Transformations from cell to chip coordinates 55 psPlaneTransform* toFPA; // Transformations from cell to FPA coordinates 56 psPlaneTransform* toSky; // Transformations from cell to sky coordinates 57 57 // Information 58 psMetadata *concepts; // Values for concepts 59 psMetadata *camera; // Camera information 60 psArray *readouts; // The readouts (referred to by number) 61 pmChip *parent; // Parent chip 62 bool valid; // Valid for operating on? 63 pmPixelData *data; // FITS data 58 psMetadata *concepts; // Cache for PS concepts 59 psMetadata *camera; // Camera information 60 psMetadata *analysis; // Cell-level analysis metadata 61 psArray *readouts; // The readouts (referred to by number) 62 pmChip *parent; // Parent chip 63 bool valid; // Do we bother about reading and working with this cell? 64 p_pmHDU *hdu; // FITS data 64 65 } pmCell; 65 66 67 typedef struct { 68 // Position on the cell 69 int col0; // Offset from the left of cell. 70 int row0; // Offset from the bottom of cell. 71 int colBins; // Amount of binning in x-dimension and parity (from sign) 72 int rowBins; // Amount of binning in y-dimension and parity (from sign) 73 // Information 74 psImage *image; // Imaging area of readout 75 psImage *mask; // Mask for image 76 psImage *weight; // Weight for image 77 psList *bias; // List of bias section (sub-)images 78 psMetadata *analysis; // Readout-level analysis metadata 79 pmCell *parent; // Parent cell 80 } pmReadout; 66 81 82 #if 0 67 83 typedef struct { 68 84 // Details for position on the cell … … 80 96 psImage *weight; // Weight image 81 97 } pmReadout; 82 98 #endif 83 99 84 100 psList *pmReadoutGetBias(pmReadout *readout // Readout for which to get the bias sections … … 87 103 88 104 // Allocators and deallocators 89 p mPixelData *pmPixelDataAlloc(const char *extname);90 void p_pm PixelDataFree(pmPixelData *pd);105 p_pmHDU *p_pmHDUAlloc(const char *extname); 106 void p_pmHDUFree(p_pmHDU *hdu); 91 107 pmFPA *pmFPAAlloc(const psMetadata *camera // Camera configuration 92 108 ); … … 107 123 psImage *image, // The pixels 108 124 psImage *mask,// The mask pixels 109 int col0, int row0, int col Parity, int rowParity, int colBin, int rowBin // Data125 int col0, int row0, int colBin, int rowBin // Data 110 126 ); 111 127 void p_pmReadoutFree(pmReadout *readout); -
trunk/archive/scripts/src/phase2/pmFPAConceptsGet.c
r5462 r5564 46 46 if (mdStatus && strlen(keyword) > 0) { 47 47 // We have a FITS header to look up --- search each level 48 if (cell && cell-> data) {49 psMetadataItem *cellItem = psMetadataLookup(cell-> data->header, keyword);48 if (cell && cell->hdu) { 49 psMetadataItem *cellItem = psMetadataLookup(cell->hdu->header, keyword); 50 50 if (cellItem) { 51 51 // XXX: Need to clean up before returning … … 54 54 } 55 55 56 if (chip && chip-> data) {57 psMetadataItem *chipItem = psMetadataLookup(chip-> data->header, keyword);56 if (chip && chip->hdu) { 57 psMetadataItem *chipItem = psMetadataLookup(chip->hdu->header, keyword); 58 58 if (chipItem) { 59 59 // XXX: Need to clean up before returning … … 62 62 } 63 63 64 if (fpa-> data) {65 psMetadataItem *fpaItem = psMetadataLookup(fpa-> data->header, keyword);64 if (fpa->hdu) { 65 psMetadataItem *fpaItem = psMetadataLookup(fpa->hdu->header, keyword); 66 66 if (fpaItem) { 67 67 // XXX: Need to clean up before returning … … 371 371 switch (item->type) { 372 372 case PS_DATA_STRING: 373 value = item->data.V; 373 value = psMemIncrRefCounter(item->data.V); 374 break; 375 case PS_DATA_F32: 376 psStringAppend(&value, "%f", item->data.F32); 377 break; 378 case PS_DATA_S32: 379 psStringAppend(&value, "%d", item->data.S32); 374 380 break; 375 381 default: … … 384 390 385 391 psMetadataAdd(concepts, PS_LIST_TAIL, name, PS_DATA_STRING, comment, value); 392 psFree(value); 386 393 } 387 394 … … 400 407 } 401 408 402 // FPA.NAME --- added by pmFPAConstruct 409 // FPA.NAME 410 p_pmFPAConceptGetString(fpa, NULL, NULL, db, fpa->concepts, "FPA.NAME", "Name of FPA"); 403 411 404 412 // FPA.AIRMASS … … 450 458 451 459 // How to interpret the RA 452 psMetadata *camera = fpa->camera; // Camera configuration data460 const psMetadata *camera = fpa->camera; // Camera configuration data 453 461 bool mdok = true; // Status of MD lookup 454 462 psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS"); … … 519 527 520 528 // How to interpret the DEC 521 psMetadata *camera = fpa->camera; // Camera configuration data529 const psMetadata *camera = fpa->camera; // Camera configuration data 522 530 bool mdok = true; // Status of MD lookup 523 531 psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS"); … … 653 661 } else if (strcasecmp(source, "HEADER") == 0) { 654 662 psMetadata *header = NULL; // The FITS header 655 if (cell-> data) {656 header = cell-> data->header;657 } else if (chip-> data) {658 header = chip-> data->header;659 } else if (fpa-> data) {660 header = fpa-> data->header;663 if (cell->hdu) { 664 header = cell->hdu->header; 665 } else if (chip->hdu) { 666 header = chip->hdu->header; 667 } else if (fpa->hdu) { 668 header = fpa->hdu->header; 661 669 } 662 670 if (! header) { … … 718 726 } else if (strcasecmp(source, "HEADER") == 0) { 719 727 psMetadata *header = NULL; // The FITS header 720 if (cell-> data) {721 header = cell-> data->header;722 } else if (chip-> data) {723 header = chip-> data->header;724 } else if (fpa-> data) {725 header = fpa-> data->header;728 if (cell->hdu) { 729 header = cell->hdu->header; 730 } else if (chip->hdu) { 731 header = chip->hdu->header; 732 } else if (fpa->hdu) { 733 header = fpa->hdu->header; 726 734 } 727 735 if (! header) { … … 832 840 } else { 833 841 // Get format 834 psMetadata *camera = fpa->camera; // The camera configuration data842 const psMetadata *camera = fpa->camera; // The camera configuration data 835 843 bool mdok = true; // Status of MD lookup 836 844 psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS"); … … 848 856 // timeString contains headers for the date and time 849 857 psMetadata *header = NULL; // The FITS header 850 if (cell-> data) {851 header = cell-> data->header;852 } else if (chip-> data) {853 header = chip-> data->header;854 } else if (fpa-> data) {855 header = fpa-> data->header;858 if (cell->hdu) { 859 header = cell->hdu->header; 860 } else if (chip->hdu) { 861 header = chip->hdu->header; 862 } else if (fpa->hdu) { 863 header = fpa->hdu->header; 856 864 } 857 865 if (! header) { -
trunk/archive/scripts/src/phase2/pmFPAConceptsSet.c
r5462 r5564 130 130 131 131 // We have a FITS header to look up --- search each level 132 if (cell && cell-> data) {133 psMetadataItem *cellItem = psMetadataLookup(cell-> data->header, keyword);132 if (cell && cell->hdu) { 133 psMetadataItem *cellItem = psMetadataLookup(cell->hdu->header, keyword); 134 134 if (cellItem) { 135 135 // XXX: Need to clean up before returning 136 psMetadataAddItem(cell-> data->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);136 psMetadataAddItem(cell->hdu->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE); 137 137 status = true; 138 138 } 139 } else if (chip && chip-> data) {140 psMetadataItem *chipItem = psMetadataLookup(chip-> data->header, keyword);139 } else if (chip && chip->hdu) { 140 psMetadataItem *chipItem = psMetadataLookup(chip->hdu->header, keyword); 141 141 if (chipItem) { 142 142 // XXX: Need to clean up before returning 143 psMetadataAddItem(chip-> data->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);143 psMetadataAddItem(chip->hdu->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE); 144 144 status = true; 145 145 } 146 } else if (fpa-> data) {147 psMetadataItem *fpaItem = psMetadataLookup(fpa-> data->header, keyword);146 } else if (fpa->hdu) { 147 psMetadataItem *fpaItem = psMetadataLookup(fpa->hdu->header, keyword); 148 148 if (fpaItem) { 149 149 // XXX: Need to clean up before returning 150 psMetadataAddItem(fpa-> data->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE);150 psMetadataAddItem(fpa->hdu->header, headerItem, PS_LIST_TAIL, PS_META_REPLACE); 151 151 status = true; 152 152 } … … 376 376 ) 377 377 { 378 // FPA.NAME --- don't need to worry about this 378 // FPA.NAME 379 setConcept(fpa, NULL, NULL, db, fpa->concepts, "FPA.NAME"); 379 380 380 381 // FPA.AIRMASS … … 397 398 398 399 // How to interpret the RA 399 psMetadata *camera = fpa->camera; // Camera configuration data400 const psMetadata *camera = fpa->camera; // Camera configuration data 400 401 bool mdok = true; // Status of MD lookup 401 402 psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS"); … … 444 445 445 446 // How to interpret the DEC 446 psMetadata *camera = fpa->camera; // Camera configuration data447 const psMetadata *camera = fpa->camera; // Camera configuration data 447 448 bool mdok = true; // Status of MD lookup 448 449 psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS"); … … 571 572 psString keyword = psMetadataLookupString(NULL, cell->camera, "CELL.TRIMSEC"); 572 573 psMetadata *header = NULL; // The FITS header 573 if (cell-> data) {574 header = cell-> data->header;575 } else if (chip-> data) {576 header = chip-> data->header;577 } else if (fpa-> data) {578 header = fpa-> data->header;574 if (cell->hdu) { 575 header = cell->hdu->header; 576 } else if (chip->hdu) { 577 header = chip->hdu->header; 578 } else if (fpa->hdu) { 579 header = fpa->hdu->header; 579 580 } 580 581 if (! header) { … … 651 652 } else { 652 653 psMetadata *header = NULL; // The FITS header 653 if (cell-> data) {654 header = cell-> data->header;655 } else if (chip-> data) {656 header = chip-> data->header;657 } else if (fpa-> data) {658 header = fpa-> data->header;654 if (cell->hdu) { 655 header = cell->hdu->header; 656 } else if (chip->hdu) { 657 header = chip->hdu->header; 658 } else if (fpa->hdu) { 659 header = fpa->hdu->header; 659 660 } 660 661 if (! header) { … … 689 690 690 691 // If these are specified by the header, we need to check to see if it's the same header keyword 691 psMetadata *camera = fpa->camera;692 const psMetadata *camera = fpa->camera; 692 693 psMetadata *translation = psMetadataLookupMD(NULL, camera, "TRANSLATION"); 693 694 psString xKeyword = psMetadataLookupString(NULL, translation, "CELL.XBIN"); … … 707 708 binString); 708 709 psMetadata *header = NULL; // The FITS header 709 if (cell-> data) {710 header = cell-> data->header;711 } else if (chip-> data) {712 header = chip-> data->header;713 } else if (fpa-> data) {714 header = fpa-> data->header;710 if (cell->hdu) { 711 header = cell->hdu->header; 712 } else if (chip->hdu) { 713 header = chip->hdu->header; 714 } else if (fpa->hdu) { 715 header = fpa->hdu->header; 715 716 } 716 717 if (! header) { … … 784 785 // We're working with two separate headers 785 786 psMetadata *header = NULL; // The FITS header 786 if (cell-> data) {787 header = cell-> data->header;788 } else if (chip-> data) {789 header = chip-> data->header;790 } else if (fpa-> data) {791 header = fpa-> data->header;787 if (cell->hdu) { 788 header = cell->hdu->header; 789 } else if (chip->hdu) { 790 header = chip->hdu->header; 791 } else if (fpa->hdu) { 792 header = fpa->hdu->header; 792 793 } 793 794 if (! header) { … … 795 796 } else { 796 797 // Get the headers 797 psMetadata *camera = fpa->camera;798 const psMetadata *camera = fpa->camera; 798 799 psMetadata *translation = psMetadataLookupMD(NULL, camera, "TRANSLATION"); 799 800 psString keywords = psMetadataLookupString(NULL, translation, "CELL.TIME"); -
trunk/archive/scripts/src/phase2/pmFPAConstruct.c
r5462 r5564 88 88 psString extName = contentItem->name; // The name of the extension 89 89 pmChip *chip = pmChipAlloc(fpa, extName); // The chip 90 chip-> data = pmPixelDataAlloc(extName); // Prepare chip to receive FITS data90 chip->hdu = p_pmHDUAlloc(extName); // Prepare chip to receive FITS data 91 91 if (contentItem->type != PS_DATA_STRING) { 92 92 psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n", … … 146 146 pmCell *cell = pmCellAlloc(chip, cellData, extName); // The cell 147 147 // psFree(cellData); 148 cell-> data = pmPixelDataAlloc(extName); // Prepare cell to receive FITS data148 cell->hdu = p_pmHDUAlloc(extName); // Prepare cell to receive FITS data 149 149 150 150 psFree(chip); … … 157 157 } else if (strcasecmp(extType, "NONE") == 0) { 158 158 // No extensions; Content contains metadata, each entry is a chip with its component cells 159 fpa-> data = pmPixelDataAlloc("PHU"); // Prepare FPA to receive FITS data159 fpa->hdu = p_pmHDUAlloc("PHU"); // Prepare FPA to receive FITS data 160 160 while (contentItem = psMetadataGetAndIncrement(contentsIter)) { 161 161 psString chipName = contentItem->name; // The name of the chip … … 196 196 if (strcasecmp(extType, "NONE") == 0) { 197 197 // There are no extensions --- only the PHU 198 chip-> data = pmPixelDataAlloc("PHU");198 chip->hdu = p_pmHDUAlloc("PHU"); 199 199 200 200 const char *contents = psMetadataLookupString(&mdStatus, camera, "CONTENTS"); … … 247 247 pmCell *cell = pmCellAlloc(chip, cellData, extName); // The cell 248 248 // psFree(cellData); 249 cell-> data = pmPixelDataAlloc(extName); // Prepare cell to receive FITS data249 cell->hdu = p_pmHDUAlloc(extName); // Prepare cell to receive FITS data 250 250 } // Iterating through contents 251 251 psFree(contentsIter); … … 273 273 { 274 274 psTrace(__func__, 0, "FPA:\n"); 275 if (fpa-> data) {276 psTrace(__func__, 1, "---> FPA is extension %s.\n", fpa-> data->extname);277 if (! fpa-> data->images) {278 psTrace(__func__, 1, "---> NO PIXELS for extension %s\n", fpa-> data->extname);275 if (fpa->hdu) { 276 psTrace(__func__, 1, "---> FPA is extension %s.\n", fpa->hdu->extname); 277 if (! fpa->hdu->images) { 278 psTrace(__func__, 1, "---> NO PIXELS for extension %s\n", fpa->hdu->extname); 279 279 } 280 280 } … … 285 285 psTrace(__func__, 1, "Chip: %d\n", i); 286 286 pmChip *chip = chips->data[i]; // The chip 287 if (chip-> data) {288 psTrace(__func__, 2, "---> Chip is extension %s.\n", chip-> data->extname);289 if (! chip-> data->images) {290 psTrace(__func__, 2, "---> NO PIXELS for extension %s\n", chip-> data->extname);287 if (chip->hdu) { 288 psTrace(__func__, 2, "---> Chip is extension %s.\n", chip->hdu->extname); 289 if (! chip->hdu->images) { 290 psTrace(__func__, 2, "---> NO PIXELS for extension %s\n", chip->hdu->extname); 291 291 } 292 292 } … … 296 296 psTrace(__func__, 2, "Cell: %d\n", j); 297 297 pmCell *cell = cells->data[j]; // The cell 298 if (cell-> data) {299 psTrace(__func__, 3, "---> Cell is extension %s.\n", cell-> data->extname);300 if (! cell-> data->images) {301 psTrace(__func__, 3, "---> NO PIXELS for extension %s\n", cell-> data->extname);298 if (cell->hdu) { 299 psTrace(__func__, 3, "---> Cell is extension %s.\n", cell->hdu->extname); 300 if (! cell->hdu->images) { 301 psTrace(__func__, 3, "---> NO PIXELS for extension %s\n", cell->hdu->extname); 302 302 } 303 303 } -
trunk/archive/scripts/src/phase2/pmFPARead.c
r5371 r5564 17 17 18 18 // Read a FITS extension into a chip 19 static bool readExtension(p mPixelData *pixelData, // Pixel data into which to read19 static bool readExtension(p_pmHDU *hdu, // Pixel data into which to read 20 20 psFits *fits // The FITS file from which to read 21 21 ) 22 22 { 23 const char *extName = pixelData->extname; // Extension name23 const char *extName = hdu->extname; // Extension name 24 24 25 25 psTrace(__func__, 7, "Moving to extension %s...\n", extName); … … 95 95 96 96 // Update the HDU with the new data 97 pixelData->images = pixels;98 pixelData->header = header;97 hdu->images = pixels; 98 hdu->header = header; 99 99 100 100 // XXX: Insert mask and weight reading here … … 105 105 // Portion out an image into the cell 106 106 static bool generateReadouts(pmCell *cell, // The cell that gets its bits 107 p mPixelData *data// Pixel data, containing image, mask, weights107 p_pmHDU *hdu // Pixel data, containing image, mask, weights 108 108 ) 109 109 { 110 psArray *images = data->images; // Array of images (each of which is a readout)111 psArray *masks = data->masks; // Array of masks (one for each readout)110 psArray *images = hdu->images; // Array of images (each of which is a readout) 111 psArray *masks = hdu->masks; // Array of masks (one for each readout) 112 112 // Iterate over each of the image planes 113 113 for (int i = 0; i < images->n; i++) { … … 118 118 } 119 119 120 pmReadout *readout = pmReadoutAlloc(cell, image, mask, 0,0, 0,0,1,1);120 pmReadout *readout = pmReadoutAlloc(cell, image, mask, 0,0,1,1); 121 121 psFree(readout); // This seems silly, but the alloc registers it with the cell, so we 122 122 // don't have to do anything with it. Perhaps we should change … … 137 137 ) 138 138 { 139 p mPixelData *pixelData= NULL; // Pixel data from FITS file139 p_pmHDU *hdu = NULL; // Pixel data from FITS file 140 140 141 141 // Read the PHU, if required … … 152 152 // Read in.... 153 153 psTrace(__func__, 1, "Working on FPA...\n"); 154 if (fpa-> data) {155 pixelData= fpa->data;156 psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", pixelData->extname);157 if (! readExtension( pixelData, fits)) {158 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", pixelData->extname);154 if (fpa->hdu) { 155 hdu = fpa->hdu; 156 psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", hdu->extname); 157 if (! readExtension(hdu, fits)) { 158 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname); 159 159 return false; 160 160 } … … 174 174 psTrace(__func__, 2, "Reading in chip %d...\n", i); 175 175 176 if (chip-> data) {177 pixelData = chip->data;178 psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", pixelData->extname, i);179 if (! readExtension( pixelData, fits)) {180 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", pixelData->extname);176 if (chip->hdu) { 177 hdu = chip->hdu; 178 psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", hdu->extname, i); 179 if (! readExtension(hdu, fits)) { 180 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname); 181 181 return false; 182 182 } … … 196 196 psTrace(__func__, 3, "Reading in cell %d...\n", j); 197 197 198 if (cell-> data) {199 pixelData = cell->data;200 psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", pixelData->extname,198 if (cell->hdu) { 199 hdu = cell->hdu; 200 psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", hdu->extname, 201 201 j); 202 if (! readExtension( pixelData, fits)) {202 if (! readExtension(hdu, fits)) { 203 203 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", 204 pixelData->extname);204 hdu->extname); 205 205 return false; 206 206 } … … 210 210 psTrace(__func__, 5, "Allocating readouts for chip %d cell %d...\n", 211 211 i, j); 212 generateReadouts(cell, pixelData);212 generateReadouts(cell, hdu); 213 213 } 214 214 } … … 216 216 return true; 217 217 } 218 219 // Translate a name from the configuration file, containing something like "%a_%d" to a real value 220 psString p_pmFPATranslateName(psString name, // The name to translate 221 pmCell *cell // The cell for which to translate 222 ) 223 { 224 // %a is the FPA.NAME 225 // %b is the CHIP.NAME 226 // %c is the CELL.NAME 227 // %d is the chip number 228 // %e if the cell number 229 // %f is the extension name 230 231 psString translation = psMemIncrRefCounter(name); // The translated string 232 char *temp = NULL; // Temporary string 233 234 pmChip *chip = cell->parent; // Chip of interest 235 pmFPA *fpa = chip->parent; // FPA of interest 236 237 // FPA.NAME 238 if (temp = strstr(translation, "%a")) { 239 // This is not particularly friendly to the memory allocation, but the cache should make it OK, 240 // and there's not much of it anyway... 241 psFree(translation); 242 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string 243 psString fpaName = psMetadataLookupString(NULL, fpa->concepts, "FPA.NAME"); // The value of FPA.NAME 244 psStringAppend(&translation, "%s%s", fpaName, temp + 2); 245 // So "translation" now contains the first part, the replaced string, and the last part. 246 } 247 248 // CHIP.NAME 249 if (temp = strstr(translation, "%b")) { 250 // This is not particularly friendly to the memory allocation, but the cache should make it OK, 251 // and there's not much of it anyway... 252 psFree(translation); 253 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string 254 psString chipName = psMetadataLookupString(NULL, chip->concepts, "CHIP.NAME"); // CHIP.NAME's value 255 psStringAppend(&translation, "%s%s", chipName, temp + 2); 256 // So "translation" now contains the first part, the replaced string, and the last part. 257 } 258 259 // CELL.NAME 260 if (temp = strstr(translation, "%c")) { 261 // This is not particularly friendly to the memory allocation, but the cache should make it OK, 262 // and there's not much of it anyway... 263 psFree(translation); 264 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string 265 psString cellName = psMetadataLookupString(NULL, cell->concepts, "CELL.NAME"); // CELL.NAME's value 266 psStringAppend(&translation, "%s%s", cellName, temp + 2); 267 // So "translation" now contains the first part, the replaced string, and the last part. 268 } 269 270 // Chip number 271 if (temp = strstr(translation, "%d")) { 272 // This is not particularly friendly to the memory allocation, but the cache should make it OK, 273 // and there's not much of it anyway... 274 psFree(translation); 275 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string 276 // Search for the pointer to get the chip number 277 int chipNum = -1; 278 psArray *chips = fpa->chips; // The array of chips 279 for (int i = 0; i < chips->n && chipNum < 0; i++) { 280 if (chips->data[i] == chip) { 281 chipNum = i; 282 } 283 } 284 if (chipNum < 0) { 285 psError(PS_ERR_IO, true, "Unable to find chip to get name: %s\n", name); 286 // Try to muddle on by leaving the number out 287 psStringAppend(&translation, "%s", temp + 2); 288 } else { 289 psStringAppend(&translation, "%d%s", chipNum, temp + 2); 290 } 291 // So "translation" now contains the first part, the replaced string, and the last part. 292 } 293 294 // Cell number 295 if (temp = strstr(translation, "%e")) { 296 // This is not particularly friendly to the memory allocation, but the cache should make it OK, 297 // and there's not much of it anyway... 298 psFree(translation); 299 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string 300 // Search for the pointer to get the cell number 301 int cellNum = -1; 302 psArray *cells = chip->cells; // The array of cells 303 for (int i = 0; i < cells->n && cellNum < 0; i++) { 304 if (cells->data[i] == cell) { 305 cellNum = i; 306 } 307 } 308 if (cellNum < 0) { 309 psError(PS_ERR_IO, true, "Unable to find cell to get name: %s\n", name); 310 // Try to muddle on by leaving the number out 311 psStringAppend(&translation, "%s", temp + 2); 312 } else { 313 psStringAppend(&translation, "%d%s", cellNum, temp + 2); 314 } 315 // So "translation" now contains the first part, the replaced string, and the last part. 316 } 317 318 // Extension name 319 if (temp = strstr(translation, "%f")) { 320 // This is not particularly friendly to the memory allocation, but the cache should make it OK, 321 // and there's not much of it anyway... 322 psFree(translation); 323 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string 324 const char *extname = NULL; 325 if (cell->hdu) { 326 extname = cell->hdu->extname; 327 } else if (chip->hdu) { 328 extname = chip->hdu->extname; 329 } else if (fpa->hdu) { 330 extname = fpa->hdu->extname; 331 } 332 psStringAppend(&translation, "%s%s", extname, temp + 2); 333 // So "translation" now contains the first part, the replaced string, and the last part. 334 } 335 336 return translation; 337 } 338 339 // Read a mask into the FPA 340 bool pmFPAReadMask(pmFPA *fpa, // FPA to read into 341 psFits *source // Source FITS file (for the original data) 342 ) 343 { 344 const psMetadata *camera = fpa->camera; // Camera configuration for FPA 345 bool mdok = false; // Status of MD lookup 346 347 // Get the required information from the camera configuration 348 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data 349 if (! mdok || ! supps) { 350 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n"); 351 return false; 352 } 353 psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE 354 if (! mdok || strlen(sourceType) <= 0) { 355 psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera " 356 "configuration!\n"); 357 return false; 358 } 359 psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask 360 if (! mdok || strlen(sourceType) <= 0) { 361 psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera " 362 "configuration!\n"); 363 return false; 364 } 365 366 // Go through the FPA to each cell/readout to get the mask 367 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the mask 368 psArray *chips = fpa->chips; // Array of chips 369 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 370 pmChip *chip = chips->data[chipNum]; // The current chip of interest 371 if (chip->valid) { 372 if (chip->hdu) { 373 hdu = chip->hdu; 374 } 375 psArray *cells = chip->cells; // Array of cells 376 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 377 pmCell *cell = cells->data[cellNum]; // The current cell of interest 378 if (cell->valid) { 379 if (cell->hdu) { 380 hdu = cell->hdu; 381 } 382 383 // Now, need to find out where to get the pixels 384 psFits *maskSource = source; // Source of mask image 385 if (strcasecmp(sourceType, "FILE") == 0) { 386 // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt" 387 psString filenameExt = p_pmFPATranslateName(name, cell); 388 char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn 389 psString filename = NULL; // The filename 390 psString extname = NULL;// The extenstion name 391 if (colon) { 392 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon)); 393 if (strlen(colon) > 1) { 394 extname = psStringCopy(colon + 1); 395 } 396 } else { 397 filename = psMemIncrRefCounter(filenameExt); 398 } 399 400 maskSource = psFitsAlloc(filename); 401 if (extname) { 402 if (! psFitsMoveExtName(maskSource, extname)) { 403 psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read mask.\n", 404 extname); 405 return false; 406 } 407 } 408 psFree(filename); 409 psFree(extname); 410 psFree(filenameExt); 411 } else if (strncasecmp(sourceType, "EXT", 3) == 0) { 412 // Source is an extension in the original file 413 psString extname = p_pmFPATranslateName(name, cell); 414 psFitsMoveExtName(maskSource, extname); 415 } 416 417 // We've arrived where the pixels are. Now we need to read them in. 418 psMetadata *header = psFitsReadHeader(NULL, maskSource); // The header 419 bool mdStatus = false; 420 int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS"); 421 if (!mdStatus) { 422 psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for " 423 "mask (%s)!\n", name); 424 } 425 if (nAxis != 2 && nAxis != 3) { 426 psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into " 427 "a single image anyway.\n"); 428 } 429 430 int numPlanes = 1; // Number of planes 431 if (nAxis == 3) { 432 numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3"); 433 if (!mdStatus) { 434 psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n"); 435 // Try to proceed by taking only the first plane 436 numPlanes = 1; 437 } 438 if (numPlanes != 1 && numPlanes != cell->readouts->n) { 439 psError(PS_ERR_IO, false, "Number of masks (%d) does not match number of " 440 "readouts (%d)\n", numPlanes, cell->readouts->n); 441 // Try to proceed by taking only the first plane 442 numPlanes = 1; 443 } 444 } 445 446 hdu->masks = psArrayAlloc(hdu->images->n); 447 448 // Read each plane into the array 449 psArray *readouts = cell->readouts; // The array of readouts 450 for (int i = 0; i < hdu->masks->n; i++) { 451 psImage *mask = NULL; // The mask to be added 452 if (i < numPlanes) { 453 // Read the mask from the file 454 psTrace(__func__, 9, "Reading plane %d\n", i); 455 psRegion region = {0, 0, 0, 0}; 456 mask = psFitsReadImage(NULL, maskSource, region, i); 457 } else { 458 // One mask in the file is provided for all planes in the original image 459 psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i); 460 psImage *original = hdu->masks->data[0]; 461 mask = psImageCopy(NULL, original, original->type.type); 462 } 463 hdu->masks->data[0] = mask; 464 pmReadout *readout = readouts->data[i]; 465 readout->mask = mask; 466 // Check the dimensions 467 // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size? 468 if (mask->numCols < readout->image->numCols || 469 mask->numRows < readout->image->numRows) 470 { 471 psError(PS_ERR_IO, false, "Mask size (%dx%d) not compatible with image (%dx%d)\n", 472 mask->numCols, mask->numRows, readout->image->numCols, 473 readout->image->numRows); 474 return false; 475 } 476 } // Iterating over readouts 477 } // Valid cells 478 } // Iterating over cells 479 } // Valid chips 480 } // Iterating over chips 481 482 return true; 483 } 484 485 486 // Read a mask into the FPA 487 // This is just a copy of the above pmFPAReadMask, replacing "mask" with "weight" throughout. 488 bool pmFPAReadWeight(pmFPA *fpa, // FPA to read into 489 psFits *source // Source FITS file (for the original data) 490 ) 491 { 492 const psMetadata *camera = fpa->camera; // Camera configuration for FPA 493 bool mdok = false; // Status of MD lookup 494 495 // Get the required information from the camera configuration 496 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data 497 if (! mdok || ! supps) { 498 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n"); 499 return false; 500 } 501 psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE 502 if (! mdok || strlen(sourceType) <= 0) { 503 psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera " 504 "configuration!\n"); 505 return false; 506 } 507 psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight 508 if (! mdok || strlen(sourceType) <= 0) { 509 psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera " 510 "configuration!\n"); 511 return false; 512 } 513 514 // Go through the FPA to each cell/readout to get the weight 515 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the weight 516 psArray *chips = fpa->chips; // Array of chips 517 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 518 pmChip *chip = chips->data[chipNum]; // The current chip of interest 519 if (chip->valid) { 520 if (chip->hdu) { 521 hdu = chip->hdu; 522 } 523 psArray *cells = chip->cells; // Array of cells 524 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 525 pmCell *cell = cells->data[cellNum]; // The current cell of interest 526 if (cell->valid) { 527 if (cell->hdu) { 528 hdu = cell->hdu; 529 } 530 531 // Now, need to find out where to get the pixels 532 psFits *weightSource = source; // Source of weight image 533 if (strcasecmp(sourceType, "FILE") == 0) { 534 // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt" 535 psString filenameExt = p_pmFPATranslateName(name, cell); 536 char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn 537 psString filename = NULL; // The filename 538 psString extname = NULL;// The extenstion name 539 if (colon) { 540 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon)); 541 if (strlen(colon) > 1) { 542 extname = psStringCopy(colon + 1); 543 } 544 } else { 545 filename = psMemIncrRefCounter(filenameExt); 546 } 547 548 weightSource = psFitsAlloc(filename); 549 if (extname) { 550 if (! psFitsMoveExtName(weightSource, extname)) { 551 psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read " 552 "weight.\n", extname); 553 return false; 554 } 555 } 556 psFree(filename); 557 psFree(extname); 558 psFree(filenameExt); 559 } else if (strncasecmp(sourceType, "EXT", 3) == 0) { 560 // Source is an extension in the original file 561 psString extname = p_pmFPATranslateName(name, cell); 562 psFitsMoveExtName(weightSource, extname); 563 } 564 565 // We've arrived where the pixels are. Now we need to read them in. 566 psMetadata *header = psFitsReadHeader(NULL, weightSource); // The header 567 bool mdStatus = false; 568 int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS"); 569 if (!mdStatus) { 570 psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for " 571 "weight (%s)!\n", name); 572 } 573 if (nAxis != 2 && nAxis != 3) { 574 psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into " 575 "a single image anyway.\n"); 576 } 577 578 int numPlanes = 1; // Number of planes 579 if (nAxis == 3) { 580 numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3"); 581 if (!mdStatus) { 582 psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n"); 583 // Try to proceed by taking only the first plane 584 numPlanes = 1; 585 } 586 if (numPlanes != 1 && numPlanes != cell->readouts->n) { 587 psError(PS_ERR_IO, false, "Number of weights (%d) does not match number of " 588 "readouts (%d)\n", numPlanes, cell->readouts->n); 589 // Try to proceed by taking only the first plane 590 numPlanes = 1; 591 } 592 } 593 594 hdu->weights = psArrayAlloc(hdu->images->n); 595 596 // Read each plane into the array 597 psArray *readouts = cell->readouts; // The array of readouts 598 for (int i = 0; i < hdu->weights->n; i++) { 599 psImage *weight = NULL; // The weight to be added 600 if (i < numPlanes) { 601 // Read the weight from the file 602 psTrace(__func__, 9, "Reading plane %d\n", i); 603 psRegion region = {0, 0, 0, 0}; 604 weight = psFitsReadImage(NULL, weightSource, region, i); 605 } else { 606 // One weight in the file is provided for all planes in the original image 607 psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i); 608 psImage *original = hdu->weights->data[0]; 609 weight = psImageCopy(NULL, original, original->type.type); 610 } 611 hdu->weights->data[0] = weight; 612 pmReadout *readout = readouts->data[i]; 613 readout->weight = weight; 614 // Check the dimensions 615 // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size? 616 if (weight->numCols < readout->image->numCols || 617 weight->numRows < readout->image->numRows) 618 { 619 psError(PS_ERR_IO, false, "Weight size (%dx%d) not compatible with image " 620 "(%dx%d)\n", weight->numCols, weight->numRows, readout->image->numCols, 621 readout->image->numRows); 622 return false; 623 } 624 } // Iterating over readouts 625 } // Valid cells 626 } // Iterating over cells 627 } // Valid chips 628 } // Iterating over chips 629 630 return true; 631 } -
trunk/archive/scripts/src/phase2/pmFPARead.h
r5105 r5564 10 10 ); 11 11 12 psString p_pmFPATranslateName(psString name, // The name to translate 13 pmCell *cell // The cell for which to translate 14 ); 15 16 bool pmFPAReadMask(pmFPA *fpa, // FPA to read into 17 psFits *source // Source FITS file (for the original data) 18 ); 19 20 bool pmFPAReadWeight(pmFPA *fpa, // FPA to read into 21 psFits *source // Source FITS file (for the original data) 22 ); 23 24 12 25 #endif -
trunk/archive/scripts/src/phase2/pmFPAWrite.c
r5371 r5564 5 5 #include "pmFPA.h" 6 6 #include "pmFPAConceptsSet.h" 7 #include "pmFPARead.h" 7 8 8 9 static bool writeHDU(psFits *fits, // FITS file to which to write 9 p mPixelData *pixelData// Pixel data to write10 p_pmHDU *hdu // Pixel data to write 10 11 ) 11 12 { 12 13 bool status = true; // Status of write, to return 13 for (int i = 0; i < pixelData->images->n; i++) {14 status &= psFitsWriteImage(fits, pixelData->header, pixelData->images->data[i], i);14 for (int i = 0; i < hdu->images->n; i++) { 15 status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i); 15 16 // XXX: Insert here the writing on mask and weight images 16 17 } … … 47 48 pmCellOutgestConcepts(cell, db); 48 49 49 if (cell-> data && strlen(cell->data->extname) > 0) {50 status &= writeHDU(fits, cell-> data);50 if (cell->hdu && strlen(cell->hdu->extname) > 0) { 51 status &= writeHDU(fits, cell->hdu); 51 52 } 52 53 } 53 54 } 54 55 55 if (chip-> data && strlen(chip->data->extname) > 0) {56 status &= writeHDU(fits, chip-> data);56 if (chip->hdu && strlen(chip->hdu->extname) > 0) { 57 status &= writeHDU(fits, chip->hdu); 57 58 } 58 59 } … … 60 61 } 61 62 62 if (fpa-> data && strlen(fpa->data->extname) > 0) {63 status &= writeHDU(fits, fpa-> data);63 if (fpa->hdu && strlen(fpa->hdu->extname) > 0) { 64 status &= writeHDU(fits, fpa->hdu); 64 65 } 65 66 66 67 return status; 67 68 } 69 70 71 bool pmFPAWriteMask(pmFPA *fpa, // FPA containing mask to write 72 psFits *fits // FITS file for image 73 ) 74 { 75 const psMetadata *camera = fpa->camera; // Camera configuration for FPA 76 bool mdok = false; // Status of MD lookup 77 78 // Get the required information from the camera configuration 79 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data 80 if (! mdok || ! supps) { 81 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n"); 82 return false; 83 } 84 psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE 85 if (! mdok || strlen(sourceType) <= 0) { 86 psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera " 87 "configuration!\n"); 88 return false; 89 } 90 psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask 91 if (! mdok || strlen(sourceType) <= 0) { 92 psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera " 93 "configuration!\n"); 94 return false; 95 } 96 97 // Go through the FPA to each cell/readout to get the mask 98 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the mask 99 psArray *chips = fpa->chips; // Array of chips 100 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 101 pmChip *chip = chips->data[chipNum]; // The current chip of interest 102 if (chip->valid) { 103 if (chip->hdu) { 104 hdu = chip->hdu; 105 } 106 psArray *cells = chip->cells; // Array of cells 107 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 108 pmCell *cell = cells->data[cellNum]; // The current cell of interest 109 if (cell->valid) { 110 if (cell->hdu) { 111 hdu = cell->hdu; 112 } 113 114 // Now, need to find out where to write the pixels 115 psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image 116 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name 117 if (strcasecmp(sourceType, "FILE") == 0) { 118 // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt" 119 psString filenameExt = p_pmFPATranslateName(name, cell); 120 char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn 121 psString filename = NULL; // The filename 122 psString extname = NULL;// The extenstion name 123 if (colon) { 124 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon)); 125 if (strlen(colon) > 1) { 126 extname = psStringCopy(colon + 1); 127 } 128 } else { 129 filename = psMemIncrRefCounter(filenameExt); 130 } 131 132 psFree(maskDest); 133 maskDest = psFitsAlloc(filename); 134 if (extname) { 135 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 136 } 137 psFree(filename); 138 psFree(extname); 139 psFree(filenameExt); 140 } else if (strncasecmp(sourceType, "EXT", 3) == 0) { 141 // Source is an extension in the original file 142 psString extname = p_pmFPATranslateName(name, cell); 143 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 144 psFree(extname); 145 } 146 147 // We've arrived where the pixels are. Now we need to write them out. 148 psArray *readouts = cell->readouts; // The array of readouts 149 for (int readNum = 0; readNum < readouts->n; readNum++) { 150 pmReadout *readout = readouts->data[readNum]; // The readout of interest 151 if (! readout->mask) { 152 psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n", 153 chipNum, cellNum, readNum); 154 } else { 155 // XXX: Need to add the extname to the existing header 156 if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) { 157 psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n", 158 readNum, hdu->extname); 159 return false; 160 } 161 } 162 } // Iterating over readouts 163 psFree(header); 164 psFree(maskDest); 165 } // Valid cells 166 } // Iterating over cells 167 } // Valid chips 168 } // Iterating over chips 169 170 return true; 171 } 172 173 174 bool pmFPAWriteWeight(pmFPA *fpa, // FPA containing mask to write 175 psFits *fits // FITS file for image 176 ) 177 { 178 const psMetadata *camera = fpa->camera; // Camera configuration for FPA 179 bool mdok = false; // Status of MD lookup 180 181 // Get the required information from the camera configuration 182 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data 183 if (! mdok || ! supps) { 184 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n"); 185 return false; 186 } 187 psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE 188 if (! mdok || strlen(sourceType) <= 0) { 189 psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera " 190 "configuration!\n"); 191 return false; 192 } 193 psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight 194 if (! mdok || strlen(sourceType) <= 0) { 195 psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera " 196 "configuration!\n"); 197 return false; 198 } 199 200 // Go through the FPA to each cell/readout to get the weight 201 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the weight 202 psArray *chips = fpa->chips; // Array of chips 203 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 204 pmChip *chip = chips->data[chipNum]; // The current chip of interest 205 if (chip->valid) { 206 if (chip->hdu) { 207 hdu = chip->hdu; 208 } 209 psArray *cells = chip->cells; // Array of cells 210 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 211 pmCell *cell = cells->data[cellNum]; // The current cell of interest 212 if (cell->valid) { 213 if (cell->hdu) { 214 hdu = cell->hdu; 215 } 216 217 // Now, need to find out where to write the pixels 218 psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image 219 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name 220 if (strcasecmp(sourceType, "FILE") == 0) { 221 // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt" 222 psString filenameExt = p_pmFPATranslateName(name, cell); 223 char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn 224 psString filename = NULL; // The filename 225 psString extname = NULL;// The extenstion name 226 if (colon) { 227 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon)); 228 if (strlen(colon) > 1) { 229 extname = psStringCopy(colon + 1); 230 } 231 } else { 232 filename = psMemIncrRefCounter(filenameExt); 233 } 234 235 psFree(weightDest); 236 weightDest = psFitsAlloc(filename); 237 if (extname) { 238 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 239 } 240 psFree(filename); 241 psFree(extname); 242 psFree(filenameExt); 243 } else if (strncasecmp(sourceType, "EXT", 3) == 0) { 244 // Source is an extension in the original file 245 psString extname = p_pmFPATranslateName(name, cell); 246 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 247 psFree(extname); 248 } 249 250 // We've arrived where the pixels are. Now we need to write them out. 251 psArray *readouts = cell->readouts; // The array of readouts 252 for (int readNum = 0; readNum < readouts->n; readNum++) { 253 pmReadout *readout = readouts->data[readNum]; // The readout of interest 254 if (! readout->weight) { 255 psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n", 256 chipNum, cellNum, readNum); 257 } else { 258 if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) { 259 psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n", 260 readNum, hdu->extname); 261 return false; 262 } 263 } 264 } // Iterating over readouts 265 psFree(header); 266 psFree(weightDest); 267 } // Valid cells 268 } // Iterating over cells 269 } // Valid chips 270 } // Iterating over chips 271 272 return true; 273 } -
trunk/archive/scripts/src/phase2/pmFPAWrite.h
r5105 r5564 9 9 ); 10 10 11 bool pmFPAWriteMask(pmFPA *fpa, // FPA containing mask to write 12 psFits *fits // FITS file for image 13 ); 14 15 bool pmFPAWriteWeight(pmFPA *fpa, // FPA containing mask to write 16 psFits *fits // FITS file for image 17 ); 18 11 19 12 20 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
