HDU.h
// Astrophysics Science Division,
// NASA/ Goddard Space Flight Center
// HEASARC
// http://heasarc.gsfc.nasa.gov
// e-mail: ccfits@legacy.gsfc.nasa.gov
//
// Original author: Ben Dorman
#ifndef HDU_H
#define HDU_H 1
#include <map>
// vector
#include <vector>
#include <list>
// CCfitsHeader
#include "CCfits.h"
// Keyword
#include "Keyword.h"
// NewKeyword
#include "NewKeyword.h"
// FitsError
#include "FitsError.h"
// FITSUtil
#include "FITSUtil.h"
namespace CCfits {
class FITS;
} // namespace CCfits
namespace CCfits {
class HDUCreator; // Needed for friend declaration
}
#ifdef _MSC_VER
#include "MSconfig.h" // for truncation warning
#endif
#include "KeywordT.h"
namespace CCfits {
class HDU
{
public:
class InvalidImageDataType : public FitsException //## Inherits: <unnamed>%394FBA12005C
{
public:
InvalidImageDataType (const string& diag, bool silent = true);
protected:
private:
private: //## implementation
};
class InvalidExtensionType : public FitsException //## Inherits: <unnamed>%3964C1D00352
{
public:
InvalidExtensionType (const string& diag, bool silent = true);
protected:
private:
private: //## implementation
};
class NoSuchKeyword : public FitsException //## Inherits: <unnamed>%398865D10264
{
public:
NoSuchKeyword (const string& diag, bool silent = true);
protected:
private:
private: //## implementation
};
class NoNullValue : public FitsException //## Inherits: <unnamed>%3B0D58CE0306
{
public:
NoNullValue (const string& diag, bool silent = true);
protected:
private:
private: //## implementation
};
HDU(const HDU &right);
bool operator==(const HDU &right) const;
bool operator!=(const HDU &right) const;
virtual HDU * clone (FITS* p) const = 0;
fitsfile* fitsPointer () const;
FITS* parent () const;
// By all means necessary, set the fitsfile pointer so that
// this HDU is the current HDU.
//
// This would appear to be a good candidate for the public
// interface.
virtual void makeThisCurrent () const;
const String& getComments ();
const string& comment () const;
// Write a comment string. A default value for the string is given
// "GenericComment" so users can put a placeholder call
// to this function in their code before knowing quite what should go in it.
void writeComment (const String& comment = "Generic Comment");
const String& getHistory ();
const string& history () const;
// Write a history string. A default value for the string is given
// "Generic History String" so users can put a placeholder call
// to this function in their code before knowing quite what should go in it.
void writeHistory (const String& history = "Generic History String");
// Write a date card.
void writeDate ();
friend std::ostream& operator << (std::ostream& s, const CCfits::HDU& right);
long axes () const;
long axis (size_t index) const;
void index (int value);
int index () const;
long bitpix () const;
virtual double scale () const;
virtual void scale (double value);
virtual double zero () const;
virtual void zero (double value);
virtual void resetImageRead ();
virtual void suppressScaling (bool toggle = true);
void writeChecksum ();
void updateChecksum ();
std::pair<int,int> verifyChecksum () const;
std::pair<unsigned long,unsigned long> getChecksum () const;
void deleteKey (const String& doomed);
void readAllKeys (const std::vector<int> & keyCategories = std::vector<int>());
void copyAllKeys (const HDU* inHdu, const std::vector<int> & keyCategories = std::vector<int>());
std::map<String, Keyword*>& keyWord ();
Keyword& keyWord (const String& keyName);
static std::vector<int> keywordCategories ();
const std::map<string,Keyword*>& keyWord () const;
const Keyword& keyWord (const string& keyname) const;
Keyword& readNextKey(const std::vector<String>& incList,
const std::vector<String>& excList,
bool searchFromBeginning = false);
public:
// Additional Public Declarations
template <typename T>
void readKey(const String& keyName, T& val);
template <typename T>
void readKeys(std::vector<String>& keyNames, std::vector<T>& vals);
template <typename T>
Keyword& addKey(const String& name, T val, const String& comment, bool isLongStr = false);
// This non-template function could be entered with Rose, but
// it's instead placed with the other addKey function to
// simplify the Doxygen generated doc file output.
Keyword* addKey(const Keyword* inKeyword);
Keyword& addKey(const String& name, const char* charString, const String& comment, bool isLongStr = false);
#ifdef TEMPLATE_AMBIG_DEFECT
inline void readKeyMS(const String& keyName, int & val);
inline void readKeys(std::vector<String>& keyNames, std::vector<String>& vals);
#endif
protected:
// Functions as the default constructor, which is required for
// the map container class.
HDU (FITS* p = 0);
HDU (FITS* p, int bitpix, int naxis, const std::vector<long>& axes);
virtual ~HDU();
Keyword& readKeyword (const String &keyname);
void readKeywords (std::list<String>& keynames);
virtual std::ostream & put (std::ostream &s) const = 0;
void bitpix (long value);
bool checkImgDataTypeChange (double zero, double scale) const;
long& naxis ();
void naxis (const long& value);
// Flags whether there were any null values found in the
// last read operation.
bool& anynul ();
void anynul (const bool& value);
FITS*& parent ();
std::vector< long >& naxes ();
long& naxes (size_t index);
void naxes (size_t index, const long& value);
// Additional Protected Declarations
private:
// clear the FITS Keyword map. To be called by
// the dtor and the copy/assignment operations.
void clearKeys ();
virtual void initRead () = 0;
void readHduInfo ();
Keyword* addKeyword (Keyword* newKey);
virtual bool compare (const HDU &right) const;
// clear the FITS Keyword map. To be called by
// the dtor and the copy/assignment operations.
void copyKeys (const HDU& right);
String getNamedLines (const String& name);
// save keyword found by read all keys into the array of keywords that have been read.
// Similar to addKeyword except there's no write and no returned value. For use by readAllKeys()
void saveReadKeyword (Keyword* newKey);
void zeroInit (double value);
void scaleInit (double value);
// Additional Private Declarations
private: //## implementation
// Data Members for Class Attributes
long m_naxis;
long m_bitpix;
int m_index;
bool m_anynul;
string m_history;
string m_comment;
double m_zero;
// Floating point scale factor for image data that takes
// the value of the BSCALE parameter.
double m_scale;
// Data Members for Associations
std::map<string,Keyword*> m_keyWord;
FITS* m_parent;
std::vector< long > m_naxes;
// Additional Implementation Declarations
static const size_t s_nCategories;
static const int s_iKeywordCategories[];
friend class HDUCreator;
friend Keyword* KeywordCreator::getKeyword(const String& keyname, HDU* p);
friend Keyword* KeywordCreator::getKeyword(const String& keyname, ValueType keyType, HDU* p);
};
template <typename T>
Keyword& HDU::addKey(const String& name, T value, const String& comment, bool isLongStr)
{
makeThisCurrent();
NewKeyword<T> keyCreator(this,value);
Keyword& newKey = *(addKeyword(keyCreator.createKeyword(name,comment,isLongStr)));
return newKey;
}
template <typename T>
void HDU::readKey(const String& keyName, T& val)
{
makeThisCurrent();
Keyword& key = readKeyword(keyName);
key.value(val);
}
template <typename T>
void HDU::readKeys(std::vector<String>& keyNames, std::vector<T>& vals)
{
size_t nRead = keyNames.size();
std::list<String> valKeys;
std::list<T> valList;
for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
// read all the keys requested, rejecting those that don't exist.
readKeywords(valKeys);
// get the values of all of the requested keys, rejecting those of the
// wrong type.
T current;
std::list<String>::iterator it = valKeys.begin();
while (it != valKeys.end())
{
try
{
m_keyWord[*it]->value(current);
valList.push_back(current);
++it;
}
catch ( Keyword::WrongKeywordValueType )
{
it = valKeys.erase(it);
}
}
keyNames.erase(keyNames.begin(),keyNames.end());
if (!valList.empty())
{
if (valList.size() != vals.size()) vals.resize(valList.size());
size_t i=0;
for (typename std::list<T>::const_iterator it1
= valList.begin(); it1 != valList.end(); ++it1,++i)
{
vals[i] = *it1;
}
for (std::list<String>::const_iterator it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
{
keyNames.push_back(*it1);
}
}
}
// Class CCfits::HDU::InvalidImageDataType
// Class CCfits::HDU::InvalidExtensionType
// Class CCfits::HDU::NoSuchKeyword
// Class CCfits::HDU::NoNullValue
// Class CCfits::HDU
inline const string& HDU::comment () const
{
return m_comment;
}
inline const string& HDU::history () const
{
return m_history;
}
inline std::ostream& operator << (std::ostream& s, const CCfits::HDU& right)
{
return right.put(s);
}
inline long HDU::axes () const
{
return m_naxis;
}
inline long HDU::axis (size_t index) const
{
return m_naxes[index];
}
inline void HDU::index (int value)
{
m_index = value;
}
inline int HDU::index () const
{
return m_index;
}
inline long HDU::bitpix () const
{
return m_bitpix;
}
inline void HDU::bitpix (long value)
{
m_bitpix = value;
}
inline double HDU::scale () const
{
return m_scale;
}
inline void HDU::scale (double value)
{
m_scale = value;
}
inline double HDU::zero () const
{
return m_zero;
}
inline void HDU::zero (double value)
{
m_zero = value;
}
inline void HDU::resetImageRead ()
{
}
inline void HDU::saveReadKeyword (Keyword* newKey)
{
m_keyWord.insert(std::map<String,Keyword*>::value_type(newKey->name(),newKey->clone()));
}
inline std::map<String, Keyword*>& HDU::keyWord ()
{
return m_keyWord;
}
inline Keyword& HDU::keyWord (const String& keyName)
{
std::map<String,Keyword*>::iterator key = m_keyWord.find(keyName);
if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyName);
return *((*key).second);
}
inline long& HDU::naxis ()
{
return m_naxis;
}
inline void HDU::naxis (const long& value)
{
m_naxis = value;
}
inline bool& HDU::anynul ()
{
return m_anynul;
}
inline void HDU::anynul (const bool& value)
{
m_anynul = value;
}
inline const std::map<string,Keyword*>& HDU::keyWord () const
{
return m_keyWord;
}
inline const Keyword& HDU::keyWord (const string& keyname) const
{
std::map<String,Keyword*>::const_iterator key = m_keyWord.find(keyname);
if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyname);
return *((*key).second);
}
inline FITS*& HDU::parent ()
{
return m_parent;
}
inline std::vector< long >& HDU::naxes ()
{
return m_naxes;
}
inline long& HDU::naxes (size_t index)
{
return m_naxes[index];
}
inline void HDU::naxes (size_t index, const long& value)
{
m_naxes[index] = value;
}
} // namespace CCfits
#ifdef SPEC_TEMPLATE_IMP_DEFECT
namespace CCfits {
inline void HDU::readKeyMS(const String& keyName, int & val)
{
makeThisCurrent();
Keyword& key = readKeyword(keyName);
key.value(val);
}
inline void HDU::readKeys(std::vector<String>& keyNames, std::vector<String>& vals)
{
size_t nRead = keyNames.size();
std::list<String> valKeys;
std::list<String> valList;
for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
// read all the keys requested, rejecting those that don't exist.
readKeywords(valKeys);
// get the values of all of the requested keys, rejecting those of the
// wrong type.
String current;
std::list<String>::iterator it = valKeys.begin();
while (it != valKeys.end())
{
try
{
m_keyWord[*it]->value(current);
valList.push_back(current);
++it;
}
catch ( Keyword::WrongKeywordValueType )
{
it = valKeys.erase(it);
}
}
keyNames.erase(keyNames.begin(),keyNames.end());
if (!valList.empty())
{
if (valList.size() != vals.size()) vals.resize(valList.size());
size_t i=0;
std::list<String>::const_iterator it1 = valList.begin();
for ( ; it1 != valList.end(); ++it1,++i)
{
vals[i] = *it1;
}
for ( it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
{
keyNames.push_back(*it1);
}
}
}
}
#endif
#endif
CCfits::HDU::getChecksum
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
CCfits::HDU::writeChecksum
void writeChecksum()
compute and write the DATASUM and CHECKSUM keyword values
Definition: HDU.cxx:500
CCfits::HDU::keywordCategories
static std::vector< int > keywordCategories()
Return the default enumerated keyword categories used by copyAllKeys()
Definition: HDU.cxx:660
CCfits::HDU::writeDate
void writeDate()
write a date string to *this.
Definition: HDU.cxx:460
CCfits::HDU::NoSuchKeyword
exception to be thrown on seek errors for keywords.
Definition: HDU.h:701
CCfits::HDU::writeComment
void writeComment(const String &comment="Generic Comment")
write a comment string.
Definition: HDU.cxx:403
CCfits::HDU::parent
FITS * parent() const
return reference to the pointer representing the FITS object containing the HDU
Definition: HDU.cxx:313
CCfits::Keyword::clone
virtual Keyword * clone() const =0
virtual copy constructor
CCfits::HDU::axis
long axis(size_t index) const
return the size of axis numbered index [zero based].
Definition: HDU.h:983
CCfits::HDU::operator!=
bool operator!=(const HDU &right) const
inequality operator
Definition: HDU.cxx:150
CCfits::HDU::NoNullValue::NoNullValue
NoNullValue(const string &diag, bool silent=true)
Exception ctor, prefixes the string &quot;Fits Error: No Null Pixel Value specified for Image &quot; before the...
Definition: HDU.cxx:76
CCfits::HDU::bitpix
long bitpix() const
return the data type keyword.
Definition: HDU.h:998
CCfits::HDU::deleteKey
void deleteKey(const String &doomed)
delete a keyword from the header
Definition: HDU.cxx:543
CCfits::HDU::clone
virtual HDU * clone(FITS *p) const =0
virtual copy constructor, to be implemented in subclasses.
CCfits::HDU::fitsPointer
fitsfile * fitsPointer() const
return the fitsfile pointer for the FITS object containing the HDU
Definition: HDU.cxx:308
CCfits::Keyword::name
const String & name() const
return the name of a keyword
Definition: Keyword.h:320
CCfits::HDU::verifyChecksum
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
CCfits::HDU::NoNullValue
exception to be thrown on seek errors for keywords.
Definition: HDU.h:712
CCfits::Keyword
Abstract base class defining the interface for Keyword objects.
Definition: Keyword.h:198
CCfits::HDU
Base class for all HDU [Header-Data Unit] objects.
Definition: HDU.h:673
CCfits::HDU::keyWord
std::map< String, Keyword * > & keyWord()
return the associative array containing the HDU keywords so far read.
Definition: HDU.h:1037
CCfits::HDU::resetImageRead
virtual void resetImageRead()
force next image reading operation to read from file instead of object cache.
Definition: HDU.h:1028
CCfits::HDU::readNextKey
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
CCfits::HDU::getHistory
const String & getHistory()
read the history information from the HDU and add it to the FITS object.
Definition: HDU.cxx:425
CCfits::HDU::InvalidExtensionType::InvalidExtensionType
InvalidExtensionType(const string &diag, bool silent=true)
Exception ctor, prefixes the string &quot;Fits Error: Extension Type: &quot; before the specific message...
Definition: HDU.cxx:54
CCfits::FITS
Memory object representation of a disk FITS file.
Definition: FITS.h:628
CCfits::FitsException
FitsException is the base class for all exceptions thrown by this library.
Definition: FitsError.h:93
CCfits::HDU::readKey
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:895
CCfits::ValueType
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition: CCfits.h:81
CCfits::HDU::index
int index() const
return the HDU number
Definition: HDU.h:993
CCfits::HDU::makeThisCurrent
virtual void makeThisCurrent() const
move the fitsfile pointer to this current HDU.
Definition: HDU.cxx:318
CCfits::HDU::HDU
HDU(const HDU &right)
copy constructor
Definition: HDU.cxx:86
CCfits::HDU::readAllKeys
void readAllKeys(const std::vector< int > &keyCategories=std::vector< int >())
read all of the keys in the header
Definition: HDU.cxx:553
CCfits::operator<<
std::ostream & operator<<(std::ostream &s, const Column &right)
output operator for Column objects.
Definition: Column.h:1337
CCfits::HDU::writeHistory
void writeHistory(const String &history="Generic History String")
write a history string.
Definition: HDU.cxx:438
CCfits::Keyword::value
T & value(T &val) const
get the keyword value
Definition: KeywordT.h:29
CCfits::HDU::axes
long axes() const
return the number of axes in the HDU data section (always 2 for tables).
Definition: HDU.h:978
CCfits::HDU::~HDU
virtual ~HDU()
destructor
Definition: HDU.cxx:138
CCfits::HDU::NoSuchKeyword::NoSuchKeyword
NoSuchKeyword(const string &diag, bool silent=true)
Exception ctor, prefixes the string &quot;Fits Error: Keyword not found: &quot; before the specific message...
Definition: HDU.cxx:66
CCfits::HDU::copyAllKeys
void copyAllKeys(const HDU *inHdu, const std::vector< int > &keyCategories=std::vector< int >())
copy all keys from another header
Definition: HDU.cxx:617
CCfits::HDU::getComments
const String & getComments()
read the comments from the HDU and add it to the FITS object.
Definition: HDU.cxx:391
CCfits::HDU::InvalidExtensionType
exception to be thrown if user requests extension type that can not be understood as ImageExt...
Definition: HDU.h:690
CCfits::HDU::operator==
bool operator==(const HDU &right) const
equality operator
Definition: HDU.cxx:145
CCfits::HDU::naxes
std::vector< long > & naxes()
return the HDU data axis array.
Definition: HDU.h:1086
CCfits::HDU::history
const string & history() const
return the history string previously read by getHistory()
Definition: HDU.h:968
CCfits::HDU::scale
virtual double scale() const
return the BSCALE keyword value
Definition: HDU.h:1008
CCfits::HDU::zero
virtual double zero() const
return the BZERO keyword value
Definition: HDU.h:1018
CCfits::HDU::updateChecksum
void updateChecksum()
update the CHECKSUM keyword value, assuming DATASUM exists and is correct
Definition: HDU.cxx:509
CCfits::HDU::InvalidImageDataType::InvalidImageDataType
InvalidImageDataType(const string &diag, bool silent=true)
Exception ctor, prefixes the string &quot;Fits Error: Invalid Data Type for Image &quot; before the specific me...
Definition: HDU.cxx:44
CCfits::HDU::comment
const string & comment() const
return the comment string previously read by getComment()
Definition: HDU.h:963
CCfits::HDU::readKeys
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:903
CCfits::HDU::addKey
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:886
CCfits::HDU::suppressScaling
virtual void suppressScaling(bool toggle=true)
turn off image scaling regardless of the BSCALE and BZERO keyword values
Definition: HDU.cxx:483
CCfits::HDU::InvalidImageDataType
exception to be thrown if user requests creation of an image of type not supported by cfitsio...
Definition: HDU.h:679