casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HDF5Lattice.h
Go to the documentation of this file.
00001 //# HDF5Lattice.h: Templated paged array in an HDF5 file
00002 //# Copyright (C) 2008
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: HDF5Lattice.h 20652 2009-07-06 05:04:32Z Malte.Marquarding $
00027 
00028 #ifndef LATTICES_HDF5LATTICE_H
00029 #define LATTICES_HDF5LATTICE_H
00030 
00031 //# Includes
00032 #include <lattices/Lattices/Lattice.h>
00033 #include <lattices/Lattices/TiledShape.h>
00034 #include <casa/HDF5/HDF5File.h>
00035 #include <casa/HDF5/HDF5Group.h>
00036 #include <casa/HDF5/HDF5DataSet.h>
00037 #include <casa/BasicSL/String.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041   // <summary>
00042   // A Lattice that is read from or written to an HDF5 dataset.
00043   // </summary>
00044 
00045   // <use visibility=export>
00046 
00047   // <reviewed reviewer="" date="" tests="tHDF5Lattice.cc">
00048   // </reviewed>
00049 
00050   // <prerequisite>
00051   //   <li> <linkto class="PagedArray">PagedArray</linkto>
00052   //   <li> <linkto class="TiledShape">TiledShape</linkto>
00053   //   <li> <linkto class="HDF5File">HDF5File</linkto>
00054   // </prerequisite>
00055 
00056   // <synopsis> 
00057   // Astronomical data arrays (like images) have to be persistent.
00058   // A Lattice is a templated abstract base class to hold any AIPS++ array.
00059   // The PagedArray class is a Lattice specialization which stores the data
00060   // in an AIPS++ table.
00061   // <br>
00062   // HDF5Lattice ia another Lattice specialization making it possible to store
00063   // an array as a dataset in a group in an HDF5 file.
00064   // <p>
00065   // When you construct an HDF5Lattice you do not read any data into
00066   // memory. Instead an HDF5 disk file is created, in a place you
00067   // specify, to hold the data. This means you need to have enough disk space
00068   // to hold the array. Constructing a new HDF5Lattice is equivalent to
00069   // creating a data set in an HDF5 file. 
00070   // <p>
00071   // To access the data in a HDF5Lattice you can (in order of preference):
00072   // <ol>
00073   // <li> Use a <linkto class=LatticeIterator>LatticeIterator</linkto>
00074   // <li> Use the getSlice and putSlice member functions
00075   // <li> Use the parenthesis operator or getAt and putAt functions
00076   // </ol>
00077   // Class PagedArray contains some more info and examples.
00078   // </synopsis> 
00079 
00080   // <example>
00081   // Create a HDF5Lattice of Floats of shape [1024,1024,4,256] in a file
00082   // called "myData_tmp.array" and initialize it to zero.
00083   // <srcblock>
00084   // const IPosition arrayShape(4,1024,1024,4,256);
00085   // const String filename("myData_tmp.array");
00086   // HDF5Lattice<Float> diskArray(arrayShape, filename);
00087   // cout << "Created a HDF5Lattice of shape " << diskArray.shape() 
00088   //   << " (" << diskArray.shape().product()/1024/1024*sizeof(Float) 
00089   //   << " MBytes)" << endl
00090   //   << "in the table called " << diskArray.tableName() << endl;
00091   // diskArray.set(0.0f);
00092   // // Using the set function is an efficient way to initialize the HDF5Lattice
00093   // // as it uses a LatticeIterator internally. Note that the set function is
00094   // // defined in the Lattice class that HDF5Lattice is derived from. 
00095   // </srcblock>
00096   // </example>
00097 
00098   // <motivation>
00099   // There was a need to be able to use HDF5 files to hold image data.
00100   // </motivation>
00101 
00102   // <templating arg=T>
00103   // <li> HDF5DataSet supports only a limited amount of types.
00104   // This restricts the template argument to
00105   // the types Bool, Int Float, Double, Complex, and DComplex.
00106   // </templating>
00107 
00108   template<typename T> class HDF5Lattice : public Lattice<T>
00109   {
00110     //# Make members of parent class known.
00111   public:
00112     using Lattice<T>::ndim;
00113 
00114   public: 
00115     // The default constructor creates an HDF5Lattice that is useless for just
00116     // about everything, except that it can be assigned to with the assignment
00117     // operator.
00118     HDF5Lattice();
00119 
00120     // Construct a new HDF5Lattice with the specified shape.
00121     // A new HDF5 file with the specified filename is constructed to hold
00122     // the array. The file will remain on disk after the HDF5Lattice goes
00123     // out of scope or is deleted.
00124     // Optionally the name of an HDF5 group can be given to create the array in.
00125     // The group is created if not existing yet.
00126     HDF5Lattice (const TiledShape& shape, const String& filename,
00127                  const String& arrayName = "array",
00128                  const String& groupName = String());
00129 
00130     // Construct a temporary HDF5Lattice with the specified shape.
00131     // A scratch file is created in the current working directory to hold
00132     // the array. This file will be deleted automatically when the HDF5Lattice
00133     // goes out of scope or is deleted.
00134     explicit HDF5Lattice (const TiledShape& shape);
00135 
00136     // Construct a new HDF5Lattice, with the specified shape, in the given
00137     // HDF5 file. The array gets the given name.
00138     // Optionally the name of an HDF5 group can be given to create the array in.
00139     // The group is created if not existing yet.
00140     HDF5Lattice (const TiledShape& shape, const CountedPtr<HDF5File>& file,
00141                  const String& arrayName, const String& groupName = String());
00142 
00143     // Reconstruct from a pre-existing HDF5Lattice in the HDF5 file and group
00144     // with the given names.
00145     explicit HDF5Lattice (const String& fileName,
00146                           const String& arrayName = "array",
00147                           const String& groupName = String());
00148 
00149     // Reconstruct from a pre-existing HDF5Lattice in the HDF5 file and group
00150     // with the given name.
00151     explicit HDF5Lattice (const CountedPtr<HDF5File>& file,
00152                           const String& arrayName,
00153                           const String& groupName = String());
00154 
00155     // The copy constructor which uses reference semantics. Copying by value
00156     // doesn't make sense, because it would require the creation of a
00157     // temporary (but possibly huge) file on disk.
00158     HDF5Lattice (const HDF5Lattice<T>& other);
00159   
00160     // The destructor flushes the HDF5Lattice's contents to disk. 
00161     ~HDF5Lattice();
00162   
00163     // The assignment operator with reference semantics. As with the copy
00164     // constructor assigning by value does not make sense.
00165     HDF5Lattice<T>& operator= (const HDF5Lattice<T>& other);
00166   
00167     // Make a copy of the object (reference semantics).
00168     virtual Lattice<T>* clone() const;
00169 
00170     // A HDF5Lattice is always persistent.
00171     virtual Bool isPersistent() const;
00172 
00173     // A HDF5Lattice is always paged to disk.
00174     virtual Bool isPaged() const;
00175 
00176     // Is the HDF5Lattice writable?
00177     virtual Bool isWritable() const;
00178 
00179     // Returns the shape of the HDF5Lattice.
00180     virtual IPosition shape() const;
00181 
00182     // Return the current HDF5 file name.
00183     // By default this includes the full path. 
00184     // The path preceeding the file name can be stripped off on request.
00185     virtual String name (Bool stripPath=False) const;
00186 
00187     // Return the current HDF5File object.
00188     const CountedPtr<HDF5File>& file() const
00189       { return itsFile; }
00190 
00191     // Returns the name of this HDF5Lattice.
00192     const String& arrayName() const
00193       { return itsDataSet->getName(); }
00194 
00195     // Returns the current tile shape for this HDF5Lattice.
00196     IPosition tileShape() const;
00197 
00198     // Set the actual cache size for this Array to be big enough for the
00199     // indicated number of tiles. This cache is not shared with other
00200     // HDF5Lattices,
00201     // Tiles are cached using an LRU algorithm.
00202     virtual void setCacheSizeInTiles (uInt howManyTiles);
00203 
00204     // Set the cache size as to "fit" the indicated access pattern.
00205     virtual void setCacheSizeFromPath (const IPosition& sliceShape,
00206                                        const IPosition& windowStart,
00207                                        const IPosition& windowLength,
00208                                        const IPosition& axisPath);
00209 
00210     // Return the value of the single element located at the argument
00211     // IPosition.
00212     // Note that <src>Lattice::operator()</src> can also be used.
00213     virtual T getAt (const IPosition& where) const;
00214   
00215     // Put the value of a single element.
00216     virtual void putAt (const T& value, const IPosition& where);
00217 
00218     // A function which checks for internal consistency. Returns False if
00219     // something nasty has happened to the HDF5Lattice. In that case
00220     // it also throws an exception.
00221     virtual Bool ok() const;
00222 
00223     // This function is used by the LatticeIterator class to generate an
00224     // iterator of the correct type for a specified Lattice. Not recommended
00225     // for general use. 
00226     virtual LatticeIterInterface<T>* makeIter (const LatticeNavigator& navigator,
00227                                                Bool useRef) const;
00228 
00229     // Do the actual getting of an array of values.
00230     virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section);
00231 
00232     // Do the actual getting of an array of values.
00233     virtual void doPutSlice (const Array<T>& sourceBuffer,
00234                              const IPosition& where,
00235                              const IPosition& stride);
00236 
00237     // Returns the maximum recommended number of pixels for a cursor. This is
00238     // the number of pixels in a tile.
00239     virtual uInt advisedMaxPixels() const;
00240 
00241     // Get the best cursor shape.
00242     virtual IPosition doNiceCursorShape (uInt maxPixels) const;
00243 
00244     // Flush the data (but do not unlock).
00245     virtual void flush();
00246 
00247   private:
00248     // Make the Array in the HDF5 file and group.
00249     void makeArray (const TiledShape& shape, const String& arrayName,
00250                     const String& groupName);
00251     // Open the Array in the HDF5 file and group.
00252     void openArray (const String& arrayName, const String& groupName);
00253     // Check if the file is writable.
00254     void checkWritable() const;
00255 
00256 
00257     CountedPtr<HDF5File>    itsFile;
00258     CountedPtr<HDF5Group>   itsGroup;
00259     CountedPtr<HDF5DataSet> itsDataSet;
00260   };
00261 
00262 
00263 } //# NAMESPACE CASA - END
00264 
00265 #ifndef CASACORE_NO_AUTO_TEMPLATES
00266 #include <lattices/Lattices/HDF5Lattice.tcc>
00267 #endif //# CASACORE_NO_AUTO_TEMPLATES
00268 
00269 #endif