casacore
Loading...
Searching...
No Matches
HDF5Image.h
Go to the documentation of this file.
1// # HDF5Image.h: astronomical image in HDF5 format
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 IMAGES_HDF5IMAGE_H
27#define IMAGES_HDF5IMAGE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/images/Images/ImageInterface.h>
32#include <casacore/images/Images/ImageAttrHandlerHDF5.h>
33#include <casacore/lattices/Lattices/HDF5Lattice.h>
34#include <memory>
35
36// # Forward Declarations
37#include <casacore/casa/iosfwd.h>
38
39namespace casacore { // # NAMESPACE CASACORE - BEGIN
40
41// <summary>
42// Read, store, and manipulate astronomical images in HDF5 format.
43// </summary>
44
45// <use visibility=export>
46
47// <reviewed reviewer="" date="" tests="tHDF5Image.cc" demos="dHDF5Image.cc">
48// </reviewed>
49
50// <prerequisite>
51// <li> <linkto class=CoordinateSystem>CoordinateSystem</linkto>
52// <li> <linkto class=ImageInterface>ImageInterface</linkto>
53// <li> <linkto class=Lattice>Lattice</linkto>
54// <li> <linkto class=LatticeIterator>LatticeIterator</linkto>
55// <li> <linkto class=LatticeNavigator>LatticeNavigator</linkto>
56// <li> <linkto class=ImageRegion>ImageRegion</linkto>
57// </prerequisite>
58
59// <etymology>
60// The HDF5Image name comes from its role as the Image class using HDF5.
61// </etymology>
62
63// <synopsis>
64// All Casacore Images are Lattices. They may be treated like any other Lattice;
65// getSlice(...), putSlice(...), LatticeIterator for iterating, etc...
66// ArrayImages contain a map, a mask for that map, and coordinate
67// information. This provides a Lattice interface for images and their
68// respective coordinates. Additional functionality is defined by the
69// ImageInterface class.
70//
71// You can use the global function <src>imagePixelType</src> to determine
72// what the pixel type of an image is before you open the image if your
73// code can work with Images of many possible types, or for error checking.
74//
75// </synopsis>
76
77// <example>
78// This example shows how to create a mask for an image, fill it, and
79// make it known to the image.
80// <srcblock>
81// // Open the image (as readonly for the moment).
82// HDF5Image<Float> myimage ("image.name");
83// // Create a mask for the image.
84// // The mask will be stored in a subtable of the image.
85// LCPagedMask mask (RegionHandler::makeMask (myimage, "mask.name"));
86// // Fill the mask with whatever values (e.g. all True).
87// mask.set (True);
88// // Make the mask known to the image (with name mask1).
89// myimage.defineRegion ("mask1", mask, RegionHandler::Masks);
90// // Make the mask the default mask for this image.
91// myimage.setDefaultMask ("mask1");
92// </srcblock>
93// It is possible to create as many masks as one likes. They can all
94// be defined as masks for the image (with different names, of course).
95// However, only one of them can be the default mask (the mask used
96// by default when the image is opened). When another mask has to be
97// used, one can do two things:
98// <ul>
99// <li> Use setDefaultMask to make the other mask the default mask.
100// This is advisable when the change should be more or less permanent.
101// <li> Open the HDF5Image without using a default mask. Thereafter
102// a <linkto class=SubImage>SubImage</linkto> object can be created
103// from the HDF5Image and the mask. This is advisable when it the
104// mask has to be used only one time.
105// </ul>
106// </example>
107
108// <motivation>
109// The size of astronomical data can be very large. The ability to fit an
110// entire image into random access memory cannot be guaranteed. Paging from
111// disk pieces of the image appeared to be the way to deal with this problem.
112// </motivation>
113
114// <note>
115// When you make a new HDF5Image, and you are transferring
116// information from some other HDF5Image, be aware that you
117// must copy, manually, things like miscInfo, imageInfo, units,
118// logSink (history) to the new file.
119// </note>
120
121template <class T>
122class HDF5Image : public ImageInterface<T> {
123 public:
124 // Construct a new Image from shape and coordinate information. The image
125 // will be stored in the named file.
126 HDF5Image(const TiledShape& mapShape, const CoordinateSystem& coordinateInfo,
127 const String& nameOfNewFile);
128
129 // Reconstruct an image from a pre-existing file.
130 // By default the default pixelmask (if available) is used.
131 explicit HDF5Image(const String& fileName, MaskSpecifier = MaskSpecifier());
132
133 // Copy constructor (reference semantics).
134 HDF5Image(const HDF5Image<T>& other);
135
137
138 // Assignment operator (reference semantics).
140
141 // Make a copy of the object (reference semantics).
142 virtual ImageInterface<T>* cloneII() const;
143
144 // Get the image type (returns name of derived class).
145 virtual String imageType() const;
146
147 // Return the current HDF5 file name. By default this includes the full path.
148 // The path preceding the file name can be stripped off on request.
149 virtual String name(Bool stripPath = False) const;
150
151 // Function which changes the shape of the ImageExpr.
152 // Throws an exception as an HDF5Image cannot be resized.
153 virtual void resize(const TiledShape& newShape);
154
155 // Check for symmetry in data members.
156 virtual Bool ok() const;
157
158 // Return the shape of the image.
159 virtual IPosition shape() const;
160
161 // Function which extracts an array from the map.
162 virtual Bool doGetSlice(Array<T>& buffer, const Slicer& theSlice);
163
164 // Function to replace the values in the map with soureBuffer.
165 virtual void doPutSlice(const Array<T>& sourceBuffer, const IPosition& where,
166 const IPosition& stride);
167
168 // Get a pointer the default pixelmask object used with this image.
169 // It returns 0 if no default pixelmask is used.
170 virtual const LatticeRegion* getRegionPtr() const;
171
172 // An HDF5Image is always persistent.
173 virtual Bool isPersistent() const;
174
175 // An HDF5Image is always paged to disk.
176 virtual Bool isPaged() const;
177
178 // Is the HDF5Image writable?
179 virtual Bool isWritable() const;
180
181 // Does the image object use a pixelmask?
182 virtual Bool hasPixelMask() const;
183
184 // Get access to the pixelmask used.
185 // An exception is thrown if the image does not use a pixelmask.
186 // <group>
187 virtual const Lattice<Bool>& pixelMask() const;
189 // </group>
190
191 // Set the default pixelmask to the mask with the given name
192 // (which has to exist in the "masks" group).
193 // If the image file is writable, the setting is persistent by writing
194 // the name as a keyword.
195 // If the given mask name is the empty string,
196 // the default pixelmask is unset.
197 virtual void setDefaultMask(const String& maskName);
198
199 // Use the mask as specified.
200 // If a mask was already in use, it is replaced by the new one.
202
203 // Replace every element, x, of the lattice with the result of f(x).
204 // you must pass in the address of the function -- so the function
205 // must be declared and defined in the scope of your program.
206 // Both versions of apply require a function that accepts a single
207 // argument of type T (the Lattice template actual type) and returns
208 // a result of the same type. The first apply expects a function with
209 // an argument passed by value; the second expects the argument to
210 // be passed by const reference. The first form ought to run faster
211 // for the built-in types, which may be an issue for large Lattices
212 // stored in memory, where disk access is not an issue.
213 // <group>
214 virtual void apply(T (*function)(T));
215 virtual void apply(T (*function)(const T&));
216 virtual void apply(const Functional<T, T>& function);
217 // </group>
218
219 // Add a lattice to this image.
221
222 // Function which sets the units associated with the image
223 // pixels (i.e. the "brightness" unit). <src>setUnits()</src> returns
224 // False if it cannot set the unit for some reason (e.g. the underlying
225 // file is not writable).
226 virtual Bool setUnits(const Unit& newUnits);
227
228 // Flushes the new coordinate system to disk if the file is writable.
230
231 // These are the true implementations of the paran operator.
232 // <note> Not for public use </note>
233 // <group>
234 virtual T getAt(const IPosition& where) const;
235 virtual void putAt(const T& value, const IPosition& where);
236 // </group>
237
238 // Replace the miscinfo in the HDF5Image.
239 // It can fail if, e.g., the underlying file is not writable.
240 virtual Bool setMiscInfo(const RecordInterface& newInfo);
241
242 // The ImageInfo object contains some miscellaneous information about the
243 // image, which unlike that stored in MiscInfo, has a standard list of
244 // things, such as the restoring beam.
245 // Note that setImageInfo REPLACES the information with the new information.
246 // It can fail if, e.g., the underlying file is not writable.
247 virtual Bool setImageInfo(const ImageInfo& info);
248
249 // Get access to the attribute handler.
250 // If a handler keyword does not exist yet, it is created if
251 // <src>createHandler</src> is set.
252 // Otherwise the handler is empty and no groups can be created for it.
253 virtual ImageAttrHandler& attrHandler(Bool createHandler = False);
254
255 // Remove a region/mask belonging to the image from the given group
256 // (which can be Any).
257 // If a mask removed is the default mask, the image gets unmasked.
258 // <br>Optionally an exception is thrown if the region does not exist.
260 Bool throwIfUnknown = True);
261
262 // This is the implementation of the letter for the envelope Iterator
263 // class. <note> Not for public use </note>.
264 virtual LatticeIterInterface<T>* makeIter(const LatticeNavigator& navigator, Bool useRef) const;
265
266 // Returns the maximum recommended number of pixels for a cursor. This is
267 // the number of pixels in a tile.
268 virtual uInt advisedMaxPixels() const;
269
270 // Help the user pick a cursor for most efficient access.
271 virtual IPosition doNiceCursorShape(uInt maxPixels) const;
272
273 // Flush the data.
274 virtual void flush();
275
276 private:
277 // Function to return the internal HDF5File object to the RegionHandler.
278 static const std::shared_ptr<HDF5File>& getFile(void* imagePtr);
279
280 // This must be called in every constructor and place where the image
281 // is attached to a new image.
288
289 void check_conformance(const Lattice<T>& other);
291 void applyMask(const String& maskName);
292
293 // # Data members.
297
298 // # Make members of parent class known.
299 public:
301 using ImageInterface<T>::logger;
307
308 protected:
314};
315
316// Tell if HDF5 images can be used.
318
319// Determine the pixel type in the HDF5Image contained in
320// <src>fileName</src>. If the file doesn't appear to be HDF5 or cannot
321// be opened, TpOther is returned.
322// <group name="pixeltype")
323DataType hdf5imagePixelType(const String& fileName);
324// Check if this HDF5 file is an HDF5 image.
325Bool isHDF5Image(const String& fileName);
326// </group>
327
328} // namespace casacore
329
330#ifndef CASACORE_NO_AUTO_TEMPLATES
331#include <casacore/images/Images/HDF5Image.tcc>
332#endif // # CASACORE_NO_AUTO_TEMPLATES
333#endif
virtual String name(Bool stripPath=False) const
Return the current HDF5 file name.
virtual T getAt(const IPosition &where) const
These are the true implementations of the paran operator.
HDF5Image(const TiledShape &mapShape, const CoordinateSystem &coordinateInfo, const String &nameOfNewFile)
Construct a new Image from shape and coordinate information.
void restoreImageInfo(const RecordInterface &rec)
void applyMask(const String &maskName)
virtual Bool isWritable() const
Is the HDF5Image writable?
HDF5Image< T > & operator+=(const Lattice< T > &other)
Add a lattice to this image.
HDF5Image(const HDF5Image< T > &other)
Copy constructor (reference semantics).
virtual Bool setUnits(const Unit &newUnits)
Function which sets the units associated with the image pixels (i.e.
virtual void putAt(const T &value, const IPosition &where)
Put the value of a single element.
virtual void doPutSlice(const Array< T > &sourceBuffer, const IPosition &where, const IPosition &stride)
Function to replace the values in the map with soureBuffer.
virtual IPosition doNiceCursorShape(uInt maxPixels) const
Help the user pick a cursor for most efficient access.
LatticeRegion * regionPtr_p
Definition HDF5Image.h:295
virtual Bool setMiscInfo(const RecordInterface &newInfo)
Replace the miscinfo in the HDF5Image.
virtual Bool doGetSlice(Array< T > &buffer, const Slicer &theSlice)
Function which extracts an array from the map.
virtual IPosition shape() const
Return the shape of the image.
virtual void apply(T(*function)(T))
Replace every element, x, of the lattice with the result of f(x).
virtual void flush()
Flush the data.
virtual String imageType() const
Get the image type (returns name of derived class).
virtual Bool setImageInfo(const ImageInfo &info)
The ImageInfo object contains some miscellaneous information about the image, which unlike that store...
virtual ImageAttrHandler & attrHandler(Bool createHandler=False)
Get access to the attribute handler.
HDF5Image(const String &fileName, MaskSpecifier=MaskSpecifier())
Reconstruct an image from a pre-existing file.
virtual Bool isPersistent() const
An HDF5Image is always persistent.
virtual void apply(T(*function)(const T &))
void applyMaskSpecifier(const MaskSpecifier &)
HDF5Lattice< T > map_p
Definition HDF5Image.h:294
virtual ImageInterface< T > * cloneII() const
Make a copy of the object (reference semantics).
virtual Lattice< Bool > & pixelMask()
virtual Bool ok() const
Check for symmetry in data members.
virtual Bool setCoordinateInfo(const CoordinateSystem &coords)
Flushes the new coordinate system to disk if the file is writable.
virtual void removeRegion(const String &name, RegionHandler::GroupType=RegionHandler::Any, Bool throwIfUnknown=True)
Remove a region/mask belonging to the image from the given group (which can be Any).
void restoreUnits(const RecordInterface &rec)
HDF5Image< T > & operator=(const HDF5Image< T > &other)
Assignment operator (reference semantics).
virtual uInt advisedMaxPixels() const
Returns the maximum recommended number of pixels for a cursor.
virtual void resize(const TiledShape &newShape)
Function which changes the shape of the ImageExpr.
void restoreMiscInfo(const RecordInterface &rec)
virtual const LatticeRegion * getRegionPtr() const
Get a pointer the default pixelmask object used with this image.
virtual void setDefaultMask(const String &maskName)
Set the default pixelmask to the mask with the given name (which has to exist in the "masks" group).
void attach_logtable()
This must be called in every constructor and place where the image is attached to a new image.
virtual void useMask(MaskSpecifier=MaskSpecifier())
Use the mask as specified.
void check_conformance(const Lattice< T > &other)
virtual LatticeIterInterface< T > * makeIter(const LatticeNavigator &navigator, Bool useRef) const
This is the implementation of the letter for the envelope Iterator class; Note: Not for public use ...
virtual void apply(const Functional< T, T > &function)
virtual Bool hasPixelMask() const
Does the image object use a pixelmask?
virtual Bool isPaged() const
An HDF5Image is always paged to disk.
static const std::shared_ptr< HDF5File > & getFile(void *imagePtr)
Function to return the internal HDF5File object to the RegionHandler.
ImageAttrHandlerHDF5 itsAttrHandler
Definition HDF5Image.h:296
virtual const Lattice< Bool > & pixelMask() const
Get access to the pixelmask used.
static Bool hasHDF5Support()
Check if there is HDF5 support compiled in.
LogIO & logSink()
Allow messages to be logged to this ImageInterface.
void setCoordsMember(const CoordinateSystem &coords)
Set the coordinate system variable.
LoggerHolder & logger()
Get access to the LoggerHolder.
void setLogMember(const LoggerHolder &logger)
Set the image logger variable.
const ImageInfo & imageInfo() const
The ImageInfo object contains some miscellaneous information about the image which unlike that stored...
virtual String getDefaultMask() const
Get the name of the default pixelmask.
virtual ImageRegion * getImageRegionPtr(const String &name, RegionHandler::GroupType=RegionHandler::Any, Bool throwIfUnknown=True) const
Get a region/mask belonging to the image from the given group (which can be Any).
void setImageInfoMember(const ImageInfo &imageInfo)
Set the image info variable.
virtual Bool hasRegion(const String &regionName, RegionHandler::GroupType=RegionHandler::Any) const
Does the image have a region with the given name?
void setMiscInfoMember(const RecordInterface &rec)
Set the miscinfo variable.
const CoordinateSystem & coordinates() const
void setUnitMember(const Unit &unit)
Set the unit variable.
GroupType
Define the possible group types (regions or masks).
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
Bool isHDF5Image(const String &fileName)
Check if this HDF5 file is an HDF5 image.
unsigned int uInt
Definition aipstype.h:49
DataType hdf5imagePixelType(const String &fileName)
Determine the pixel type in the HDF5Image contained in fileName.
RecordInterface()
The default constructor creates an empty record with a variable structure.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
Bool canUseHDF5Image()
Tell if HDF5 images can be used.
Definition HDF5Image.h:317
const Bool True
Definition aipstype.h:41
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360