- Timestamp:
- May 3, 2010, 8:45:22 AM (16 years ago)
- Location:
- branches/simmosaic_branches
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
psLib/src/fits/psFitsHeader.c (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/simmosaic_branches
- Property svn:mergeinfo changed
-
branches/simmosaic_branches/psLib/src/fits/psFitsHeader.c
r23376 r27839 50 50 51 51 // List of the start of FITS header keys not to write (handled by cfitsio); NULL-terminated 52 static const char *noWriteFitsKeyStarts[] = { "NAXIS", "TTYPE", "TFORM", NULL };52 static const char *noWriteFitsKeyStarts[] = { "NAXIS", "TTYPE", "TFORM", "TZERO", "TSCAL", NULL }; 53 53 54 54 // List of compressed FITS header keys not to write (handled by cfitsio); NULL-terminated … … 148 148 } 149 149 150 151 150 bool psFitsCheckCompressedImagePHU(const psFits *fits, psMetadata *header) 152 151 { … … 306 305 keyType = 'C'; 307 306 } 307 psTrace("psLib.fits", 3, "Reading keyword %s, type %c\n", keyName, keyType); 308 308 if (status != 0) { 309 309 break; … … 328 328 -INFINITY); 329 329 } else { 330 success = psMetadataAddS32(header, PS_LIST_TAIL, keyNameTrans, dupFlag, keyComment, 331 atoi(keyValue)); 330 long long value = atoll(keyValue); // Value 331 if (value > PS_MIN_S32 && value < PS_MAX_S32) { 332 success = psMetadataAddS32(header, PS_LIST_TAIL, keyNameTrans, dupFlag, keyComment, 333 value); 334 } else { 335 success = psMetadataAddS64(header, PS_LIST_TAIL, keyNameTrans, dupFlag, keyComment, 336 value); 337 } 332 338 } 333 339 break; … … 497 503 static bool fitsWriteHeader(psFits *fits, // The FITS file handle 498 504 const psMetadata *output, // Metadata that is to be output into the FITS file 499 bool keyStarts // Write out the key starts? 505 bool keyStarts, // Write out the key starts? 506 bool phuImage // Are we writing a PHU image? 500 507 ) 501 508 { 502 509 int status = 0; // Status of cfitsio calls 510 int extnum = psFitsGetExtNum(fits); // Number of extension 503 511 bool simple = true; // If SIMPLE is T, then the file should conform to the FITS standard 504 512 psFitsCompressionType compress = psFitsCompressionGetType(fits); // Compression type 505 if (psFitsGetExtNum(fits) == 0) { 513 if (extnum == 0) { 514 506 515 // We allow the user to write SIMPLE, but it must be boolean 507 516 psMetadataItem *simpleItem = psMetadataLookup(output, "SIMPLE"); // SIMPLE in the header 508 517 if (simpleItem) { 509 if (simpleItem->type != PS_DATA_BOOL) { 510 psError(PS_ERR_BAD_PARAMETER_TYPE, true, "SIMPLE in a FITS header must be of boolean type: " 511 "not %x --- assuming FALSE.\n", simpleItem->type); 518 if (simpleItem->type != PS_DATA_BOOL || !simpleItem->data.B) { 512 519 int value = false; // Temporary holder for boolean 520 psWarning("Writing SIMPLE=F to FITS header by request"); 513 521 fits_update_key(fits->fd, TLOGICAL, "SIMPLE", &value, 514 522 "File does not conform to FITS standard", &status); 515 523 simple = false; 516 } else if (!simpleItem->data.B) {517 simple = false;518 int value = false; // Temporary holder for boolean519 fits_update_key(fits->fd, TLOGICAL, "SIMPLE", &value,520 "File does not conform to FITS standard", &status);521 524 } 522 525 // Uncompressed SIMPLE = T is taken care of by cfitsio. … … 526 529 bool compressing = ((!fits->options || fits->options->conventions.compression) && 527 530 compress != PS_FITS_COMPRESS_NONE) ? true : false; // Are we compressing? 528 529 531 if (compressing) { 530 532 psMetadataItem *simpleItem = psMetadataLookup(output, "SIMPLE"); // SIMPLE in the header … … 538 540 } 539 541 } 540 // int value = simple; // Temporary holder for boolean 541 // fits_update_key(fits->fd, TLOGICAL, "ZSIMPLE", &value, 542 // "Uncompressed file's conformance to FITS standard", &status); 542 if (simple && phuImage && extnum == 1) { 543 // ZSIMPLE is required for decompression with funpack, etc. For funpack to work, ZSIMPLE needs to 544 // go early in the FITS header (otherwise we get "Extension doesn't start with SIMPLE or XTENSION 545 // keyword."). We put it after ZIMAGE by reading ZIMAGE (which sets the insertion pointer) and 546 // then inserting ZSIMPLE. 547 char comment[FLEN_CARD]; // Comment for ZIMAGE; unused 548 int value; // Value for ZIMAGE; unused 549 psTrace("psLib.fits", 3, "Writing header ZSIMPLE to preserve PHU"); 550 fits_read_key(fits->fd, TLOGICAL, "ZIMAGE", &value, comment, &status); 551 fits_insert_key_log(fits->fd, "ZSIMPLE", simple, "Uncompressed file's conforms to FITS", &status); 552 } 543 553 } 544 554 … … 560 570 // to preserve NAXISn etc for reference, so we don't do this. 561 571 572 if (keywordInList(name, noWriteFitsKeys) || 573 (keyStarts && keywordStartsWith(name, noWriteFitsKeyStarts))) { 574 psTrace("psLib.fits", 3, "Not writing FITS keyword %s", name); 575 continue; 576 } 577 562 578 // Options for compression 563 579 if (compressing) { … … 567 583 if (keywordInList(name, noWriteCompressedKeys) || 568 584 (keyStarts && keywordStartsWith(name, noWriteCompressedKeyStarts))) { 585 psTrace("psLib.fits", 3, "Not writing FITS keyword %s", name); 569 586 continue; 570 587 } 571 588 } else if (keywordInList(name, noWriteCompressedKeys) || 572 589 (keyStarts && keywordStartsWith(name, noWriteCompressedKeyStarts))) { 573 continue; 574 } 575 576 if (keywordInList(name, noWriteFitsKeys) || 577 (keyStarts && keywordStartsWith(name, noWriteFitsKeyStarts))) { 590 psTrace("psLib.fits", 3, "Not writing FITS keyword %s", name); 578 591 continue; 579 592 } … … 584 597 psWarning("COMMENT header is not of type STRING (%x) --- ignored.", item->type); 585 598 } else { 599 psTrace("psLib.fits", 5, "Writing header COMMENT: %s", item->data.str); 586 600 fits_write_comment(fits->fd, item->data.str, &status); 587 601 } … … 590 604 psWarning("COMMENT header is not of type STRING (%x) --- ignored.", item->type); 591 605 } else { 606 psTrace("psLib.fits", 5, "Writing header HISTORY: %s", item->data.str); 592 607 fits_write_history(fits->fd, item->data.str, &status); 593 608 } … … 597 612 case PS_DATA_BOOL: { 598 613 int value = item->data.B; 614 psTrace("psLib.fits", 5, "Writing BOOL header %s: %d", name, value); 599 615 fits_update_key(fits->fd, TLOGICAL, name, &value, item->comment, &status); 600 616 break; 601 617 } 602 618 case PS_DATA_S8: 619 psTrace("psLib.fits", 5, "Writing S8 header %s: %d", name, (int)item->data.S8); 603 620 fits_update_key(fits->fd, TBYTE, name, &item->data.S8, item->comment, &status); 604 621 break; 605 622 case PS_DATA_S16: 623 psTrace("psLib.fits", 5, "Writing S16 header %s: %d", name, (int)item->data.S16); 606 624 fits_update_key(fits->fd, TSHORT, name, &item->data.S16, item->comment, &status); 607 625 break; 608 626 case PS_DATA_S32: 627 psTrace("psLib.fits", 5, "Writing S32 header %s: %d", name, (int)item->data.S32); 609 628 fits_update_key(fits->fd, TINT, name, &item->data.S32, item->comment, &status); 610 629 break; 611 630 case PS_DATA_S64: 631 psTrace("psLib.fits", 5, "Writing S64 header %s: %" PRId64, name, item->data.S64); 612 632 fits_update_key(fits->fd, TLONGLONG, name, &item->data.S64, item->comment, &status); 613 633 break; 614 634 case PS_DATA_U8: { 615 635 unsigned short int temp = item->data.U8; 636 psTrace("psLib.fits", 5, "Writing U8 header %s: %d", name, (int)item->data.U8); 616 637 fits_update_key(fits->fd, TUSHORT, name, &temp, item->comment, &status); 617 638 break; 618 639 } 619 640 case PS_DATA_U16: 641 psTrace("psLib.fits", 5, "Writing U16 header %s: %d", name, (int)item->data.U16); 620 642 fits_update_key(fits->fd, TUSHORT, name, &item->data.U16, item->comment, &status); 621 643 break; 622 644 case PS_DATA_U32: 645 psTrace("psLib.fits", 5, "Writing U32 header %s: %d", name, (unsigned int)item->data.U32); 623 646 fits_update_key(fits->fd, TUINT, name, &item->data.U32, item->comment, &status); 624 647 break; … … 631 654 } 632 655 psS64 temp = item->data.U64; // Signed version 656 psTrace("psLib.fits", 5, "Writing U64 header %s: %" PRIu64, name, item->data.U64); 633 657 fits_update_key(fits->fd, TLONGLONG, name, &temp, item->comment, &status); 634 658 break; 635 659 case PS_DATA_F32: { 636 660 int infCheck = 0; // Result of isinf() 661 psTrace("psLib.fits", 5, "Writing F32 header %s: %f", name, item->data.F32); 637 662 if (isnan(item->data.F32)) { 638 663 fits_update_key(fits->fd, TSTRING, name, "NaN", item->comment, &status); … … 651 676 case PS_DATA_F64: { 652 677 int infCheck = 0; // Result of isinf() 678 psTrace("psLib.fits", 5, "Writing F32 header %s: %lf", name, item->data.F64); 653 679 if (isnan(item->data.F64)) { 654 680 fits_update_key(fits->fd, TSTRING, name, "NaN", item->comment, &status); … … 666 692 } 667 693 case PS_DATA_STRING: 694 psTrace("psLib.fits", 5, "Writing STR header %s: %s", name, item->data.str); 668 695 fits_update_key(fits->fd, TSTRING, name, item->data.V, item->comment, &status); 669 696 break; … … 699 726 PS_ASSERT_METADATA_NON_NULL(output, false); 700 727 701 return fitsWriteHeader(fits, output, true); 728 return fitsWriteHeader(fits, output, true, false); 729 } 730 731 bool psFitsWriteHeaderImage(psFits *fits, 732 const psMetadata *output, 733 bool phuImage 734 ) 735 { 736 PS_ASSERT_FITS_NON_NULL(fits, false); 737 PS_ASSERT_FITS_WRITABLE(fits, false); 738 PS_ASSERT_METADATA_NON_NULL(output, false); 739 740 return fitsWriteHeader(fits, output, true, phuImage); 702 741 } 703 742 … … 733 772 } 734 773 735 if (output && !fitsWriteHeader(fits, output, false )) {774 if (output && !fitsWriteHeader(fits, output, false, false)) { 736 775 psError(PS_ERR_IO, false, "Unable to write FITS header.\n"); 737 776 return false;
Note:
See TracChangeset
for help on using the changeset viewer.
