FITSUtil.h

00001 //      This is version 1.6 release dated Nov 2006
00002 //      Astrophysics Science Division,
00003 //      NASA/ Goddard Space Flight Center
00004 //      HEASARC
00005 //      http://heasarc.gsfc.nasa.gov
00006 //      e-mail: ccfits@legacy.gsfc.nasa.gov
00007 //
00008 //      Original author: Ben Dorman, L3-Communications EER Systems Inc.
00009 
00010 #ifndef FITSUTIL_H
00011 #define FITSUTIL_H 1
00012 #include "CCfits.h"
00013 
00014 // complex
00015 #include <complex>
00016 // functional
00017 #include <functional>
00018 // valarray
00019 #include <valarray>
00020 // string
00021 #include <string>
00022 // vector
00023 #include <vector>
00024 // FitsError
00025 #include "FitsError.h"
00026 #include <typeinfo>
00027 
00028 
00029 namespace CCfits {
00030 
00031   namespace FITSUtil {
00032 
00281 #ifdef _MSC_VER
00282 #include "MSconfig.h" // for truncation double to float warning
00283 #endif
00284 
00285      template <typename T>
00286     void swap(T& left,T& right);
00287 
00288     template <typename T>
00289     void swap(std::vector<T>& left, std::vector<T>& right);
00290 
00291     string lowerCase(const string& inputString);
00292 
00293     string upperCase(const string& inputString);
00294 
00295   // Check if a file name includes an image compression specifier,
00296   // and return its location if it exists.
00297     string::size_type checkForCompressString(const string& fileName);
00298 
00299   struct InvalidConversion : public FitsException
00300   {
00301                 InvalidConversion(const string& diag, bool silent=false);
00302 
00303   };
00304 
00305   struct MatchStem : public std::binary_function<string,string,bool>
00306   {
00307           bool operator()(const string& left, const string& right) const;
00308   };
00309 
00310   static const  double d1(0);
00311   static const  float  f1(0);
00312   static const  std::complex<float> c1(0.);
00313   static const  std::complex<double> d2(0.);
00314   static const  string s1("");
00315   static const  int    i1(0);
00316   static const  unsigned int  u1(0);        
00317   static const  long l1(0);
00318   static const  unsigned long ul1(0);
00319   static const  LONGLONG ll1(0);
00320   static const  short s2(0);
00321   static const  unsigned short us1(0); 
00322   static const  bool b1(false);
00323   static const  unsigned char b2(0);  
00324 
00325   char** CharArray(const std::vector<string>& inArray);
00326 
00327   string FITSType2String( int typeInt );
00328 
00329 
00330   template <typename S, typename T> 
00331   void fill(std::vector<S>& outArray, const std::vector<T>& inArray,size_t first, size_t last);
00332 
00333   template <typename S, typename T> 
00334   void fill(std::valarray<S>& outArray, const std::valarray<T>& inArray);
00335 
00336   template <typename S, typename T> 
00337   void fill(std::valarray<S>& outArray, const std::vector<T>& inArray,size_t first, size_t last);
00338 
00339 
00340   template <typename S, typename T> 
00341   void fill(std::vector<S>& outArray, const std::valarray<T>& inArray);
00342 
00343   // VF<-AF
00344    void fill(std::vector<std::complex<float> >& outArray, 
00345                   const std::valarray<std::complex<float> >& inArray);
00346 
00347   // VF<-AD
00348   void fill(std::vector<std::complex<float> >& outArray, 
00349                   const std::valarray<std::complex<double> >& inArray);
00350 
00351   // VD<-AD
00352   void fill(std::vector<std::complex<double> >& outArray, 
00353                   const std::valarray<std::complex<double> >& inArray);
00354 
00355 
00356   // VD<-AF
00357   void fill(std::vector<std::complex<double> >& outArray, 
00358                   const std::valarray<std::complex<float> >& inArray);
00359 
00360   template <typename T>
00361   void fill(std::vector<string>& outArray, const std::vector<T>& inArray, size_t first, size_t last);
00362 
00363   template <typename T>
00364   void fill(std::vector<T>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
00365 
00366   template <typename S> 
00367   void fill(std::valarray<S>& outArray, const std::vector<string>& inArray,size_t first, size_t last);
00368 
00369 //  template <typename S, typename T>
00370 //  void fill(std::valarray<std::complex<S> >& outArray, const std::valarray<std::complex<T> >& inArray);            
00371   // seems no other way of doing this.
00372 
00373   // VF<-VF
00374 #ifdef TEMPLATE_AMBIG_DEFECT
00375   void fillMSvfvf(std::vector<std::complex<float> >& outArray, 
00376                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00377 #endif
00378 
00379   void fill(std::vector<std::complex<float> >& outArray, 
00380                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00381 
00382   // VF<-VD
00383 #ifdef TEMPLATE_AMBIG_DEFECT
00384     void fillMSvfvd(std::vector<std::complex<float> >& outArray, 
00385                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00386 #endif
00387 
00388    void fill(std::vector<std::complex<float> >& outArray, 
00389                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00390 
00391    // VD<-VD
00392 #ifdef TEMPLATE_AMBIG_DEFECT
00393  void fillMSvdvd(std::vector<std::complex<double> >& outArray, 
00394                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00395 #endif
00396 
00397    void fill(std::vector<std::complex<double> >& outArray, 
00398                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00399 
00400 #ifdef TEMPLATE_AMBIG_DEFECT
00401   void fillMSvdvf(std::vector<std::complex<double> >& outArray, 
00402                                 const std::vector<std::complex<float> >& inArray, 
00403                         size_t first, size_t last);
00404 #else
00405   void fill(std::vector<std::complex<double> >& outArray, 
00406                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00407 #endif
00408 
00409   // AF<-VD
00410   void fill(std::valarray<std::complex<float> >& outArray, 
00411                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00412 
00413   // AF<-VF
00414 #ifdef TEMPLATE_AMBIG_DEFECT
00415  void fillMSafvf(std::valarray<std::complex<float> >& outArray, 
00416                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00417 #else
00418  void fill(std::valarray<std::complex<float> >& outArray, 
00419                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00420 #endif
00421 
00422  // AD<-VF
00423 #ifdef TEMPLATE_AMBIG_DEFECT
00424   void fillMSadvf(std::valarray<std::complex<double> >& outArray, 
00425                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00426 #else
00427   void fill(std::valarray<std::complex<double> >& outArray, 
00428                   const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
00429 #endif
00430 
00431   // AD<-VD
00432 #ifdef TEMPLATE_AMBIG_DEFECT
00433   void fillMSadvd(std::valarray<std::complex<double> >& outArray, 
00434                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00435 #else
00436   void fill(std::valarray<std::complex<double> >& outArray, 
00437                   const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
00438 #endif
00439 
00440   // AF<-AF
00441   void fill(std::valarray<std::complex<float> >& outArray,  
00442                   const std::valarray<std::complex<float> >& inArray);
00443   // AD<-AD
00444   void fill(std::valarray<std::complex<double> >& outArray,  
00445                   const std::valarray<std::complex<double> >& inArray);
00446   // AF<-AD
00447   void fill(std::valarray<std::complex<float> >& outArray, 
00448                   const std::valarray<std::complex<double> >& inArray);
00449   // AD<-AF
00450   void fill(std::valarray<std::complex<double> >& outArray,  
00451                   const std::valarray<std::complex<float> >& inArray);
00452 
00453 #if TEMPLATE_AMBIG_DEFECT || TEMPLATE_AMBIG7_DEFECT
00454   void fillMSvsvs(std::vector<string>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
00455 #endif
00456 
00457 
00458   void fill(std::vector<string>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
00459 
00460   template <typename S, typename T>
00461   string errorMessage(const S& out, const T& in);
00462 
00463   // copy a fitsfile pointer.
00464   fitsfile* copyFitsPtr(fitsfile * inPtr);
00465 
00466 
00467 
00468     template <class T>
00469     struct MatchPtrName : public std::binary_function<T,std::string,bool>  //## Inherits: <unnamed>%39491BC9025D
00470     {
00471           //    Parameterized Class MatchPtrName
00472           bool operator () (const T& left, const string& right) const;
00473 
00474       public:
00475       protected:
00476       private:
00477       private: //## implementation
00478     };
00479 
00480 
00481 
00482     template <class T>
00483     struct MatchName : public std::binary_function<T,std::string,bool>  //## Inherits: <unnamed>%39491BC50121
00484     {
00485           bool operator () (const T& left, const string& right) const;
00486 
00487       public:
00488       protected:
00489       private:
00490       private: //## implementation
00491     };
00492 
00493 
00494 
00495     template <class T>
00496     struct MatchNum : public std::binary_function<T,int,bool>  //## Inherits: <unnamed>%39491BCE01C0
00497     {
00498           bool operator () (const T& left, const int& right) const;
00499 
00500       public:
00501       protected:
00502       private:
00503       private: //## implementation
00504     };
00505 
00506 
00507 
00508     template <typename T>
00509     struct MatchType 
00510     {
00511           ValueType operator () ();
00512 
00513       public:
00514       protected:
00515       private:
00516       private: //## implementation
00517     };
00518 
00519 
00520 
00521     template <typename T>
00522     struct CVarray 
00523     {
00524           T* operator () (const std::vector<T>& inArray);
00525 
00526       public:
00527       protected:
00528       private:
00529       private: //## implementation
00530     };
00531 
00532 
00533 
00534     template <typename T>
00535     struct FitsNullValue 
00536     {
00537           T operator () ();
00538 
00539       public:
00540       protected:
00541       private:
00542       private: //## implementation
00543     };
00544 
00545 
00546 
00547     template <typename T>
00548     struct MatchImageType 
00549     {
00550           ImageType operator () ();
00551 
00552       public:
00553       protected:
00554       private:
00555       private: //## implementation
00556     };
00557 
00558 
00559 
00560     template <class T>
00561     struct MatchPtrNum : public std::binary_function<T,int,bool>  //## Inherits: <unnamed>%39491BD3034B
00562     {
00563           bool operator () (const T& left, const int& right) const;
00564 
00565       public:
00566       protected:
00567       private:
00568       private: //## implementation
00569     };
00570     //  auto_ptr analogue for arrays.
00571 
00572 
00573 
00574     template <typename X>
00575     class auto_array_ptr 
00576     {
00577       public:
00578           explicit auto_array_ptr (X* p = 0) throw ();
00579           explicit auto_array_ptr (auto_array_ptr<X>& right) throw ();
00580           ~auto_array_ptr();
00581 
00582           void operator = (auto_array_ptr<X>& right);
00583           X& operator * () throw ();
00584           X& operator [] (size_t i) throw ();
00585           X operator [] (size_t i) const throw ();
00586           X* get () const;
00587           X* release () throw ();
00588           X* reset (X* p) throw ();
00589           static void remove (X*& x);
00590 
00591       protected:
00592       private:
00593       private: //## implementation
00594         // Data Members for Class Attributes
00595           X* m_p;
00596 
00597     };
00598 
00599 
00600 
00601     template <typename T>
00602     struct ComparePtrIndex : public std::binary_function<T,T,bool>  //## Inherits: <unnamed>%3B24DB930299
00603     {
00604           bool operator () (const T* left, const T* right);
00605 
00606       public:
00607       protected:
00608       private:
00609       private: //## implementation
00610     };
00611 
00612 
00613 
00614     template <typename T>
00615     struct CAarray 
00616     {
00617           T* operator () (const std::valarray<T>& inArray);
00618 
00619       public:
00620       protected:
00621       private:
00622       private: //## implementation
00623     };
00624 
00625 
00626 
00627     template <typename T>
00628     struct CVAarray 
00629     {
00630           T* operator () (const std::vector< std::valarray<T> >& inArray);
00631 
00632       public:
00633       protected:
00634       private:
00635       private: //## implementation
00636     };
00637 
00638 
00639 
00640     class UnrecognizedType : public FitsException  //## Inherits: <unnamed>%3CE143AB00C6
00641     {
00642       public:
00643           UnrecognizedType (string diag, bool silent = true);
00644 
00645       protected:
00646       private:
00647       private: //## implementation
00648     };
00649 
00650     // Parameterized Class CCfits::FITSUtil::MatchPtrName 
00651 
00652     template <class T>
00653     inline bool MatchPtrName<T>::operator () (const T& left, const string& right) const
00654     {
00655        return left->name() == right;
00656     }
00657 
00658     // Parameterized Class CCfits::FITSUtil::MatchName 
00659 
00660     template <class T>
00661     inline bool MatchName<T>::operator () (const T& left, const string& right) const
00662     {
00663        return left.name() == right;
00664     }
00665 
00666     // Parameterized Class CCfits::FITSUtil::MatchNum 
00667 
00668     template <class T>
00669     inline bool MatchNum<T>::operator () (const T& left, const int& right) const
00670     {
00671     return left.index() == right;
00672     }
00673 
00674     // Parameterized Class CCfits::FITSUtil::MatchType 
00675 
00676     // Parameterized Class CCfits::FITSUtil::CVarray 
00677 
00678     template <typename T>
00679     inline T* CVarray<T>::operator () (const std::vector<T>& inArray)
00680     {
00681 
00682       // convert std containers used commonly in FITS to C arrays in an exception
00683       // safe manner that is also clear about resource ownership.      
00684       auto_array_ptr<T> pC(new T[inArray.size()]);
00685       T* c = pC.get();
00686       std::copy(inArray.begin(),inArray.end(),&c[0]);
00687       return pC.release();
00688     }
00689 
00690     // Parameterized Class CCfits::FITSUtil::FitsNullValue 
00691 
00692     template <typename T>
00693     inline T FitsNullValue<T>::operator () ()
00694     {
00695            FITSUtil::MatchType<T> null;  
00696            switch ( null() )
00697            {
00698                 // the casts have no function apart from removing compiler warnings.
00699                 case Tfloat:
00700                 case Tcomplex:
00701                         return static_cast<T>(FLOATNULLVALUE);
00702                 case Tdouble:
00703                 case Tdblcomplex:
00704                         return static_cast<T>(DOUBLENULLVALUE);
00705                 default:
00706                         return 0;
00707             }
00708     }
00709 
00710     // Parameterized Class CCfits::FITSUtil::MatchImageType 
00711 
00712     // Parameterized Class CCfits::FITSUtil::MatchPtrNum 
00713 
00714     template <class T>
00715     inline bool MatchPtrNum<T>::operator () (const T& left, const int& right) const
00716     {
00717     return left->index() == right;
00718     }
00719 
00720     // Parameterized Class CCfits::FITSUtil::auto_array_ptr 
00721 
00722     // Parameterized Class CCfits::FITSUtil::ComparePtrIndex 
00723 
00724     // Parameterized Class CCfits::FITSUtil::CAarray 
00725 
00726     // Parameterized Class CCfits::FITSUtil::CVAarray 
00727 
00728     // Class CCfits::FITSUtil::UnrecognizedType 
00729 
00730     // Parameterized Class CCfits::FITSUtil::MatchPtrName 
00731 
00732     // Parameterized Class CCfits::FITSUtil::MatchName 
00733 
00734     // Parameterized Class CCfits::FITSUtil::MatchNum 
00735 
00736     // Parameterized Class CCfits::FITSUtil::MatchType 
00737 
00738     template <typename T>
00739     ValueType MatchType<T>::operator () ()
00740     {
00741 
00742     if ( typeid(T) == typeid(d1) ) return Tdouble;
00743     if ( typeid(T) == typeid(f1) ) return Tfloat;
00744     if ( typeid(T) == typeid(c1) ) return Tcomplex;
00745     if ( typeid(T) == typeid(d2) ) return Tdblcomplex;
00746     if ( typeid(T) == typeid(s1) ) return Tstring;
00747     if ( typeid(T) == typeid(i1) ) return Tint;
00748     if ( typeid(T) == typeid(u1) ) return Tuint;
00749     if ( typeid(T) == typeid(s2) ) return Tshort;
00750     if ( typeid(T) == typeid(us1) ) return Tushort;
00751     if ( typeid(T) == typeid(b1) ) return Tlogical;
00752     if ( typeid(T) == typeid(b2) ) return Tbyte;
00753     if ( typeid(T) == typeid(l1) ) return Tlong;
00754     if ( typeid(T) == typeid(ul1) ) return Tulong;
00755     // Carefull, on some compilers LONGLONG == long,
00756     // so this should go after test for long.
00757     if ( typeid(T) == typeid(ll1) ) return Tlonglong;
00758     throw UnrecognizedType("Invalid data type for FITS Data I/O\n");    
00759     }
00760 
00761     // Parameterized Class CCfits::FITSUtil::CVarray 
00762 
00763     // Parameterized Class CCfits::FITSUtil::MatchImageType 
00764 
00765     template <typename T>
00766     ImageType MatchImageType<T>::operator () ()
00767     {
00768     if ( typeid(T) == typeid(b2) ) return Ibyte;    
00769     if ( typeid(T) == typeid(s2) ) return Ishort;
00770     if ( typeid(T) == typeid(l1) ) return Ilong;
00771     if ( typeid(T) == typeid(f1) ) return Ifloat;
00772     if ( typeid(T) == typeid(d1) ) return Idouble;
00773     if ( typeid(T) == typeid(us1) ) return Iushort;
00774     if ( typeid(T) == typeid(ul1) ) return Iulong;
00775     MatchType<T> errType;
00776     string diag ("Image: ");
00777     diag += FITSType2String(errType());
00778     throw UnrecognizedType(diag);        
00779     }
00780 
00781     // Parameterized Class CCfits::FITSUtil::MatchPtrNum 
00782 
00783     // Parameterized Class CCfits::FITSUtil::auto_array_ptr 
00784 
00785     template <typename X>
00786     auto_array_ptr<X>::auto_array_ptr (X* p) throw ()
00787           : m_p(p)
00788     {
00789     }
00790 
00791     template <typename X>
00792     auto_array_ptr<X>::auto_array_ptr (auto_array_ptr<X>& right) throw ()
00793           : m_p(right.release())
00794     {
00795     }
00796 
00797 
00798     template <typename X>
00799     auto_array_ptr<X>::~auto_array_ptr()
00800     {
00801       delete [] m_p;
00802     }
00803 
00804 
00805     template <typename X>
00806     void auto_array_ptr<X>::operator = (auto_array_ptr<X>& right)
00807     {
00808       if (this != &right)
00809       {
00810                 remove(m_p);
00811                 m_p = right.release();       
00812       }
00813     }
00814 
00815     template <typename X>
00816     X& auto_array_ptr<X>::operator * () throw ()
00817     {
00818       return *m_p;
00819     }
00820 
00821     template <typename X>
00822     X& auto_array_ptr<X>::operator [] (size_t i) throw ()
00823     {
00824       return m_p[i];
00825     }
00826 
00827     template <typename X>
00828     X auto_array_ptr<X>::operator [] (size_t i) const throw ()
00829     {
00830       return m_p[i];
00831     }
00832 
00833     template <typename X>
00834     X* auto_array_ptr<X>::get () const
00835     {
00836       return m_p;
00837     }
00838 
00839     template <typename X>
00840     X* auto_array_ptr<X>::release () throw ()
00841     {
00842       return reset(0);
00843     }
00844 
00845     template <typename X>
00846     X* auto_array_ptr<X>::reset (X* p) throw ()
00847     {
00848       // set the auto_ptr to manage p and return the old pointer it was managing.
00849       X* __tmp = m_p; 
00850       m_p = p;
00851       return __tmp;
00852     }
00853 
00854     template <typename X>
00855     void auto_array_ptr<X>::remove (X*& x)
00856     {
00857       X* __tmp(x);
00858       x = 0;
00859       delete [] __tmp;
00860     }
00861 
00862     // Parameterized Class CCfits::FITSUtil::ComparePtrIndex 
00863 
00864     template <typename T>
00865     bool ComparePtrIndex<T>::operator () (const T* left, const T* right)
00866     {
00867       return (left->index() < right->index());
00868     }
00869 
00870     // Parameterized Class CCfits::FITSUtil::CAarray 
00871 
00872     template <typename T>
00873     T* CAarray<T>::operator () (const std::valarray<T>& inArray)
00874     {
00875       size_t n(inArray.size());
00876       auto_array_ptr<T> pC(new T[n]);
00877       T* c= pC.get();
00878       for (size_t j = 0; j < n; ++j) c[j] = inArray[j];
00879       return pC.release();      
00880     }
00881 
00882     // Parameterized Class CCfits::FITSUtil::CVAarray 
00883 
00884     template <typename T>
00885     T* CVAarray<T>::operator () (const std::vector< std::valarray<T> >& inArray)
00886     {
00887       size_t  sz(0);
00888       size_t  n(inArray.size());
00889 
00890       std::vector<size_t> nr(n);
00891 
00892       size_t i = 0; // for MS VC++ bug
00893       for ( i = 0; i < n; ++i)
00894       {
00895          nr[i] = inArray[i].size();
00896          sz += nr[i];
00897 
00898       }
00899       auto_array_ptr<T> pC(new T[sz]);
00900       T* c = pC.get();
00901 
00902       size_t k(0);
00903       for ( i = 0; i < n; ++i)
00904       {
00905          size_t& m = nr[i];
00906          const std::valarray<T>& current = inArray[i];
00907          for (size_t j=0; j < m ; ++j) c[k++] = current[j];
00908       }
00909 
00910       return pC.release();      
00911     }
00912 
00913   } // namespace FITSUtil
00914 } // namespace CCfits
00915 
00916 namespace CCfits
00917 {
00918 
00919 
00920         namespace FITSUtil
00921         {                
00922 
00923                 template <typename T>
00924                 void swap(T& left, T& right)
00925                 {
00926                         T temp(left);
00927                         left = right;
00928                         right = temp;                
00929                 }
00930 
00931                 template <typename T>
00932                 void swap(std::vector<T>& left, std::vector<T>& right)
00933                 {
00934                         left.swap(right);        
00935                 }
00936 
00937                 template <>
00938                 inline string FitsNullValue<string>::operator () ()
00939                 {
00940                         return string("");
00941                 }
00942 
00943         }
00944 }
00945 
00946 
00947 
00948 #endif

Generated on Fri Nov 3 17:09:05 2006 for CCfits by  doxygen 1.4.7