Commit 83bcfcbbcb13503d4a5edf24012f0bf5ad8a96f4
1 parent
892123f9
add image() and table() HDU access methods
Showing
2 changed files
with
154 additions
and
32 deletions
include/GFits.hpp
| ... | ... | @@ -23,6 +23,7 @@ |
| 23 | 23 | #include <vector> |
| 24 | 24 | #include "GFitsHDU.hpp" |
| 25 | 25 | #include "GFitsImage.hpp" |
| 26 | +#include "GFitsTable.hpp" | |
| 26 | 27 | |
| 27 | 28 | |
| 28 | 29 | /***********************************************************************//** |
| ... | ... | @@ -51,15 +52,19 @@ public: |
| 51 | 52 | GFits& operator= (const GFits& fits); |
| 52 | 53 | |
| 53 | 54 | // Methods |
| 54 | - void clear(void); | |
| 55 | - int size(void) const; | |
| 56 | - void open(const std::string& filename); | |
| 57 | - void save(void); | |
| 58 | - void saveto(const std::string& filename, bool clobber = false); | |
| 59 | - void close(void); | |
| 60 | - void append(GFitsHDU* hdu); | |
| 61 | - GFitsHDU* hdu(const std::string& extname) const; | |
| 62 | - GFitsHDU* hdu(int extno) const; | |
| 55 | + void clear(void); | |
| 56 | + int size(void) const; | |
| 57 | + void open(const std::string& filename); | |
| 58 | + void save(void); | |
| 59 | + void saveto(const std::string& filename, bool clobber = false); | |
| 60 | + void close(void); | |
| 61 | + void append(GFitsHDU* hdu); | |
| 62 | + GFitsHDU* hdu(const std::string& extname) const; | |
| 63 | + GFitsHDU* hdu(int extno) const; | |
| 64 | + GFitsImage* image(const std::string& extname) const; | |
| 65 | + GFitsImage* image(int extno) const; | |
| 66 | + GFitsTable* table(const std::string& extname) const; | |
| 67 | + GFitsTable* table(int extno) const; | |
| 63 | 68 | |
| 64 | 69 | private: |
| 65 | 70 | // Private methods | ... | ... |
src/fits/GFits.cpp
| ... | ... | @@ -33,15 +33,20 @@ |
| 33 | 33 | #define G_SAVETO "GFits::saveto(std::string&,bool)" |
| 34 | 34 | #define G_HDU1 "GFits::hdu(std::string&)" |
| 35 | 35 | #define G_HDU2 "GFits::hdu(int)" |
| 36 | +#define G_IMAGE1 "GFits::image(std::string&)" | |
| 37 | +#define G_IMAGE2 "GFits::image(int)" | |
| 38 | +#define G_TABLE1 "GFits::table(std::string&)" | |
| 39 | +#define G_TABLE2 "GFits::table(int)" | |
| 36 | 40 | #define G_FREE_MEM "GFits::free_members()" |
| 37 | 41 | #define G_NEW_IMAGE "GFits::new_image()" |
| 38 | 42 | |
| 39 | 43 | /* __ Macros _____________________________________________________________ */ |
| 40 | 44 | |
| 41 | 45 | /* __ Coding definitions _________________________________________________ */ |
| 46 | +#define DELETE_EMPTY_FITS_FILES 0 //!< Do not write empty FITS files | |
| 42 | 47 | |
| 43 | 48 | /* __ Debug definitions __________________________________________________ */ |
| 44 | -#define DEBUG 0 | |
| 49 | +#define DEBUG 0 //!< Code debugging option | |
| 45 | 50 | |
| 46 | 51 | |
| 47 | 52 | /*========================================================================== |
| ... | ... | @@ -500,6 +505,8 @@ void GFits::append(GFitsHDU* hdu) |
| 500 | 505 | * |
| 501 | 506 | * @exception GException::fits_hdu_not_found |
| 502 | 507 | * No HDU with specified name has been found. |
| 508 | + * | |
| 509 | + * Returns a pointer to the HDU with the specified extname. | |
| 503 | 510 | ***************************************************************************/ |
| 504 | 511 | GFitsHDU* GFits::hdu(const std::string& extname) const |
| 505 | 512 | { |
| ... | ... | @@ -532,10 +539,13 @@ GFitsHDU* GFits::hdu(const std::string& extname) const |
| 532 | 539 | /***********************************************************************//** |
| 533 | 540 | * @brief Get pointer to HDU |
| 534 | 541 | * |
| 535 | - * @param[in] extno Extension number (starting from 0) | |
| 542 | + * @param[in] extno Extension number (starting from 0). | |
| 536 | 543 | * |
| 537 | 544 | * @exception GException::fits_hdu_not_found |
| 538 | 545 | * No HDU with specified extension number has been found. |
| 546 | + * | |
| 547 | + * Returns a pointer to the HDU with the specified extension number extno. | |
| 548 | + * Performs extno range checking if G_RANGE_CHECK is defined. | |
| 539 | 549 | ***************************************************************************/ |
| 540 | 550 | GFitsHDU* GFits::hdu(int extno) const |
| 541 | 551 | { |
| ... | ... | @@ -561,6 +571,108 @@ GFitsHDU* GFits::hdu(int extno) const |
| 561 | 571 | } |
| 562 | 572 | |
| 563 | 573 | |
| 574 | +/***********************************************************************//** | |
| 575 | + * @brief Get pointer to image HDU | |
| 576 | + * | |
| 577 | + * @param[in] extname Name of HDU extension. | |
| 578 | + * | |
| 579 | + * @exception GException::fits_hdu_not_image | |
| 580 | + * Requested HDU is not an image. | |
| 581 | + * | |
| 582 | + * Returns a pointer to the image HDU with extension name extname. | |
| 583 | + ***************************************************************************/ | |
| 584 | +GFitsImage* GFits::image(const std::string& extname) const | |
| 585 | +{ | |
| 586 | + // Get HDU pointer | |
| 587 | + GFitsHDU* ptr = hdu(extname); | |
| 588 | + | |
| 589 | + // Throw an error if HDU is not an image | |
| 590 | + if (ptr->exttype() != GFitsHDU::HT_IMAGE) | |
| 591 | + throw GException::fits_hdu_not_image(G_IMAGE1, extname, ptr->exttype()); | |
| 592 | + | |
| 593 | + // Return pointer | |
| 594 | + return ((GFitsImage*)ptr); | |
| 595 | +} | |
| 596 | + | |
| 597 | + | |
| 598 | +/***********************************************************************//** | |
| 599 | + * @brief Get pointer to image HDU | |
| 600 | + * | |
| 601 | + * @param[in] extno Extension number (starting from 0). | |
| 602 | + * | |
| 603 | + * @exception GException::fits_hdu_not_image | |
| 604 | + * Requested HDU is not an image. | |
| 605 | + * | |
| 606 | + * Returns a pointer to the image HDU with extension number extno. | |
| 607 | + ***************************************************************************/ | |
| 608 | +GFitsImage* GFits::image(int extno) const | |
| 609 | +{ | |
| 610 | + // Get HDU pointer | |
| 611 | + GFitsHDU* ptr = hdu(extno); | |
| 612 | + | |
| 613 | + // Throw an error if HDU is not an image | |
| 614 | + if (ptr->exttype() != GFitsHDU::HT_IMAGE) | |
| 615 | + throw GException::fits_hdu_not_image(G_IMAGE2, | |
| 616 | + "(extno="+str(extno)+")", | |
| 617 | + ptr->exttype()); | |
| 618 | + | |
| 619 | + // Return pointer | |
| 620 | + return ((GFitsImage*)ptr); | |
| 621 | +} | |
| 622 | + | |
| 623 | + | |
| 624 | +/***********************************************************************//** | |
| 625 | + * @brief Get pointer to table HDU | |
| 626 | + * | |
| 627 | + * @param[in] extname Name of HDU extension. | |
| 628 | + * | |
| 629 | + * @exception GException::fits_hdu_not_table | |
| 630 | + * Requested HDU is not a table. | |
| 631 | + * | |
| 632 | + * Returns a pointer to the table HDU with extension name extname. | |
| 633 | + ***************************************************************************/ | |
| 634 | +GFitsTable* GFits::table(const std::string& extname) const | |
| 635 | +{ | |
| 636 | + // Get HDU pointer | |
| 637 | + GFitsHDU* ptr = hdu(extname); | |
| 638 | + | |
| 639 | + // Throw an error if HDU is not a table | |
| 640 | + if (ptr->exttype() != GFitsHDU::HT_ASCII_TABLE && | |
| 641 | + ptr->exttype() != GFitsHDU::HT_BIN_TABLE) | |
| 642 | + throw GException::fits_hdu_not_table(G_TABLE1, extname, ptr->exttype()); | |
| 643 | + | |
| 644 | + // Return pointer | |
| 645 | + return ((GFitsTable*)ptr); | |
| 646 | +} | |
| 647 | + | |
| 648 | + | |
| 649 | +/***********************************************************************//** | |
| 650 | + * @brief Get pointer to table HDU | |
| 651 | + * | |
| 652 | + * @param[in] extno Extension number (starting from 0). | |
| 653 | + * | |
| 654 | + * @exception GException::fits_hdu_not_table | |
| 655 | + * Requested HDU is not a table. | |
| 656 | + * | |
| 657 | + * Returns a pointer to the table HDU with extension number extno. | |
| 658 | + ***************************************************************************/ | |
| 659 | +GFitsTable* GFits::table(int extno) const | |
| 660 | +{ | |
| 661 | + // Get HDU pointer | |
| 662 | + GFitsHDU* ptr = hdu(extno); | |
| 663 | + | |
| 664 | + // Throw an error if HDU is not a table | |
| 665 | + if (ptr->exttype() != GFitsHDU::HT_ASCII_TABLE && | |
| 666 | + ptr->exttype() != GFitsHDU::HT_BIN_TABLE) | |
| 667 | + throw GException::fits_hdu_not_table(G_TABLE2, | |
| 668 | + "(extno="+str(extno)+")", | |
| 669 | + ptr->exttype()); | |
| 670 | + | |
| 671 | + // Return pointer | |
| 672 | + return ((GFitsTable*)ptr); | |
| 673 | +} | |
| 674 | + | |
| 675 | + | |
| 564 | 676 | /*========================================================================== |
| 565 | 677 | = = |
| 566 | 678 | = Private methods = |
| ... | ... | @@ -614,34 +726,39 @@ void GFits::copy_members(const GFits& fits) |
| 614 | 726 | /***********************************************************************//** |
| 615 | 727 | * @brief Delete class members |
| 616 | 728 | * |
| 617 | - * This method also closes a file. In case that there are no HDUs or in case | |
| 618 | - * that the FITS file is not correct the result file will be deleted. This | |
| 619 | - * prevents leaving corrupted files on disk. | |
| 729 | + * This method also closes a file. If DELETE_EMPTY_FITS_FILES=1, files | |
| 730 | + * without HDUs or corrupted files will be deleted. This prevents leaving | |
| 731 | + * corrupted files on disk (yet, corrupted files may be generated by another | |
| 732 | + * application, thus this is not 100% safe; better make the code solid | |
| 733 | + * against reading corrupted FITS files). | |
| 620 | 734 | ***************************************************************************/ |
| 621 | 735 | void GFits::free_members(void) |
| 622 | 736 | { |
| 623 | 737 | // If FITS file has been opened then close it now |
| 624 | 738 | if (m_fitsfile != NULL) { |
| 625 | 739 | |
| 626 | - // If there are no HDUs then delete the file (don't worry about error) | |
| 627 | - //if (size() == 0) { | |
| 628 | - // int status = 0; | |
| 629 | - // __ffdelt(FPTR(m_fitsfile), &status); | |
| 630 | - //} | |
| 631 | - | |
| 632 | - // ... otherwise close the file | |
| 633 | - //else { | |
| 740 | + // Compile option: If there are no HDUs then delete the file (don't | |
| 741 | + // worry about error) | |
| 742 | + #if DELETE_EMPTY_FITS_FILES | |
| 743 | + if (size() == 0) { | |
| 634 | 744 | int status = 0; |
| 635 | - status = __ffclos(FPTR(m_fitsfile), &status); | |
| 636 | - if (status == 252) { | |
| 637 | - int new_status = 0; | |
| 638 | - __ffdelt(FPTR(m_fitsfile), &new_status); | |
| 639 | - throw GException::fits_error(G_FREE_MEM, status); | |
| 640 | - } | |
| 641 | - else if (status != 0) | |
| 642 | - throw GException::fits_error(G_FREE_MEM, status); | |
| 643 | - //} | |
| 644 | - } | |
| 745 | + __ffdelt(FPTR(m_fitsfile), &status); | |
| 746 | + return; | |
| 747 | + } | |
| 748 | + #endif | |
| 749 | + | |
| 750 | + // Close the file | |
| 751 | + int status = 0; | |
| 752 | + status = __ffclos(FPTR(m_fitsfile), &status); | |
| 753 | + if (status == 252) { | |
| 754 | + int new_status = 0; | |
| 755 | + __ffdelt(FPTR(m_fitsfile), &new_status); | |
| 756 | + throw GException::fits_error(G_FREE_MEM, status); | |
| 757 | + } | |
| 758 | + else if (status != 0) | |
| 759 | + throw GException::fits_error(G_FREE_MEM, status); | |
| 760 | + | |
| 761 | + } // endif: There was an open FITS file | |
| 645 | 762 | |
| 646 | 763 | // Return |
| 647 | 764 | return; | ... | ... |