00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef HDU_H
00013 #define HDU_H 1
00014 #include <map>
00015
00016
00017 #include <vector>
00018 #include <list>
00019
00020 #include "CCfits.h"
00021
00022 #include "Keyword.h"
00023
00024 #include "NewKeyword.h"
00025
00026 #include "FitsError.h"
00027
00028 #include "FITSUtil.h"
00029
00030 namespace CCfits {
00031 class FITS;
00032 class FITSBase;
00033
00034 }
00035 namespace CCfits {
00036 class HDUCreator;
00037 }
00038
00039 #ifdef _MSC_VER
00040 #include "MSconfig.h"
00041 #endif
00042 #include "KeywordT.h"
00043
00044
00045 namespace CCfits {
00546 class HDU
00547 {
00548
00549 public:
00550
00551
00552
00553 class InvalidImageDataType : public FitsException
00554 {
00555 public:
00556 InvalidImageDataType (const string& diag, bool silent = true);
00557
00558 protected:
00559 private:
00560 private:
00561 };
00562
00563
00564
00565 class InvalidExtensionType : public FitsException
00566 {
00567 public:
00568 InvalidExtensionType (const string& diag, bool silent = true);
00569
00570 protected:
00571 private:
00572 private:
00573 };
00574
00575
00576
00577 class NoSuchKeyword : public FitsException
00578 {
00579 public:
00580 NoSuchKeyword (const string& diag, bool silent = true);
00581
00582 protected:
00583 private:
00584 private:
00585 };
00586
00587
00588
00589 class NoNullValue : public FitsException
00590 {
00591 public:
00592 NoNullValue (const string& diag, bool silent = true);
00593
00594 protected:
00595 private:
00596 private:
00597 };
00598 HDU(const HDU &right);
00599 bool operator==(const HDU &right) const;
00600
00601 bool operator!=(const HDU &right) const;
00602
00603 virtual HDU * clone (FITSBase* p) const = 0;
00604 fitsfile* fitsPointer () const;
00605 FITSBase* parent () const;
00606
00607
00608
00609
00610
00611 virtual void makeThisCurrent () const;
00612 const String& getComments ();
00613 const string& comment () const;
00614
00615
00616
00617 void writeComment (const String& comment = "Generic Comment");
00618 const String& getHistory ();
00619 const string& history () const;
00620
00621
00622
00623 void writeHistory (const String& history = "Generic History String");
00624
00625 void writeDate ();
00626 friend std::ostream& operator << (std::ostream& s, const CCfits::HDU& right);
00627 long axes () const;
00628 long axis (size_t index) const;
00629 void index (int value);
00630 int index () const;
00631 long bitpix () const;
00632 virtual double scale () const;
00633 virtual void scale (double value);
00634 virtual double zero () const;
00635 virtual void zero (double value);
00636 void suppressScaling (bool toggle = true);
00637 void writeChecksum ();
00638 void updateChecksum ();
00639 std::pair<int,int> verifyChecksum () const;
00640 std::pair<unsigned long,unsigned long> getChecksum () const;
00641 void deleteKey (const String& doomed);
00642 void readAllKeys ();
00643 void copyAllKeys (const HDU* inHdu);
00644 std::map<String, Keyword*>& keyWord ();
00645 Keyword& keyWord (const String& keyName);
00646 static std::vector<int> keywordCategories ();
00647 const std::map<string,Keyword*>& keyWord () const;
00648 const Keyword& keyWord (const string& keyname) const;
00649
00650 public:
00651
00652 template <typename T>
00653 void readKey(const String& keyName, T& val);
00654
00655 template <typename T>
00656 void readKeys(std::vector<String>& keyNames, std::vector<T>& vals);
00657
00658 template <typename T>
00659 Keyword& addKey(const String& name, T val, const String& comment);
00660
00661
00662
00663
00664 Keyword* addKey(const Keyword* inKeyword);
00665
00666 Keyword& addKey(const String& name, const char* charString, const String& comment);
00667
00668 #ifdef TEMPLATE_AMBIG_DEFECT
00669 inline void readKeyMS(const String& keyName, int & val);
00670 inline void readKeys(std::vector<String>& keyNames, std::vector<String>& vals);
00671
00672 #endif
00673 protected:
00674
00675
00676 HDU (FITSBase* p = 0);
00677 HDU (FITSBase* p, int bitpix, int naxis, const std::vector<long>& axes);
00678 virtual ~HDU();
00679
00680 Keyword& readKeyword (const String &keyname);
00681 void readKeywords (std::list<String>& keynames);
00682 virtual std::ostream & put (std::ostream &s) const = 0;
00683 void bitpix (long value);
00684 bool checkImgDataTypeChange (double zero, double scale) const;
00685 long& naxis ();
00686 void naxis (const long& value);
00687
00688
00689 bool& anynul ();
00690 void anynul (const bool& value);
00691 FITSBase*& parent ();
00692 std::vector< long >& naxes ();
00693 long& naxes (size_t index);
00694 void naxes (size_t index, const long& value);
00695
00696
00697
00698 private:
00699
00700
00701 void clearKeys ();
00702 virtual void initRead () = 0;
00703 void readHduInfo ();
00704 Keyword* addKeyword (Keyword* newKey);
00705 virtual bool compare (const HDU &right) const;
00706
00707
00708 void copyKeys (const HDU& right);
00709 String getNamedLines (const String& name);
00710
00711
00712 void saveReadKeyword (Keyword* newKey);
00713 void zeroInit (double value);
00714 void scaleInit (double value);
00715
00716
00717
00718 private:
00719
00720 long m_naxis;
00721 long m_bitpix;
00722 int m_index;
00723 bool m_anynul;
00724 string m_history;
00725 string m_comment;
00726 double m_zero;
00727
00728
00729 double m_scale;
00730
00731
00732 std::map<string,Keyword*> m_keyWord;
00733 FITSBase* m_parent;
00734 std::vector< long > m_naxes;
00735
00736
00737 static const size_t s_nCategories;
00738 static const int s_iKeywordCategories[];
00739
00740 friend class HDUCreator;
00741 friend Keyword* KeywordCreator::getKeyword(const String& keyname, HDU* p);
00742 friend Keyword* KeywordCreator::getKeyword(const String& keyname, ValueType keyType, HDU* p);
00743 };
00744 template <typename T>
00745 Keyword& HDU::addKey(const String& name, T value, const String& comment)
00746 {
00747 makeThisCurrent();
00748 NewKeyword<T> keyCreator(this,value);
00749 Keyword& newKey = *(addKeyword(keyCreator.createKeyword(name,comment)));
00750 return newKey;
00751 }
00752
00753 template <typename T>
00754 void HDU::readKey(const String& keyName, T& val)
00755 {
00756 makeThisCurrent();
00757 Keyword& key = readKeyword(keyName);
00758 key.value(val);
00759 }
00760
00761
00762 template <typename T>
00763 void HDU::readKeys(std::vector<String>& keyNames, std::vector<T>& vals)
00764 {
00765 size_t nRead = keyNames.size();
00766
00767 std::list<String> valKeys;
00768 std::list<T> valList;
00769 for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
00770
00771
00772 readKeywords(valKeys);
00773
00774
00775
00776
00777 T current;
00778 std::list<String>::iterator it = valKeys.begin();
00779 while (it != valKeys.end())
00780 {
00781 try
00782 {
00783 m_keyWord[*it]->value(current);
00784 valList.push_back(current);
00785 ++it;
00786 }
00787 catch ( Keyword::WrongKeywordValueType )
00788 {
00789 it = valKeys.erase(it);
00790 }
00791 }
00792
00793 keyNames.erase(keyNames.begin(),keyNames.end());
00794
00795 if (!valList.empty())
00796 {
00797 if (valList.size() != vals.size()) vals.resize(valList.size());
00798
00799 size_t i=0;
00800 for (typename std::list<T>::const_iterator it1
00801 = valList.begin(); it1 != valList.end(); ++it1,++i)
00802 {
00803 vals[i] = *it1;
00804 }
00805 for (std::list<String>::const_iterator it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
00806 {
00807 keyNames.push_back(*it1);
00808 }
00809 }
00810
00811 }
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823 inline const string& HDU::comment () const
00824 {
00825 return m_comment;
00826 }
00827
00828 inline const string& HDU::history () const
00829 {
00830 return m_history;
00831 }
00832
00833 inline std::ostream& operator << (std::ostream& s, const CCfits::HDU& right)
00834 {
00835 return right.put(s);
00836 }
00837
00838 inline long HDU::axes () const
00839 {
00840
00841 return m_naxis;
00842 }
00843
00844 inline long HDU::axis (size_t index) const
00845 {
00846
00847 return m_naxes[index];
00848 }
00849
00850 inline void HDU::index (int value)
00851 {
00852
00853 m_index = value;
00854 }
00855
00856 inline int HDU::index () const
00857 {
00858 return m_index;
00859 }
00860
00861 inline long HDU::bitpix () const
00862 {
00863 return m_bitpix;
00864 }
00865
00866 inline void HDU::bitpix (long value)
00867 {
00868 m_bitpix = value;
00869 }
00870
00871 inline double HDU::scale () const
00872 {
00873 return m_scale;
00874 }
00875
00876 inline void HDU::scale (double value)
00877 {
00878 m_scale = value;
00879 }
00880
00881 inline double HDU::zero () const
00882 {
00883 return m_zero;
00884 }
00885
00886 inline void HDU::zero (double value)
00887 {
00888 m_zero = value;
00889 }
00890
00891 inline void HDU::saveReadKeyword (Keyword* newKey)
00892 {
00893 m_keyWord.insert(std::map<String,Keyword*>::value_type(newKey->name(),newKey->clone()));
00894 }
00895
00896 inline std::map<String, Keyword*>& HDU::keyWord ()
00897 {
00898
00899 return m_keyWord;
00900 }
00901
00902 inline Keyword& HDU::keyWord (const String& keyName)
00903 {
00904 std::map<String,Keyword*>::iterator key = m_keyWord.find(keyName);
00905 if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyName);
00906 return *((*key).second);
00907 }
00908
00909 inline long& HDU::naxis ()
00910 {
00911 return m_naxis;
00912 }
00913
00914 inline void HDU::naxis (const long& value)
00915 {
00916 m_naxis = value;
00917 }
00918
00919 inline bool& HDU::anynul ()
00920 {
00921 return m_anynul;
00922 }
00923
00924 inline void HDU::anynul (const bool& value)
00925 {
00926 m_anynul = value;
00927 }
00928
00929 inline const std::map<string,Keyword*>& HDU::keyWord () const
00930 {
00931 return m_keyWord;
00932 }
00933
00934 inline const Keyword& HDU::keyWord (const string& keyname) const
00935 {
00936 std::map<String,Keyword*>::const_iterator key = m_keyWord.find(keyname);
00937 if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyname);
00938 return *((*key).second);
00939 }
00940
00941 inline FITSBase*& HDU::parent ()
00942 {
00943 return m_parent;
00944 }
00945
00946 inline std::vector< long >& HDU::naxes ()
00947 {
00948 return m_naxes;
00949 }
00950
00951 inline long& HDU::naxes (size_t index)
00952 {
00953 return m_naxes[index];
00954 }
00955
00956 inline void HDU::naxes (size_t index, const long& value)
00957 {
00958 m_naxes[index] = value;
00959 }
00960
00961 }
00962 #ifdef SPEC_TEMPLATE_IMP_DEFECT
00963 namespace CCfits {
00964
00965 inline void HDU::readKeyMS(const String& keyName, int & val)
00966 {
00967 makeThisCurrent();
00968 Keyword& key = readKeyword(keyName);
00969 key.value(val);
00970 }
00971
00972 inline void HDU::readKeys(std::vector<String>& keyNames, std::vector<String>& vals)
00973 {
00974 size_t nRead = keyNames.size();
00975
00976 std::list<String> valKeys;
00977 std::list<String> valList;
00978 for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
00979
00980
00981 readKeywords(valKeys);
00982
00983
00984
00985
00986 String current;
00987 std::list<String>::iterator it = valKeys.begin();
00988 while (it != valKeys.end())
00989 {
00990 try
00991 {
00992 m_keyWord[*it]->value(current);
00993 valList.push_back(current);
00994 ++it;
00995 }
00996 catch ( Keyword::WrongKeywordValueType )
00997 {
00998 it = valKeys.erase(it);
00999 }
01000 }
01001
01002 keyNames.erase(keyNames.begin(),keyNames.end());
01003
01004 if (!valList.empty())
01005 {
01006 if (valList.size() != vals.size()) vals.resize(valList.size());
01007
01008 size_t i=0;
01009 std::list<String>::const_iterator it1 = valList.begin();
01010 for ( ; it1 != valList.end(); ++it1,++i)
01011 {
01012 vals[i] = *it1;
01013 }
01014 for ( it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
01015 {
01016 keyNames.push_back(*it1);
01017 }
01018 }
01019
01020 }
01021 }
01022 #endif
01023
01024
01025 #endif