Changeset 4694 for trunk/archive/scripts/src/pmFPARead.c
- Timestamp:
- Aug 1, 2005, 2:21:14 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/scripts/src/pmFPARead.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/scripts/src/pmFPARead.c
r4386 r4694 5 5 #include "papStuff.h" 6 6 #include "papFocalPlane.h" 7 #include "pmFPARead.h" 7 8 8 9 // NOTE: Need to deal with header inheritance … … 11 12 // File-static functions 12 13 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 13 14 // Copy metadata elements into another metadata15 static void metadataCopy(psMetadata *to, // Metadata to which to copy16 psMetadata *from // Metdata from which to copy17 )18 {19 psMetadataIterator *mdIter = psMetadataIteratorAlloc(from, PS_LIST_HEAD, NULL); // Iterator for the MD20 psMetadataItem *item = NULL; // Item from the metadata21 while (item = psMetadataGetAndIncrement(mdIter)) {22 psTrace(__func__, 9, "Adding %s to metadata...\n", item->name);23 if (! psMetadataAddItem(to, item, PS_LIST_TAIL, PS_META_REPLACE)) {24 psLogMsg(__func__, PS_LOG_WARN, "Unable to add item (%s: %s) to metadata: ignored\n", item->name,25 item->comment);26 }27 }28 psFree(mdIter);29 }30 31 // Read data for a particular cell from the camera configuration32 static psMetadata *getCellData(const psMetadata *camera, // The camera configuration33 const char *cellName // The name of the cell34 )35 {36 bool status = true; // Result of MD lookup37 psMetadata *cells = psMetadataLookupMD(&status, camera, "CELLS"); // The CELLS38 if (! status) {39 psError(PS_ERR_IO, false, "Unable to determine CELLS of camera.\n");40 return NULL;41 }42 43 psMetadata *cellData = psMetadataLookupMD(&status, cells, cellName); // The data for the particular cell44 if (! status) {45 psError(PS_ERR_IO, false, "Unable to find specs for cell %s: ignored\n", cellName);46 return NULL;47 } else {48 return cellData;49 }50 }51 14 52 15 // Read a FITS extension into a chip … … 57 20 ) 58 21 { 22 psTrace(__func__, 7, "Moving to extension %s...\n", extName); 59 23 if (strncmp(extName, "PHU", 3) == 0) { 60 24 if (!psFitsMoveExtNum(fits, 0, false)) { … … 66 30 return false; 67 31 } 32 psTrace(__func__, 7, "Reading header....\n"); 68 33 *header = psFitsReadHeader(*header, fits); 34 if (! *header) { 35 psError(PS_ERR_IO, false, "Unable to read FITS header!\n"); 36 return false; 37 } 38 39 psTrace(__func__, 7, "Checking NAXIS....\n"); 69 40 bool mdStatus = false; 70 41 int nAxis = psMetadataLookupS32(&mdStatus, *header, "NAXIS"); … … 77 48 "anyway.\n"); 78 49 } 50 psTrace(__func__, 9, "NAXIS = %d\n", nAxis); 79 51 80 52 int numPlanes = 1; // Number of planes … … 89 61 90 62 // Read each plane into the array 63 psTrace(__func__, 7, "Reading %d planes into array....\n", numPlanes); 91 64 *images = psArrayAlloc(numPlanes); // Array of images 92 65 for (int i = 0; i < numPlanes; i++) { 93 (*images)->data[i] = psFitsReadImage((*images)->data[i], fits, region, 0); 66 psTrace(__func__, 9, "Reading plane %d\n", i); 67 psMemCheckCorruption(true); 68 (*images)->data[i] = psFitsReadImage(NULL, fits, region, i); 69 psTrace(__func__, 10, "Done\n"); 94 70 if (! (*images)->data[i]) { 95 71 psError(PS_ERR_IO, false, "Unable to read image for extension %s, plane %d\n", extName, i); … … 119 95 region.y1); 120 96 numFound++; 97 // Convert from FITS standard to PS standard 98 region.x0 -= 1; 99 region.y0 -= 1; 100 // We don't touch the x1 and y1 values because they aren't included by psImageSubset. 121 101 psImage *subImage = psImageSubset(image, region); 122 102 psListAdd(list, PS_LIST_TAIL, subImage); … … 166 146 // We have the region: [x0:x1,y0:y1] 167 147 psRegion region = psRegionFromString(regionString); 148 // Convert from FITS standard to PS standard 149 region.x0 -= 1; 150 region.y0 -= 1; 151 // We don't touch the x1 and y1 values because they aren't included by psImageSubset. 168 152 psImage *subImage = psImageSubset(image, region); 169 153 psListAdd(subImages, PS_LIST_TAIL, subImage); … … 251 235 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 252 236 253 papFPA *pmFPARead(psFits *fits, // A FITS file 254 const psMetadata *camera, // The camera configuration 255 psDB *db // Database 256 ) 257 { 258 papFPA *fpa = papFPAAlloc(camera, db); // The FPA to fill out 259 bool mdStatus = true; // Status from metadata lookups 260 const char *phuType = psMetadataLookupString(&mdStatus, camera, "PHU"); // What is the PHU? 261 const char *extType = psMetadataLookupString(&mdStatus, camera, "EXTENSIONS"); // What's in the extensions? 262 if (strncmp(phuType, "FPA", 3) == 0) { 263 // The FITS file contains an entire FPA 264 265 psMetadata *contents = psMetadataLookupMD(&mdStatus, camera, "CONTENTS"); // The CONTENTS 266 if (! mdStatus) { 267 psError(PS_ERR_IO, false, "Unable to determine CONTENTS of camera.\n"); 268 psFree(fpa); 269 return NULL; 270 } 271 272 // Set up iteration over the contents 273 psMetadataIterator *contentsIter = psMetadataIteratorAlloc(contents, PS_LIST_HEAD, NULL); 274 psMetadataItem *contentItem = NULL; // Item from the metadata 275 276 if (strncmp(extType, "CHIP", 4) == 0) { 277 // Extensions are chips; Content contains a list of cells 278 while (contentItem = psMetadataGetAndIncrement(contentsIter)) { 279 const char *extName = contentItem->name; // The name of the extension 280 papChip *chip = papChipAlloc(fpa, extName); // The chip 281 readExtension(&(chip->pixels), &(chip->header), fits, extName); 282 if (contentItem->type != PS_META_STR) { 283 psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n", 284 contentItem->type); 285 } else { 286 const char *content = contentItem->data.V; // The content of the extension 287 psTrace(__func__, 7, "Content of %s is: %s\n", extName, content); 288 psList *cellNames = papSplit(content, " ,"); // A list of the component cells 289 psListIterator *cellNamesIter = psListIteratorAlloc(cellNames, PS_LIST_HEAD, NULL); 290 char *cellName = NULL; // The name of a cell 291 while (cellName = psListGetAndIncrement(cellNamesIter)) { 292 // Get the cell data 293 papCell *cell = papCellAlloc(chip, cellName, ((psArray*)chip->pixels)->n); // The cell 294 psMetadata *cellData = getCellData(camera, cellName); 295 metadataCopy(cell->values, cellData); 296 portionCell(cell, chip->pixels, chip->header); 297 } 298 psFree(cellNamesIter); 299 psFree(cellNames); 237 bool pmFPARead(papFPA *fpa, // FPA to read into 238 psFits *fits // FITS file from which to read 239 ) 240 { 241 psArray *pixels = NULL; // Current pixels from FITS file (array of images) 242 psMetadata *header = NULL; // Current header from FITS file 243 244 psTrace(__func__, 1, "Working on FPA...\n"); 245 if (fpa->extname) { 246 psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", fpa->extname); 247 if (! readExtension(&fpa->pixels, &fpa->header, fits, fpa->extname)) { 248 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", fpa->extname); 249 return false; 250 } 251 pixels = fpa->pixels; 252 header = fpa->header; 253 } 254 255 psArray *chips = fpa->chips; // Array of chips 256 // Iterate over the FPA 257 for (int i = 0; i < chips->n; i++) { 258 papChip *chip = chips->data[i]; // The chip 259 psTrace(__func__, 2, "Working on chip %d...\n", i); 260 if (chip->extname) { 261 psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", chip->extname, i); 262 if (! readExtension(&chip->pixels, &chip->header, fits, chip->extname)) { 263 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", chip->extname); 264 return false; 265 } 266 pixels = chip->pixels; 267 header = chip->header; 268 } 269 // Iterate over the chip 270 psArray *cells = chip->cells; // Array of cells 271 for (int j = 0; j < cells->n; j++) { 272 papCell *cell = cells->data[j]; // The cell 273 psTrace(__func__, 3, "Working on cell %d...\n", j); 274 if (cell->extname) { 275 psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", cell->extname, j); 276 if (! readExtension(&cell->pixels, &cell->header, fits, cell->extname)) { 277 psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", cell->extname); 278 return false; 300 279 } 301 } 302 303 } else if (strncmp(extType, "CELL", 4) == 0) { 304 // Extensions are cells; Content contains a chip name and cell type 305 psMetadata *chipNumbers = psMetadataAlloc(); // Given a chip name, holds the chip number 306 while (contentItem = psMetadataGetAndIncrement(contentsIter)) { 307 const char *extName = contentItem->name; // The name of the extension 308 psTrace(__func__, 1, "Getting %s....\n", extName); 309 310 if (contentItem->type != PS_META_STR) { 311 psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n", 312 contentItem->type); 313 } else { 314 const char *content = contentItem->data.V; // The content of the extension 315 psList *contents = papSplit(content, ": "); // Split the name from the type 316 if (contents->size != 2) { 317 psLogMsg(__func__, PS_LOG_WARN, "Unable to read contents of %s: ignored.\n", extName); 318 } else { 319 const char *chipName = psListGet(contents, 0); // The name of the chip 320 const char *cellType = psListGet(contents, 1); // The type of cell 321 psTrace(__func__, 7, "Extension is cell of type %s, from chip %s\n", cellType, 322 chipName); 323 324 papChip *chip = psMetadataLookupChip(&mdStatus, fpa->chips, chipName); // The chip 325 if (! mdStatus && ! chip) { 326 chip = papChipAlloc(fpa, chipName); 327 } 328 // The cell 329 psArray *images = NULL; 330 psMetadata *header = NULL; 331 psTrace(__func__, 7, "Reading extension %s\n", extName); 332 readExtension(&images, &header, fits, extName); 333 psTrace(__func__, 7, "Allocating cell %s\n", cellType); 334 papCell *cell = papCellAlloc(chip, cellType, images->n); // The cell 335 cell->pixels = images; 336 cell->header = header; 337 psMetadata *cellData = getCellData(camera, cellType); 338 metadataCopy(cell->values, cellData); 339 psTrace(__func__, 7, "Portioning cell....\n"); 340 portionCell(cell, cell->pixels, cell->header); 341 psTrace(__func__, 7, "Done.\n"); 342 } 343 } 344 } 345 346 } else if (strncmp(extType, "NONE", 4) == 0) { 347 // No extensions; Content contains metadata, each entry is a chip with its component cells 348 readExtension(&(fpa->pixels), &(fpa->header), fits, "PHU"); 349 while (contentItem = psMetadataGetAndIncrement(contentsIter)) { 350 const char *chipName = contentItem->name; // The name of the chip 351 352 if (contentItem->type != PS_META_STR) { 353 psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n", 354 contentItem->type); 355 } else { 356 const char *content = contentItem->data.V; // The content of the extension 357 papChip *chip = papChipAlloc(fpa, content); // The chip 358 psList *cellNames = papSplit(content, ", "); // Split the list of cells 359 psListIterator *cellNamesIter = psListIteratorAlloc(cellNames, PS_LIST_HEAD, false); 360 char *cellName = NULL; // Name of the cell 361 while (cellName = psListGetAndIncrement(cellNamesIter)) { 362 papCell *cell = papCellAlloc(chip, cellName, ((psArray*)fpa->pixels)->n); // The cell 363 psMetadata *cellData = getCellData(camera, cellName); 364 metadataCopy(cell->values, cellData); 365 portionCell(cell, fpa->pixels, fpa->header); 366 } 367 psFree(cellNamesIter); 368 } 369 } 370 371 } else { 372 psError(PS_ERR_IO, false, "EXTENSIONS in camera definition is not CHIP, CELL or NONE.\n"); 373 psFree(fpa); 374 return NULL; 375 } // Type of extension 376 377 psFree(contentsIter); 378 379 } else if (strncmp(phuType, "CHIP", 4) == 0) { 380 // The FITS file contains a single chip only 381 papChip *chip = papChipAlloc(fpa, "CHIP"); // The chip 382 383 if (strncmp(extType, "NONE", 4) == 0) { 384 // There are no extensions --- only the PHU 385 readExtension(&(fpa->pixels), &(fpa->header), fits, "PHU"); 386 387 const char *contents = psMetadataLookupString(&mdStatus, camera, "CONTENTS"); 388 if (! mdStatus) { 389 psError(PS_ERR_IO, false, "Unable to determine CONTENTS of camera.\n"); 390 psFree(fpa); 391 return NULL; 392 } 393 psList *cellNames = papSplit(contents, " ,"); // Names of cells 394 psListIterator *cellIter = psListIteratorAlloc(cellNames, PS_LIST_HEAD, false); // Iterator 395 const char *cellName = NULL; 396 while (cellName = psListGetAndIncrement(cellIter)) { 397 papCell *cell = papCellAlloc(chip, cellName, ((psArray*)fpa->pixels)->n); // The cell 398 psMetadata *cellData = getCellData(camera, cellName); 399 metadataCopy(cell->values, cellData); 400 portionCell(cell, fpa->pixels, fpa->header); 401 } 402 psFree(cellIter); 403 } else if (strncmp(extType, "CELL", 4) == 0) { 404 // Extensions are cells 405 psMetadata *contents = psMetadataLookupMD(&mdStatus, camera, "CONTENTS"); // The CONTENTS 406 if (! mdStatus) { 407 psError(PS_ERR_IO, false, "Unable to determine CONTENTS of camera.\n"); 408 psFree(fpa); 409 return NULL; 410 } 411 412 if (strncmp(extType, "CELL", 4) != 0) { 413 psLogMsg(__func__, PS_LOG_WARN, "EXTENSIONS in camera definition is %s, but PHU is CHIP.\n" 414 "EXTENSIONS assumed to be CELL.\n", extType); 415 } 416 417 // Iterate through the contents 418 psMetadataIterator *contentsIter = psMetadataIteratorAlloc(contents, PS_LIST_HEAD, NULL); 419 psMetadataItem *contentItem = NULL; // Item from metadata 420 while (contentItem = psMetadataGetAndIncrement(contentsIter)) { 421 const char *extName = contentItem->name; // The name of the extension 422 423 psMemCheckCorruption(true); 424 425 psTrace(__func__, 1, "Getting %s....\n", extName); 426 427 // Content is a cell type 428 if (contentItem->type != PS_META_STR) { 429 psLogMsg(__func__, PS_LOG_WARN, 430 "CONTENT metadata for extension %s is not of type string, but %x --- ignored\n", 431 extName, contentItem->type); 432 } else { 433 const char *cellType = contentItem->data.V; // The type of cell 434 psTrace(__func__, 2, "Cell type is %s\n", cellType); 435 psArray *images = NULL; 436 psMetadata *header = NULL; 437 readExtension(&images, &header, fits, extName); 438 papCell *cell = papCellAlloc(chip, extName, images->n); // The cell 439 cell->pixels = images; 440 cell->header = header; 441 psMetadata *cellData = getCellData(camera, cellType); 442 metadataCopy(cell->values, cellData); 443 portionCell(cell, cell->pixels, cell->header); 444 } 445 } // Iterating through contents 446 psFree(contentsIter); 447 448 } else { 449 psError(PS_ERR_IO, false, "EXTENSIONS in camera definition is neither CELL or NONE.\n"); 450 psFree(fpa); 451 return NULL; 452 } 453 454 } else { 455 psError(PS_ERR_IO, true, 456 "The PHU type specified in the camera configuration (%s) is not FPA or CHIP.\n", 457 phuType); 458 psFree(fpa); 459 return NULL; 460 } 461 462 return fpa; 463 } 464 465 void pmFPAPrint(papFPA *fpa // FPA to print 466 ) 467 { 468 // Print out the focal plane 469 psMetadataIterator *fpaIter = psMetadataIteratorAlloc(fpa->chips, PS_LIST_HEAD, NULL); // Iterator for FPA 470 psMetadataItem *fpaItem = NULL; // Item from metadata 471 psTrace("pmFPARead", 0, "FPA:\n"); 472 if (fpa->pixels) { 473 psTrace("pmFPARead", 1, "---> FPA contains pixels.\n"); 474 } 475 while (fpaItem = psMetadataGetAndIncrement(fpaIter)) { 476 if (fpaItem->type != PS_META_CHIP) { 477 psError(PS_ERR_IO, false, "FPA content is not a chip.\n"); 478 } else { 479 psTrace("pmFPARead", 1, "Chip: %s\n", fpaItem->name); 480 papChip *chip = fpaItem->data.V; // The chip 481 if (chip->pixels) { 482 psTrace("pmFPARead", 2, "---> Chip contains pixels.\n"); 483 } 484 // Iterator for chip 485 psMetadataIterator *chipIter = psMetadataIteratorAlloc(chip->cells, PS_LIST_HEAD, NULL); 486 psMetadataItem *chipItem = NULL; // Item from metadata 487 while (chipItem = psMetadataGetAndIncrement(chipIter)) { 488 if (chipItem->type != PS_META_CELL) { 489 psError(PS_ERR_IO, false, "Chip content is not a cell.\n"); 490 } else { 491 psTrace("pmFPARead", 2, "Cell: %s\n", chipItem->name); 492 papCell *cell = chipItem->data.V; // The cell 493 if (cell->pixels) { 494 psTrace("pmFPARead", 3, "---> Cell contains pixels.\n"); 495 } 496 // Iterator for cell values 497 psMetadataIterator *cellIter = psMetadataIteratorAlloc(cell->values, PS_LIST_HEAD, NULL); 498 psMetadataItem *cellItem = NULL; // Item from metadata 499 while (cellItem = psMetadataGetAndIncrement(cellIter)) { 500 switch(cellItem->type) { 501 case PS_META_STR: 502 psTrace("pmFPARead", 3, "%s: %s\n", cellItem->name, cellItem->data.V); 503 break; 504 case PS_META_F32: 505 psTrace("pmFPARead", 3, "%s: %f\n", cellItem->name, cellItem->data.F32); 506 break; 507 case PS_META_S32: 508 psTrace("pmFPARead", 3, "%s: %d\n", cellItem->name, cellItem->data.S32); 509 break; 510 case PS_META_META: 511 psTrace("pmFPARead", 3, "%s:\n", cellItem->name); 512 psMetadataPrint(cellItem->data.V, 4); 513 break; 514 default: 515 psError(PS_ERR_IO, false, "Unknown type for cell (%x).\n", cellItem->type); 516 } 517 } // Iterating through cell 518 psFree(cellIter); 519 520 // Iterator for cell 521 psTrace("pmFPARead", 3, "Readouts:\n"); 522 psArray *readouts = cell->readouts; // The readouts 523 for (int i = 0; i < readouts->n; i++) { 524 papReadout *readout = readouts->data[i]; // The readout 525 psImage *image = readout->image; // The image 526 psTrace("pmFPARead", 4, "Image: [%d:%d,%d:%d]\n", image->col0, image->col0 + 527 image->numCols, image->row0, image->row0 + image->numRows); 528 psList *overscans = readout->overscans; // The list of overscans 529 psListIterator *overscansIter = psListIteratorAlloc(overscans, PS_LIST_HEAD, false); 530 while (image = psListGetAndIncrement(overscansIter)) { 531 psTrace("pmFPARead", 4, "Overscan: [%d:%d,%d:%d]\n", image->col0, image->col0 + 532 image->numCols, image->row0, image->row0 + image->numRows); 533 } 534 psFree(overscansIter); 535 } 536 } 537 } // Iterating through chip 538 psFree(chipIter); 539 } 540 } // Iterating through FPA 541 psFree(fpaIter); 542 } 280 pixels = cell->pixels; 281 header = cell->header; 282 } 283 284 psTrace(__func__, 5, "Allocating readouts and extracting overscans etc for chip %d cell %d...\n", 285 i, j); 286 portionCell(cell, pixels, header); 287 } 288 } 289 290 return true; 291 }
Note:
See TracChangeset
for help on using the changeset viewer.
