hdf_io.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2018, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_HDF_IO_H
24 #define O2SCL_HDF_IO_H
25 
26 /** \file hdf_io.h
27  \brief File defining HDF I/O for selected \o2 objects
28 */
29 #include <boost/numeric/ublas/vector.hpp>
30 
31 #include <o2scl/hdf_file.h>
32 #include <o2scl/table.h>
33 #include <o2scl/table_units.h>
34 #include <o2scl/hist.h>
35 #include <o2scl/hist_2d.h>
36 #include <o2scl/table3d.h>
37 #include <o2scl/tensor_grid.h>
38 #include <o2scl/expval.h>
39 #include <o2scl/contour.h>
40 #include <o2scl/uniform_grid.h>
41 #include <o2scl/prob_dens_mdim_amr.h>
42 
43 /** \brief The \o2 namespace for I/O with HDF
44  */
45 namespace o2scl_hdf {
46 
47  /** \brief Input a \ref o2scl::prob_dens_mdim_amr object from a
48  \ref hdf_file
49  */
50  template<class vec_t, class mat_t>
51  void hdf_input(hdf_file &hf,
53  std::string name) {
54 
55  // If no name specified, find name of first group of specified type
56  if (name.length()==0) {
57  hf.find_object_by_type("prob_dens_mdim_amr",name);
58  if (name.length()==0) {
59  O2SCL_ERR2("No object of type prob_dens_mdim_amr found in ",
60  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
61  }
62  }
63 
64  // Open main group
65  hid_t top=hf.get_current_id();
66  hid_t group=hf.open_group(name);
67  hf.set_current_id(group);
68 
69  size_t nd, dc, ms;
70  std::vector<double> data;
71  std::vector<size_t> insides;
72 
73  hf.get_szt("n_dim",nd);
74  hf.get_szt("dim_choice",dc);
75  hf.get_szt("mesh_size",ms);
76  hf.getd_vec("data",data);
77  hf.get_szt_vec("insides",insides);
78 
79  p.set_from_vectors(nd,dc,ms,data,insides);
80 
81  // Close group
82  hf.close_group(group);
83 
84  // Return location to previous value
85  hf.set_current_id(top);
86 
87  return;
88  }
89 
90  /** \brief Output a \ref o2scl::prob_dens_mdim_amr
91  object to a \ref hdf_file
92  */
93  template<class vec_t, class mat_t>
94  void hdf_output(hdf_file &hf,
96  std::string name) {
97 
98  if (hf.has_write_access()==false) {
99  O2SCL_ERR2("File not opened with write access in hdf_output",
100  "(hdf_file,prob_dens_mdim_amr<>,string).",
102  }
103 
104  // Start group
105  hid_t top=hf.get_current_id();
106  hid_t group=hf.open_group(name);
107  hf.set_current_id(group);
108 
109  // Add typename
110  hf.sets_fixed("o2scl_type","prob_dens_mdim_amr");
111 
112  size_t nd, dc, ms;
113  std::vector<double> data;
114  std::vector<size_t> insides;
115 
116  p.copy_to_vectors(nd,dc,ms,data,insides);
117 
118  hf.set_szt("n_dim",nd);
119  hf.set_szt("dim_choice",dc);
120  hf.set_szt("mesh_size",ms);
121  hf.setd_vec("data",data);
122  hf.set_szt_vec("insides",insides);
123 
124  // Close table_units group
125  hf.close_group(group);
126 
127  // Return location to previous value
128  hf.set_current_id(top);
129 
130  return;
131  }
132 
133  /** \brief Output a \ref o2scl::table object to a \ref hdf_file
134  */
135  void hdf_output(hdf_file &hf, o2scl::table<> &t, std::string name);
136 
137 #ifndef O2SCL_NO_HDF_INPUT
138  /** \brief Input a \ref o2scl::table object from a \ref hdf_file
139 
140  \comment
141  Note that a default value is not allowed here because this
142  is a template function
143  \endcomment
144  */
145  template<class vec_t>
146  void hdf_input(hdf_file &hf, o2scl::table<vec_t> &t, std::string name) {
147 
148  // If no name specified, find name of first group of specified type
149  if (name.length()==0) {
150  hf.find_object_by_type("table",name);
151  if (name.length()==0) {
152  O2SCL_ERR2("No object of type table found in ",
153  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
154  }
155  }
156 
157  // Open main group
158  hid_t top=hf.get_current_id();
159  hid_t group=hf.open_group(name);
160  hf.set_current_id(group);
161 
162  // Input the table data
163  hdf_input_data(hf,t);
164 
165  // Close group
166  hf.close_group(group);
167 
168  // Return location to previous value
169  hf.set_current_id(top);
170 
171  t.check_synchro();
172 
173  return;
174  }
175 #endif
176 
177  /** \brief Internal function for outputting a \ref o2scl::table object
178  */
179  void hdf_output_data(hdf_file &hf, o2scl::table<> &t);
180 
181  /** \brief Internal function for inputting a \ref o2scl::table object
182  */
183  template<class vec_t>
185  hid_t group=hf.get_current_id();
186 
187  // Clear previous data
188  t.clear_table();
189  t.clear_constants();
190 
191  // Check typename
192  std::string type2;
193  hf.gets_fixed("o2scl_type",type2);
194  if (type2!="table") {
195  O2SCL_ERR2("Typename in HDF group does not match ",
196  "class in o2scl_hdf::hdf_input_data().",o2scl::exc_einval);
197  }
198 
199  // Storage
200  std::vector<std::string> cnames, cols;
201  typedef boost::numeric::ublas::vector<double> ubvector;
202  ubvector cvalues;
203 
204  // Get constants
205  hf.gets_vec("con_names",cnames);
206  hf.getd_vec_copy("con_values",cvalues);
207  if (cnames.size()!=cvalues.size()) {
208  O2SCL_ERR2("Size mismatch between constant names and values ",
209  "in o2scl_hdf::hdf_input_data().",o2scl::exc_einval);
210  }
211  for(size_t i=0;i<cnames.size();i++) {
212  t.add_constant(cnames[i],cvalues[i]);
213  }
214 
215  // Get column names
216  hf.gets_vec("col_names",cols);
217  for(size_t i=0;i<cols.size();i++) {
218  t.new_column(cols[i]);
219  }
220 
221  // Get number of lines
222  int nlines2;
223  hf.geti("nlines",nlines2);
224  t.set_nlines(nlines2);
225 
226  // Output the interpolation type
227  hf.get_szt_def("itype",o2scl::itp_cspline,t.itype);
228 
229  // Open data group
230  hid_t group2=hf.open_group("data");
231  hf.set_current_id(group2);
232 
233  if (nlines2>0) {
234 
235  // Get data
236  for(size_t i=0;i<t.get_ncolumns();i++) {
237  ubvector vtmp(nlines2);
238  hf.getd_vec_copy(t.get_column_name(i),vtmp);
239  for(int j=0;j<nlines2;j++) {
240  t.set(t.get_column_name(i),j,vtmp[j]);
241  }
242  }
243 
244  }
245 
246  // Close groups
247  hf.close_group(group2);
248 
249  hf.set_current_id(group);
250 
251  // Check that input created a valid table
252  t.check_synchro();
253 
254  return;
255  }
256 
257  /** \brief Output a \ref o2scl::table_units object to a \ref hdf_file
258  */
259  void hdf_output(hdf_file &hf, o2scl::table_units<> &t,
260  std::string name);
261 
262  /** \brief Input a \ref o2scl::table_units object from a \ref hdf_file
263 
264  \comment
265  Note that a default value is not allowed here because this
266  is a template function
267  \endcomment
268  */
269  template<class vec_t>
271  std::string name) {
272 
273  // If no name specified, find name of first group of specified type
274  if (name.length()==0) {
275  hf.find_object_by_type("table",name);
276  if (name.length()==0) {
277  O2SCL_ERR2("No object of type table found in ",
278  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
279  }
280  }
281 
282  // Open main group
283  hid_t top=hf.get_current_id();
284  hid_t group=hf.open_group(name);
285  hf.set_current_id(group);
286 
287  // Input the table_units data
288  hdf_input_data(hf,t);
289 
290  // Close group
291  hf.close_group(group);
292 
293  // Return location to previous value
294  hf.set_current_id(top);
295 
296  t.check_synchro();
297 
298  return;
299  }
300 
301  /** \brief Internal function for outputting a \ref o2scl::table_units object
302  */
303  void hdf_output_data(hdf_file &hf, o2scl::table_units<> &t);
304 
305  /** \brief Internal function for inputting a \ref o2scl::table_units object
306  */
307  template<class vec_t>
309  // Input base table object
310  o2scl::table<vec_t> *tbase=dynamic_cast<o2scl::table_units<vec_t> *>(&t);
311  if (tbase==0) {
312  O2SCL_ERR2("Cast failed in hdf_input_data",
313  "(hdf_file &, table_units &).",o2scl::exc_efailed);
314  }
315  hdf_input_data(hf,*tbase);
316 
317  // Get unit flag
318  int uf;
319  hf.geti("unit_flag",uf);
320 
321  // If present, get units
322  if (uf>0) {
323  std::vector<std::string> units;
324  hf.gets_vec("units",units);
325  for(size_t i=0;i<units.size();i++) {
326  t.set_unit(t.get_column_name(i),units[i]);
327  }
328  }
329 
330  return;
331  }
332 
333  /// Output a \ref o2scl::hist object to a \ref hdf_file
334  void hdf_output(hdf_file &hf, o2scl::hist &h, std::string name);
335  /// Input a \ref o2scl::hist object from a \ref hdf_file
336  void hdf_input(hdf_file &hf, o2scl::hist &h, std::string name="");
337  /// Output a \ref o2scl::hist_2d object to a \ref hdf_file
338  void hdf_output(hdf_file &hf, const o2scl::hist_2d &h, std::string name);
339  /// Input a \ref o2scl::hist_2d object from a \ref hdf_file
340  void hdf_input(hdf_file &hf, o2scl::hist_2d &h, std::string name="");
341  /// Output a \ref o2scl::table3d object to a \ref hdf_file
342  void hdf_output(hdf_file &hf, const o2scl::table3d &h, std::string name);
343  /// Input a \ref o2scl::table3d object from a \ref hdf_file
344  void hdf_input(hdf_file &hf, o2scl::table3d &h, std::string name="");
345  /// Output a \ref o2scl::expval_scalar object to a \ref hdf_file
346  void hdf_output(hdf_file &hf, o2scl::expval_scalar &h,
347  std::string name);
348  /// Input a \ref o2scl::expval_scalar object from a \ref hdf_file
350  std::string name="");
351  /// Output a \ref o2scl::expval_vector object to a \ref hdf_file
352  void hdf_output(hdf_file &hf, o2scl::expval_vector &h,
353  std::string name);
354  /// Input a \ref o2scl::expval_vector object from a \ref hdf_file
355  void hdf_input(hdf_file &hf, o2scl::expval_vector &h, std::string name="");
356  /// Output a \ref o2scl::expval_matrix object to a \ref hdf_file
357  void hdf_output(hdf_file &hf, o2scl::expval_matrix &h,
358  std::string name);
359  /// Input a \ref o2scl::expval_matrix object from a \ref hdf_file
360  void hdf_input(hdf_file &hf, o2scl::expval_matrix &h, std::string name="");
361  /// Output a \ref o2scl::uniform_grid object to a \ref hdf_file
362  void hdf_output(hdf_file &hf, o2scl::uniform_grid<double> &h,
363  std::string name);
364  /// Input a \ref o2scl::uniform_grid object from a \ref hdf_file
366  std::string name="");
367  /// Output a vector of \ref o2scl::contour_line objects to a \ref hdf_file
368  void hdf_output(hdf_file &hf, const std::vector<o2scl::contour_line> &cl,
369  std::string name);
370  /// Input a vector of \ref o2scl::contour_line objects from a \ref hdf_file
371  void hdf_input(hdf_file &hf, std::vector<o2scl::contour_line> &cl,
372  std::string name="");
373  /// Output a vector of \ref o2scl::edge_crossings objects to a \ref hdf_file
374  void hdf_output(hdf_file &hf, const std::vector<o2scl::edge_crossings> &ec,
375  std::string name);
376  /// Input a vector of \ref o2scl::edge_crossings objects from a \ref hdf_file
377  void hdf_input(hdf_file &hf, std::vector<o2scl::edge_crossings> &ec,
378  std::string name="");
379  /// Output a \ref o2scl::tensor_grid object to a \ref hdf_file
380  void hdf_output(hdf_file &hf, o2scl::tensor_grid<std::vector<double>,
381  std::vector<size_t> > &t, std::string name);
382  /// Input a \ref o2scl::tensor_grid object from a \ref hdf_file
383  void hdf_input(hdf_file &hf, o2scl::tensor_grid<std::vector<double>,
384  std::vector<size_t> > &t, std::string name="");
385 
386 }
387 
388 #endif
int getd_vec(std::string name, std::vector< double > &v)
Get vector dataset and place data in v.
Tensor class with arbitrary dimensions with a grid.
Definition: tensor_grid.h:46
int setd_vec(std::string name, const std::vector< double > &v)
Set vector dataset named name with v.
Matrix expectation value.
Definition: expval.h:579
int geti(std::string name, int &i)
Get a integer named name.
int set_szt_vec(std::string name, const std::vector< size_t > &v)
Set vector dataset named name with v.
int get_szt_def(std::string name, size_t def, size_t &i)
Get a size_t named name.
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
Definition: hdf_io.h:184
void set_szt(std::string name, size_t u)
Set an unsigned integer named name to value u.
Data table table class.
Definition: table.h:49
void set_nlines(size_t il)
Set the number of lines.
Definition: table.h:461
invalid argument supplied by user
Definition: err_hnd.h:59
A two-dimensional histogram class.
Definition: hist_2d.h:103
void sets_fixed(std::string name, std::string s)
Set a fixed-length string named name to value s.
void check_synchro() const
Check if the tree and list are properly synchronized.
Definition: table.h:2551
int get_szt(std::string name, size_t &u)
Get an unsigned integer named name.
hid_t get_current_id()
Retrieve the current working id.
generic failure
Definition: err_hnd.h:61
A one-dimensional histogram class.
Definition: hist.h:113
virtual void clear_table()
Clear the table and the column names (but leave constants)
Definition: table.h:2213
int close_group(hid_t group)
Close a previously created group.
Definition: hdf_file.h:290
int find_object_by_type(std::string type, std::string &name, int verbose=0)
Look in hdf_file hf for an O<span style=&#39;position: relative; top: 0.3em; font-size: 0...
void set_unit(std::string scol, std::string unit)
Set the unit for column scol to unit.
Definition: table_units.h:405
Cubic spline for natural boundary conditions.
Definition: interp.h:73
int gets_fixed(std::string name, std::string &s)
Get a fixed-length string named name.
Vector expectation value.
Definition: expval.h:304
void copy_to_vectors(size_t &nd, size_t &dc, size_t &ms, std::vector< double > &data, std::vector< size_t > &insides)
Copy the object data to three size_t numbers and two vectors.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
The O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl namespace ...
Definition: table.h:53
bool has_write_access()
If true, then the file has read and write access.
Definition: hdf_file.h:148
void set_current_id(hid_t cur)
Set the current working id.
virtual void add_constant(std::string name, double val)
Add a constant, or if the constant already exists, change its value.
Definition: table.h:2373
Scalar expectation value.
Definition: expval.h:181
Probability distribution from an adaptive mesh created using a matrix of points.
size_t get_ncolumns() const
Return the number of columns.
Definition: table.h:443
Data table table class with units.
Definition: table_units.h:42
void new_column(std::string head)
Add a new column owned by the table table .
Definition: table.h:728
void set_from_vectors(size_t &nd, size_t &dc, size_t &ms, const std::vector< double > &data, const std::vector< size_t > &insides)
Set the object from data specified as three size_t numbers and a set of two vectors.
A data structure containing one or more slices of two-dimensional data points defined on a grid...
Definition: table3d.h:77
Store data in an O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$sc...
Definition: hdf_file.h:101
void set(std::string scol, size_t row, double val)
Set row row of column named col to value val . .
Definition: table.h:308
int gets_vec(std::string name, std::vector< std::string > &s)
Get a vector of strings named name and store it in s.
int getd_vec_copy(std::string name, vec_t &v)
Get vector dataset and place data in v.
Definition: hdf_file.h:319
size_t itype
Current interpolation type.
Definition: table.h:3000
std::string get_column_name(size_t icol) const
Returns the name of column col .
Definition: table.h:796
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
Definition: hdf_io.h:146
void clear_constants()
CLear all constants.
Definition: table.h:2240
int get_szt_vec(std::string name, std::vector< size_t > &v)
Get vector dataset and place data in v.
hid_t open_group(hid_t init_id, std::string path)
Open a group relative to the location specified in init_id.

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).