00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef PHDUT_H
00011 #define PHDUT_H
00012 #include "PrimaryHDU.h"
00013 #include <iostream>
00014 #include <exception>
00015
00016 namespace CCfits
00017 {
00018
00019 template <typename S>
00020 void PHDU::read (std::valarray<S>& image)
00021 {
00022 long init(1);
00023 long nElements(std::accumulate(naxes().begin(),naxes().end(),init,
00024 std::multiplies<long>()));
00025
00026 read(image,1,nElements,static_cast<S*>(0));
00027 }
00028
00029
00030 template <typename S>
00031 void PHDU::read (std::valarray<S>& image, long first,long nElements)
00032 {
00033 read(image, first,nElements,static_cast<S*>(0));
00034 }
00035
00036 template <typename S>
00037 void PHDU::read (std::valarray<S>& image, long first, long nElements, S* nullValue)
00038 {
00039 makeThisCurrent();
00040 if ( PrimaryHDU<S>* phdu = dynamic_cast<PrimaryHDU<S>*>(this) )
00041 {
00042
00043 const std::valarray<S>& __tmp = phdu->readImage(first,nElements,nullValue);
00044 image.resize(__tmp.size());
00045 image = __tmp;
00046 }
00047 else
00048 {
00049 if (bitpix() == Ifloat)
00050 {
00051 PrimaryHDU<float>& phdu
00052 = dynamic_cast<PrimaryHDU<float>&>(*this);
00053 float nulVal(0);
00054 if (nullValue) nulVal = static_cast<float>(*nullValue);
00055 FITSUtil::fill(image,phdu.readImage(first,nElements,&nulVal));
00056
00057 }
00058 else if (bitpix() == Idouble)
00059 {
00060 PrimaryHDU<double>& phdu
00061 = dynamic_cast<PrimaryHDU<double>&>(*this);
00062 double nulVal(0);
00063 if (nullValue) nulVal = static_cast<double>(*nullValue);
00064 FITSUtil::fill(image,phdu.readImage(first,nElements,&nulVal));
00065
00066 }
00067 else if (bitpix() == Ibyte)
00068 {
00069 PrimaryHDU<unsigned char>& phdu
00070 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00071 unsigned char nulVal(0);
00072 if (nullValue) nulVal = static_cast<unsigned char>(*nullValue);
00073 FITSUtil::fill(image,phdu.readImage(first,nElements,&nulVal));
00074 }
00075 else if (bitpix() == Ilong)
00076 {
00077 if ( zero() == ULBASE && scale() == 1)
00078 {
00079 PrimaryHDU<unsigned long>& phdu
00080 = dynamic_cast<PrimaryHDU<unsigned long>&>(*this);
00081 unsigned long nulVal(0);
00082 if (nullValue) nulVal
00083 = static_cast<unsigned long>(*nullValue);
00084 FITSUtil::fill(image,
00085 phdu.readImage(first,nElements,&nulVal));
00086 }
00087 else
00088 {
00089 PrimaryHDU<long>& phdu
00090 = dynamic_cast<PrimaryHDU<long>&>(*this);
00091 long nulVal(0);
00092 if (nullValue) nulVal = static_cast<long>(*nullValue);
00093 FITSUtil::fill(image,
00094 phdu.readImage(first,nElements,&nulVal));
00095 }
00096 }
00097 else if (bitpix() == Ishort)
00098 {
00099 if ( zero() == USBASE && scale() == 1)
00100 {
00101 PrimaryHDU<unsigned short>& phdu
00102 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00103 unsigned short nulVal(0);
00104 if (nullValue) nulVal
00105 = static_cast<unsigned short>(*nullValue);
00106 FITSUtil::fill(image,
00107 phdu.readImage(first,nElements,&nulVal));
00108 }
00109 else
00110 {
00111 PrimaryHDU<short>& phdu
00112 = dynamic_cast<PrimaryHDU<short>&>(*this);
00113 short nulVal(0);
00114 if (nullValue) nulVal = static_cast<short>(*nullValue);
00115 FITSUtil::fill(image,
00116 phdu.readImage(first,nElements,&nulVal));
00117
00118 }
00119 }
00120 else
00121 {
00122 throw CCfits::FitsFatal(" casting image types ");
00123 }
00124 }
00125
00126 }
00127
00128 template<typename S>
00129 void PHDU::read (std::valarray<S>& image, const std::vector<long>& first,
00130 long nElements,
00131 S* nullValue)
00132 {
00133 makeThisCurrent();
00134 long firstElement(0);
00135 long dimSize(1);
00136 std::vector<long> inputDimensions(naxis(),1);
00137 size_t sNaxis = static_cast<size_t>(naxis());
00138 size_t n(std::min(sNaxis,first.size()));
00139 std::copy(&first[0],&first[0]+n,&inputDimensions[0]);
00140 for (long i = 0; i < naxis(); ++i)
00141 {
00142
00143 firstElement += ((inputDimensions[i] - 1)*dimSize);
00144 dimSize *=naxes(i);
00145 }
00146 ++firstElement;
00147
00148
00149 read(image, firstElement,nElements,nullValue);
00150
00151
00152
00153 }
00154
00155 template<typename S>
00156 void PHDU::read (std::valarray<S>& image, const std::vector<long>& first,
00157 long nElements)
00158 {
00159 read(image, first,nElements,static_cast<S*>(0));
00160
00161 }
00162
00163 template<typename S>
00164 void PHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex,
00165 const std::vector<long>& lastVertex,
00166 const std::vector<long>& stride,
00167 S* nullValue)
00168 {
00169 makeThisCurrent();
00170 if (PrimaryHDU<S>* phdu = dynamic_cast<PrimaryHDU<S>*>(this))
00171 {
00172 const std::valarray<S>& __tmp
00173 = phdu->readImage(firstVertex,lastVertex,stride,nullValue);
00174 image.resize(__tmp.size());
00175 image = __tmp;
00176 }
00177 else
00178 {
00179
00180 if (bitpix() == Ifloat)
00181 {
00182 float nulVal(0);
00183 if (nullValue) nulVal = static_cast<float>(*nullValue);
00184 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this);
00185 FITSUtil::fill(image,
00186 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00187 }
00188 else if (bitpix() == Idouble)
00189 {
00190 PrimaryHDU<double>& phdu = dynamic_cast<PrimaryHDU<double>&>(*this);
00191 double nulVal(0);
00192 if (nullValue) nulVal = static_cast<double>(*nullValue);
00193 FITSUtil::fill(image,
00194 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00195 }
00196 else if (bitpix() == Ibyte)
00197 {
00198 PrimaryHDU<unsigned char>& phdu
00199 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00200 unsigned char nulVal(0);
00201 if (nullValue) nulVal = static_cast<unsigned char>(*nullValue);
00202 FITSUtil::fill(image,
00203 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00204 }
00205 else if (bitpix() == Ilong)
00206 {
00207 if ( zero() == ULBASE && scale() == 1)
00208 {
00209 PrimaryHDU<unsigned long>& phdu
00210 = dynamic_cast<PrimaryHDU<unsigned long>&>(*this);
00211 unsigned long nulVal(0);
00212 if (nullValue) nulVal
00213 = static_cast<unsigned long>(*nullValue);
00214 FITSUtil::fill(image,
00215 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00216 }
00217 else
00218 {
00219 PrimaryHDU<long>& phdu
00220 = dynamic_cast<PrimaryHDU<long>&>(*this);
00221 long nulVal(0);
00222 if (nullValue) nulVal = static_cast<long>(*nullValue);
00223 FITSUtil::fill(image,
00224 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00225 }
00226 }
00227 else if (bitpix() == Ishort)
00228 {
00229 if ( zero() == USBASE && scale() == 1)
00230 {
00231 PrimaryHDU<unsigned short>& phdu
00232 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00233 unsigned short nulVal(0);
00234 if (nullValue) nulVal
00235 = static_cast<unsigned short>(*nullValue);
00236 FITSUtil::fill(image,
00237 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00238 }
00239 else
00240 {
00241 PrimaryHDU<short>& phdu
00242 = dynamic_cast<PrimaryHDU<short>&>(*this);
00243 short nulVal(0);
00244 if (nullValue) nulVal = static_cast<short>(*nullValue);
00245 FITSUtil::fill(image,
00246 phdu.readImage(firstVertex,lastVertex,stride,&nulVal));
00247 }
00248 }
00249 else
00250 {
00251 throw CCfits::FitsFatal(" casting image types ");
00252 }
00253 }
00254 }
00255
00256 template<typename S>
00257 void PHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex,
00258 const std::vector<long>& lastVertex,
00259 const std::vector<long>& stride)
00260 {
00261 read(image, firstVertex,lastVertex,stride,static_cast<S*>(0));
00262 }
00263
00264 template <typename S>
00265 void PHDU::write(long first,
00266 long nElements,
00267 const std::valarray<S>& data,
00268 S* nullValue)
00269 {
00270
00271
00272
00273 makeThisCurrent();
00274 if (PrimaryHDU<S>* image = dynamic_cast<PrimaryHDU<S>*>(this))
00275 {
00276 image->writeImage(first,nElements,data,nullValue);
00277 }
00278 else
00279 {
00280 if (bitpix() == Ifloat)
00281 {
00282 std::valarray<float> __tmp;
00283 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this);
00284 FITSUtil::fill(__tmp,data);
00285 float* pfNullValue = 0;
00286 float fNullValue = 0.0;
00287 if (nullValue)
00288 {
00289 fNullValue = static_cast<float>(*nullValue);
00290 pfNullValue = &fNullValue;
00291 }
00292 phdu.writeImage(first,nElements,__tmp, pfNullValue);
00293 }
00294 else if (bitpix() == Idouble)
00295 {
00296 std::valarray<double> __tmp;
00297 PrimaryHDU<double>& phdu
00298 = dynamic_cast<PrimaryHDU<double>&>(*this);
00299 FITSUtil::fill(__tmp,data);
00300 double* pdNullValue = 0;
00301 double dNullValue = 0.0;
00302 if (nullValue)
00303 {
00304 dNullValue = static_cast<double>(*nullValue);
00305 pdNullValue = &dNullValue;
00306 }
00307 phdu.writeImage(first,nElements,__tmp, pdNullValue);
00308 }
00309 else if (bitpix() == Ibyte)
00310 {
00311 PrimaryHDU<unsigned char>& phdu
00312 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00313 std::valarray<unsigned char> __tmp;
00314 unsigned char blankVal(0);
00315 try
00316 {
00317 readKey("BLANK",blankVal);
00318 std::valarray<S> copyData(data);
00319 std::replace(©Data[0],©Data[0]+data.size(),
00320 static_cast<unsigned char>(*nullValue),blankVal);
00321
00322 FITSUtil::fill(__tmp,copyData);
00323
00324 phdu.writeImage(first,nElements,__tmp); }
00325 catch (HDU::NoSuchKeyword)
00326 {
00327 throw NoNullValue("Primary");
00328 }
00329
00330 }
00331 else if (bitpix() == Ilong)
00332 {
00333 if ( zero() == ULBASE && scale() == 1)
00334 {
00335 PrimaryHDU<unsigned long>& phdu
00336 = dynamic_cast<PrimaryHDU<unsigned long>&>(*this);
00337 std::valarray<unsigned long> __tmp;
00338 unsigned long blankVal(0);
00339 try
00340 {
00341 readKey("BLANK",blankVal);
00342 std::valarray<S> copyData(data);
00343 std::replace(©Data[0],©Data[0]+data.size(),
00344 static_cast<unsigned long>(*nullValue),blankVal);
00345 FITSUtil::fill(__tmp,copyData);
00346 phdu.writeImage(first,nElements,__tmp);
00347 }
00348 catch (HDU::NoSuchKeyword)
00349 {
00350 throw NoNullValue("Primary");
00351 }
00352 }
00353 else
00354 {
00355 PrimaryHDU<long>& phdu
00356 = dynamic_cast<PrimaryHDU<long>&>(*this);
00357 std::valarray<long> __tmp;
00358 long blankVal(0);
00359 try
00360 {
00361 readKey("BLANK",blankVal);
00362 std::valarray<S> copyData(data);
00363 std::replace(©Data[0],©Data[0]+data.size(),
00364 static_cast<long>(*nullValue),blankVal);
00365
00366 FITSUtil::fill(__tmp,copyData);
00367 phdu.writeImage(first,nElements,__tmp);
00368 }
00369 catch (HDU::NoSuchKeyword)
00370 {
00371 throw NoNullValue("Primary");
00372 }
00373 }
00374 }
00375 else if (bitpix() == Ishort)
00376 {
00377 if ( zero() == USBASE && scale() == 1)
00378 {
00379 PrimaryHDU<unsigned short>& phdu
00380 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00381 std::valarray<unsigned short> __tmp;
00382 unsigned short blankVal(0);
00383 try
00384 {
00385 readKey("BLANK",blankVal);
00386 std::valarray<S> copyData(data);
00387 std::replace(©Data[0],©Data[0]+data.size(),
00388 static_cast<unsigned short>(*nullValue),blankVal);
00389 FITSUtil::fill(__tmp,copyData);
00390 phdu.writeImage(first,nElements,__tmp);
00391 }
00392 catch (HDU::NoSuchKeyword)
00393 {
00394 throw NoNullValue("Primary");
00395 }
00396 }
00397 else
00398 {
00399 PrimaryHDU<short>& phdu
00400 = dynamic_cast<PrimaryHDU<short>&>(*this);
00401 std::valarray<short> __tmp;
00402 short blankVal(0);
00403 try
00404 {
00405 readKey("BLANK",blankVal);
00406 std::valarray<S> copyData(data);
00407 std::replace(©Data[0],©Data[0]+data.size(),
00408 static_cast<short>(*nullValue),blankVal);
00409
00410 FITSUtil::fill(__tmp,copyData);
00411 phdu.writeImage(first,nElements,__tmp);
00412 }
00413 catch (HDU::NoSuchKeyword)
00414 {
00415 throw NoNullValue("Primary");
00416 }
00417 }
00418 }
00419 else
00420 {
00421 FITSUtil::MatchType<S> errType;
00422 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00423 }
00424 }
00425 }
00426
00427
00428 template <typename S>
00429 void PHDU::write(long first,
00430 long nElements,
00431 const std::valarray<S>& data)
00432 {
00433
00434
00435
00436 makeThisCurrent();
00437 if ( PrimaryHDU<S>* image = dynamic_cast<PrimaryHDU<S>*>(this) )
00438 {
00439 image->writeImage(first,nElements,data);
00440 }
00441 else
00442 {
00443 if (bitpix() == Ifloat)
00444 {
00445 std::valarray<float> __tmp;
00446 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this);
00447 FITSUtil::fill(__tmp,data);
00448 phdu.writeImage(first,nElements,__tmp);
00449 }
00450 else if (bitpix() == Idouble)
00451 {
00452 std::valarray<double> __tmp;
00453 PrimaryHDU<double>& phdu
00454 = dynamic_cast<PrimaryHDU<double>&>(*this);
00455 FITSUtil::fill(__tmp,data);
00456 phdu.writeImage(first,nElements,__tmp);
00457 }
00458 else if (bitpix() == Ibyte)
00459 {
00460 PrimaryHDU<unsigned char>& phdu
00461 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00462 std::valarray<unsigned char> __tmp;
00463 FITSUtil::fill(__tmp,data);
00464 phdu.writeImage(first,nElements,__tmp);
00465 }
00466 else if (bitpix() == Ilong)
00467 {
00468 if ( zero() == ULBASE && scale() == 1)
00469 {
00470 PrimaryHDU<unsigned long>& phdu
00471 = dynamic_cast<PrimaryHDU<unsigned long>&>(*this);
00472 std::valarray<unsigned long> __tmp;
00473 FITSUtil::fill(__tmp,data);
00474 phdu.writeImage(first,nElements,__tmp);
00475 }
00476 else
00477 {
00478 PrimaryHDU<long>& phdu
00479 = dynamic_cast<PrimaryHDU<long>&>(*this);
00480 std::valarray<long> __tmp;
00481 FITSUtil::fill(__tmp,data);
00482 phdu.writeImage(first,nElements,__tmp);
00483 }
00484 }
00485 else if (bitpix() == Ishort)
00486 {
00487 if ( zero() == USBASE && scale() == 1)
00488 {
00489 PrimaryHDU<unsigned short>& phdu
00490 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00491 std::valarray<unsigned short> __tmp;
00492 FITSUtil::fill(__tmp,data);
00493 phdu.writeImage(first,nElements,__tmp);
00494 }
00495 else
00496 {
00497 PrimaryHDU<short>& phdu
00498 = dynamic_cast<PrimaryHDU<short>&>(*this);
00499 std::valarray<short> __tmp;
00500 FITSUtil::fill(__tmp,data);
00501 phdu.writeImage(first,nElements,__tmp);
00502 }
00503 }
00504 else
00505 {
00506 FITSUtil::MatchType<S> errType;
00507 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00508 }
00509 }
00510 }
00511
00512 template <typename S>
00513 void PHDU::write(const std::vector<long>& first,
00514 long nElements,
00515 const std::valarray<S>& data,
00516 S* nullValue)
00517 {
00518 makeThisCurrent();
00519 size_t n(first.size());
00520 long firstElement(0);
00521 long dimSize(1);
00522 for (long i = 0; i < first.size(); ++i)
00523 {
00524 firstElement += ((first[i] - 1)*dimSize);
00525 dimSize *=naxes(i);
00526 }
00527 ++firstElement;
00528
00529 write(firstElement,nElements,data,nullValue);
00530 }
00531
00532 template <typename S>
00533 void PHDU::write(const std::vector<long>& first,
00534 long nElements,
00535 const std::valarray<S>& data)
00536 {
00537 makeThisCurrent();
00538 size_t n(first.size());
00539 long firstElement(0);
00540 long dimSize(1);
00541 for (long i = 0; i < first.size(); ++i)
00542 {
00543
00544 firstElement += ((first[i] - 1)*dimSize);
00545 dimSize *=naxes(i);
00546 }
00547 ++firstElement;
00548
00549 write(firstElement,nElements,data);
00550 }
00551
00552
00553 template <typename S>
00554 void PHDU::write(const std::vector<long>& firstVertex,
00555 const std::vector<long>& lastVertex,
00556 const std::vector<long>& stride,
00557 const std::valarray<S>& data)
00558 {
00559 makeThisCurrent();
00560 try
00561 {
00562 PrimaryHDU<S>& image = dynamic_cast<PrimaryHDU<S>&>(*this);
00563 image.writeImage(firstVertex,lastVertex,stride,data);
00564 }
00565 catch (std::bad_cast)
00566 {
00567
00568
00569 if (bitpix() == Ifloat)
00570 {
00571 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this);
00572 size_t n(data.size());
00573 std::valarray<float> __tmp(n);
00574 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00575 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00576
00577 }
00578 else if (bitpix() == Idouble)
00579 {
00580 PrimaryHDU<double>& phdu
00581 = dynamic_cast<PrimaryHDU<double>&>(*this);
00582 size_t n(data.size());
00583 std::valarray<double> __tmp(n);
00584 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00585 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00586 }
00587 else if (bitpix() == Ibyte)
00588 {
00589 PrimaryHDU<unsigned char>& phdu
00590 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00591 size_t n(data.size());
00592 std::valarray<unsigned char> __tmp(n);
00593 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00594 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00595 }
00596 else if (bitpix() == Ilong)
00597 {
00598 if ( zero() == ULBASE && scale() == 1)
00599 {
00600 PrimaryHDU<unsigned long>& phdu
00601 = dynamic_cast<PrimaryHDU<unsigned long>&>(*this);
00602 size_t n(data.size());
00603 std::valarray<unsigned long> __tmp(n);
00604 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00605 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00606
00607 }
00608 else
00609 {
00610 PrimaryHDU<long>& phdu
00611 = dynamic_cast<PrimaryHDU<long>&>(*this);
00612 size_t n(data.size());
00613 std::valarray<long> __tmp(n);
00614 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00615 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00616 }
00617 }
00618 else if (bitpix() == Ishort)
00619 {
00620 if ( zero() == USBASE && scale() == 1)
00621 {
00622 PrimaryHDU<unsigned short>& phdu
00623 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00624 size_t n(data.size());
00625 std::valarray<unsigned short> __tmp(n);
00626 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00627 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00628
00629 }
00630 else
00631 {
00632 PrimaryHDU<short>& phdu
00633 = dynamic_cast<PrimaryHDU<short>&>(*this);
00634 size_t n(data.size());
00635 std::valarray<short> __tmp(n);
00636 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00637 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00638 }
00639 }
00640 else
00641 {
00642 FITSUtil::MatchType<S> errType;
00643 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00644 }
00645 }
00646 }
00647
00648
00649
00650
00651 }
00652 #endif