casacore
Loading...
Searching...
No Matches
HDF5Record.h
Go to the documentation of this file.
1//# HDF5Record.h: A class to write/read a record into HDF5
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 CASA_HDF5RECORD_H
27#define CASA_HDF5RECORD_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/HDF5/HDF5Object.h>
32#include <casacore/casa/HDF5/HDF5DataType.h>
33#include <casacore/casa/Containers/Record.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37 // <summary>
38 // A class to write/read a record into HDF5.
39 // </summary>
40
41 // <use visibility=export>
42
43 // <reviewed reviewer="" date="" tests="tHDF5Record.cc">
44 // </reviewed>
45
46 // <prerequisite>
47 // <li> <a href="http://hdf.ncsa.uiuc.edu">HDF5 system</a>
48 // <li> <linkto class=Record>class Record</linkto>
49 // </prerequisite>
50
51 // <synopsis>
52 // This class has a static function to write a Record (or TableRecord)
53 // into an HDF5 file by storing it as attributes for the given group.
54 // Another static function can read back the Record.
55 // It can handle all types of fields in a record.
56 // <br>
57 // A few remarks:
58 // <ul>
59 // <li> When writing the record, it first deletes all attributes of the group
60 // to be sure that the group's attributes only contain the record.
61 // <li> A Casacore Record is a recursive structure, so it is written as
62 // nested groups. The name of a subgroup is the name of the subrecord.
63 // <li> HDF5 cannot deal with empty arrays. Therefore they are written as
64 // a special compound type holding the rank and type of the empty array.
65 // <li> HDF5 cannot hold empty fixed length strings. This is solved by
66 // storing an empty string with the special value <tt>__empty__</tt>.
67 // </ul>
68 // </synopsis>
69
70 // <motivation>
71 // Record is a very important class in Casacore images, so it has to be
72 // possible to read and write them from/to HDF5.
73 // </motivation>
74
76 {
77 public:
78 // Read a record from the attributes of the given group.
79 // Nested records are read back correctly.
80 // An empty record is returned if the group does not exist.
81 static Record readRecord (const HDF5Object& parentHid,
82 const String& groupName);
83
84 // Write the record as attributes of a group of the given parent.
85 // Nested records are written as nested groups.
86 // The group is deleted first if it already exists.
87 static void writeRecord (const HDF5Object& parentHid,
88 const String& recordName,
89 const RecordInterface& rec);
90
91 // Remove the record (i.e. group) from the given parent.
92 // Nothing is done if the record does not exist.
93 static void remove (const HDF5Object& parentHid,
94 const String& recordName);
95
96 // Read the (possibly nested) record values from the given group hid.
97 static Record doReadRecord (hid_t parentHid);
98
99 // Write the (possibly nested) record values into the given group hid.
100 static void doWriteRecord (const HDF5Object& groupHid,
101 const RecordInterface& rec);
102
103 private:
104 // Read a scalar value and add it to the record.
105 static void readScalar (hid_t attrId, hid_t dtid,
106 const String& name, RecordInterface& rec);
107
108 // Read an array value and add it to the record.
109 static void readArray (hid_t attrId, hid_t dtid, const IPosition&,
110 const String& name, RecordInterface& rec);
111
112 // Read a scalar string from an attribute and add it to the record.
113 static void readScaString (hid_t attrId, Int sz,
114 const String& name, RecordInterface& rec);
115
116 // Read a array of strings from an atrribute and add it to the record.
117 static void readArrString (hid_t attrId, const IPosition&,
118 const String& name, RecordInterface& rec);
119
120 // Read a field containing an empty array.
121 static void readEmptyArray (hid_t attrId,
122 const String& name, RecordInterface& rec);
123
124 // Read a field containing a scalar of fixed length.
125 template<typename T>
126 static void readSca (hid_t attrId, const String& name,
127 RecordInterface& rec)
128 {
129 T value;
130 HDF5DataType dtype((T*)0);
131 read (attrId, &value, dtype);
132 rec.define (name, value);
133 }
134
135 // Read a field containing an array of fixed length elements.
136 template<typename T>
137 static void readArr (hid_t attrId, const IPosition& shape,
138 const String& name,
139 RecordInterface& rec)
140 {
142 HDF5DataType dtype((T*)0);
143 read (attrId, value.data(), dtype);
144 rec.define (name, value);
145 }
146
147 // Read fixed length values from an attribute (scalar and array).
148 static void read (hid_t attrId, void* value,
149 const HDF5DataType& dtype);
150
151 // Write a fixed length scalar value as attribute.
152 static void writeScalar (hid_t parentHid, const String& name,
153 const void* value,
154 const HDF5DataType& dtype);
155
156 // Write an array of fixed length values as attribute.
157 static void writeArray (hid_t parentHid, const String& name,
158 const void* value, const IPosition& shape,
159 const HDF5DataType& dtype);
160
161 // Write a scalar string as attribute.
162 // HDF5 cannot handle empty strings, so for empty strings a special
163 // value is written.
164 static void writeScaString (hid_t parentHid, const String& name,
165 const String& value);
166
167 // Write an array of strings as attribute.
168 // HDF5 cannot handle empty strings, so for empty strings a special
169 // value is written.
170 static void writeArrString (hid_t parentHid, const String& name,
171 const Array<String>& value);
172
173 // Write a field containing an empty array.
174 static void writeEmptyArray (hid_t groupHid, const String& name,
175 Int rank, DataType dtype);
176
177 // Write a field containing a fixed length scalar value.
178 template<typename T>
179 static void writeSca (hid_t parentHid, const String& name,
180 const RecordInterface& rec, Int i)
181 {
182 T value;
183 rec.get (i, value);
184 HDF5DataType dtype((T*)0);
185 writeScalar (parentHid, name, &value, dtype);
186 }
187
188 // Write a field containing an array of fixed length elements.
189 template<typename T>
190 static void writeArr (hid_t parentHid, const String& name,
191 const RecordInterface& rec, Int i)
192 {
194 rec.get (i, value);
195 HDF5DataType dtype((T*)0);
196 writeArray (parentHid, name, value.data(), value.shape(), dtype);
197 }
198
199 };
200
201}
202
203#endif
static void writeArrString(hid_t parentHid, const String &name, const Array< String > &value)
Write an array of strings as attribute.
static void readArrString(hid_t attrId, const IPosition &, const String &name, RecordInterface &rec)
Read a array of strings from an atrribute and add it to the record.
static void writeEmptyArray(hid_t groupHid, const String &name, Int rank, DataType dtype)
Write a field containing an empty array.
static void remove(const HDF5Object &parentHid, const String &recordName)
Remove the record (i.e.
static void readSca(hid_t attrId, const String &name, RecordInterface &rec)
Read a field containing a scalar of fixed length.
Definition HDF5Record.h:126
static Record readRecord(const HDF5Object &parentHid, const String &groupName)
Read a record from the attributes of the given group.
static void readEmptyArray(hid_t attrId, const String &name, RecordInterface &rec)
Read a field containing an empty array.
static void readArr(hid_t attrId, const IPosition &shape, const String &name, RecordInterface &rec)
Read a field containing an array of fixed length elements.
Definition HDF5Record.h:137
static void writeRecord(const HDF5Object &parentHid, const String &recordName, const RecordInterface &rec)
Write the record as attributes of a group of the given parent.
static void writeScaString(hid_t parentHid, const String &name, const String &value)
Write a scalar string as attribute.
static Record doReadRecord(hid_t parentHid)
Read the (possibly nested) record values from the given group hid.
static void writeSca(hid_t parentHid, const String &name, const RecordInterface &rec, Int i)
Write a field containing a fixed length scalar value.
Definition HDF5Record.h:179
static void doWriteRecord(const HDF5Object &groupHid, const RecordInterface &rec)
Write the (possibly nested) record values into the given group hid.
static void readScaString(hid_t attrId, Int sz, const String &name, RecordInterface &rec)
Read a scalar string from an attribute and add it to the record.
static void writeArr(hid_t parentHid, const String &name, const RecordInterface &rec, Int i)
Write a field containing an array of fixed length elements.
Definition HDF5Record.h:190
static void writeScalar(hid_t parentHid, const String &name, const void *value, const HDF5DataType &dtype)
Write a fixed length scalar value as attribute.
static void readScalar(hid_t attrId, hid_t dtid, const String &name, RecordInterface &rec)
Read a scalar value and add it to the record.
static void read(hid_t attrId, void *value, const HDF5DataType &dtype)
Read fixed length values from an attribute (scalar and array).
static void readArray(hid_t attrId, hid_t dtid, const IPosition &, const String &name, RecordInterface &rec)
Read an array value and add it to the record.
static void writeArray(hid_t parentHid, const String &name, const void *value, const IPosition &shape, const HDF5DataType &dtype)
Write an array of fixed length values as attribute.
const IPosition & shape() const
Return the shape of the Lattice including all degenerate axes (ie.
void define(const RecordFieldId &, Bool value)
Define a value for the given field.
void get(const RecordFieldId &, Bool &value) const
Get the value of the given field.
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
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
int Int
Definition aipstype.h:48
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.