CCfits  2.5
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,extimage.readImage(first,nElements, &nulVal));
00060                         }
00061                         else if (bitpix() == Idouble)
00062                         {
00063                                 ImageExt<double>& extimage 
00064                                                 = dynamic_cast<ImageExt<double>&>(*this);
00065                                 double nulVal(0);
00066                                 if (nulValue) nulVal = static_cast<double>(*nulValue);                                 
00067                                 FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00068                         }
00069                         else if (bitpix() == Ibyte)
00070                         {
00071                                 ImageExt<unsigned char>& extimage 
00072                                                 = dynamic_cast<ImageExt<unsigned char>&>(*this);
00073                                 unsigned char nulVal(0);
00074                                 if (nulValue) nulVal = static_cast<unsigned char>(*nulValue);                                 
00075                                 FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00076                         } 
00077                         else if (bitpix() == Ilong)
00078                         {
00079                                 if ( zero() == ULBASE && scale() == 1)
00080                                 {                                
00081                                         ImageExt<unsigned INT32BIT>& extimage 
00082                                                 = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00083                                         unsigned INT32BIT nulVal(0);
00084                                         if (nulValue) nulVal 
00085                                                 = static_cast<unsigned INT32BIT>(*nulValue);                                 
00086                                         FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00087                                 }
00088                                 else
00089                                 {
00090                                         ImageExt<INT32BIT>& extimage 
00091                                                         = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00092                                         INT32BIT nulVal(0);
00093                                         if (nulValue) nulVal = static_cast<INT32BIT>(*nulValue);                                 
00094                                         FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00095                                }
00096                         }    
00097                         else if (bitpix() == Ilonglong)
00098                         {
00099                                 ImageExt<LONGLONG>& extimage 
00100                                                 = dynamic_cast<ImageExt<LONGLONG>&>(*this);
00101                                 LONGLONG nulVal(0);
00102                                 if (nulValue) nulVal = static_cast<LONGLONG>(*nulValue);                                 
00103                                 FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00104                         } 
00105                         else if (bitpix() == Ishort)
00106                         {
00107                                 if ( zero() == USBASE && scale() == 1)
00108                                 {                                
00109                                         ImageExt<unsigned short>& extimage
00110                                                 = dynamic_cast<ImageExt<unsigned short>&>(*this);
00111                                         unsigned short nulVal(0);
00112                                         if (nulValue) nulVal 
00113                                                 = static_cast<unsigned short>(*nulValue);                                 
00114                                         FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00115                                 }
00116                                 else
00117                                 {
00118                                         ImageExt<short>& extimage 
00119                                                         = dynamic_cast<ImageExt<short>&>(*this);
00120                                         short nulVal(0);
00121                                         if (nulValue) nulVal = static_cast<short>(*nulValue);                                 
00122                                         FITSUtil::fill(image,extimage.readImage(first,nElements, &nulVal));
00123                                }
00124                         }          
00125                         else 
00126                         {
00127                                 throw CCfits::FitsFatal(" casting image types ");
00128                         }     
00129                 }
00130 
00131         }  
00132 
00133         template<typename S>
00134         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& first,
00135                                 long nElements, 
00136                                 S* nulValue)
00137         {
00138                 makeThisCurrent();
00139                 long firstElement(0);
00140                 long dimSize(1);
00141                 std::vector<long> inputDimensions(naxis(),1);
00142                 size_t sNaxis = static_cast<size_t>(naxis());
00143                 size_t n(std::min(sNaxis,first.size()));
00144                 std::copy(&first[0],&first[0]+n,&inputDimensions[0]);                
00145                 for (long i = 0; i < naxis(); ++i)
00146                 {
00147 
00148                    firstElement +=  ((inputDimensions[i] - 1)*dimSize);
00149                    dimSize *=naxes(i);   
00150                 }
00151                 ++firstElement;                
00152 
00153 
00154                 read(image, firstElement,nElements,nulValue);
00155 
00156 
00157 
00158         } 
00159 
00160         template<typename S>
00161         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& first, 
00162                                 long nElements) 
00163         {
00164                 makeThisCurrent();
00165                 read(image, first,nElements,static_cast<S*>(0));
00166 
00167         } 
00168 
00169         template<typename S>
00170         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex, 
00171                                 const std::vector<long>& lastVertex, 
00172                                 const std::vector<long>& stride, 
00173                                 S* nulValue)
00174         {
00175                 makeThisCurrent();
00176                 if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00177                 {
00178                    const std::valarray<S>& __tmp = 
00179                       extimage->readImage(firstVertex,lastVertex,stride,nulValue);
00180                    image.resize(__tmp.size());
00181                    image = __tmp;
00182                 }
00183                 else
00184                 {
00185                         // FITSutil::fill will take care of sizing.
00186                         if (bitpix() == Ifloat)
00187                         {
00188                                 float nulVal(0);
00189                                 if (nulValue) nulVal = static_cast<float>(*nulValue);                                 
00190                                 ImageExt<float>& extimage = dynamic_cast<ImageExt<float>&>(*this);
00191                                 FITSUtil::fill(image,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,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00199                         }
00200                         else if (bitpix() == Ibyte)
00201                         {
00202                                 ImageExt<unsigned char>& extimage 
00203                                                 = dynamic_cast<ImageExt<unsigned char>&>(*this);
00204                                 unsigned char nulVal(0);
00205                                 if (nulValue) nulVal = static_cast<unsigned char>(*nulValue);                                 
00206                                 FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00207                         } 
00208                         else if (bitpix() == Ilong)
00209                         {
00210                                 if ( zero() == ULBASE && scale() == 1)
00211                                 {                                
00212                                         ImageExt<unsigned INT32BIT>& extimage 
00213                                                 = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00214                                         unsigned INT32BIT nulVal(0);
00215                                         if (nulValue) 
00216                                                 nulVal = static_cast<unsigned INT32BIT>(*nulValue);                                 
00217                                         FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00218                                 }
00219                                 else
00220                                 {
00221                                         ImageExt<INT32BIT>& extimage = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00222                                         INT32BIT nulVal(0);
00223                                         if (nulValue) nulVal = static_cast<INT32BIT>(*nulValue);                                 
00224                                         FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00225                                 }
00226                         }    
00227                         else if (bitpix() == Ilonglong)
00228                         {
00229                                 ImageExt<LONGLONG>& extimage 
00230                                                 = dynamic_cast<ImageExt<LONGLONG>&>(*this);
00231                                 LONGLONG nulVal(0);
00232                                 if (nulValue) nulVal = static_cast<LONGLONG>(*nulValue);                                 
00233                                 FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00234                         } 
00235                         else if (bitpix() == Ishort)
00236                         {
00237                                 if ( zero() == USBASE && scale() == 1)
00238                                 {                                
00239                                         ImageExt<unsigned short>& extimage 
00240                                                 = dynamic_cast<ImageExt<unsigned short>&>(*this);
00241                                         unsigned short nulVal(0);
00242                                         if (nulValue) nulVal 
00243                                                 = static_cast<unsigned short>(*nulValue);                                 
00244                                         FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00245                                 }
00246                                 else
00247                                 {        
00248                                         ImageExt<short>& extimage 
00249                                                         = dynamic_cast<ImageExt<short>&>(*this);
00250                                         short nulVal(0);
00251                                         if (nulValue) nulVal = static_cast<short>(*nulValue);                                 
00252                                         FITSUtil::fill(image,extimage.readImage(firstVertex,lastVertex,stride,&nulVal));
00253                                 }
00254                         }          
00255                         else 
00256                         {
00257                                 throw CCfits::FitsFatal(" casting image types ");
00258                         }     
00259                 }
00260         }  
00261 
00262         template<typename S>
00263         void ExtHDU::read (std::valarray<S>& image, const std::vector<long>& firstVertex, 
00264                                 const std::vector<long>& lastVertex, 
00265                                 const std::vector<long>& stride) 
00266         {
00267                 makeThisCurrent();
00268                 read(image, firstVertex,lastVertex,stride,static_cast<S*>(0));
00269         }  
00270 
00271         template <typename S>
00272         void ExtHDU::write(long first,long nElements,const std::valarray<S>& data,S* nulValue)
00273         {                
00274                 // throw if we called image read/write operations on a table.
00275                 makeThisCurrent();
00276                 if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00277                 {
00278                         extimage->writeImage(first,nElements,data,nulValue);
00279                 }
00280                 else
00281                 {
00282                         if (bitpix() == Ifloat)
00283                         {
00284                                 std::valarray<float> __tmp;                               
00285                                 ImageExt<float>& imageExt = dynamic_cast<ImageExt<float>&>(*this);
00286                                 FITSUtil::fill(__tmp,data);
00287                                 float* pfNullValue = 0;
00288                                 float fNullValue = 0.0;
00289                                 if (nulValue)
00290                                 {
00291                                    fNullValue = static_cast<float>(*nulValue);
00292                                    pfNullValue = &fNullValue;
00293                                 }
00294                                 imageExt.writeImage(first,nElements,__tmp, pfNullValue);
00295                         }
00296                         else if (bitpix() == Idouble)
00297                         {
00298                                 std::valarray<double> __tmp;                                
00299                                 ImageExt<double>& imageExt 
00300                                                 = dynamic_cast<ImageExt<double>&>(*this);
00301                                 FITSUtil::fill(__tmp,data);
00302                                 double* pdNullValue = 0;
00303                                 double dNullValue = 0.0;
00304                                 if (nulValue)
00305                                 {
00306                                    dNullValue = static_cast<double>(*nulValue);
00307                                    pdNullValue = &dNullValue;
00308                                 }
00309                                 imageExt.writeImage(first,nElements,__tmp,pdNullValue);                              
00310                         }
00311                         else if (bitpix() == Ibyte)
00312                         {
00313                                 ImageExt<unsigned char>& imageExt 
00314                                                 = dynamic_cast<ImageExt<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 (nulValue)
00320                                 {
00321                                    bNull = static_cast<unsigned char>(*nulValue);
00322                                    pbNull = &bNull;
00323                                 }                                        
00324                                 imageExt.writeImage(first,nElements,__tmp, pbNull); 
00325 
00326                         } 
00327                         else if (bitpix() == Ilong)
00328                         {                               
00329                                 if ( zero() == ULBASE && scale() == 1)
00330                                 {                                
00331                                         ImageExt<unsigned INT32BIT>& imageExt
00332                                                = dynamic_cast<ImageExt<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 (nulValue)
00339                                         {
00340                                            lNull = static_cast<unsigned INT32BIT>(*nulValue);
00341                                            plNull = &lNull;
00342                                         }                                        
00343                                         imageExt.writeImage(first,nElements,__tmp,plNull);                          
00344                                 }
00345                                 else
00346                                 {                        
00347                                         ImageExt<INT32BIT>& imageExt 
00348                                                         = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00349                                         std::valarray<INT32BIT> __tmp;                                 
00350                                         FITSUtil::fill(__tmp,data);                                        
00351                                         INT32BIT *plNull=0;
00352                                         INT32BIT lNull=0;
00353                                         if (nulValue)
00354                                         {
00355                                            lNull = static_cast<INT32BIT>(*nulValue);
00356                                            plNull = &lNull;
00357                                         }                                        
00358                                         imageExt.writeImage(first,nElements,__tmp,plNull);                          
00359                                 }                        
00360                         }    
00361                         else if (bitpix() == Ilonglong)
00362                         {
00363                                 ImageExt<LONGLONG>& imageExt 
00364                                                 = dynamic_cast<ImageExt<LONGLONG>&>(*this);
00365                                 std::valarray<LONGLONG> __tmp; 
00366                                 FITSUtil::fill(__tmp,data);                                        
00367                                 LONGLONG *pllNull=0;
00368                                 LONGLONG llNull=0;
00369                                 if (nulValue)
00370                                 {
00371                                    llNull = static_cast<LONGLONG>(*nulValue);
00372                                    pllNull = &llNull;
00373                                 }                                        
00374                                 imageExt.writeImage(first,nElements,__tmp, pllNull); 
00375 
00376                         } 
00377                         else if (bitpix() == Ishort)
00378                         {
00379                                 if ( zero() == USBASE && scale() == 1)
00380                                 {                                
00381                                         ImageExt<unsigned short>& imageExt
00382                                                  = dynamic_cast<ImageExt<unsigned short>&>(*this);
00383                                         std::valarray<unsigned short> __tmp;
00384                                         FITSUtil::fill(__tmp,data);
00385                                         unsigned short *psNull=0;
00386                                         unsigned short sNull=0;
00387                                         if (nulValue)
00388                                         {
00389                                            sNull = static_cast<unsigned short>(*nulValue);
00390                                            psNull = &sNull;
00391                                         }                                        
00392                                         imageExt.writeImage(first,nElements,__tmp,psNull);                          
00393                                 }
00394                                 else
00395                                 {             
00396                                         ImageExt<short>& imageExt 
00397                                                         = dynamic_cast<ImageExt<short>&>(*this);
00398                                         std::valarray<short> __tmp; 
00399                                         FITSUtil::fill(__tmp,data);                                        
00400                                         short *psNull=0;
00401                                         short sNull=0;
00402                                         if (nulValue)
00403                                         {
00404                                            sNull = static_cast<short>(*nulValue);
00405                                            psNull = &sNull;
00406                                         }                                        
00407                                         imageExt.writeImage(first,nElements,__tmp,psNull);                          
00408                                 } 
00409                         }        
00410                         else
00411                         {
00412                                 FITSUtil::MatchType<S> errType;                                
00413                                 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00414                         }        
00415                 }
00416         }
00417 
00418         template <typename S>
00419         void ExtHDU::write(long first,
00420                             long nElements,const std::valarray<S>& data)
00421         {
00422            write(first, nElements, data, static_cast<S*>(0));                
00423         }
00424 
00425         template <typename S>
00426         void ExtHDU::write(const std::vector<long>& first,
00427                         long nElements,
00428                         const std::valarray<S>& data,
00429                         S* nulValue)
00430         {        
00431                 // throw if we called image read/write operations on a table.
00432                 makeThisCurrent();
00433                 size_t n(first.size());
00434                 long firstElement(0);
00435                 long dimSize(1);
00436                 for (long i = 0; i < n; ++i)
00437                 {
00438                         firstElement +=  ((first[i] - 1)*dimSize);
00439                         dimSize *=naxes(i);   
00440                 }       
00441                 ++firstElement;
00442 
00443                 write(firstElement,nElements,data,nulValue);
00444         }
00445 
00446         template <typename S>
00447         void ExtHDU::write(const std::vector<long>& first,
00448                         long nElements,
00449                         const std::valarray<S>& data)
00450         {        
00451                 // throw if we called image read/write operations on a table.
00452                 makeThisCurrent();
00453                 size_t n(first.size());
00454                 long firstElement(0);
00455                 long dimSize(1);
00456                 for (long i = 0; i < n; ++i)
00457                 {
00458 
00459                         firstElement +=  ((first[i] - 1)*dimSize);
00460                         dimSize *=naxes(i);   
00461                 }       
00462                 ++firstElement;
00463 
00464                 write(firstElement,nElements,data);                     
00465         }        
00466 
00467 
00468         template <typename S>
00469         void ExtHDU::write(const std::vector<long>& firstVertex,
00470                         const std::vector<long>& lastVertex,
00471                         const std::valarray<S>& data)
00472         {
00473                 makeThisCurrent();
00474                 if (ImageExt<S>* extimage = dynamic_cast<ImageExt<S>*>(this))
00475                 {
00476                         extimage->writeImage(firstVertex,lastVertex,data);  
00477                 }
00478                 else
00479                 {
00480                          // write input type S to Image type...
00481 
00482                         if (bitpix() == Ifloat)
00483                         {
00484                                 ImageExt<float>& extimage = dynamic_cast<ImageExt<float>&>(*this);
00485                                 size_t n(data.size());
00486                                 std::valarray<float> __tmp(n);
00487                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00488                                 extimage.writeImage(firstVertex,lastVertex,__tmp);
00489 
00490                         }
00491                         else if (bitpix() == Idouble)
00492                         {
00493                                 ImageExt<double>& extimage 
00494                                         = dynamic_cast<ImageExt<double>&>(*this);
00495                                 size_t n(data.size());
00496                                 std::valarray<double> __tmp(n);
00497                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00498                                 extimage.writeImage(firstVertex,lastVertex,__tmp);
00499                         }
00500                         else if (bitpix() == Ibyte)
00501                         {
00502                                 ImageExt<unsigned char>& extimage 
00503                                         = dynamic_cast<ImageExt<unsigned char>&>(*this);
00504                                 size_t n(data.size());
00505                                 std::valarray<unsigned char> __tmp(n);
00506                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00507                                 extimage.writeImage(firstVertex,lastVertex,__tmp);                        
00508                         } 
00509                         else if (bitpix() == Ilong)
00510                         {
00511                                 if ( zero() == ULBASE && scale() == 1)
00512                                 {                                
00513                                         ImageExt<unsigned INT32BIT>& extimage 
00514                                                 = dynamic_cast<ImageExt<unsigned INT32BIT>&>(*this);
00515                                         size_t n(data.size());
00516                                         std::valarray<unsigned INT32BIT> __tmp(n);
00517                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00518                                         extimage.writeImage(firstVertex,lastVertex,__tmp);    
00519                                 }
00520                                 else                              
00521                                 {
00522                                         ImageExt<INT32BIT>& extimage 
00523                                                         = dynamic_cast<ImageExt<INT32BIT>&>(*this);
00524                                         size_t n(data.size());
00525                                         std::valarray<INT32BIT> __tmp(n);
00526                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00527                                         extimage.writeImage(firstVertex,lastVertex,__tmp);
00528                                 }                              
00529                         }    
00530                         else if (bitpix() == Ilonglong)
00531                         {
00532                                 ImageExt<LONGLONG>& extimage 
00533                                         = dynamic_cast<ImageExt<LONGLONG>&>(*this);
00534                                 size_t n(data.size());
00535                                 std::valarray<LONGLONG> __tmp(n);
00536                                 for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00537                                 extimage.writeImage(firstVertex,lastVertex,__tmp);                        
00538                         } 
00539                         else if (bitpix() == Ishort)
00540                         {
00541                                 if ( zero() == USBASE && scale() == 1)
00542                                 {
00543                                         ImageExt<unsigned short>& extimage 
00544                                                 = dynamic_cast<ImageExt<unsigned short>&>(*this);
00545                                         size_t n(data.size());
00546                                         std::valarray<unsigned short> __tmp(n);
00547                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00548                                         extimage.writeImage(firstVertex,lastVertex,__tmp);  
00549                                 }
00550                                 else
00551                                 {                    
00552                                         ImageExt<short>& extimage 
00553                                                         = dynamic_cast<ImageExt<short>&>(*this);
00554                                         size_t n(data.size());
00555                                         std::valarray<short> __tmp(n);
00556                                         for (size_t j= 0; j < n; ++j) __tmp[j] = data[j];
00557                                         extimage.writeImage(firstVertex,lastVertex,__tmp);     
00558                                 }                             
00559                         }          
00560                         else
00561                         {
00562                                 FITSUtil::MatchType<S> errType;                                
00563                                 throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType()));
00564                         }        
00565                 }  
00566         }  
00567 
00568 
00569 } //namespace CCfits
00570 
00571 #endif