ImageExt.h

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

Generated on Tue Feb 5 11:26:56 2008 for CCfits by  doxygen 1.5.4