casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ScalarMeasColumn.h
Go to the documentation of this file.
00001 //# ScalarMeasColumn.h: Access to Scalar Measure Columns in Tables.
00002 //# Copyright (C) 1997,1998,1999,2000
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 //# $Id: ScalarMeasColumn.h 21298 2012-12-07 14:53:03Z gervandiepen $
00027 
00028 #ifndef MEASURES_SCALARMEASCOLUMN_H
00029 #define MEASURES_SCALARMEASCOLUMN_H
00030 
00031 //# Includes
00032 #include <measures/TableMeasures/TableMeasColumn.h>
00033 #include <measures/Measures/MeasRef.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 template <class T> class ArrayColumn;
00039 template <class T> class ScalarColumn;
00040 
00041 
00042 // <summary>
00043 // Read only access to table scalar Measure columns.
00044 // </summary>
00045 
00046 // <use visibility=export>
00047 
00048 // <reviewed reviewer="Bob Garwood" date="1999/12/23" tests="tTableMeasures.cc">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //# Classes you should understand before using this one.
00053 //   <li> <linkto module=Measures>Measures</linkto>
00054 //   <li> <linkto module=Tables>Tables</linkto>
00055 //   <li> <linkto class=TableMeasDesc>TableMeasDesc</linkto>
00056 // </prerequisite>
00057 
00058 // <synopsis>
00059 // ScalarMeasColumn objects can be used to access scalar Measure Columns
00060 // in tables, both for reading and writing (if the table is writable).
00061 //
00062 // Before a column can be accessed it must have previously been defined as
00063 // a Measure column by use of the
00064 // <linkto class="TableMeasDesc">TableMeasDesc</linkto> object.
00065 //
00066 // The ScalarMeasColumn class is templated on Measure type.
00067 // Typedefs exist in the various Measure classes
00068 // (e.g. <linkto class=MEpoch>MEpoch</linkto>) to make declaration
00069 // less long winded.
00070 // Constructing scalar Measure column objects using these typedefs looks like
00071 // this:
00072 // <srcblock>
00073 // MEpoch::ScalarMeasColumn ec(table, "ColumnName);
00074 // </srcblock>
00075 //
00076 // <h3>Reading and writing Measures</h3>
00077 //
00078 // The reading and writing of Measures columns is very similar to reading and
00079 // writing of "ordinary" Table columns.
00080 // <linkto class="ScalarMeasColumn#get">get()</linkto>
00081 // and <linkto class="ScalarMeasColumn#get">operator()</linkto>
00082 // exist for reading Measures and the
00083 // <linkto class="ScalarMeasColumn#put">put()</linkto> member for adding
00084 // Measures to a column.  (put() is obviously not defined for
00085 // ScalarMeasColumn objects.)  Each of these members accepts a row number
00086 // as an argument.
00087 // The get() function gets the measure with the reference and offset as
00088 // it is stored in the column. Furthermore the convert() function is
00089 // available to get the measure with the given reference, possible offset,
00090 // and possible frame
00091 //
00092 // When a Measure is put, the reference and possible offset are converted
00093 // if the measure column is defined with a fixed reference and/or offset.
00094 // If the column's reference and offset are variable, the reference and
00095 // offset of the measure as put are written into the appropriate
00096 // reference and offset columns.
00097 // </synopsis>
00098 
00099 // <example>
00100 // <srcblock>
00101 //     // This creates a Scalar MEpoch column for read/write access.  Column
00102 //     // "Time1" must exist in Table "tab" and must have previously been
00103 //     // defined as a MEpoch column using a TableMeasDesc.
00104 //     MEpoch::ScalarMeasColumn timeCol(tab, "Time1");
00105 //      
00106 //     // print some details about the column
00107 //     if (timeCol.measDesc().isRefCodeVariable()) {
00108 //        cout << "The column has variable references." << endl;
00109 //     } else {
00110 //         cout << "The fixed MeasRef for the column is: "
00111 //              << timeCol.getMeasRef() << endl;
00112 //     }
00113 //
00114 //     // Add tab.nrow() measures to the column.        
00115 //     MEpoch tm(Quantity(MeasData::MJD2000, "d"), MEpoch::TAI);
00116 //     for (uInt i=0; i<tab.nrow(); i++) {
00117 //         timeCol.put(i, tm);
00118 //     }
00119 //
00120 //     // We could read from the column using timeCol but instead a read
00121 //     // only column object is created.
00122 //     MEpoch::ScalarMeasColumn timeColRead(tab, "Time1");
00123 //     for (i=0; i<tab.nrow(); i++) {
00124 //         cout << timeColRead(i) << endl;
00125 //     }
00126 // </srcblock>
00127 // </example>
00128 
00129 // <motivation>
00130 // The standard Casacore Table system does not support Measures columns.
00131 // This class overcomes this limitation.
00132 // </motivation>
00133 //
00134 // <thrown>
00135 //    <li>AipsError during construction if the column specified variable
00136 //        offsets which are stored in an Array- rather than a ScalarColumn.
00137 // </thrown>
00138 //
00139 //# <todo asof="$DATE:$">
00140 //# </todo>
00141 
00142 template <class M> class ScalarMeasColumn : public TableMeasColumn
00143 {
00144 public:
00145   // The default constructor creates a null object.  Useful for creating
00146   // arrays of ScalarMeasColumn objects.  Attempting to use a null object
00147   // will produce a segmentation fault so care needs to be taken to
00148   // initialize the objects first by using attach().
00149   // An ScalarMeasColumn object can be tested if it is null by using the
00150   // isNull() member.
00151   ScalarMeasColumn();
00152 
00153   // Create the ScalarMeasColumn from the table and column Name.
00154   ScalarMeasColumn (const Table& tab, const String& columnName);
00155 
00156   // Copy constructor (copy semantics).
00157   ScalarMeasColumn (const ScalarMeasColumn<M>& that);
00158 
00159   virtual ~ScalarMeasColumn();
00160 
00161   // Change the reference to another column.
00162   void reference (const ScalarMeasColumn<M>& that);
00163 
00164   // Attach a column to the object.
00165   void attach (const Table& tab, const String& columnName);
00166 
00167   // Get the Measure contained in the specified row.
00168   // It returns the Measure as found in the table.
00169   // <group name=get>
00170   void get (uInt rownr, M& meas) const;
00171   M operator() (uInt rownr) const;
00172   // </group>
00173 
00174   // Get the Measure contained in the specified row and convert
00175   // it to the reference and offset found in the given measure.
00176   M convert (uInt rownr, const M& meas) const
00177     { return convert (rownr, meas.getRef()); }
00178 
00179   // Get the Measure contained in the specified row and convert
00180   // it to the given reference.
00181   // <group>
00182   M convert (uInt rownr, const MeasRef<M>& measRef) const;
00183   M convert (uInt rownr, uInt refCode) const;
00184   // </group>
00185 
00186   // Returns the column's fixed reference or the reference of the last
00187   // read Measure if references are variable.
00188   const MeasRef<M>& getMeasRef() const
00189     { return itsMeasRef; }
00190 
00191   // Reset the refCode, offset, or units.
00192   // It overwrites the value used when defining the TableMeasDesc.
00193   // Resetting the refCode and offset can only be done if they were
00194   // defined as fixed in the description.
00195   // <note role=tip>
00196   // In principle the functions can only be used if the table is empty,
00197   // otherwise already written values have thereafter the incorrect
00198   // reference, offset, or unit.
00199   // However, it is possible that part of the table is already
00200   // written and that the entire measure column is filled in later.
00201   // In that case the reference, offset, or units can be set by using
00202   // a False <src>tableMustBeEmpty</src> argument.
00203   // </note>
00204   // <group>
00205   void setDescRefCode (uInt refCode, Bool tableMustBeEmpty=True);
00206   void setDescOffset (const Measure& offset, Bool tableMustBeEmpty=True);
00207   void setDescUnits (const Vector<Unit>& units, Bool tableMustBeEmpty=True);
00208   // </group>
00209 
00210   // Put a Measure into the given row.
00211   // <group name=put>
00212   void put (uInt rownr, const M& meas);
00213   // </group>
00214 
00215 protected:
00216   // Make a MeasRef for the given row.
00217   MeasRef<M> makeMeasRef (uInt rownr) const;
00218 
00219 private:
00220   //# Whether conversion is needed during a put.  True if either
00221   //# the reference code or offset is fixed for the column
00222   Bool itsConvFlag;
00223   //# Column which contains the Measure's actual data. An array column
00224   //# is needed if the data component of the underlying Measure is
00225   //# represented by more than 1 value
00226   ArrayColumn<Double>* itsArrDataCol;
00227   ScalarColumn<Double>* itsScaDataCol;
00228   //# Its MeasRef code column when references are variable.
00229   ScalarColumn<Int>* itsRefIntCol;
00230   ScalarColumn<String>* itsRefStrCol;
00231   //# Column containing its variable offsets. Only applicable if the
00232   //# measure references have offsets and they are variable.
00233   ScalarMeasColumn<M>* itsOffsetCol;
00234   //# This is either the column's fixed Measure reference or the reference
00235   //# of the last Measure read.
00236   MeasRef<M> itsMeasRef;
00237 
00238 
00239   // Assignment makes no sense in a readonly class.
00240   // Declaring this operator private makes it unusable.
00241   ScalarMeasColumn& operator= (const ScalarMeasColumn<M>& that);
00242 
00243   // Check if refs have the same value (as opposed to being the same object).
00244   Bool equalRefs (const MRBase& r1, const MRBase& r2) const;
00245 
00246   //# Deletes allocated memory etc. Called by ~tor and any member which
00247   //# needs to reallocate data.
00248   void cleanUp();
00249 };
00250 
00251 
00252 } //# NAMESPACE CASA - END
00253 
00254 
00255 //# Make old name ROScalarMeasColumn still available.
00256 #define ROScalarMeasColumn ScalarMeasColumn
00257 
00258 
00259 #ifndef CASACORE_NO_AUTO_TEMPLATES
00260 #include <measures/TableMeasures/ScalarMeasColumn.tcc>
00261 #endif //# CASACORE_NO_AUTO_TEMPLATES
00262 #endif