Changeset 27177 for trunk/psModules/src/objects/pmPSF_IO.c
- Timestamp:
- Mar 3, 2010, 5:11:47 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/objects/pmPSF_IO.c (modified) (32 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/objects/pmPSF_IO.c
r25979 r27177 122 122 if (view->chip == -1) { 123 123 if (!pmPSFmodelWriteFPA(fpa, view, file, config)) { 124 psError( PS_ERR_IO, false, "Failed to write PSF for fpa");124 psError(psErrorCodeLast(), false, "Failed to write PSF for fpa"); 125 125 return false; 126 126 } … … 135 135 if (view->cell == -1) { 136 136 if (!pmPSFmodelWriteChip (chip, view, file, config)) { 137 psError( PS_ERR_IO, false, "Failed to write PSF for chip");137 psError(psErrorCodeLast(), false, "Failed to write PSF for chip"); 138 138 return false; 139 139 } … … 141 141 } 142 142 143 psError(P S_ERR_IO, false, "PSF must be written at the chip level");143 psError(PM_ERR_CONFIG, true, "PSF must be written at the chip level"); 144 144 return false; 145 145 } … … 158 158 thisView->chip = i; 159 159 if (!pmPSFmodelWriteChip (chip, thisView, file, config)) { 160 psError( PS_ERR_IO, false, "Failed to write PSF for %dth chip", i);160 psError(psErrorCodeLast(), false, "Failed to write PSF for %dth chip", i); 161 161 psFree(thisView); 162 162 return false; … … 173 173 PS_ASSERT_PTR_NON_NULL(chip, false); 174 174 175 if (!pmPSFmodelWrite (chip->analysis, view, file, config)) {176 psError( PS_ERR_IO, false, "Failed to write PSF for chip");175 if (!pmPSFmodelWrite(chip->analysis, view, file, config)) { 176 psError(psErrorCodeLast(), false, "Failed to write PSF for chip"); 177 177 return false; 178 178 } … … 197 197 char *headName, *tableName, *residName; 198 198 199 if (!analysis) return false; 199 if (!analysis) { 200 psError(PM_ERR_PROG, true, "No analysis metadata for chip."); 201 return false; 202 } 200 203 201 204 // select the current recipe 202 205 psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, "PSPHOT"); 203 206 if (!recipe) { 204 psError(P S_ERR_UNKNOWN, false, "missing recipe %s", "PSPHOT");207 psError(PM_ERR_CONFIG, false, "missing recipe %s", "PSPHOT"); 205 208 return false; 206 209 } … … 213 216 // get the current header 214 217 pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing 218 if (!fpa) { 219 psError(psErrorCodeLast(), false, "Unable to get FPA for writing."); 220 return false; 221 } 215 222 pmHDU *hdu = psMemIncrRefCounter(pmFPAviewThisHDU(view, fpa)); 216 223 psFree(fpa); 217 224 if (!hdu) { 218 psError(P S_ERR_UNKNOWN, false, "Unable to find HDU");225 psError(PM_ERR_CONFIG, false, "Unable to find HDU"); 219 226 return false; 220 227 } … … 234 241 psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES"); 235 242 if (!menu) { 236 psError(P S_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");243 psError(PM_ERR_CONFIG, true, "missing EXTNAME.RULES in camera.config"); 237 244 psFree(hdu); 238 245 return false; … … 242 249 rule = psMetadataLookupStr(&status, menu, "PSF.HEAD"); 243 250 if (!rule) { 244 psError(P S_ERR_UNKNOWN, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config");251 psError(PM_ERR_CONFIG, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config"); 245 252 psFree(hdu); 246 253 return false; … … 251 258 rule = psMetadataLookupStr(&status, menu, "PSF.TABLE"); 252 259 if (!rule) { 253 psError(P S_ERR_UNKNOWN, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");260 psError(PM_ERR_CONFIG, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config"); 254 261 psFree (headName); 255 262 psFree(hdu); … … 261 268 rule = psMetadataLookupStr(&status, menu, "PSF.RESID"); 262 269 if (!rule) { 263 psError(P S_ERR_UNKNOWN, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");270 psError(PM_ERR_CONFIG, false, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config"); 264 271 psFree (headName); 265 272 psFree (tableName); … … 294 301 } 295 302 296 psFitsWriteBlank (file->fits, hdu->header, headName); 303 if (!psFitsWriteBlank(file->fits, hdu->header, headName)) { 304 psError(psErrorCodeLast(), false, "Unable to write PSF PHU."); 305 psFree(hdu); 306 return false; 307 } 297 308 psTrace ("pmFPAfile", 5, "wrote ext head %s (type: %d)\n", file->filename, file->type); 298 309 file->header = hdu->header; … … 304 315 pmPSF *psf = psMetadataLookupPtr (&status, analysis, "PSPHOT.PSF"); 305 316 if (!psf) { 306 psError(P S_ERR_UNKNOWN, true, "missing PSF for this analysis metadata");317 psError(PM_ERR_PROG, true, "missing PSF for this analysis metadata"); 307 318 psFree (tableName); 308 319 psFree (residName); … … 322 333 psMetadataAddBool (header, PS_LIST_TAIL, "ERR_PAR", 0, "Use Poisson errors in fits?", psf->poissonErrorsParams); 323 334 324 int nPar = pmModelClassParameterCount (psf->type) ;335 int nPar = pmModelClassParameterCount (psf->type); 325 336 psMetadataAdd (header, PS_LIST_TAIL, "PSF_NPAR", PS_DATA_S32, "PSF model parameter count", nPar); 326 337 … … 436 447 // write an empty FITS segment if we have no PSF information 437 448 if (psfTable->n == 0) { 438 // XXX this is probably an error (if we have a PSF, how do we have no data?) 439 psFitsWriteBlank (file->fits, header, tableName); 449 psError(PM_ERR_PROG, true, "No PSF data to write."); 450 psFree(tableName); 451 psFree(residName); 452 psFree(psfTable); 453 psFree(header); 454 return false; 440 455 } else { 441 456 psTrace ("pmFPAfile", 5, "writing psf data %s\n", tableName); 442 if (!psFitsWriteTable (file->fits, header, psfTable, tableName)) {443 psError( PS_ERR_IO, false, "writing psf table data %s\n", tableName);457 if (!psFitsWriteTable(file->fits, header, psfTable, tableName)) { 458 psError(psErrorCodeLast(), false, "Error writing psf table data %s\n", tableName); 444 459 psFree (tableName); 445 460 psFree (residName); … … 459 474 if (psf->residuals == NULL) { 460 475 // set some header keywords to make it clear there are no residuals? 461 psFitsWriteBlank (file->fits, header, residName); 476 if (!psFitsWriteBlank(file->fits, header, residName)) { 477 psError(psErrorCodeLast(), false, "Unable to write blank PSF residual image."); 478 psFree(residName); 479 psFree(header); 480 return false; 481 } 462 482 psFree (residName); 463 483 psFree (header); … … 479 499 psArrayAdd (images, 1, psf->residuals->Ry); 480 500 481 psFitsWriteImageCube (file->fits, header, images, residName); 501 if (!psFitsWriteImageCube (file->fits, header, images, residName)) { 502 psError(psErrorCodeLast(), false, "Unable to write PSF residuals."); 503 psFree(images); 504 psFree(residName); 505 psFree(header); 506 return false; 507 } 482 508 psFree (images); 483 509 } else { 484 510 // this call creates an extension with NAXIS3 = 1 485 psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName); 511 if (!psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName)) { 512 psError(psErrorCodeLast(), false, "Unable to write PSF residuals."); 513 psFree(images); 514 psFree(residName); 515 psFree(header); 516 return false; 517 } 486 518 } 487 519 psFree (residName); … … 495 527 if (0) { 496 528 // set some header keywords to make it clear there are no residuals? 497 psFitsWriteBlank (file->fits, header, residName); 529 if (!psFitsWriteBlank (file->fits, header, residName)) { 530 psError(psErrorCodeLast(), false, "Unable to write blank PSF residuals."); 531 psFree(residName); 532 psFree(header); 533 return false; 534 } 498 535 psFree (residName); 499 536 psFree (header); … … 501 538 } 502 539 503 int DX = 65; 504 int DY = 65; 505 506 psImage *psfMosaic = psImageAlloc (DX, DY, PS_TYPE_F32); 507 psImageInit (psfMosaic, 0.0); 508 509 pmModel *modelRef = pmModelAlloc(psf->type); 510 511 // use the center of the center pixel of the image 512 float xc = 0.5*psf->fieldNx; 513 float yc = 0.5*psf->fieldNy; 514 515 // assign the x and y coords to the image center 516 // create an object with center intensity of 1000 517 modelRef->params->data.F32[PM_PAR_SKY] = 0; 518 modelRef->params->data.F32[PM_PAR_I0] = 1.000; 519 modelRef->params->data.F32[PM_PAR_XPOS] = xc; 520 modelRef->params->data.F32[PM_PAR_YPOS] = yc; 521 522 // create modelPSF from this model 523 pmModel *model = pmModelFromPSF (modelRef, psf); 524 if (model) { 525 // place the reference object in the image center 526 pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, 0.0, 0.0); 527 psFree (model); 528 529 if (false) { 530 // this call creates an extension with NAXIS3 = 3 531 psArray *images = psArrayAllocEmpty (3); 532 psArrayAdd (images, 1, psfMosaic); 533 // psArrayAdd (images, 1, psfModel); 534 // psArrayAdd (images, 1, psfModel); 535 536 psFitsWriteImageCube (file->fits, header, images, "PSF_MODEL"); 537 psFree (images); 538 } else { 539 // this call creates an extension with NAXIS3 = 1 540 // XXX need to replace PSF_MODEL with rule-based name like residName 541 psFitsWriteImage(file->fits, header, psfMosaic, 0, "PSF_MODEL"); 542 } 543 } 540 int DX = 65; 541 int DY = 65; 542 543 psImage *psfMosaic = psImageAlloc (DX, DY, PS_TYPE_F32); 544 psImageInit (psfMosaic, 0.0); 545 546 pmModel *modelRef = pmModelAlloc(psf->type); 547 548 // use the center of the center pixel of the image 549 float xc = 0.5*psf->fieldNx; 550 float yc = 0.5*psf->fieldNy; 551 552 // assign the x and y coords to the image center 553 // create an object with center intensity of 1000 554 modelRef->params->data.F32[PM_PAR_SKY] = 0; 555 modelRef->params->data.F32[PM_PAR_I0] = 1.000; 556 modelRef->params->data.F32[PM_PAR_XPOS] = xc; 557 modelRef->params->data.F32[PM_PAR_YPOS] = yc; 558 559 // create modelPSF from this model 560 pmModel *model = pmModelFromPSF (modelRef, psf); 561 if (model) { 562 // place the reference object in the image center 563 pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, 0.0, 0.0); 564 psFree (model); 565 566 if (false) { 567 // this call creates an extension with NAXIS3 = 3 568 psArray *images = psArrayAllocEmpty (3); 569 psArrayAdd (images, 1, psfMosaic); 570 // psArrayAdd (images, 1, psfModel); 571 // psArrayAdd (images, 1, psfModel); 572 573 if (!psFitsWriteImageCube (file->fits, header, images, "PSF_MODEL")) { 574 psError(psErrorCodeLast(), false, "Unable to write PSF representation."); 575 psFree(images); 576 psFree(psfMosaic); 577 psFree(modelRef); 578 psFree(header); 579 return false; 580 } 581 psFree (images); 582 } else { 583 // this call creates an extension with NAXIS3 = 1 584 // XXX need to replace PSF_MODEL with rule-based name like residName 585 if (!psFitsWriteImage(file->fits, header, psfMosaic, 0, "PSF_MODEL")) { 586 psError(psErrorCodeLast(), false, "Unable to write PSF representation."); 587 psFree(psfMosaic); 588 psFree(modelRef); 589 psFree(header); 590 return false; 591 } 592 } 593 } 544 594 545 595 psFree (psfMosaic); … … 594 644 // find the FPA phu 595 645 pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing 646 if (!fpa) { 647 psError(psErrorCodeLast(), false, "Unable to build FPA to write."); 648 return false; 649 } 596 650 pmHDU *phu = psMemIncrRefCounter(pmFPAviewThisPHU(view, fpa)); 597 651 psFree(fpa); … … 604 658 psMetadataCopy (outhead, phu->header); 605 659 } else { 606 pmConfigConformHeader (outhead, file->format); 660 if (!pmConfigConformHeader (outhead, file->format)) { 661 psError(psErrorCodeLast(), false, "Unable to conform header of PSF PHU."); 662 psFree(phu); 663 return false; 664 } 607 665 } 608 666 psFree(phu); 609 667 610 668 psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true); 611 psFitsWriteBlank (file->fits, outhead, ""); 669 if (!psFitsWriteBlank (file->fits, outhead, "")) { 670 psError(psErrorCodeLast(), false, "Unable to write PHU for PSF."); 671 psFree(outhead); 672 return false; 673 } 612 674 file->wrote_phu = true; 613 675 … … 639 701 } 640 702 641 psError(P S_ERR_IO, false, "PSF must be read at the chip level");703 psError(PM_ERR_CONFIG, true, "PSF must be read at the chip level"); 642 704 return false; 643 705 } … … 666 728 667 729 if (!pmPSFmodelRead (chip->analysis, view, file, config)) { 668 psError( PS_ERR_IO, false, "Failed to write PSF for chip");730 psError(psErrorCodeLast(), false, "Failed to write PSF for chip"); 669 731 return false; 670 732 } … … 689 751 psMetadata *recipe = psMetadataLookupPtr (NULL, config->recipes, "PSPHOT"); 690 752 if (!recipe) { 691 psError(P S_ERR_UNKNOWN, false, "missing recipe %s", "PSPHOT");753 psError(PM_ERR_CONFIG, false, "missing recipe %s", "PSPHOT"); 692 754 return false; 693 755 } … … 696 758 psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "EXTNAME.RULES"); 697 759 if (!menu) { 698 psError(P S_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");760 psError(PM_ERR_CONFIG, true, "missing EXTNAME.RULES in camera.config"); 699 761 return false; 700 762 } … … 702 764 rule = psMetadataLookupStr(&status, menu, "PSF.TABLE"); 703 765 if (!rule) { 704 psError(P S_ERR_UNKNOWN, true, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");766 psError(PM_ERR_CONFIG, true, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config"); 705 767 return false; 706 768 } … … 709 771 rule = psMetadataLookupStr(&status, menu, "PSF.RESID"); 710 772 if (!rule) { 711 psError(P S_ERR_UNKNOWN, true, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config");773 psError(PM_ERR_CONFIG, true, "missing entry for PSF.RESID in EXTNAME.RULES in camera.config"); 712 774 return false; 713 775 } … … 717 779 // advance to the table data extension 718 780 // since we have read the IMAGE header, the TABLE header should exist 719 if (!psFitsMoveExtName (file->fits, tableName)) { 720 psAbort("cannot find data extension %s in %s", tableName, file->filename); 781 if (!psFitsMoveExtName(file->fits, tableName)) { 782 psError(psErrorCodeLast(), false, "cannot find data extension %s in %s", tableName, file->filename); 783 return false; 721 784 } 722 785 723 786 // load the PSF model table header 724 787 header = psFitsReadHeader (NULL, file->fits); 725 if (!header) psAbort("cannot read table header"); 788 if (!header) { 789 psError(psErrorCodeLast(), false, "Cannot read PSF table header."); 790 return false; 791 } 726 792 727 793 pmPSFOptions *options = pmPSFOptionsAlloc(); … … 835 901 char *modeName = psMetadataLookupStr (&status, header, name); 836 902 if (!status) { 837 psError(P S_ERR_UNKNOWN, true, "inconsistent PSF header: NX & NY defined for PAR %d, but not MD", i);903 psError(PM_ERR_IO, true, "inconsistent PSF header: NX & NY defined for PAR %d, but not MD", i); 838 904 return false; 839 905 } … … 869 935 // read the raw table data 870 936 psArray *table = psFitsReadTable (file->fits); 937 if (!table) { 938 psError(psErrorCodeLast(), false, "Unable to read PSF table."); 939 psFree(header); 940 return false; 941 } 871 942 872 943 // fill in the matching psf->params entries … … 913 984 // since we have read the IMAGE header, the TABLE header should exist 914 985 if (!psFitsMoveExtName (file->fits, imageName)) { 915 psAbort("cannot find data extension %s in %s", imageName, file->filename); 986 psError(psErrorCodeLast(), false, "Cannot find PSF data extension %s in %s", 987 imageName, file->filename); 988 return false; 916 989 } 917 990 918 991 header = psFitsReadHeader (NULL, file->fits); 992 if (!header) { 993 psError(psErrorCodeLast(), false, "Unable to read PSF header."); 994 return false; 995 } 919 996 int Naxis = psMetadataLookupS32 (&status, header, "NAXIS"); 920 997 if (Naxis != 0) { … … 936 1013 937 1014 psRegion fullImage = {0, 0, 0, 0}; 938 psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0); // Desired pixels 1015 if (!psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0)) { 1016 psError(psErrorCodeLast(), false, "Unable to read PSF residual image."); 1017 return false; 1018 } 939 1019 if (Nz > 1) { 940 1020 assert (Nz == 3); 941 psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1); // Desired pixels 942 psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2); // Desired pixels 1021 if (!psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1)) { 1022 psError(psErrorCodeLast(), false, "Unable to read PSF residual image."); 1023 return false; 1024 } 1025 if (!psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2)) { 1026 psError(psErrorCodeLast(), false, "Unable to read PSF residual image."); 1027 return false; 1028 } 943 1029 } 944 1030 // XXX notice that we are not saving the resid->mask
Note:
See TracChangeset
for help on using the changeset viewer.
