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