// 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 FITSUTIL_H #define FITSUTIL_H 1 #include "CCfits.h" // functional #include // complex #include // valarray #include // vector #include // string #include // FitsError #include "FitsError.h" #include namespace CCfits { namespace FITSUtil { /*! \namespace FITSUtil \brief FITSUtil is a namespace containing functions used internally by CCfits, but which might be of use for other applications. */ /*! \class MatchName \brief predicate for classes that have a name attribute; match input string with instance name. Usage: MatchName Ex; list ListObject; // ... ... // find_if(ListObject.begin(),ListObject().end(),bind2nd(Ex,"needle")); Since most of the classes within CCfits are not implemented with lists, these functions are now of little direct use. */ /*! \class MatchPtrName \brief as for MatchName, only with the input class a pointer. */ /*! \class MatchNum \brief predicate for classes that have an index attribute; match input index with instance value. Usage: MatchName Ex; list ListObject; // ... ... // find_if(ListObject.begin(),ListObject().end(),bind2nd(Ex,5)); Since most of the classes within CCfits are implemented with std::maps rather than lists, these functions are now of little direct use. */ /*! \class MatchPtrNum \brief as for MatchNum, only with the input class a pointer. */ /*! \class MatchType \brief function object that returns the FITS ValueType corresponding to an input intrinsic type This is particularly useful inside templated class instances where calls to cfitsio need to supply a value type. With this function one can extract the value type from the class type. usage: MatchType type; ValueType dataType = type(); Uses run-time type information (RTTI) methods. */ /*! \class UnrecognizedType @ingroup FITSexcept @brief exception thrown by MatchType if it encounters data type incompatible with cfitsio. */ /*! \class auto_array_ptr \brief A class that mimics the std:: library auto_ptr class, but works with arrays. This code was written by Jack Reeves and first appeared C++ Report, March 1996 edition. Although some authors think one shouldn't need such a contrivance, there seems to be a need for it when wrapping C code. Usage: replace float* f = new float[200]; with FITSUtil::auto_array_ptr f(new float[200]); Then the memory will be managed correctly in the presence of exceptions, and delete will be called automatically for f when leaving scope. */ /*! \fn explicit auto_array_ptr::auto_array_ptr (X* p = 0) throw (); \brief constructor. allows creation of pointer to null, can be modified by reset() */ /*! \fn explicit auto_array_ptr::auto_array_ptr (auto_array_ptr& right) throw (); \brief copy constructor */ /*!\fn auto_array_ptr::~auto_array_ptr(); \brief destructor. */ /*!\fn void auto_array_ptr::operator = (auto_array_ptr& right); \brief assignment operator: transfer of ownership semantics */ /*!\fn X& auto_array_ptr::operator * () throw (); \brief deference operator */ /*!\fn X& auto_array_ptr::operator [] (size_t i) throw (); \brief return a reference to the ith element of the array */ /*!\fn X auto_array_ptr::operator [] (size_t i) const throw (); \brief return a copy of the ith element of the array */ /*!\fn X* auto_array_ptr::get () const; \brief return a token for the underlying content of *this */ /*!\fn X* auto_array_ptr::release () throw (); \brief return underlying content of *this, transferring memory ownership */ /*!\fn X* auto_array_ptr::reset (X* p) throw (); \brief change the content of the auto_array_ptr to p */ /*!\fn static void auto_array_ptr::remove (X*& x) throw (); \brief utility function to delete the memory owned by x and set it to null. */ /*! \fn char** CharArray(const std::vector& inArray) \brief function object that returns a C-array of strings from a std::vector< std::vector > object, such as used in a string valued Table column. This exists because the return type for a specialization (T**) is incompatible with typenames T other than string, which return a T*. */ /*! \class CVarray \brief Function object class for returning C arrays from standard library objects used in the FITS library implementation. There are 3 versions which convert std::vector, std::valarray, and std::vector > objects to pointers to T, called CVarray, CAarray, and CVAarray. An alternative function, CharArray, is provided to deal with the special case of vector string arrays. */ /*! \fn T* CVarray::operator () (const std::vector& inArray); \brief operator returning C array for use with scalar column data. */ /*! \class CAarray \brief function object returning C array from a valarray. see CVarray for details */ /*! \fn T* CAarray::operator () (const std::valarray& inArray); \brief operator returning C array for use with image data. */ /*! \class CVAarray \brief function object returning C array from a vector of valarrays. see CVarray for details */ /*! \fn T* CVAarray::operator () (const std::vector< std::valarray >& inArray); \brief operator returning C array for use with vector column data. */ /*! \fn template void fill(std::vector< S > &outArray, const std::vector< T > &inArray, size_t first, size_t last); \brief Convert input vector of type T to output vector of type S. The functions FITSUtil::fill do conversions between CCfits' internal representation and user's input types. They encapsulate the task of memory allocation also, facilitating support of implicit conversions between the users data type and representation and what is required by the CCfits implementation. For example a user can create a std::vector for storage in a single row of a Binary Table column, of type long. The internal representation is a std::valarray object. \param outArray output data \param inArray input data \param first first element of inArray to be written to outArray \param last last element of inArray to be written to outArray */ #ifdef _MSC_VER #include "MSconfig.h" // for truncation double to float warning #endif template void swap(T& left,T& right); template void swap(std::vector& left, std::vector& right); string lowerCase(const string& inputString); string upperCase(const string& inputString); // Check if a file name includes an image compression specifier, // and return its location if it exists. string::size_type checkForCompressString(const string& fileName); struct InvalidConversion : public FitsException { InvalidConversion(const string& diag, bool silent=false); }; struct MatchStem : public std::binary_function { bool operator()(const string& left, const string& right) const; }; static const double d1(0); static const float f1(0); static const std::complex c1(0.); static const std::complex d2(0.); static const string s1(""); static const int i1(0); static const unsigned int u1(0); static const long l1(0); static const unsigned long ul1(0); static const LONGLONG ll1(0); static const short s2(0); static const unsigned short us1(0); static const bool b1(false); static const unsigned char b2(0); char** CharArray(const std::vector& inArray); string FITSType2String( int typeInt ); template void fill(std::vector& outArray, const std::vector& inArray,size_t first, size_t last); template void fill(std::valarray& outArray, const std::valarray& inArray); template void fill(std::valarray& outArray, const std::vector& inArray,size_t first, size_t last); template void fill(std::vector& outArray, const std::valarray& inArray); // VF<-AF void fill(std::vector >& outArray, const std::valarray >& inArray); // VF<-AD void fill(std::vector >& outArray, const std::valarray >& inArray); // VD<-AD void fill(std::vector >& outArray, const std::valarray >& inArray); // VD<-AF void fill(std::vector >& outArray, const std::valarray >& inArray); template void fill(std::vector& outArray, const std::vector& inArray, size_t first, size_t last); template void fill(std::vector& outArray, const std::vector& inArray, size_t first, size_t last); template void fill(std::valarray& outArray, const std::vector& inArray,size_t first, size_t last); // template // void fill(std::valarray >& outArray, const std::valarray >& inArray); // seems no other way of doing this. // VF<-VF #ifdef TEMPLATE_AMBIG_DEFECT void fillMSvfvf(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif void fill(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); // VF<-VD #ifdef TEMPLATE_AMBIG_DEFECT void fillMSvfvd(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif void fill(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); // VD<-VD #ifdef TEMPLATE_AMBIG_DEFECT void fillMSvdvd(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif void fill(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); #ifdef TEMPLATE_AMBIG_DEFECT void fillMSvdvf(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); #else void fill(std::vector >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif // AF<-VD void fill(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); // AF<-VF #ifdef TEMPLATE_AMBIG_DEFECT void fillMSafvf(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); #else void fill(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif // AD<-VF #ifdef TEMPLATE_AMBIG_DEFECT void fillMSadvf(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); #else void fill(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif // AD<-VD #ifdef TEMPLATE_AMBIG_DEFECT void fillMSadvd(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); #else void fill(std::valarray >& outArray, const std::vector >& inArray, size_t first, size_t last); #endif // AF<-AF void fill(std::valarray >& outArray, const std::valarray >& inArray); // AD<-AD void fill(std::valarray >& outArray, const std::valarray >& inArray); // AF<-AD void fill(std::valarray >& outArray, const std::valarray >& inArray); // AD<-AF void fill(std::valarray >& outArray, const std::valarray >& inArray); #if TEMPLATE_AMBIG_DEFECT || TEMPLATE_AMBIG7_DEFECT void fillMSvsvs(std::vector& outArray, const std::vector& inArray, size_t first, size_t last); #endif void fill(std::vector& outArray, const std::vector& inArray, size_t first, size_t last); template string errorMessage(const S& out, const T& in); template struct MatchPtrName : public std::binary_function //## Inherits: %39491BC9025D { // Parameterized Class MatchPtrName bool operator () (const T& left, const string& right) const; public: protected: private: private: //## implementation }; template struct MatchName : public std::binary_function //## Inherits: %39491BC50121 { bool operator () (const T& left, const string& right) const; public: protected: private: private: //## implementation }; template struct MatchNum : public std::binary_function //## Inherits: %39491BCE01C0 { bool operator () (const T& left, const int& right) const; public: protected: private: private: //## implementation }; template struct MatchType { ValueType operator () (); public: protected: private: private: //## implementation }; template struct CVarray { T* operator () (const std::vector& inArray); public: protected: private: private: //## implementation }; template struct FitsNullValue { T operator () (); public: protected: private: private: //## implementation }; template struct MatchImageType { ImageType operator () (); public: protected: private: private: //## implementation }; template struct MatchPtrNum : public std::binary_function //## Inherits: %39491BD3034B { bool operator () (const T& left, const int& right) const; public: protected: private: private: //## implementation }; // auto_ptr analogue for arrays. template class auto_array_ptr { public: explicit auto_array_ptr (X* p = 0) throw (); explicit auto_array_ptr (auto_array_ptr& right) throw (); ~auto_array_ptr(); void operator = (auto_array_ptr& right); X& operator * () throw (); X& operator [] (size_t i) throw (); X operator [] (size_t i) const throw (); X* get () const; X* release () throw (); X* reset (X* p) throw (); static void remove (X*& x); protected: private: private: //## implementation // Data Members for Class Attributes X* m_p; }; template struct ComparePtrIndex : public std::binary_function //## Inherits: %3B24DB930299 { bool operator () (const T* left, const T* right); public: protected: private: private: //## implementation }; template struct CAarray { T* operator () (const std::valarray& inArray); public: protected: private: private: //## implementation }; template struct CVAarray { T* operator () (const std::vector< std::valarray >& inArray); public: protected: private: private: //## implementation }; class UnrecognizedType : public FitsException //## Inherits: %3CE143AB00C6 { public: UnrecognizedType (string diag, bool silent = true); protected: private: private: //## implementation }; // Parameterized Class CCfits::FITSUtil::MatchPtrName template inline bool MatchPtrName::operator () (const T& left, const string& right) const { return left->name() == right; } // Parameterized Class CCfits::FITSUtil::MatchName template inline bool MatchName::operator () (const T& left, const string& right) const { return left.name() == right; } // Parameterized Class CCfits::FITSUtil::MatchNum template inline bool MatchNum::operator () (const T& left, const int& right) const { return left.index() == right; } // Parameterized Class CCfits::FITSUtil::MatchType // Parameterized Class CCfits::FITSUtil::CVarray template inline T* CVarray::operator () (const std::vector& inArray) { // convert std containers used commonly in FITS to C arrays in an exception // safe manner that is also clear about resource ownership. auto_array_ptr pC(new T[inArray.size()]); T* c = pC.get(); std::copy(inArray.begin(),inArray.end(),&c[0]); return pC.release(); } // Parameterized Class CCfits::FITSUtil::FitsNullValue template inline T FitsNullValue::operator () () { // This works for int types. Float, complex, and string types // are handled below with specialized templates. return 0; } // Parameterized Class CCfits::FITSUtil::MatchImageType // Parameterized Class CCfits::FITSUtil::MatchPtrNum template inline bool MatchPtrNum::operator () (const T& left, const int& right) const { return left->index() == right; } // Parameterized Class CCfits::FITSUtil::auto_array_ptr // Parameterized Class CCfits::FITSUtil::ComparePtrIndex // Parameterized Class CCfits::FITSUtil::CAarray // Parameterized Class CCfits::FITSUtil::CVAarray // Class CCfits::FITSUtil::UnrecognizedType // Parameterized Class CCfits::FITSUtil::MatchPtrName // Parameterized Class CCfits::FITSUtil::MatchName // Parameterized Class CCfits::FITSUtil::MatchNum // Parameterized Class CCfits::FITSUtil::MatchType template ValueType MatchType::operator () () { if ( typeid(T) == typeid(d1) ) return Tdouble; if ( typeid(T) == typeid(f1) ) return Tfloat; if ( typeid(T) == typeid(c1) ) return Tcomplex; if ( typeid(T) == typeid(d2) ) return Tdblcomplex; if ( typeid(T) == typeid(s1) ) return Tstring; if ( typeid(T) == typeid(i1) ) return Tint; if ( typeid(T) == typeid(u1) ) return Tuint; if ( typeid(T) == typeid(s2) ) return Tshort; if ( typeid(T) == typeid(us1) ) return Tushort; if ( typeid(T) == typeid(b1) ) return Tlogical; if ( typeid(T) == typeid(b2) ) return Tbyte; if ( typeid(T) == typeid(l1) ) return Tlong; if ( typeid(T) == typeid(ul1) ) return Tulong; // Carefull, on some compilers LONGLONG == long, // so this should go after test for long. if ( typeid(T) == typeid(ll1) ) return Tlonglong; throw UnrecognizedType("Invalid data type for FITS Data I/O\n"); } // Parameterized Class CCfits::FITSUtil::CVarray // Parameterized Class CCfits::FITSUtil::MatchImageType template ImageType MatchImageType::operator () () { if ( typeid(T) == typeid(b2) ) return Ibyte; if ( typeid(T) == typeid(s2) ) return Ishort; if ( typeid(T) == typeid(l1) ) return Ilong; if ( typeid(T) == typeid(f1) ) return Ifloat; if ( typeid(T) == typeid(d1) ) return Idouble; if ( typeid(T) == typeid(us1) ) return Iushort; if ( typeid(T) == typeid(ul1) ) return Iulong; if ( typeid(T) == typeid(ll1) ) return Ilonglong; MatchType errType; string diag ("Image: "); diag += FITSType2String(errType()); throw UnrecognizedType(diag); } // Parameterized Class CCfits::FITSUtil::MatchPtrNum // Parameterized Class CCfits::FITSUtil::auto_array_ptr template auto_array_ptr::auto_array_ptr (X* p) throw () : m_p(p) { } template auto_array_ptr::auto_array_ptr (auto_array_ptr& right) throw () : m_p(right.release()) { } template auto_array_ptr::~auto_array_ptr() { delete [] m_p; } template void auto_array_ptr::operator = (auto_array_ptr& right) { if (this != &right) { remove(m_p); m_p = right.release(); } } template X& auto_array_ptr::operator * () throw () { return *m_p; } template X& auto_array_ptr::operator [] (size_t i) throw () { return m_p[i]; } template X auto_array_ptr::operator [] (size_t i) const throw () { return m_p[i]; } template X* auto_array_ptr::get () const { return m_p; } template X* auto_array_ptr::release () throw () { return reset(0); } template X* auto_array_ptr::reset (X* p) throw () { // set the auto_ptr to manage p and return the old pointer it was managing. X* __tmp = m_p; m_p = p; return __tmp; } template void auto_array_ptr::remove (X*& x) { X* __tmp(x); x = 0; delete [] __tmp; } // Parameterized Class CCfits::FITSUtil::ComparePtrIndex template bool ComparePtrIndex::operator () (const T* left, const T* right) { return (left->index() < right->index()); } // Parameterized Class CCfits::FITSUtil::CAarray template T* CAarray::operator () (const std::valarray& inArray) { size_t n(inArray.size()); auto_array_ptr pC(new T[n]); T* c= pC.get(); for (size_t j = 0; j < n; ++j) c[j] = inArray[j]; return pC.release(); } // Parameterized Class CCfits::FITSUtil::CVAarray template T* CVAarray::operator () (const std::vector< std::valarray >& inArray) { size_t sz(0); size_t n(inArray.size()); std::vector nr(n); size_t i = 0; // for MS VC++ bug for ( i = 0; i < n; ++i) { nr[i] = inArray[i].size(); sz += nr[i]; } auto_array_ptr pC(new T[sz]); T* c = pC.get(); size_t k(0); for ( i = 0; i < n; ++i) { size_t& m = nr[i]; const std::valarray& current = inArray[i]; for (size_t j=0; j < m ; ++j) c[k++] = current[j]; } return pC.release(); } } // namespace FITSUtil } // namespace CCfits namespace CCfits { namespace FITSUtil { template void swap(T& left, T& right) { T temp(left); left = right; right = temp; } template void swap(std::vector& left, std::vector& right) { left.swap(right); } template <> inline string FitsNullValue::operator () () { return string(""); } template <> inline float FitsNullValue::operator () () { return FLOATNULLVALUE; } template <> inline double FitsNullValue::operator () () { return DOUBLENULLVALUE; } template <> inline std::complex FitsNullValue >::operator () () { return std::complex(FLOATNULLVALUE); } template <> inline std::complex FitsNullValue >::operator () () { return std::complex(DOUBLENULLVALUE); } } // end namespace FITSUtil } // end namespace CCfits #endif