CCfits  2.6
FITSUtil.h
1 // Astrophysics Science Division,
2 // NASA/ Goddard Space Flight Center
3 // HEASARC
4 // http://heasarc.gsfc.nasa.gov
5 // e-mail: ccfits@legacy.gsfc.nasa.gov
6 //
7 // Original author: Ben Dorman
8 
9 #ifndef FITSUTIL_H
10 #define FITSUTIL_H 1
11 #include "CCfits.h"
12 
13 // functional
14 #include <functional>
15 // complex
16 #include <complex>
17 // valarray
18 #include <valarray>
19 // vector
20 #include <vector>
21 // string
22 #include <string>
23 // FitsError
24 #include "FitsError.h"
25 #include <typeinfo>
26 
27 
28 namespace CCfits {
29 
30  namespace FITSUtil {
31 
280 #ifdef _MSC_VER
281 #include "MSconfig.h" // for truncation double to float warning
282 #endif
283 
284  template <typename T>
285  void swap(T& left,T& right);
286 
287  template <typename T>
288  void swap(std::vector<T>& left, std::vector<T>& right);
289 
290  string lowerCase(const string& inputString);
291 
292  string upperCase(const string& inputString);
293 
294  // Check if a file name includes an image compression specifier,
295  // and return its location if it exists.
296  string::size_type checkForCompressString(const string& fileName);
297 
298  struct InvalidConversion : public FitsException
299  {
300  InvalidConversion(const string& diag, bool silent=false);
301 
302  };
303 
304  struct MatchStem
305  {
306  bool operator()(const string& left, const string& right) const;
307  };
308 
309  static const double d1(0);
310  static const float f1(0);
311  static const std::complex<float> c1(0.);
312  static const std::complex<double> d2(0.);
313  static const string s1("");
314  static const int i1(0);
315  static const unsigned int u1(0);
316  static const long l1(0);
317  static const unsigned long ul1(0);
318  static const LONGLONG ll1(0);
319  static const short s2(0);
320  static const unsigned short us1(0);
321  static const bool b1(false);
322  static const unsigned char b2(0);
323 
324  char** CharArray(const std::vector<string>& inArray);
325 
326  string FITSType2String( int typeInt );
327 
328 
329  template <typename S, typename T>
330  void fill(std::vector<S>& outArray, const std::vector<T>& inArray,size_t first, size_t last);
331 
332  template <typename S, typename T>
333  void fill(std::valarray<S>& outArray, const std::valarray<T>& inArray);
334 
335  template <typename S, typename T>
336  void fill(std::valarray<S>& outArray, const std::vector<T>& inArray,size_t first, size_t last);
337 
338 
339  template <typename S, typename T>
340  void fill(std::vector<S>& outArray, const std::valarray<T>& inArray);
341 
342  // VF<-AF
343  void fill(std::vector<std::complex<float> >& outArray,
344  const std::valarray<std::complex<float> >& inArray);
345 
346  // VF<-AD
347  void fill(std::vector<std::complex<float> >& outArray,
348  const std::valarray<std::complex<double> >& inArray);
349 
350  // VD<-AD
351  void fill(std::vector<std::complex<double> >& outArray,
352  const std::valarray<std::complex<double> >& inArray);
353 
354 
355  // VD<-AF
356  void fill(std::vector<std::complex<double> >& outArray,
357  const std::valarray<std::complex<float> >& inArray);
358 
359  template <typename T>
360  void fill(std::vector<string>& outArray, const std::vector<T>& inArray, size_t first, size_t last);
361 
362  template <typename T>
363  void fill(std::vector<T>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
364 
365  template <typename S>
366  void fill(std::valarray<S>& outArray, const std::vector<string>& inArray,size_t first, size_t last);
367 
368 // template <typename S, typename T>
369 // void fill(std::valarray<std::complex<S> >& outArray, const std::valarray<std::complex<T> >& inArray);
370  // seems no other way of doing this.
371 
372  // VF<-VF
373 #ifdef TEMPLATE_AMBIG_DEFECT
374  void fillMSvfvf(std::vector<std::complex<float> >& outArray,
375  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
376 #endif
377 
378  void fill(std::vector<std::complex<float> >& outArray,
379  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
380 
381  // VF<-VD
382 #ifdef TEMPLATE_AMBIG_DEFECT
383  void fillMSvfvd(std::vector<std::complex<float> >& outArray,
384  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
385 #endif
386 
387  void fill(std::vector<std::complex<float> >& outArray,
388  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
389 
390  // VD<-VD
391 #ifdef TEMPLATE_AMBIG_DEFECT
392  void fillMSvdvd(std::vector<std::complex<double> >& outArray,
393  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
394 #endif
395 
396  void fill(std::vector<std::complex<double> >& outArray,
397  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
398 
399 #ifdef TEMPLATE_AMBIG_DEFECT
400  void fillMSvdvf(std::vector<std::complex<double> >& outArray,
401  const std::vector<std::complex<float> >& inArray,
402  size_t first, size_t last);
403 #else
404  void fill(std::vector<std::complex<double> >& outArray,
405  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
406 #endif
407 
408  // AF<-VD
409  void fill(std::valarray<std::complex<float> >& outArray,
410  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
411 
412  // AF<-VF
413 #ifdef TEMPLATE_AMBIG_DEFECT
414  void fillMSafvf(std::valarray<std::complex<float> >& outArray,
415  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
416 #else
417  void fill(std::valarray<std::complex<float> >& outArray,
418  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
419 #endif
420 
421  // AD<-VF
422 #ifdef TEMPLATE_AMBIG_DEFECT
423  void fillMSadvf(std::valarray<std::complex<double> >& outArray,
424  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
425 #else
426  void fill(std::valarray<std::complex<double> >& outArray,
427  const std::vector<std::complex<float> >& inArray, size_t first, size_t last);
428 #endif
429 
430  // AD<-VD
431 #ifdef TEMPLATE_AMBIG_DEFECT
432  void fillMSadvd(std::valarray<std::complex<double> >& outArray,
433  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
434 #else
435  void fill(std::valarray<std::complex<double> >& outArray,
436  const std::vector<std::complex<double> >& inArray, size_t first, size_t last);
437 #endif
438 
439  // AF<-AF
440  void fill(std::valarray<std::complex<float> >& outArray,
441  const std::valarray<std::complex<float> >& inArray);
442  // AD<-AD
443  void fill(std::valarray<std::complex<double> >& outArray,
444  const std::valarray<std::complex<double> >& inArray);
445  // AF<-AD
446  void fill(std::valarray<std::complex<float> >& outArray,
447  const std::valarray<std::complex<double> >& inArray);
448  // AD<-AF
449  void fill(std::valarray<std::complex<double> >& outArray,
450  const std::valarray<std::complex<float> >& inArray);
451 
452 #if TEMPLATE_AMBIG_DEFECT || TEMPLATE_AMBIG7_DEFECT
453  void fillMSvsvs(std::vector<string>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
454 #endif
455 
456 
457  void fill(std::vector<string>& outArray, const std::vector<string>& inArray, size_t first, size_t last);
458 
459  template <typename S, typename T>
460  string errorMessage(const S& out, const T& in);
461 
462 
463 
464  template <class T>
465  struct MatchPtrName
466  {
467  // Parameterized Class MatchPtrName
468  bool operator () (const T& left, const string& right) const;
469 
470  public:
471  protected:
472  private:
473  private: //## implementation
474  };
475 
476 
477 
478  template <class T>
479  struct MatchName
480  {
481  bool operator () (const T& left, const string& right) const;
482 
483  public:
484  protected:
485  private:
486  private: //## implementation
487  };
488 
489 
490 
491  template <class T>
492  struct MatchNum
493  {
494  bool operator () (const T& left, const int& right) const;
495 
496  public:
497  protected:
498  private:
499  private: //## implementation
500  };
501 
502 
503 
504  template <typename T>
505  struct MatchType
506  {
507  ValueType operator () ();
508 
509  public:
510  protected:
511  private:
512  private: //## implementation
513  };
514 
515 
516 
517  template <typename T>
518  struct CVarray
519  {
520  T* operator () (const std::vector<T>& inArray);
521 
522  public:
523  protected:
524  private:
525  private: //## implementation
526  };
527 
528 
529 
530  template <typename T>
531  struct FitsNullValue
532  {
533  T operator () ();
534 
535  public:
536  protected:
537  private:
538  private: //## implementation
539  };
540 
541 
542 
543  template <typename T>
544  struct MatchImageType
545  {
546  ImageType operator () ();
547 
548  public:
549  protected:
550  private:
551  private: //## implementation
552  };
553 
554 
555 
556  template <class T>
557  struct MatchPtrNum
558  {
559  bool operator () (const T& left, const int& right) const;
560 
561  public:
562  protected:
563  private:
564  private: //## implementation
565  };
566  // auto_ptr analogue for arrays.
567 
568 
569 
570  template <typename X>
572  {
573  public:
574  explicit auto_array_ptr (X* p = 0) throw ();
575  explicit auto_array_ptr (auto_array_ptr<X>& right) throw ();
576  ~auto_array_ptr();
577 
578  void operator = (auto_array_ptr<X>& right);
579  X& operator * () throw ();
580  X& operator [] (size_t i) throw ();
581  X operator [] (size_t i) const throw ();
582  X* get () const;
583  X* release () throw ();
584  X* reset (X* p) throw ();
585  static void remove (X*& x);
586 
587  protected:
588  private:
589  private: //## implementation
590  // Data Members for Class Attributes
591  X* m_p;
592 
593  };
594 
595 
596 
597  template <typename T>
598  struct ComparePtrIndex
599  {
600  bool operator () (const T* left, const T* right);
601 
602  public:
603  protected:
604  private:
605  private: //## implementation
606  };
607 
608 
609 
610  template <typename T>
611  struct CAarray
612  {
613  T* operator () (const std::valarray<T>& inArray);
614 
615  public:
616  protected:
617  private:
618  private: //## implementation
619  };
620 
621 
622 
623  template <typename T>
624  struct CVAarray
625  {
626  T* operator () (const std::vector< std::valarray<T> >& inArray);
627 
628  public:
629  protected:
630  private:
631  private: //## implementation
632  };
633 
634 
635 
636  class UnrecognizedType : public FitsException //## Inherits: <unnamed>%3CE143AB00C6
637  {
638  public:
639  UnrecognizedType (string diag, bool silent = true);
640 
641  protected:
642  private:
643  private: //## implementation
644  };
645 
646  // Parameterized Class CCfits::FITSUtil::MatchPtrName
647 
648  template <class T>
649  inline bool MatchPtrName<T>::operator () (const T& left, const string& right) const
650  {
651  return left->name() == right;
652  }
653 
654  // Parameterized Class CCfits::FITSUtil::MatchName
655 
656  template <class T>
657  inline bool MatchName<T>::operator () (const T& left, const string& right) const
658  {
659  return left.name() == right;
660  }
661 
662  // Parameterized Class CCfits::FITSUtil::MatchNum
663 
664  template <class T>
665  inline bool MatchNum<T>::operator () (const T& left, const int& right) const
666  {
667  return left.index() == right;
668  }
669 
670  // Parameterized Class CCfits::FITSUtil::MatchType
671 
672  // Parameterized Class CCfits::FITSUtil::CVarray
673 
674  template <typename T>
675  inline T* CVarray<T>::operator () (const std::vector<T>& inArray)
676  {
677 
678  // convert std containers used commonly in FITS to C arrays in an exception
679  // safe manner that is also clear about resource ownership.
680  auto_array_ptr<T> pC(new T[inArray.size()]);
681  T* c = pC.get();
682  std::copy(inArray.begin(),inArray.end(),&c[0]);
683  return pC.release();
684  }
685 
686  // Parameterized Class CCfits::FITSUtil::FitsNullValue
687 
688  template <typename T>
689  inline T FitsNullValue<T>::operator () ()
690  {
691  // This works for int types. Float, complex, and string types
692  // are handled below with specialized templates.
693  return 0;
694  }
695 
696  // Parameterized Class CCfits::FITSUtil::MatchImageType
697 
698  // Parameterized Class CCfits::FITSUtil::MatchPtrNum
699 
700  template <class T>
701  inline bool MatchPtrNum<T>::operator () (const T& left, const int& right) const
702  {
703  return left->index() == right;
704  }
705 
706  // Parameterized Class CCfits::FITSUtil::auto_array_ptr
707 
708  // Parameterized Class CCfits::FITSUtil::ComparePtrIndex
709 
710  // Parameterized Class CCfits::FITSUtil::CAarray
711 
712  // Parameterized Class CCfits::FITSUtil::CVAarray
713 
714  // Class CCfits::FITSUtil::UnrecognizedType
715 
716  // Parameterized Class CCfits::FITSUtil::MatchPtrName
717 
718  // Parameterized Class CCfits::FITSUtil::MatchName
719 
720  // Parameterized Class CCfits::FITSUtil::MatchNum
721 
722  // Parameterized Class CCfits::FITSUtil::MatchType
723 
724  template <typename T>
725  ValueType MatchType<T>::operator () ()
726  {
727 
728  if ( typeid(T) == typeid(d1) ) return Tdouble;
729  if ( typeid(T) == typeid(f1) ) return Tfloat;
730  if ( typeid(T) == typeid(c1) ) return Tcomplex;
731  if ( typeid(T) == typeid(d2) ) return Tdblcomplex;
732  if ( typeid(T) == typeid(s1) ) return Tstring;
733  if ( typeid(T) == typeid(i1) ) return Tint;
734  if ( typeid(T) == typeid(u1) ) return Tuint;
735  if ( typeid(T) == typeid(s2) ) return Tshort;
736  if ( typeid(T) == typeid(us1) ) return Tushort;
737  if ( typeid(T) == typeid(b1) ) return Tlogical;
738  if ( typeid(T) == typeid(b2) ) return Tbyte;
739  if ( typeid(T) == typeid(l1) ) return Tlong;
740  if ( typeid(T) == typeid(ul1) ) return Tulong;
741  // Carefull, on some compilers LONGLONG == long,
742  // so this should go after test for long.
743  if ( typeid(T) == typeid(ll1) ) return Tlonglong;
744  throw UnrecognizedType("Invalid data type for FITS Data I/O\n");
745  }
746 
747  // Parameterized Class CCfits::FITSUtil::CVarray
748 
749  // Parameterized Class CCfits::FITSUtil::MatchImageType
750 
751  template <typename T>
752  ImageType MatchImageType<T>::operator () ()
753  {
754  if ( typeid(T) == typeid(b2) ) return Ibyte;
755  if ( typeid(T) == typeid(s2) ) return Ishort;
756  if ( typeid(T) == typeid(l1) ) return Ilong;
757  if ( typeid(T) == typeid(f1) ) return Ifloat;
758  if ( typeid(T) == typeid(d1) ) return Idouble;
759  if ( typeid(T) == typeid(us1) ) return Iushort;
760  if ( typeid(T) == typeid(ul1) ) return Iulong;
761  if ( typeid(T) == typeid(ll1) ) return Ilonglong;
762  MatchType<T> errType;
763  string diag ("Image: ");
764  diag += FITSType2String(errType());
765  throw UnrecognizedType(diag);
766  }
767 
768  // Parameterized Class CCfits::FITSUtil::MatchPtrNum
769 
770  // Parameterized Class CCfits::FITSUtil::auto_array_ptr
771 
772  template <typename X>
774  : m_p(p)
775  {
776  }
777 
778  template <typename X>
780  : m_p(right.release())
781  {
782  }
783 
784 
785  template <typename X>
787  {
788  delete [] m_p;
789  }
790 
791 
792  template <typename X>
794  {
795  if (this != &right)
796  {
797  remove(m_p);
798  m_p = right.release();
799  }
800  }
801 
802  template <typename X>
804  {
805  return *m_p;
806  }
807 
808  template <typename X>
809  X& auto_array_ptr<X>::operator [] (size_t i) throw ()
810  {
811  return m_p[i];
812  }
813 
814  template <typename X>
815  X auto_array_ptr<X>::operator [] (size_t i) const throw ()
816  {
817  return m_p[i];
818  }
819 
820  template <typename X>
822  {
823  return m_p;
824  }
825 
826  template <typename X>
828  {
829  return reset(0);
830  }
831 
832  template <typename X>
833  X* auto_array_ptr<X>::reset (X* p) throw ()
834  {
835  // set the auto_ptr to manage p and return the old pointer it was managing.
836  X* __tmp = m_p;
837  m_p = p;
838  return __tmp;
839  }
840 
841  template <typename X>
843  {
844  X* __tmp(x);
845  x = 0;
846  delete [] __tmp;
847  }
848 
849  // Parameterized Class CCfits::FITSUtil::ComparePtrIndex
850 
851  template <typename T>
852  bool ComparePtrIndex<T>::operator () (const T* left, const T* right)
853  {
854  return (left->index() < right->index());
855  }
856 
857  // Parameterized Class CCfits::FITSUtil::CAarray
858 
859  template <typename T>
860  T* CAarray<T>::operator () (const std::valarray<T>& inArray)
861  {
862  size_t n(inArray.size());
863  auto_array_ptr<T> pC(new T[n]);
864  T* c= pC.get();
865  for (size_t j = 0; j < n; ++j) c[j] = inArray[j];
866  return pC.release();
867  }
868 
869  // Parameterized Class CCfits::FITSUtil::CVAarray
870 
871  template <typename T>
872  T* CVAarray<T>::operator () (const std::vector< std::valarray<T> >& inArray)
873  {
874  size_t sz(0);
875  size_t n(inArray.size());
876 
877  std::vector<size_t> nr(n);
878 
879  size_t i = 0; // for MS VC++ bug
880  for ( i = 0; i < n; ++i)
881  {
882  nr[i] = inArray[i].size();
883  sz += nr[i];
884 
885  }
886  auto_array_ptr<T> pC(new T[sz]);
887  T* c = pC.get();
888 
889  size_t k(0);
890  for ( i = 0; i < n; ++i)
891  {
892  size_t& m = nr[i];
893  const std::valarray<T>& current = inArray[i];
894  for (size_t j=0; j < m ; ++j) c[k++] = current[j];
895  }
896 
897  return pC.release();
898  }
899 
900  } // namespace FITSUtil
901 } // namespace CCfits
902 
903 namespace CCfits
904 {
905 
906  namespace FITSUtil
907  {
908 
909  template <typename T>
910  void swap(T& left, T& right)
911  {
912  T temp(left);
913  left = right;
914  right = temp;
915  }
916 
917  template <typename T>
918  void swap(std::vector<T>& left, std::vector<T>& right)
919  {
920  left.swap(right);
921  }
922 
923  template <>
924  inline string FitsNullValue<string>::operator () ()
925  {
926  return string("");
927  }
928 
929  template <>
930  inline float FitsNullValue<float>::operator () ()
931  {
932  return FLOATNULLVALUE;
933  }
934 
935  template <>
936  inline double FitsNullValue<double>::operator () ()
937  {
938  return DOUBLENULLVALUE;
939  }
940 
941  template <>
942  inline std::complex<float> FitsNullValue<std::complex<float> >::operator () ()
943  {
944  return std::complex<float>(FLOATNULLVALUE);
945  }
946 
947  template <>
948  inline std::complex<double> FitsNullValue<std::complex<double> >::operator () ()
949  {
950  return std::complex<double>(DOUBLENULLVALUE);
951  }
952 
953  } // end namespace FITSUtil
954 } // end namespace CCfits
955 
956 
957 
958 #endif
function object returning C array from a vector of valarrays. see CVarray for details ...
Definition: FITSUtil.h:624
X * release()
return underlying content of *this, transferring memory ownership
Definition: FITSUtil.h:827
as for MatchNum, only with the input class a pointer.
Definition: FITSUtil.h:557
X & operator[](size_t i)
return a reference to the ith element of the array
Definition: FITSUtil.h:809
A class that mimics the std:: library auto_ptr class, but works with arrays.
Definition: FITSUtil.h:571
predicate for classes that have a name attribute; match input string with instance name...
Definition: FITSUtil.h:479
X * reset(X *p)
change the content of the auto_array_ptr to p
Definition: FITSUtil.h:833
static void remove(X *&x)
utility function to delete the memory owned by x and set it to null.
Definition: FITSUtil.h:842
T * operator()(const std::vector< T > &inArray)
operator returning C array for use with scalar column data.
Definition: FITSUtil.h:675
function object returning C array from a valarray. see CVarray for details
Definition: FITSUtil.h:611
Function object class for returning C arrays from standard library objects used in the FITS library i...
Definition: FITSUtil.h:518
void operator=(auto_array_ptr< X > &right)
assignment operator: transfer of ownership semantics
Definition: FITSUtil.h:793
as for MatchName, only with the input class a pointer.
Definition: FITSUtil.h:465
function object that returns the FITS ValueType corresponding to an input intrinsic type ...
Definition: FITSUtil.h:505
predicate for classes that have an index attribute; match input index with instance value...
Definition: FITSUtil.h:492
FitsException is the base class for all exceptions thrown by this library.
Definition: FitsError.h:93
X * get() const
return a token for the underlying content of *this
Definition: FITSUtil.h:821
~auto_array_ptr()
destructor.
Definition: FITSUtil.h:786
auto_array_ptr(X *p=0)
constructor. allows creation of pointer to null, can be modified by reset()
Definition: FITSUtil.h:773
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition: CCfits.h:81
X & operator*()
deference operator
Definition: FITSUtil.h:803
T * operator()(const std::valarray< T > &inArray)
operator returning C array for use with image data.
Definition: FITSUtil.h:860
exception thrown by MatchType if it encounters data type incompatible with cfitsio.
Definition: FITSUtil.h:636
T * operator()(const std::vector< std::valarray< T > > &inArray)
operator returning C array for use with vector column data.
Definition: FITSUtil.h:872