ColumnT.h

00001 //1
00002 //2
00003 //3
00004 //4
00005 
00006 //      This is version 1.8 release dated Oct 2007
00007 
00008 //      Astrophysics Science Division,
00009 //      NASA/ Goddard Space Flight Center
00010 //      HEASARC
00011 //      http://heasarc.gsfc.nasa.gov
00012 //      e-mail: ccfits@legacy.gsfc.nasa.gov
00013 //
00014 //      Original author: Ben Dorman, L3-Communications EER Systems Inc.
00015 
00016 #ifndef COLUMNT_H
00017 #define COLUMNT_H
00018 
00019 #ifdef _MSC_VER
00020 #include "MSconfig.h"
00021 #endif
00022 
00023 #include "ColumnData.h"
00024 #include "ColumnVectorData.h"
00025 #include "FITSUtil.h"
00026 #include <typeinfo>
00027 #include <vector>
00028 #include <algorithm>
00029 #include "NewKeyword.h"
00030 
00031 #ifdef SSTREAM_DEFECT
00032 #       include <strstream>
00033 #else
00034 #       include <sstream>
00035 #endif
00036 
00037 
00038 // by design, if the data are not read yet we will return an exception.
00039 // here the test is if the entire column has already been read. 
00040 using std::complex;
00041 using std::valarray;
00042 
00043 // get specified elements of a scalar column. These two functions allow the
00044 // user to return either a vector or a valarray depending on the input container.
00045 
00046 namespace CCfits 
00047 {
00048         template <typename S>
00049         void Column::read(std::vector<S>& vals, long first, long last) 
00050         {
00051                 read(vals,first,last,static_cast<S*>(0));
00052         }
00053 
00054 
00055         template <typename S>
00056         void Column::read(std::vector<S>& vals, long first, long last, S* nullValue) 
00057         {
00058                 // problem: S does not give the type of the Column, but the return type,
00059                 // so the user must specify this.
00060                 parent()->makeThisCurrent();
00061                 long nelements = numberOfElements(first,last);
00062 
00063                 if  (ColumnData<S>* col = dynamic_cast<ColumnData<S>*>(this))
00064                 {
00065                         // fails if user requested outputType different from input type.
00066 
00067 
00068                         if (!isRead()) col->readColumnData(first,nelements,nullValue);
00069                         // scalar column with vector output can just be assigned.
00070                         FITSUtil::fill(vals,col->data(),first,last);
00071                 }
00072                 else
00073                 {
00074                         FITSUtil::MatchType<S> outputType;
00075                         if ( outputType() == type() ) 
00076                         { 
00077                                 // in this case user tried to read vector data from scalar,
00078                                 // (i.e. first argument was vector<valarray<S> >.
00079                                 // since the cast won't fail on template parameter grounds.
00080                                 throw Column::WrongColumnType(name());
00081                         }
00082 
00083                         try
00084                         {
00085                             // about exceptions. The dynamic_casts could throw
00086                             // std::bad_cast. If this happens something is seriously
00087                             // wrong since the Column stores the value of type() 
00088                             // appropriate to each of the casts on construction.
00089                             // 
00090                             // the InvalidDataType exception should not be possible.
00091                             if  ( type() == Tdouble )
00092                             {
00093                                     ColumnData<double>& col 
00094                                               = dynamic_cast<ColumnData<double>&>(*this);
00095                                     if (!isRead()) col.readColumnData(first,nelements);                                  
00096                                     FITSUtil::fill(vals,col.data(),first,last);
00097 
00098                             }
00099                             else if (type() == Tfloat)
00100                             {
00101                                     ColumnData<float>& col 
00102                                             = dynamic_cast<ColumnData<float>&>(*this);
00103                                     if (!isRead()) col.readColumnData(first,nelements);                                  
00104                                     FITSUtil::fill(vals,col.data(),first,last);
00105                             }
00106                             else if (type() == Tint)
00107                             {
00108                                     int nullVal(0);
00109                                     if (nullValue) nullVal = static_cast<int>(*nullValue);
00110                                     ColumnData<int>& col  
00111                                        = dynamic_cast<ColumnData<int>&>(*this);
00112                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00113                                     FITSUtil::fill(vals,col.data(),first,last);
00114                             }
00115                             else if (type() == Tshort)
00116                             {
00117                                     short nullVal(0);
00118                                     if (nullValue) nullVal = static_cast<short>(*nullValue);
00119                                     ColumnData<short>& col 
00120                                      = dynamic_cast<ColumnData<short>&>(*this);
00121                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00122                                     FITSUtil::fill(vals,col.data(),first,last);
00123                             }
00124                             else if (type() == Tlong)
00125                             {   
00126                                     long nullVal(0);
00127                                     if (nullValue) nullVal = static_cast<long>(*nullValue); 
00128                                     ColumnData<long>& col 
00129                                       = dynamic_cast<ColumnData<long>&>(*this);
00130                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00131                                     FITSUtil::fill(vals,col.data(),first,last);
00132                             }
00133                             else if (type() == Tlonglong)
00134                             {   
00135                                     LONGLONG nullVal(0);
00136                                     if (nullValue) nullVal = static_cast<LONGLONG>(*nullValue); 
00137                                     ColumnData<LONGLONG>& col 
00138                                       = dynamic_cast<ColumnData<LONGLONG>&>(*this);
00139                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00140                                     FITSUtil::fill(vals,col.data(),first,last);
00141                             }
00142                             else if (type() == Tlogical)
00143                             {   
00144                                     bool nullVal(0);
00145                                     if (nullValue) nullVal = static_cast<bool>(*nullValue); 
00146                                     ColumnData<bool>& col 
00147                                       = dynamic_cast<ColumnData<bool>&>(*this);
00148                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00149                                     FITSUtil::fill(vals,col.data(),first,last);
00150                             }
00151                             else if (type() == Tbit || type() == Tbyte)
00152                             {
00153                                     unsigned char nullVal(0);
00154                                     if (nullValue) nullVal = static_cast<unsigned char>(*nullValue); 
00155                                     ColumnData<unsigned char>& col 
00156                                             = dynamic_cast<ColumnData<unsigned char>&>(*this);
00157                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal); 
00158                                     FITSUtil::fill(vals,col.data(),first,last);
00159                             }
00160                             else if (type() == Tushort)
00161                             {
00162                                     unsigned short nullVal(0);
00163                                     if (nullValue) nullVal= static_cast<unsigned short>(*nullValue);
00164                                     ColumnData<unsigned short>& col 
00165                                             = dynamic_cast<ColumnData<unsigned short>&>(*this);
00166                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00167                                     FITSUtil::fill(vals,col.data(),first,last);
00168                             }
00169                             else if (type() == Tuint)
00170                             {
00171                                     unsigned int nullVal(0);
00172                                     if (nullValue) nullVal = static_cast<unsigned int>(*nullValue);
00173                                     ColumnData<unsigned int>& col 
00174                                             = dynamic_cast<ColumnData<unsigned int>&>(*this);
00175                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00176                                     FITSUtil::fill(vals,col.data(),first,last);
00177                             }
00178                             else if (type() == Tulong)
00179                             {
00180                                     unsigned long nullVal(0);
00181                                     if (nullValue) nullVal = static_cast<unsigned long>(*nullValue);
00182                                     ColumnData<unsigned long>& col 
00183                                              = dynamic_cast<ColumnData<unsigned long>&>(*this);
00184                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                  
00185                                     FITSUtil::fill(vals,col.data(),first,last);
00186                             }
00187                             else
00188                             {
00189                                       throw InvalidDataType(name());
00190 
00191                             }
00192 
00193                         }
00194                         catch (std::bad_cast)
00195                         {
00196                                 throw WrongColumnType(name());
00197                         }
00198                 }
00199 
00200         }
00201 
00202         template <typename S>
00203         void Column::read(std::valarray<S>& vals, long first, long last) 
00204         {
00205                 read(vals,first,last,static_cast<S*>(0));
00206         }
00207 
00208 
00209         template <typename S>
00210         void Column::read(std::valarray<S>& vals, long first, long last, S* nullValue) 
00211         {        
00212                 // require the whole scalar column to have been read.
00213 
00214 
00215                 long nelements = numberOfElements(first,last);
00216                 parent()->makeThisCurrent();                
00217                 if ( ColumnData<S>* col = dynamic_cast<ColumnData<S>*>(this))
00218                 {
00219                         // fails if user requested outputType different from input type.
00220 
00221 
00222                         if (!isRead()) col->readColumnData(first,nelements,nullValue);                                  
00223                         FITSUtil::fill(vals,col->data(),first,last);
00224 
00225                 }
00226                 else
00227                 {
00228                         FITSUtil::MatchType<S> outputType;
00229                         if ( outputType() == type() ) 
00230                         { 
00231                                 // in this case user tried to read vector data from scalar,
00232                                 // (i.e. first argument was vector<valarray<S> >.
00233                                 // since the cast won't fail on template parameter grounds.
00234                                 throw Column::WrongColumnType(name());
00235                         }
00236 
00237                         try
00238                         {
00239                             // about exceptions. The dynamic_casts could throw
00240                             // std::bad_cast. If this happens something is seriously
00241                             // wrong since the Column stores the value of type() 
00242                             // appropriate to each of the casts on construction.
00243                             // 
00244                             // the InvalidDataType exception should not be possible.
00245                             if  ( type() == Tdouble )
00246                             {
00247                                     ColumnData<double>& col 
00248                                               = dynamic_cast<ColumnData<double>&>(*this);
00249                                     if (!isRead()) col.readColumnData(first,nelements);                                  
00250                                     FITSUtil::fill(vals,col.data(),first,last);
00251                             }
00252                             else if (type() == Tfloat)
00253                             {
00254                                     ColumnData<float>& col 
00255                                             = dynamic_cast<ColumnData<float>&>(*this);
00256                                     if (!isRead()) col.readColumnData(first,nelements);                                  
00257                                     FITSUtil::fill(vals,col.data(),first,last);
00258                             }
00259                             else if (type() == Tint)
00260                             {
00261                                     int nullVal(0);
00262                                     if (nullValue) nullVal = static_cast<int>(*nullValue); 
00263                                     ColumnData<int>& col  
00264                                             = dynamic_cast<ColumnData<int>&>(*this);
00265                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00266                                     FITSUtil::fill(vals,col.data(),first,last);
00267                             }
00268                             else if (type() == Tshort)
00269                             {
00270                                     short nullVal(0);
00271                                     if (nullValue) nullVal = static_cast<short>(*nullValue); 
00272                                     ColumnData<short>& col 
00273                                             = dynamic_cast<ColumnData<short>&>(*this);
00274                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00275                                     FITSUtil::fill(vals,col.data(),first,last);
00276                             }
00277                             else if (type() == Tlong)
00278                             {   
00279                                     long nullVal(0);
00280                                     if (nullValue) nullVal = static_cast<long>(*nullValue); 
00281                                     ColumnData<long>& col 
00282                                             = dynamic_cast<ColumnData<long>&>(*this);
00283                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);
00284                                     FITSUtil::fill(vals,col.data(),first,last);
00285                             }
00286                             else if (type() == Tlonglong)
00287                             {   
00288                                     LONGLONG nullVal(0);
00289                                     if (nullValue) nullVal = static_cast<LONGLONG>(*nullValue); 
00290                                     ColumnData<LONGLONG>& col 
00291                                             = dynamic_cast<ColumnData<LONGLONG>&>(*this);
00292                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);
00293                                     FITSUtil::fill(vals,col.data(),first,last);
00294                             }
00295                             else if (type() == Tlogical)
00296                             {   
00297                                     bool nullVal(0);
00298                                     if (nullValue) nullVal = static_cast<bool>(*nullValue); 
00299                                     ColumnData<bool>& col 
00300                                             = dynamic_cast<ColumnData<bool>&>(*this);
00301                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00302                                     FITSUtil::fill(vals,col.data(),first,last);
00303                             }
00304                             else if (type() == Tbit || type() == Tbyte)
00305                             {
00306                                     unsigned char nullVal(0);
00307                                     if (nullValue) nullVal = static_cast<unsigned char>(*nullValue); 
00308                                     ColumnData<unsigned char>& col 
00309                                             = dynamic_cast<ColumnData<unsigned char>&>(*this);
00310                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00311                                     FITSUtil::fill(vals,col.data(),first,last);
00312                             }
00313                             else if (type() == Tushort)
00314                             {
00315                                     unsigned short nullVal(0);
00316                                     if (nullValue) nullVal 
00317                                             = static_cast<unsigned short>(*nullValue); 
00318                                     ColumnData<unsigned short>& col 
00319                                             = dynamic_cast<ColumnData<unsigned short>&>(*this);
00320                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00321                                     FITSUtil::fill(vals,col.data(),first,last);
00322                             }
00323                             else if (type() == Tuint)
00324                             {
00325                                     unsigned int nullVal(0);
00326                                     if (nullValue) nullVal 
00327                                             = static_cast<unsigned int>(*nullValue); 
00328                                     ColumnData<unsigned int>& col 
00329                                             = dynamic_cast<ColumnData<unsigned int>&>(*this);
00330                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00331                                     FITSUtil::fill(vals,col.data(),first,last);
00332                             }
00333                             else if (type() == Tulong)
00334                             {
00335                                     unsigned long nullVal(0);
00336                                     if (nullValue) nullVal 
00337                                             = static_cast<unsigned long>(*nullValue); 
00338                                     ColumnData<unsigned long>& col 
00339                                             = dynamic_cast<ColumnData<unsigned long>&>(*this);
00340                                     if (!isRead()) col.readColumnData(first,nelements,&nullVal);                                    
00341                                     FITSUtil::fill(vals,col.data(),first,last);
00342                             }
00343                             else
00344                             {
00345                                       throw InvalidDataType(name());
00346 
00347                             }
00348 
00349                         }
00350                         catch (std::bad_cast)
00351                         {
00352                              throw WrongColumnType(name());
00353                         }
00354                     }
00355 
00356         }
00357 
00358         // get a single row from a vector column. There's no default row number, must
00359         // be specified.
00360         template <typename S>
00361         void Column::read(std::valarray<S>& vals, long row) 
00362         {
00363                 read(vals,row,static_cast<S*>(0));
00364         }
00365 
00366 
00367         template <typename S>
00368         void Column::read(std::valarray<S>& vals, long row, S* nullValue) 
00369         {
00370                 if (row > parent()->rows())
00371                 {
00372                    throw Column::InvalidRowNumber(name());
00373                 }
00374                 parent()->makeThisCurrent();                
00375                 // isRead() returns true if the data were read in the ctor.
00376                 if ( ColumnVectorData<S>* col = dynamic_cast<ColumnVectorData<S>*>(this))
00377                 {
00378                         // fails if user requested outputType different from input type.
00379 
00380 
00381 
00382                         // input and output are both valarrays. Since one should not
00383                         // be able to call a constructor for a non-numeric valarray type,
00384                         // there shouldn't be any InvalidType problems. However, there
00385                         // is still the vector/scalar possibility and the implicit
00386                         // conversion request to deal with.
00387 
00388                         if (!isRead()) col->readRow(row,nullValue);
00389                         FITSUtil::fill(vals,col->data(row));
00390                 }
00391                 else
00392                 {
00393                         FITSUtil::MatchType<S> outputType;
00394                         if ( outputType() == type() ) 
00395                         { 
00396                                 // in this case user tried to read vector row from scalar column.
00397                                 // one could be charitable and return a valarray of size 1,
00398                                 // but... I'm going to throw an exception suggesting the user
00399                                 // might not have meant that.
00400 
00401                                 throw Column::WrongColumnType(name());
00402                         }
00403 
00404                         // the InvalidDataType exception should not be possible.
00405                         try
00406                         {
00407                             // about exceptions. The dynamic_casts could throw
00408                             // std::bad_cast. If this happens something is seriously
00409                             // wrong since the Column stores the value of type() 
00410                             // appropriate to each of the casts on construction.
00411                             // 
00412                             // the InvalidDataType exception should not be possible.
00413                             if  ( type() == Tdouble || type() == VTdouble )
00414                             {
00415                                     ColumnVectorData<double>& col 
00416                                               = dynamic_cast<ColumnVectorData<double>&>(*this);
00417                                     if (!isRead()) col.readRow(row);                                  
00418                                     FITSUtil::fill(vals,col.data(row));
00419 
00420                             }
00421                             else if (type() == Tfloat  || type() == VTfloat )
00422                             { 
00423                                     ColumnVectorData<float>& col 
00424                                           = dynamic_cast<ColumnVectorData<float>&>(*this);
00425                                     if (!isRead()) col.readRow(row); 
00426                                     FITSUtil::fill(vals,col.data(row));
00427                             }
00428                             else if (type() == Tint  || type() == VTint )
00429                             {
00430                                     int nullVal(0);
00431                                     if (nullValue) nullVal = static_cast<int>(*nullValue); 
00432                                     ColumnVectorData<int>& col  
00433                                             = dynamic_cast<ColumnVectorData<int>&>(*this);
00434                                     if (!isRead()) col.readRow(row,&nullVal); 
00435                                     FITSUtil::fill(vals,col.data(row));
00436                             }
00437                             else if (type() == Tshort  || type() == VTshort  )
00438                             {
00439                                     short nullVal(0);
00440                                     if (nullValue) nullVal = static_cast<short>(*nullValue); 
00441                                     ColumnVectorData<short>& col 
00442                                             = dynamic_cast<ColumnVectorData<short>&>(*this);
00443                                     if (!isRead()) col.readRow(row,&nullVal); 
00444                                     FITSUtil::fill(vals,col.data(row));
00445                             }
00446                             else if (type() == Tlong  || type() == VTlong )
00447                             {   
00448                                     long nullVal(0);
00449                                     if (nullValue) nullVal = static_cast<long>(*nullValue); 
00450                                     ColumnVectorData<long>& col 
00451                                             = dynamic_cast<ColumnVectorData<long>&>(*this);
00452                                     if (!isRead()) col.readRow(row,&nullVal); 
00453                                     FITSUtil::fill(vals,col.data(row));
00454                             }
00455                             else if (type() == Tlonglong  || type() == VTlonglong )
00456                             {   
00457                                     LONGLONG nullVal(0);
00458                                     if (nullValue) nullVal = static_cast<LONGLONG>(*nullValue); 
00459                                     ColumnVectorData<LONGLONG>& col 
00460                                             = dynamic_cast<ColumnVectorData<LONGLONG>&>(*this);
00461                                     if (!isRead()) col.readRow(row,&nullVal); 
00462                                     FITSUtil::fill(vals,col.data(row));
00463                             }
00464                             else if (type() == Tlogical  || type() == VTlogical )
00465                             {   
00466                                     bool nullVal(0);
00467                                     if (nullValue) nullVal = static_cast<bool>(*nullValue); 
00468                                     ColumnVectorData<bool>& col 
00469                                             = dynamic_cast<ColumnVectorData<bool>&>(*this);
00470                                     if (!isRead()) col.readRow(row,&nullVal); 
00471                                     FITSUtil::fill(vals,col.data(row));
00472                             }
00473                             else if (type() == Tbit || type() == Tbyte ||  
00474                                     type() == VTbit || type() == VTbyte )
00475                             {
00476                                     unsigned char nullVal(0);
00477                                     if (nullValue) nullVal 
00478                                                 = static_cast<unsigned char>(*nullValue); 
00479                                     ColumnVectorData<unsigned char>& col 
00480                                           = dynamic_cast<ColumnVectorData<unsigned char>&>(*this);
00481                                     if (!isRead()) col.readRow(row,&nullVal); 
00482                                     FITSUtil::fill(vals,col.data(row));
00483                             }
00484                             else if (type() == Tushort || type() == VTushort)
00485                             {
00486                                     unsigned short nullVal(0);
00487                                     if (nullValue) nullVal 
00488                                                 = static_cast<unsigned short>(*nullValue); 
00489                                     ColumnVectorData<unsigned short>& col 
00490                                           = dynamic_cast<ColumnVectorData<unsigned short>&>(*this);
00491                                     if (!isRead()) col.readRow(row,&nullVal); 
00492                                     FITSUtil::fill(vals,col.data(row));
00493                             }
00494                             else if (type() == Tuint || type() == VTuint)
00495                             {
00496                                     unsigned int nullVal(0);
00497                                     if (nullValue) nullVal 
00498                                                 = static_cast<unsigned int>(*nullValue); 
00499                                     ColumnVectorData<unsigned int>& col 
00500                                           = dynamic_cast<ColumnVectorData<unsigned int>&>(*this);
00501                                     if (!isRead()) col.readRow(row,&nullVal); 
00502                                     FITSUtil::fill(vals,col.data(row));
00503                             }
00504                             else if (type() == Tulong || type() == VTulong)
00505                             {
00506                                     unsigned long nullVal(0);
00507                                     if (nullValue) nullVal 
00508                                                 = static_cast<unsigned long>(*nullValue); 
00509                                     ColumnVectorData<unsigned long>& col 
00510                                             = dynamic_cast<ColumnVectorData<unsigned long>&>(*this);
00511                                     if (!isRead()) col.readRow(row,&nullVal); 
00512                                     FITSUtil::fill(vals,col.data(row));
00513                             }
00514                             else
00515                             {
00516                                     throw InvalidDataType(name());
00517 
00518                             }
00519 
00520                         }
00521                         catch (std::bad_cast)
00522                         {
00523                             throw WrongColumnType(name());
00524                         }     
00525                  }
00526         }
00527 
00528         template <typename S>
00529         void Column::readArrays(std::vector<std::valarray<S> >& vals, long first, long last)  
00530         {
00531                 readArrays(vals,first,last,static_cast<S*>(0));
00532         }
00533 
00534         template <typename S>
00535         void Column::readArrays(std::vector<std::valarray<S> >& vals, 
00536                                 long first, long last, S* nullValue)
00537         {
00538 
00539                 parent()->makeThisCurrent();
00540                 // again, can only call this if the entire column has been read from disk.
00541                 // user expects 1 based indexing. If 0 based indices are supplied,
00542                 // add one to both ranges.
00543                 long range = numberOfElements(first,last);
00544 
00545                 vals.resize(range);
00546 
00547 
00548                 if ( ColumnVectorData<S>* col = dynamic_cast<ColumnVectorData<S>*>(this))
00549                 {
00550                         for (int j = 0; j < range; ++j) 
00551                         {
00552                                 if (!isRead()) col->readRow(j + first,nullValue);                             
00553                                 FITSUtil::fill(vals[j],col->data(j+first));
00554                         }
00555                 }
00556                 else
00557                 {
00558                         FITSUtil::MatchType<S> outputType;
00559                         if ( outputType() == type() ) 
00560                         { 
00561                                 // in this case user tried to read vector data from scalar,
00562                                 // (i.e. first argument was vector<valarray<S> >.
00563                                 // since the cast won't fail on template parameter grounds.
00564                                 throw Column::WrongColumnType(name());
00565                         }
00566                         // the InvalidDataType exception should not be possible.
00567                         try
00568                         {
00569                             if  ( type() == Tdouble || type() == VTdouble )
00570                             {
00571                                     ColumnVectorData<double>& col 
00572                                             = dynamic_cast<ColumnVectorData<double>&>(*this);
00573                                     for (int j = 0; j < range; ++j) 
00574                                     {
00575                                         if (!isRead()) col.readRow(j + first); 
00576                                         FITSUtil::fill(vals[j],col.data(j+first));
00577                                     }
00578                             }
00579                             else if  ( type() == Tfloat || type() == VTfloat  )
00580                             {
00581                                     ColumnVectorData<float>& col 
00582                                             = dynamic_cast<ColumnVectorData<float>&>(*this);
00583                                     for (int j = 0; j < range; ++j) 
00584                                     {
00585                                         if (!isRead()) col.readRow(j + first); 
00586                                         FITSUtil::fill(vals[j],col.data(j+first));
00587                                     }
00588                             }
00589                             else if  ( type() == Tint   || type() == VTint )
00590                             {
00591                                     int nullVal(0);
00592                                     if (nullValue) nullVal  = static_cast<int>(*nullValue); 
00593                                     ColumnVectorData<int>& col  
00594                                             = dynamic_cast<ColumnVectorData<int>&>(*this);
00595                                     for (int j = 0; j < range; ++j) 
00596                                     {
00597                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00598                                         FITSUtil::fill(vals[j],col.data(j+first));
00599                                     }
00600                             }
00601                             else if  ( type() == Tshort  || type() == VTshort )
00602                             {
00603                                     short nullVal(0);
00604                                     if (nullValue) nullVal  = static_cast<short>(*nullValue); 
00605                                     ColumnVectorData<short>& col 
00606                                             = dynamic_cast<ColumnVectorData<short>&>(*this);
00607                                     for (int j = 0; j < range; ++j) 
00608                                     {
00609                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00610                                         FITSUtil::fill(vals[j],col.data(j+first));
00611                                     }
00612                             }
00613                             else if  ( type() == Tlong   || type() == VTlong )
00614                             {
00615                                     long nullVal(0);
00616                                     if (nullValue) nullVal  = static_cast<long>(*nullValue); 
00617                                     ColumnVectorData<long>& col 
00618                                             = dynamic_cast<ColumnVectorData<long>&>(*this);
00619                                     for (int j = 0; j < range; ++j) 
00620                                     {
00621                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00622                                         FITSUtil::fill(vals[j],col.data(j+first));
00623                                     }
00624                             }
00625                             else if  ( type() == Tlonglong   || type() == VTlonglong )
00626                             {
00627                                     LONGLONG nullVal(0);
00628                                     if (nullValue) nullVal  = static_cast<LONGLONG>(*nullValue); 
00629                                     ColumnVectorData<LONGLONG>& col 
00630                                             = dynamic_cast<ColumnVectorData<LONGLONG>&>(*this);
00631                                     for (int j = 0; j < range; ++j) 
00632                                     {
00633                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00634                                         FITSUtil::fill(vals[j],col.data(j+first));
00635                                     }
00636                             }
00637                             else if  ( type() == Tlogical   || type() == VTlogical )
00638                             {
00639                                     bool nullVal(0);
00640                                     if (nullValue) nullVal   = static_cast<bool>(*nullValue); 
00641                                     ColumnVectorData<bool>& col 
00642                                             = dynamic_cast<ColumnVectorData<bool>&>(*this);
00643                                     for (int j = 0; j < range; ++j) 
00644                                     {
00645                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00646                                         FITSUtil::fill(vals[j],col.data(j+first));
00647                                     }
00648                             }
00649                             else if (type() == Tbit || type() == Tbyte ||  
00650                                     type() == VTbit || type() == VTbyte )
00651                             {
00652                                     unsigned char nullVal(0);
00653                                     if (nullValue) nullVal 
00654                                                 = static_cast<unsigned char>(*nullValue); 
00655                                     ColumnVectorData<unsigned char>& col 
00656                                            = dynamic_cast<ColumnVectorData<unsigned char>&>(*this);
00657                                     for (int j = 0; j < range; ++j) 
00658                                     {
00659                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00660                                         FITSUtil::fill(vals[j],col.data(j+first));
00661                                     }
00662                             }                            
00663                             else if  ( type() == Tushort   || type() == VTushort )
00664                             {
00665                                     unsigned short nullVal(0);
00666                                     if (nullValue) nullVal 
00667                                                 = static_cast<unsigned short>(*nullValue); 
00668                                     ColumnVectorData<unsigned short>& col 
00669                                         = dynamic_cast<ColumnVectorData<unsigned short>&>(*this);
00670                                     for (int j = 0; j < range; ++j) 
00671                                     {
00672                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00673                                         FITSUtil::fill(vals[j],col.data(j+first));
00674                                     }
00675                             }
00676                             else if  ( type() == Tuint   || type() == VTuint )
00677                             {
00678                                     unsigned int nullVal(0);
00679                                     if (nullValue) nullVal 
00680                                                 = static_cast<unsigned int>(*nullValue); 
00681                                     ColumnVectorData<unsigned int>& col 
00682                                             = dynamic_cast<ColumnVectorData<unsigned int>&>(*this);
00683                                     for (int j = 0; j < range; ++j) 
00684                                     {
00685                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00686                                         FITSUtil::fill(vals[j],col.data(j+first));
00687                                     }
00688                             }
00689                             else if  ( type() == Tulong   || type() == VTulong  )
00690                             {
00691                                     unsigned long nullVal(0);
00692                                     if (nullValue) nullVal 
00693                                                 = static_cast<unsigned long>(*nullValue); 
00694                                     ColumnVectorData<unsigned long>& col 
00695                                           = dynamic_cast<ColumnVectorData<unsigned long>&>(*this);
00696                                     for (int j = 0; j < range; ++j) 
00697                                     {
00698                                         if (!isRead()) col.readRow(j + first,&nullVal); 
00699                                         FITSUtil::fill(vals[j],col.data(j+first));
00700                                     }
00701                             } 
00702                             else
00703                             {
00704                                     throw InvalidDataType(name());
00705                             }
00706 
00707                         }
00708                         catch (std::bad_cast)
00709                         {
00710                             throw WrongColumnType(name());
00711 
00712                         }     
00713 
00714                 }        
00715         }
00716 
00717         template <typename S>                   
00718         void Column::write (const std::vector<S>& indata, long firstRow)
00719         {
00720                 // nullValue is now a pointer, so this is ok. 
00721                 // got to cast the 0 to a pointer to S to avoid
00722                 // overloading ambiguities.      
00723                 write(indata,firstRow,static_cast<S*>(0));
00724         }
00725 
00726         template <typename S>                   
00727         void Column::write (const std::valarray<S>& indata, long firstRow)
00728         {
00729                 size_t n(indata.size());
00730                 std::vector<S> __tmp(n);
00731                 for (size_t j = 0; j < n; ++j) __tmp[j] = indata[j];
00732                 write(__tmp,firstRow,static_cast<S*>(0));
00733         }
00734 
00735         template <typename S>                   
00736         void Column::write (S* indata, long nRows, long firstRow)
00737         {
00738                 write(indata,nRows,firstRow,static_cast<S*>(0));                
00739         }
00740 
00741 
00742         template <typename S>                   
00743         void Column::write (const std::vector<S>& indata, long firstRow, S* nullValue)
00744         {
00745                 // although underlying code needs to convert the input vector
00746                 // into a C array, this must be the underlying implementation
00747                 // [which the others call] because it accepts string arguments
00748                 // which the version with a pointer won't. [no version that
00749                 // translates to a char** argument].
00750 
00751 
00752                 parent()->makeThisCurrent();
00753                 firstRow = std::max(firstRow,static_cast<long>(1));
00754                 if (ColumnData<S>* col = dynamic_cast<ColumnData<S>*>(this))
00755                 {
00756                         col->writeData(indata,firstRow,nullValue);
00757                 }
00758                 else
00759                 {
00760                         // alright, input data type has to be rewritten as output
00761                         // data type.
00762                         FITSUtil::MatchType<S> inType;
00763                         if ( inType() == type()) 
00764                         {
00765                                 String msg("Incorrect call: writing to vector column ");
00766                                 msg += name();
00767                                 msg += " requires specification of # rows or vector lengths";
00768                                 throw WrongColumnType(msg);
00769                         }
00770                         else
00771                         {
00772                             if  ( type() == Tdouble )
00773                             {
00774                                 ColumnData<double>& col 
00775                                         = dynamic_cast<ColumnData<double>&>(*this);
00776                                 std::vector<double> __tmp;
00777                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00778                                 col.writeData(__tmp,firstRow);
00779                             }
00780                             else if  ( type() == Tfloat )
00781                             {
00782                                 ColumnData<float>& col 
00783                                         = dynamic_cast<ColumnData<float>&>(*this);
00784                                 std::vector<float> __tmp;
00785                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00786                                 col.writeData(__tmp,firstRow);
00787                             }
00788                             else if  ( type() == Tint )
00789                             {
00790                                 int nullVal(0);
00791                                 if (nullValue) nullVal = static_cast<int>(*nullValue); 
00792                                 ColumnData<int>& col  
00793                                         = dynamic_cast<ColumnData<int>&>(*this);
00794                                 std::vector<int> __tmp;
00795                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00796                                 col.writeData(__tmp,firstRow,&nullVal);
00797                             }
00798                             else if  ( type() == Tshort )
00799                             {
00800                                 short nullVal(0);
00801                                 if (nullValue) nullVal = static_cast<short>(*nullValue); 
00802                                 ColumnData<short>& col 
00803                                         = dynamic_cast<ColumnData<short>&>(*this);
00804                                 std::vector<short> __tmp;
00805                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00806                                 col.writeData(__tmp,firstRow,&nullVal);
00807                             }
00808                             else if  ( type() == Tlong )
00809                             {
00810                                 long nullVal(0);
00811                                 if (nullValue) nullVal = static_cast<long>(*nullValue); 
00812                                 ColumnData<long>& col 
00813                                         = dynamic_cast<ColumnData<long>&>(*this);
00814                                 std::vector<long> __tmp;
00815                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00816                                 col.writeData(__tmp,firstRow,&nullVal);
00817                             }
00818                             else if  ( type() == Tlonglong )
00819                             {
00820                                 LONGLONG nullVal(0);
00821                                 if (nullValue) nullVal = static_cast<LONGLONG>(*nullValue); 
00822                                 ColumnData<LONGLONG>& col 
00823                                         = dynamic_cast<ColumnData<LONGLONG>&>(*this);
00824                                 std::vector<LONGLONG> __tmp;
00825                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00826                                 col.writeData(__tmp,firstRow,&nullVal);
00827                             }
00828                             else if  ( type() == Tlogical )
00829                             {
00830                                 bool nullVal(0);
00831                                 if (nullValue) nullVal = static_cast<bool>(*nullValue); 
00832                                 ColumnData<bool>& col 
00833                                         = dynamic_cast<ColumnData<bool>&>(*this);
00834                                 std::vector<bool> __tmp;
00835                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00836                                 col.writeData(__tmp,firstRow,&nullVal);
00837                             }
00838                             else if  ( type() == Tbyte )
00839                             {
00840                                 unsigned char nullVal(0);
00841                                 if (nullValue) nullVal = static_cast<unsigned char>(*nullValue); 
00842                                 ColumnData<unsigned char>& col 
00843                                         = dynamic_cast<ColumnData<unsigned char>&>(*this);
00844                                 std::vector<unsigned char> __tmp;
00845                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00846                                 col.writeData(__tmp,firstRow,&nullVal);
00847                             }                            
00848                             else if  ( type() == Tushort )
00849                             {
00850                                 unsigned short nullVal(0);
00851                                 if (nullValue) nullVal = static_cast<unsigned short>(*nullValue); 
00852                                 ColumnData<unsigned short>& col 
00853                                         = dynamic_cast<ColumnData<unsigned short>&>(*this);
00854                                 std::vector<unsigned short> __tmp;
00855                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00856                                 col.writeData(__tmp,firstRow,&nullVal);
00857                             }
00858                             else if  ( type() == Tuint )
00859                             {
00860                                 unsigned int nullVal(0);
00861                                 if (nullValue) nullVal = static_cast<unsigned int>(*nullValue); 
00862                                 ColumnData<unsigned int>& col 
00863                                         = dynamic_cast<ColumnData<unsigned int>&>(*this);
00864                                 std::vector<unsigned int> __tmp;
00865                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00866                                 col.writeData(__tmp,firstRow,&nullVal);
00867                             }
00868                             else if  ( type() == Tulong )
00869                             {
00870                                 unsigned long nullVal(0);
00871                                 if (nullValue) nullVal = static_cast<unsigned long>(*nullValue); 
00872                                 ColumnData<unsigned long>& col 
00873                                         = dynamic_cast<ColumnData<unsigned long>&>(*this);
00874                                 std::vector<unsigned long> __tmp;
00875                                 FITSUtil::fill(__tmp,indata,1,indata.size());
00876                                 col.writeData(__tmp,firstRow,&nullVal);
00877                             } 
00878                             else
00879                             {
00880                                     throw InvalidDataType(name());
00881                             }
00882                         }
00883                 }
00884         }
00885 
00886 
00887         template <typename S>                   
00888         void Column::write (const std::valarray<S>& indata, long firstRow, S* nullValue)
00889         {
00890                 // for scalar columns.        
00891                 std::vector<S> __tmp;
00892                 FITSUtil::fill(__tmp,indata);    
00893                 write(__tmp,firstRow,nullValue);          
00894         }
00895 
00896         template <typename S>                   
00897         void Column::write (S* indata, long nRows, long firstRow, S* nullValue)
00898         {
00899                 // for scalar columns, data specified with C array
00900                 if (nRows <= 0) throw InvalidNumberOfRows(nRows);
00901                 std::vector<S> __tmp(nRows);
00902                 std::copy(&indata[0],&indata[nRows],__tmp.begin());
00903                 write(__tmp,firstRow, nullValue);
00904 
00905         }
00906 
00907         template <typename S>
00908         void Column::write (const std::valarray<S>& indata, const std::vector<long>& vectorLengths,  
00909                                 long firstRow)
00910         {
00911                 // variable length arrays written from an input valarray.
00912                 // does not allow NULL value.
00913 
00914                 using std::valarray;
00915                 parent()->makeThisCurrent();
00916                 firstRow = std::max(firstRow,static_cast<long>(1));
00917                 if (ColumnVectorData<S>* col = dynamic_cast<ColumnVectorData<S>*>(this))
00918                 {
00919                         col->writeData(indata,vectorLengths,firstRow);
00920                 }
00921                 else
00922                 {
00923                         // alright, input data type has to be rewritten as output
00924                         // data type.
00925                         FITSUtil::MatchType<S> inType;
00926                         if ( inType() == type()) 
00927                         {
00928                                 String msg("Incorrect call: scalar column ");
00929                                 msg += name();
00930                                 msg += " does not have vector lengths";
00931                                 throw WrongColumnType(msg);
00932                         }
00933                         else
00934                         {
00935                             if  ( type() == Tdouble )
00936                             {
00937                                 ColumnVectorData<double>& col 
00938                                         = dynamic_cast<ColumnVectorData<double>&>(*this);
00939                                 valarray<double> __tmp;
00940                                 FITSUtil::fill(__tmp,indata);
00941                                 col.writeData(__tmp,vectorLengths,firstRow);
00942                             }
00943                             else if  ( type() == Tfloat )
00944                             {
00945                                 ColumnVectorData<float>& col 
00946                                         = dynamic_cast<ColumnVectorData<float>&>(*this);
00947                                 valarray<float> __tmp;
00948                                 FITSUtil::fill(__tmp,indata);
00949                                 col.writeData(__tmp,vectorLengths,firstRow);
00950                             }
00951                             else if  ( type() == Tint )
00952                             {
00953                                 ColumnVectorData<int>& col  
00954                                         = dynamic_cast<ColumnVectorData<int>&>(*this);
00955                                 valarray<int> __tmp;
00956                                 FITSUtil::fill(__tmp,indata);
00957                                 col.writeData(__tmp,vectorLengths,firstRow);
00958                             }
00959                             else if  ( type() == Tshort )
00960                             {
00961                                 ColumnVectorData<short>& col 
00962                                         = dynamic_cast<ColumnVectorData<short>&>(*this);
00963                                 valarray<short> __tmp;
00964                                 FITSUtil::fill(__tmp,indata);
00965                                 col.writeData(__tmp,vectorLengths,firstRow);
00966                             }
00967                             else if  ( type() == Tlong )
00968                             {
00969                                 ColumnVectorData<long>& col 
00970                                         = dynamic_cast<ColumnVectorData<long>&>(*this);
00971                                 valarray<long> __tmp;
00972                                 FITSUtil::fill(__tmp,indata);
00973                                 col.writeData(__tmp,vectorLengths,firstRow);
00974                             }
00975                             else if  ( type() == Tlonglong )
00976                             {
00977                                 ColumnVectorData<LONGLONG>& col 
00978                                         = dynamic_cast<ColumnVectorData<LONGLONG>&>(*this);
00979                                 valarray<LONGLONG> __tmp;
00980                                 FITSUtil::fill(__tmp,indata);
00981                                 col.writeData(__tmp,vectorLengths,firstRow);
00982                             }
00983                             else if  ( type() == Tlogical )
00984                             {
00985                                 ColumnVectorData<bool>& col 
00986                                         = dynamic_cast<ColumnVectorData<bool>&>(*this);
00987                                 valarray<bool> __tmp;
00988                                 FITSUtil::fill(__tmp,indata);
00989                                 col.writeData(__tmp,vectorLengths,firstRow);
00990                             }
00991                             else if  ( type() == Tbyte )
00992                             {
00993                                 ColumnVectorData<unsigned char>& col 
00994                                         = dynamic_cast<ColumnVectorData<unsigned char>&>(*this);
00995                                 valarray<unsigned char> __tmp;
00996                                 FITSUtil::fill(__tmp,indata);
00997                                 col.writeData(__tmp,firstRow);
00998                             }                            
00999                             else if  ( type() == Tushort )
01000                             {
01001                                 ColumnVectorData<unsigned short>& col 
01002                                         = dynamic_cast<ColumnVectorData<unsigned short>&>(*this);
01003                                 valarray<unsigned short> __tmp;
01004                                 FITSUtil::fill(__tmp,indata);
01005                                 col.writeData(__tmp,vectorLengths,firstRow);
01006                             }
01007                             else if  ( type() == Tuint )
01008                             {
01009                                 ColumnVectorData<unsigned int>& col 
01010                                         = dynamic_cast<ColumnVectorData<unsigned int>&>(*this);
01011                                 valarray<unsigned int> __tmp;
01012                                 FITSUtil::fill(__tmp,indata);
01013                                 col.writeData(__tmp,vectorLengths,firstRow);
01014                             }
01015                             else if  ( type() == Tulong )
01016                             {
01017                                 ColumnVectorData<unsigned long>& col 
01018                                         = dynamic_cast<ColumnVectorData<unsigned long>&>(*this);
01019                                 valarray<unsigned long> __tmp;
01020                                 FITSUtil::fill(__tmp,indata);
01021                                 col.writeData(__tmp,vectorLengths,firstRow);
01022                             } 
01023                             else
01024                             {
01025                                     throw InvalidDataType(name());
01026                             }
01027                         }
01028                 }
01029         }
01030 
01031         template <typename S>
01032         void Column::write (const std::vector<S>& indata,const std::vector<long>& vectorLengths,
01033                                 long firstRow)
01034         {
01035                 // variable length write
01036                 // implement as valarray version
01037                 std::valarray<S> __tmp(indata.size());
01038                 std::copy(indata.begin(),indata.end(),&__tmp[0]);
01039                 write(__tmp,vectorLengths,firstRow);  
01040 
01041         }
01042 
01043         template <typename S>
01044         void Column::write (S* indata, long nelements, const std::vector<long>& vectorLengths,
01045                                         long firstRow)
01046         {
01047                 // implement as valarray version, which will also check array size.
01048                 size_t n(vectorLengths.size());
01049                 std::valarray<S> __tmp(indata,nelements);
01050                 write(__tmp,vectorLengths,firstRow);
01051         }        
01052 
01053         template <typename S>
01054         void Column::write (const std::valarray<S>& indata, long nRows, long firstRow)
01055         {
01056                 write(indata,nRows,firstRow,static_cast<S*>(0));
01057         }
01058 
01059         template <typename S>
01060         void Column::write (const std::vector<S>& indata, long nRows, long firstRow)
01061         {
01062                 write(indata,nRows,firstRow,static_cast<S*>(0));  
01063         }
01064 
01065         template <typename S>
01066         void Column::write (S* indata, long nelements, long nRows, long firstRow)
01067         {
01068                 write(indata,nelements,nRows,firstRow,static_cast<S*>(0));              
01069         }        
01070 
01071 
01072 
01073         template <typename S>
01074         void Column::write (const std::valarray<S>& indata, long nRows, long firstRow,
01075                                 S* nullValue)
01076         {
01077                 // primary implementation,  nullValue: write fixed-length data
01078                 // to rows of a vector column.  
01079                 if (nRows <= 0) throw InvalidNumberOfRows(nRows);
01080                 parent()->makeThisCurrent();
01081                 firstRow = std::max(firstRow,static_cast<long>(1));
01082                 if (ColumnVectorData<S>* col = dynamic_cast<ColumnVectorData<S>*>(this))
01083                 {
01084                         col->writeData(indata,nRows,firstRow,nullValue);
01085                 }
01086                 else 
01087                 {
01088                         // alright, input data type has to be rewritten as output
01089                         // data type.
01090                         FITSUtil::MatchType<S> inType;
01091                         if ( inType() == type()) 
01092                         {
01093                                 String 
01094                                   msg("Incorrect call: writing to valarray data to scalar column: ");
01095                                   msg += name();
01096                                   msg += " does not require specification of number of rows";
01097                                 throw WrongColumnType(msg);
01098                         }
01099                         else
01100                         {
01101                             if  ( type() == Tdouble )
01102                             {
01103                                 ColumnVectorData<double>& col 
01104                                         = dynamic_cast<ColumnVectorData<double>&>(*this);
01105                                 std::valarray<double> __tmp;
01106                                 FITSUtil::fill(__tmp,indata);
01107                                 col.writeData(__tmp,nRows,firstRow);
01108                             }
01109                             else if  ( type() == Tfloat )
01110                             {
01111                                 ColumnVectorData<float>& col 
01112                                         = dynamic_cast<ColumnVectorData<float>&>(*this);
01113                                 std::valarray<float> __tmp;
01114                                 FITSUtil::fill(__tmp,indata);
01115                                 col.writeData(__tmp,nRows,firstRow);
01116                             }
01117                             else if  ( type() == Tint )
01118                             {
01119                                 int nullVal(0);
01120                                 if (nullValue) nullVal = static_cast<int>(*nullValue); 
01121                                 ColumnVectorData<int>& col  
01122                                         = dynamic_cast<ColumnVectorData<int>&>(*this);
01123                                 std::valarray<int> __tmp;
01124                                 FITSUtil::fill(__tmp,indata);
01125                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01126                             }
01127                             else if  ( type() == Tshort )
01128                             {
01129                                 short nullVal(0);
01130                                 if (nullValue) nullVal = static_cast<short>(*nullValue); 
01131                                 ColumnVectorData<short>& col 
01132                                         = dynamic_cast<ColumnVectorData<short>&>(*this);
01133                                 std::valarray<short> __tmp;
01134                                 FITSUtil::fill(__tmp,indata);
01135                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01136                             }
01137                             else if  ( type() == Tlong )
01138                             {
01139                                 long nullVal(0);
01140                                 if (nullValue) nullVal = static_cast<long>(*nullValue); 
01141                                 ColumnVectorData<long>& col 
01142                                         = dynamic_cast<ColumnVectorData<long>&>(*this);
01143                                 std::valarray<long> __tmp;
01144                                 FITSUtil::fill(__tmp,indata);
01145                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01146                             }
01147                             else if  ( type() == Tlonglong )
01148                             {
01149                                 LONGLONG nullVal(0);
01150                                 if (nullValue) nullVal = static_cast<LONGLONG>(*nullValue); 
01151                                 ColumnVectorData<LONGLONG>& col 
01152                                         = dynamic_cast<ColumnVectorData<LONGLONG>&>(*this);
01153                                 std::valarray<LONGLONG> __tmp;
01154                                 FITSUtil::fill(__tmp,indata);
01155                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01156                             }
01157                             else if  ( type() == Tlogical )
01158                             {
01159                                 bool nullVal(0);
01160                                 if (nullValue) nullVal = static_cast<bool>(*nullValue); 
01161                                 ColumnVectorData<bool>& col 
01162                                         = dynamic_cast<ColumnVectorData<bool>&>(*this);
01163                                 std::valarray<bool> __tmp;
01164                                 FITSUtil::fill(__tmp,indata);
01165                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01166                             }
01167                             else if  ( type() == Tbyte )
01168                             {
01169                                 unsigned char nullVal(0);
01170                                 if (nullValue) nullVal = static_cast<unsigned char>(*nullValue);
01171                                 ColumnVectorData<unsigned char>& col 
01172                                         = dynamic_cast<ColumnVectorData<unsigned char>&>(*this);
01173                                 std::valarray<unsigned char> __tmp;
01174                                 FITSUtil::fill(__tmp,indata);
01175                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01176                             }                            
01177                             else if  ( type() == Tushort )
01178                             {
01179                                 unsigned short nullVal(0);
01180                                 if (nullValue) nullVal = static_cast<unsigned short>(*nullValue);
01181                                 ColumnVectorData<unsigned short>& col 
01182                                         = dynamic_cast<ColumnVectorData<unsigned short>&>(*this);
01183                                 std::valarray<unsigned short> __tmp;
01184                                 FITSUtil::fill(__tmp,indata);
01185                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01186                             }
01187                             else if  ( type() == Tuint )
01188                             {
01189                                 unsigned int nullVal(0);
01190                                 if (nullValue) nullVal = static_cast<unsigned int>(*nullValue);
01191                                 ColumnVectorData<unsigned int>& col 
01192                                         = dynamic_cast<ColumnVectorData<unsigned int>&>(*this);
01193                                 std::valarray<unsigned int> __tmp;
01194                                 FITSUtil::fill(__tmp,indata);
01195                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01196                             }
01197                             else if  ( type() == Tulong )
01198                             {
01199                                 unsigned long nullVal(0);
01200                                 if (nullValue) nullVal = static_cast<unsigned long>(*nullValue);
01201                                 ColumnVectorData<unsigned long>& col 
01202                                         = dynamic_cast<ColumnVectorData<unsigned long>&>(*this);
01203                                 std::valarray<unsigned long> __tmp;
01204                                 FITSUtil::fill(__tmp,indata);
01205                                 col.writeData(__tmp,nRows,firstRow,&nullVal);
01206                             } 
01207                             else
01208                             {
01209                                     throw InvalidDataType(name());
01210                             }
01211                         }
01212                 }
01213         }
01214 
01215         template <typename S>
01216         void Column::write (const std::vector<S>& indata, long nRows, long firstRow, S* nullValue)
01217         {
01218                 // fixed length write of vector
01219                 // implement as valarray version
01220                 if (nRows <= 0) throw InvalidNumberOfRows(nRows);
01221                 std::valarray<S> __tmp(indata.size());
01222                 std::copy(indata.begin(),indata.end(),&__tmp[0]);
01223                 write(__tmp,nRows,firstRow, nullValue);  
01224         }
01225 
01226         template <typename S>
01227         void Column::write (S* indata, long nelements, long nRows, long firstRow, S* nullValue)
01228         {
01229                 // fixed length write of C-array
01230                 // implement as valarray version
01231                 if (nRows <= 0) throw InvalidNumberOfRows(nRows);
01232                 std::valarray<S> __tmp(indata,nelements);
01233                 write(__tmp,nRows,firstRow, nullValue);              
01234         }        
01235 
01236 
01237         template <typename S>
01238         void Column::writeArrays (const std::vector<std::valarray<S> >& indata, long firstRow)
01239         {
01240                 // vector<valarray>, no null value. 
01241                 writeArrays(indata,firstRow,static_cast<S*>(0));
01242         } 
01243 
01244         template <typename S>
01245         void Column::writeArrays (const std::vector<std::valarray<S> >& indata, long firstRow,
01246                                         S* nullValue)
01247         {
01248                 // vector<valarray>, null value. primary
01249 
01250 
01251                 using std::valarray;
01252                 using std::vector;
01253                 parent()->makeThisCurrent();
01254                 firstRow = std::max(firstRow,static_cast<long>(1));
01255                 if (ColumnVectorData<S>* col = dynamic_cast<ColumnVectorData<S>*>(this))
01256                 {
01257                          col->writeData(indata,firstRow,nullValue);
01258                 }
01259                 else
01260                 {
01261                         // alright, input data type has to be rewritten as output
01262                         // data type.
01263                         FITSUtil::MatchType<S> inType;
01264                         if ( inType() == type()) 
01265                         {
01266                                 String msg("Incorrect call: writing vectors to scalar column ");
01267                                 throw WrongColumnType(msg);
01268                         }
01269                         else
01270                         {
01271                             size_t n(indata.size());                            
01272                             if  ( type() == Tdouble )
01273                             {
01274                                 ColumnVectorData<double>& col 
01275                                         = dynamic_cast<ColumnVectorData<double>&>(*this);
01276                                 vector<valarray<double> > __tmp(n);
01277                                 for (size_t i = 0; i < n; ++i)
01278                                 {
01279                                         FITSUtil::fill(__tmp[i],indata[i]);
01280                                 }
01281                                 col.writeData(__tmp,firstRow);
01282                             }
01283                             else if  ( type() == Tfloat )
01284                             {
01285                                 ColumnVectorData<float>& col 
01286                                         = dynamic_cast<ColumnVectorData<float>&>(*this);
01287                                 vector<valarray<float> > __tmp(n);
01288                                 for (size_t i = 0; i < n; ++i)
01289                                 {
01290                                         FITSUtil::fill(__tmp[i],indata[i]);
01291                                 }                                
01292                                 col.writeData(__tmp,firstRow);
01293                             }
01294                             else if  ( type() == Tint )
01295                             {
01296                                 ColumnVectorData<int>& col  
01297                                         = dynamic_cast<ColumnVectorData<int>&>(*this);
01298                                 vector<valarray<int> > __tmp(n);
01299                                 int nullVal(0);
01300                                 if (nullValue) nullVal = static_cast<int>(*nullValue);
01301                                 for (size_t i = 0; i < n; ++i)
01302                                 {
01303                                         FITSUtil::fill(__tmp[i],indata[i]);
01304                                 }                                
01305                                 col.writeData(__tmp,firstRow,&nullVal);
01306                             }
01307                             else if  ( type() == Tshort )
01308                             {
01309                                 ColumnVectorData<short>& col 
01310                                         = dynamic_cast<ColumnVectorData<short>&>(*this);
01311                                 vector<valarray<short> > __tmp(n);
01312                                 short nullVal(0);
01313                                 if (nullValue) nullVal = static_cast<short>(*nullValue);
01314                                 for (size_t i = 0; i < n; ++i)
01315                                 {
01316                                         FITSUtil::fill(__tmp[i],indata[i]);
01317                                 }                                
01318                                 col.writeData(__tmp,firstRow,&nullVal);
01319                             }
01320                             else if  ( type() == Tlong )
01321                             {
01322                                 ColumnVectorData<long>& col 
01323                                         = dynamic_cast<ColumnVectorData<long>&>(*this);
01324                                 vector<valarray<long> > __tmp(n);
01325                                 long nullVal(0);
01326                                 if (nullValue) nullVal = static_cast<long>(*nullValue);
01327                                 for (size_t i = 0; i < n; ++i)
01328                                 {
01329                                         FITSUtil::fill(__tmp[i],indata[i]);
01330                                 }                                
01331                                 col.writeData(__tmp,firstRow,&nullVal);
01332                             }
01333                             else if  ( type() == Tlonglong )
01334                             {
01335                                 ColumnVectorData<LONGLONG>& col 
01336                                         = dynamic_cast<ColumnVectorData<LONGLONG>&>(*this);
01337                                 vector<valarray<LONGLONG> > __tmp(n);
01338                                 LONGLONG nullVal(0);
01339                                 if (nullValue) nullVal = static_cast<LONGLONG>(*nullValue);
01340                                 for (size_t i = 0; i < n; ++i)
01341                                 {
01342                                         FITSUtil::fill(__tmp[i],indata[i]);
01343                                 }                                
01344                                 col.writeData(__tmp,firstRow,&nullVal);
01345                             }
01346                             else if  ( type() == Tlogical )
01347                             {
01348                                 ColumnVectorData<bool>& col 
01349                                         = dynamic_cast<ColumnVectorData<bool>&>(*this);
01350                                 bool nullVal(0);
01351                                 if (nullValue) nullVal = static_cast<bool>(*nullValue);
01352                                 vector<valarray<bool> > __tmp(n);
01353                                 for (size_t i = 0; i < n; ++i)
01354                                 {
01355                                         FITSUtil::fill(__tmp[i],indata[i]);
01356                                 }                                
01357                                 col.writeData(__tmp,firstRow,&nullVal);
01358                             }
01359                             else if  ( type() == Tbyte )
01360                             {
01361                                 ColumnVectorData<unsigned char>& col 
01362                                         = dynamic_cast<ColumnVectorData<unsigned char>&>(*this);
01363                                 unsigned char nullVal(0);
01364                                 if (nullValue) nullVal = static_cast<unsigned char>(*nullValue);
01365                                 vector<valarray<unsigned char> > __tmp(n);
01366                                 for (size_t i = 0; i < n; ++i)
01367                                 {
01368                                         FITSUtil::fill(__tmp[i],indata[i]);
01369                                 }                                                                
01370                                 col.writeData(__tmp,firstRow,&nullVal);
01371                             }                            
01372                             else if  ( type() == Tushort )
01373                             {
01374                                 ColumnVectorData<unsigned short>& col 
01375                                         = dynamic_cast<ColumnVectorData<unsigned short>&>(*this);
01376                                 unsigned short nullVal(0);
01377                                 if (nullValue) nullVal = static_cast<unsigned short>(*nullValue);
01378                                 vector<valarray<unsigned short> > __tmp(n);
01379                                 for (size_t i = 0; i < n; ++i)
01380                                 {
01381                                         FITSUtil::fill(__tmp[i],indata[i]);
01382                                 }                                
01383                                 col.writeData(__tmp,firstRow,&nullVal);
01384                             }
01385                             else if  ( type() == Tuint )
01386                             {
01387                                 ColumnVectorData<unsigned int>& col 
01388                                         = dynamic_cast<ColumnVectorData<unsigned int>&>(*this);
01389                                 unsigned int nullVal(0);
01390                                 if (nullValue) nullVal = static_cast<unsigned int>(*nullValue);
01391                                 vector<valarray<unsigned int> > __tmp(n);
01392                                 for (size_t i = 0; i < n; ++i)
01393                                 {
01394                                         FITSUtil::fill(__tmp[i],indata[i]);
01395                                 }                                
01396                                 col.writeData(__tmp,firstRow,&nullVal);
01397                             }
01398                             else if  ( type() == Tulong )
01399                             {
01400                                 ColumnVectorData<unsigned long>& col 
01401                                         = dynamic_cast<ColumnVectorData<unsigned long>&>(*this);
01402                                 unsigned long nullVal(0);
01403                                 if (nullValue) nullVal = static_cast<unsigned long>(*nullValue);
01404                                 vector<valarray<unsigned long> > __tmp(n);
01405                                 for (size_t i = 0; i < n; ++i)
01406                                 {
01407                                         FITSUtil::fill(__tmp[i],indata[i]);
01408                                 }                                
01409                                 col.writeData(__tmp,firstRow,&nullVal);
01410                             } 
01411                             else
01412                             {
01413                                     throw InvalidDataType(name());
01414                             }
01415                         }
01416                 }
01417         } 
01418 
01419 
01420         template <typename T>
01421         void Column::addNullValue(T nullVal)
01422         {
01423                 parent()->makeThisCurrent();
01424                 int status(0);
01425 #ifdef SSTREAM_DEFECT
01426                 std::ostrstream keyName;
01427                 keyName << "TNULL" << index() << std::ends;
01428                 char* nullKey = const_cast<char*>(keyName.str());
01429 #else
01430                 std::ostringstream keyName;          
01431                 keyName << "TNULL" << index();
01432                 String keyNameStr = keyName.str();
01433                 char* nullKey = const_cast<char*>(keyNameStr.c_str());
01434 #endif
01435 
01436 
01437                 FITSUtil::MatchType<T> type;
01438 
01439                 T dataType(type());
01440                 // update key but don't add to keyword list because it's really a column
01441                 // property not a table metadata property. And it needs to be automatically
01442                 // renumbered if columns are inserted or deleted.
01443                 if (fits_update_key(fitsPointer(),dataType,nullKey,&nullVal,0,&status))
01444                         throw FitsError(status);
01445 
01446                 if (fits_set_hdustruc(fitsPointer(),&status)) throw FitsError(status);   
01447 
01448 
01449                 if (ColumnVectorData<T>* col = dynamic_cast<ColumnVectorData<T>*>(this))
01450                 {
01451 
01452                         col->nullValue(nullVal);
01453                 }
01454                 else
01455                 {
01456                         try
01457                         {
01458                                 ColumnData<T>& col 
01459                                         = dynamic_cast<ColumnData<T>&>(*this);
01460                                 col.nullValue(nullVal); 
01461                         }
01462                         catch (std::bad_cast)
01463                         {
01464                                 throw InvalidDataType(" setting null value for column ");
01465                         }                       
01466                 }
01467 
01468         }
01469 } // namespace CCfits
01470 
01471 #endif

Generated on Fri Oct 12 13:39:38 2007 for CCfits by  doxygen 1.4.7