Changeset 5786 for trunk/archive/scripts/src/phase2/pmFPAWrite.c
- Timestamp:
- Dec 13, 2005, 5:47:35 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/archive/scripts/src/phase2/pmFPAWrite.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/scripts/src/phase2/pmFPAWrite.c
r5633 r5786 7 7 #include "pmFPARead.h" 8 8 9 static bool writeHDU(psFits *fits, // FITS file to which to write10 p_pmHDU *hdu// Pixel data to write11 ) 12 { 13 bool status = true; // Status of write, to return9 static bool writeHDU(psFits *fits, // FITS file to which to write 10 p_pmHDU *hdu // Pixel data to write 11 ) 12 { 13 bool status = true; // Status of write, to return 14 14 for (int i = 0; i < hdu->images->n; i++) { 15 status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);16 // XXX: Insert here the writing on mask and weight images15 status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i); 16 // XXX: Insert here the writing on mask and weight images 17 17 } 18 18 … … 21 21 22 22 23 bool pmFPAWrite(psFits *fits, // FITS file to which to write 24 pmFPA *fpa, // FPA to write 25 psDB *db // Database to update 26 ) 27 { 28 bool status = true; // Status of writing, to return 29 23 bool pmFPAWrite(psFits *fits, // FITS file to which to write 24 pmFPA *fpa, // FPA to write 25 psDB *db // Database to update 26 ) 27 { 28 bool status = true; // Status of writing, to return 29 30 psTrace(__func__, 7, "Outgesting FPA concepts...\n"); 30 31 pmFPAOutgestConcepts(fpa, db); 31 32 32 33 // Write the primary header 33 34 if (fpa->phu) { 34 status &= psFitsMoveExtNum(fits, 0, false);35 status &= psFitsWriteHeader(fpa->phu, fits);36 } 37 38 psArray *chips = fpa->chips; // Array of component chips35 status &= psFitsMoveExtNum(fits, 0, false); 36 status &= psFitsWriteHeader(fpa->phu, fits); 37 } 38 39 psArray *chips = fpa->chips; // Array of component chips 39 40 for (int i = 0; i < chips->n; i++) { 40 pmChip *chip = chips->data[i]; // The component chip 41 if (chip->valid) { 42 pmChipOutgestConcepts(chip, db); 43 44 psArray *cells = chip->cells; // Array of component cells 45 for (int j = 0; j < cells->n; j++) { 46 pmCell *cell = cells->data[j]; // The component cell 47 if (cell->valid) { 48 pmCellOutgestConcepts(cell, db); 49 50 if (cell->hdu && strlen(cell->hdu->extname) > 0) { 51 status &= writeHDU(fits, cell->hdu); 52 } 53 } 54 } 55 56 if (chip->hdu && strlen(chip->hdu->extname) > 0) { 57 status &= writeHDU(fits, chip->hdu); 58 } 59 } 41 pmChip *chip = chips->data[i]; // The component chip 42 if (chip->valid) { 43 psTrace(__func__, 1, "Writing out chip %d...\n", i); 44 45 pmChipOutgestConcepts(chip, db); 46 47 psArray *cells = chip->cells; // Array of component cells 48 for (int j = 0; j < cells->n; j++) { 49 pmCell *cell = cells->data[j]; // The component cell 50 if (cell->valid) { 51 psTrace(__func__, 2, "Writing out cell, %d...\n", j); 52 53 pmCellOutgestConcepts(cell, db); 54 55 if (cell->hdu && strlen(cell->hdu->extname) > 0) { 56 status &= writeHDU(fits, cell->hdu); 57 } 58 } 59 } 60 61 if (chip->hdu && strlen(chip->hdu->extname) > 0) { 62 status &= writeHDU(fits, chip->hdu); 63 } 64 } 60 65 61 66 } 62 67 63 68 if (fpa->hdu && strlen(fpa->hdu->extname) > 0) { 64 status &= writeHDU(fits, fpa->hdu);69 status &= writeHDU(fits, fpa->hdu); 65 70 } 66 71 … … 71 76 72 77 73 bool pmFPAWriteMask(pmFPA *fpa, // FPA containing mask to write74 psFits *fits// FITS file for image78 bool pmFPAWriteMask(pmFPA *fpa, // FPA containing mask to write 79 psFits *fits // FITS file for image 75 80 ) 76 81 { 77 82 const psMetadata *camera = fpa->camera; // Camera configuration for FPA 78 bool mdok = false; // Status of MD lookup83 bool mdok = false; // Status of MD lookup 79 84 80 85 // Get the required information from the camera configuration 81 86 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data 82 87 if (! mdok || ! supps) { 83 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");84 return false;88 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n"); 89 return false; 85 90 } 86 91 psString sourceType = psMetadataLookupString(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE 87 92 if (! mdok || strlen(sourceType) <= 0) { 88 psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "89 "configuration!\n");90 return false;93 psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera " 94 "configuration!\n"); 95 return false; 91 96 } 92 97 psString name = psMetadataLookupString(&mdok, supps, "MASK.NAME"); // Name of mask 93 98 if (! mdok || strlen(sourceType) <= 0) { 94 psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "95 "configuration!\n");96 return false;99 psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera " 100 "configuration!\n"); 101 return false; 97 102 } 98 103 99 104 // Go through the FPA to each cell/readout to get the mask 100 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the mask101 psArray *chips = fpa->chips; // Array of chips105 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the mask 106 psArray *chips = fpa->chips; // Array of chips 102 107 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 103 pmChip *chip = chips->data[chipNum]; // The current chip of interest104 if (chip->valid) {105 if (chip->hdu) {106 hdu = chip->hdu;107 }108 psArray *cells = chip->cells;// Array of cells109 for (int cellNum = 0; cellNum < cells->n; cellNum++) {110 pmCell *cell = cells->data[cellNum]; // The current cell of interest111 if (cell->valid) {112 if (cell->hdu) {113 hdu = cell->hdu;114 }115 116 // Now, need to find out where to write the pixels117 psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image118 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name119 if (strcasecmp(sourceType, "FILE") == 0) {120 // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"121 psString filenameExt = p_pmFPATranslateName(name, cell);122 char *colon = strchr(filenameExt, ':');// Pointer to a colon in the filename-extn123 psString filename = NULL; // The filename124 psString extname = NULL;// The extenstion name125 if (colon) {126 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));127 if (strlen(colon) > 1) {128 extname = psStringCopy(colon + 1);129 }130 } else {131 filename = psMemIncrRefCounter(filenameExt);132 }133 134 psFree(maskDest);135 maskDest = psFitsAlloc(filename);136 if (extname) {137 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);138 }139 psFree(filename);140 psFree(extname);141 psFree(filenameExt);142 } else if (strncasecmp(sourceType, "EXT", 3) == 0) {143 // Source is an extension in the original file144 psString extname = p_pmFPATranslateName(name, cell);145 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);146 psFree(extname);147 }148 149 // We've arrived where the pixels are. Now we need to write them out.150 psArray *readouts = cell->readouts; // The array of readouts151 for (int readNum = 0; readNum < readouts->n; readNum++) {152 pmReadout *readout = readouts->data[readNum]; // The readout of interest153 if (! readout->mask) {154 psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",155 chipNum, cellNum, readNum);156 } else {157 // XXX: Need to add the extname to the existing header158 if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) {159 psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",160 readNum, hdu->extname);161 return false;162 }163 }164 } // Iterating over readouts165 psFree(header);166 psFree(maskDest);167 } // Valid cells168 } // Iterating over cells169 } // Valid chips108 pmChip *chip = chips->data[chipNum]; // The current chip of interest 109 if (chip->valid) { 110 if (chip->hdu) { 111 hdu = chip->hdu; 112 } 113 psArray *cells = chip->cells; // Array of cells 114 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 115 pmCell *cell = cells->data[cellNum]; // The current cell of interest 116 if (cell->valid) { 117 if (cell->hdu) { 118 hdu = cell->hdu; 119 } 120 121 // Now, need to find out where to write the pixels 122 psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image 123 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name 124 if (strcasecmp(sourceType, "FILE") == 0) { 125 // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt" 126 psString filenameExt = p_pmFPATranslateName(name, cell); 127 char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn 128 psString filename = NULL; // The filename 129 psString extname = NULL;// The extenstion name 130 if (colon) { 131 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon)); 132 if (strlen(colon) > 1) { 133 extname = psStringCopy(colon + 1); 134 } 135 } else { 136 filename = psMemIncrRefCounter(filenameExt); 137 } 138 139 psFree(maskDest); 140 maskDest = psFitsOpen(filename, "rw"); 141 if (extname) { 142 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 143 } 144 psFree(filename); 145 psFree(extname); 146 psFree(filenameExt); 147 } else if (strncasecmp(sourceType, "EXT", 3) == 0) { 148 // Source is an extension in the original file 149 psString extname = p_pmFPATranslateName(name, cell); 150 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 151 psFree(extname); 152 } 153 154 // We've arrived where the pixels are. Now we need to write them out. 155 psArray *readouts = cell->readouts; // The array of readouts 156 for (int readNum = 0; readNum < readouts->n; readNum++) { 157 pmReadout *readout = readouts->data[readNum]; // The readout of interest 158 if (! readout->mask) { 159 psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n", 160 chipNum, cellNum, readNum); 161 } else { 162 // XXX: Need to add the extname to the existing header 163 if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) { 164 psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n", 165 readNum, hdu->extname); 166 return false; 167 } 168 } 169 } // Iterating over readouts 170 psFree(header); 171 psFree(maskDest); 172 } // Valid cells 173 } // Iterating over cells 174 } // Valid chips 170 175 } // Iterating over chips 171 176 … … 174 179 175 180 176 bool pmFPAWriteWeight(pmFPA *fpa, // FPA containing mask to write177 psFits *fits// FITS file for image181 bool pmFPAWriteWeight(pmFPA *fpa, // FPA containing mask to write 182 psFits *fits // FITS file for image 178 183 ) 179 184 { 180 185 const psMetadata *camera = fpa->camera; // Camera configuration for FPA 181 bool mdok = false; // Status of MD lookup186 bool mdok = false; // Status of MD lookup 182 187 183 188 // Get the required information from the camera configuration 184 189 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data 185 190 if (! mdok || ! supps) { 186 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");187 return false;191 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n"); 192 return false; 188 193 } 189 194 psString sourceType = psMetadataLookupString(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE 190 195 if (! mdok || strlen(sourceType) <= 0) { 191 psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "192 "configuration!\n");193 return false;196 psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera " 197 "configuration!\n"); 198 return false; 194 199 } 195 200 psString name = psMetadataLookupString(&mdok, supps, "WEIGHT.NAME"); // Name of weight 196 201 if (! mdok || strlen(sourceType) <= 0) { 197 psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "198 "configuration!\n");199 return false;202 psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera " 203 "configuration!\n"); 204 return false; 200 205 } 201 206 202 207 // Go through the FPA to each cell/readout to get the weight 203 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the weight204 psArray *chips = fpa->chips; // Array of chips208 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the weight 209 psArray *chips = fpa->chips; // Array of chips 205 210 for (int chipNum = 0; chipNum < chips->n; chipNum++) { 206 pmChip *chip = chips->data[chipNum]; // The current chip of interest207 if (chip->valid) {208 if (chip->hdu) {209 hdu = chip->hdu;210 }211 psArray *cells = chip->cells;// Array of cells212 for (int cellNum = 0; cellNum < cells->n; cellNum++) {213 pmCell *cell = cells->data[cellNum]; // The current cell of interest214 if (cell->valid) {215 if (cell->hdu) {216 hdu = cell->hdu;217 }218 219 // Now, need to find out where to write the pixels220 psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image221 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name222 if (strcasecmp(sourceType, "FILE") == 0) {223 // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"224 psString filenameExt = p_pmFPATranslateName(name, cell);225 char *colon = strchr(filenameExt, ':');// Pointer to a colon in the filename-extn226 psString filename = NULL; // The filename227 psString extname = NULL;// The extenstion name228 if (colon) {229 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));230 if (strlen(colon) > 1) {231 extname = psStringCopy(colon + 1);232 }233 } else {234 filename = psMemIncrRefCounter(filenameExt);235 }236 237 psFree(weightDest);238 weightDest = psFitsAlloc(filename);239 if (extname) {240 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);241 }242 psFree(filename);243 psFree(extname);244 psFree(filenameExt);245 } else if (strncasecmp(sourceType, "EXT", 3) == 0) {246 // Source is an extension in the original file247 psString extname = p_pmFPATranslateName(name, cell);248 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);249 psFree(extname);250 }251 252 // We've arrived where the pixels are. Now we need to write them out.253 psArray *readouts = cell->readouts; // The array of readouts254 for (int readNum = 0; readNum < readouts->n; readNum++) {255 pmReadout *readout = readouts->data[readNum]; // The readout of interest256 if (! readout->weight) {257 psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",258 chipNum, cellNum, readNum);259 } else {260 if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) {261 psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",262 readNum, hdu->extname);263 return false;264 }265 }266 } // Iterating over readouts267 psFree(header);268 psFree(weightDest);269 } // Valid cells270 } // Iterating over cells271 } // Valid chips211 pmChip *chip = chips->data[chipNum]; // The current chip of interest 212 if (chip->valid) { 213 if (chip->hdu) { 214 hdu = chip->hdu; 215 } 216 psArray *cells = chip->cells; // Array of cells 217 for (int cellNum = 0; cellNum < cells->n; cellNum++) { 218 pmCell *cell = cells->data[cellNum]; // The current cell of interest 219 if (cell->valid) { 220 if (cell->hdu) { 221 hdu = cell->hdu; 222 } 223 224 // Now, need to find out where to write the pixels 225 psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image 226 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name 227 if (strcasecmp(sourceType, "FILE") == 0) { 228 // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt" 229 psString filenameExt = p_pmFPATranslateName(name, cell); 230 char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn 231 psString filename = NULL; // The filename 232 psString extname = NULL;// The extenstion name 233 if (colon) { 234 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon)); 235 if (strlen(colon) > 1) { 236 extname = psStringCopy(colon + 1); 237 } 238 } else { 239 filename = psMemIncrRefCounter(filenameExt); 240 } 241 242 psFree(weightDest); 243 weightDest = psFitsOpen(filename, "rw"); 244 if (extname) { 245 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 246 } 247 psFree(filename); 248 psFree(extname); 249 psFree(filenameExt); 250 } else if (strncasecmp(sourceType, "EXT", 3) == 0) { 251 // Source is an extension in the original file 252 psString extname = p_pmFPATranslateName(name, cell); 253 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname); 254 psFree(extname); 255 } 256 257 // We've arrived where the pixels are. Now we need to write them out. 258 psArray *readouts = cell->readouts; // The array of readouts 259 for (int readNum = 0; readNum < readouts->n; readNum++) { 260 pmReadout *readout = readouts->data[readNum]; // The readout of interest 261 if (! readout->weight) { 262 psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n", 263 chipNum, cellNum, readNum); 264 } else { 265 if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) { 266 psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n", 267 readNum, hdu->extname); 268 return false; 269 } 270 } 271 } // Iterating over readouts 272 psFree(header); 273 psFree(weightDest); 274 } // Valid cells 275 } // Iterating over cells 276 } // Valid chips 272 277 } // Iterating over chips 273 278
Note:
See TracChangeset
for help on using the changeset viewer.
