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
121 template <class T> class HDF5Image: public ImageInterface<T>
122 {
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,
127 const CoordinateSystem& coordinateInfo,
128 const String& nameOfNewFile);
129
130 // Reconstruct an image from a pre-existing file.
131 // By default the default pixelmask (if available) is used.
132 explicit HDF5Image (const String& fileName, MaskSpecifier = MaskSpecifier());
133
134 // Copy constructor (reference semantics).
135 HDF5Image (const HDF5Image<T>& other);
136
138
139 // Assignment operator (reference semantics).
141
142 // Make a copy of the object (reference semantics).
143 virtual ImageInterface<T>* cloneII() const;
144
145 // Get the image type (returns name of derived class).
146 virtual String imageType() const;
147
148 // Return the current HDF5 file name. By default this includes the full path.
149 // The path preceding the file name can be stripped off on request.
150 virtual String name (Bool stripPath=False) const;
151
152 // Function which changes the shape of the ImageExpr.
153 // Throws an exception as an HDF5Image cannot be resized.
154 virtual void resize(const TiledShape& newShape);
155
156 // Check for symmetry in data members.
157 virtual Bool ok() const;
158
159 // Return the shape of the image.
160 virtual IPosition shape() const;
161
162 // Function which extracts an array from the map.
163 virtual Bool doGetSlice (Array<T>& buffer, const Slicer& theSlice);
164
165 // Function to replace the values in the map with soureBuffer.
166 virtual void doPutSlice (const Array<T>& sourceBuffer,
167 const IPosition& where,
168 const IPosition& stride);
169
170 // Get a pointer the default pixelmask object used with this image.
171 // It returns 0 if no default pixelmask is used.
172 virtual const LatticeRegion* getRegionPtr() const;
173
174 // An HDF5Image is always persistent.
175 virtual Bool isPersistent() const;
176
177 // An HDF5Image is always paged to disk.
178 virtual Bool isPaged() const;
179
180 // Is the HDF5Image writable?
181 virtual Bool isWritable() const;
182
183 // Does the image object use a pixelmask?
184 virtual Bool hasPixelMask() const;
185
186 // Get access to the pixelmask used.
187 // An exception is thrown if the image does not use a pixelmask.
188 // <group>
189 virtual const Lattice<Bool>& pixelMask() const;
191 // </group>
192
193 // Set the default pixelmask to the mask with the given name
194 // (which has to exist in the "masks" group).
195 // If the image file is writable, the setting is persistent by writing
196 // the name as a keyword.
197 // If the given mask name is the empty string,
198 // the default pixelmask is unset.
199 virtual void setDefaultMask (const String& maskName);
200
201 // Use the mask as specified.
202 // If a mask was already in use, it is replaced by the new one.
204
205 // Replace every element, x, of the lattice with the result of f(x).
206 // you must pass in the address of the function -- so the function
207 // must be declared and defined in the scope of your program.
208 // Both versions of apply require a function that accepts a single
209 // argument of type T (the Lattice template actual type) and returns
210 // a result of the same type. The first apply expects a function with
211 // an argument passed by value; the second expects the argument to
212 // be passed by const reference. The first form ought to run faster
213 // for the built-in types, which may be an issue for large Lattices
214 // stored in memory, where disk access is not an issue.
215 // <group>
216 virtual void apply (T (*function)(T));
217 virtual void apply (T (*function)(const T& ));
218 virtual void apply (const Functional<T,T>& function);
219 // </group>
220
221 // Add a lattice to this image.
223
224 // Function which sets the units associated with the image
225 // pixels (i.e. the "brightness" unit). <src>setUnits()</src> returns
226 // False if it cannot set the unit for some reason (e.g. the underlying
227 // file is not writable).
228 virtual Bool setUnits (const Unit& newUnits);
229
230 // Flushes the new coordinate system to disk if the file is writable.
231 virtual Bool setCoordinateInfo (const CoordinateSystem& coords);
232
233 // These are the true implementations of the paran operator.
234 // <note> Not for public use </note>
235 // <group>
236 virtual T getAt (const IPosition& where) const;
237 virtual void putAt (const T& value, const IPosition& where);
238 // </group>
239
240 // Replace the miscinfo in the HDF5Image.
241 // It can fail if, e.g., the underlying file is not writable.
242 virtual Bool setMiscInfo (const RecordInterface& newInfo);
243
244 // The ImageInfo object contains some miscellaneous information about the
245 // image, which unlike that stored in MiscInfo, has a standard list of
246 // things, such as the restoring beam.
247 // Note that setImageInfo REPLACES the information with the new information.
248 // It can fail if, e.g., the underlying file is not writable.
249 virtual Bool setImageInfo(const ImageInfo& info);
250
251 // Get access to the attribute handler.
252 // If a handler keyword does not exist yet, it is created if
253 // <src>createHandler</src> is set.
254 // Otherwise the handler is empty and no groups can be created for it.
255 virtual ImageAttrHandler& attrHandler (Bool createHandler=False);
256
257 // Remove a region/mask belonging to the image from the given group
258 // (which can be Any).
259 // If a mask removed is the default mask, the image gets unmasked.
260 // <br>Optionally an exception is thrown if the region does not exist.
261 virtual void removeRegion (const String& name,
263 Bool throwIfUnknown = True);
264
265 // This is the implementation of the letter for the envelope Iterator
266 // class. <note> Not for public use </note>.
268 (const LatticeNavigator& navigator,
269 Bool useRef) const;
270
271 // Returns the maximum recommended number of pixels for a cursor. This is
272 // the number of pixels in a tile.
273 virtual uInt advisedMaxPixels() const;
274
275 // Help the user pick a cursor for most efficient access.
276 virtual IPosition doNiceCursorShape (uInt maxPixels) const;
277
278 // Flush the data.
279 virtual void flush();
280
281
282 private:
283 // Function to return the internal HDF5File object to the RegionHandler.
284 static const std::shared_ptr<HDF5File>& getFile (void* imagePtr);
285
286 // This must be called in every constructor and place where the image
287 // is attached to a new image.
290 void restoreUnits (const RecordInterface& rec);
294
295 void check_conformance (const Lattice<T>& other);
297 void applyMask (const String& maskName);
298
299 //# Data members.
303
304 //# Make members of parent class known.
305 public:
307 using ImageInterface<T>::logger;
313 protected:
319 };
320
321
322 // Tell if HDF5 images can be used.
325
326 // Determine the pixel type in the HDF5Image contained in
327 // <src>fileName</src>. If the file doesn't appear to be HDF5 or cannot
328 // be opened, TpOther is returned.
329 // <group name="pixeltype")
330 DataType hdf5imagePixelType (const String& fileName);
331 // Check if this HDF5 file is an HDF5 image.
332 Bool isHDF5Image (const String& fileName);
333 // </group>
334
335
336} //# NAMESPACE CASACORE - END
337
338#ifndef CASACORE_NO_AUTO_TEMPLATES
339#include <casacore/images/Images/HDF5Image.tcc>
340#endif //# CASACORE_NO_AUTO_TEMPLATES
341#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:301
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:300
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:302
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:223
this file contains all the compiler specific defines
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.
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.
Bool canUseHDF5Image()
Tell if HDF5 images can be used.
Definition HDF5Image.h:323
const Bool True
Definition aipstype.h:41