PrimaryHDU.h

00001 //   Read the documentation to learn more about C++ code generator
00002 //   versioning.
00003 //      This is version 1.7 release dated June 2007
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, L3-Communications EER Systems Inc.
00011 
00012 #ifndef PRIMARYHDU_H
00013 #define PRIMARYHDU_H 1
00014 
00015 // Image
00016 #include "Image.h"
00017 // PHDU
00018 #include "PHDU.h"
00019 // FITS
00020 #include "FITS.h"
00021 // valarray
00022 #include <valarray>
00023 // HDUCreator
00024 #include "HDUCreator.h"
00025 #include "CCfits.h"
00026 #include <functional>
00027 #include <numeric>
00028 #include <memory>
00029 
00030 
00031 namespace CCfits {
00032 
00033 
00034 
00035   template <typename T>
00036   class PrimaryHDU : public PHDU  //## Inherits: <unnamed>%394E6F870338
00037   {
00038 
00039     public:
00040         virtual PrimaryHDU<T> * clone (FITSBase* p) const;
00041         //      Read data reads the image if readFlag is true and
00042         //      optional keywords if supplied. Thus, with no arguments,
00043         //      readData() does nothing.
00044         virtual void readData (bool readFlag = false, const std::vector<String>& keys = std::vector<String>());
00045         const std::valarray<T>& image () const;
00046         std::valarray<T>& image ();
00047         void setImage (const std::valarray<T>& inData);
00048         //      Read data reads the image if readFlag is true and
00049         //      optional keywords if supplied. Thus, with no arguments,
00050         //      readData() does nothing.
00051         virtual const std::valarray<T>& readImage (long first, long nElements, T* nullValue);
00052         //      Read data reads the image if readFlag is true and
00053         //      optional keywords if supplied. Thus, with no arguments,
00054         //      readData() does nothing.
00055         virtual const std::valarray<T>& readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue);
00056         //      Read data reads the image if readFlag is true and
00057         //      optional keywords if supplied. Thus, with no arguments,
00058         //      readData() does nothing.
00059         virtual void writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue = 0);
00060         //      Read data reads the image if readFlag is true and
00061         //      optional keywords if supplied. Thus, with no arguments,
00062         //      readData() does nothing.
00063         virtual void writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData);
00064 
00065       // Additional Public Declarations
00066 
00067     protected:
00068         //      Constructor for new FITS objects, takes as arguments
00069         //      the required keywords for a primary HDU.
00070         PrimaryHDU (FITSBase* p, const int bitpix, const int naxis, const std::vector<long>& naxes, const std::valarray<T>& data = std::valarray<T>());
00071         //      Custom constructor. Allows specification of data to be read and whether to read data at
00072         //      construction or wait until the image data are requested. The default is 'lazy initialization:'
00073         //      wait until asked.
00074         PrimaryHDU (FITSBase* p, bool readFlag = false, const std::vector<String>& keys = std::vector<String>());
00075 
00076       // Additional Protected Declarations
00077 
00078     private:
00079         PrimaryHDU(const PrimaryHDU< T > &right);
00080         PrimaryHDU< T > & operator=(const PrimaryHDU< T > &right);
00081 
00082         virtual std::ostream & put (std::ostream &s) const;
00083         const Image<T>& data () const;
00084 
00085       // Additional Private Declarations
00086 
00087     private: //## implementation
00088       // Data Members for Associations
00089         Image<T> m_data;
00090 
00091       // Additional Implementation Declarations
00092       friend class HDUCreator;
00093       friend class PHDU;
00094   };
00095 
00096   // Parameterized Class CCfits::PrimaryHDU 
00097 
00098   template <typename T>
00099   inline std::ostream & PrimaryHDU<T>::put (std::ostream &s) const
00100   {
00101   s << "PrimaryHDU:: Simple? " << simple() << " Extend?: " << extend() << 
00102     " Bitpix: " << bitpix() << " naxis = " << axes() << "\n";
00103   s << "Axis Lengths: \n";
00104 
00105 
00106 
00107   for (int i=0; i < axes(); i++)
00108      s  << " axis[" << i << "] " << axis(i) << "\n";
00109 
00110  s << "\nNumber of keywords read: " << keyWord().size() <<  "\n";
00111 
00112   for (std::map<String,Keyword*>::const_iterator ki = keyWord().begin();
00113         ki != keyWord().end(); ki++)
00114   {
00115         s << *((*ki).second) << std::endl;              
00116   }  
00117 
00118 
00119   s << " HISTORY: " << history() << '\n';
00120   s << " COMMENTS: " <<comment() << '\n';
00121   return s;  
00122   }
00123 
00124   template <typename T>
00125   inline const Image<T>& PrimaryHDU<T>::data () const
00126   {
00127     return m_data;
00128   }
00129 
00130   // Parameterized Class CCfits::PrimaryHDU 
00131 
00132   template <typename T>
00133   PrimaryHDU<T>::PrimaryHDU(const PrimaryHDU<T> &right)
00134       : PHDU(right), m_data(right.m_data)
00135   {
00136   }
00137 
00138   template <typename T>
00139   PrimaryHDU<T>::PrimaryHDU (FITSBase* p, const int bitpix, const int naxis, const std::vector<long>& naxes, const std::valarray<T>& data)
00140         : PHDU(p,bitpix,naxis,naxes),m_data(data)
00141   {
00142   }
00143 
00144   template <typename T>
00145   PrimaryHDU<T>::PrimaryHDU (FITSBase* p, bool readFlag, const std::vector<String>& keys)
00146         : PHDU(p), m_data()
00147   {
00148   initRead();
00149 
00150   if (readFlag || keys.size()) readData(readFlag,keys);  
00151 
00152   }
00153 
00154 
00155   template <typename T>
00156   PrimaryHDU<T> * PrimaryHDU<T>::clone (FITSBase* p) const
00157   {
00158   PrimaryHDU<T>* cloned = new PrimaryHDU<T>(*this);
00159   cloned->parent() = p;
00160   return cloned;
00161   }
00162 
00163   template <typename T>
00164   void PrimaryHDU<T>::readData (bool readFlag, const std::vector<String>& keys)
00165   {
00166 
00167   // Default reading mode. Read everything if readFlag is true.
00168   makeThisCurrent();
00169 
00170   if ( keys.size() > 0) 
00171   {
00172         std::list<String> keyList(keys.size());
00173         // keys is converted to a list so that any keys not in the header
00174         // can be easily erased. internally an exception will be thrown,
00175         // on a missing key, and its catch clause will print a message.
00176         std::copy(keys.begin(),keys.end(),keyList.begin());
00177         readKeywords(keyList);
00178   }
00179   // read the entire image, setting null values to the
00180   // return value from FitsNullValue<T>. It would be easy to make the null value
00181   // a user defined input, but that's not implemented yet.
00182   if ( readFlag && (naxis() > 0) )  
00183   {
00184         FITSUtil::FitsNullValue<T> null;
00185         long init(1);
00186         T nulValue(null());
00187         long nelements(std::accumulate(&naxes()[0],&naxes()[naxis()],init,std::multiplies<long>() ));
00188         readImage(1,nelements,&nulValue);
00189 
00190     }
00191   }
00192 
00193   template <typename T>
00194   const std::valarray<T>& PrimaryHDU<T>::image () const
00195   {
00196 
00197     return m_data.image();
00198   }
00199 
00200   template <typename T>
00201   std::valarray<T>& PrimaryHDU<T>::image ()
00202   {
00203 
00204     return m_data.image();
00205   }
00206 
00207   template <typename T>
00208   void PrimaryHDU<T>::setImage (const std::valarray<T>& inData)
00209   {
00210     m_data.image().resize(inData.size());
00211     m_data.setImage(inData);
00212   }
00213 
00214   template <typename T>
00215   const std::valarray<T>& PrimaryHDU<T>::readImage (long first, long nElements, T* nullValue)
00216   {
00217     makeThisCurrent();
00218     return m_data.readImage(fitsPointer(),first,nElements,nullValue,naxes(),anynul());
00219   }
00220 
00221   template <typename T>
00222   const std::valarray<T>& PrimaryHDU<T>::readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue)
00223   {
00224     makeThisCurrent();
00225     return m_data.readImage(fitsPointer(),firstVertex,lastVertex,stride,nullValue,naxes(),anynul());
00226   }
00227 
00228   template <typename T>
00229   void PrimaryHDU<T>::writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue)
00230   {
00231     m_data.writeImage(fitsPointer(),first,nElements,inData,naxes(),nullValue);
00232   }
00233 
00234   template <typename T>
00235   void PrimaryHDU<T>::writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData)
00236   {
00237     m_data.writeImage(fitsPointer(),firstVertex,lastVertex,stride,inData,naxes());
00238   }
00239 
00240   // Additional Declarations
00241 
00242 } // namespace CCfits
00243 
00244 
00245 #endif

Generated on Thu Jun 28 11:49:08 2007 for CCfits by  doxygen 1.4.7