casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TableQuantumDesc.h
Go to the documentation of this file.
00001 //# TableQuantumDesc.h: Defines a Quantum column in a Table.
00002 //# Copyright (C) 1997,1998,1999,2000,2001
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: TableQuantumDesc.h 21298 2012-12-07 14:53:03Z gervandiepen $
00027 
00028 #ifndef MEASURES_TABLEQUANTUMDESC_H
00029 #define MEASURES_TABLEQUANTUMDESC_H
00030 
00031 //# Includes
00032 #include <casa/Arrays/Vector.h>
00033 #include <casa/BasicSL/String.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 class TableDesc;
00039 class Table;
00040 class TableRecord;
00041 class TableColumn;
00042 class Unit;
00043 
00044 
00045 // <summary>
00046 // A class for defining Quantum columns in Tables.
00047 // </summary>
00048 
00049 // <use visibility=export>
00050 
00051 // <reviewed reviewer="Bob Garwood" date="1999/12/23" tests="tTableQuantum.cc">
00052 // </reviewed>
00053 
00054 // <prerequisite>
00055 //# Classes you should understand before using this one.
00056 //   <li> <linkto class=Table>Table</linkto>
00057 //   <li> <linkto class=Quantum>Quantum</linkto>
00058 // </prerequisite>
00059 
00060 // <synopsis>
00061 // A TableQuantumDesc object is used to define a Quantum column in a Table.
00062 // The use of this object and the associated Scalar- and ArrayQuantColumn
00063 // objects make it possible to store (and retrieve) Quanta in Tables.<br>
00064 //
00065 // TableQuantumDesc objects are analogous to ColumnDesc objects in that they
00066 // add information, describing the characteristics of a column, to the Table
00067 // Descriptor before the Table is created.  However, rather than
00068 // replacing the use of a ColumnDesc object, a TableQuantumDesc is
00069 // used in conjunction with a ColumnDesc in the definition of
00070 // Quantum columns.<br>
00071 //
00072 //   <note role=caution>
00073 //      A good understanding of the Table system is essential
00074 //      before attempting to use this class.
00075 //   </note>
00076 //
00077 // Defining a Quantum column requires the following steps:
00078 // <ol>
00079 // <li> Use a normal Scalar- or ArrayColumnDesc to define a column to use for
00080 //      the Quanta.
00081 // <li> If needed (see
00082 //      <A HREF="#TableQuantumDesc:Quantum Units">below</A>) define a column
00083 //      for the Quantum Units.
00084 // <li> Add the columns to the Table Descriptor.
00085 // <li> Declare a TableQuantumDesc to associate the column defined in step 1
00086 //      and the Unit column from step 2 and update the Table Descriptor.
00087 // <li> Setup and create the Table.
00088 // </ol>
00089 // It is also possible to define a Quantum column after the table is created.
00090 // which is useful when columns (to be used for quanta) are added to
00091 // an already existing table. <br>
00092 //
00093 // The type of the quantum columns must match the type of the underlying
00094 // Quanta that are to be stored in the column.  Hence, for a column of
00095 // Quantum&lt;Complex&gt; a ScalarColumnDesc&lt;Complex&gt; must be used.<br>
00096 //
00097 // As with standard Table Columns Quanta can be stored in Scalar and Array
00098 // columns. This must be specified in advance by using either a
00099 // Scalar- or ArrayColumnDesc.<br>
00100 //
00101 // After the Table has be created a Quantum column can be accessed for writing
00102 // and reading of Quanta via the
00103 // <linkto class="ScalarQuantColumn">(RO)ScalarQuantColumn&lt;T&gt;</linkto>
00104 // and
00105 // <linkto class="ArrayQuantColumn">(RO)ArrayQuantColumn&lt;T&gt;</linkto>
00106 // objects.
00107 //
00108 // <ANCHOR NAME="TableQuantumDesc:Quantum Units">
00109 // <h3>Quantum Units</h3></ANCHOR>
00110 // The treatment of the Unit component of a Quantum in the TableQuantumDesc
00111 // class varies depending on your needs.    The main consideration
00112 // is whether the Quanta to be stored in a specific column are to have the
00113 // same Unit or whether their Units could differ.  In the simple case,
00114 // where the
00115 // Quanta have the same unit, a TableQuantumDesc is declared with the
00116 // Unit value specified as a parameter. The following defines a Quantum
00117 // column with units "deg":
00118 //
00119 // <srcblock>
00120 //      ScalarColumnDesc<Double> scd("QuantumCol");
00121 //      ...
00122 //      // defines QuantumCol as a Quantum column with fix Units "deg"
00123 //      TableQuantumDesc tqd(td, "QuantumCol", Unit("deg"));
00124 // </srcblock>
00125 //
00126 // This constructor stores the value for the Unit as a
00127 // column keyword.  In situations, however, where it is necessary to
00128 // store a distinct Unit with each Quantum, it is necessary to define
00129 // an additional column for storing the Unit component of each Quantum.
00130 // The TableQuantumDesc constructor for this takes the name of
00131 // the Unit column as
00132 // a parameter.  Hence an additional column must be defined for storing the
00133 // Units and its type must be string.  The following
00134 // example shows how to set up a Quantum column with support for Quantum
00135 // unit variability:
00136 //
00137 // <srcblock>
00138 //      // the quanta values stored here
00139 //      ScalarColumnDesc<Double> scd("QuantumCol");
00140 //      // a String column for the Units
00141 //      ScalarColumnDesc<String> scd("QuantumUnitCol");
00142 //      ...
00143 //      TableQuantumDesc tqd(td, "QuantumCol", "QuantumUnitCol");
00144 // </srcblock>
00145 //
00146 // One further consideration is that for Array Quantum Columns it is
00147 // necessary to
00148 // decide on a level of granularity for the Unit storage you would like.
00149 // In Array Quantum columns it is possible to store a distinct Unit per row or
00150 // per array element per row.  This distinction is established when the
00151 // Unit column is declared.  Defining a ScalarColumn for Units specifies per
00152 // row variability, that is, each row in an array column of Quanta will
00153 // have the same unit.  Alternatively, use of an ArrayColumn for the Unit
00154 // column
00155 // specifies that every Quantum stored will have its unit stored as well.
00156 // In both cases the Unit column's type must be String.  The following
00157 // defines an Array Quantum Column with per row Unit storage:
00158 //
00159 // <srcblock>
00160 //      // for the Quanta values
00161 //      ArrayColumnDesc<Double> scd("ArrayQuantumCol");
00162 //      // per row storage of units
00163 //      ScalarColumnDesc<String> scd("QuantumUnitCol");
00164 //      ...
00165 //      TableQuantumDesc tqd(td, "ArrayQuantumCol", "QuantumUnitCol");
00166 // </srcblock>
00167 //
00168 // And finally, an array Quantum Column with an Array Unit Column:
00169 //
00170 // <srcblock>
00171 //      // for Quanta values
00172 //      ArrayColumnDesc<Double> scd("ArrayQuantumCol");
00173 //      // per element storage of Units
00174 //      ArrayColumnDesc<String> scd("ArrayUnitCol");
00175 //      ...
00176 //      TableQuantumDesc tqd(td, "ArrayQuantumCol", "ArrayUnitCol");
00177 // </srcblock>
00178 //
00179 //
00180 // After constructing an TableQuantumDesc object use of the write() member
00181 // updates the Table Descriptor or Table object.
00182 // <linkto class="ScalarQuantColumn">(RO)ScalarQuantColumn&lt;T&gt;</linkto>
00183 // and
00184 // <linkto class="ArrayQuantColumn">(RO)ArrayQuantColumn&lt;T&gt;</linkto>
00185 // are subsequently used to read-only and read/write access the Quantum
00186 // Columns.
00187 // </synopsis>
00188 //
00189 // <example>
00190 // <srcblock>
00191 //     // create a table descriptor as normal
00192 //     TableDesc td("measTD", "1", TableDesc::New);
00193 //     td.comment() = "A table containing measures and quantums";
00194 //
00195 //     // This example sets up a Quantum<Complex> column but any valid Quantum
00196 //     // type can be specified.  However, the type of the Quantums to be
00197 //     // stored must match the type of the underlying table column.
00198 //     ScalarColumnDesc<Complex> tcdQCplx("Quant", "A quantum complex column");
00199 //
00200 //     // For a Quantum array column an ArrayColumnDesc is first defined
00201 //     ArrayColumnDesc<Double> tcdQDoub("QuantArray", "A quantum array col");
00202 //
00203 //     // The QuantumArray column has variable units.  A string is needed
00204 //     // for these.  This could be done in two ways depending on what is
00205 //     // wanted.  Units can vary per element of array per row or
00206 //     // just per row.  In the first instance an ArrayColumn<String> would be
00207 //     // require.  Here we want to vary units only per row.
00208 //     ScalarColumnDesc<String> tcdUnits("VarQuantUnits", "Quantum units");
00209 //
00210 //     // Add the columns to the Table Descriptor
00211 //     td.addColumn(tcdQplx);
00212 //     td.addColumn(tcdQDoub);
00213 //     td.addColumn(tcdUnits);
00214 //
00215 //     // Create the TableQuantumDesc with units "deg" and an Array Quantum
00216 //     // Column with per row Unit granularity
00217 //     TableQuantumDesc tqdS(td, "Quant", unit("deg"));
00218 //     TableQuantumDesc tqdA(td, "QuantArray", "VarQuantUnits");
00219 //
00220 //     // Update the Table Descriptor
00221 //     tqdA.write(td);
00222 //     tqdS.write(td);
00223 //
00224 //     // Setup and create the new table as usual.
00225 //     SetupNewTable newtab("mtab", td, Table::New);
00226 //     Table qtab(newtab);
00227 //
00228 //     // Now ScalarQuantColumn and ArrayQuantColumn objects could be
00229 //     // constructed to access the columns...
00230 // </srcblock>
00231 // Note that writing the Quantum description could also be done
00232 // after the table is created. It is meaningless in this case, but
00233 // it is useful when columns (to be used for quanta) are added to
00234 // an already existing table.
00235 // be used as 
00236 // <srcblock>
00237 //     // Setup and create the new table as usual.
00238 //     SetupNewTable newtab("mtab", td, Table::New);
00239 //     Table qtab(newtab);
00240 //
00241 //     // Update the Table Descriptor
00242 //     tqdA.write(qtab);
00243 //     tqdS.write(qtab);
00244 // </srcblock>
00245 // </example>
00246 
00247 // <motivation>
00248 // This class assists in the definition of a Quantum Table Column.
00249 // </motivation>
00250 
00251 // <thrown>
00252 //    <li>AipsError during construction if the column doesn't exist.
00253 //    <li>AipsError during construction if the unit's column doesn't
00254 //        exist (when variable units).
00255 //    <li>AipsError during construction if the type of the variable unit's
00256 //        column is not String.
00257 //    <li>AipsError during a reconstruct if the column doesn't have a Unit.
00258 // </thrown>
00259 
00260 //# <todo asof="$DATE:$">
00261 //# A List of bugs, limitations, extensions or planned refinements.
00262 //# </todo>
00263 
00264 class TableQuantumDesc
00265 {
00266 public:
00267   // Constructs a Quantum column descriptor with null units (Unit == "").
00268   // The column should have already been added to the TableDesc.
00269   // An exception is thrown if the column doesn't exist.
00270   TableQuantumDesc (const TableDesc& td, const String& column);
00271 
00272   // Constructs a Quantum column descriptor with the specified Quantum unit.
00273   // The column should have already been added to the TableDesc.
00274   // An exception is thrown if the column doesn't exist.
00275   TableQuantumDesc (const TableDesc& td, const String& column, const Unit&);
00276 
00277   // Constructs a Quantum column descriptor with the specified Quantum units.
00278   // The column should have already been added to the TableDesc.
00279   // An exception is thrown if the column doesn't exist.
00280   // <group>
00281   TableQuantumDesc (const TableDesc& td, const String& column,
00282                     const Vector<String>& unitNames);
00283   TableQuantumDesc (const TableDesc& td, const String& column,
00284                     const Vector<Unit>&);
00285   // </group>
00286 
00287   // Constructs a Quantum column descriptor with variable units stored in
00288   // unitCol.  Both the quantum and unit column should exist in the
00289   // TableDesc.
00290   //# Note that the Char* constructor is needed, otherwise the compiler
00291   //# cannot choose between String and Unit.
00292   //<group>
00293   TableQuantumDesc (const TableDesc& td, const String& column,
00294                     const String& unitCol);
00295   TableQuantumDesc (const TableDesc& td, const String& column,
00296                     const Char* unitCol);
00297   //</group>
00298 
00299   // Copy constructor (copy semantics).
00300   TableQuantumDesc (const TableQuantumDesc& that);
00301 
00302   ~TableQuantumDesc();
00303 
00304   // Reconstructs a previously constructed TableQuantumDesc.
00305   static TableQuantumDesc* reconstruct (const TableDesc& td,
00306                                         const String& column);
00307 
00308   // Assignment.
00309   TableQuantumDesc& operator= (const TableQuantumDesc& that);
00310 
00311   // Returns the Quantum column descriptor's units.  A empty vector is
00312   // returned if units have not been specified.  This could be because the null
00313   // unit constructor was used or because the units are variable.
00314   const Vector<String>& getUnits() const
00315     { return itsUnitsName; }
00316 
00317   // Returns True if descriptor set for variable units (one per row)
00318   Bool isUnitVariable() const
00319     { return (! itsUnitsColName.empty()); }
00320 
00321   // Returns the name of the quantum column.
00322   const String& columnName() const
00323     { return itsColName; }
00324 
00325   // Returns the name of the units column (an empty String is returned
00326   // if the units are not variable).
00327   const String& unitColumnName() const
00328     { return itsUnitsColName; }
00329 
00330   // Makes the TableQuantumDesc persistent (updates the Table Descriptor).
00331   // <group>
00332   void write (TableDesc&);
00333   void write (Table&);
00334   // </group>
00335 
00336   // Does this column contain table quanta?
00337   static Bool hasQuanta (const TableColumn& column);
00338 
00339 private:
00340   // Name of column which stores the Quantum's values.
00341   String itsColName;
00342   // The Quantum's unit as a string.
00343   Vector<String> itsUnitsName;
00344   // Name of units column if units are variable.
00345   String itsUnitsColName;
00346 
00347 
00348   // Write the actual keywords.
00349   void writeKeys (TableRecord& columnKeyset);
00350 
00351   // Throw an exception if the quantum column doesn't exist.
00352   void checkColumn (const TableDesc& td) const;
00353 
00354   // Throw an exception if the variable units column isn't a string column.
00355   void checkUnitsColumn (const TableDesc& td) const;
00356 };
00357 
00358 
00359 
00360 } //# NAMESPACE CASA - END
00361 
00362 #endif