CCfits 2.7
HDU.h
1// Astrophysics Science Division,
2// NASA/ Goddard Space Flight Center
3// HEASARC
4// http://heasarc.gsfc.nasa.gov
5// e-mail: ccfits@legacy.gsfc.nasa.gov
6//
7// Original author: Ben Dorman
8
9#ifndef HDU_H
10#define HDU_H 1
11#include <map>
12
13// vector
14#include <vector>
15#include <list>
16// CCfitsHeader
17#include "CCfits.h"
18// Keyword
19#include "Keyword.h"
20// NewKeyword
21#include "NewKeyword.h"
22// FitsError
23#include "FitsError.h"
24// FITSUtil
25#include "FITSUtil.h"
26
27namespace CCfits {
28 class FITS;
29
30} // namespace CCfits
31namespace CCfits {
32 class HDUCreator; // Needed for friend declaration
33}
34
35#ifdef _MSC_VER
36#include "MSconfig.h" // for truncation warning
37#endif
38#include "KeywordT.h"
39
40
41namespace CCfits {
42
686 class HDU
687 {
688
689 public:
690
691
692 class InvalidImageDataType : public FitsException //## Inherits: <unnamed>%394FBA12005C
693 {
694 public:
695 InvalidImageDataType (const string& diag, bool silent = true);
696
697 protected:
698 private:
699 private: //## implementation
700 };
701
702
703 class InvalidExtensionType : public FitsException //## Inherits: <unnamed>%3964C1D00352
704 {
705 public:
706 InvalidExtensionType (const string& diag, bool silent = true);
707
708 protected:
709 private:
710 private: //## implementation
711 };
712
713
714 class NoSuchKeyword : public FitsException //## Inherits: <unnamed>%398865D10264
715 {
716 public:
717 NoSuchKeyword (const string& diag, bool silent = true);
718
719 protected:
720 private:
721 private: //## implementation
722 };
723
724
725 class NoNullValue : public FitsException //## Inherits: <unnamed>%3B0D58CE0306
726 {
727 public:
728 NoNullValue (const string& diag, bool silent = true);
729
730 protected:
731 private:
732 private: //## implementation
733 };
734
735
736 HDU(const HDU &right);
737 bool operator==(const HDU &right) const;
738
739 bool operator!=(const HDU &right) const;
740
741 virtual HDU * clone (FITS* p) const = 0;
742 fitsfile* fitsPointer () const;
743 FITS* parent () const;
744 // By all means necessary, set the fitsfile pointer so that
745 // this HDU is the current HDU.
746 //
747 // This would appear to be a good candidate for the public
748 // interface.
749 virtual void makeThisCurrent () const;
750 const String& getComments ();
751 const string& comment () const;
752 // Write a comment string. A default value for the string is given
753 // "GenericComment" so users can put a placeholder call
754 // to this function in their code before knowing quite what should go in it.
755 void writeComment (const String& comment = "Generic Comment");
756 const String& getHistory ();
757 const string& history () const;
758 // Write a history string. A default value for the string is given
759 // "Generic History String" so users can put a placeholder call
760 // to this function in their code before knowing quite what should go in it.
761 void writeHistory (const String& history = "Generic History String");
762 // Write a date card.
763 void writeDate ();
764 friend std::ostream& operator << (std::ostream& s, const CCfits::HDU& right);
765 long axes () const;
766 long axis (size_t index) const;
767 void index (int value);
768 int index () const;
769 long bitpix () const;
770 virtual double scale () const;
771 virtual void scale (double value);
772 virtual double zero () const;
773 virtual void zero (double value);
774 virtual void resetImageRead ();
775 virtual void suppressScaling (bool toggle = true);
776 void writeChecksum ();
777 void updateChecksum ();
778 std::pair<int,int> verifyChecksum () const;
779 std::pair<unsigned long,unsigned long> getChecksum () const;
780 void deleteKey (const String& doomed);
781 void readAllKeys (const std::vector<int> & keyCategories = std::vector<int>());
782 void copyAllKeys (const HDU* inHdu, const std::vector<int> & keyCategories = std::vector<int>());
783 std::map<String, Keyword*>& keyWord ();
784 Keyword& keyWord (const String& keyName);
785 static std::vector<int> keywordCategories ();
786 const std::map<string,Keyword*>& keyWord () const;
787 const Keyword& keyWord (const string& keyname) const;
788 Keyword& readNextKey(const std::vector<String>& incList,
789 const std::vector<String>& excList,
790 bool searchFromBeginning = false);
791
792
793 public:
794 // Additional Public Declarations
795
796 template <typename T>
797 void readKey(const String& keyName, T& val);
798
799 template <typename T>
800 void readKeys(std::vector<String>& keyNames, std::vector<T>& vals);
801
802 template <typename T>
803 Keyword& addKey(const String& name, T val, const String& comment, bool isLongStr = false);
804
805 // This non-template function could be entered with Rose, but
806 // it's instead placed with the other addKey function to
807 // simplify the Doxygen generated doc file output.
808 Keyword* addKey(const Keyword* inKeyword);
809
810 Keyword& addKey(const String& name, const char* charString, const String& comment, bool isLongStr = false);
811
812 Keyword& addKeyNull(const String& name, const String& comment, bool isLongStr = false);
813
814#ifdef TEMPLATE_AMBIG_DEFECT
815 inline void readKeyMS(const String& keyName, int & val);
816 inline void readKeys(std::vector<String>& keyNames, std::vector<String>& vals);
817
818#endif
819
820
821 protected:
822
823 // Functions as the default constructor, which is required for
824 // the map container class.
825 HDU (FITS* p = 0);
826 HDU (FITS* p, int bitpix, int naxis, const std::vector<long>& axes);
827 virtual ~HDU();
828
829 Keyword& readKeyword (const String &keyname);
830 void readKeywords (std::list<String>& keynames);
831 virtual std::ostream & put (std::ostream &s) const = 0;
832 void bitpix (long value);
833 bool checkImgDataTypeChange (double zero, double scale) const;
834 long& naxis ();
835 void naxis (const long& value);
836 // Flags whether there were any null values found in the
837 // last read operation.
838 bool& anynul ();
839 void anynul (const bool& value);
840 FITS*& parent ();
841
842 std::vector< long >& naxes ();
843 long& naxes (size_t index);
844 void naxes (size_t index, const long& value);
845
846 // Additional Protected Declarations
847
848
849 private:
850
851 // clear the FITS Keyword map. To be called by
852 // the dtor and the copy/assignment operations.
853 void clearKeys ();
854 virtual void initRead () = 0;
855 void readHduInfo ();
856 Keyword* addKeyword (Keyword* newKey);
857 virtual bool compare (const HDU &right) const;
858 // clear the FITS Keyword map. To be called by
859 // the dtor and the copy/assignment operations.
860 void copyKeys (const HDU& right);
861 String getNamedLines (const String& name);
862 // save keyword found by read all keys into the array of keywords that have been read.
863 // Similar to addKeyword except there's no write and no returned value. For use by readAllKeys()
864 void saveReadKeyword (Keyword* newKey);
865 void zeroInit (double value);
866 void scaleInit (double value);
867
868 // Additional Private Declarations
869
870
871 private: //## implementation
872 // Data Members for Class Attributes
873 long m_naxis;
874 long m_bitpix;
875 int m_index;
876 bool m_anynul;
877 string m_history;
878 string m_comment;
879 double m_zero;
880 // Floating point scale factor for image data that takes
881 // the value of the BSCALE parameter.
882 double m_scale;
883
884 // Data Members for Associations
885 std::map<string,Keyword*> m_keyWord;
886 FITS* m_parent;
887 std::vector< long > m_naxes;
888
889 // Additional Implementation Declarations
890 static const size_t s_nCategories;
891 static const int s_iKeywordCategories[];
892
893 friend class HDUCreator;
894 friend Keyword* KeywordCreator::getKeyword(const String& keyname, HDU* p);
895 friend Keyword* KeywordCreator::getKeyword(const String& keyname, ValueType keyType, HDU* p);
896
897 };
898
899
900 template <typename T>
901 Keyword& HDU::addKey(const String& name, T value, const String& comment, bool isLongStr)
902 {
904 NewKeyword<T> keyCreator(this,value);
905 Keyword& newKey = *(addKeyword(keyCreator.createKeyword(name,comment,isLongStr)));
906 return newKey;
907 }
908
909 template <typename T>
910 void HDU::readKey(const String& keyName, T& val)
911 {
913 Keyword& key = readKeyword(keyName);
914 key.value(val);
915 }
916
917 template <typename T>
918 void HDU::readKeys(std::vector<String>& keyNames, std::vector<T>& vals)
919 {
920 size_t nRead = keyNames.size();
921
922 std::list<String> valKeys;
923 std::list<T> valList;
924 for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
925 // read all the keys requested, rejecting those that don't exist.
926
927 readKeywords(valKeys);
928
929 // get the values of all of the requested keys, rejecting those of the
930 // wrong type.
931
932 T current;
933 std::list<String>::iterator it = valKeys.begin();
934 while (it != valKeys.end())
935 {
936 try
937 {
938 m_keyWord[*it]->value(current);
939 valList.push_back(current);
940 ++it;
941 }
942 catch ( Keyword::WrongKeywordValueType )
943 {
944 it = valKeys.erase(it);
945 }
946 }
947
948 keyNames.erase(keyNames.begin(),keyNames.end());
949
950 if (!valList.empty())
951 {
952 if (valList.size() != vals.size()) vals.resize(valList.size());
953
954 size_t i=0;
955 for (typename std::list<T>::const_iterator it1
956 = valList.begin(); it1 != valList.end(); ++it1,++i)
957 {
958 vals[i] = *it1;
959 }
960 for (std::list<String>::const_iterator it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
961 {
962 keyNames.push_back(*it1);
963 }
964 }
965
966 }
967
968 // Class CCfits::HDU::InvalidImageDataType
969
970 // Class CCfits::HDU::InvalidExtensionType
971
972 // Class CCfits::HDU::NoSuchKeyword
973
974 // Class CCfits::HDU::NoNullValue
975
976 // Class CCfits::HDU
977
978 inline const string& HDU::comment () const
979 {
980 return m_comment;
981 }
982
983 inline const string& HDU::history () const
984 {
985 return m_history;
986 }
987
988 inline std::ostream& operator << (std::ostream& s, const CCfits::HDU& right)
989 {
990 return right.put(s);
991 }
992
993 inline long HDU::axes () const
994 {
995 return m_naxis;
996 }
997
998 inline long HDU::axis (size_t index) const
999 {
1000 return m_naxes[index];
1001 }
1002
1003 inline void HDU::index (int value)
1004 {
1005 m_index = value;
1006 }
1007
1008 inline int HDU::index () const
1009 {
1010 return m_index;
1011 }
1012
1013 inline long HDU::bitpix () const
1014 {
1015 return m_bitpix;
1016 }
1017
1018 inline void HDU::bitpix (long value)
1019 {
1020 m_bitpix = value;
1021 }
1022
1023 inline double HDU::scale () const
1024 {
1025 return m_scale;
1026 }
1027
1028 inline void HDU::scale (double value)
1029 {
1030 m_scale = value;
1031 }
1032
1033 inline double HDU::zero () const
1034 {
1035 return m_zero;
1036 }
1037
1038 inline void HDU::zero (double value)
1039 {
1040 m_zero = value;
1041 }
1042
1043 inline void HDU::resetImageRead ()
1044 {
1045 }
1046
1047 inline void HDU::saveReadKeyword (Keyword* newKey)
1048 {
1049 m_keyWord.insert(std::map<String,Keyword*>::value_type(newKey->name(),newKey->clone()));
1050 }
1051
1052 inline std::map<String, Keyword*>& HDU::keyWord ()
1053 {
1054 return m_keyWord;
1055 }
1056
1057 inline Keyword& HDU::keyWord (const String& keyName)
1058 {
1059 std::map<String,Keyword*>::iterator key = m_keyWord.find(keyName);
1060 if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyName);
1061 return *((*key).second);
1062 }
1063
1064 inline long& HDU::naxis ()
1065 {
1066 return m_naxis;
1067 }
1068
1069 inline void HDU::naxis (const long& value)
1070 {
1071 m_naxis = value;
1072 }
1073
1074 inline bool& HDU::anynul ()
1075 {
1076 return m_anynul;
1077 }
1078
1079 inline void HDU::anynul (const bool& value)
1080 {
1081 m_anynul = value;
1082 }
1083
1084 inline const std::map<string,Keyword*>& HDU::keyWord () const
1085 {
1086 return m_keyWord;
1087 }
1088
1089 inline const Keyword& HDU::keyWord (const string& keyname) const
1090 {
1091 std::map<String,Keyword*>::const_iterator key = m_keyWord.find(keyname);
1092 if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyname);
1093 return *((*key).second);
1094 }
1095
1096 inline FITS*& HDU::parent ()
1097 {
1098 return m_parent;
1099 }
1100
1101 inline std::vector< long >& HDU::naxes ()
1102 {
1103 return m_naxes;
1104 }
1105
1106 inline long& HDU::naxes (size_t index)
1107 {
1108 return m_naxes[index];
1109 }
1110
1111 inline void HDU::naxes (size_t index, const long& value)
1112 {
1113 m_naxes[index] = value;
1114 }
1115
1116} // namespace CCfits
1117
1118#ifdef SPEC_TEMPLATE_IMP_DEFECT
1119namespace CCfits {
1120
1121 inline void HDU::readKeyMS(const String& keyName, int & val)
1122 {
1124 Keyword& key = readKeyword(keyName);
1125 key.value(val);
1126 }
1127
1128 inline void HDU::readKeys(std::vector<String>& keyNames, std::vector<String>& vals)
1129 {
1130 size_t nRead = keyNames.size();
1131
1132 std::list<String> valKeys;
1133 std::list<String> valList;
1134 for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
1135 // read all the keys requested, rejecting those that don't exist.
1136
1137 readKeywords(valKeys);
1138
1139 // get the values of all of the requested keys, rejecting those of the
1140 // wrong type.
1141
1142 String current;
1143 std::list<String>::iterator it = valKeys.begin();
1144 while (it != valKeys.end())
1145 {
1146 try
1147 {
1148 m_keyWord[*it]->value(current);
1149 valList.push_back(current);
1150 ++it;
1151 }
1152 catch ( Keyword::WrongKeywordValueType )
1153 {
1154 it = valKeys.erase(it);
1155 }
1156 }
1157
1158 keyNames.erase(keyNames.begin(),keyNames.end());
1159
1160 if (!valList.empty())
1161 {
1162 if (valList.size() != vals.size()) vals.resize(valList.size());
1163
1164 size_t i=0;
1165 std::list<String>::const_iterator it1 = valList.begin();
1166 for ( ; it1 != valList.end(); ++it1,++i)
1167 {
1168 vals[i] = *it1;
1169 }
1170 for ( it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
1171 {
1172 keyNames.push_back(*it1);
1173 }
1174 }
1175
1176 }
1177}
1178 #endif
1179
1180
1181#endif
Memory object representation of a disk FITS file.
Definition FITS.h:629
FitsException is the base class for all exceptions thrown by this library.
Definition FitsError.h:94
exception to be thrown if user requests extension type that can not be understood as ImageExt,...
Definition HDU.h:704
exception to be thrown if user requests creation of an image of type not supported by cfitsio.
Definition HDU.h:693
exception to be thrown on seek errors for keywords.
Definition HDU.h:726
exception to be thrown on seek errors for keywords.
Definition HDU.h:715
Base class for all HDU [Header-Data Unit] objects.
Definition HDU.h:687
std::map< String, Keyword * > & keyWord()
return the associative array containing the HDU keywords so far read.
Definition HDU.h:1052
Keyword & addKey(const String &name, T val, const String &comment, bool isLongStr=false)
create a new keyword in the HDU with specified value and comment fields
Definition HDU.h:901
void writeComment(const String &comment="Generic Comment")
write a comment string.
Definition HDU.cxx:403
const String & getHistory()
read the history information from the HDU and add it to the FITS object.
Definition HDU.cxx:425
fitsfile * fitsPointer() const
return the fitsfile pointer for the FITS object containing the HDU
Definition HDU.cxx:308
virtual HDU * clone(FITS *p) const =0
virtual copy constructor, to be implemented in subclasses.
void readKeys(std::vector< String > &keyNames, std::vector< T > &vals)
read a set of specified keywords of the same data type from the header of a disk FITS file and return...
Definition HDU.h:918
Keyword & readNextKey(const std::vector< String > &incList, const std::vector< String > &excList, bool searchFromBeginning=false)
Read the next key in the HDU which matches a string in incList, and does not match string in excList.
Definition HDU.cxx:783
std::vector< long > & naxes()
return the HDU data axis array.
Definition HDU.h:1101
void writeChecksum()
compute and write the DATASUM and CHECKSUM keyword values
Definition HDU.cxx:500
std::pair< int, int > verifyChecksum() const
verify the HDU by computing the checksums and comparing them with the CHECKSUM/DATASUM keywords
Definition HDU.cxx:518
std::pair< unsigned long, unsigned long > getChecksum() const
compute and return the checksum values for the HDU without creating or modifying the CHECKSUM/DATASUM...
Definition HDU.cxx:532
void writeDate()
write a date string to *this.
Definition HDU.cxx:460
void updateChecksum()
update the CHECKSUM keyword value, assuming DATASUM exists and is correct
Definition HDU.cxx:509
Keyword & addKeyNull(const String &name, const String &comment, bool isLongStr=false)
create a new keyword in the HDU with a null (ie. undefined) value.
Definition HDU.cxx:896
int index() const
return the HDU number
Definition HDU.h:1008
const String & getComments()
read the comments from the HDU and add it to the FITS object.
Definition HDU.cxx:391
static std::vector< int > keywordCategories()
Return the default enumerated keyword categories used by copyAllKeys()
Definition HDU.cxx:660
virtual double scale() const
return the BSCALE keyword value
Definition HDU.h:1023
virtual void suppressScaling(bool toggle=true)
turn off image scaling regardless of the BSCALE and BZERO keyword values
Definition HDU.cxx:483
bool operator!=(const HDU &right) const
inequality operator
Definition HDU.cxx:150
long axis(size_t index) const
return the size of axis numbered index [zero based].
Definition HDU.h:998
void copyAllKeys(const HDU *inHdu, const std::vector< int > &keyCategories=std::vector< int >())
copy all keys from another header
Definition HDU.cxx:617
void readKey(const String &keyName, T &val)
read a keyword of specified type from the header of a disk FITS file and return its value.
Definition HDU.h:910
void readAllKeys(const std::vector< int > &keyCategories=std::vector< int >())
read all of the keys in the header
Definition HDU.cxx:553
virtual void resetImageRead()
force next image reading operation to read from file instead of object cache.
Definition HDU.h:1043
virtual void makeThisCurrent() const
move the fitsfile pointer to this current HDU.
Definition HDU.cxx:318
FITS * parent() const
return reference to the pointer representing the FITS object containing the HDU
Definition HDU.cxx:313
long bitpix() const
return the data type keyword.
Definition HDU.h:1013
bool operator==(const HDU &right) const
equality operator
Definition HDU.cxx:145
const string & comment() const
return the comment string previously read by getComment()
Definition HDU.h:978
const string & history() const
return the history string previously read by getHistory()
Definition HDU.h:983
virtual ~HDU()
destructor
Definition HDU.cxx:138
long axes() const
return the number of axes in the HDU data section (always 2 for tables).
Definition HDU.h:993
void writeHistory(const String &history="Generic History String")
write a history string.
Definition HDU.cxx:438
void deleteKey(const String &doomed)
delete a keyword from the header
Definition HDU.cxx:543
virtual double zero() const
return the BZERO keyword value
Definition HDU.h:1033
Abstract base class defining the interface for Keyword objects.
Definition Keyword.h:199
T & value(T &val) const
get the keyword value
Definition KeywordT.h:29
const String & name() const
return the name of a keyword
Definition Keyword.h:322
virtual Keyword * clone() const =0
virtual copy constructor
Namespace enclosing all CCfits classes and globals definitions.
Definition AsciiTable.cxx:26
std::ostream & operator<<(std::ostream &s, const Column &right)
output operator for Column objects.
Definition Column.h:1337
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition CCfits.h:81