CCfits  2.4
ExtHDUT.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 #ifndef EXTHDUT_H
00010 #define EXTHDUT_H
00011 #include "ImageExt.h"
00012 #include "Table.h"
00013 #include "Column.h"
00014 
00015 namespace CCfits 
00016 {
00017         template <typename S>
00018         void ExtHDU::read (std::valarray<S>& image) 
00019         {
00020                 makeThisCurrent();
00021                 long init(1);
00022                 long nElements(std::accumulate(naxes().begin(),naxes().end(),init,
00023                                 std::multiplies<long>()));
00024                 read(image,1,nElements,static_cast<S*>(0));
00025 
00026 
00027         }
00028 
00029 
00030 
00031         template <typename S>
00032         void ExtHDU::read (std::valarray<S>& image, long first,long nElements) 
00033         {
00034                 makeThisCurrent();
00035                 read(image, first,nElements,static_cast<S*>(0));
00036         }
00037 
00038         template <typename S>
00039         void ExtHDU::read (std::valarray<S>& image, long first, long nElements,  S* nulValue) 
00040         {
00041 
00042                 makeThisCurrent();                
00043                 if ( ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00044                 {
00045                         // proceed if cast is successful.                  
00046                         const std::valarray<S>& __tmp 
00047                                         = extimage->readImage(first,nElements,nulValue);
00048                         image.resize(__tmp.size());
00049                         image = __tmp;
00050                 }
00051                 else 
00052                 {
00053                         if (bitpix() == Ifloat)
00054                         {
00055                                 ImageExt<float>& extimage 
00056                                                 = dynamic_cast<ImageExt<float>&>(*this);
00057                                 float nulVal(0);
00058                                 if (nulValue) nulVal = static_cast<float>(*nulValue);                                 
00059                                 FITSUtil::fill(image,
00060                                                 extimage.readImage(first,nElements,&nulVal));
00061                         }
00062                         else if (bitpix() == Idouble)
00063                         {
00064                                 ImageExt<double>& extimage 
00065                                                 = dynamic_cast<ImageExt<double>&>(*this);
00066                                 double nulVal(0);
00067                                 if (nulValue) nulVal = static_cast<double>(*nulValue);                                 
00068                                 FITSUtil::fill(image,
00069                                                 extimage.readImage(first,nElements,&nulVal));
00070                         }
00071                         else if (bitpix() == Ibyte)
00072                         {
00073                                 ImageExt<unsigned char>& extimage 
00074                                                 = dynamic_cast<ImageExt<unsigned char>&>(*this);
00075                                 unsigned char nulVal(0);
00076                                 if (nulValue) nulVal = static_cast<unsigned char>(*nulValue);                                 
00077                                 FITSUtil::fill(image,
00078                                                 extimage.readImage(first,nElements,&nulVal));
00079                         } 
00080                         else if (bitpix() == Ilong)
00081                         {
00082                                 if ( zero() == ULBASE && scale() == 1)
00083                                 {                                
00084                                         ImageExt<unsigned INT32BIT>& extimage 
00085                                                 = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00086                                         unsigned INT32BIT nulVal(0);
00087                                         if (nulValue) nulVal 
00088                                                 = static_cast<unsigned INT32BIT>(*nulValue);                                 
00089                                         FITSUtil::fill(image,
00090                                                 extimage.readImage(first,nElements,&nulVal));
00091                                 }
00092                                 else
00093                                 {
00094                                         ImageExt<INT32BIT>& extimage 
00095                                                         = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00096                                         INT32BIT nulVal(0);
00097                                         if (nulValue) nulVal = static_cast<INT32BIT>(*nulValue);                                 
00098                                         FITSUtil::fill(image,
00099                                                 extimage.readImage(first,nElements,&nulVal));
00100                                 }
00101                         }    
00102                         else if (bitpix() == Ishort)
00103                         {
00104                                 if ( zero() == USBASE && scale() == 1)
00105                                 {                                
00106                                         ImageExt<unsigned short>& extimage
00107                                                 = dynamic_cast<ImageExt<unsigned short>&>(*this);
00108                                         unsigned short nulVal(0);
00109                                         if (nulValue) nulVal 
00110                                                 = static_cast<unsigned short>(*nulValue);                                 
00111                                         FITSUtil::fill(image,
00112                                                 extimage.readImage(first,nElements,&nulVal));
00113                                 }
00114                                 else
00115                                 {
00116                                         ImageExt<short>& extimage 
00117                                                         = dynamic_cast<ImageExt<short>&>(*this);
00118                                         short nulVal(0);
00119                                         if (nulValue) nulVal = static_cast<short>(*nulValue);                                 
00120                                         FITSUtil::fill(image,
00121                                                 extimage.readImage(first,nElements,&nulVal));
00122                                 }
00123                         }          
00124                         else 
00125                         {
00126                                 throw CCfits::FitsFatal(" casting image types ");
00127                         }     
00128                 }
00129 
00130         }  
00131 
00132         template<typename S>
00133         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& first,
00134                                 long nElements, 
00135                                 S* nulValue)
00136         {
00137                 makeThisCurrent();
00138                 long firstElement(0);
00139                 long dimSize(1);
00140                 std::vector<long> inputDimensions(naxis(),1);
00141                 size_t sNaxis = static_cast<size_t>(naxis());
00142                 size_t n(std::min(sNaxis,first.size()));
00143                 std::copy(&first[0],&first[0]+n,&inputDimensions[0]);                
00144                 for (long i = 0; i < naxis(); ++i)
00145                 {
00146 
00147                    firstElement +=  ((inputDimensions[i] - 1)*dimSize);
00148                    dimSize *=naxes(i);   
00149                 }
00150                 ++firstElement;                
00151 
00152 
00153                 read(image, firstElement,nElements,nulValue);
00154 
00155 
00156 
00157         } 
00158 
00159         template<typename S>
00160         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& first, 
00161                                 long nElements) 
00162         {
00163                 makeThisCurrent();
00164                 read(image, first,nElements,static_cast<S*>(0));
00165 
00166         } 
00167 
00168         template<typename S>
00169         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex, 
00170                                 const std::vector<long>& lastVertex, 
00171                                 const std::vector<long>& stride, 
00172                                 S* nulValue)
00173         {
00174                 makeThisCurrent();
00175                 if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00176                 {
00177                         const std::valarray<S>& __tmp 
00178                                       = extimage->readImage(firstVertex,lastVertex,stride,nulValue);
00179                         image.resize(__tmp.size());
00180                         image = __tmp;
00181                 }
00182                 else
00183                 {
00184                         // FITSutil::fill will take care of sizing.
00185                         if (bitpix() == Ifloat)
00186                         {
00187                                 float nulVal(0);
00188                                 if (nulValue) nulVal = static_cast<float>(*nulValue);                                 
00189                                 ImageExt<float>& extimage = dynamic_cast<ImageExt<float>&>(*this);
00190                                 FITSUtil::fill(image,
00191                                         extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00192                         }
00193                         else if (bitpix() == Idouble)
00194                         {
00195                                 ImageExt<double>& extimage = dynamic_cast<ImageExt<double>&>(*this);
00196                                 double nulVal(0);
00197                                 if (nulValue) nulVal = static_cast<double>(*nulValue);                                 
00198                                 FITSUtil::fill(image,
00199                                          extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00200                         }
00201                         else if (bitpix() == Ibyte)
00202                         {
00203                                 ImageExt<unsigned char>& extimage 
00204                                                 = dynamic_cast<ImageExt<unsigned char>&>(*this);
00205                                 unsigned char nulVal(0);
00206                                 if (nulValue) nulVal = static_cast<unsigned char>(*nulValue);                                 
00207                                 FITSUtil::fill(image,
00208                                          extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00209                         } 
00210                         else if (bitpix() == Ilong)
00211                         {
00212                                 if ( zero() == ULBASE && scale() == 1)
00213                                 {                                
00214                                         ImageExt<unsigned INT32BIT>& extimage 
00215                                                 = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00216                                         unsigned INT32BIT nulVal(0);
00217                                         if (nulValue) 
00218                                                 nulVal = static_cast<unsigned INT32BIT>(*nulValue);                                 
00219                                         FITSUtil::fill(image,
00220                                          extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00221                                 }
00222                                 else
00223                                 {
00224                                         ImageExt<INT32BIT>& extimage = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00225                                         INT32BIT nulVal(0);
00226                                         if (nulValue) nulVal = static_cast<INT32BIT>(*nulValue);                                 
00227                                         FITSUtil::fill(image,
00228                                          extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00229                                 }
00230                         }    
00231                         else if (bitpix() == Ishort)
00232                         {
00233                                 if ( zero() == USBASE && scale() == 1)
00234                                 {                                
00235                                         ImageExt<unsigned short>& extimage 
00236                                                 = dynamic_cast<ImageExt<unsigned short>&>(*this);
00237                                         unsigned short nulVal(0);
00238                                         if (nulValue) nulVal 
00239                                                 = static_cast<unsigned short>(*nulValue);                                 
00240                                         FITSUtil::fill(image,
00241                                          extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00242                                 }
00243                                 else
00244                                 {        
00245                                         ImageExt<short>& extimage 
00246                                                         = dynamic_cast<ImageExt<short>&>(*this);
00247                                         short nulVal(0);
00248                                         if (nulValue) nulVal = static_cast<short>(*nulValue);                                 
00249                                         FITSUtil::fill(image,
00250                                          extimage.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 ExtHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex, 
00262                                 const std::vector<long>& lastVertex, 
00263                                 const std::vector<long>& stride) 
00264         {
00265                 makeThisCurrent();
00266                 read(image, firstVertex,lastVertex,stride,static_cast<S*>(0));
00267         }  
00268 
00269         template <typename S>
00270         void ExtHDU::write(long first,long nElements,const std::valarray<S>& data,S* nulValue)
00271         {                
00272                 // throw if we called image read/write operations on a table.
00273                 makeThisCurrent();
00274                 if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00275                 {
00276                         extimage->writeImage(first,nElements,data,nulValue);
00277                 }
00278                 else
00279                 {
00280                         if (bitpix() == Ifloat)
00281                         {
00282                                 std::valarray<float> __tmp;                               
00283                                 ImageExt<float>& imageExt = dynamic_cast<ImageExt<float>&>(*this);
00284                                 FITSUtil::fill(__tmp,data);
00285                                 float* pfNullValue = 0;
00286                                 float fNullValue = 0.0;
00287                                 if (nulValue)
00288                                 {
00289                                    fNullValue = static_cast<float>(*nulValue);
00290                                    pfNullValue = &fNullValue;
00291                                 }
00292                                 imageExt.writeImage(first,nElements,__tmp, pfNullValue);
00293                         }
00294                         else if (bitpix() == Idouble)
00295                         {
00296                                 std::valarray<double> __tmp;                                
00297                                 ImageExt<double>& imageExt 
00298                                                 = dynamic_cast<ImageExt<double>&>(*this);
00299                                 FITSUtil::fill(__tmp,data);
00300                                 double* pdNullValue = 0;
00301                                 double dNullValue = 0.0;
00302                                 if (nulValue)
00303                                 {
00304                                    dNullValue = static_cast<double>(*nulValue);
00305                                    pdNullValue = &dNullValue;
00306                                 }
00307                                 imageExt.writeImage(first,nElements,__tmp,pdNullValue);                              
00308                         }
00309                         else if (bitpix() == Ibyte)
00310                         {
00311                                 ImageExt<unsigned char>& imageExt 
00312                                                 = dynamic_cast<ImageExt<unsigned char>&>(*this);
00313                                 std::valarray<unsigned char> __tmp; 
00314                                 FITSUtil::fill(__tmp,data);                                        
00315                                 unsigned char *pbNull=0;
00316                                 unsigned char bNull=0;
00317                                 if (nulValue)
00318                                 {
00319                                    bNull = static_cast<unsigned char>(*nulValue);
00320                                    pbNull = &bNull;
00321                                 }                                        
00322                                 imageExt.writeImage(first,nElements,__tmp, pbNull); 
00323 
00324                         } 
00325                         else if (bitpix() == Ilong)
00326                         {                               
00327                                 if ( zero() == ULBASE && scale() == 1)
00328                                 {                                
00329                                         ImageExt<unsigned INT32BIT>& imageExt
00330                                                = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00331                                         std::valarray<unsigned INT32BIT> __tmp;
00332 
00333                                         FITSUtil::fill(__tmp,data);
00334                                         unsigned INT32BIT *plNull=0;
00335                                         unsigned INT32BIT lNull=0;
00336                                         if (nulValue)
00337                                         {
00338                                            lNull = static_cast<unsigned INT32BIT>(*nulValue);
00339                                            plNull = &lNull;
00340                                         }                                        
00341                                         imageExt.writeImage(first,nElements,__tmp,plNull);                          
00342                                 }
00343                                 else
00344                                 {                        
00345                                         ImageExt<INT32BIT>& imageExt 
00346                                                         = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00347                                         std::valarray<INT32BIT> __tmp;                                 
00348                                         FITSUtil::fill(__tmp,data);                                        
00349                                         INT32BIT *plNull=0;
00350                                         INT32BIT lNull=0;
00351                                         if (nulValue)
00352                                         {
00353                                            lNull = static_cast<INT32BIT>(*nulValue);
00354                                            plNull = &lNull;
00355                                         }                                        
00356                                         imageExt.writeImage(first,nElements,__tmp,plNull);                          
00357                                 }                        
00358                         }    
00359                         else if (bitpix() == Ishort)
00360                         {
00361                                 if ( zero() == USBASE && scale() == 1)
00362                                 {                                
00363                                         ImageExt<unsigned short>& imageExt
00364                                                  = dynamic_cast<ImageExt<unsigned short>&>(*this);
00365                                         std::valarray<unsigned short> __tmp;
00366                                         FITSUtil::fill(__tmp,data);
00367                                         unsigned short *psNull=0;
00368                                         unsigned short sNull=0;
00369                                         if (nulValue)
00370                                         {
00371                                            sNull = static_cast<unsigned short>(*nulValue);
00372                                            psNull = &sNull;
00373                                         }                                        
00374                                         imageExt.writeImage(first,nElements,__tmp,psNull);                          
00375                                 }
00376                                 else
00377                                 {             
00378                                         ImageExt<short>& imageExt 
00379                                                         = dynamic_cast<ImageExt<short>&>(*this);
00380                                         std::valarray<short> __tmp; 
00381                                         FITSUtil::fill(__tmp,data);                                        
00382                                         short *psNull=0;
00383                                         short sNull=0;
00384                                         if (nulValue)
00385                                         {
00386                                            sNull = static_cast<short>(*nulValue);
00387                                            psNull = &sNull;
00388                                         }                                        
00389                                         imageExt.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         template <typename S>
00401         void ExtHDU::write(long first,
00402                             long nElements,const std::valarray<S>& data)
00403         {
00404            write(first, nElements, data, static_cast<S*>(0));                
00405         }
00406 
00407         template <typename S>
00408         void ExtHDU::write(const std::vector<long>& first,
00409                         long nElements,
00410                         const std::valarray<S>& data,
00411                         S* nulValue)
00412         {        
00413                 // throw if we called image read/write operations on a table.
00414                 makeThisCurrent();
00415                 size_t n(first.size());
00416                 long firstElement(0);
00417                 long dimSize(1);
00418                 for (long i = 0; i < first.size(); ++i)
00419                 {
00420                         firstElement +=  ((first[i] - 1)*dimSize);
00421                         dimSize *=naxes(i);   
00422                 }       
00423                 ++firstElement;
00424 
00425                 write(firstElement,nElements,data,nulValue);
00426         }
00427 
00428         template <typename S>
00429         void ExtHDU::write(const std::vector<long>& first,
00430                         long nElements,
00431                         const std::valarray<S>& data)
00432         {        
00433                 // throw if we called image read/write operations on a table.
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 ExtHDU::write(const std::vector<long>& firstVertex,
00452                         const std::vector<long>& lastVertex,
00453                         const std::valarray<S>& data)
00454         {
00455                 makeThisCurrent();
00456                 if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00457                 {
00458                         extimage->writeImage(firstVertex,lastVertex,data);  
00459                 }
00460                 else
00461                 {
00462                          // write input type S to Image type...
00463 
00464                         if (bitpix() == Ifloat)
00465                         {
00466                                 ImageExt<float>& extimage = dynamic_cast<ImageExt<float>&>(*this);
00467                                 size_t n(data.size());
00468                                 std::valarray<float> __tmp(n);
00469                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00470                                 extimage.writeImage(firstVertex,lastVertex,__tmp);
00471 
00472                         }
00473                         else if (bitpix() == Idouble)
00474                         {
00475                                 ImageExt<double>& extimage 
00476                                         = dynamic_cast<ImageExt<double>&>(*this);
00477                                 size_t n(data.size());
00478                                 std::valarray<double> __tmp(n);
00479                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00480                                 extimage.writeImage(firstVertex,lastVertex,__tmp);
00481                         }
00482                         else if (bitpix() == Ibyte)
00483                         {
00484                                 ImageExt<unsigned char>& extimage 
00485                                         = dynamic_cast<ImageExt<unsigned char>&>(*this);
00486                                 size_t n(data.size());
00487                                 std::valarray<unsigned char> __tmp(n);
00488                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00489                                 extimage.writeImage(firstVertex,lastVertex,__tmp);                        
00490                         } 
00491                         else if (bitpix() == Ilong)
00492                         {
00493                                 if ( zero() == ULBASE && scale() == 1)
00494                                 {                                
00495                                         ImageExt<unsigned INT32BIT>& extimage 
00496                                                 = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00497                                         size_t n(data.size());
00498                                         std::valarray<unsigned INT32BIT> __tmp(n);
00499                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00500                                         extimage.writeImage(firstVertex,lastVertex,__tmp);    
00501                                 }
00502                                 else                              
00503                                 {
00504                                         ImageExt<INT32BIT>& extimage 
00505                                                         = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00506                                         size_t n(data.size());
00507                                         std::valarray<INT32BIT> __tmp(n);
00508                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00509                                         extimage.writeImage(firstVertex,lastVertex,__tmp);
00510                                 }                              
00511                         }    
00512                         else if (bitpix() == Ishort)
00513                         {
00514                                 if ( zero() == USBASE && scale() == 1)
00515                                 {
00516                                         ImageExt<unsigned short>& extimage 
00517                                                 = dynamic_cast<ImageExt<unsigned short>&>(*this);
00518                                         size_t n(data.size());
00519                                         std::valarray<unsigned short> __tmp(n);
00520                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00521                                         extimage.writeImage(firstVertex,lastVertex,__tmp);  
00522                                 }
00523                                 else
00524                                 {                    
00525                                         ImageExt<short>& extimage 
00526                                                         = dynamic_cast<ImageExt<short>&>(*this);
00527                                         size_t n(data.size());
00528                                         std::valarray<short> __tmp(n);
00529                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00530                                         extimage.writeImage(firstVertex,lastVertex,__tmp);     
00531                                 }                             
00532                         }          
00533                         else
00534                         {
00535                                 FITSUtil::MatchType<S> errType;                                
00536                                 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00537                         }        
00538                 }  
00539         }  
00540 
00541 
00542 } //namespace CCfits
00543 
00544 #endif