CCfits  2.6
ExtHDUT.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 EXTHDUT_H
10 #define EXTHDUT_H
11 #include "ImageExt.h"
12 #include "Table.h"
13 #include "Column.h"
14 
15 namespace CCfits
16 {
17  template <typename S>
18  void ExtHDU::read (std::valarray<S>& image)
19  {
21  long init(1);
22  long nElements(std::accumulate(naxes().begin(),naxes().end(),init,
23  std::multiplies<long>()));
24  read(image,1,nElements,static_cast<S*>(0));
25 
26 
27  }
28 
29 
30 
31  template <typename S>
32  void ExtHDU::read (std::valarray<S>& image, long first,long nElements)
33  {
35  read(image, first,nElements,static_cast<S*>(0));
36  }
37 
38  template <typename S>
39  void ExtHDU::read (std::valarray<S>& image, long first, long nElements, S* nulValue)
40  {
41 
42  makeThisCurrent();
43  if ( ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
44  {
45  // proceed if cast is successful.
46  const std::valarray<S>& __tmp =
47  extimage->readImage(first,nElements,nulValue);
48  image.resize(__tmp.size());
49  image = __tmp;
50  }
51  else
52  {
53  if (bitpix() == Ifloat)
54  {
55  ImageExt<float>& extimage
56  = dynamic_cast<ImageExt<float>&>(*this);
57  float nulVal(0);
58  if (nulValue) nulVal = static_cast<float>(*nulValue);
59  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
60  }
61  else if (bitpix() == Idouble)
62  {
63  ImageExt<double>& extimage
64  = dynamic_cast<ImageExt<double>&>(*this);
65  double nulVal(0);
66  if (nulValue) nulVal = static_cast<double>(*nulValue);
67  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
68  }
69  else if (bitpix() == Ibyte)
70  {
71  ImageExt<unsigned char>& extimage
72  = dynamic_cast<ImageExt<unsigned char>&>(*this);
73  unsigned char nulVal(0);
74  if (nulValue) nulVal = static_cast<unsigned char>(*nulValue);
75  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
76  }
77  else if (bitpix() == Ilong)
78  {
79  if ( zero() == ULBASE && scale() == 1)
80  {
81  ImageExt<unsigned INT32BIT>& extimage
82  = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
83  unsigned INT32BIT nulVal(0);
84  if (nulValue) nulVal
85  = static_cast<unsigned INT32BIT>(*nulValue);
86  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
87  }
88  else
89  {
90  ImageExt<INT32BIT>& extimage
91  = dynamic_cast<ImageExt<INT32BIT>&>(*this);
92  INT32BIT nulVal(0);
93  if (nulValue) nulVal = static_cast<INT32BIT>(*nulValue);
94  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
95  }
96  }
97  else if (bitpix() == Ilonglong)
98  {
99  ImageExt<LONGLONG>& extimage
100  = dynamic_cast<ImageExt<LONGLONG>&>(*this);
101  LONGLONG nulVal(0);
102  if (nulValue) nulVal = static_cast<LONGLONG>(*nulValue);
103  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
104  }
105  else if (bitpix() == Ishort)
106  {
107  if ( zero() == USBASE && scale() == 1)
108  {
109  ImageExt<unsigned short>& extimage
110  = dynamic_cast<ImageExt<unsigned short>&>(*this);
111  unsigned short nulVal(0);
112  if (nulValue) nulVal
113  = static_cast<unsigned short>(*nulValue);
114  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
115  }
116  else
117  {
118  ImageExt<short>& extimage
119  = dynamic_cast<ImageExt<short>&>(*this);
120  short nulVal(0);
121  if (nulValue) nulVal = static_cast<short>(*nulValue);
122  FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
123  }
124  }
125  else
126  {
127  throw CCfits::FitsFatal(" casting image types ");
128  }
129  }
130 
131  }
132 
133  template<typename S>
134  void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& first,
135  long nElements,
136  S* nulValue)
137  {
138  makeThisCurrent();
139  long firstElement(0);
140  long dimSize(1);
141  std::vector<long> inputDimensions(naxis(),1);
142  size_t sNaxis = static_cast<size_t>(naxis());
143  size_t n(std::min(sNaxis,first.size()));
144  std::copy(&first[0],&first[0]+n,&inputDimensions[0]);
145  for (long i = 0; i < naxis(); ++i)
146  {
147 
148  firstElement += ((inputDimensions[i] - 1)*dimSize);
149  dimSize *=naxes(i);
150  }
151  ++firstElement;
152 
153 
154  read(image, firstElement,nElements,nulValue);
155 
156 
157 
158  }
159 
160  template<typename S>
161  void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& first,
162  long nElements)
163  {
164  makeThisCurrent();
165  read(image, first,nElements,static_cast<S*>(0));
166 
167  }
168 
169  template<typename S>
170  void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex,
171  const std::vector<long>& lastVertex,
172  const std::vector<long>& stride,
173  S* nulValue)
174  {
175  makeThisCurrent();
176  if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
177  {
178  const std::valarray<S>& __tmp =
179  extimage->readImage(firstVertex,lastVertex,stride,nulValue);
180  image.resize(__tmp.size());
181  image = __tmp;
182  }
183  else
184  {
185  // FITSutil::fill will take care of sizing.
186  if (bitpix() == Ifloat)
187  {
188  float nulVal(0);
189  if (nulValue) nulVal = static_cast<float>(*nulValue);
190  ImageExt<float>& extimage = dynamic_cast<ImageExt<float>&>(*this);
191  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
192  }
193  else if (bitpix() == Idouble)
194  {
195  ImageExt<double>& extimage = dynamic_cast<ImageExt<double>&>(*this);
196  double nulVal(0);
197  if (nulValue) nulVal = static_cast<double>(*nulValue);
198  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
199  }
200  else if (bitpix() == Ibyte)
201  {
202  ImageExt<unsigned char>& extimage
203  = dynamic_cast<ImageExt<unsigned char>&>(*this);
204  unsigned char nulVal(0);
205  if (nulValue) nulVal = static_cast<unsigned char>(*nulValue);
206  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
207  }
208  else if (bitpix() == Ilong)
209  {
210  if ( zero() == ULBASE && scale() == 1)
211  {
212  ImageExt<unsigned INT32BIT>& extimage
213  = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
214  unsigned INT32BIT nulVal(0);
215  if (nulValue)
216  nulVal = static_cast<unsigned INT32BIT>(*nulValue);
217  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
218  }
219  else
220  {
221  ImageExt<INT32BIT>& extimage = dynamic_cast<ImageExt<INT32BIT>&>(*this);
222  INT32BIT nulVal(0);
223  if (nulValue) nulVal = static_cast<INT32BIT>(*nulValue);
224  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
225  }
226  }
227  else if (bitpix() == Ilonglong)
228  {
229  ImageExt<LONGLONG>& extimage
230  = dynamic_cast<ImageExt<LONGLONG>&>(*this);
231  LONGLONG nulVal(0);
232  if (nulValue) nulVal = static_cast<LONGLONG>(*nulValue);
233  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
234  }
235  else if (bitpix() == Ishort)
236  {
237  if ( zero() == USBASE && scale() == 1)
238  {
239  ImageExt<unsigned short>& extimage
240  = dynamic_cast<ImageExt<unsigned short>&>(*this);
241  unsigned short nulVal(0);
242  if (nulValue) nulVal
243  = static_cast<unsigned short>(*nulValue);
244  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
245  }
246  else
247  {
248  ImageExt<short>& extimage
249  = dynamic_cast<ImageExt<short>&>(*this);
250  short nulVal(0);
251  if (nulValue) nulVal = static_cast<short>(*nulValue);
252  FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
253  }
254  }
255  else
256  {
257  throw CCfits::FitsFatal(" casting image types ");
258  }
259  }
260  }
261 
262  template<typename S>
263  void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex,
264  const std::vector<long>& lastVertex,
265  const std::vector<long>& stride)
266  {
267  makeThisCurrent();
268  read(image, firstVertex,lastVertex,stride,static_cast<S*>(0));
269  }
270 
271  template <typename S>
272  void ExtHDU::write(long first,long nElements,const std::valarray<S>& data,S* nulValue)
273  {
274  // throw if we called image read/write operations on a table.
275  makeThisCurrent();
276  if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
277  {
278  extimage->writeImage(first,nElements,data,nulValue);
279  }
280  else
281  {
282  if (bitpix() == Ifloat)
283  {
284  std::valarray<float> __tmp;
285  ImageExt<float>& imageExt = dynamic_cast<ImageExt<float>&>(*this);
286  FITSUtil::fill(__tmp,data);
287  float* pfNullValue = 0;
288  float fNullValue = 0.0;
289  if (nulValue)
290  {
291  fNullValue = static_cast<float>(*nulValue);
292  pfNullValue = &fNullValue;
293  }
294  imageExt.writeImage(first,nElements,__tmp, pfNullValue);
295  }
296  else if (bitpix() == Idouble)
297  {
298  std::valarray<double> __tmp;
299  ImageExt<double>& imageExt
300  = dynamic_cast<ImageExt<double>&>(*this);
301  FITSUtil::fill(__tmp,data);
302  double* pdNullValue = 0;
303  double dNullValue = 0.0;
304  if (nulValue)
305  {
306  dNullValue = static_cast<double>(*nulValue);
307  pdNullValue = &dNullValue;
308  }
309  imageExt.writeImage(first,nElements,__tmp,pdNullValue);
310  }
311  else if (bitpix() == Ibyte)
312  {
313  ImageExt<unsigned char>& imageExt
314  = dynamic_cast<ImageExt<unsigned char>&>(*this);
315  std::valarray<unsigned char> __tmp;
316  FITSUtil::fill(__tmp,data);
317  unsigned char *pbNull=0;
318  unsigned char bNull=0;
319  if (nulValue)
320  {
321  bNull = static_cast<unsigned char>(*nulValue);
322  pbNull = &bNull;
323  }
324  imageExt.writeImage(first,nElements,__tmp, pbNull);
325 
326  }
327  else if (bitpix() == Ilong)
328  {
329  if ( zero() == ULBASE && scale() == 1)
330  {
331  ImageExt<unsigned INT32BIT>& imageExt
332  = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
333  std::valarray<unsigned INT32BIT> __tmp;
334 
335  FITSUtil::fill(__tmp,data);
336  unsigned INT32BIT *plNull=0;
337  unsigned INT32BIT lNull=0;
338  if (nulValue)
339  {
340  lNull = static_cast<unsigned INT32BIT>(*nulValue);
341  plNull = &lNull;
342  }
343  imageExt.writeImage(first,nElements,__tmp,plNull);
344  }
345  else
346  {
347  ImageExt<INT32BIT>& imageExt
348  = dynamic_cast<ImageExt<INT32BIT>&>(*this);
349  std::valarray<INT32BIT> __tmp;
350  FITSUtil::fill(__tmp,data);
351  INT32BIT *plNull=0;
352  INT32BIT lNull=0;
353  if (nulValue)
354  {
355  lNull = static_cast<INT32BIT>(*nulValue);
356  plNull = &lNull;
357  }
358  imageExt.writeImage(first,nElements,__tmp,plNull);
359  }
360  }
361  else if (bitpix() == Ilonglong)
362  {
363  ImageExt<LONGLONG>& imageExt
364  = dynamic_cast<ImageExt<LONGLONG>&>(*this);
365  std::valarray<LONGLONG> __tmp;
366  FITSUtil::fill(__tmp,data);
367  LONGLONG *pllNull=0;
368  LONGLONG llNull=0;
369  if (nulValue)
370  {
371  llNull = static_cast<LONGLONG>(*nulValue);
372  pllNull = &llNull;
373  }
374  imageExt.writeImage(first,nElements,__tmp, pllNull);
375 
376  }
377  else if (bitpix() == Ishort)
378  {
379  if ( zero() == USBASE && scale() == 1)
380  {
381  ImageExt<unsigned short>& imageExt
382  = dynamic_cast<ImageExt<unsigned short>&>(*this);
383  std::valarray<unsigned short> __tmp;
384  FITSUtil::fill(__tmp,data);
385  unsigned short *psNull=0;
386  unsigned short sNull=0;
387  if (nulValue)
388  {
389  sNull = static_cast<unsigned short>(*nulValue);
390  psNull = &sNull;
391  }
392  imageExt.writeImage(first,nElements,__tmp,psNull);
393  }
394  else
395  {
396  ImageExt<short>& imageExt
397  = dynamic_cast<ImageExt<short>&>(*this);
398  std::valarray<short> __tmp;
399  FITSUtil::fill(__tmp,data);
400  short *psNull=0;
401  short sNull=0;
402  if (nulValue)
403  {
404  sNull = static_cast<short>(*nulValue);
405  psNull = &sNull;
406  }
407  imageExt.writeImage(first,nElements,__tmp,psNull);
408  }
409  }
410  else
411  {
412  FITSUtil::MatchType<S> errType;
413  throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
414  }
415  }
416  }
417 
418  template <typename S>
419  void ExtHDU::write(long first,
420  long nElements,const std::valarray<S>& data)
421  {
422  write(first, nElements, data, static_cast<S*>(0));
423  }
424 
425  template <typename S>
426  void ExtHDU::write(const std::vector<long>& first,
427  long nElements,
428  const std::valarray<S>& data,
429  S* nulValue)
430  {
431  // throw if we called image read/write operations on a table.
432  makeThisCurrent();
433  size_t n(first.size());
434  long firstElement(0);
435  long dimSize(1);
436  for (long i = 0; i < n; ++i)
437  {
438  firstElement += ((first[i] - 1)*dimSize);
439  dimSize *=naxes(i);
440  }
441  ++firstElement;
442 
443  write(firstElement,nElements,data,nulValue);
444  }
445 
446  template <typename S>
447  void ExtHDU::write(const std::vector<long>& first,
448  long nElements,
449  const std::valarray<S>& data)
450  {
451  // throw if we called image read/write operations on a table.
452  makeThisCurrent();
453  size_t n(first.size());
454  long firstElement(0);
455  long dimSize(1);
456  for (long i = 0; i < n; ++i)
457  {
458 
459  firstElement += ((first[i] - 1)*dimSize);
460  dimSize *=naxes(i);
461  }
462  ++firstElement;
463 
464  write(firstElement,nElements,data);
465  }
466 
467 
468  template <typename S>
469  void ExtHDU::write(const std::vector<long>& firstVertex,
470  const std::vector<long>& lastVertex,
471  const std::valarray<S>& data)
472  {
473  makeThisCurrent();
474  if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
475  {
476  extimage->writeImage(firstVertex,lastVertex,data);
477  }
478  else
479  {
480  // write input type S to Image type...
481 
482  if (bitpix() == Ifloat)
483  {
484  ImageExt<float>& extimage = dynamic_cast<ImageExt<float>&>(*this);
485  size_t n(data.size());
486  std::valarray<float> __tmp(n);
487  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
488  extimage.writeImage(firstVertex,lastVertex,__tmp);
489 
490  }
491  else if (bitpix() == Idouble)
492  {
493  ImageExt<double>& extimage
494  = dynamic_cast<ImageExt<double>&>(*this);
495  size_t n(data.size());
496  std::valarray<double> __tmp(n);
497  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
498  extimage.writeImage(firstVertex,lastVertex,__tmp);
499  }
500  else if (bitpix() == Ibyte)
501  {
502  ImageExt<unsigned char>& extimage
503  = dynamic_cast<ImageExt<unsigned char>&>(*this);
504  size_t n(data.size());
505  std::valarray<unsigned char> __tmp(n);
506  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
507  extimage.writeImage(firstVertex,lastVertex,__tmp);
508  }
509  else if (bitpix() == Ilong)
510  {
511  if ( zero() == ULBASE && scale() == 1)
512  {
513  ImageExt<unsigned INT32BIT>& extimage
514  = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
515  size_t n(data.size());
516  std::valarray<unsigned INT32BIT> __tmp(n);
517  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
518  extimage.writeImage(firstVertex,lastVertex,__tmp);
519  }
520  else
521  {
522  ImageExt<INT32BIT>& extimage
523  = dynamic_cast<ImageExt<INT32BIT>&>(*this);
524  size_t n(data.size());
525  std::valarray<INT32BIT> __tmp(n);
526  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
527  extimage.writeImage(firstVertex,lastVertex,__tmp);
528  }
529  }
530  else if (bitpix() == Ilonglong)
531  {
532  ImageExt<LONGLONG>& extimage
533  = dynamic_cast<ImageExt<LONGLONG>&>(*this);
534  size_t n(data.size());
535  std::valarray<LONGLONG> __tmp(n);
536  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
537  extimage.writeImage(firstVertex,lastVertex,__tmp);
538  }
539  else if (bitpix() == Ishort)
540  {
541  if ( zero() == USBASE && scale() == 1)
542  {
543  ImageExt<unsigned short>& extimage
544  = dynamic_cast<ImageExt<unsigned short>&>(*this);
545  size_t n(data.size());
546  std::valarray<unsigned short> __tmp(n);
547  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
548  extimage.writeImage(firstVertex,lastVertex,__tmp);
549  }
550  else
551  {
552  ImageExt<short>& extimage
553  = dynamic_cast<ImageExt<short>&>(*this);
554  size_t n(data.size());
555  std::valarray<short> __tmp(n);
556  for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
557  extimage.writeImage(firstVertex,lastVertex,__tmp);
558  }
559  }
560  else
561  {
562  FITSUtil::MatchType<S> errType;
563  throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
564  }
565  }
566  }
567 
568 
569 } //namespace CCfits
570 
571 #endif
void read(std::valarray< S > &image)
Read image data into container.
Definition: ExtHDUT.h:18
virtual void makeThisCurrent() const
move the fitsfile pointer to this current HDU.
Definition: ExtHDU.cxx:208
void write(const std::vector< long > &first, long nElements, const std::valarray< S > &data, S *nullValue)
Write a set of pixels to an image extension with the first pixel specified by an n-tuple,...
Definition: ExtHDUT.h:426
exception thrown by MatchType if it encounters data type incompatible with cfitsio.
Definition: FITSUtil.h:637
[potential] base class for exceptions to be thrown on internal library error.
Definition: FitsError.h:127
std::vector< long > & naxes()
return the HDU data axis array.
Definition: HDU.h:1086
virtual double scale() const
return the BSCALE keyword value
Definition: HDU.h:1008
long bitpix() const
return the data type keyword.
Definition: HDU.h:998
virtual double zero() const
return the BZERO keyword value
Definition: HDU.h:1018
Namespace enclosing all CCfits classes and globals definitions.
Definition: AsciiTable.cxx:26
function object that returns the FITS ValueType corresponding to an input intrinsic type
Definition: FITSUtil.h:506