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