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