CCfits
2.5
|
00001 // Astrophysics Science Division, 00002 // NASA/ Goddard Space Flight Center 00003 // HEASARC 00004 // http://heasarc.gsfc.nasa.gov 00005 // e-mail: ccfits@legacy.gsfc.nasa.gov 00006 // 00007 // Original author: Ben Dorman 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 // proceed if cast is successful. 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 INT32BIT>& phdu 00080 = dynamic_cast<PrimaryHDU<unsigned INT32BIT>&>(*this); 00081 unsigned INT32BIT nulVal(0); 00082 if (nullValue) nulVal 00083 = static_cast<unsigned INT32BIT>(*nullValue); 00084 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); 00085 } 00086 else 00087 { 00088 PrimaryHDU<INT32BIT>& phdu 00089 = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this); 00090 INT32BIT nulVal(0); 00091 if (nullValue) nulVal = static_cast<INT32BIT>(*nullValue); 00092 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); 00093 } 00094 } 00095 else if (bitpix() == Ilonglong) 00096 { 00097 PrimaryHDU<LONGLONG>& phdu = dynamic_cast<PrimaryHDU<LONGLONG>&>(*this); 00098 LONGLONG nulVal(0); 00099 if (nullValue) nulVal = static_cast<LONGLONG>(*nullValue); 00100 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); 00101 } 00102 else if (bitpix() == Ishort) 00103 { 00104 if ( zero() == USBASE && scale() == 1) 00105 { 00106 PrimaryHDU<unsigned short>& phdu 00107 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this); 00108 unsigned short nulVal(0); 00109 if (nullValue) nulVal 00110 = static_cast<unsigned short>(*nullValue); 00111 FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); 00112 } 00113 else 00114 { 00115 PrimaryHDU<short>& phdu 00116 = dynamic_cast<PrimaryHDU<short>&>(*this); 00117 short nulVal(0); 00118 if (nullValue) nulVal = static_cast<short>(*nullValue); 00119 FITSUtil::fill(image,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[0]+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 // FITSutil::fill will take care of sizing. 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,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00189 } 00190 else if (bitpix() == Idouble) 00191 { 00192 PrimaryHDU<double>& phdu = dynamic_cast<PrimaryHDU<double>&>(*this); 00193 double nulVal(0); 00194 if (nullValue) nulVal = static_cast<double>(*nullValue); 00195 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00196 } 00197 else if (bitpix() == Ibyte) 00198 { 00199 PrimaryHDU<unsigned char>& phdu 00200 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this); 00201 unsigned char nulVal(0); 00202 if (nullValue) nulVal = static_cast<unsigned char>(*nullValue); 00203 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00204 } 00205 else if (bitpix() == Ilong) 00206 { 00207 if ( zero() == ULBASE && scale() == 1) 00208 { 00209 PrimaryHDU<unsigned INT32BIT>& phdu 00210 = dynamic_cast<PrimaryHDU<unsigned INT32BIT>&>(*this); 00211 unsigned INT32BIT nulVal(0); 00212 if (nullValue) nulVal 00213 = static_cast<unsigned INT32BIT>(*nullValue); 00214 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00215 } 00216 else 00217 { 00218 PrimaryHDU<INT32BIT>& phdu 00219 = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this); 00220 INT32BIT nulVal(0); 00221 if (nullValue) nulVal = static_cast<INT32BIT>(*nullValue); 00222 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00223 } 00224 } 00225 else if (bitpix() == Ilonglong) 00226 { 00227 PrimaryHDU<LONGLONG>& phdu 00228 = dynamic_cast<PrimaryHDU<LONGLONG>&>(*this); 00229 LONGLONG nulVal(0); 00230 if (nullValue) nulVal = static_cast<LONGLONG>(*nullValue); 00231 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00232 } 00233 else if (bitpix() == Ishort) 00234 { 00235 if ( zero() == USBASE && scale() == 1) 00236 { 00237 PrimaryHDU<unsigned short>& phdu 00238 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this); 00239 unsigned short nulVal(0); 00240 if (nullValue) nulVal 00241 = static_cast<unsigned short>(*nullValue); 00242 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00243 } 00244 else 00245 { 00246 PrimaryHDU<short>& phdu 00247 = dynamic_cast<PrimaryHDU<short>&>(*this); 00248 short nulVal(0); 00249 if (nullValue) nulVal = static_cast<short>(*nullValue); 00250 FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); 00251 } 00252 } 00253 else 00254 { 00255 throw CCfits::FitsFatal(" casting image types "); 00256 } 00257 } 00258 } 00259 00260 template<typename S> 00261 void PHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex, 00262 const std::vector<long>& lastVertex, 00263 const std::vector<long>& stride) 00264 { 00265 read(image, firstVertex,lastVertex,stride,static_cast<S*>(0)); 00266 } 00267 00268 template <typename S> 00269 void PHDU::write(long first, 00270 long nElements, 00271 const std::valarray<S>& data, 00272 S* nullValue) 00273 { 00274 00275 makeThisCurrent(); 00276 if (PrimaryHDU<S>* image = dynamic_cast<PrimaryHDU<S>*>(this)) 00277 { 00278 image->writeImage(first,nElements,data,nullValue); 00279 } 00280 else 00281 { 00282 if (bitpix() == Ifloat) 00283 { 00284 std::valarray<float> __tmp; 00285 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this); 00286 FITSUtil::fill(__tmp,data); 00287 float* pfNullValue = 0; 00288 float fNullValue = 0.0; 00289 if (nullValue) 00290 { 00291 fNullValue = static_cast<float>(*nullValue); 00292 pfNullValue = &fNullValue; 00293 } 00294 phdu.writeImage(first,nElements,__tmp, pfNullValue); 00295 } 00296 else if (bitpix() == Idouble) 00297 { 00298 std::valarray<double> __tmp; 00299 PrimaryHDU<double>& phdu 00300 = dynamic_cast<PrimaryHDU<double>&>(*this); 00301 FITSUtil::fill(__tmp,data); 00302 double* pdNullValue = 0; 00303 double dNullValue = 0.0; 00304 if (nullValue) 00305 { 00306 dNullValue = static_cast<double>(*nullValue); 00307 pdNullValue = &dNullValue; 00308 } 00309 phdu.writeImage(first,nElements,__tmp, pdNullValue); 00310 } 00311 else if (bitpix() == Ibyte) 00312 { 00313 PrimaryHDU<unsigned char>& phdu 00314 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this); 00315 std::valarray<unsigned char> __tmp; 00316 FITSUtil::fill(__tmp,data); 00317 unsigned char *pbNull=0; 00318 unsigned char bNull=0; 00319 if (nullValue) 00320 { 00321 bNull = static_cast<unsigned char>(*nullValue); 00322 pbNull = &bNull; 00323 } 00324 phdu.writeImage(first,nElements,__tmp, pbNull); 00325 00326 } 00327 else if (bitpix() == Ilong) 00328 { 00329 if ( zero() == ULBASE && scale() == 1) 00330 { 00331 PrimaryHDU<unsigned INT32BIT>& phdu 00332 = dynamic_cast<PrimaryHDU<unsigned INT32BIT>&>(*this); 00333 std::valarray<unsigned INT32BIT> __tmp; 00334 00335 FITSUtil::fill(__tmp,data); 00336 unsigned INT32BIT *plNull=0; 00337 unsigned INT32BIT lNull=0; 00338 if (nullValue) 00339 { 00340 lNull = static_cast<unsigned INT32BIT>(*nullValue); 00341 plNull = &lNull; 00342 } 00343 phdu.writeImage(first,nElements,__tmp, plNull); 00344 } 00345 else 00346 { 00347 PrimaryHDU<INT32BIT>& phdu 00348 = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this); 00349 std::valarray<INT32BIT> __tmp; 00350 00351 FITSUtil::fill(__tmp,data); 00352 INT32BIT *plNull=0; 00353 INT32BIT lNull=0; 00354 if (nullValue) 00355 { 00356 lNull = static_cast<INT32BIT>(*nullValue); 00357 plNull = &lNull; 00358 } 00359 phdu.writeImage(first,nElements,__tmp, plNull); 00360 } 00361 } 00362 else if (bitpix() == Ilonglong) 00363 { 00364 PrimaryHDU<LONGLONG>& phdu 00365 = dynamic_cast<PrimaryHDU<LONGLONG>&>(*this); 00366 std::valarray<LONGLONG> __tmp; 00367 FITSUtil::fill(__tmp,data); 00368 LONGLONG *pllNull=0; 00369 LONGLONG llNull=0; 00370 if (nullValue) 00371 { 00372 llNull = static_cast<LONGLONG>(*nullValue); 00373 pllNull = &llNull; 00374 } 00375 phdu.writeImage(first,nElements,__tmp, pllNull); 00376 00377 } 00378 else if (bitpix() == Ishort) 00379 { 00380 if ( zero() == USBASE && scale() == 1) 00381 { 00382 PrimaryHDU<unsigned short>& phdu 00383 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this); 00384 std::valarray<unsigned short> __tmp; 00385 FITSUtil::fill(__tmp,data); 00386 unsigned short *psNull=0; 00387 unsigned short sNull=0; 00388 if (nullValue) 00389 { 00390 sNull = static_cast<unsigned short>(*nullValue); 00391 psNull = &sNull; 00392 } 00393 phdu.writeImage(first,nElements,__tmp, psNull); 00394 } 00395 else 00396 { 00397 PrimaryHDU<short>& phdu 00398 = dynamic_cast<PrimaryHDU<short>&>(*this); 00399 std::valarray<short> __tmp; 00400 00401 FITSUtil::fill(__tmp,data); 00402 short *psNull=0; 00403 short sNull=0; 00404 if (nullValue) 00405 { 00406 sNull = static_cast<short>(*nullValue); 00407 psNull = &sNull; 00408 } 00409 phdu.writeImage(first,nElements,__tmp,psNull); 00410 } 00411 } 00412 else 00413 { 00414 FITSUtil::MatchType<S> errType; 00415 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType())); 00416 } 00417 } 00418 } 00419 00420 00421 template <typename S> 00422 void PHDU::write(long first, 00423 long nElements, 00424 const std::valarray<S>& data) 00425 { 00426 write(first, nElements, data, static_cast<S*>(0)); 00427 } 00428 00429 template <typename S> 00430 void PHDU::write(const std::vector<long>& first, 00431 long nElements, 00432 const std::valarray<S>& data, 00433 S* nullValue) 00434 { 00435 makeThisCurrent(); 00436 size_t n(first.size()); 00437 long firstElement(0); 00438 long dimSize(1); 00439 for (long i = 0; i < n; ++i) 00440 { 00441 firstElement += ((first[i] - 1)*dimSize); 00442 dimSize *=naxes(i); 00443 } 00444 ++firstElement; 00445 00446 write(firstElement,nElements,data,nullValue); 00447 } 00448 00449 template <typename S> 00450 void PHDU::write(const std::vector<long>& first, 00451 long nElements, 00452 const std::valarray<S>& data) 00453 { 00454 makeThisCurrent(); 00455 size_t n(first.size()); 00456 long firstElement(0); 00457 long dimSize(1); 00458 for (long i = 0; i < n; ++i) 00459 { 00460 00461 firstElement += ((first[i] - 1)*dimSize); 00462 dimSize *=naxes(i); 00463 } 00464 ++firstElement; 00465 00466 write(firstElement,nElements,data); 00467 } 00468 00469 00470 template <typename S> 00471 void PHDU::write(const std::vector<long>& firstVertex, 00472 const std::vector<long>& lastVertex, 00473 const std::vector<long>& stride, 00474 const std::valarray<S>& data) 00475 { 00476 makeThisCurrent(); 00477 try 00478 { 00479 PrimaryHDU<S>& image = dynamic_cast<PrimaryHDU<S>&>(*this); 00480 image.writeImage(firstVertex,lastVertex,stride,data); 00481 } 00482 catch (std::bad_cast) 00483 { 00484 // write input type S to Image type... 00485 00486 if (bitpix() == Ifloat) 00487 { 00488 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this); 00489 size_t n(data.size()); 00490 std::valarray<float> __tmp(n); 00491 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00492 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00493 00494 } 00495 else if (bitpix() == Idouble) 00496 { 00497 PrimaryHDU<double>& phdu 00498 = dynamic_cast<PrimaryHDU<double>&>(*this); 00499 size_t n(data.size()); 00500 std::valarray<double> __tmp(n); 00501 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00502 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00503 } 00504 else if (bitpix() == Ibyte) 00505 { 00506 PrimaryHDU<unsigned char>& phdu 00507 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this); 00508 size_t n(data.size()); 00509 std::valarray<unsigned char> __tmp(n); 00510 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00511 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00512 } 00513 else if (bitpix() == Ilong) 00514 { 00515 if ( zero() == ULBASE && scale() == 1) 00516 { 00517 PrimaryHDU<unsigned INT32BIT>& phdu 00518 = dynamic_cast<PrimaryHDU<unsigned INT32BIT>&>(*this); 00519 size_t n(data.size()); 00520 std::valarray<unsigned INT32BIT> __tmp(n); 00521 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00522 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00523 00524 } 00525 else 00526 { 00527 PrimaryHDU<INT32BIT>& phdu 00528 = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this); 00529 size_t n(data.size()); 00530 std::valarray<INT32BIT> __tmp(n); 00531 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00532 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00533 } 00534 } 00535 else if (bitpix() == Ilonglong) 00536 { 00537 PrimaryHDU<LONGLONG>& phdu 00538 = dynamic_cast<PrimaryHDU<LONGLONG>&>(*this); 00539 size_t n(data.size()); 00540 std::valarray<LONGLONG> __tmp(n); 00541 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00542 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00543 } 00544 else if (bitpix() == Ishort) 00545 { 00546 if ( zero() == USBASE && scale() == 1) 00547 { 00548 PrimaryHDU<unsigned short>& phdu 00549 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this); 00550 size_t n(data.size()); 00551 std::valarray<unsigned short> __tmp(n); 00552 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00553 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00554 00555 } 00556 else 00557 { 00558 PrimaryHDU<short>& phdu 00559 = dynamic_cast<PrimaryHDU<short>&>(*this); 00560 size_t n(data.size()); 00561 std::valarray<short> __tmp(n); 00562 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; 00563 phdu.writeImage(firstVertex,lastVertex,stride,__tmp); 00564 } 00565 } 00566 else 00567 { 00568 FITSUtil::MatchType<S> errType; 00569 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType())); 00570 } 00571 } 00572 } 00573 00574 00575 00576 00577 } // namespace CCfits 00578 #endif