12 #include "PrimaryHDU.h"
20 void PHDU::read (std::valarray<S>& image)
23 long nElements(std::accumulate(
naxes().begin(),
naxes().end(),init,
24 std::multiplies<long>()));
26 read(image,1,nElements,static_cast<S*>(0));
31 void PHDU::read (std::valarray<S>& image,
long first,
long nElements)
33 read(image, first,nElements,static_cast<S*>(0));
37 void PHDU::read (std::valarray<S>& image,
long first,
long nElements, S* nullValue)
40 if ( PrimaryHDU<S>* phdu =
dynamic_cast<PrimaryHDU<S>*
>(
this) )
43 const std::valarray<S>& __tmp = phdu->readImage(first,nElements,nullValue);
44 image.resize(__tmp.size());
51 PrimaryHDU<float>& phdu
52 =
dynamic_cast<PrimaryHDU<float>&
>(*this);
54 if (nullValue) nulVal =
static_cast<float>(*nullValue);
55 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal));
58 else if (
bitpix() == Idouble)
60 PrimaryHDU<double>& phdu
61 =
dynamic_cast<PrimaryHDU<double>&
>(*this);
63 if (nullValue) nulVal =
static_cast<double>(*nullValue);
64 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal));
67 else if (
bitpix() == Ibyte)
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));
75 else if (
bitpix() == Ilong)
79 PrimaryHDU<unsigned INT32BIT>& phdu
80 =
dynamic_cast<PrimaryHDU<unsigned INT32BIT>&
>(*this);
81 unsigned INT32BIT nulVal(0);
83 =
static_cast<unsigned INT32BIT
>(*nullValue);
84 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal));
88 PrimaryHDU<INT32BIT>& phdu
89 =
dynamic_cast<PrimaryHDU<INT32BIT>&
>(*this);
91 if (nullValue) nulVal =
static_cast<INT32BIT
>(*nullValue);
92 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal));
95 else if (
bitpix() == Ilonglong)
97 PrimaryHDU<LONGLONG>& phdu =
dynamic_cast<PrimaryHDU<LONGLONG>&
>(*this);
99 if (nullValue) nulVal =
static_cast<LONGLONG
>(*nullValue);
100 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal));
102 else if (
bitpix() == Ishort)
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));
115 PrimaryHDU<short>& phdu
116 =
dynamic_cast<PrimaryHDU<short>&
>(*this);
118 if (nullValue) nulVal =
static_cast<short>(*nullValue);
119 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal));
132 void PHDU::read (std::valarray<S>& image,
const std::vector<long>& first,
137 long firstElement(0);
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)
146 firstElement += ((inputDimensions[i] - 1)*dimSize);
152 read(image, firstElement,nElements,nullValue);
159 void PHDU::read (std::valarray<S>& image,
const std::vector<long>& first,
162 read(image, first,nElements,static_cast<S*>(0));
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,
173 if (PrimaryHDU<S>* phdu =
dynamic_cast<PrimaryHDU<S>*
>(
this))
175 const std::valarray<S>& __tmp =
176 phdu->readImage(firstVertex,lastVertex,stride,nullValue);
177 image.resize(__tmp.size());
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));
190 else if (
bitpix() == Idouble)
192 PrimaryHDU<double>& phdu =
dynamic_cast<PrimaryHDU<double>&
>(*this);
194 if (nullValue) nulVal =
static_cast<double>(*nullValue);
195 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
197 else if (
bitpix() == Ibyte)
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));
205 else if (
bitpix() == Ilong)
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));
218 PrimaryHDU<INT32BIT>& phdu
219 =
dynamic_cast<PrimaryHDU<INT32BIT>&
>(*this);
221 if (nullValue) nulVal =
static_cast<INT32BIT
>(*nullValue);
222 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
225 else if (
bitpix() == Ilonglong)
227 PrimaryHDU<LONGLONG>& phdu
228 =
dynamic_cast<PrimaryHDU<LONGLONG>&
>(*this);
230 if (nullValue) nulVal =
static_cast<LONGLONG
>(*nullValue);
231 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
233 else if (
bitpix() == Ishort)
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));
246 PrimaryHDU<short>& phdu
247 =
dynamic_cast<PrimaryHDU<short>&
>(*this);
249 if (nullValue) nulVal =
static_cast<short>(*nullValue);
250 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
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)
265 read(image, firstVertex,lastVertex,stride,static_cast<S*>(0));
268 template <
typename S>
271 const std::valarray<S>& data,
276 if (PrimaryHDU<S>* image =
dynamic_cast<PrimaryHDU<S>*
>(
this))
278 image->writeImage(first,nElements,data,nullValue);
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;
291 fNullValue =
static_cast<float>(*nullValue);
292 pfNullValue = &fNullValue;
294 phdu.writeImage(first,nElements,__tmp, pfNullValue);
296 else if (
bitpix() == Idouble)
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;
306 dNullValue =
static_cast<double>(*nullValue);
307 pdNullValue = &dNullValue;
309 phdu.writeImage(first,nElements,__tmp, pdNullValue);
311 else if (
bitpix() == Ibyte)
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;
321 bNull =
static_cast<unsigned char>(*nullValue);
324 phdu.writeImage(first,nElements,__tmp, pbNull);
327 else if (
bitpix() == Ilong)
331 PrimaryHDU<unsigned INT32BIT>& phdu
332 =
dynamic_cast<PrimaryHDU<unsigned INT32BIT>&
>(*this);
333 std::valarray<unsigned INT32BIT> __tmp;
335 FITSUtil::fill(__tmp,data);
336 unsigned INT32BIT *plNull=0;
337 unsigned INT32BIT lNull=0;
340 lNull =
static_cast<unsigned INT32BIT
>(*nullValue);
343 phdu.writeImage(first,nElements,__tmp, plNull);
347 PrimaryHDU<INT32BIT>& phdu
348 =
dynamic_cast<PrimaryHDU<INT32BIT>&
>(*this);
349 std::valarray<INT32BIT> __tmp;
351 FITSUtil::fill(__tmp,data);
356 lNull =
static_cast<INT32BIT
>(*nullValue);
359 phdu.writeImage(first,nElements,__tmp, plNull);
362 else if (
bitpix() == Ilonglong)
364 PrimaryHDU<LONGLONG>& phdu
365 =
dynamic_cast<PrimaryHDU<LONGLONG>&
>(*this);
366 std::valarray<LONGLONG> __tmp;
367 FITSUtil::fill(__tmp,data);
372 llNull =
static_cast<LONGLONG
>(*nullValue);
375 phdu.writeImage(first,nElements,__tmp, pllNull);
378 else if (
bitpix() == Ishort)
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;
390 sNull =
static_cast<unsigned short>(*nullValue);
393 phdu.writeImage(first,nElements,__tmp, psNull);
397 PrimaryHDU<short>& phdu
398 =
dynamic_cast<PrimaryHDU<short>&
>(*this);
399 std::valarray<short> __tmp;
401 FITSUtil::fill(__tmp,data);
406 sNull =
static_cast<short>(*nullValue);
409 phdu.writeImage(first,nElements,__tmp,psNull);
421 template <
typename S>
424 const std::valarray<S>& data)
426 write(first, nElements, data, static_cast<S*>(0));
429 template <
typename S>
432 const std::valarray<S>& data,
436 size_t n(first.size());
437 long firstElement(0);
439 for (
long i = 0; i < n; ++i)
441 firstElement += ((first[i] - 1)*dimSize);
446 write(firstElement,nElements,data,nullValue);
449 template <
typename S>
452 const std::valarray<S>& data)
455 size_t n(first.size());
456 long firstElement(0);
458 for (
long i = 0; i < n; ++i)
461 firstElement += ((first[i] - 1)*dimSize);
466 write(firstElement,nElements,data);
470 template <
typename S>
472 const std::vector<long>& lastVertex,
473 const std::vector<long>& stride,
474 const std::valarray<S>& data)
479 PrimaryHDU<S>& image =
dynamic_cast<PrimaryHDU<S>&
>(*this);
480 image.writeImage(firstVertex,lastVertex,stride,data);
482 catch (std::bad_cast&)
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);
495 else if (
bitpix() == Idouble)
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);
504 else if (
bitpix() == Ibyte)
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);
513 else if (
bitpix() == Ilong)
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);
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);
535 else if (
bitpix() == Ilonglong)
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);
544 else if (
bitpix() == Ishort)
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);
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);
long bitpix() const
return the data type keyword.
Definition: HDU.h:998
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: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, processing undefined data.
Definition: PHDUT.h:430
virtual void makeThisCurrent() const
move the fitsfile pointer to this current HDU.
Definition: HDU.cxx:318
virtual double zero() const
return the BZERO keyword value
Definition: PHDU.cxx:160
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:1086