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 public:
77 // Read a record from the attributes of the given group.
78 // Nested records are read back correctly.
79 // An empty record is returned if the group does not exist.
80 static Record readRecord(const HDF5Object& parentHid, const String& groupName);
81
82 // Write the record as attributes of a group of the given parent.
83 // Nested records are written as nested groups.
84 // The group is deleted first if it already exists.
85 static void writeRecord(const HDF5Object& parentHid, const String& recordName,
86 const RecordInterface& rec);
87
88 // Remove the record (i.e. group) from the given parent.
89 // Nothing is done if the record does not exist.
90 static void remove(const HDF5Object& parentHid, const String& recordName);
91
92 // Read the (possibly nested) record values from the given group hid.
93 static Record doReadRecord(hid_t parentHid);
94
95 // Write the (possibly nested) record values into the given group hid.
96 static void doWriteRecord(const HDF5Object& groupHid, const RecordInterface& rec);
97
98 private:
99 // Read a scalar value and add it to the record.
100 static void readScalar(hid_t attrId, hid_t dtid, const String& name, RecordInterface& rec);
101
102 // Read an array value and add it to the record.
103 static void readArray(hid_t attrId, hid_t dtid, const IPosition&, const String& name,
104 RecordInterface& rec);
105
106 // Read a scalar string from an attribute and add it to the record.
107 static void readScaString(hid_t attrId, Int sz, const String& name, RecordInterface& rec);
108
109 // Read a array of strings from an atrribute and add it to the record.
110 static void readArrString(hid_t attrId, const IPosition&, const String& name,
111 RecordInterface& rec);
112
113 // Read a field containing an empty array.
114 static void readEmptyArray(hid_t attrId, const String& name, RecordInterface& rec);
115
116 // Read a field containing a scalar of fixed length.
117 template <typename T>
118 static void readSca(hid_t attrId, const String& name, RecordInterface& rec) {
119 T value;
120 HDF5DataType dtype((T*)0);
121 read(attrId, &value, dtype);
122 rec.define(name, value);
123 }
124
125 // Read a field containing an array of fixed length elements.
126 template <typename T>
127 static void readArr(hid_t attrId, const IPosition& shape, const String& name,
128 RecordInterface& rec) {
130 HDF5DataType dtype((T*)0);
131 read(attrId, value.data(), dtype);
132 rec.define(name, value);
133 }
134
135 // Read fixed length values from an attribute (scalar and array).
136 static void read(hid_t attrId, void* value, const HDF5DataType& dtype);
137
138 // Write a fixed length scalar value as attribute.
139 static void writeScalar(hid_t parentHid, const String& name, const void* value,
140 const HDF5DataType& dtype);
141
142 // Write an array of fixed length values as attribute.
143 static void writeArray(hid_t parentHid, const String& name, const void* value,
144 const IPosition& shape, const HDF5DataType& dtype);
145
146 // Write a scalar string as attribute.
147 // HDF5 cannot handle empty strings, so for empty strings a special
148 // value is written.
149 static void writeScaString(hid_t parentHid, const String& name, const String& value);
150
151 // Write an array of strings as attribute.
152 // HDF5 cannot handle empty strings, so for empty strings a special
153 // value is written.
154 static void writeArrString(hid_t parentHid, const String& name, const Array<String>& value);
155
156 // Write a field containing an empty array.
157 static void writeEmptyArray(hid_t groupHid, const String& name, Int rank, DataType dtype);
158
159 // Write a field containing a fixed length scalar value.
160 template <typename T>
161 static void writeSca(hid_t parentHid, const String& name, const RecordInterface& rec, Int i) {
162 T value;
163 rec.get(i, value);
164 HDF5DataType dtype((T*)0);
165 writeScalar(parentHid, name, &value, dtype);
166 }
167
168 // Write a field containing an array of fixed length elements.
169 template <typename T>
170 static void writeArr(hid_t parentHid, const String& name, const RecordInterface& rec, Int i) {
172 rec.get(i, value);
173 HDF5DataType dtype((T*)0);
174 writeArray(parentHid, name, value.data(), value.shape(), dtype);
175 }
176};
177
178} // namespace casacore
179
180#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:118
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:127
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:161
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:170
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.
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
virtual int read()
The read()' and write()' functions control reading and writing data from the external FITS I/O medium...
IPosition shape(const RecordFieldId &) const
Get the actual shape of this field.
String name() const
Return the name of the field.
RecordInterface()
The default constructor creates an empty record with a variable structure.
int Int
Definition aipstype.h:48
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360