// Astrophysics Science Division, // NASA/ Goddard Space Flight Center // HEASARC // http://heasarc.gsfc.nasa.gov // e-mail: ccfits@legacy.gsfc.nasa.gov // // Original author: Ben Dorman #ifndef IMAGEEXT_H #define IMAGEEXT_H 1 // ExtHDU #include "ExtHDU.h" // HDUCreator #include "HDUCreator.h" // Image #include "Image.h" // FITSUtil #include "FITSUtil.h" #ifdef _MSC_VER #include "MSconfig.h" // for truncation warning #endif namespace CCfits { /*! \class ImageExt ImageExt is a subclass of ExtHDU that contains image data of type T. */ /*! \fn virtual ImageExt::~ImageExt(); \brief destructor */ /*! \fn virtual void ImageExt::readData (bool readFlag = false, const std::vector& keys = std::vector()); \brief read Image extension HDU data Called by FITS ctor, not intended for general use. parameters control how much gets read on initialization. \param readFlag read the image data if true \param key a vector of strings of keyword names to be read from the HDU */ template class ImageExt : public ExtHDU //## Inherits: %3804A11121D8 { public: virtual ~ImageExt(); virtual ImageExt * clone (FITSBase* p) const; virtual void readData (bool readFlag = false, const std::vector& keys = std::vector()); virtual void zero (double value); virtual void scale (double value); virtual double zero () const; virtual double scale () const; virtual void suppressScaling(bool toggle = true); virtual void resetImageRead (); // Additional Public Declarations protected: ImageExt (FITSBase* p, const String &hduName, bool readDataFlag = false, const std::vector& keys = std::vector(), int version = 1); ImageExt (FITSBase* p, const String &hduName, int bpix, int naxis, const std::vector& naxes, int version = 1); // Additional Protected Declarations virtual void checkExtensionType() const; private: ImageExt(const ImageExt< T > &right); ImageExt< T > & operator=(const ImageExt< T > &right); virtual void initRead (); virtual std::ostream & put (std::ostream &s) const; const std::valarray& readImage (long first, long nElements, T* nullValue); const std::valarray& readImage (const std::vector& firstVertex, const std::vector& lastVertex, const std::vector& stride,T* nullValue); void writeImage (long first, long nElements, const std::valarray& inData, T* nullValue = 0); void writeImage (const std::vector& firstVertex, const std::vector& lastVertex, const std::valarray& inData); const Image& data () const; // Additional Private Declarations private: //## implementation // Data Members for Associations Image m_data; // Additional Implementation Declarations friend class ExtHDU; friend class HDUCreator; }; // Parameterized Class CCfits::ImageExt template inline std::ostream & ImageExt::put (std::ostream &s) const { s << "Image Extension:: " << " Name: " << name() << " Extension: " << xtension() << " BITPIX "<< bitpix() << '\n'; s << " Axis Lengths: \n"; for (size_t j =1; j <= static_cast( axes() ) ; j++) { s << " Axis: " << j << " " << axis(j-1) << '\n'; } s << "Image Extension:: Version: " << version() << " HDU number: " << index() << '\n'; s << " HISTORY: " << history() << '\n'; s << " COMMENTS: " < inline const Image& ImageExt::data () const { return m_data; } // Parameterized Class CCfits::ImageExt template ImageExt::ImageExt(const ImageExt &right) : ExtHDU(right), m_data(right.m_data) { } template ImageExt::ImageExt (FITSBase* p, const String &hduName, bool readDataFlag, const std::vector& keys, int version) : ExtHDU(p,ImageHdu,hduName,version), m_data() { initRead(); if (readDataFlag || keys.size() ) readData(readDataFlag,keys); } template ImageExt::ImageExt (FITSBase* p, const String &hduName, int bpix, int naxis, const std::vector& naxes, int version) : ExtHDU(p,ImageHdu,hduName,bpix,naxis,naxes,version), m_data() { // resize m_image according to naxes, and data according to m_image, // and equate them. Valarray = must be performed on items of the same // size according to the standard. int status (0); FITSUtil::CVarray convert; FITSUtil::auto_array_ptr axis(convert(naxes)); static char EXTNAME[] = "EXTNAME"; static char HDUVERS[] = "HDUVERS"; if ( fits_create_img(fitsPointer(), bpix, naxis, axis.get(), &status) ) { throw FitsError(status); } else { char * comment = 0; if (fits_write_key(fitsPointer(),Tstring,EXTNAME, const_cast(hduName.c_str()), comment,&status)) { throw FitsError(status); } if (version != 0 && fits_write_key(fitsPointer(),Tint,HDUVERS,&version, comment,&status)) throw FitsError(status); } } template ImageExt::~ImageExt() { } template void ImageExt::initRead () { } template ImageExt * ImageExt::clone (FITSBase* p) const { ImageExt* cloned = new ImageExt(*this); cloned->parent() = p; return cloned; } template void ImageExt::readData (bool readFlag, const std::vector& keys) { // Default reading mode. Read everything if readFlag is true. // this is identical to the equivalent method for PrimaryHDU, // so will one day turn this into a simple call that shares the code. makeThisCurrent(); if ( keys.size() > 0) { std::list keyList; // keys is converted to a list so that any keys not in the header // can be easily erased. internally an exception will be thrown, // on a missing key, and its catch clause will print a message. for (std::vector::const_iterator j = keys.begin(); j != keys.end(); ++j) { keyList.push_back(*j); } readKeywords(keyList); } if ( readFlag) // read the entire image, setting null values to FLT_MIN. { FITSUtil::FitsNullValue null; T nulval = null(); long first(1); long nelements(1); for (size_t i = 0; i < naxes().size(); i++) nelements *= naxes(i); m_data.readImage(fitsPointer(),first,nelements,&nulval,naxes(),anynul()); } } template const std::valarray& ImageExt::readImage (long first, long nElements,T* nullValue) { checkExtensionType(); return m_data.readImage(fitsPointer(),first,nElements,nullValue,naxes(),anynul()); } template const std::valarray& ImageExt::readImage (const std::vector& firstVertex, const std::vector& lastVertex, const std::vector& stride, T* nullValue) { checkExtensionType(); return m_data.readImage(fitsPointer(),firstVertex,lastVertex,stride,nullValue,naxes(),anynul()); } template void ImageExt::writeImage (long first, long nElements, const std::valarray& inData, T* nullValue) { checkExtensionType(); long newNaxisN=0; m_data.writeImage(fitsPointer(),first,nElements,inData,naxes(),newNaxisN,nullValue); if (newNaxisN) naxes(naxes().size()-1,newNaxisN); } template void ImageExt::writeImage (const std::vector& firstVertex, const std::vector& lastVertex, const std::valarray& inData) { checkExtensionType(); long newNaxisN=0; m_data.writeImage(fitsPointer(),firstVertex,lastVertex,inData,naxes(),newNaxisN); if (newNaxisN) naxes(naxes().size()-1,newNaxisN); } template void ImageExt::zero (double value) { makeThisCurrent(); if (checkImgDataTypeChange(value, scale())) { if (naxis()) { int status(0); if (fits_update_key(fitsPointer(), Tdouble, BZERO, &value, 0, &status)) throw FitsError(status); fits_flush_file(fitsPointer(), &status); HDU::zero(value); } } else { bool silent=false; string msg("CCfits Error: Cannot set BZERO to a value which will change image data\n"); msg += " from integer type to floating point type."; throw FitsException(msg,silent); } m_data.scalingHasChanged(); } template void ImageExt::scale (double value) { makeThisCurrent(); if (checkImgDataTypeChange(zero(), value)) { if (naxis()) { int status(0); if (fits_update_key(fitsPointer(), Tdouble, BSCALE, &value, 0, &status)) throw FitsError(status); fits_flush_file(fitsPointer(), &status); HDU::scale(value); } } else { bool silent=false; string msg("CCfits Error: Cannot set BSCALE to a value which will change image data\n"); msg += " from integer type to floating point type."; throw FitsException(msg,silent); } m_data.scalingHasChanged(); } template double ImageExt::zero () const { return HDU::zero(); } template double ImageExt::scale () const { return HDU::scale(); } template void ImageExt::suppressScaling (bool toggle) { HDU::suppressScaling(toggle); m_data.scalingHasChanged(); } template void ImageExt::resetImageRead() { m_data.resetRead(); } // Additional Declarations template inline void ImageExt::checkExtensionType() const { } } // namespace CCfits #endif