00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef FITSUTIL_H
00013 #define FITSUTIL_H 1
00014 #include "CCfits.h"
00015
00016
00017 #include "FitsError.h"
00018
00019 #include <functional>
00020
00021 #include <string>
00022
00023 #include <complex>
00024
00025 #include <valarray>
00026
00027 #include <vector>
00028 #include <typeinfo>
00029
00030
00031 namespace CCfits {
00032
00033 namespace FITSUtil {
00034
00283 #ifdef _MSC_VER
00284 #include "MSconfig.h"
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
00298
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
00346 void fill(std::vector<std::complex<float> >& outArray,
00347 const std::valarray<std::complex<float> >& inArray);
00348
00349
00350 void fill(std::vector<std::complex<float> >& outArray,
00351 const std::valarray<std::complex<double> >& inArray);
00352
00353
00354 void fill(std::vector<std::complex<double> >& outArray,
00355 const std::valarray<std::complex<double> >& inArray);
00356
00357
00358
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
00372
00373
00374
00375
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
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
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
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
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
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
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
00443 void fill(std::valarray<std::complex<float> >& outArray,
00444 const std::valarray<std::complex<float> >& inArray);
00445
00446 void fill(std::valarray<std::complex<double> >& outArray,
00447 const std::valarray<std::complex<double> >& inArray);
00448
00449 void fill(std::valarray<std::complex<float> >& outArray,
00450 const std::valarray<std::complex<double> >& inArray);
00451
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 fitsfile* copyFitsPtr(fitsfile * inPtr);
00467
00468
00469
00470 template <class T>
00471 struct MatchPtrName : public std::binary_function<T,std::string,bool>
00472 {
00473
00474 bool operator () (const T& left, const string& right) const;
00475
00476 public:
00477 protected:
00478 private:
00479 private:
00480 };
00481
00482
00483
00484 template <class T>
00485 struct MatchName : public std::binary_function<T,std::string,bool>
00486 {
00487 bool operator () (const T& left, const string& right) const;
00488
00489 public:
00490 protected:
00491 private:
00492 private:
00493 };
00494
00495
00496
00497 template <class T>
00498 struct MatchNum : public std::binary_function<T,int,bool>
00499 {
00500 bool operator () (const T& left, const int& right) const;
00501
00502 public:
00503 protected:
00504 private:
00505 private:
00506 };
00507
00508
00509
00510 template <typename T>
00511 struct MatchType
00512 {
00513 ValueType operator () ();
00514
00515 public:
00516 protected:
00517 private:
00518 private:
00519 };
00520
00521
00522
00523 template <typename T>
00524 struct CVarray
00525 {
00526 T* operator () (const std::vector<T>& inArray);
00527
00528 public:
00529 protected:
00530 private:
00531 private:
00532 };
00533
00534
00535
00536 template <typename T>
00537 struct FitsNullValue
00538 {
00539 T operator () ();
00540
00541 public:
00542 protected:
00543 private:
00544 private:
00545 };
00546
00547
00548
00549 template <typename T>
00550 struct MatchImageType
00551 {
00552 ImageType operator () ();
00553
00554 public:
00555 protected:
00556 private:
00557 private:
00558 };
00559
00560
00561
00562 template <class T>
00563 struct MatchPtrNum : public std::binary_function<T,int,bool>
00564 {
00565 bool operator () (const T& left, const int& right) const;
00566
00567 public:
00568 protected:
00569 private:
00570 private:
00571 };
00572
00573
00574
00575
00576 template <typename X>
00577 class auto_array_ptr
00578 {
00579 public:
00580 explicit auto_array_ptr (X* p = 0) throw ();
00581 explicit auto_array_ptr (auto_array_ptr<X>& right) throw ();
00582 ~auto_array_ptr();
00583
00584 void operator = (auto_array_ptr<X>& right);
00585 X& operator * () throw ();
00586 X& operator [] (size_t i) throw ();
00587 X operator [] (size_t i) const throw ();
00588 X* get () const;
00589 X* release () throw ();
00590 X* reset (X* p) throw ();
00591 static void remove (X*& x);
00592
00593 protected:
00594 private:
00595 private:
00596
00597 X* m_p;
00598
00599 };
00600
00601
00602
00603 template <typename T>
00604 struct ComparePtrIndex : public std::binary_function<T,T,bool>
00605 {
00606 bool operator () (const T* left, const T* right);
00607
00608 public:
00609 protected:
00610 private:
00611 private:
00612 };
00613
00614
00615
00616 template <typename T>
00617 struct CAarray
00618 {
00619 T* operator () (const std::valarray<T>& inArray);
00620
00621 public:
00622 protected:
00623 private:
00624 private:
00625 };
00626
00627
00628
00629 template <typename T>
00630 struct CVAarray
00631 {
00632 T* operator () (const std::vector< std::valarray<T> >& inArray);
00633
00634 public:
00635 protected:
00636 private:
00637 private:
00638 };
00639
00640
00641
00642 class UnrecognizedType : public FitsException
00643 {
00644 public:
00645 UnrecognizedType (string diag, bool silent = true);
00646
00647 protected:
00648 private:
00649 private:
00650 };
00651
00652
00653
00654 template <class T>
00655 inline bool MatchPtrName<T>::operator () (const T& left, const string& right) const
00656 {
00657 return left->name() == right;
00658 }
00659
00660
00661
00662 template <class T>
00663 inline bool MatchName<T>::operator () (const T& left, const string& right) const
00664 {
00665 return left.name() == right;
00666 }
00667
00668
00669
00670 template <class T>
00671 inline bool MatchNum<T>::operator () (const T& left, const int& right) const
00672 {
00673 return left.index() == right;
00674 }
00675
00676
00677
00678
00679
00680 template <typename T>
00681 inline T* CVarray<T>::operator () (const std::vector<T>& inArray)
00682 {
00683
00684
00685
00686 auto_array_ptr<T> pC(new T[inArray.size()]);
00687 T* c = pC.get();
00688 std::copy(inArray.begin(),inArray.end(),&c[0]);
00689 return pC.release();
00690 }
00691
00692
00693
00694 template <typename T>
00695 inline T FitsNullValue<T>::operator () ()
00696 {
00697 FITSUtil::MatchType<T> null;
00698 switch ( null() )
00699 {
00700
00701 case Tfloat:
00702 case Tcomplex:
00703 return static_cast<T>(FLOATNULLVALUE);
00704 case Tdouble:
00705 case Tdblcomplex:
00706 return static_cast<T>(DOUBLENULLVALUE);
00707 default:
00708 return 0;
00709 }
00710 }
00711
00712
00713
00714
00715
00716 template <class T>
00717 inline bool MatchPtrNum<T>::operator () (const T& left, const int& right) const
00718 {
00719 return left->index() == right;
00720 }
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740 template <typename T>
00741 ValueType MatchType<T>::operator () ()
00742 {
00743
00744 if ( typeid(T) == typeid(d1) ) return Tdouble;
00745 if ( typeid(T) == typeid(f1) ) return Tfloat;
00746 if ( typeid(T) == typeid(c1) ) return Tcomplex;
00747 if ( typeid(T) == typeid(d2) ) return Tdblcomplex;
00748 if ( typeid(T) == typeid(s1) ) return Tstring;
00749 if ( typeid(T) == typeid(i1) ) return Tint;
00750 if ( typeid(T) == typeid(u1) ) return Tuint;
00751 if ( typeid(T) == typeid(s2) ) return Tshort;
00752 if ( typeid(T) == typeid(us1) ) return Tushort;
00753 if ( typeid(T) == typeid(b1) ) return Tlogical;
00754 if ( typeid(T) == typeid(b2) ) return Tbyte;
00755 if ( typeid(T) == typeid(l1) ) return Tlong;
00756 if ( typeid(T) == typeid(ul1) ) return Tulong;
00757
00758
00759 if ( typeid(T) == typeid(ll1) ) return Tlonglong;
00760 throw UnrecognizedType("Invalid data type for FITS Data I/O\n");
00761 }
00762
00763
00764
00765
00766
00767 template <typename T>
00768 ImageType MatchImageType<T>::operator () ()
00769 {
00770 if ( typeid(T) == typeid(b2) ) return Ibyte;
00771 if ( typeid(T) == typeid(s2) ) return Ishort;
00772 if ( typeid(T) == typeid(l1) ) return Ilong;
00773 if ( typeid(T) == typeid(f1) ) return Ifloat;
00774 if ( typeid(T) == typeid(d1) ) return Idouble;
00775 if ( typeid(T) == typeid(us1) ) return Iushort;
00776 if ( typeid(T) == typeid(ul1) ) return Iulong;
00777 MatchType<T> errType;
00778 string diag ("Image: ");
00779 diag += FITSType2String(errType());
00780 throw UnrecognizedType(diag);
00781 }
00782
00783
00784
00785
00786
00787 template <typename X>
00788 auto_array_ptr<X>::auto_array_ptr (X* p) throw ()
00789 : m_p(p)
00790 {
00791 }
00792
00793 template <typename X>
00794 auto_array_ptr<X>::auto_array_ptr (auto_array_ptr<X>& right) throw ()
00795 : m_p(right.release())
00796 {
00797 }
00798
00799
00800 template <typename X>
00801 auto_array_ptr<X>::~auto_array_ptr()
00802 {
00803 delete [] m_p;
00804 }
00805
00806
00807 template <typename X>
00808 void auto_array_ptr<X>::operator = (auto_array_ptr<X>& right)
00809 {
00810 if (this != &right)
00811 {
00812 remove(m_p);
00813 m_p = right.release();
00814 }
00815 }
00816
00817 template <typename X>
00818 X& auto_array_ptr<X>::operator * () throw ()
00819 {
00820 return *m_p;
00821 }
00822
00823 template <typename X>
00824 X& auto_array_ptr<X>::operator [] (size_t i) throw ()
00825 {
00826 return m_p[i];
00827 }
00828
00829 template <typename X>
00830 X auto_array_ptr<X>::operator [] (size_t i) const throw ()
00831 {
00832 return m_p[i];
00833 }
00834
00835 template <typename X>
00836 X* auto_array_ptr<X>::get () const
00837 {
00838 return m_p;
00839 }
00840
00841 template <typename X>
00842 X* auto_array_ptr<X>::release () throw ()
00843 {
00844 return reset(0);
00845 }
00846
00847 template <typename X>
00848 X* auto_array_ptr<X>::reset (X* p) throw ()
00849 {
00850
00851 X* __tmp = m_p;
00852 m_p = p;
00853 return __tmp;
00854 }
00855
00856 template <typename X>
00857 void auto_array_ptr<X>::remove (X*& x)
00858 {
00859 X* __tmp(x);
00860 x = 0;
00861 delete [] __tmp;
00862 }
00863
00864
00865
00866 template <typename T>
00867 bool ComparePtrIndex<T>::operator () (const T* left, const T* right)
00868 {
00869 return (left->index() < right->index());
00870 }
00871
00872
00873
00874 template <typename T>
00875 T* CAarray<T>::operator () (const std::valarray<T>& inArray)
00876 {
00877 size_t n(inArray.size());
00878 auto_array_ptr<T> pC(new T[n]);
00879 T* c= pC.get();
00880 for (size_t j = 0; j < n; ++j) c[j] = inArray[j];
00881 return pC.release();
00882 }
00883
00884
00885
00886 template <typename T>
00887 T* CVAarray<T>::operator () (const std::vector< std::valarray<T> >& inArray)
00888 {
00889 size_t sz(0);
00890 size_t n(inArray.size());
00891
00892 std::vector<size_t> nr(n);
00893
00894 size_t i = 0;
00895 for ( i = 0; i < n; ++i)
00896 {
00897 nr[i] = inArray[i].size();
00898 sz += nr[i];
00899
00900 }
00901 auto_array_ptr<T> pC(new T[sz]);
00902 T* c = pC.get();
00903
00904 size_t k(0);
00905 for ( i = 0; i < n; ++i)
00906 {
00907 size_t& m = nr[i];
00908 const std::valarray<T>& current = inArray[i];
00909 for (size_t j=0; j < m ; ++j) c[k++] = current[j];
00910 }
00911
00912 return pC.release();
00913 }
00914
00915 }
00916 }
00917
00918 namespace CCfits
00919 {
00920
00921
00922 namespace FITSUtil
00923 {
00924
00925 template <typename T>
00926 void swap(T& left, T& right)
00927 {
00928 T temp(left);
00929 left = right;
00930 right = temp;
00931 }
00932
00933 template <typename T>
00934 void swap(std::vector<T>& left, std::vector<T>& right)
00935 {
00936 left.swap(right);
00937 }
00938
00939 template <>
00940 inline string FitsNullValue<string>::operator () ()
00941 {
00942 return string("");
00943 }
00944
00945 }
00946 }
00947
00948
00949
00950 #endif