CCfits  2.4
PHDUT.h
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,
00085                                                         phdu.readImage(first,nElements,&nulVal));                              
00086                                 }
00087                                 else
00088                                 {
00089                                         PrimaryHDU<INT32BIT>& phdu 
00090                                                         = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this);
00091                                         INT32BIT nulVal(0);
00092                                         if (nullValue) nulVal = static_cast<INT32BIT>(*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                         // FITSutil::fill will take care of sizing.
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 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,
00215                                              phdu.readImage(firstVertex,lastVertex,stride,&nulVal));                            
00216                                 }
00217                                 else
00218                                 {
00219                                         PrimaryHDU<INT32BIT>& phdu 
00220                                                         = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this);
00221                                         INT32BIT nulVal(0);
00222                                         if (nullValue) nulVal = static_cast<INT32BIT>(*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                 makeThisCurrent();
00272                 if (PrimaryHDU<S>* image = dynamic_cast<PrimaryHDU<S>*>(this))
00273                 {
00274                         image->writeImage(first,nElements,data,nullValue);
00275                 }
00276                 else
00277                 {
00278                         if (bitpix() == Ifloat)
00279                         {
00280                                 std::valarray<float> __tmp;                               
00281                                 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this);
00282                                 FITSUtil::fill(__tmp,data);
00283                                 float* pfNullValue = 0;
00284                                 float fNullValue = 0.0;
00285                                 if (nullValue)
00286                                 {
00287                                    fNullValue = static_cast<float>(*nullValue);
00288                                    pfNullValue = &fNullValue;
00289                                 }                                  
00290                                 phdu.writeImage(first,nElements,__tmp, pfNullValue);
00291                         }
00292                         else if (bitpix() == Idouble)
00293                         {
00294                                 std::valarray<double> __tmp;                                
00295                                 PrimaryHDU<double>& phdu 
00296                                                 = dynamic_cast<PrimaryHDU<double>&>(*this);
00297                                 FITSUtil::fill(__tmp,data);
00298                                 double* pdNullValue = 0;
00299                                 double dNullValue = 0.0;
00300                                 if (nullValue)
00301                                 {
00302                                    dNullValue = static_cast<double>(*nullValue);
00303                                    pdNullValue = &dNullValue;
00304                                 }                                  
00305                                 phdu.writeImage(first,nElements,__tmp, pdNullValue);
00306                         }
00307                         else if (bitpix() == Ibyte)
00308                         {
00309                                 PrimaryHDU<unsigned char>& phdu 
00310                                                 = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00311                                 std::valarray<unsigned char> __tmp;         
00312                                 FITSUtil::fill(__tmp,data);
00313                                 unsigned char *pbNull=0;
00314                                 unsigned char bNull=0;
00315                                 if (nullValue)
00316                                 {
00317                                    bNull = static_cast<unsigned char>(*nullValue);
00318                                    pbNull = &bNull;
00319                                 }                                        
00320                                 phdu.writeImage(first,nElements,__tmp, pbNull);
00321 
00322                         } 
00323                         else if (bitpix() == Ilong)
00324                         {                               
00325                                 if ( zero() == ULBASE && scale() == 1)
00326                                 {
00327                                         PrimaryHDU<unsigned INT32BIT>& phdu 
00328                                                 = dynamic_cast<PrimaryHDU<unsigned INT32BIT>&>(*this);
00329                                         std::valarray<unsigned INT32BIT> __tmp;
00330                                            
00331                                         FITSUtil::fill(__tmp,data);                                        
00332                                         unsigned INT32BIT *plNull=0;
00333                                         unsigned INT32BIT lNull=0;
00334                                         if (nullValue)
00335                                         {
00336                                             lNull = static_cast<unsigned INT32BIT>(*nullValue);
00337                                             plNull = &lNull;
00338                                         }
00339                                         phdu.writeImage(first,nElements,__tmp, plNull);                          
00340                                 }
00341                                 else
00342                                 {
00343                                         PrimaryHDU<INT32BIT>& phdu 
00344                                                         = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this);
00345                                         std::valarray<INT32BIT> __tmp;   
00346 
00347                                         FITSUtil::fill(__tmp,data);
00348                                         INT32BIT *plNull=0;
00349                                         INT32BIT lNull=0;
00350                                         if (nullValue)
00351                                         {
00352                                             lNull = static_cast<INT32BIT>(*nullValue);
00353                                             plNull = &lNull;
00354                                         }
00355                                         phdu.writeImage(first,nElements,__tmp, plNull);                          
00356                                 }
00357                         }    
00358                         else if (bitpix() == Ishort)
00359                         {
00360                                 if ( zero() == USBASE && scale() == 1)
00361                                 {
00362                                         PrimaryHDU<unsigned short>& phdu 
00363                                                 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00364                                         std::valarray<unsigned short> __tmp;   
00365                                         FITSUtil::fill(__tmp,data);
00366                                         unsigned short *psNull=0;
00367                                         unsigned short sNull=0;
00368                                         if (nullValue)
00369                                         {
00370                                            sNull = static_cast<unsigned short>(*nullValue);
00371                                            psNull = &sNull;
00372                                         }                                        
00373                                         phdu.writeImage(first,nElements,__tmp, psNull);                          
00374                                 }
00375                                 else
00376                                 {
00377                                         PrimaryHDU<short>& phdu 
00378                                                         = dynamic_cast<PrimaryHDU<short>&>(*this);
00379                                         std::valarray<short> __tmp;   
00380 
00381                                         FITSUtil::fill(__tmp,data);
00382                                         short *psNull=0;
00383                                         short sNull=0;
00384                                         if (nullValue)
00385                                         {
00386                                            sNull = static_cast<short>(*nullValue);
00387                                            psNull = &sNull;
00388                                         }                                        
00389                                         phdu.writeImage(first,nElements,__tmp,psNull);                          
00390                                 }
00391                         }           
00392                         else
00393                         {
00394                                 FITSUtil::MatchType<S> errType;                                
00395                                 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00396                         }        
00397                 }
00398         }
00399 
00400 
00401         template <typename S>
00402         void PHDU::write(long first,
00403                             long nElements,
00404                         const std::valarray<S>& data)
00405         {
00406            write(first, nElements, data, static_cast<S*>(0));                
00407         }        
00408 
00409         template <typename S>
00410         void PHDU::write(const std::vector<long>& first,
00411                         long nElements,
00412                         const std::valarray<S>& data,
00413                         S* nullValue)
00414         {        
00415                 makeThisCurrent();
00416                 size_t n(first.size());
00417                 long firstElement(0);
00418                 long dimSize(1);
00419                 for (long i = 0; i < first.size(); ++i)
00420                 {
00421                         firstElement +=  ((first[i] - 1)*dimSize);
00422                         dimSize *=naxes(i);   
00423                 }       
00424                 ++firstElement;
00425 
00426                 write(firstElement,nElements,data,nullValue);
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         {        
00434                 makeThisCurrent();
00435                 size_t n(first.size());
00436                 long firstElement(0);
00437                 long dimSize(1);
00438                 for (long i = 0; i < first.size(); ++i)
00439                 {
00440 
00441                         firstElement +=  ((first[i] - 1)*dimSize);
00442                         dimSize *=naxes(i);   
00443                 }       
00444                 ++firstElement;
00445 
00446                 write(firstElement,nElements,data);                     
00447         }        
00448 
00449 
00450         template <typename S>
00451         void PHDU::write(const std::vector<long>& firstVertex,
00452                         const std::vector<long>& lastVertex,
00453                         const std::vector<long>& stride, 
00454                         const std::valarray<S>& data)
00455         {
00456                 makeThisCurrent();
00457                 try
00458                 {
00459                         PrimaryHDU<S>& image = dynamic_cast<PrimaryHDU<S>&>(*this);
00460                         image.writeImage(firstVertex,lastVertex,stride,data);  
00461                 }
00462                 catch (std::bad_cast)
00463                 {
00464                          // write input type S to Image type...
00465 
00466                         if (bitpix() == Ifloat)
00467                         {
00468                                 PrimaryHDU<float>& phdu = dynamic_cast<PrimaryHDU<float>&>(*this);
00469                                 size_t n(data.size());
00470                                 std::valarray<float> __tmp(n);
00471                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00472                                 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00473 
00474                         }
00475                         else if (bitpix() == Idouble)
00476                         {
00477                                 PrimaryHDU<double>& phdu 
00478                                         = dynamic_cast<PrimaryHDU<double>&>(*this);
00479                                 size_t n(data.size());
00480                                 std::valarray<double> __tmp(n);
00481                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00482                                 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);
00483                         }
00484                         else if (bitpix() == Ibyte)
00485                         {
00486                                 PrimaryHDU<unsigned char>& phdu 
00487                                         = dynamic_cast<PrimaryHDU<unsigned char>&>(*this);
00488                                 size_t n(data.size());
00489                                 std::valarray<unsigned char> __tmp(n);
00490                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00491                                 phdu.writeImage(firstVertex,lastVertex,stride,__tmp);                        
00492                         } 
00493                         else if (bitpix() == Ilong)
00494                         {
00495                                 if ( zero() == ULBASE && scale() == 1)
00496                                 {
00497                                         PrimaryHDU<unsigned INT32BIT>& phdu 
00498                                                 = dynamic_cast<PrimaryHDU<unsigned INT32BIT>&>(*this);
00499                                         size_t n(data.size());
00500                                         std::valarray<unsigned INT32BIT> __tmp(n);
00501                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00502                                         phdu.writeImage(firstVertex,lastVertex,stride,__tmp);                         
00503 
00504                                 }
00505                                 else
00506                                 {
00507                                         PrimaryHDU<INT32BIT>& phdu 
00508                                                         = dynamic_cast<PrimaryHDU<INT32BIT>&>(*this);
00509                                         size_t n(data.size());
00510                                         std::valarray<INT32BIT> __tmp(n);
00511                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00512                                         phdu.writeImage(firstVertex,lastVertex,stride,__tmp);                         
00513                                 }
00514                         }                           
00515                         else if (bitpix() == Ishort)
00516                         {
00517                                 if ( zero() == USBASE && scale() == 1)
00518                                 {
00519                                         PrimaryHDU<unsigned short>& phdu 
00520                                                 = dynamic_cast<PrimaryHDU<unsigned short>&>(*this);
00521                                         size_t n(data.size());
00522                                         std::valarray<unsigned short> __tmp(n);
00523                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00524                                         phdu.writeImage(firstVertex,lastVertex,stride,__tmp);                         
00525 
00526                                 }
00527                                 else
00528                                 {
00529                                         PrimaryHDU<short>& phdu 
00530                                                         = dynamic_cast<PrimaryHDU<short>&>(*this);
00531                                         size_t n(data.size());
00532                                         std::valarray<short> __tmp(n);
00533                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00534                                         phdu.writeImage(firstVertex,lastVertex,stride,__tmp);                         
00535                                 }
00536                         }          
00537                         else
00538                         {
00539                                 FITSUtil::MatchType<S> errType;                                
00540                                 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00541                         }        
00542                 }  
00543         }  
00544 
00545 
00546 
00547 
00548 } // namespace CCfits
00549 #endif