Changeset 6840 for branches/rel10_ifa/psModules/src/astrom/pmFPARead.c
- Timestamp:
- Apr 11, 2006, 6:11:54 PM (20 years ago)
- File:
-
- 1 edited
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
Note:
See TracChangeset
for help on using the changeset viewer.
