ImageExt.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 IMAGEEXT_H
00013 #define IMAGEEXT_H 1
00014 
00015 // Image
00016 #include "Image.h"
00017 // ExtHDU
00018 #include "ExtHDU.h"
00019 // FITSUtil
00020 #include "FITSUtil.h"
00021 // HDUCreator
00022 #include "HDUCreator.h"
00023 #ifdef _MSC_VER
00024 #include "MSconfig.h" // for truncation warning
00025 #endif
00026 
00027 
00028 namespace CCfits {
00029 
00030 
00031 
00032   template <typename T>
00033   class ImageExt : public ExtHDU  //## Inherits: <unnamed>%3804A11121D8
00034   {
00035 
00036     public:
00037         ImageExt(const ImageExt< T > &right);
00038         ~ImageExt();
00039 
00040         virtual ImageExt<T> * clone (FITSBase* p) const;
00041         virtual void readData (bool readFlag = false, const std::vector<String>& keys = std::vector<String>());
00042         const std::valarray<T>& image () const;
00043         void setImage (const std::valarray<T>& inData);
00044         virtual void zero (double value);
00045         virtual void scale (double value);
00046         virtual double zero () const;
00047         virtual double scale () const;
00048 
00049       // Additional Public Declarations
00050 
00051     protected:
00052         ImageExt (FITSBase* p, const String &hduName, bool readDataFlag = false, const std::vector<String>& keys = std::vector<String>(), int version = 1);
00053         ImageExt (FITSBase* p, const String &hduName, int bpix, int naxis, const std::vector<long>& naxes, int version = 1);
00054 
00055       // Additional Protected Declarations
00056         virtual void checkExtensionType() const;
00057     private:
00058         virtual void initRead ();
00059         virtual std::ostream & put (std::ostream &s) const;
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 const std::valarray<T>& readImage (long first, long nElements, T* nullValue);
00064         //      Read data reads the image if readFlag is true and
00065         //      optional keywords if supplied. Thus, with no arguments,
00066         //      readData() does nothing.
00067         virtual const std::valarray<T>& readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue);
00068         //      Read data reads the image if readFlag is true and
00069         //      optional keywords if supplied. Thus, with no arguments,
00070         //      readData() does nothing.
00071         virtual void writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue = 0);
00072         //      Read data reads the image if readFlag is true and
00073         //      optional keywords if supplied. Thus, with no arguments,
00074         //      readData() does nothing.
00075         virtual void writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::valarray<T>& inData);
00076         const Image<T>& data () const;
00077 
00078       // Additional Private Declarations
00079 
00080     private: //## implementation
00081       // Data Members for Associations
00082         Image<T> m_data;
00083 
00084       // Additional Implementation Declarations
00085       friend class ExtHDU;
00086       friend class HDUCreator;
00087   };
00088 
00089   // Parameterized Class CCfits::ImageExt 
00090 
00091   template <typename T>
00092   inline std::ostream & ImageExt<T>::put (std::ostream &s) const
00093   {
00094   s << "Image Extension::  "  <<  " Name: " << name() << " Extension: " << xtension() 
00095           << " BITPIX "<< bitpix() << '\n';
00096 
00097   s <<  " Axis Lengths: \n";
00098   for (size_t j =1; j < static_cast<size_t>( axes() ) ; j++)
00099   {
00100         s << " Axis: " << j << "  " << axis(j-1) << '\n';  
00101   }
00102 
00103 
00104 
00105   s << "Image Extension:: Version: " << version() << " HDU number: " <<  index() << '\n';
00106 
00107   s << " HISTORY: " << history() << '\n';
00108   s << " COMMENTS: " <<comment() << '\n';
00109 
00110   s << "BinTable:: nKeywords: " << keyWord().size() << '\n';
00111 
00112     return s;
00113   }
00114 
00115   template <typename T>
00116   inline const Image<T>& ImageExt<T>::data () const
00117   {
00118     return m_data;
00119   }
00120 
00121   // Parameterized Class CCfits::ImageExt 
00122 
00123   template <typename T>
00124   ImageExt<T>::ImageExt(const ImageExt<T> &right)
00125       : ExtHDU(right), m_data(right.m_data)
00126   {
00127   }
00128 
00129   template <typename T>
00130   ImageExt<T>::ImageExt (FITSBase* p, const String &hduName, bool readDataFlag, const std::vector<String>& keys, int version)
00131       : ExtHDU(p,ImageHdu,hduName,version),  m_data()
00132   {
00133   initRead();
00134   if (readDataFlag || keys.size() ) readData(readDataFlag,keys);  
00135   }
00136 
00137   template <typename T>
00138   ImageExt<T>::ImageExt (FITSBase* p, const String &hduName, int bpix, int naxis, const std::vector<long>& naxes, int version)
00139       : ExtHDU(p,ImageHdu,hduName,bpix,naxis,naxes,version), m_data()
00140   {
00141   // resize m_image according to naxes, and data according to m_image,
00142   // and equate them. Valarray = must be performed on items of the same
00143   // size according to the standard.
00144   int status (0);
00145   FITSUtil::CVarray<long> convert;
00146   FITSUtil::auto_array_ptr<long> axis(convert(naxes));
00147   static char EXTNAME[] = "EXTNAME";
00148   static char HDUVERS[] = "HDUVERS";
00149 
00150           if ( fits_create_img(fitsPointer(), bpix, naxis, axis.get(), &status) )
00151           {
00152 
00153                 throw FitsError(status);
00154           } 
00155           else
00156           {
00157                 char * comment = 0;
00158                 if (fits_write_key(fitsPointer(),Tstring,EXTNAME,
00159                                 const_cast<char*>(hduName.c_str()), comment,&status)) 
00160                 {
00161                         throw FitsError(status);
00162                 }                
00163                 if (version != 0 && fits_write_key(fitsPointer(),Tint,HDUVERS,&version,
00164                                         comment,&status)) throw FitsError(status);     
00165           }      
00166   }
00167 
00168 
00169   template <typename T>
00170   ImageExt<T>::~ImageExt()
00171   {
00172   }
00173 
00174 
00175   template <typename T>
00176   void ImageExt<T>::initRead ()
00177   {
00178   }
00179 
00180   template <typename T>
00181   ImageExt<T> * ImageExt<T>::clone (FITSBase* p) const
00182   {
00183   ImageExt<T>* cloned = new ImageExt<T>(*this);
00184   cloned->parent() = p;
00185   return cloned;
00186   }
00187 
00188   template <typename T>
00189   void ImageExt<T>::readData (bool readFlag, const std::vector<String>& keys)
00190   {
00191   // Default reading mode. Read everything if readFlag is true.
00192   // this is identical to the equivalent method for PrimaryHDU<T>,
00193   // so will one day turn this into a simple call that shares the code.
00194   makeThisCurrent();
00195 
00196   if ( keys.size() > 0) 
00197   {
00198         std::list<string> keyList;
00199         // keys is converted to a list so that any keys not in the header
00200         // can be easily erased. internally an exception will be thrown,
00201         // on a missing key, and its catch clause will print a message.
00202         for (std::vector<string>::const_iterator j = keys.begin(); j != keys.end(); ++j)
00203         {
00204                 keyList.push_back(*j);
00205         } 
00206         readKeywords(keyList);
00207   }
00208 
00209   if ( readFlag)  // read the entire image, setting null values to FLT_MIN.
00210   {
00211 
00212         FITSUtil::FitsNullValue<T> null;
00213         T nulval = null();
00214         long first(1);
00215         long nelements(1);
00216         for (size_t i = 0; i < naxes().size(); i++) nelements *= naxes(i);
00217         m_data.readImage(fitsPointer(),first,nelements,&nulval,naxes(),anynul());
00218 
00219     }
00220   }
00221 
00222   template <typename T>
00223   const std::valarray<T>& ImageExt<T>::image () const
00224   {
00225 
00226     return m_data.image();
00227   }
00228 
00229   template <typename T>
00230   void ImageExt<T>::setImage (const std::valarray<T>& inData)
00231   {
00232   }
00233 
00234   template <typename T>
00235   const std::valarray<T>& ImageExt<T>::readImage (long first, long nElements, T* nullValue)
00236   {
00237     checkExtensionType();
00238     return m_data.readImage(fitsPointer(),first,nElements,nullValue,naxes(),anynul());
00239   }
00240 
00241   template <typename T>
00242   const std::valarray<T>& ImageExt<T>::readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue)
00243   {
00244     checkExtensionType();
00245     return m_data.readImage(fitsPointer(),firstVertex,lastVertex,stride,nullValue,naxes(),anynul());
00246   }
00247 
00248   template <typename T>
00249   void ImageExt<T>::writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue)
00250   {
00251     checkExtensionType();
00252     m_data.writeImage(fitsPointer(),first,nElements,inData,naxes(),nullValue);
00253   }
00254 
00255   template <typename T>
00256   void ImageExt<T>::writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::valarray<T>& inData)
00257   {
00258     checkExtensionType();
00259     m_data.writeImage(fitsPointer(),firstVertex,lastVertex,inData,naxes());
00260   }
00261 
00262   template <typename T>
00263   void ImageExt<T>::zero (double value)
00264   {
00265     HDU::zero(value);
00266     if (naxis())
00267     {
00268         int status(0);
00269         if (fits_set_bscale(fitsPointer(),scale(),value,&status)) throw FitsError(status);
00270     }
00271   }
00272 
00273   template <typename T>
00274   void ImageExt<T>::scale (double value)
00275   {
00276     HDU::scale(value);
00277     if (naxis())
00278     {
00279         int status(0);
00280         if (fits_set_bscale(fitsPointer(),value,zero(),&status)) throw FitsError(status);
00281     } 
00282   }
00283 
00284   template <typename T>
00285   double ImageExt<T>::zero () const
00286   {
00287 
00288     return HDU::zero();
00289   }
00290 
00291   template <typename T>
00292   double ImageExt<T>::scale () const
00293   {
00294 
00295     return HDU::scale();
00296   }
00297 
00298   // Additional Declarations
00299     template <typename T>
00300     inline void ImageExt<T>::checkExtensionType() const
00301     {
00302 
00303     }
00304 } // namespace CCfits
00305 
00306 
00307 #endif

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