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