Column.h

00001 //      This is version 1.6 release dated Nov 2006
00002 //      Astrophysics Science Division,
00003 //      NASA/ Goddard Space Flight Center
00004 //      HEASARC
00005 //      http://heasarc.gsfc.nasa.gov
00006 //      e-mail: ccfits@legacy.gsfc.nasa.gov
00007 //
00008 //      Original author: Ben Dorman, L3-Communications EER Systems Inc.
00009 
00010 #ifndef COLUMN_H
00011 #define COLUMN_H 1
00012 #include <iostream>
00013 
00014 // CCfitsHeader
00015 #include "CCfits.h"
00016 // Table
00017 #include "Table.h"
00018 // FitsError
00019 #include "FitsError.h"
00020 // FITSUtil
00021 #include "FITSUtil.h"
00022 
00023 #include <complex>
00024 
00025 
00026 namespace CCfits {
00027 
00028 
00029 
00756   class Column 
00757   {
00758 
00759     public:
00760 
00761 
00762 
00763       class RangeError : public FitsException  //## Inherits: <unnamed>%3946526D031A
00764       {
00765         public:
00766             RangeError (const String& msg, bool silent = true);
00767 
00768         protected:
00769         private:
00770         private: //## implementation
00771       };
00772 
00773 
00774 
00775       class InvalidDataType : public FitsException  //## Inherits: <unnamed>%3947CF30033E
00776       {
00777         public:
00778             InvalidDataType (const String& str = string(), bool silent = true);
00779 
00780         protected:
00781         private:
00782         private: //## implementation
00783       };
00784 
00785 
00786 
00787       class InvalidRowParameter : public FitsException  //## Inherits: <unnamed>%39B5310F01A0
00788       {
00789         public:
00790             InvalidRowParameter (const String& diag, bool silent = true);
00791 
00792         protected:
00793         private:
00794         private: //## implementation
00795       };
00796 
00797 
00798 
00799       class WrongColumnType : public FitsException  //## Inherits: <unnamed>%39B545780082
00800       {
00801         public:
00802             WrongColumnType (const String& diag, bool silent = true);
00803 
00804         protected:
00805         private:
00806         private: //## implementation
00807       };
00808 
00809 
00810 
00811       class UnspecifiedLengths : public FitsException  //## Inherits: <unnamed>%3A018C9D007D
00812       {
00813         public:
00814             UnspecifiedLengths (const String& diag, bool silent = true);
00815 
00816         protected:
00817         private:
00818         private: //## implementation
00819       };
00820 
00821 
00822 
00823       class InvalidRowNumber : public FitsException  //## Inherits: <unnamed>%3B0A850F0307
00824       {
00825         public:
00826             InvalidRowNumber (const String& diag, bool silent = true);
00827 
00828         protected:
00829         private:
00830         private: //## implementation
00831       };
00832 
00833 
00834 
00835       class InsufficientElements : public FitsException  //## Inherits: <unnamed>%3B0BE611010A
00836       {
00837         public:
00838             InsufficientElements (const String& msg, bool silent = true);
00839 
00840         protected:
00841         private:
00842         private: //## implementation
00843       };
00844 
00845 
00846 
00847       class NoNullValue : public FitsException  //## Inherits: <unnamed>%3B0D589A0092
00848       {
00849         public:
00850             NoNullValue (const String& diag, bool silent = true);
00851 
00852         protected:
00853         private:
00854         private: //## implementation
00855       };
00856 
00857 
00858 
00859       class InvalidNumberOfRows : public FitsException  //## Inherits: <unnamed>%3B20EB8B0205
00860       {
00861         public:
00862             InvalidNumberOfRows (size_t number, bool silent = true);
00863 
00864         protected:
00865         private:
00866         private: //## implementation
00867       };
00868         Column(const Column &right);
00869         virtual ~Column();
00870         bool operator==(const Column &right) const;
00871 
00872         bool operator!=(const Column &right) const;
00873 
00874         virtual void readData (long firstRow, long nelements, long firstElem = 1) = 0;
00875         //      Virtual copy constructor.
00876         virtual Column * clone () const = 0;
00877         int rows () const;
00878         void setDisplay ();
00879         virtual void setDimen ();
00880         friend std::ostream& operator << (std::ostream& s, const Column& right);
00881         //      Inequality operators for imposing sort order on columns.
00882         friend bool operator < (const Column& left, const Column& right);
00883         //      Inequality operators for imposing sort order on columns.
00884         friend bool operator > (const Column& left, const Column& right);
00885         void setLimits (ValueType type);
00886         void unit (const String& value);
00887         int index () const;
00888         void index (int value);
00889         bool isRead () const;
00890         void isRead (bool value);
00891         long width () const;
00892         void width (long value);
00893         size_t repeat () const;
00894         bool varLength () const;
00895         double scale () const;
00896         void scale (double value);
00897         double zero () const;
00898         void zero (double value);
00899         const String& display () const;
00900         const String& dimen () const;
00901         void dimen (const String& value);
00902         ValueType type () const;
00903         void type (ValueType value);
00904         static const String& TFORM ();
00905         static const String& TDISP ();
00906         static const String& TSCAL ();
00907         static const String& TZERO ();
00908         static const String& TDIM ();
00909         const String& format () const;
00910         const String& unit () const;
00911         const String& name () const;
00912 
00913     public:
00914       // Additional Public Declarations
00915 
00916         // scalar column interface. Column's Data Member is a std::vector<T>,
00917         // input data is std::vector<S>, std::valarray<S> or S* where S is not
00918         // in general the same as T.
00919 
00920         template <typename S>                   
00921         void write (const std::vector<S>& indata, long firstRow);
00922 
00923         void write (const std::vector<std::complex<float> >& indata, long firstRow);
00924 
00925         void write (const std::vector<std::complex<double> >& indata, long firstRow);
00926 
00927         template <typename S>                   
00928         void write (const std::valarray<S>& indata, long firstRow);
00929 
00930         void write (const std::valarray<std::complex<float> >& indata, long firstRow);
00931 
00932         void write (const std::valarray<std::complex<double> >& indata, long firstRow);
00933 
00934         template <typename S>                   
00935         void write (S* indata, long nRows, long firstRow);
00936 
00937 
00938         template <typename S>                   
00939         void write (const std::vector<S>& indata, long firstRow, S* nullValue);
00940 
00941         template <typename S>                   
00942         void write (const std::valarray<S>& indata, long firstRow, S* nullValue);
00943 
00944         template <typename S>                   
00945         void write (S* indata, long nRows, long firstRow, S* nullValue);        
00946         // vector column interface. We provide an interface that allows input of a vector, valarray and C-array.
00947         // there are versions that write variable numbers of elements per row as specified
00948         // in the vectorLengths argument. The user also can directly write a vector<valarray<T> >
00949         // object which is how the data are stored in the ColumnVectorData object. 
00950         // this last one is also used internally to implement the variable lengths versions.
00951 
00952         // fixed length write to binary table from valarray.
00953 
00954 
00955         template <typename S>
00956         void write (const std::valarray<S>& indata, long nRows, long firstRow);
00957 
00958         void write (const std::valarray<std::complex<float> >& indata, long nRows, long firstRow);
00959 
00960         void write (const std::valarray<std::complex<double> >& indata, long nRows, long firstRow);
00961 
00962 
00963         template <typename S>
00964         void write (const std::vector<S>& indata, long nRows, long firstRow);
00965 
00966         void write (const std::vector<std::complex<float> >& indata, long nRows, long firstRow);
00967 
00968         void write (const std::vector<std::complex<double> >& indata, long nRows, long firstRow);
00969 
00970 
00971 
00972         template <typename S>
00973         void write (S* indata, long nElements, long nRows, long firstRow);
00974 
00975 
00976         template <typename S>
00977         void write (const std::valarray<S>& indata, long nRows, long firstRow, S* nullValue);
00978 
00979 
00980         template <typename S>
00981         void write (const std::vector<S>& indata, long nRows, long firstRow, S* nullValue);
00982 
00983         template <typename S>
00984         void write (S* indata, long nElements, long nRows, long firstRow, S* nullValue);
00985 
00986         // variable-length write to vector column from valarray or vector.
00987 
00988         template <typename S>
00989         void write (const std::valarray<S>& indata,  
00990                         const std::vector<long>& vectorLengths, 
00991                         long firstRow);        
00992 
00993         void write (const std::valarray<std::complex<float> >& indata,  
00994                         const std::vector<long>& vectorLengths, 
00995                         long firstRow);        
00996 
00997         void write (const std::valarray<std::complex<double> >& indata,  
00998                         const std::vector<long>& vectorLengths, 
00999                         long firstRow);      
01000 
01001         template <typename S>
01002         void write (const std::vector<S>& indata, 
01003                         const std::vector<long>& vectorLengths, 
01004                         long firstRow);
01005 
01006 
01007 
01008         void write (const std::vector<std::complex<float> >& indata, 
01009                         const std::vector<long>& vectorLengths, 
01010                         long firstRow);
01011 
01012         void write (const std::vector<std::complex<double> >& indata, 
01013                         const std::vector<long>& vectorLengths, 
01014                         long firstRow);
01015 
01016         template <typename S>
01017         void write (S* indata, long nElements,  
01018                         const std::vector<long>& vectorLengths, 
01019                         long firstRow);
01020 
01021         template <typename S>
01022         void writeArrays (const std::vector<std::valarray<S> >& indata, long firstRow); 
01023 
01024         void writeArrays (const std::vector<std::valarray<std::complex<float> > >& indata, long firstRow); 
01025 
01026         void writeArrays (const std::vector<std::valarray<std::complex<double> > >& indata, long firstRow); 
01027 
01028         template <typename S>
01029         void writeArrays (const std::vector<std::valarray<S> >& indata, long firstRow, S* nullValue);  
01030 
01031         // get specified elements of a scalar column, returned as a std::vector
01032         // S is NOT the type of the column data itself, it is the type of the returned
01033         // data.
01034 
01035         template <typename S>
01036         void read(std::vector<S>& vals, long first, long last) ;
01037 
01038         // VC++, at least, won't compile these as template covering std::complex instances.
01039         void read(std::vector< std::complex<float> >& , long first, long last);
01040 
01041         void read(std::vector< std::complex<double> >& , long first, long last);
01042 
01043         void read(std::vector<String>& vals, long first, long last);
01044 
01045         // return a set of rows from a scalar column as a valarray.
01046         template <typename S>
01047         void read(std::valarray<S>& vals, long first, long last) ;
01048 
01049         void read(std::valarray<std::complex<float> >& vals, long first, long last) ;
01050 
01051         void read(std::valarray<std::complex<double> >& vals, long first, long last) ;
01052 
01053         // return a single  row of a vector column.
01054         template <typename S>
01055         void read(std::valarray<S>& vals, long rows) ;
01056 
01057         void read(std::valarray<std::complex<float> >& vals, long rows) ;
01058 
01059         void read(std::valarray<std::complex<double> >& vals, long rows) ;
01060 
01061         // get a set of rows from a vector column.
01062         template <typename S>
01063         void readArrays(std::vector<std::valarray<S> >& vals, long first, long last) ;
01064 
01065         void readArrays(std::vector<std::valarray<std::complex<float> > >& vals, long first, long last) ;
01066 
01067         void readArrays(std::vector<std::valarray<std::complex<double> > >& vals, long first, long last) ;
01068 
01069         // nullValue has no meaning when the target column has floating point/std::complex
01070         // type. Also, implict conversion of std::complex to pure real is not supported
01071         // by cfitsio.
01072 
01073         template <typename S>
01074         void read(std::vector<S>& vals, long first, long last, S* nullValue) ;
01075 
01076         // return a set of rows from a scalar column as a valarray.
01077         template <typename S>
01078         void read(std::valarray<S>& vals, long first, long last, S* nullValue);
01079 
01080         // return a single  row of a vector column.
01081         template <typename S>
01082         void read(std::valarray<S>& vals, long rows, S* nullValue) ;
01083 
01084         // get a set of rows from a vector column.
01085         template <typename S>
01086         void readArrays(std::vector<std::valarray<S> >& vals, long first, long last, S* nullValue);
01087 
01088         // add a null value to the column 
01089         template <typename T>
01090         void addNullValue(T nullVal);
01091 
01092         void write (const std::vector<String>& indata, long firstRow);
01093 
01094         friend void Table::insertRows(long first, long number);
01095 
01096         friend void Table::deleteRows(long first, long number);
01097 
01098         friend void Table::deleteRows(const std::vector<long>& rowList);
01099 
01100         friend void Table::initRead();
01101 
01102         friend void Table::reindex();
01103     protected:
01104         Column (int columnIndex,        // The column index, i.e. the integer n in the keyword TCOLn
01105         const String &columnName,       // The column name, curiously TTYPEn
01106         ValueType type, const String &format,   // The TFORMn keyword.
01107         const String &unit,     // The TUNITn keyword
01108         Table* p,       // ! The Table containing the Column object
01109         int rpt = 1, long w = 1, const String &comment = "");
01110         Column (Table* p = 0);
01111 
01112         virtual bool compare (const Column &right) const;
01113         fitsfile* fitsPointer ();
01114         //      Protected method to set the current HDU to be the one containing this Column object. For use in
01115         //      public read/write methods to ensure that data regarding numbers of rows and width relate to the
01116         //      right HDU
01117         void makeHDUCurrent ();
01118         virtual std::ostream& put (std::ostream& s) const;
01119         Table* parent () const;
01120         void varLength (bool value);
01121         static const String& TBCOL ();
01122         static const String& TTYPE ();
01123         static const String& TUNIT ();
01124         static const String& TNULL ();
01125         static const String& TLMIN ();
01126         static const String& TLMAX ();
01127         static const String& TDMAX ();
01128         static const String& TDMIN ();
01129         Table* parent ();
01130         static const std::vector<String>& columnKeys ();
01131         const String& comment () const;
01132 
01133       // Additional Protected Declarations
01134 
01135     private:
01136         Column & operator=(const Column &right);
01137 
01138         //      Insert one or more blank rows into a FITS column.
01139         virtual void insertRows (long first, long number = 1) = 0;
01140         virtual void deleteRows (long first, long number = 1) = 0;
01141         static void loadColumnKeys ();
01142         void name (const String& value);
01143         void format (const String& value);
01144         long numberOfElements (long& first, long& last);
01145 
01146       // Data Members for Class Attributes
01147         static const String s_TBCOL;
01148         static const String s_TTYPE;
01149         static const String s_TFORM;
01150         static const String s_TDISP;
01151         static const String s_TUNIT;
01152         static const String s_TSCAL;
01153         static const String s_TZERO;
01154         static const String s_TDIM;
01155         static const String s_TNULL;
01156         static const String s_TLMIN;
01157         static const String s_TLMAX;
01158         static const String s_TDMAX;
01159         static const String s_TDMIN;
01160 
01161       // Additional Private Declarations
01162 
01163     private: //## implementation
01164       // Data Members for Class Attributes
01165         int m_index;
01166         bool m_isRead;
01167         long m_width;
01168         size_t m_repeat;
01169         bool m_varLength;
01170         double m_scale;
01171         double m_zero;
01172         String m_display;
01173         String m_dimen;
01174         ValueType m_type;
01175         static const short LLIMITSHORT;
01176         static const long LLIMITLONG;
01177         static const unsigned short LLIMITUSHORT;
01178         static const unsigned long LLIMITULONG;
01179         static const unsigned char LLIMITUCHAR;
01180         static const float LLIMITFLOAT;
01181         static const double LLIMITDOUBLE;
01182         static const short ULIMITSHORT;
01183         static const long ULIMITLONG;
01184         static const unsigned short ULIMITUSHORT;
01185         static const unsigned long ULIMITULONG;
01186         static const unsigned char ULIMITUCHAR;
01187         static const float ULIMITFLOAT;
01188         static const double ULIMITDOUBLE;
01189         static const int LLIMITINT;
01190         static const int ULIMITINT;
01191         static const unsigned int LLIMITUINT;
01192         static const unsigned int ULIMITUINT;
01193         static const LONGLONG LLIMITLONGLONG;
01194         static const LONGLONG ULIMITLONGLONG;
01195 
01196       // Data Members for Associations
01197         Table* m_parent;
01198         static std::vector<String> s_columnKeys;
01199         String m_comment;
01200         String m_format;
01201         String m_unit;
01202         String m_name;
01203 
01204       // Additional Implementation Declarations
01205 
01206   };
01207 
01208   // Class CCfits::Column::RangeError 
01209 
01210   // Class CCfits::Column::InvalidDataType 
01211 
01212   // Class CCfits::Column::InvalidRowParameter 
01213 
01214   // Class CCfits::Column::WrongColumnType 
01215 
01216   // Class CCfits::Column::UnspecifiedLengths 
01217 
01218   // Class CCfits::Column::InvalidRowNumber 
01219 
01220   // Class CCfits::Column::InsufficientElements 
01221 
01222   // Class CCfits::Column::NoNullValue 
01223 
01224   // Class CCfits::Column::InvalidNumberOfRows 
01225 
01226   // Class CCfits::Column 
01227 
01228   inline void Column::setDimen ()
01229   {
01230   // default implementation: do nothing. Overridden by ColumnVectorData.
01231   }
01232 
01233   inline std::ostream& operator << (std::ostream& s, const Column& right)
01234   {
01235 
01236     return right.put(s);
01237   }
01238 
01239   inline bool operator < (const Column& left, const Column& right)
01240   {
01241 
01242     return left.m_index < right.m_index;
01243   }
01244 
01245   inline bool operator > (const Column& left, const Column& right)
01246   {
01247 
01248     return left.m_index > right.m_index;
01249   }
01250 
01251   inline int Column::index () const
01252   {
01253     return m_index;
01254   }
01255 
01256   inline void Column::index (int value)
01257   {
01258     m_index = value;
01259   }
01260 
01261   inline bool Column::isRead () const
01262   {
01263     return m_isRead;
01264   }
01265 
01266   inline void Column::isRead (bool value)
01267   {
01268     m_isRead = value;
01269   }
01270 
01271   inline long Column::width () const
01272   {
01273     return m_width;
01274   }
01275 
01276   inline void Column::width (long value)
01277   {
01278     m_width = value;
01279   }
01280 
01281   inline size_t Column::repeat () const
01282   {
01283     return m_repeat;
01284   }
01285 
01286   inline bool Column::varLength () const
01287   {
01288     return m_varLength;
01289   }
01290 
01291   inline double Column::scale () const
01292   {
01293     return m_scale;
01294   }
01295 
01296   inline void Column::scale (double value)
01297   {
01298     m_scale = value;
01299     int status(0);
01300     if (fits_set_tscale(fitsPointer(),m_index,value,m_zero,&status)) throw FitsError(status);
01301   }
01302 
01303   inline double Column::zero () const
01304   {
01305     return m_zero;
01306   }
01307 
01308   inline void Column::zero (double value)
01309   {
01310     m_zero = value;
01311   }
01312 
01313   inline const String& Column::display () const
01314   {
01315     return m_display;
01316   }
01317 
01318   inline const String& Column::dimen () const
01319   {
01320     return m_dimen;
01321   }
01322 
01323   inline void Column::dimen (const String& value)
01324   {
01325     m_dimen = value;
01326   }
01327 
01328   inline ValueType Column::type () const
01329   {
01330     return m_type;
01331   }
01332 
01333   inline void Column::type (ValueType value)
01334   {
01335     m_type = value;
01336   }
01337 
01338   inline const String& Column::TBCOL ()
01339   {
01340     return s_TBCOL;
01341   }
01342 
01343   inline const String& Column::TTYPE ()
01344   {
01345     return s_TTYPE;
01346   }
01347 
01348   inline const String& Column::TFORM ()
01349   {
01350     return s_TFORM;
01351   }
01352 
01353   inline const String& Column::TDISP ()
01354   {
01355     return s_TDISP;
01356   }
01357 
01358   inline const String& Column::TUNIT ()
01359   {
01360     return s_TUNIT;
01361   }
01362 
01363   inline const String& Column::TSCAL ()
01364   {
01365     return s_TSCAL;
01366   }
01367 
01368   inline const String& Column::TZERO ()
01369   {
01370     return s_TZERO;
01371   }
01372 
01373   inline const String& Column::TDIM ()
01374   {
01375     return s_TDIM;
01376   }
01377 
01378   inline const String& Column::TNULL ()
01379   {
01380     return s_TNULL;
01381   }
01382 
01383   inline const String& Column::TLMIN ()
01384   {
01385     return s_TLMIN;
01386   }
01387 
01388   inline const String& Column::TLMAX ()
01389   {
01390     return s_TLMAX;
01391   }
01392 
01393   inline const String& Column::TDMAX ()
01394   {
01395     return s_TDMAX;
01396   }
01397 
01398   inline const String& Column::TDMIN ()
01399   {
01400     return s_TDMIN;
01401   }
01402 
01403   inline Table* Column::parent ()
01404   {
01405     return m_parent;
01406   }
01407 
01408   inline const std::vector<String>& Column::columnKeys ()
01409   {
01410     return s_columnKeys;
01411   }
01412 
01413   inline const String& Column::comment () const
01414   {
01415     return m_comment;
01416   }
01417 
01418   inline const String& Column::format () const
01419   {
01420     return m_format;
01421   }
01422 
01423   inline const String& Column::unit () const
01424   {
01425     return m_unit;
01426   }
01427 
01428   inline const String& Column::name () const
01429   {
01430     return m_name;
01431   }
01432 
01433 } // namespace CCfits
01434 
01435 
01436 #endif

Generated on Fri Nov 3 17:09:04 2006 for CCfits by  doxygen 1.4.7