casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TiledShapeStMan.h
Go to the documentation of this file.
00001 //# TiledShapeStMan.h: Tiled Data Storage Manager using the shape as id
00002 //# Copyright (C) 1998,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 //# $Id: TiledShapeStMan.h 21012 2010-12-21 07:22:02Z gervandiepen $
00027 
00028 #ifndef TABLES_TILEDSHAPESTMAN_H
00029 #define TABLES_TILEDSHAPESTMAN_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <tables/Tables/TiledStMan.h>
00035 #include <casa/Containers/Block.h>
00036 #include <casa/BasicSL/String.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward Declarations
00041 
00042 
00043 // <summary>
00044 // Tiled Data Storage Manager using the shape as id.
00045 // </summary>
00046 
00047 // <use visibility=export>
00048 
00049 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00050 // </reviewed>
00051 
00052 // <prerequisite>
00053 //# Classes you should understand before using this one.
00054 //   <li> <linkto class=TiledStMan>TiledStMan</linkto>
00055 //   <li> <linkto class=TSMCube>TSMCube</linkto>
00056 //   <li> <linkto class=ROTiledStManAccessor>ROTiledStManAccessor</linkto>
00057 //        for a discussion of the maximum cache size
00058 // </prerequisite>
00059 
00060 // <etymology>
00061 // TiledShapeStMan is the Tiled Storage Manager where the shape is used as id
00062 // to support variable shaped arrays.
00063 // </etymology>
00064 
00065 // <synopsis>
00066 // TiledShapeStMan is a derivation from TiledStMan, the abstract
00067 // tiled storage manager class. A description of the basics
00068 // of tiled storage managers is given in the
00069 // <linkto module=Tables:TiledStMan>Tables module</linkto> description.
00070 // <p>
00071 // TiledShapeStMan creates a hypercube for each different shape of
00072 // the data arrays. For example, if a table contains line and continuum
00073 // data of an observation, it results in 2 hypercubes.
00074 // TiledShapeStMan does it all automatically, so it is much easier to use
00075 // than class <linkto class=TiledDataStMan>TiledDataStMan</linkto>.
00076 // <br>TiledShapeStMan is meant for columns with not too many different
00077 // shapes, otherwise looking for a matching hypercube may take too long.
00078 // When many different shapes are used, class
00079 // <linkto class=TiledCellStMan>TiledCellStMan</linkto>
00080 // should be used instead.
00081 //
00082 // TiledShapeStMan has the following (extra) properties:
00083 // <ul>
00084 //  <li> It can only handle columns containing arrays, thus not scalars.
00085 //  <li> Addition of a row sets the appropriate data arrays
00086 //       in that row temporarily to an empty hypercube.
00087 //       However, if the data arrays have a fixed shape, the
00088 //       shape is known and the hypercube can be generated immediately.
00089 //       Note that for a fixed shape column, one can as well use class
00090 //       <linkto class=TiledColumnStMan>TiledColumnStMan</linkto>.
00091 //  <li> When the shape of the data array in a row is set for the
00092 //       first time, it is known which hypercube should be used or
00093 //       if a new hypercube has to be created.
00094 //       <br>Note that is is not possible to change the shape of an array.
00095 //       If that is needed, TiledCellStMan should be used instead.
00096 //       <br>Note that a hypercolumn has a given dimensionality, so each
00097 //       data cell in the hypercolumn has to match that dimensionality.
00098 //  <li> Although there are multiple hypercubes, an id value is not needed.
00099 //       The shape serves as the id value.
00100 //  <li> Coordinates for the hypercubes can be defined and (of course)
00101 //       their shapes have to match the hypercube shape.
00102 //       Their values have to be put explicitly (so it is not possible
00103 //       to define them via an addHypercube call like in
00104 //       <linkto class=TiledDataStMan>TiledDataStMan</linkto>).
00105 //       It is possible to put the coordinate values before or after
00106 //       the shape of the data array in that row is defined.
00107 //  <li> It is possible to define a (default) tile shape in the
00108 //       TiledShapeStMan constructor. When setting the shape of the
00109 //       array in a row (using <linkto class=ArrayColumn>
00110 //       ArrayColumn::setShape</linkto>), it is possible to override
00111 //       that default for the hypercube in this particular row.
00112 //       However, since the tile shape is only used when creating
00113 //       a hypercube, using an overriding tile shape makes only
00114 //       sense when a given array shape is used for the first time.
00115 //       Note that the dimensionality of the hypercube is one higher
00116 //       than the dimensionality of the data arrays (since the hypercube
00117 //       contains multiple rows). It means that the number of values in
00118 //       tile shape can be one more than the number of axes in the data
00119 //       array. The last tile shape value defaults to 1; the other
00120 //       tile shape values have to be defined.
00121 // </ul>
00122 // </synopsis> 
00123 
00124 // <motivation>
00125 // TiledDataStMan proved to be very powerful, but also a bit cumbersome
00126 // to use because a few special functions need to be called.
00127 // TiledShapeStMan alleviates that problem.
00128 // </motivation>
00129 
00130 // <example>
00131 // <srcblock>
00132 //  // Define the table description and the columns in it.
00133 //  TableDesc td ("", "1", TableDesc::Scratch);
00134 //  td.addColumn (ArrayColumnDesc<float>  ("RA", 1));
00135 //  td.addColumn (ArrayColumnDesc<float>  ("Dec", 1));
00136 //  td.addColumn (ScalarColumnDesc<float> ("Velocity"));
00137 //  td.addColumn (ArrayColumnDesc<float>  ("Image", 2));
00138 //  // Define the 3-dim hypercolumn with its data and coordinate columns.
00139 //  // Note that its dimensionality must be one higher than the dimensionality
00140 //  // of the data cells.
00141 //  td.defineHypercolumn ("TSMExample",
00142 //                        3,
00143 //                        stringToVector ("Image"),
00144 //                        stringToVector ("RA,Dec,Velocity"));
00145 //  // Now create a new table from the description.
00146 //  SetupNewTable newtab("tTiledShapeStMan_tmp.data", td, Table::New);
00147 //  // Create a TiledShapeStMan storage manager for the hypercolumn
00148 //  // and bind the columns to it.
00149 //  // The (default) tile shape has to be specified for the storage manager.
00150 //  TiledShapeStMan sm1 ("TSMExample", IPosition(3,16,32,32));
00151 //  newtab.bindAll (sm1);
00152 //  // Create the table.
00153 //  Table table(newtab);
00154 //  // Define the values for the coordinates of the hypercube.
00155 //  Vector<float> raValues(512);
00156 //  Vector<float> DecValues(512);
00157 //  indgen (raValues);
00158 //  indgen (decValues, float(100));
00159 //  ArrayColumn<float> ra (table, "RA");
00160 //  ArrayColumn<float> dec (table, "Dec");
00161 //  ScalarColumn<float> velocity (table, "Velocity");
00162 //  ArrayColumn<float> image (table, "Image");
00163 //  Cube<float> imageValues(IPosition(2,512,512));
00164 //  indgen (imageValues);
00165 //  // Write some data into the data columns.
00166 //  uInt i;
00167 //  for (i=0; i<64; i++) {
00168 //      table.addRow();
00169 //      image.put (i, imageValues);
00170 //      ra.put (i, raValues);
00171 //      dec.put (i, decValues);
00172 //      velocity.put (i, float(i));
00173 //  }
00174 // </srcblock>
00175 // Note that in this example the same shape is used for each row,
00176 // but it could have been different.
00177 // </example>
00178 
00179 //# <todo asof="$DATE:$">
00180 //# A List of bugs, limitations, extensions or planned refinements.
00181 //# </todo>
00182 
00183 
00184 class TiledShapeStMan : public TiledStMan
00185 {
00186 public:
00187     // Create a TiledShapeStMan storage manager for the hypercolumn
00188     // with the given name.
00189     // The hypercolumn name is also the name of the storage manager.
00190     // The given maximum cache size (default is unlimited) is persistent,
00191     // thus will be reused when the table is read back. Note that the class
00192     // <linkto class=ROTiledStManAccessor>ROTiledStManAccessor</linkto>
00193     // allows one to overwrite the maximum cache size temporarily.
00194     // <br>The constructor taking a Record expects fields in the record with
00195     // the name of the arguments in uppercase. If not defined, their
00196     // default value is used.
00197     // <group>
00198     TiledShapeStMan (const String& hypercolumnName,
00199                      const IPosition& defaultTileShape,
00200                      uInt maximumCacheSize = 0);
00201     TiledShapeStMan (const String& hypercolumnName,
00202                      const Record& spec);
00203     // </group>
00204 
00205     ~TiledShapeStMan();
00206 
00207     // Clone this object.
00208     // It does not clone TSMColumn objects possibly used.
00209     virtual DataManager* clone() const;
00210 
00211     // Get the type name of the data manager (i.e. TiledShapeStMan).
00212     virtual String dataManagerType() const;
00213 
00214     // Return a record containing data manager specifications and info.
00215     virtual Record dataManagerSpec() const;
00216 
00217     // TiledShapeStMan can access a column if there are 2 hypercubes
00218     // and the first one is empty.
00219     // reask is set to True (because next time things might be different).
00220     virtual Bool canAccessColumn (Bool& reask) const;
00221 
00222     // Test if only one hypercube is used by this storage manager.
00223     // If not, throw an exception. Otherwise return the hypercube.
00224     virtual TSMCube* singleHypercube();
00225 
00226     // Set the shape and tile shape of the given hypercube.
00227     // It is used when the first row in a new hypercube is written.
00228     // If needed it adds a dimension to the shape, which reflects the
00229     // row dimension. The tile shape in that dimension is by default 1.
00230     virtual void setShape (uInt rownr, TSMCube* hypercube,
00231                            const IPosition& shape,
00232                            const IPosition& tileShape);
00233 
00234     // Make the object from the type name string.
00235     // This function gets registered in the DataManager "constructor" map.
00236     static DataManager* makeObject (const String& dataManagerType,
00237                                     const Record& spec);
00238 
00239 private:
00240     // Create a TiledShapeStMan.
00241     // This constructor is private, because it should only be used
00242     // by makeObject.
00243     TiledShapeStMan();
00244 
00245     // Forbid copy constructor.
00246     TiledShapeStMan (const TiledShapeStMan&);
00247 
00248     // Forbid assignment.
00249     TiledShapeStMan& operator= (const TiledShapeStMan&);
00250 
00251     // Get the default tile shape.
00252     virtual IPosition defaultTileShape() const;
00253 
00254     // Add rows to the storage manager.
00255     void addRow (uInt nrrow);
00256 
00257     // Find the hypercube for the given shape.
00258     // It returns -1 when not found.
00259     Int findHypercube (const IPosition& shape);
00260 
00261     // Add a hypercube.
00262     // The number of rows in the table must be large enough to
00263     // accommodate this hypercube.
00264     // The possible id values must be given in the record, while
00265     // coordinate values are optional. The field names in the record
00266     // should match the coordinate and id column names.
00267     // The last dimension in the cube shape can be zero, indicating that
00268     // the hypercube is extensible.
00269     void addHypercube (uInt rownr,
00270                        const IPosition& cubeShape,
00271                        const IPosition& tileShape);
00272 
00273     // Extend the hypercube with the given number of elements in
00274     // the last dimension.
00275     // The record should contain the id values (to get the correct
00276     // hypercube) and optionally coordinate values for the elements added.
00277     void extendHypercube (uInt rownr, uInt cubeNr);
00278 
00279     // Get the hypercube in which the given row is stored.
00280     virtual TSMCube* getHypercube (uInt rownr);
00281 
00282     // Get the hypercube in which the given row is stored.
00283     // It also returns the position of the row in that hypercube.
00284     virtual TSMCube* getHypercube (uInt rownr, IPosition& position);
00285 
00286     // Check if the hypercolumn definition fits this storage manager.
00287     virtual void setupCheck (const TableDesc& tableDesc,
00288                              const Vector<String>& dataNames) const;
00289 
00290     // Flush and optionally fsync the data.
00291     // It returns a True status if it had to flush (i.e. if data have changed).
00292     virtual Bool flush (AipsIO&, Bool fsync);
00293 
00294     // Let the storage manager create files as needed for a new table.
00295     // This allows a column with an indirect array to create its file.
00296     virtual void create (uInt nrrow);
00297 
00298     // Read the header info.
00299     virtual void readHeader (uInt nrrow, Bool firstTime);
00300 
00301     // Update the map of row numbers to cube number plus offset.
00302     void updateRowMap (uInt cubeNr, uInt pos, uInt rownr);
00303 
00304     // Extend the map of row numbers to cube number plus offset
00305     // will new empty entries.
00306     void extendRowMap (uInt nrow);
00307 
00308 
00309     //# Declare the data members.
00310     // The default tile shape.
00311     IPosition defaultTileShape_p;
00312     // The map of row number to cube and position in cube.
00313     Block<uInt> rowMap_p;
00314     Block<uInt> cubeMap_p;
00315     Block<uInt> posMap_p;
00316     // The nr of elements used in the map blocks.
00317     uInt nrUsedRowMap_p;
00318     // The last hypercube found.
00319     Int lastHC_p;
00320 };
00321 
00322 
00323 
00324 
00325 } //# NAMESPACE CASA - END
00326 
00327 #endif