00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef FITSUTIL_H
00011 #define FITSUTIL_H 1
00012 #include "CCfits.h"
00013
00014
00015 #include <complex>
00016
00017 #include <functional>
00018
00019 #include <valarray>
00020
00021 #include <string>
00022
00023 #include <vector>
00024
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"
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
00296
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
00344 void fill(std::vector<std::complex<float> >& outArray,
00345 const std::valarray<std::complex<float> >& inArray);
00346
00347
00348 void fill(std::vector<std::complex<float> >& outArray,
00349 const std::valarray<std::complex<double> >& inArray);
00350
00351
00352 void fill(std::vector<std::complex<double> >& outArray,
00353 const std::valarray<std::complex<double> >& inArray);
00354
00355
00356
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
00370
00371
00372
00373
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
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
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
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
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
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
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
00441 void fill(std::valarray<std::complex<float> >& outArray,
00442 const std::valarray<std::complex<float> >& inArray);
00443
00444 void fill(std::valarray<std::complex<double> >& outArray,
00445 const std::valarray<std::complex<double> >& inArray);
00446
00447 void fill(std::valarray<std::complex<float> >& outArray,
00448 const std::valarray<std::complex<double> >& inArray);
00449
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
00464 fitsfile* copyFitsPtr(fitsfile * inPtr);
00465
00466
00467
00468 template <class T>
00469 struct MatchPtrName : public std::binary_function<T,std::string,bool>
00470 {
00471
00472 bool operator () (const T& left, const string& right) const;
00473
00474 public:
00475 protected:
00476 private:
00477 private:
00478 };
00479
00480
00481
00482 template <class T>
00483 struct MatchName : public std::binary_function<T,std::string,bool>
00484 {
00485 bool operator () (const T& left, const string& right) const;
00486
00487 public:
00488 protected:
00489 private:
00490 private:
00491 };
00492
00493
00494
00495 template <class T>
00496 struct MatchNum : public std::binary_function<T,int,bool>
00497 {
00498 bool operator () (const T& left, const int& right) const;
00499
00500 public:
00501 protected:
00502 private:
00503 private:
00504 };
00505
00506
00507
00508 template <typename T>
00509 struct MatchType
00510 {
00511 ValueType operator () ();
00512
00513 public:
00514 protected:
00515 private:
00516 private:
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:
00530 };
00531
00532
00533
00534 template <typename T>
00535 struct FitsNullValue
00536 {
00537 T operator () ();
00538
00539 public:
00540 protected:
00541 private:
00542 private:
00543 };
00544
00545
00546
00547 template <typename T>
00548 struct MatchImageType
00549 {
00550 ImageType operator () ();
00551
00552 public:
00553 protected:
00554 private:
00555 private:
00556 };
00557
00558
00559
00560 template <class T>
00561 struct MatchPtrNum : public std::binary_function<T,int,bool>
00562 {
00563 bool operator () (const T& left, const int& right) const;
00564
00565 public:
00566 protected:
00567 private:
00568 private:
00569 };
00570
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:
00594
00595 X* m_p;
00596
00597 };
00598
00599
00600
00601 template <typename T>
00602 struct ComparePtrIndex : public std::binary_function<T,T,bool>
00603 {
00604 bool operator () (const T* left, const T* right);
00605
00606 public:
00607 protected:
00608 private:
00609 private:
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:
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:
00636 };
00637
00638
00639
00640 class UnrecognizedType : public FitsException
00641 {
00642 public:
00643 UnrecognizedType (string diag, bool silent = true);
00644
00645 protected:
00646 private:
00647 private:
00648 };
00649
00650
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
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
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
00675
00676
00677
00678 template <typename T>
00679 inline T* CVarray<T>::operator () (const std::vector<T>& inArray)
00680 {
00681
00682
00683
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
00691
00692 template <typename T>
00693 inline T FitsNullValue<T>::operator () ()
00694 {
00695 FITSUtil::MatchType<T> null;
00696 switch ( null() )
00697 {
00698
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
00711
00712
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
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
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
00756
00757 if ( typeid(T) == typeid(ll1) ) return Tlonglong;
00758 throw UnrecognizedType("Invalid data type for FITS Data I/O\n");
00759 }
00760
00761
00762
00763
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
00782
00783
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
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
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
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
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;
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 }
00914 }
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