CCfits: ImageExt.h Source File

CCfits 2.7

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List

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