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