casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MSTable.h
Go to the documentation of this file.
00001 //# MSTable.h: A Table to hold astronomical data (a set of Measurements)
00002 //# Copyright (C) 1996,1997,2000,2001,2002
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: MSTable.h 20299 2008-04-03 05:56:44Z gervandiepen $
00028 
00029 #ifndef MS_MSTABLE_H
00030 #define MS_MSTABLE_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Utilities/DataType.h>
00034 #include <tables/Tables/Table.h>
00035 #include <casa/Containers/SimOrdMap.h>
00036 #include <casa/Utilities/CountedPtr.h>
00037 #include <casa/BasicSL/String.h>
00038 #include <tables/Tables/ColumnDesc.h>
00039 
00040 namespace casa { //# NAMESPACE CASA - BEGIN
00041 
00042 //# Forward Declarations, more could be if they weren't part of the
00043 //# static classes 
00044 class TableRecord;
00045 template <class T> class Block;
00046 
00047 // <summary> 
00048 // A Table intended to hold astronomical data
00049 // </summary>
00050 
00051 // <use visibility=export>
00052 
00053 // <reviewed reviewer="Bob Garwood" date="1997/02/01" tests="" demos="">
00054 
00055 // <prerequisite>
00056 //   <li> <linkto module="Tables:description">Tables</linkto> module
00057 // </prerequisite>
00058 //
00059 // <etymology>
00060 // The MSTable is the base class for all MeasurementSet Tables, hence the
00061 // name.
00062 // </etymology>
00063 //
00064 // <synopsis> 
00065 // A MSTable is a Table.  Most operations on a MSTable are
00066 // Table operations. See the <linkto module="Tables:description">Tables</linkto> 
00067 // module for a list of those operations.  The member functions provided by this
00068 // class are primarily convenience functions to help users follow the 
00069 // agreed upon column and keyword naming conventions.  They are useful when
00070 // creating a Table following the MSTable conventions from
00071 // scratch as well as when creating the column objects to access those
00072 // columns. All actual MeasurementSet Tables will be derived from this class.
00073 //
00074 // The standard way of accessing
00075 // table columns is through Strings.  Mistakes in typing the column
00076 // name will not be caught at compile time (and may not be caught at
00077 // run time).  We have therefore decided to use an enumeration
00078 // to specify columns so that many mistakes will be caught at compile
00079 // time.  This requires functions to map to and from this enumeration
00080 // to the strings that are ultimately used. 
00081 //
00082 // Upon destruction, the table is checked to see that all
00083 // required columns and keywords are still present.
00084 // If not an exception is thrown.  Nevertheless,
00085 // the table will be flushed to disk if it is writable -
00086 // preserving its state.
00087 //
00088 // </synopsis> 
00089 //
00090 // <example>
00091 // For examples of use, see the MeasurementSet class.
00092 // </example>
00093 //
00094 // <motivation>
00095 // The Table module is more than adequate as a container of data.  
00096 // However, in order for applications to be useful with data from 
00097 // different sources, some conventions need to be adopted in the use 
00098 // of Tables to store data.  The MSTable provides the framework for
00099 // these conventions and conversion functions. The actual definitions
00100 // of columns and keywords are found in the derived classes and their
00101 // "enum" base class (e.g. MSAntenna and MSAntennaEnums).
00102 // </motivation>
00103 //
00104 // <todo asof="1996/2/22">
00105 // <li> referenceCopy() should be more flexible with the storage managers used 
00106 //      for the columns which are not merely references.
00107 // <li> When ForwardColumnEngine is fixed so that it can deal with
00108 //      tables already in the cache, modify the test program.  It may also
00109 //      be necessary to modify referenceCopy().
00110 // </todo>
00111 
00112 template <class ColEnum, class KeyEnum> class MSTable : public Table 
00113 {
00114 public:
00115     // ColEnum convenience functions
00116     // <group name=columns>
00117     // check to see if a column exists
00118     Bool isColumn(ColEnum which) const;
00119 
00120     // check to see if a column is writable
00121     // <group>
00122     Bool isColumnWritable(ColEnum which) const;
00123     Bool isColumnWritable (const String& columnName) const 
00124     { return Table::isColumnWritable(columnName); }
00125     Bool isColumnWritable (uInt columnIndex) const
00126     { return Table::isColumnWritable(columnIndex); }
00127     // </group>
00128 
00129     // Information about scalar vs array of a column
00130     // <group>
00131     Bool isScalar(ColEnum which) const;
00132     Bool isArray(ColEnum which) const;  
00133     // </group>
00134 
00135     // Return the UNIT keyword value associated with the specified column
00136     // <group>
00137     const String& unit(const String& which) const;
00138     const String& unit(ColEnum which) const
00139     { return unit(columnName(which)); }
00140     // </group>
00141 
00142     // Convert a ColEnum to the actual column name.
00143     static const String& columnName(ColEnum which);
00144     // Convert a name to a ColEnum
00145     static ColEnum columnType(const String &name);
00146     //  return the data type for a given ColEnum
00147     static DataType columnDataType(ColEnum which);
00148     // return the standard comment for a given ColEnum
00149     static const String& columnStandardComment(ColEnum which);
00150     // return the UNIT string for a given ColEnum
00151     static const String& columnUnit(ColEnum which);
00152     // return the MEASURE_TYPE string for a given ColEnum
00153     static const String& columnMeasureType(ColEnum which);
00154 
00155     // add a column to a TableDesc
00156     // An exception is thrown for an invalid data type.  This indicates a 
00157     // programming error in this class when this occurs.
00158     // For Array columns you can optionally define the dimension here.
00159     // For Measure columns you can define a variable reference column.
00160     // <thrown>
00161     //   <li> AipsError
00162     // </thrown>
00163     static void addColumnToDesc(TableDesc & tabDesc, ColEnum which,
00164                                 Int ndim=-1,const String& refCol="");
00165     // add a column to a TableDesc, defining the shape and setting
00166     // the ColumnDesc option (Fixed, Undefined, Direct) 
00167     // For Measure columns you can define a variable reference column.
00168     static void addColumnToDesc(TableDesc & tabDesc, ColEnum which,
00169                           const IPosition& shape, ColumnDesc::Option option,
00170                                 const String& refCol="");
00171 
00172     // </group>
00173 
00174     // KeyEnum convenience functions
00175     // <group name=keywords>
00176     static const String& keywordName(KeyEnum which);
00177     static KeyEnum keywordType(const String &name);
00178     static DataType keywordDataType(KeyEnum which);
00179     static const String& keywordStandardComment(KeyEnum which);
00180 
00181     // check to see if a keyword exists
00182     Bool isKeyword(KeyEnum which) const;
00183 
00184     // add a keyword to a TableDesc
00185     // An exception is thrown for an invalid data type.  This indicates a 
00186     // missing data type in the code..
00187     // <thrown>
00188     //   <li> AipsError
00189     // </thrown>
00190     static void addKeyToDesc(TableDesc & tabDesc, KeyEnum key);
00191 
00192     // </group>
00193 
00194     // tableDesc convenience functions
00195     // <group>
00196  
00197     // check that a TableDesc is valid 
00198     static Bool validate(const TableDesc& tabDesc);
00199  
00200     // check that the keyword set is valid 
00201     static Bool validate(const TableRecord& tabKeySet);
00202  
00203     // validate self (make sure that this MS is valid)
00204     Bool validate() const 
00205       { return this->isNull() ? False : validate(this->tableDesc());}
00206  
00207     // return the required table description
00208     static const TableDesc& requiredTableDesc();
00209 
00210     // Add the compress option for the given column to the TableDesc.
00211     // It can only be used for a Float or a Complex column.
00212     // For complex columns the type determines which CompressComplex
00213     // engine is used. "SD" means that CompressComplexSD is used; otherwise
00214     // CompressComplex is used.
00215     static void addColumnCompression (TableDesc& td, ColEnum which,
00216                                       Bool autoScale = True,
00217                                       const String& type = String());
00218 
00219     // </group>
00220 
00221 protected:
00222     // These constructors mirror the Table ones
00223     // <group name=tableLikeConstructors>
00224     // Default constructor for use by derived classes
00225     MSTable ();
00226     MSTable (const String &tableName, TableOption option);
00227     MSTable (const String &tableName, const TableLock& lockOptions,
00228              TableOption option);
00229     MSTable (const String &tableName, const String &tableDescName,
00230              TableOption option);
00231     MSTable (const String &tableName, const String &tableDescName,
00232              const TableLock& lockOptions, TableOption option);
00233     MSTable (SetupNewTable &newTab, uInt nrrow,
00234              Bool initialize);
00235     MSTable (SetupNewTable &newTab, const TableLock& lockOptions, uInt nrrow,
00236              Bool initialize);
00237     MSTable (const Table &table);
00238     MSTable (const MSTable<ColEnum,KeyEnum> &other);
00239     // </group>
00240     ~MSTable();
00241 
00242     //  Assignment operator, reference semantics
00243     MSTable& operator=(const MSTable<ColEnum,KeyEnum>&);
00244  
00245     // These are the static ordered maps which contain the above info
00246     // ColEnum -> name
00247     static SimpleOrderedMap<Int, String> columnMap_p;
00248     // ColEnum -> DataType
00249     static SimpleOrderedMap<Int, Int> colDTypeMap_p;
00250     // ColEnum -> comment string
00251     static SimpleOrderedMap<Int, String> colCommentMap_p;
00252     // ColEnum -> UNIT string
00253     static SimpleOrderedMap<Int, String> colUnitMap_p;
00254     // ColEnum -> MEASURE_TYPE string
00255     static SimpleOrderedMap<Int, String> colMeasureTypeMap_p;
00256  
00257 
00258     // KeyEnum -> name
00259     static SimpleOrderedMap<Int, String> keywordMap_p;
00260     // KeyEnum -> DataType
00261     static SimpleOrderedMap<Int, Int> keyDTypeMap_p;
00262     // KeyEnum -> comment string
00263     static SimpleOrderedMap<Int, String> keyCommentMap_p;
00264 
00265     // The required TableDesc
00266     //# following fails in static initialization (segm. fault).
00267     //    static TableDesc requiredTD_p;
00268     static SimpleCountedConstPtr<TableDesc> requiredTD_p;
00269  
00270     // Define an entry in the column maps
00271     static void colMapDef(ColEnum col,
00272                           const String& colName,
00273                           DataType colType,
00274                           const String& colComment,
00275                           const String& colUnit="",
00276                           const String& colMeasureType="");
00277 
00278     // Define an entry in the keyword maps
00279     static void keyMapDef(KeyEnum key,
00280                           const String& keyName,
00281                           DataType keyType,
00282                           const String& keyComment);
00283 
00284     // Return a table that references all columns in this table except for
00285     // those given in writableColumns, those are empty and writable.
00286     Table referenceCopy(const String& newTableName, 
00287                         const Block<String>& writableColumns) const;
00288 };
00289 
00290 
00291 } //# NAMESPACE CASA - END
00292 
00293 // #ifndef CASACORE_NO_AUTO_TEMPLATES
00294 // #include <ms/MeasurementSets/MSTable.tcc>
00295 // #endif //# CASACORE_NO_AUTO_TEMPLATES
00296 #endif