9 double hdf_file_apriori_double(
const HdfFile& h,
const std::string& gname)
11 return h.
read_field<double, 1>(gname +
"/a_priori")(0);
14 std::string hdf_file_read_string(
const HdfFile& h,
const std::string& fname)
19 std::vector<std::string> hdf_file_read_string_vector(
const HdfFile& h,
const std::string& fname)
21 blitz::Array<std::string, 1> str_arr = h.
read_field<std::string, 1>(fname);
22 std::vector<std::string> result;
23 for(
int i = 0; i < str_arr.rows(); i++)
24 result.push_back(str_arr(i));
28 std::vector<std::string> hdf_file_read_string_vector_row(
const HdfFile& h,
const std::string& fname,
int row)
30 blitz::Array<std::string, 2> str_arr = h.
read_field<std::string, 2>(fname);
31 std::vector<std::string> result;
32 for(
int i = 0; i < str_arr.cols(); i++)
33 result.push_back(str_arr(0,i));
37 Array<int, 1> hdf_file_read_int_1d(
const HdfFile& h,
const std::string& fname)
42 Array<int, 2> hdf_file_read_int_2d(
const HdfFile& h,
const std::string& fname)
47 Array<int, 3> hdf_file_read_int_3d(
const HdfFile& h,
const std::string& fname)
52 double hdf_file_read_double_scalar(
const HdfFile& h,
const std::string& gname)
57 Array<double, 1> hdf_file_read_double_1d(
const HdfFile& h,
58 const std::string& fname)
63 double hdf_file_read_double_1d_i(
const HdfFile& h,
64 const std::string& fname,
67 blitz::TinyVector<int,1> start;
68 blitz::TinyVector<int,1> sz;
71 return h.
read_field<double, 1>(fname, start, sz)(0);
75 const std::string& fname)
81 Array<double, 2> hdf_file_read_double_2d(
const HdfFile& h,
82 const std::string& fname)
87 double hdf_file_read_double_2d_i(
const HdfFile& h,
88 const std::string& fname,
91 blitz::TinyVector<int,2> start;
92 blitz::TinyVector<int,2> sz;
95 return h.
read_field<double, 2>(fname, start, sz)(0, 0);
99 const std::string& fname)
105 Array<double, 3> hdf_file_read_double_3d(
const HdfFile& h,
106 const std::string& fname)
111 double hdf_file_read_double_3d_i(
const HdfFile& h,
112 const std::string& fname,
115 blitz::TinyVector<int,3> start;
116 blitz::TinyVector<int,3> sz;
119 return h.
read_field<double, 3>(fname, start, sz)(0, 0, 0);
123 const std::string& fname)
128 Array<double, 4> hdf_file_read_double_4d(
const HdfFile& h,
129 const std::string& fname)
136 Array<double, 3> hdf_file_read_double_4d_sounding(
const HdfFile& h,
137 const std::string& fname,
140 TinyVector<int, 4> shp = h.
read_shape<4>(fname);
141 TinyVector<int,4> start;
142 TinyVector<int,4> sz;
143 start = 0, Sounding_num, 0, 0;
144 sz = shp(0), 1, shp(2), shp(3);
145 return h.
read_field<double, 4>(fname, start, sz)(Range::all(), 0,
146 Range::all(), Range::all());
150 double hdf_file_read_double_4d_i(
const HdfFile& h,
151 const std::string& fname,
152 int I,
int J,
int K,
int L)
154 blitz::TinyVector<int,4> start;
155 blitz::TinyVector<int,4> sz;
158 return h.
read_field<double, 4>(fname, start, sz)(0, 0, 0, 0);
161 Array<double, 5> hdf_file_read_double_5d(
const HdfFile& h,
162 const std::string& fname)
167 Array<double, 1> hdf_file_apriori(
const HdfFile& h,
168 const std::string& gname)
170 return h.
read_field<double, 1>(gname +
"/a_priori");
174 const std::string& gname)
179 Array<double, 1> hdf_file_apriori2(
const HdfFile& h,
180 const std::string& gname,
183 return h.
read_field<double, 2>(gname +
"/a_priori")(row, Range::all());
186 Array<double, 1> hdf_file_apriori3(
const HdfFile& h,
187 const std::string& gname,
190 return h.
read_field<double, 3>(gname +
"/a_priori")(row, col, Range::all());
194 const std::string& gname,
201 Array<double, 2> hdf_file_covariance(
const HdfFile& h,
202 const std::string& gname)
204 return h.
read_field<double, 2>(gname +
"/covariance");
207 Array<double, 2> hdf_file_covariance2(
const HdfFile& h,
208 const std::string& gname,
211 return h.
read_field<double, 3>(gname +
"/covariance")(row, Range::all(),
215 Array<double, 2> hdf_file_covariance3(
const HdfFile& h,
216 const std::string& gname,
219 return h.
read_field<double, 4>(gname +
"/covariance")(row, col, Range::all(),
224 .def(luabind::constructor<std::string>())
225 .def(
"read_string", &hdf_file_read_string)
226 .def(
"read_string_vector", &hdf_file_read_string_vector)
227 .def(
"read_string_vector", &hdf_file_read_string_vector_row)
228 .def(
"read_int_1d", &hdf_file_read_int_1d)
229 .def(
"read_int_2d", &hdf_file_read_int_2d)
230 .def(
"read_int_3d", &hdf_file_read_int_3d)
231 .def(
"read_double_scalar", &hdf_file_read_double_scalar)
232 .def(
"read_double_1d", &hdf_file_read_double_1d)
233 .def(
"read_double_1d", &hdf_file_read_double_1d_i)
234 .def(
"read_double_with_unit_1d", &hdf_file_read_double_with_unit_1d)
235 .def(
"read_double_2d", &hdf_file_read_double_2d)
236 .def(
"read_double_2d", &hdf_file_read_double_2d_i)
237 .def(
"read_double_with_unit_2d", &hdf_file_read_double_with_unit_2d)
238 .def(
"read_double_3d", &hdf_file_read_double_3d)
239 .def(
"read_double_3d", &hdf_file_read_double_3d_i)
240 .def(
"read_double_with_unit_3d", &hdf_file_read_double_with_unit_3d)
241 .def(
"read_double_4d", &hdf_file_read_double_4d)
242 .def(
"read_double_4d_sounding", &hdf_file_read_double_4d_sounding)
243 .def(
"read_double_5d", &hdf_file_read_double_5d)
244 .def(
"apriori", &hdf_file_apriori)
245 .def(
"apriori_with_unit", &hdf_file_apriori_with_unit)
246 .def(
"apriori", &hdf_file_apriori2)
247 .def(
"apriori_with_unit", &hdf_file_apriori2_with_unit)
248 .def(
"apriori", &hdf_file_apriori3)
249 .def(
"apriori_double", &hdf_file_apriori_double)
250 .def(
"covariance", &hdf_file_covariance)
251 .def(
"covariance", &hdf_file_covariance2)
252 .def(
"covariance", &hdf_file_covariance3)
262 : fname(Fname), mode_(M)
267 flag = H5F_ACC_RDONLY;
273 flag = H5F_ACC_TRUNC;
276 throw Exception(
"This shouldn't happen");
278 H5::Exception::dontPrint();
283 FileAccPropList file_access = FileAccPropList();
284 file_access.setFcloseDegree(H5F_CLOSE_SEMI);
286 h.reset(
new H5File(Fname, flag, FileCreatPropList::DEFAULT, file_access));
287 }
catch(
const H5::Exception& e) {
289 en <<
"While trying to open file '" << Fname
290 <<
"' a HDF 5 Exception thrown:\n" 291 <<
" " << e.getDetailMsg();
302 #if H5_VERS_MAJOR == 1 && H5_VERS_MINOR >= 10 && H5_VERS_RELEASE >= 1 303 void HdfFile::create_group_if_needed(
const std::string& Dataname, H5::H5Location& Parent)
305 void HdfFile::create_group_if_needed(
const std::string& Dataname, H5::CommonFG& Parent)
308 size_t i = Dataname.find_first_of(
'/');
309 if(i ==std::string::npos)
313 create_group_if_needed(Dataname.substr(i + 1), Parent);
315 std::string gname = Dataname.substr(0, i);
316 if(!is_present(gname, Parent)) {
317 Group
g = Parent.createGroup(gname);
318 create_group_if_needed(Dataname.substr(i + 1),
g);
320 Group
g = Parent.openGroup(gname);
321 create_group_if_needed(Dataname.substr(i + 1),
g);
330 bool HdfFile::is_group(
const std::string& Objname)
const 333 h->getObjinfo(Objname, statbuf);
334 return statbuf.type == H5G_GROUP;
342 #if H5_VERS_MAJOR == 1 && H5_VERS_MINOR >= 10 && H5_VERS_RELEASE >= 1 343 bool HdfFile::is_present(
const std::string& Objname,
344 const H5::H5Location& Parent)
const 346 bool HdfFile::is_present(
const std::string& Objname,
347 const H5::CommonFG& Parent)
const 352 Parent.getObjinfo(Objname, statbuf);
353 }
catch(
const H5::Exception& E) {
366 open_attribute(Aname);
367 }
catch(
const H5::Exception& E) {
378 H5::Attribute HdfFile::open_attribute(
const std::string& Aname)
const 380 size_t i = Aname.find_last_of(
'/');
381 if(i == std::string::npos) {
383 e <<
"The attribute name '" << Aname <<
"' is not a valid name";
386 std::string gname = Aname.substr(0, i);
387 std::string aname = Aname.substr(i + 1);
388 if(is_group(gname)) {
389 Group attr_group = h->openGroup(gname);
390 return attr_group.openAttribute(aname);
392 DataSet attr_dataset = h->openDataSet(gname);
393 return attr_dataset.openAttribute(aname);
410 std::string u = read_attribute<std::string>(Dataname +
"/Unit");
411 if(u ==
"Rotation_deg")
413 else if(u ==
"Distance_m")
415 else if(u ==
"Velocity")
417 else if(u ==
"Radiance")
419 else if(u ==
"Wavelength_microns")
422 throw Exception(
"Unrecognized unit '" + u +
" found for attribute " 423 + Dataname +
"/Unit");
426 res =
Unit(read_attribute<std::string>(Dataname +
"/Units"));
434 H5::Attribute HdfFile::create_attribute(
const std::string& Aname,
435 const H5::DataSpace& Ds,
const H5::DataType& P)
437 size_t i = Aname.find_last_of(
'/');
438 if(i == std::string::npos) {
440 e <<
"The attribute name '" << Aname <<
"' is not a valid name";
443 std::string gname = Aname.substr(0, i);
444 std::string aname = Aname.substr(i + 1);
445 if(!is_present(gname, *h))
446 create_group_if_needed(Aname, *h);
447 if(is_group(gname)) {
448 Group attr_group = h->openGroup(gname);
449 return attr_group.createAttribute(aname, P, Ds);
451 DataSet attr_dataset = h->openDataSet(gname);
452 return attr_dataset.createAttribute(aname, P, Ds);
464 const std::string& Description,
492 const std::string& Dim2)
495 std::vector<std::string> dim;
509 const std::string& Dim2,
510 const std::string& Dim3)
513 std::vector<std::string> dim;
530 void HdfFile::write_type(
const std::string& Dataname,
const H5::DataType& P)
532 if(P ==PredType::NATIVE_INT32)
534 else if(P ==PredType::NATIVE_FLOAT)
542 <<
" File name: " <<
file_name() <<
"\n";
blitz::TinyVector< int, D > read_shape(const std::string &Dataname) const
Reads the shape of a dataset.
const Unit s("s", 1.0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0)
blitz::Array< T, D > read_field(const std::string &Dataname) const
Read a given field.
bool has_object(const std::string &Objname) const
Check to see if an object (such as a Dataset) is in the file.
const Unit cm("cm", 1e-2 *m)
void print(std::ostream &Os) const
Unit read_units(const std::string &Dataname) const
Read the units for a dataset.
This is the base of the exception hierarchy for Full Physics code.
const Unit sr("sr", 1.0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
const Unit micron("micron", 1e-6 *m)
This class reads and writes a HDF5 file.
#define REGISTER_LUA_CLASS(X)
Apply value function to a blitz array.
const Unit inv_cm("cm^-1", pow(cm, -1))
void dimension_metadata(const std::string &Name, const std::string &Description, int Size)
The SDOS products have a particular metadata convention where the information about each of the Dimen...
const Unit K("K", 1.0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
const Unit m("m", 1.0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Unit pow(const Unit &Dunit, const boost::rational< int > &Exponent)
void write_attribute(const std::string &Aname, const blitz::Array< T, D > &Data)
Write attribute to file.
const std::string & file_name() const
File name.
const Unit g("g", 1e-3 *kg)
Libraries such as boost::units allow unit handling where we know the units at compile time...
HdfFile(const std::string &Fname, Mode M=READ)
Open the given file with the given mode.
Contains classes to abstract away details in various Spurr Radiative Transfer software.
#define REGISTER_LUA_END()
ArrayWithUnit< T, D > read_field_with_unit(const std::string &Dataname) const
Read a given field, along with metadata describing the units.
void shape_metadata(const std::string &Name, const std::string &Dim1)
The SDOS products have a particular metadata convention where the information about each of the Shape...
bool has_attribute(const std::string &Aname) const
Check to see if a attribute is in the file.
const Unit deg("deg", pi/180.0 *rad)