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