ImageExt.h

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

Generated on Fri Nov 3 17:09:05 2006 for CCfits by  doxygen 1.4.7