casacore
Loading...
Searching...
No Matches
HDF5Lattice.h
Go to the documentation of this file.
1//# HDF5Lattice.h: Templated paged array in an HDF5 file
2//# Copyright (C) 2008
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef LATTICES_HDF5LATTICE_H
27#define LATTICES_HDF5LATTICE_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/lattices/Lattices/Lattice.h>
32#include <casacore/lattices/Lattices/TiledShape.h>
33#include <casacore/casa/HDF5/HDF5File.h>
34#include <casacore/casa/HDF5/HDF5Group.h>
35#include <casacore/casa/HDF5/HDF5DataSet.h>
36#include <casacore/casa/BasicSL/String.h>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40 // <summary>
41 // A Lattice that is read from or written to an HDF5 dataset.
42 // </summary>
43
44 // <use visibility=export>
45
46 // <reviewed reviewer="" date="" tests="tHDF5Lattice.cc">
47 // </reviewed>
48
49 // <prerequisite>
50 // <li> <linkto class="PagedArray">PagedArray</linkto>
51 // <li> <linkto class="TiledShape">TiledShape</linkto>
52 // <li> <linkto class="HDF5File">HDF5File</linkto>
53 // </prerequisite>
54
55 // <synopsis>
56 // Astronomical data arrays (like images) have to be persistent.
57 // A Lattice is a templated abstract base class to hold any Casacore array.
58 // The PagedArray class is a Lattice specialization which stores the data
59 // in a Casacore table.
60 // <br>
61 // HDF5Lattice ia another Lattice specialization making it possible to store
62 // an array as a dataset in a group in an HDF5 file.
63 // <p>
64 // When you construct an HDF5Lattice you do not read any data into
65 // memory. Instead an HDF5 disk file is created, in a place you
66 // specify, to hold the data. This means you need to have enough disk space
67 // to hold the array. Constructing a new HDF5Lattice is equivalent to
68 // creating a data set in an HDF5 file.
69 // <p>
70 // To access the data in a HDF5Lattice you can (in order of preference):
71 // <ol>
72 // <li> Use a <linkto class=LatticeIterator>LatticeIterator</linkto>
73 // <li> Use the getSlice and putSlice member functions
74 // <li> Use the parenthesis operator or getAt and putAt functions
75 // </ol>
76 // Class PagedArray contains some more info and examples.
77 // </synopsis>
78
79 // <example>
80 // Create a HDF5Lattice of Floats of shape [1024,1024,4,256] in a file
81 // called "myData_tmp.array" and initialize it to zero.
82 // <srcblock>
83 // const IPosition arrayShape(4,1024,1024,4,256);
84 // const String filename("myData_tmp.array");
85 // HDF5Lattice<Float> diskArray(arrayShape, filename);
86 // cout << "Created a HDF5Lattice of shape " << diskArray.shape()
87 // << " (" << diskArray.shape().product()/1024/1024*sizeof(Float)
88 // << " MBytes)" << endl
89 // << "in the table called " << diskArray.tableName() << endl;
90 // diskArray.set(0.0f);
91 // // Using the set function is an efficient way to initialize the HDF5Lattice
92 // // as it uses a LatticeIterator internally. Note that the set function is
93 // // defined in the Lattice class that HDF5Lattice is derived from.
94 // </srcblock>
95 // </example>
96
97 // <motivation>
98 // There was a need to be able to use HDF5 files to hold image data.
99 // </motivation>
100
101 // <templating arg=T>
102 // <li> HDF5DataSet supports only a limited amount of types.
103 // This restricts the template argument to
104 // the types Bool, Int Float, Double, Complex, and DComplex.
105 // </templating>
106
107 template<typename T> class HDF5Lattice : public Lattice<T>
108 {
109 //# Make members of parent class known.
110 public:
111 using Lattice<T>::ndim;
112
113 public:
114 // The default constructor creates an HDF5Lattice that is useless for just
115 // about everything, except that it can be assigned to with the assignment
116 // operator.
118
119 // Construct a new HDF5Lattice with the specified shape.
120 // A new HDF5 file with the specified filename is constructed to hold
121 // the array. The file will remain on disk after the HDF5Lattice goes
122 // out of scope or is deleted.
123 // Optionally the name of an HDF5 group can be given to create the array in.
124 // The group is created if not existing yet.
125 HDF5Lattice (const TiledShape& shape, const String& filename,
126 const String& arrayName = "array",
127 const String& groupName = String());
128
129 // Construct a temporary HDF5Lattice with the specified shape.
130 // A scratch file is created in the current working directory to hold
131 // the array. This file will be deleted automatically when the HDF5Lattice
132 // goes out of scope or is deleted.
133 explicit HDF5Lattice (const TiledShape& shape);
134
135 // Construct a new HDF5Lattice, with the specified shape, in the given
136 // HDF5 file. The array gets the given name.
137 // Optionally the name of an HDF5 group can be given to create the array in.
138 // The group is created if not existing yet.
139 HDF5Lattice (const TiledShape& shape, const std::shared_ptr<HDF5File>& file,
140 const String& arrayName, const String& groupName = String());
141
142 // Reconstruct from a pre-existing HDF5Lattice in the HDF5 file and group
143 // with the given names.
144 explicit HDF5Lattice (const String& fileName,
145 const String& arrayName = "array",
146 const String& groupName = String());
147
148 // Reconstruct from a pre-existing HDF5Lattice in the HDF5 file and group
149 // with the given name.
150 explicit HDF5Lattice (const std::shared_ptr<HDF5File>& file,
151 const String& arrayName,
152 const String& groupName = String());
153
154 // The copy constructor which uses reference semantics. Copying by value
155 // doesn't make sense, because it would require the creation of a
156 // temporary (but possibly huge) file on disk.
158
159 // The destructor flushes the HDF5Lattice's contents to disk.
161
162 // The assignment operator with reference semantics. As with the copy
163 // constructor assigning by value does not make sense.
165
166 // Make a copy of the object (reference semantics).
167 virtual Lattice<T>* clone() const;
168
169 // A HDF5Lattice is always persistent.
170 virtual Bool isPersistent() const;
171
172 // A HDF5Lattice is always paged to disk.
173 virtual Bool isPaged() const;
174
175 // Is the HDF5Lattice writable?
176 virtual Bool isWritable() const;
177
178 // Returns the shape of the HDF5Lattice.
179 virtual IPosition shape() const;
180
181 // Return the current HDF5 file name.
182 // By default this includes the full path.
183 // The path preceeding the file name can be stripped off on request.
184 virtual String name (Bool stripPath=False) const;
185
186 // Return the current HDF5File object.
187 const std::shared_ptr<HDF5File>& file() const
188 { return itsFile; }
189
190 // Return the current HDF5Group object.
191 const std::shared_ptr<HDF5Group>& group() const
192 { return itsGroup; }
193
194 // Returns the current HDF5DataSet object
195 const std::shared_ptr<HDF5DataSet>& array() const
196 { return itsDataSet; }
197
198 // Returns the name of this HDF5Lattice.
199 const String& arrayName() const
200 { return itsDataSet->getName(); }
201
202 // Returns the current tile shape for this HDF5Lattice.
204
205 // Set the actual cache size for this Array to be big enough for the
206 // indicated number of tiles. This cache is not shared with other
207 // HDF5Lattices,
208 // Tiles are cached using an LRU algorithm.
209 virtual void setCacheSizeInTiles (uInt howManyTiles);
210
211 // Set the cache size as to "fit" the indicated access pattern.
212 virtual void setCacheSizeFromPath (const IPosition& sliceShape,
213 const IPosition& windowStart,
214 const IPosition& windowLength,
215 const IPosition& axisPath);
216
217 // Return the value of the single element located at the argument
218 // IPosition.
219 // Note that <src>Lattice::operator()</src> can also be used.
220 virtual T getAt (const IPosition& where) const;
221
222 // Put the value of a single element.
223 virtual void putAt (const T& value, const IPosition& where);
224
225 // A function which checks for internal consistency. Returns False if
226 // something nasty has happened to the HDF5Lattice. In that case
227 // it also throws an exception.
228 virtual Bool ok() const;
229
230 // This function is used by the LatticeIterator class to generate an
231 // iterator of the correct type for a specified Lattice. Not recommended
232 // for general use.
234 Bool useRef) const;
235
236 // Do the actual getting of an array of values.
237 virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section);
238
239 // Do the actual getting of an array of values.
240 virtual void doPutSlice (const Array<T>& sourceBuffer,
241 const IPosition& where,
242 const IPosition& stride);
243
244 // Returns the maximum recommended number of pixels for a cursor. This is
245 // the number of pixels in a tile.
246 virtual uInt advisedMaxPixels() const;
247
248 // Get the best cursor shape.
249 virtual IPosition doNiceCursorShape (uInt maxPixels) const;
250
251 // Flush the data (but do not unlock).
252 virtual void flush();
253
254 private:
255 // Make the Array in the HDF5 file and group.
257 const String& groupName);
258 // Open the Array in the HDF5 file and group.
259 void openArray (const String& arrayName, const String& groupName);
260 // Check if the file is writable.
261 void checkWritable() const;
262
263
264 std::shared_ptr<HDF5File> itsFile;
265 std::shared_ptr<HDF5Group> itsGroup;
266 std::shared_ptr<HDF5DataSet> itsDataSet;
268 };
269
270
271} //# NAMESPACE CASACORE - END
272
273#ifndef CASACORE_NO_AUTO_TEMPLATES
274#include <casacore/lattices/Lattices/HDF5Lattice.tcc>
275#endif //# CASACORE_NO_AUTO_TEMPLATES
276
277#endif
~HDF5Lattice()
The destructor flushes the HDF5Lattice's contents to disk.
IPosition tileShape() const
Returns the current tile shape for this HDF5Lattice.
virtual IPosition shape() const
Returns the shape of the HDF5Lattice.
const std::shared_ptr< HDF5DataSet > & array() const
Returns the current HDF5DataSet object.
virtual void setCacheSizeFromPath(const IPosition &sliceShape, const IPosition &windowStart, const IPosition &windowLength, const IPosition &axisPath)
Set the cache size as to "fit" the indicated access pattern.
virtual void putAt(const T &value, const IPosition &where)
Put the value of a single element.
virtual Bool ok() const
A function which checks for internal consistency.
const std::shared_ptr< HDF5Group > & group() const
Return the current HDF5Group object.
HDF5Lattice()
The default constructor creates an HDF5Lattice that is useless for just about everything,...
HDF5Lattice(const std::shared_ptr< HDF5File > &file, const String &arrayName, const String &groupName=String())
Reconstruct from a pre-existing HDF5Lattice in the HDF5 file and group with the given name.
HDF5Lattice(const TiledShape &shape, const std::shared_ptr< HDF5File > &file, const String &arrayName, const String &groupName=String())
Construct a new HDF5Lattice, with the specified shape, in the given HDF5 file.
virtual LatticeIterInterface< T > * makeIter(const LatticeNavigator &navigator, Bool useRef) const
This function is used by the LatticeIterator class to generate an iterator of the correct type for a ...
HDF5Lattice(const String &fileName, const String &arrayName="array", const String &groupName=String())
Reconstruct from a pre-existing HDF5Lattice in the HDF5 file and group with the given names.
virtual void flush()
Flush the data (but do not unlock).
virtual T getAt(const IPosition &where) const
Return the value of the single element located at the argument IPosition.
std::shared_ptr< HDF5Group > itsGroup
virtual Bool doGetSlice(Array< T > &buffer, const Slicer &section)
Do the actual getting of an array of values.
virtual void doPutSlice(const Array< T > &sourceBuffer, const IPosition &where, const IPosition &stride)
Do the actual getting of an array of values.
HDF5Lattice< T > & operator=(const HDF5Lattice< T > &other)
The assignment operator with reference semantics.
virtual Bool isPersistent() const
A HDF5Lattice is always persistent.
void checkWritable() const
Check if the file is writable.
virtual IPosition doNiceCursorShape(uInt maxPixels) const
Get the best cursor shape.
std::shared_ptr< HDF5DataSet > itsDataSet
HDF5Lattice(const HDF5Lattice< T > &other)
The copy constructor which uses reference semantics.
const std::shared_ptr< HDF5File > & file() const
Return the current HDF5File object.
HDF5Lattice(const TiledShape &shape, const String &filename, const String &arrayName="array", const String &groupName=String())
Construct a new HDF5Lattice with the specified shape.
void makeArray(const TiledShape &shape, const String &arrayName, const String &groupName)
Make the Array in the HDF5 file and group.
virtual String name(Bool stripPath=False) const
Return the current HDF5 file name.
const String & arrayName() const
Returns the name of this HDF5Lattice.
void openArray(const String &arrayName, const String &groupName)
Open the Array in the HDF5 file and group.
HDF5Lattice(const TiledShape &shape)
Construct a temporary HDF5Lattice with the specified shape.
virtual Bool isPaged() const
A HDF5Lattice is always paged to disk.
virtual Bool isWritable() const
Is the HDF5Lattice writable?
virtual uInt advisedMaxPixels() const
Returns the maximum recommended number of pixels for a cursor.
std::shared_ptr< HDF5File > itsFile
virtual void setCacheSizeInTiles(uInt howManyTiles)
Set the actual cache size for this Array to be big enough for the indicated number of tiles.
virtual Lattice< T > * clone() const
Make a copy of the object (reference semantics).
virtual uInt ndim() const
Return the number of axes in this Lattice.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.