CCfits  2.4
ImageExt.h
1 // Astrophysics Science Division,
2 // NASA/ Goddard Space Flight Center
3 // HEASARC
4 // http://heasarc.gsfc.nasa.gov
5 // e-mail: ccfits@legacy.gsfc.nasa.gov
6 //
7 // Original author: Ben Dorman
8 
9 #ifndef IMAGEEXT_H
10 #define IMAGEEXT_H 1
11 
12 // ExtHDU
13 #include "ExtHDU.h"
14 // HDUCreator
15 #include "HDUCreator.h"
16 // Image
17 #include "Image.h"
18 // FITSUtil
19 #include "FITSUtil.h"
20 #ifdef _MSC_VER
21 #include "MSconfig.h" // for truncation warning
22 #endif
23 
24 
25 namespace CCfits {
26 
55  template <typename T>
56  class ImageExt : public ExtHDU //## Inherits: <unnamed>%3804A11121D8
57  {
58 
59  public:
60  virtual ~ImageExt();
61 
62  virtual ImageExt<T> * clone (FITSBase* p) const;
63  virtual void readData (bool readFlag = false, const std::vector<String>& keys = std::vector<String>());
64  const std::valarray<T>& image () const;
65  virtual void zero (double value);
66  virtual void scale (double value);
67  virtual double zero () const;
68  virtual double scale () const;
69 
70  // Additional Public Declarations
71 
72  protected:
73  ImageExt (FITSBase* p, const String &hduName, bool readDataFlag = false, const std::vector<String>& keys = std::vector<String>(), int version = 1);
74  ImageExt (FITSBase* p, const String &hduName, int bpix, int naxis, const std::vector<long>& naxes, int version = 1);
75 
76  // Additional Protected Declarations
77  virtual void checkExtensionType() const;
78  private:
79  ImageExt(const ImageExt< T > &right);
80  ImageExt< T > & operator=(const ImageExt< T > &right);
81 
82  virtual void initRead ();
83  virtual std::ostream & put (std::ostream &s) const;
84  // Read data reads the image if readFlag is true and
85  // optional keywords if supplied. Thus, with no arguments,
86  // readData() does nothing.
87  virtual const std::valarray<T>& readImage (long first, long nElements, T* nullValue);
88  // Read data reads the image if readFlag is true and
89  // optional keywords if supplied. Thus, with no arguments,
90  // readData() does nothing.
91  virtual const std::valarray<T>& readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue);
92  // Read data reads the image if readFlag is true and
93  // optional keywords if supplied. Thus, with no arguments,
94  // readData() does nothing.
95  virtual void writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue = 0);
96  // Read data reads the image if readFlag is true and
97  // optional keywords if supplied. Thus, with no arguments,
98  // readData() does nothing.
99  virtual void writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::valarray<T>& inData);
100  const Image<T>& data () const;
101 
102  // Additional Private Declarations
103 
104  private: //## implementation
105  // Data Members for Associations
106  Image<T> m_data;
107 
108  // Additional Implementation Declarations
109  friend class ExtHDU;
110  friend class HDUCreator;
111  };
112 
113  // Parameterized Class CCfits::ImageExt
114 
115  template <typename T>
116  inline std::ostream & ImageExt<T>::put (std::ostream &s) const
117  {
118  s << "Image Extension:: " << " Name: " << name() << " Extension: " << xtension()
119  << " BITPIX "<< bitpix() << '\n';
120 
121  s << " Axis Lengths: \n";
122  for (size_t j =1; j < static_cast<size_t>( axes() ) ; j++)
123  {
124  s << " Axis: " << j << " " << axis(j-1) << '\n';
125  }
126 
127 
128 
129  s << "Image Extension:: Version: " << version() << " HDU number: " << index() << '\n';
130 
131  s << " HISTORY: " << history() << '\n';
132  s << " COMMENTS: " <<comment() << '\n';
133 
134  s << "BinTable:: nKeywords: " << keyWord().size() << '\n';
135 
136  return s;
137  }
138 
139  template <typename T>
140  inline const Image<T>& ImageExt<T>::data () const
141  {
142  return m_data;
143  }
144 
145  // Parameterized Class CCfits::ImageExt
146 
147  template <typename T>
148  ImageExt<T>::ImageExt(const ImageExt<T> &right)
149  : ExtHDU(right), m_data(right.m_data)
150  {
151  }
152 
153  template <typename T>
154  ImageExt<T>::ImageExt (FITSBase* p, const String &hduName, bool readDataFlag, const std::vector<String>& keys, int version)
155  : ExtHDU(p,ImageHdu,hduName,version), m_data()
156  {
157  initRead();
158  if (readDataFlag || keys.size() ) readData(readDataFlag,keys);
159  }
160 
161  template <typename T>
162  ImageExt<T>::ImageExt (FITSBase* p, const String &hduName, int bpix, int naxis, const std::vector<long>& naxes, int version)
163  : ExtHDU(p,ImageHdu,hduName,bpix,naxis,naxes,version), m_data()
164  {
165  // resize m_image according to naxes, and data according to m_image,
166  // and equate them. Valarray = must be performed on items of the same
167  // size according to the standard.
168  int status (0);
169  FITSUtil::CVarray<long> convert;
170  FITSUtil::auto_array_ptr<long> axis(convert(naxes));
171  static char EXTNAME[] = "EXTNAME";
172  static char HDUVERS[] = "HDUVERS";
173 
174  if ( fits_create_img(fitsPointer(), bpix, naxis, axis.get(), &status) )
175  {
176 
177  throw FitsError(status);
178  }
179  else
180  {
181  char * comment = 0;
182  if (fits_write_key(fitsPointer(),Tstring,EXTNAME,
183  const_cast<char*>(hduName.c_str()), comment,&status))
184  {
185  throw FitsError(status);
186  }
187  if (version != 0 && fits_write_key(fitsPointer(),Tint,HDUVERS,&version,
188  comment,&status)) throw FitsError(status);
189  }
190  }
191 
192 
193  template <typename T>
194  ImageExt<T>::~ImageExt()
195  {
196  }
197 
198 
199  template <typename T>
200  void ImageExt<T>::initRead ()
201  {
202  }
203 
204  template <typename T>
205  ImageExt<T> * ImageExt<T>::clone (FITSBase* p) const
206  {
207  ImageExt<T>* cloned = new ImageExt<T>(*this);
208  cloned->parent() = p;
209  return cloned;
210  }
211 
212  template <typename T>
213  void ImageExt<T>::readData (bool readFlag, const std::vector<String>& keys)
214  {
215  // Default reading mode. Read everything if readFlag is true.
216  // this is identical to the equivalent method for PrimaryHDU<T>,
217  // so will one day turn this into a simple call that shares the code.
218  makeThisCurrent();
219 
220  if ( keys.size() > 0)
221  {
222  std::list<string> keyList;
223  // keys is converted to a list so that any keys not in the header
224  // can be easily erased. internally an exception will be thrown,
225  // on a missing key, and its catch clause will print a message.
226  for (std::vector<string>::const_iterator j = keys.begin(); j != keys.end(); ++j)
227  {
228  keyList.push_back(*j);
229  }
230  readKeywords(keyList);
231  }
232 
233  if ( readFlag) // read the entire image, setting null values to FLT_MIN.
234  {
235 
236  FITSUtil::FitsNullValue<T> null;
237  T nulval = null();
238  long first(1);
239  long nelements(1);
240  for (size_t i = 0; i < naxes().size(); i++) nelements *= naxes(i);
241  m_data.readImage(fitsPointer(),first,nelements,&nulval,naxes(),anynul());
242 
243  }
244  }
245 
246  template <typename T>
247  const std::valarray<T>& ImageExt<T>::image () const
248  {
249 
250  return m_data.image();
251  }
252 
253  template <typename T>
254  const std::valarray<T>& ImageExt<T>::readImage (long first, long nElements, T* nullValue)
255  {
256  checkExtensionType();
257  return m_data.readImage(fitsPointer(),first,nElements,nullValue,naxes(),anynul());
258  }
259 
260  template <typename T>
261  const std::valarray<T>& ImageExt<T>::readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue)
262  {
263  checkExtensionType();
264  return m_data.readImage(fitsPointer(),firstVertex,lastVertex,stride,nullValue,naxes(),anynul());
265  }
266 
267  template <typename T>
268  void ImageExt<T>::writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue)
269  {
270  checkExtensionType();
271  m_data.writeImage(fitsPointer(),first,nElements,inData,naxes(),nullValue);
272  }
273 
274  template <typename T>
275  void ImageExt<T>::writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::valarray<T>& inData)
276  {
277  checkExtensionType();
278  m_data.writeImage(fitsPointer(),firstVertex,lastVertex,inData,naxes());
279  }
280 
281  template <typename T>
282  void ImageExt<T>::zero (double value)
283  {
284  makeThisCurrent();
285  if (checkImgDataTypeChange(value, scale()))
286  {
287  if (naxis())
288  {
289  int status(0);
290  if (fits_update_key(fitsPointer(), Tdouble, BZERO, &value, 0, &status))
291  throw FitsError(status);
292  fits_flush_file(fitsPointer(), &status);
293  HDU::zero(value);
294  }
295  }
296  else
297  {
298  bool silent=false;
299  string msg("CCfits Error: Cannot set BZERO to a value which will change image data\n");
300  msg += " from integer type to floating point type.";
301  throw FitsException(msg,silent);
302  }
303  }
304 
305  template <typename T>
306  void ImageExt<T>::scale (double value)
307  {
308  makeThisCurrent();
309  if (checkImgDataTypeChange(zero(), value))
310  {
311  if (naxis())
312  {
313  int status(0);
314  if (fits_update_key(fitsPointer(), Tdouble, BSCALE, &value, 0, &status))
315  throw FitsError(status);
316  fits_flush_file(fitsPointer(), &status);
317  HDU::scale(value);
318  }
319  }
320  else
321  {
322  bool silent=false;
323  string msg("CCfits Error: Cannot set BSCALE to a value which will change image data\n");
324  msg += " from integer type to floating point type.";
325  throw FitsException(msg,silent);
326  }
327  }
328 
329  template <typename T>
330  double ImageExt<T>::zero () const
331  {
332 
333  return HDU::zero();
334  }
335 
336  template <typename T>
337  double ImageExt<T>::scale () const
338  {
339 
340  return HDU::scale();
341  }
342 
343  // Additional Declarations
344  template <typename T>
345  inline void ImageExt<T>::checkExtensionType() const
346  {
347 
348  }
349 } // namespace CCfits
350 
351 
352 #endif
HduType xtension() const
return the extension type
Definition: ExtHDU.h:669
int version() const
return the extension version number.
Definition: ExtHDU.h:659
long axis(size_t index) const
return the size of axis numbered index [zero based].
Definition: HDU.h:841
long bitpix() const
return the data type keyword.
Definition: HDU.h:858
fitsfile * fitsPointer() const
return the fitsfile pointer for the FITS object containing the HDU
Definition: HDU.cxx:309
std::map< String, Keyword * > & keyWord()
return the associative array containing the HDU keywords so far read.
Definition: HDU.h:893
Namespace enclosing all CCfits classes and globals definitions.
Definition: AsciiTable.cxx:26
virtual void makeThisCurrent() const
move the fitsfile pointer to this current HDU.
Definition: HDU.cxx:321
void index(int value)
set the HDU number
Definition: HDU.h:847
const String & name() const
return the name of the extension.
Definition: ExtHDU.h:633
ExtHDU(const ExtHDU &right)
copy constructor
Definition: ExtHDU.cxx:55
long axes() const
return the number of axes in the HDU data section (always 2 for tables).
Definition: HDU.h:835
std::vector< long > & naxes()
return the HDU data axis array.
Definition: HDU.h:943
const string & history() const
return the history string previously read by getHistory()
Definition: HDU.h:825
virtual double scale() const
return the BSCALE keyword value
Definition: HDU.h:868
virtual double zero() const
return the BZERO keyword value
Definition: HDU.h:878
const string & comment() const
return the comment string previously read by getComment()
Definition: HDU.h:820