casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RowCopier.h
Go to the documentation of this file.
00001 //# RowCopier.h: RowCopier copies part or all of a row from one table to another.
00002 //# Copyright (C) 1995,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: RowCopier.h 21051 2011-04-20 11:46:29Z gervandiepen $
00027 
00028 
00029 #ifndef TABLES_ROWCOPIER_H
00030 #define TABLES_ROWCOPIER_H
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 class Table;
00039 template<class T> class Vector;
00040 class String;
00041 class ColumnHolder; //# Only in the .cc file
00042 
00043 
00044 // <summary> 
00045 // RowCopier copies all or part of a row from one table to another.
00046 // </summary>
00047 
00048 // <use visibility=export>
00049 
00050 // <reviewed reviewer="Mark Wieringa" date="21Nov94" tests="tRowCopier">
00051 // </reviewed>
00052 
00053 // <prerequisite>
00054 //  <li> Table
00055 // </prerequisite>
00056 
00057 // <etymology>
00058 // RowCopier is a class that copies rows from one table to another, hence
00059 // the name.
00060 // </etymology>
00061 
00062 // <synopsis>
00063 // The primary organization in a Table is the TableColumn.  However, data
00064 // is often organized primarily by rows, at least initially (e.g. for an
00065 // on-line system, the data will arrive in chunks that are likely to be
00066 // individual rows rather than individual columns).  RowCopier is used 
00067 // to copy values in a row from all or some of the columns of one table to
00068 // another table.
00069 //
00070 // Some things to keep in mind:
00071 // <ol>
00072 // <li> For each column to be copied, the data types and dimensionality 
00073 // must match.
00074 // <li> The input row number need not be the same as the output row number.
00075 // <li> The output row number must already exist (i.e. no new rows are created).
00076 // <li> The output column name need not be the same as the input column name.
00077 // <li> The output column name and input column name, when specified, must 
00078 // already exist.
00079 // <li> The output table and each output column must be writable.
00080 // </ol>
00081 // </synopsis>
00082 
00083 // <example>
00084 // In the FITS Binary Table extension to Table conversion class, BinTable,
00085 // the input FITS file is a stream that must be read sequentially, so the
00086 // input arrives row-by-row.  Internally, there is a single row table that
00087 // is used to hold the values for the current row.  To fill an aips++ table
00088 // with the data from each row, one creates the output table using the
00089 // table descriptor from the input, single-row table and uses RowCopier to
00090 // copy the single-row table to the appropriate row of the full table, 
00091 // refilling the single-row table at each step. This is how that looks
00092 // (leaving out some details not important to this example):
00093 // 
00094 // Background:
00095 //   singleRowTab is a table constisting of a single row.  It is filled
00096 //   from the input FITS classes using the fillRow() member function.
00097 //   The nrows() member function returns the total number of FITS binary
00098 //   table rows and currrow() returns the current row number.
00099 //
00100 // <srcblock>
00101 // //   Create an empty Table able to hold all remaining FITS rows, including
00102 // //   the current one and having the same descriptor as singleRowTab
00103 //    SetupNewTable newTab("FullTable", singleRowTab.getDescriptor(), 
00104 //                         Table:New);
00105 //    Table full(newTab, (nrows() - currrow() + 1));
00106 // //   create the copier to copy all columns
00107 //    RowCopier copier(full, singleRowTab);
00108 //    // loop over all remaining rows
00109 //    // since full was just created, we start filling it at row 0.
00110 //    for (uInt outRow = 0, fitsRow = currrow(); fitsRow < nrows();
00111 //         outRow++, fitsRow++) {
00112 //        // copy the only row from currRowTab (row 0) to the outRow of full
00113 //        copier.copy(outRow, 0);
00114 //        // fill the next row of currRowTab
00115 //        fillRow();
00116 //    }
00117 // </srcblock>
00118 //
00119 // This example shows how to copy some of the values from one table to
00120 // another.  This is a contrived example.  The input table 
00121 // has columns named "HSource" and "VSource" along with other columns.
00122 // This example places the values from these columns to columns named
00123 // "RA (1950)" and "DEC (1950)" in the output table (which also has other
00124 // columns).  Note that each input column must have the same type and
00125 // dimensionality as the corresponding output column.  
00126 //  
00127 // <srcblock>
00128 //  // construct a vector of the input column names to copy and the
00129 //  // associated output column names
00130 //  Vector<String> inColNames(2), outColNames(2);
00131 //  inColNames(0) = "HSource"; outColNames(0) = "RA (1950)"
00132 //  inColNames(1) = "VSource"; outColNames(1) = "DEC (1950)"
00133 //
00134 //  // construct the copier
00135 //  RowCopier copier(inTable, outTable, inColNames, outColNames);
00136 //
00137 //  // Copy a row from in to out, obviously a typical use would do
00138 //  // more than just one row.
00139 //  copier.copy(outRownr, outRownr-1);
00140 // </srcblock>
00141 // </example>
00142 
00143 // <motivation>
00144 // See the comments in the synopsis.
00145 // </motivation>
00146 
00147 // <todo asof=$DATE:$">
00148 //  <li> resize should probably happen in powers of 2
00149 //  <li> is throwing exceptions really what we want to happen?
00150 // </todo>
00151 
00152 
00153 class RowCopier {
00154 public:
00155     // This constructs a copier which will copy all columns which have the 
00156     // same name in both tables from in to out.
00157     // An exception is thrown if the columns having the same name in both
00158     // tables are not conformant (not the same type and not both scalar of both
00159     // array columns)
00160     // <thrown>
00161     //  <li> TableError
00162     // </thrown>
00163     RowCopier (Table &out, const Table &in);
00164 
00165     // This constructs a copier which will copy innames columns to outnames 
00166     // columns, outnames and innames must be conformant.  Columns are 
00167     // matched up element-by-element in innames and outnames.
00168     // An exception is thrown if an element of innames or outnames is not
00169     // present in the corresponding table, if innames and outnames are
00170     // not conformant and if the corresponding columns are not conformant
00171     // (not the same type and not both scalar or both array columns)
00172     // <thrown>
00173     //  <li> TableError
00174     // </thrown>
00175     RowCopier (Table &out, const Table &in, const Vector<String>& outNames,
00176                const Vector<String>& inNames);
00177    
00178     // The things that actually do the copying when requested.
00179     // <group>
00180     // Copy different row numbers.
00181     Bool copy (uInt toRow, uInt fromRow);
00182     // Copy to and from the same row number
00183     Bool copy (uInt rownr);
00184     // </group>
00185 
00186     ~RowCopier();
00187 
00188 private:
00189     //# The following constructors and operator don't seem to be useful
00190     // <group>
00191     RowCopier();
00192     RowCopier(const RowCopier &other);
00193     RowCopier &operator=(const RowCopier &other);
00194     // </group>
00195 
00196     // The ColumnHolder class exists only in the .cc file, it is what
00197     // ultimately does the work.
00198     ColumnHolder *columns_p;
00199 };
00200 
00201 
00202 inline Bool RowCopier::copy (uInt rownr)
00203     { return copy (rownr, rownr); }
00204 
00205 
00206 
00207 } //# NAMESPACE CASA - END
00208 
00209 #endif