FITSUtil.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 FITSUTIL_H
#define FITSUTIL_H 1
#include "CCfits.h"
// functional
#include <functional>
// complex
#include <complex>
// valarray
#include <valarray>
// vector
#include <vector>
// string
#include <string>
// FitsError
#include "FitsError.h"
#include <typeinfo>
namespace CCfits {
namespace FITSUtil {
#ifdef _MSC_VER
#include "MSconfig.h" // for truncation double to float warning
#endif
template <typename T>
void swap(T& left,T& right);
template <typename T>
void swap(std::vector<T>& left, std::vector<T>& 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
{
bool operator()(const string& left, const string& right) const;
};
static const double d1(0);
static const float f1(0);
static const std::complex<float> c1(0.);
static const std::complex<double> 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<string>& inArray);
string FITSType2String( int typeInt );
template <typename S, typename T>
void fill(std::vector<S>& outArray, const std::vector<T>& inArray,size_t first, size_t last);
template <typename S, typename T>
void fill(std::valarray<S>& outArray, const std::valarray<T>& inArray);
template <typename S, typename T>
void fill(std::valarray<S>& outArray, const std::vector<T>& inArray,size_t first, size_t last);
template <typename S, typename T>
void fill(std::vector<S>& outArray, const std::valarray<T>& inArray);
// VF<-AF
void fill(std::vector<std::complex<float> >& outArray,
const std::valarray<std::complex<float> >& inArray);
// VF<-AD
void fill(std::vector<std::complex<float> >& outArray,
const std::valarray<std::complex<double> >& inArray);
// VD<-AD
void fill(std::vector<std::complex<double> >& outArray,
const std::valarray<std::complex<double> >& inArray);
// VD<-AF
void fill(std::vector<std::complex<double> >& outArray,
const std::valarray<std::complex<float> >& inArray);
template <typename T>
void fill(std::vector<string>& outArray, const std::vector<T>& inArray, size_t first, size_t last);
template <typename T>
void fill(std::vector<T>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
template <typename S>
void fill(std::valarray<S>& outArray, const std::vector<string>& inArray,size_t first, size_t last);
// template <typename S, typename T>
// void fill(std::valarray<std::complex<S> >& outArray, const std::valarray<std::complex<T> >& inArray);
// seems no other way of doing this.
// VF<-VF
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSvfvf(std::vector<std::complex<float> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
#endif
void fill(std::vector<std::complex<float> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
// VF<-VD
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSvfvd(std::vector<std::complex<float> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
#endif
void fill(std::vector<std::complex<float> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
// VD<-VD
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSvdvd(std::vector<std::complex<double> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
#endif
void fill(std::vector<std::complex<double> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSvdvf(std::vector<std::complex<double> >& outArray,
const std::vector<std::complex<float> >& inArray,
size_t first, size_t last);
#else
void fill(std::vector<std::complex<double> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
#endif
// AF<-VD
void fill(std::valarray<std::complex<float> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
// AF<-VF
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSafvf(std::valarray<std::complex<float> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
#else
void fill(std::valarray<std::complex<float> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
#endif
// AD<-VF
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSadvf(std::valarray<std::complex<double> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
#else
void fill(std::valarray<std::complex<double> >& outArray,
const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
#endif
// AD<-VD
#ifdef TEMPLATE_AMBIG_DEFECT
void fillMSadvd(std::valarray<std::complex<double> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
#else
void fill(std::valarray<std::complex<double> >& outArray,
const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
#endif
// AF<-AF
void fill(std::valarray<std::complex<float> >& outArray,
const std::valarray<std::complex<float> >& inArray);
// AD<-AD
void fill(std::valarray<std::complex<double> >& outArray,
const std::valarray<std::complex<double> >& inArray);
// AF<-AD
void fill(std::valarray<std::complex<float> >& outArray,
const std::valarray<std::complex<double> >& inArray);
// AD<-AF
void fill(std::valarray<std::complex<double> >& outArray,
const std::valarray<std::complex<float> >& inArray);
#if TEMPLATE_AMBIG_DEFECT || TEMPLATE_AMBIG7_DEFECT
void fillMSvsvs(std::vector<string>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
#endif
void fill(std::vector<string>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
template <typename S, typename T>
string errorMessage(const S& out, const T& in);
template <class T>
struct MatchPtrName
{
// Parameterized Class MatchPtrName
bool operator () (const T& left, const string& right) const;
public:
protected:
private:
private: //## implementation
};
template <class T>
struct MatchName
{
bool operator () (const T& left, const string& right) const;
public:
protected:
private:
private: //## implementation
};
template <class T>
struct MatchNum
{
bool operator () (const T& left, const int& right) const;
public:
protected:
private:
private: //## implementation
};
template <typename T>
struct MatchType
{
ValueType operator () ();
public:
protected:
private:
private: //## implementation
};
template <typename T>
struct CVarray
{
T* operator () (const std::vector<T>& inArray);
public:
protected:
private:
private: //## implementation
};
template <typename T>
struct FitsNullValue
{
T operator () ();
public:
protected:
private:
private: //## implementation
};
template <typename T>
struct MatchImageType
{
ImageType operator () ();
public:
protected:
private:
private: //## implementation
};
template <class T>
struct MatchPtrNum
{
bool operator () (const T& left, const int& right) const;
public:
protected:
private:
private: //## implementation
};
// auto_ptr analogue for arrays.
template <typename X>
class auto_array_ptr
{
public:
explicit auto_array_ptr (X* p = 0) throw ();
explicit auto_array_ptr (auto_array_ptr<X>& right) throw ();
~auto_array_ptr();
void operator = (auto_array_ptr<X>& 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 <typename T>
struct ComparePtrIndex
{
bool operator () (const T* left, const T* right);
public:
protected:
private:
private: //## implementation
};
template <typename T>
struct CAarray
{
T* operator () (const std::valarray<T>& inArray);
public:
protected:
private:
private: //## implementation
};
template <typename T>
struct CVAarray
{
T* operator () (const std::vector< std::valarray<T> >& inArray);
public:
protected:
private:
private: //## implementation
};
class UnrecognizedType : public FitsException //## Inherits: <unnamed>%3CE143AB00C6
{
public:
UnrecognizedType (string diag, bool silent = true);
protected:
private:
private: //## implementation
};
// Parameterized Class CCfits::FITSUtil::MatchPtrName
template <class T>
inline bool MatchPtrName<T>::operator () (const T& left, const string& right) const
{
return left->name() == right;
}
// Parameterized Class CCfits::FITSUtil::MatchName
template <class T>
inline bool MatchName<T>::operator () (const T& left, const string& right) const
{
return left.name() == right;
}
// Parameterized Class CCfits::FITSUtil::MatchNum
template <class T>
inline bool MatchNum<T>::operator () (const T& left, const int& right) const
{
return left.index() == right;
}
// Parameterized Class CCfits::FITSUtil::MatchType
// Parameterized Class CCfits::FITSUtil::CVarray
template <typename T>
inline T* CVarray<T>::operator () (const std::vector<T>& 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<T> 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 <typename T>
inline T FitsNullValue<T>::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 <class T>
inline bool MatchPtrNum<T>::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 <typename T>
ValueType MatchType<T>::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 <typename T>
ImageType MatchImageType<T>::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<T> errType;
string diag ("Image: ");
diag += FITSType2String(errType());
throw UnrecognizedType(diag);
}
// Parameterized Class CCfits::FITSUtil::MatchPtrNum
// Parameterized Class CCfits::FITSUtil::auto_array_ptr
template <typename X>
auto_array_ptr<X>::auto_array_ptr (X* p) throw ()
: m_p(p)
{
}
template <typename X>
auto_array_ptr<X>::auto_array_ptr (auto_array_ptr<X>& right) throw ()
: m_p(right.release())
{
}
template <typename X>
auto_array_ptr<X>::~auto_array_ptr()
{
delete [] m_p;
}
template <typename X>
void auto_array_ptr<X>::operator = (auto_array_ptr<X>& right)
{
if (this != &right)
{
remove(m_p);
m_p = right.release();
}
}
template <typename X>
X& auto_array_ptr<X>::operator * () throw ()
{
return *m_p;
}
template <typename X>
X& auto_array_ptr<X>::operator [] (size_t i) throw ()
{
return m_p[i];
}
template <typename X>
X auto_array_ptr<X>::operator [] (size_t i) const throw ()
{
return m_p[i];
}
template <typename X>
X* auto_array_ptr<X>::get () const
{
return m_p;
}
template <typename X>
X* auto_array_ptr<X>::release () throw ()
{
return reset(0);
}
template <typename X>
X* auto_array_ptr<X>::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 <typename X>
void auto_array_ptr<X>::remove (X*& x)
{
X* __tmp(x);
x = 0;
delete [] __tmp;
}
// Parameterized Class CCfits::FITSUtil::ComparePtrIndex
template <typename T>
bool ComparePtrIndex<T>::operator () (const T* left, const T* right)
{
return (left->index() < right->index());
}
// Parameterized Class CCfits::FITSUtil::CAarray
template <typename T>
T* CAarray<T>::operator () (const std::valarray<T>& inArray)
{
size_t n(inArray.size());
auto_array_ptr<T> 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 <typename T>
T* CVAarray<T>::operator () (const std::vector< std::valarray<T> >& inArray)
{
size_t sz(0);
size_t n(inArray.size());
std::vector<size_t> 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<T> 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<T>& 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 <typename T>
void swap(T& left, T& right)
{
T temp(left);
left = right;
right = temp;
}
template <typename T>
void swap(std::vector<T>& left, std::vector<T>& right)
{
left.swap(right);
}
template <>
inline string FitsNullValue<string>::operator () ()
{
return string("");
}
template <>
inline float FitsNullValue<float>::operator () ()
{
return FLOATNULLVALUE;
}
template <>
inline double FitsNullValue<double>::operator () ()
{
return DOUBLENULLVALUE;
}
template <>
inline std::complex<float> FitsNullValue<std::complex<float> >::operator () ()
{
return std::complex<float>(FLOATNULLVALUE);
}
template <>
inline std::complex<double> FitsNullValue<std::complex<double> >::operator () ()
{
return std::complex<double>(DOUBLENULLVALUE);
}
} // end namespace FITSUtil
} // end namespace CCfits
#endif
CCfits::FITSUtil::CVAarray
function object returning C array from a vector of valarrays. see CVarray for details ...
Definition: FITSUtil.h:624
CCfits::FITSUtil::auto_array_ptr::release
X * release()
return underlying content of *this, transferring memory ownership
Definition: FITSUtil.h:827
CCfits::FITSUtil::MatchPtrNum
as for MatchNum, only with the input class a pointer.
Definition: FITSUtil.h:557
CCfits::FITSUtil::auto_array_ptr::operator[]
X & operator[](size_t i)
return a reference to the ith element of the array
Definition: FITSUtil.h:809
CCfits::FITSUtil::auto_array_ptr
A class that mimics the std:: library auto_ptr class, but works with arrays.
Definition: FITSUtil.h:571
CCfits::FITSUtil::MatchName
predicate for classes that have a name attribute; match input string with instance name...
Definition: FITSUtil.h:479
CCfits::FITSUtil::auto_array_ptr::reset
X * reset(X *p)
change the content of the auto_array_ptr to p
Definition: FITSUtil.h:833
CCfits::FITSUtil::auto_array_ptr::remove
static void remove(X *&x)
utility function to delete the memory owned by x and set it to null.
Definition: FITSUtil.h:842
CCfits::FITSUtil::CVarray::operator()
T * operator()(const std::vector< T > &inArray)
operator returning C array for use with scalar column data.
Definition: FITSUtil.h:675
CCfits::FITSUtil::CAarray
function object returning C array from a valarray. see CVarray for details
Definition: FITSUtil.h:611
CCfits::FITSUtil::CVarray
Function object class for returning C arrays from standard library objects used in the FITS library i...
Definition: FITSUtil.h:518
CCfits::FITSUtil::auto_array_ptr::operator=
void operator=(auto_array_ptr< X > &right)
assignment operator: transfer of ownership semantics
Definition: FITSUtil.h:793
CCfits::FITSUtil::MatchPtrName
as for MatchName, only with the input class a pointer.
Definition: FITSUtil.h:465
CCfits::FITSUtil::MatchType
function object that returns the FITS ValueType corresponding to an input intrinsic type ...
Definition: FITSUtil.h:505
CCfits::FITSUtil::MatchNum
predicate for classes that have an index attribute; match input index with instance value...
Definition: FITSUtil.h:492
CCfits::FitsException
FitsException is the base class for all exceptions thrown by this library.
Definition: FitsError.h:93
CCfits::FITSUtil::auto_array_ptr::get
X * get() const
return a token for the underlying content of *this
Definition: FITSUtil.h:821
CCfits::FITSUtil::auto_array_ptr::~auto_array_ptr
~auto_array_ptr()
destructor.
Definition: FITSUtil.h:786
CCfits::FITSUtil::auto_array_ptr::auto_array_ptr
auto_array_ptr(X *p=0)
constructor. allows creation of pointer to null, can be modified by reset()
Definition: FITSUtil.h:773
CCfits::ValueType
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition: CCfits.h:81
CCfits::FITSUtil::auto_array_ptr::operator*
X & operator*()
deference operator
Definition: FITSUtil.h:803
CCfits::FITSUtil::CAarray::operator()
T * operator()(const std::valarray< T > &inArray)
operator returning C array for use with image data.
Definition: FITSUtil.h:860
CCfits::FITSUtil::UnrecognizedType
exception thrown by MatchType if it encounters data type incompatible with cfitsio.
Definition: FITSUtil.h:636
CCfits::FITSUtil::CVAarray::operator()
T * operator()(const std::vector< std::valarray< T > > &inArray)
operator returning C array for use with vector column data.
Definition: FITSUtil.h:872