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