Changeset 6840
- Timestamp:
- Apr 11, 2006, 6:11:54 PM (20 years ago)
- Location:
- branches/rel10_ifa/psModules/src/astrom
- Files:
-
- 2 edited
-
pmFPARead.c (modified) (1 diff)
-
pmFPAWrite.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/rel10_ifa/psModules/src/astrom/pmFPARead.c
r6720 r6840 156 156 } 157 157 158 159 #if 0160 // Don't know that we need these161 162 // Read the PHU into the nominated HDU163 static bool readPHU(pmHDU *hdu, // HDU to read into164 psFits *fits // FITS file from which to read165 )166 {167 if (! hdu || ! hdu->phu) {168 return false; // Nothing to see here, move along169 }170 if (hdu->header) {171 return true; // Already something to see here, no need to gawk at it... (;172 }173 // Read the PHU174 psFitsMoveExtNum(fits, 0, false);175 hdu->header = psFitsReadHeader(hdu->header, fits);176 return true;177 }178 179 // Read the PHU into a cell180 bool pmCellReadPHU(pmCell *cell, // Cell to read into181 psFits *fits // FITS file from which to read182 )183 {184 return readPHU(cell->hdu, fits) || readPHU(cell->parent->hdu, fits) ||185 readPHU(cell->parent->parent->hdu, fits);186 187 }188 189 // Read the PHU into a chip190 bool pmChipReadPHU(pmChip *chip, // Chip to read into191 psFits *fits // FITS file from which to read192 )193 {194 return readPHU(chip->hdu, fits) || readPHU(chip->parent->hdu, fits);195 }196 197 // Read the PHU into an FPA198 bool pmFPAReadPHU(pmFPA *fpa, // FPA to read into199 psFits *fits // FITS file from which to read200 )201 {202 return readPHU(fpa->hdu, fits);203 }204 205 // Translate a name from the configuration file, containing something like "%a_%d" to a real value206 psString p_pmFPATranslateName(const psString name, // The name to translate207 const pmCell *cell // The cell for which to translate208 )209 {210 // %a is the FPA.NAME211 // %b is the CHIP.NAME212 // %c is the CELL.NAME213 // %d is the chip number214 // %e if the cell number215 // %f is the extension name216 217 psString translation = psMemIncrRefCounter(name); // The translated string218 char *temp = NULL; // Temporary string219 220 pmChip *chip = cell->parent; // Chip of interest221 pmFPA *fpa = chip->parent; // FPA of interest222 223 // FPA.NAME224 if ((temp = strstr(translation, "%a"))) {225 // This is not particularly friendly to the memory allocation, but the cache should make it OK,226 // and there's not much of it anyway...227 psFree(translation);228 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string229 psString fpaName = psMetadataLookupStr(NULL, fpa->concepts, "FPA.NAME"); // The value of FPA.NAME230 psStringAppend(&translation, "%s%s", fpaName, temp + 2);231 // So "translation" now contains the first part, the replaced string, and the last part.232 }233 234 // CHIP.NAME235 if ((temp = strstr(translation, "%b"))) {236 // This is not particularly friendly to the memory allocation, but the cache should make it OK,237 // and there's not much of it anyway...238 psFree(translation);239 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string240 psString chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME"); // CHIP.NAME's value241 psStringAppend(&translation, "%s%s", chipName, temp + 2);242 // So "translation" now contains the first part, the replaced string, and the last part.243 }244 245 // CELL.NAME246 if ((temp = strstr(translation, "%c"))) {247 // This is not particularly friendly to the memory allocation, but the cache should make it OK,248 // and there's not much of it anyway...249 psFree(translation);250 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string251 psString cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // CELL.NAME's value252 psStringAppend(&translation, "%s%s", cellName, temp + 2);253 // So "translation" now contains the first part, the replaced string, and the last part.254 }255 256 // Chip number257 if ((temp = strstr(translation, "%d"))) {258 // This is not particularly friendly to the memory allocation, but the cache should make it OK,259 // and there's not much of it anyway...260 psFree(translation);261 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string262 // Search for the pointer to get the chip number263 int chipNum = -1;264 psArray *chips = fpa->chips; // The array of chips265 for (int i = 0; i < chips->n && chipNum < 0; i++) {266 if (chips->data[i] == chip) {267 chipNum = i;268 }269 }270 if (chipNum < 0) {271 psError(PS_ERR_IO, true, "Unable to find chip to get name: %s\n", name);272 // Try to muddle on by leaving the number out273 psStringAppend(&translation, "%s", temp + 2);274 } else {275 psStringAppend(&translation, "%d%s", chipNum, temp + 2);276 }277 // So "translation" now contains the first part, the replaced string, and the last part.278 }279 280 // Cell number281 if ((temp = strstr(translation, "%e"))) {282 // This is not particularly friendly to the memory allocation, but the cache should make it OK,283 // and there's not much of it anyway...284 psFree(translation);285 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string286 // Search for the pointer to get the cell number287 int cellNum = -1;288 psArray *cells = chip->cells; // The array of cells289 for (int i = 0; i < cells->n && cellNum < 0; i++) {290 if (cells->data[i] == cell) {291 cellNum = i;292 }293 }294 if (cellNum < 0) {295 psError(PS_ERR_IO, true, "Unable to find cell to get name: %s\n", name);296 // Try to muddle on by leaving the number out297 psStringAppend(&translation, "%s", temp + 2);298 } else {299 psStringAppend(&translation, "%d%s", cellNum, temp + 2);300 }301 // So "translation" now contains the first part, the replaced string, and the last part.302 }303 304 // Extension name305 if ((temp = strstr(translation, "%f"))) {306 // This is not particularly friendly to the memory allocation, but the cache should make it OK,307 // and there's not much of it anyway...308 psFree(translation);309 translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string310 const char *extname = NULL;311 if (cell->hdu) {312 extname = cell->hdu->extname;313 } else if (chip->hdu) {314 extname = chip->hdu->extname;315 } else if (fpa->hdu) {316 extname = fpa->hdu->extname;317 }318 psStringAppend(&translation, "%s%s", extname, temp + 2);319 // So "translation" now contains the first part, the replaced string, and the last part.320 }321 322 return translation;323 }324 325 // Get filename and extension out of "somefile.fits:ext"326 psString p_pmFPATranslateFileExt(psString *extName, // Extension name, to be returned327 const psString name, // The string to be translated and then parsed328 const pmCell *cell // The cell329 )330 {331 psString filenameExt = p_pmFPATranslateName(name, cell);332 const char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn333 psString filename = NULL; // The filename334 if (extName && *extName) {335 psFree(*extName);336 }337 if (extName) {338 *extName = NULL;339 }340 341 if (colon) {342 filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));343 if (strlen(colon) > 1) {344 if (extName) {345 *extName = psStringCopy(colon + 1);346 }347 }348 } else {349 filename = psMemIncrRefCounter(filenameExt);350 }351 352 return filename;353 }354 355 // Read a mask into the FPA356 bool pmFPAReadMask(pmFPA *fpa, // FPA to read into357 psFits *source // Source FITS file (for the original data)358 )359 {360 const psMetadata *camera = fpa->camera; // Camera configuration for FPA361 bool mdok = false; // Status of MD lookup362 363 // Get the required information from the camera configuration364 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data365 if (! mdok || ! supps) {366 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");367 return false;368 }369 psString sourceType = psMetadataLookupStr(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE370 if (! mdok || strlen(sourceType) <= 0) {371 psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "372 "configuration!\n");373 return false;374 }375 psString name = psMetadataLookupStr(&mdok, supps, "MASK.NAME"); // Name of mask376 if (! mdok || strlen(sourceType) <= 0) {377 psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "378 "configuration!\n");379 return false;380 }381 382 // Go through the FPA to each cell/readout to get the mask383 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the mask384 psArray *chips = fpa->chips; // Array of chips385 for (int chipNum = 0; chipNum < chips->n; chipNum++) {386 pmChip *chip = chips->data[chipNum]; // The current chip of interest387 if (chip->process && !chip->data_exists) {388 if (chip->hdu) {389 hdu = chip->hdu;390 }391 psArray *cells = chip->cells; // Array of cells392 for (int cellNum = 0; cellNum < cells->n; cellNum++) {393 pmCell *cell = cells->data[cellNum]; // The current cell of interest394 if (cell->process && !cell->data_exists) {395 if (cell->hdu) {396 hdu = cell->hdu;397 }398 399 // Now, need to find out where to get the pixels400 psFits *maskSource = psMemIncrRefCounter(source); // Source of mask image401 if (strcasecmp(sourceType, "FILE") == 0) {402 // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"403 psString extname = NULL; // Extension name404 psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename405 406 psFree(maskSource);407 maskSource = psFitsOpen(filename, "r");408 if (extname) {409 if (! psFitsMoveExtName(maskSource, extname)) {410 psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read mask.\n",411 extname);412 return false;413 }414 }415 psFree(filename);416 psFree(extname);417 } else if (strncasecmp(sourceType, "EXT", 3) == 0) {418 // Source is an extension in the original file419 psString extname = p_pmFPATranslateName(name, cell);420 psFitsMoveExtName(maskSource, extname);421 }422 423 // We've arrived where the pixels are. Now we need to read them in.424 psMetadata *header = psFitsReadHeader(NULL, maskSource); // The header425 bool mdStatus = false;426 int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS");427 if (!mdStatus) {428 psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for "429 "mask (%s)!\n", name);430 }431 if (nAxis != 2 && nAxis != 3) {432 psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into "433 "a single image anyway.\n");434 }435 436 int numPlanes = 1; // Number of planes437 if (nAxis == 3) {438 numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3");439 if (!mdStatus) {440 psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n");441 // Try to proceed by taking only the first plane442 numPlanes = 1;443 }444 if (numPlanes != 1 && numPlanes != cell->readouts->n) {445 psError(PS_ERR_IO, false, "Number of masks (%d) does not match number of "446 "readouts (%d)\n", numPlanes, cell->readouts->n);447 // Try to proceed by taking only the first plane448 numPlanes = 1;449 }450 }451 452 hdu->masks = psArrayAlloc(hdu->images->n);453 454 // Read each plane into the array455 psArray *readouts = cell->readouts; // The array of readouts456 for (int i = 0; i < hdu->masks->n; i++) {457 psImage *mask = NULL; // The mask to be added458 if (i < numPlanes) {459 // Read the mask from the file460 psTrace(__func__, 9, "Reading plane %d\n", i);461 psRegion region = {0, 0, 0, 0};462 mask = psFitsReadImage(NULL, maskSource, region, i);463 } else {464 // One mask in the file is provided for all planes in the original image465 psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i);466 psImage *original = hdu->masks->data[0];467 mask = psImageCopy(NULL, original, original->type.type);468 }469 hdu->masks->data[0] = mask;470 pmReadout *readout = readouts->data[i];471 readout->mask = mask;472 // Check the dimensions473 // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size?474 if (mask->numCols < readout->image->numCols ||475 mask->numRows < readout->image->numRows) {476 psError(PS_ERR_IO, false, "Mask size (%dx%d) not compatible with image (%dx%d)\n",477 mask->numCols, mask->numRows, readout->image->numCols,478 readout->image->numRows);479 return false;480 }481 } // Iterating over readouts482 psFree(maskSource);483 } // Valid cells484 } // Iterating over cells485 } // Valid chips486 } // Iterating over chips487 488 return true;489 }490 491 492 // Read a mask into the FPA493 // This is just a copy of the above pmFPAReadMask, replacing "mask" with "weight" throughout.494 bool pmFPAReadWeight(pmFPA *fpa, // FPA to read into495 psFits *source // Source FITS file (for the original data)496 )497 {498 const psMetadata *camera = fpa->camera; // Camera configuration for FPA499 bool mdok = false; // Status of MD lookup500 501 // Get the required information from the camera configuration502 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data503 if (! mdok || ! supps) {504 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");505 return false;506 }507 psString sourceType = psMetadataLookupStr(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE508 if (! mdok || strlen(sourceType) <= 0) {509 psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "510 "configuration!\n");511 return false;512 }513 psString name = psMetadataLookupStr(&mdok, supps, "WEIGHT.NAME"); // Name of weight514 if (! mdok || strlen(sourceType) <= 0) {515 psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "516 "configuration!\n");517 return false;518 }519 520 // Go through the FPA to each cell/readout to get the weight521 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the weight522 psArray *chips = fpa->chips; // Array of chips523 for (int chipNum = 0; chipNum < chips->n; chipNum++) {524 pmChip *chip = chips->data[chipNum]; // The current chip of interest525 if (chip->process && !chip->data_exists) {526 if (chip->hdu) {527 hdu = chip->hdu;528 }529 psArray *cells = chip->cells; // Array of cells530 for (int cellNum = 0; cellNum < cells->n; cellNum++) {531 pmCell *cell = cells->data[cellNum]; // The current cell of interest532 if (cell->process && !cell->data_exists) {533 if (cell->hdu) {534 hdu = cell->hdu;535 }536 537 // Now, need to find out where to get the pixels538 psFits *weightSource = psMemIncrRefCounter(source); // Source of weight image539 if (strcasecmp(sourceType, "FILE") == 0) {540 // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"541 psString extname = NULL; // Extension name542 psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename543 544 psFree(weightSource);545 weightSource = psFitsOpen(filename, "r");546 if (extname) {547 if (! psFitsMoveExtName(weightSource, extname)) {548 psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read "549 "weight.\n", extname);550 return false;551 }552 }553 psFree(filename);554 psFree(extname);555 } else if (strncasecmp(sourceType, "EXT", 3) == 0) {556 // Source is an extension in the original file557 psString extname = p_pmFPATranslateName(name, cell);558 psFitsMoveExtName(weightSource, extname);559 }560 561 // We've arrived where the pixels are. Now we need to read them in.562 psMetadata *header = psFitsReadHeader(NULL, weightSource); // The header563 bool mdStatus = false;564 int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS");565 if (!mdStatus) {566 psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for "567 "weight (%s)!\n", name);568 }569 if (nAxis != 2 && nAxis != 3) {570 psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into "571 "a single image anyway.\n");572 }573 574 int numPlanes = 1; // Number of planes575 if (nAxis == 3) {576 numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3");577 if (!mdStatus) {578 psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n");579 // Try to proceed by taking only the first plane580 numPlanes = 1;581 }582 if (numPlanes != 1 && numPlanes != cell->readouts->n) {583 psError(PS_ERR_IO, false, "Number of weights (%d) does not match number of "584 "readouts (%d)\n", numPlanes, cell->readouts->n);585 // Try to proceed by taking only the first plane586 numPlanes = 1;587 }588 }589 590 hdu->weights = psArrayAlloc(hdu->images->n);591 592 // Read each plane into the array593 psArray *readouts = cell->readouts; // The array of readouts594 for (int i = 0; i < hdu->weights->n; i++) {595 psImage *weight = NULL; // The weight to be added596 if (i < numPlanes) {597 // Read the weight from the file598 psTrace(__func__, 9, "Reading plane %d\n", i);599 psRegion region = {0, 0, 0, 0};600 weight = psFitsReadImage(NULL, weightSource, region, i);601 } else {602 // One weight in the file is provided for all planes in the original image603 psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i);604 psImage *original = hdu->weights->data[0];605 weight = psImageCopy(NULL, original, original->type.type);606 }607 hdu->weights->data[0] = weight;608 pmReadout *readout = readouts->data[i];609 readout->weight = weight;610 // Check the dimensions611 // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size?612 if (weight->numCols < readout->image->numCols ||613 weight->numRows < readout->image->numRows) {614 psError(PS_ERR_IO, false, "Weight size (%dx%d) not compatible with image "615 "(%dx%d)\n", weight->numCols, weight->numRows, readout->image->numCols,616 readout->image->numRows);617 return false;618 }619 } // Iterating over readouts620 psFree(weightSource);621 } // Valid cells622 } // Iterating over cells623 } // Valid chips624 } // Iterating over chips625 626 return true;627 }628 629 630 #endif -
branches/rel10_ifa/psModules/src/astrom/pmFPAWrite.c
r6734 r6840 77 77 } 78 78 79 80 81 82 #if 083 static bool writeHDU(psFits *fits, // FITS file to which to write84 pmHDU *hdu // Pixel data to write85 )86 {87 bool status = true; // Status of write, to return88 for (int i = 0; i < hdu->images->n; i++) {89 status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);90 // XXX: Insert here the writing on mask and weight images91 }92 93 return status;94 }95 96 97 bool pmFPAWrite(psFits *fits, // FITS file to which to write98 pmFPA *fpa, // FPA to write99 psDB *db // Database to update100 )101 {102 bool status = true; // Status of writing, to return103 104 psTrace(__func__, 1, "Writing FPA...\n");105 106 psTrace(__func__, 7, "Outgesting FPA concepts...\n");107 pmConceptsWriteFPA(fpa, db);108 109 // Write the primary header110 if (fpa->phu) {111 status &= psFitsMoveExtNum(fits, 0, false);112 status &= psFitsWriteHeader(fpa->phu, fits);113 }114 115 psArray *chips = fpa->chips; // Array of component chips116 for (int i = 0; i < chips->n; i++) {117 pmChip *chip = chips->data[i]; // The component chip118 if (chip && chip->exists && chip->process) {119 psTrace(__func__, 1, "Writing out chip %d...\n", i);120 121 pmConceptsWriteChip(chip, db);122 123 psArray *cells = chip->cells; // Array of component cells124 for (int j = 0; j < cells->n; j++) {125 pmCell *cell = cells->data[j]; // The component cell126 if (cell && cell->exists && cell->process) {127 psTrace(__func__, 2, "Writing out cell, %d...\n", j);128 129 pmConceptsWriteCell(cell, db);130 131 if (cell->hdu && strlen(cell->hdu->extname) > 0) {132 status &= writeHDU(fits, cell->hdu);133 }134 }135 }136 137 if (chip->hdu && strlen(chip->hdu->extname) > 0) {138 status &= writeHDU(fits, chip->hdu);139 }140 }141 142 }143 144 if (fpa->hdu && strlen(fpa->hdu->extname) > 0) {145 status &= writeHDU(fits, fpa->hdu);146 }147 148 psTrace(__func__, 1, "Done writing FPA...\n");149 150 return status;151 }152 153 154 bool pmFPAWriteMask(pmFPA *fpa, // FPA containing mask to write155 psFits *fits // FITS file for image156 )157 {158 const psMetadata *camera = fpa->camera; // Camera configuration for FPA159 bool mdok = false; // Status of MD lookup160 161 // Get the required information from the camera configuration162 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data163 if (! mdok || ! supps) {164 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");165 return false;166 }167 psString sourceType = psMetadataLookupStr(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE168 if (! mdok || strlen(sourceType) <= 0) {169 psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "170 "configuration!\n");171 return false;172 }173 psString name = psMetadataLookupStr(&mdok, supps, "MASK.NAME"); // Name of mask174 if (! mdok || strlen(sourceType) <= 0) {175 psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "176 "configuration!\n");177 return false;178 }179 180 // Go through the FPA to each cell/readout to get the mask181 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the mask182 psArray *chips = fpa->chips; // Array of chips183 for (int chipNum = 0; chipNum < chips->n; chipNum++) {184 pmChip *chip = chips->data[chipNum]; // The current chip of interest185 if (chip->exists && chip->process) {186 if (chip->hdu) {187 hdu = chip->hdu;188 }189 psArray *cells = chip->cells; // Array of cells190 for (int cellNum = 0; cellNum < cells->n; cellNum++) {191 pmCell *cell = cells->data[cellNum]; // The current cell of interest192 if (cell->exists && cell->process) {193 if (cell->hdu) {194 hdu = cell->hdu;195 }196 197 // Now, need to find out where to write the pixels198 psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image199 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name200 if (strcasecmp(sourceType, "FILE") == 0) {201 // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"202 psString extname = NULL; // Extension name203 psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename204 psFree(maskDest);205 maskDest = psFitsOpen(filename, "rw");206 if (extname) {207 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);208 }209 psFree(filename);210 psFree(extname);211 } else if (strncasecmp(sourceType, "EXT", 3) == 0) {212 // Source is an extension in the original file213 psString extname = p_pmFPATranslateName(name, cell);214 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);215 psFree(extname);216 }217 218 // We've arrived where the pixels are. Now we need to write them out.219 psArray *readouts = cell->readouts; // The array of readouts220 for (int readNum = 0; readNum < readouts->n; readNum++) {221 pmReadout *readout = readouts->data[readNum]; // The readout of interest222 if (! readout->mask) {223 psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",224 chipNum, cellNum, readNum);225 } else {226 // XXX: Need to add the extname to the existing header227 if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) {228 psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",229 readNum, hdu->extname);230 return false;231 }232 }233 } // Iterating over readouts234 psFree(header);235 psFree(maskDest);236 } // Valid cells237 } // Iterating over cells238 } // Valid chips239 } // Iterating over chips240 241 return true;242 }243 244 245 bool pmFPAWriteWeight(pmFPA *fpa, // FPA containing mask to write246 psFits *fits // FITS file for image247 )248 {249 const psMetadata *camera = fpa->camera; // Camera configuration for FPA250 bool mdok = false; // Status of MD lookup251 252 // Get the required information from the camera configuration253 psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data254 if (! mdok || ! supps) {255 psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");256 return false;257 }258 psString sourceType = psMetadataLookupStr(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE259 if (! mdok || strlen(sourceType) <= 0) {260 psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "261 "configuration!\n");262 return false;263 }264 psString name = psMetadataLookupStr(&mdok, supps, "WEIGHT.NAME"); // Name of weight265 if (! mdok || strlen(sourceType) <= 0) {266 psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "267 "configuration!\n");268 return false;269 }270 271 // Go through the FPA to each cell/readout to get the weight272 p_pmHDU *hdu = fpa->hdu; // The HDU into which we will read the weight273 psArray *chips = fpa->chips; // Array of chips274 for (int chipNum = 0; chipNum < chips->n; chipNum++) {275 pmChip *chip = chips->data[chipNum]; // The current chip of interest276 if (chip->exists && chip->process) {277 if (chip->hdu) {278 hdu = chip->hdu;279 }280 psArray *cells = chip->cells; // Array of cells281 for (int cellNum = 0; cellNum < cells->n; cellNum++) {282 pmCell *cell = cells->data[cellNum]; // The current cell of interest283 if (cell->exists && cell->process) {284 if (cell->hdu) {285 hdu = cell->hdu;286 }287 288 // Now, need to find out where to write the pixels289 psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image290 psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name291 if (strcasecmp(sourceType, "FILE") == 0) {292 // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"293 psString extname = NULL; // Extension name294 psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename295 296 psFree(weightDest);297 weightDest = psFitsOpen(filename, "rw");298 if (extname) {299 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);300 }301 psFree(filename);302 psFree(extname);303 } else if (strncasecmp(sourceType, "EXT", 3) == 0) {304 // Source is an extension in the original file305 psString extname = p_pmFPATranslateName(name, cell);306 psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);307 psFree(extname);308 }309 310 // We've arrived where the pixels are. Now we need to write them out.311 psArray *readouts = cell->readouts; // The array of readouts312 for (int readNum = 0; readNum < readouts->n; readNum++) {313 pmReadout *readout = readouts->data[readNum]; // The readout of interest314 if (! readout->weight) {315 psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",316 chipNum, cellNum, readNum);317 } else {318 if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) {319 psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",320 readNum, hdu->extname);321 return false;322 }323 }324 } // Iterating over readouts325 psFree(header);326 psFree(weightDest);327 } // Valid cells328 } // Iterating over cells329 } // Valid chips330 } // Iterating over chips331 332 return true;333 }334 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
