CCfits
2.5
|
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 protected: 01207 Column (int columnIndex, // The column index, i.e. the integer n in the keyword TCOLn 01208 const String &columnName, // The column name, curiously TTYPEn 01209 ValueType type, const String &format, // The TFORMn keyword. 01210 const String &unit, // The TUNITn keyword 01211 Table* p, // ! The Table containing the Column object 01212 int rpt = 1, long w = 1, const String &comment = ""); 01213 Column (Table* p = 0); 01214 01215 virtual bool compare (const Column &right) const; 01216 fitsfile* fitsPointer (); 01217 // Protected method to set the current HDU to be the one containing this Column object. For use in 01218 // public read/write methods to ensure that data regarding numbers of rows and width relate to the 01219 // right HDU 01220 void makeHDUCurrent (); 01221 virtual std::ostream& put (std::ostream& s) const; 01222 void varLength (bool value); 01223 static const String& TBCOL (); 01224 static const String& TTYPE (); 01225 static const String& TUNIT (); 01226 static const String& TNULL (); 01227 static const String& TLMIN (); 01228 static const String& TLMAX (); 01229 static const String& TDMAX (); 01230 static const String& TDMIN (); 01231 static const std::vector<String>& columnKeys (); 01232 const String& comment () const; 01233 01234 // Additional Protected Declarations 01235 01236 private: 01237 Column & operator=(const Column &right); 01238 01239 // Insert one or more blank rows into a FITS column. 01240 virtual void insertRows (long first, long number = 1) = 0; 01241 virtual void deleteRows (long first, long number = 1) = 0; 01242 static void loadColumnKeys (); 01243 void name (const String& value); 01244 void format (const String& value); 01245 long numberOfElements (long& first, long& last); 01246 01247 // Data Members for Class Attributes 01248 static const String s_TBCOL; 01249 static const String s_TTYPE; 01250 static const String s_TFORM; 01251 static const String s_TDISP; 01252 static const String s_TUNIT; 01253 static const String s_TSCAL; 01254 static const String s_TZERO; 01255 static const String s_TDIM; 01256 static const String s_TNULL; 01257 static const String s_TLMIN; 01258 static const String s_TLMAX; 01259 static const String s_TDMAX; 01260 static const String s_TDMIN; 01261 01262 // Additional Private Declarations 01263 01264 private: //## implementation 01265 // Data Members for Class Attributes 01266 int m_index; 01267 bool m_isRead; 01268 long m_width; 01269 size_t m_repeat; 01270 bool m_varLength; 01271 double m_scale; 01272 double m_zero; 01273 String m_display; 01274 String m_dimen; 01275 ValueType m_type; 01276 static const short LLIMITSHORT; 01277 static const long LLIMITLONG; 01278 static const unsigned short LLIMITUSHORT; 01279 static const unsigned long LLIMITULONG; 01280 static const unsigned char LLIMITUCHAR; 01281 static const float LLIMITFLOAT; 01282 static const double LLIMITDOUBLE; 01283 static const short ULIMITSHORT; 01284 static const long ULIMITLONG; 01285 static const unsigned short ULIMITUSHORT; 01286 static const unsigned long ULIMITULONG; 01287 static const unsigned char ULIMITUCHAR; 01288 static const float ULIMITFLOAT; 01289 static const double ULIMITDOUBLE; 01290 static const int LLIMITINT; 01291 static const int ULIMITINT; 01292 static const unsigned int LLIMITUINT; 01293 static const unsigned int ULIMITUINT; 01294 static const LONGLONG LLIMITLONGLONG; 01295 static const LONGLONG ULIMITLONGLONG; 01296 01297 // Data Members for Associations 01298 Table* m_parent; 01299 static std::vector<String> s_columnKeys; 01300 String m_comment; 01301 String m_format; 01302 String m_unit; 01303 String m_name; 01304 01305 // Additional Implementation Declarations 01306 01307 }; 01308 01309 // Class CCfits::Column::RangeError 01310 01311 // Class CCfits::Column::InvalidDataType 01312 01313 // Class CCfits::Column::InvalidRowParameter 01314 01315 // Class CCfits::Column::WrongColumnType 01316 01317 // Class CCfits::Column::UnspecifiedLengths 01318 01319 // Class CCfits::Column::InvalidRowNumber 01320 01321 // Class CCfits::Column::InsufficientElements 01322 01323 // Class CCfits::Column::NoNullValue 01324 01325 // Class CCfits::Column::InvalidNumberOfRows 01326 01327 // Class CCfits::Column 01328 01329 inline void Column::setDimen () 01330 { 01331 // default implementation: do nothing. Overridden by ColumnVectorData. 01332 } 01333 01334 inline std::ostream& operator << (std::ostream& s, const Column& right) 01335 { 01336 01337 return right.put(s); 01338 } 01339 01340 inline bool operator < (const Column& left, const Column& right) 01341 { 01342 01343 return left.m_index < right.m_index; 01344 } 01345 01346 inline bool operator > (const Column& left, const Column& right) 01347 { 01348 01349 return left.m_index > right.m_index; 01350 } 01351 01352 inline void Column::resetRead () 01353 { 01354 m_isRead = false; 01355 } 01356 01357 inline int Column::index () const 01358 { 01359 return m_index; 01360 } 01361 01362 inline void Column::index (int value) 01363 { 01364 m_index = value; 01365 } 01366 01367 inline bool Column::isRead () const 01368 { 01369 return m_isRead; 01370 } 01371 01372 inline void Column::isRead (bool value) 01373 { 01374 m_isRead = value; 01375 } 01376 01377 inline long Column::width () const 01378 { 01379 return m_width; 01380 } 01381 01382 inline void Column::width (long value) 01383 { 01384 m_width = value; 01385 } 01386 01387 inline size_t Column::repeat () const 01388 { 01389 return m_repeat; 01390 } 01391 01392 inline bool Column::varLength () const 01393 { 01394 return m_varLength; 01395 } 01396 01397 inline double Column::scale () const 01398 { 01399 return m_scale; 01400 } 01401 01402 inline void Column::scale (double value) 01403 { 01404 m_scale = value; 01405 int status(0); 01406 if (fits_set_tscale(fitsPointer(),m_index,value,m_zero,&status)) throw FitsError(status); 01407 } 01408 01409 inline double Column::zero () const 01410 { 01411 return m_zero; 01412 } 01413 01414 inline void Column::zero (double value) 01415 { 01416 m_zero = value; 01417 } 01418 01419 inline const String& Column::display () const 01420 { 01421 return m_display; 01422 } 01423 01424 inline const String& Column::dimen () const 01425 { 01426 return m_dimen; 01427 } 01428 01429 inline void Column::dimen (const String& value) 01430 { 01431 m_dimen = value; 01432 } 01433 01434 inline ValueType Column::type () const 01435 { 01436 return m_type; 01437 } 01438 01439 inline void Column::type (ValueType value) 01440 { 01441 m_type = value; 01442 } 01443 01444 inline const String& Column::TBCOL () 01445 { 01446 return s_TBCOL; 01447 } 01448 01449 inline const String& Column::TTYPE () 01450 { 01451 return s_TTYPE; 01452 } 01453 01454 inline const String& Column::TFORM () 01455 { 01456 return s_TFORM; 01457 } 01458 01459 inline const String& Column::TDISP () 01460 { 01461 return s_TDISP; 01462 } 01463 01464 inline const String& Column::TUNIT () 01465 { 01466 return s_TUNIT; 01467 } 01468 01469 inline const String& Column::TSCAL () 01470 { 01471 return s_TSCAL; 01472 } 01473 01474 inline const String& Column::TZERO () 01475 { 01476 return s_TZERO; 01477 } 01478 01479 inline const String& Column::TDIM () 01480 { 01481 return s_TDIM; 01482 } 01483 01484 inline const String& Column::TNULL () 01485 { 01486 return s_TNULL; 01487 } 01488 01489 inline const String& Column::TLMIN () 01490 { 01491 return s_TLMIN; 01492 } 01493 01494 inline const String& Column::TLMAX () 01495 { 01496 return s_TLMAX; 01497 } 01498 01499 inline const String& Column::TDMAX () 01500 { 01501 return s_TDMAX; 01502 } 01503 01504 inline const String& Column::TDMIN () 01505 { 01506 return s_TDMIN; 01507 } 01508 01509 inline const std::vector<String>& Column::columnKeys () 01510 { 01511 return s_columnKeys; 01512 } 01513 01514 inline const String& Column::comment () const 01515 { 01516 return m_comment; 01517 } 01518 01519 inline const String& Column::format () const 01520 { 01521 return m_format; 01522 } 01523 01524 inline const String& Column::unit () const 01525 { 01526 return m_unit; 01527 } 01528 01529 inline const String& Column::name () const 01530 { 01531 return m_name; 01532 } 01533 01534 } // namespace CCfits 01535 01536 01537 #endif