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