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