FITSUtil.h

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

Generated on Thu Nov 6 10:44:38 2008 for CCfits by  doxygen 1.5.4