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