CCfits  2.7
Column.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 COLUMN_H
00010 #define COLUMN_H 1
00011 #include <iostream>
00012 
00013 // CCfitsHeader
00014 #include "CCfits.h"
00015 // Table
00016 #include "Table.h"
00017 // FitsError
00018 #include "FitsError.h"
00019 // FITSUtil
00020 #include "FITSUtil.h"
00021 
00022 #include <complex>
00023 
00024 
00025 namespace CCfits {
00026 
00027 
00028 
00841   class Column 
00842   {
00843 
00844     public:
00845 
00846 
00847 
00848       class RangeError : public FitsException  //## Inherits: <unnamed>%3946526D031A
00849       {
00850         public:
00851             RangeError (const String& msg, bool silent = true);
00852 
00853         protected:
00854         private:
00855         private: //## implementation
00856       };
00857 
00858 
00859 
00860       class InvalidDataType : public FitsException  //## Inherits: <unnamed>%3947CF30033E
00861       {
00862         public:
00863             InvalidDataType (const String& str = string(), bool silent = true);
00864 
00865         protected:
00866         private:
00867         private: //## implementation
00868       };
00869 
00870 
00871 
00872       class InvalidRowParameter : public FitsException  //## Inherits: <unnamed>%39B5310F01A0
00873       {
00874         public:
00875             InvalidRowParameter (const String& diag, bool silent = true);
00876 
00877         protected:
00878         private:
00879         private: //## implementation
00880       };
00881 
00882 
00883 
00884       class WrongColumnType : public FitsException  //## Inherits: <unnamed>%39B545780082
00885       {
00886         public:
00887             WrongColumnType (const String& diag, bool silent = true);
00888 
00889         protected:
00890         private:
00891         private: //## implementation
00892       };
00893 
00894 
00895 
00896       class UnspecifiedLengths : public FitsException  //## Inherits: <unnamed>%3A018C9D007D
00897       {
00898         public:
00899             UnspecifiedLengths (const String& diag, bool silent = true);
00900 
00901         protected:
00902         private:
00903         private: //## implementation
00904       };
00905 
00906 
00907 
00908       class InvalidRowNumber : public FitsException  //## Inherits: <unnamed>%3B0A850F0307
00909       {
00910         public:
00911             InvalidRowNumber (const String& diag, bool silent = true);
00912 
00913         protected:
00914         private:
00915         private: //## implementation
00916       };
00917 
00918 
00919 
00920       class InsufficientElements : public FitsException  //## Inherits: <unnamed>%3B0BE611010A
00921       {
00922         public:
00923             InsufficientElements (const String& msg, bool silent = true);
00924 
00925         protected:
00926         private:
00927         private: //## implementation
00928       };
00929 
00930 
00931 
00932       class NoNullValue : public FitsException  //## Inherits: <unnamed>%3B0D589A0092
00933       {
00934         public:
00935             NoNullValue (const String& diag, bool silent = true);
00936 
00937         protected:
00938         private:
00939         private: //## implementation
00940       };
00941 
00942 
00943 
00944       class InvalidNumberOfRows : public FitsException  //## Inherits: <unnamed>%3B20EB8B0205
00945       {
00946         public:
00947             InvalidNumberOfRows (int number, bool silent = true);
00948 
00949         protected:
00950         private:
00951         private: //## implementation
00952       };
00953         Column(const Column &right);
00954         virtual ~Column();
00955         bool operator==(const Column &right) const;
00956 
00957         bool operator!=(const Column &right) const;
00958 
00959         virtual void readData (long firstRow, long nelements, long firstElem = 1) = 0;
00960         //  Virtual copy constructor.
00961         virtual Column * clone () const = 0;
00962         int rows () const;
00963         void setDisplay ();
00964         virtual void setDimen ();
00965         friend std::ostream& operator << (std::ostream& s, const Column& right);
00966         
00967         // The parent SET function is needed by Table classes, but 
00968         //  should not be part of the user interface.  It is deliberately left
00969         //   out of the document section up top.
00970         Table* parent () const;
00971         void setParent(Table* parent);
00972         
00973         //  Inequality operators for imposing sort order on columns.
00974         friend bool operator < (const Column& left, const Column& right);
00975         //  Inequality operators for imposing sort order on columns.
00976         friend bool operator > (const Column& left, const Column& right);
00977         void setLimits (ValueType type);
00978         void unit (const String& value);
00979         void resetRead ();
00980         int index () const;
00981         void index (int value);
00982         bool isRead () const;
00983         void isRead (bool value);
00984         long width () const;
00985         void width (long value);
00986         size_t repeat () const;
00987         bool varLength () const;
00988         double scale () const;
00989         void scale (double value);
00990         double zero () const;
00991         void zero (double value);
00992         const String& display () const;
00993         const String& dimen () const;
00994         void dimen (const String& value);
00995         ValueType type () const;
00996         void type (ValueType value);
00997         static const String& TFORM ();
00998         static const String& TDISP ();
00999         static const String& TSCAL ();
01000         static const String& TZERO ();
01001         static const String& TDIM ();
01002         const String& format () const;
01003         const String& unit () const;
01004         const String& name () const;
01005 
01006     public:
01007       // Additional Public Declarations
01008 
01009         // scalar column interface. Column's Data Member is a std::vector<T>,
01010         // input data is std::vector<S>, std::valarray<S> or S* where S is not
01011         // in general the same as T.
01012 
01013         template <typename S>                   
01014         void write (const std::vector<S>& indata, long firstRow);
01015 
01016         void write (const std::vector<std::complex<float> >& indata, long firstRow);
01017 
01018         void write (const std::vector<std::complex<double> >& indata, long firstRow);
01019 
01020         template <typename S>                   
01021         void write (const std::valarray<S>& indata, long firstRow);
01022 
01023         void write (const std::valarray<std::complex<float> >& indata, long firstRow);
01024 
01025         void write (const std::valarray<std::complex<double> >& indata, long firstRow);
01026 
01027         template <typename S>                   
01028         void write (S* indata, long nRows, long firstRow);
01029 
01030 
01031         template <typename S>                   
01032         void write (const std::vector<S>& indata, long firstRow, S* nullValue);
01033 
01034         template <typename S>                   
01035         void write (const std::valarray<S>& indata, long firstRow, S* nullValue);
01036 
01037         template <typename S>                   
01038         void write (S* indata, long nRows, long firstRow, S* nullValue);        
01039         // vector column interface. We provide an interface that allows input of a vector, valarray and C-array.
01040     // there are versions that write variable numbers of elements per row as specified
01041         // in the vectorLengths argument. The user also can directly write a vector<valarray<T> >
01042         // object which is how the data are stored in the ColumnVectorData object. 
01043         // this last one is also used internally to implement the variable lengths versions.
01044 
01045         // fixed length write to binary table from valarray.
01046 
01047 
01048         template <typename S>
01049         void write (const std::valarray<S>& indata, long nRows, long firstRow);
01050 
01051         void write (const std::valarray<std::complex<float> >& indata, long nRows, long firstRow);
01052 
01053         void write (const std::valarray<std::complex<double> >& indata, long nRows, long firstRow);
01054 
01055 
01056         template <typename S>
01057         void write (const std::vector<S>& indata, long nRows, long firstRow);
01058 
01059         void write (const std::vector<std::complex<float> >& indata, long nRows, long firstRow);
01060 
01061         void write (const std::vector<std::complex<double> >& indata, long nRows, long firstRow);
01062 
01063 
01064 
01065         template <typename S>
01066         void write (S* indata, long nElements, long nRows, long firstRow);
01067 
01068 
01069         template <typename S>
01070         void write (const std::valarray<S>& indata, long nRows, long firstRow, S* nullValue);
01071 
01072 
01073         template <typename S>
01074         void write (const std::vector<S>& indata, long nRows, long firstRow, S* nullValue);
01075 
01076         template <typename S>
01077         void write (S* indata, long nElements, long nRows, long firstRow, S* nullValue);
01078 
01079         // variable-length write to vector column from valarray or vector.
01080 
01081         template <typename S>
01082         void write (const std::valarray<S>& indata,  
01083                         const std::vector<long>& vectorLengths, 
01084                         long firstRow);        
01085 
01086         void write (const std::valarray<std::complex<float> >& indata,  
01087                         const std::vector<long>& vectorLengths, 
01088                         long firstRow);        
01089 
01090         void write (const std::valarray<std::complex<double> >& indata,  
01091                         const std::vector<long>& vectorLengths, 
01092                         long firstRow);      
01093 
01094         template <typename S>
01095         void write (const std::vector<S>& indata, 
01096                         const std::vector<long>& vectorLengths, 
01097                         long firstRow);
01098 
01099 
01100 
01101         void write (const std::vector<std::complex<float> >& indata, 
01102                         const std::vector<long>& vectorLengths, 
01103                         long firstRow);
01104 
01105         void write (const std::vector<std::complex<double> >& indata, 
01106                         const std::vector<long>& vectorLengths, 
01107                         long firstRow);
01108 
01109         template <typename S>
01110         void write (S* indata, long nElements,  
01111                         const std::vector<long>& vectorLengths, 
01112                         long firstRow);
01113 
01114         template <typename S>
01115         void writeArrays (const std::vector<std::valarray<S> >& indata, long firstRow); 
01116 
01117         void writeArrays (const std::vector<std::valarray<std::complex<float> > >& indata, long firstRow); 
01118 
01119         void writeArrays (const std::vector<std::valarray<std::complex<double> > >& indata, long firstRow); 
01120 
01121     template <typename S>
01122         void writeArrays (const std::vector<std::valarray<S> >& indata, long firstRow, S* nullValue);  
01123 
01124         // get specified elements of a scalar column, returned as a std::vector
01125         // S is NOT the type of the column data itself, it is the type of the returned
01126         // data.
01127 
01128         template <typename S>
01129         void read(std::vector<S>& vals, long first, long last) ;
01130 
01131         // VC++, at least, won't compile these as template covering std::complex instances.
01132         void read(std::vector< std::complex<float> >& , long first, long last);
01133 
01134         void read(std::vector< std::complex<double> >& , long first, long last);
01135 
01136         void read(std::vector<String>& vals, long first, long last);
01137 
01138         // return a set of rows from a scalar column as a valarray.
01139         template <typename S>
01140         void read(std::valarray<S>& vals, long first, long last) ;
01141 
01142         void read(std::valarray<std::complex<float> >& vals, long first, long last) ;
01143 
01144         void read(std::valarray<std::complex<double> >& vals, long first, long last) ;
01145 
01146         // return a single  row of a vector column.
01147         template <typename S>
01148         void read(std::valarray<S>& vals, long rows) ;
01149         template <typename S>
01150         void read(std::vector<S>& vals, long rows);
01151         void read(std::valarray<std::complex<float> >& vals, long rows) ;
01152         void read(std::valarray<std::complex<double> >& vals, long rows) ;
01153         void read(std::vector<std::complex<float> >& vals, long rows) ;
01154         void read(std::vector<std::complex<double> >& vals, long rows) ;
01155 
01156         // get a set of rows from a vector column.
01157         template <typename S>
01158         void readArrays(std::vector<std::valarray<S> >& vals, long first, long last) ;
01159 
01160         void readArrays(std::vector<std::valarray<std::complex<float> > >& vals, long first, long last) ;
01161 
01162         void readArrays(std::vector<std::valarray<std::complex<double> > >& vals, long first, long last) ;
01163 
01164         // nullValue has no meaning when the target column has floating point/std::complex
01165         // type. Also, implict conversion of std::complex to pure real is not supported
01166         // by cfitsio.
01167 
01168         template <typename S>
01169         void read(std::vector<S>& vals, long first, long last, S* nullValue) ;
01170 
01171         // return a set of rows from a scalar column as a valarray.
01172         template <typename S>
01173         void read(std::valarray<S>& vals, long first, long last, S* nullValue);
01174 
01175         // return a single  row of a vector column.
01176         template <typename S>
01177         void read(std::valarray<S>& vals, long rows, S* nullValue) ;
01178 
01179         template <typename S>
01180         void read(std::vector<S>& vals, long rows, S* nullValue) ;
01181         
01182         // get a set of rows from a vector column.
01183         template <typename S>
01184         void readArrays(std::vector<std::valarray<S> >& vals, long first, long last, S* nullValue);
01185 
01186         // add a null value to the column 
01187         template <typename T>
01188         void addNullValue(T nullVal);
01189 
01190         // get the TNULL setting
01191         template <typename T>
01192         bool getNullValue(T* nullVal) const;
01193 
01194         void write (const std::vector<String>& indata, long firstRow);
01195 
01196         friend void Table::insertRows(long first, long number);
01197 
01198         friend void Table::deleteRows(long first, long number);
01199 
01200         friend void Table::deleteRows(const std::vector<long>& rowList);
01201 
01202         friend void Table::initRead();
01203 
01204         friend void Table::reindex(int startNum, bool isInsert);
01205         
01206         friend void Table::updateRows();
01207 
01208     protected:
01209         Column (int columnIndex,    // The column index, i.e. the integer n in the keyword TCOLn
01210         const String &columnName,   // The column name, curiously TTYPEn
01211         ValueType type, const String &format,   // The TFORMn keyword.
01212         const String &unit,     // The TUNITn keyword
01213         Table* p,   // ! The Table containing the Column object
01214         int rpt = 1, long w = 1, const String &comment = "");
01215         Column (Table* p = 0);
01216 
01217         virtual bool compare (const Column &right) const;
01218         fitsfile* fitsPointer ();
01219         //  Protected method to set the current HDU to be the one containing this Column object. For use in
01220         //  public read/write methods to ensure that data regarding numbers of rows and width relate to the
01221         //  right HDU
01222         void makeHDUCurrent ();
01223         virtual std::ostream& put (std::ostream& s) const;
01224         void varLength (bool value);
01225         static const String& TBCOL ();
01226         static const String& TTYPE ();
01227         static const String& TUNIT ();
01228         static const String& TNULL ();
01229         static const String& TLMIN ();
01230         static const String& TLMAX ();
01231         static const String& TDMAX ();
01232         static const String& TDMIN ();
01233         static const std::vector<String>& columnKeys ();
01234         const String& comment () const;
01235 
01236       // Additional Protected Declarations
01237 
01238     private:
01239         Column & operator=(const Column &right);
01240 
01241         //  Insert one or more blank rows into a FITS column.
01242         virtual void insertRows (long first, long number = 1) = 0;
01243         virtual void deleteRows (long first, long number = 1) = 0;
01244         virtual size_t getStoredDataSize() const = 0;
01245         static void loadColumnKeys ();
01246         void name (const String& value);
01247         void format (const String& value);
01248         long numberOfElements (long& first, long& last);
01249 
01250       // Data Members for Class Attributes
01251         static const String s_TBCOL;
01252         static const String s_TTYPE;
01253         static const String s_TFORM;
01254         static const String s_TDISP;
01255         static const String s_TUNIT;
01256         static const String s_TSCAL;
01257         static const String s_TZERO;
01258         static const String s_TDIM;
01259         static const String s_TNULL;
01260         static const String s_TLMIN;
01261         static const String s_TLMAX;
01262         static const String s_TDMAX;
01263         static const String s_TDMIN;
01264 
01265       // Additional Private Declarations
01266 
01267     private: //## implementation
01268       // Data Members for Class Attributes
01269         int m_index;
01270         bool m_isRead;
01271         long m_width;
01272         size_t m_repeat;
01273         bool m_varLength;
01274         double m_scale;
01275         double m_zero;
01276         String m_display;
01277         String m_dimen;
01278         ValueType m_type;
01279         static const short LLIMITSHORT;
01280         static const long LLIMITLONG;
01281         static const unsigned short LLIMITUSHORT;
01282         static const unsigned long LLIMITULONG;
01283         static const unsigned char LLIMITUCHAR;
01284         static const float LLIMITFLOAT;
01285         static const double LLIMITDOUBLE;
01286         static const short ULIMITSHORT;
01287         static const long ULIMITLONG;
01288         static const unsigned short ULIMITUSHORT;
01289         static const unsigned long ULIMITULONG;
01290         static const unsigned char ULIMITUCHAR;
01291         static const float ULIMITFLOAT;
01292         static const double ULIMITDOUBLE;
01293         static const int LLIMITINT;
01294         static const int ULIMITINT;
01295         static const unsigned int LLIMITUINT;
01296         static const unsigned int ULIMITUINT;
01297         static const LONGLONG LLIMITLONGLONG;
01298         static const LONGLONG ULIMITLONGLONG;
01299 
01300       // Data Members for Associations
01301         Table* m_parent;
01302         static std::vector<String> s_columnKeys;
01303         String m_comment;
01304         String m_format;
01305         String m_unit;
01306         String m_name;
01307 
01308       // Additional Implementation Declarations
01309 
01310   };
01311 
01312   // Class CCfits::Column::RangeError 
01313 
01314   // Class CCfits::Column::InvalidDataType 
01315 
01316   // Class CCfits::Column::InvalidRowParameter 
01317 
01318   // Class CCfits::Column::WrongColumnType 
01319 
01320   // Class CCfits::Column::UnspecifiedLengths 
01321 
01322   // Class CCfits::Column::InvalidRowNumber 
01323 
01324   // Class CCfits::Column::InsufficientElements 
01325 
01326   // Class CCfits::Column::NoNullValue 
01327 
01328   // Class CCfits::Column::InvalidNumberOfRows 
01329 
01330   // Class CCfits::Column 
01331 
01332   inline void Column::setDimen ()
01333   {
01334   // default implementation: do nothing. Overridden by ColumnVectorData.
01335   }
01336 
01337   inline std::ostream& operator << (std::ostream& s, const Column& right)
01338   {
01339 
01340     return right.put(s);
01341   }
01342 
01343   inline bool operator < (const Column& left, const Column& right)
01344   {
01345 
01346     return left.m_index < right.m_index;
01347   }
01348 
01349   inline bool operator > (const Column& left, const Column& right)
01350   {
01351 
01352     return left.m_index > right.m_index;
01353   }
01354 
01355   inline void Column::resetRead ()
01356   {
01357      m_isRead = false;
01358   }
01359 
01360   inline int Column::index () const
01361   {
01362     return m_index;
01363   }
01364 
01365   inline void Column::index (int value)
01366   {
01367     m_index = value;
01368   }
01369 
01370   inline bool Column::isRead () const
01371   {
01372     return m_isRead;
01373   }
01374 
01375   inline void Column::isRead (bool value)
01376   {
01377     m_isRead = value;
01378   }
01379 
01380   inline long Column::width () const
01381   {
01382     return m_width;
01383   }
01384 
01385   inline void Column::width (long value)
01386   {
01387     m_width = value;
01388   }
01389 
01390   inline size_t Column::repeat () const
01391   {
01392     return m_repeat;
01393   }
01394 
01395   inline bool Column::varLength () const
01396   {
01397     return m_varLength;
01398   }
01399 
01400   inline double Column::scale () const
01401   {
01402     return m_scale;
01403   }
01404 
01405   inline void Column::scale (double value)
01406   {
01407     m_scale = value;
01408     int status(0);
01409     if (fits_set_tscale(fitsPointer(),m_index,value,m_zero,&status)) throw FitsError(status);
01410   }
01411 
01412   inline double Column::zero () const
01413   {
01414     return m_zero;
01415   }
01416 
01417   inline void Column::zero (double value)
01418   {
01419     m_zero = value;
01420   }
01421 
01422   inline const String& Column::display () const
01423   {
01424     return m_display;
01425   }
01426 
01427   inline const String& Column::dimen () const
01428   {
01429     return m_dimen;
01430   }
01431 
01432   inline void Column::dimen (const String& value)
01433   {
01434     m_dimen = value;
01435   }
01436 
01437   inline ValueType Column::type () const
01438   {
01439     return m_type;
01440   }
01441 
01442   inline void Column::type (ValueType value)
01443   {
01444     m_type = value;
01445   }
01446 
01447   inline const String& Column::TBCOL ()
01448   {
01449     return s_TBCOL;
01450   }
01451 
01452   inline const String& Column::TTYPE ()
01453   {
01454     return s_TTYPE;
01455   }
01456 
01457   inline const String& Column::TFORM ()
01458   {
01459     return s_TFORM;
01460   }
01461 
01462   inline const String& Column::TDISP ()
01463   {
01464     return s_TDISP;
01465   }
01466 
01467   inline const String& Column::TUNIT ()
01468   {
01469     return s_TUNIT;
01470   }
01471 
01472   inline const String& Column::TSCAL ()
01473   {
01474     return s_TSCAL;
01475   }
01476 
01477   inline const String& Column::TZERO ()
01478   {
01479     return s_TZERO;
01480   }
01481 
01482   inline const String& Column::TDIM ()
01483   {
01484     return s_TDIM;
01485   }
01486 
01487   inline const String& Column::TNULL ()
01488   {
01489     return s_TNULL;
01490   }
01491 
01492   inline const String& Column::TLMIN ()
01493   {
01494     return s_TLMIN;
01495   }
01496 
01497   inline const String& Column::TLMAX ()
01498   {
01499     return s_TLMAX;
01500   }
01501 
01502   inline const String& Column::TDMAX ()
01503   {
01504     return s_TDMAX;
01505   }
01506 
01507   inline const String& Column::TDMIN ()
01508   {
01509     return s_TDMIN;
01510   }
01511 
01512   inline const std::vector<String>& Column::columnKeys ()
01513   {
01514     return s_columnKeys;
01515   }
01516 
01517   inline const String& Column::comment () const
01518   {
01519     return m_comment;
01520   }
01521 
01522   inline const String& Column::format () const
01523   {
01524     return m_format;
01525   }
01526 
01527   inline const String& Column::unit () const
01528   {
01529     return m_unit;
01530   }
01531 
01532   inline const String& Column::name () const
01533   {
01534     return m_name;
01535   }
01536 
01537 } // namespace CCfits
01538 
01539 
01540 #endif