hdf_io.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, 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 
42 /** \brief The \o2 namespace for I/O with HDF
43  */
44 namespace o2scl_hdf {
45 
46  /** \brief Output a \ref o2scl::table object to a \ref hdf_file
47  */
48  void hdf_output(hdf_file &hf, o2scl::table<> &t, std::string name);
49 
50 #ifndef O2SCL_NO_HDF_INPUT
51  /** \brief Input a \ref o2scl::table object from a \ref hdf_file
52 
53  \comment
54  Note that a default value is not allowed here because this
55  is a template function
56  \endcomment
57  */
58  template<class vec_t>
59  void hdf_input(hdf_file &hf, o2scl::table<vec_t> &t, std::string name) {
60 
61  // If no name specified, find name of first group of specified type
62  if (name.length()==0) {
63  hf.find_group_by_type("table",name);
64  if (name.length()==0) {
65  O2SCL_ERR2("No object of type table found in ",
66  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
67  }
68  }
69 
70  // Open main group
71  hid_t top=hf.get_current_id();
72  hid_t group=hf.open_group(name);
73  hf.set_current_id(group);
74 
75  // Input the table data
76  hdf_input_data(hf,t);
77 
78  // Close group
79  hf.close_group(group);
80 
81  // Return location to previous value
82  hf.set_current_id(top);
83 
84  return;
85  }
86 #endif
87 
88  /** \brief Internal function for outputting a \ref o2scl::table object
89  */
90  void hdf_output_data(hdf_file &hf, o2scl::table<> &t);
91 
92  /** \brief Internal function for inputting a \ref o2scl::table object
93  */
94  template<class vec_t>
96  hid_t group=hf.get_current_id();
97 
98  // Clear previous data
99  t.clear_table();
100  t.clear_constants();
101 
102  // Check typename
103  std::string type2;
104  hf.gets_fixed("o2scl_type",type2);
105  if (type2!="table") {
106  O2SCL_ERR2("Typename in HDF group does not match ",
107  "class in o2scl_hdf::hdf_input_data().",o2scl::exc_einval);
108  }
109 
110  // Storage
111  std::vector<std::string> cnames, cols;
112  typedef boost::numeric::ublas::vector<double> ubvector;
113  ubvector cvalues;
114 
115  // Get constants
116  hf.gets_vec("con_names",cnames);
117  hf.getd_vec_copy("con_values",cvalues);
118  if (cnames.size()!=cvalues.size()) {
119  O2SCL_ERR2("Size mismatch between constant names and values ",
120  "in o2scl_hdf::hdf_input_data().",o2scl::exc_einval);
121  }
122  for(size_t i=0;i<cnames.size();i++) {
123  t.add_constant(cnames[i],cvalues[i]);
124  }
125 
126  // Get column names
127  hf.gets_vec("col_names",cols);
128  for(size_t i=0;i<cols.size();i++) {
129  t.new_column(cols[i]);
130  }
131 
132  // Get number of lines
133  int nlines2;
134  hf.geti("nlines",nlines2);
135  t.set_nlines(nlines2);
136 
137  // Output the interpolation type
138  hf.get_szt_def("itype",o2scl::itp_cspline,t.itype);
139 
140  // Open data group
141  hid_t group2=hf.open_group("data");
142  hf.set_current_id(group2);
143 
144  if (nlines2>0) {
145 
146  // Get data
147  for(size_t i=0;i<t.get_ncolumns();i++) {
148  ubvector vtmp(nlines2);
149  hf.getd_vec_copy(t.get_column_name(i),vtmp);
150  for(int j=0;j<nlines2;j++) {
151  t.set(t.get_column_name(i),j,vtmp[j]);
152  }
153  }
154 
155  }
156 
157  // Close groups
158  hf.close_group(group2);
159 
160  hf.set_current_id(group);
161 
162  // Check that input created a valid table
163  t.check_synchro();
164 
165  return;
166  }
167 
168  /** \brief Output a \ref o2scl::table_units object to a \ref hdf_file
169  */
170  void hdf_output(hdf_file &hf, o2scl::table_units<> &t,
171  std::string name);
172 
173  /** \brief Input a \ref o2scl::table_units object from a \ref hdf_file
174 
175  \comment
176  Note that a default value is not allowed here because this
177  is a template function
178  \endcomment
179  */
180  template<class vec_t>
182  std::string name) {
183 
184  // If no name specified, find name of first group of specified type
185  if (name.length()==0) {
186  hf.find_group_by_type("table",name);
187  if (name.length()==0) {
188  O2SCL_ERR2("No object of type table found in ",
189  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
190  }
191  }
192 
193  // Open main group
194  hid_t top=hf.get_current_id();
195  hid_t group=hf.open_group(name);
196  hf.set_current_id(group);
197 
198  // Input the table_units data
199  hdf_input_data(hf,t);
200 
201  // Close group
202  hf.close_group(group);
203 
204  // Return location to previous value
205  hf.set_current_id(top);
206 
207  return;
208  }
209 
210  /** \brief Internal function for outputting a \ref o2scl::table_units object
211  */
212  void hdf_output_data(hdf_file &hf, o2scl::table_units<> &t);
213 
214  /** \brief Internal function for inputting a \ref o2scl::table_units object
215  */
216  template<class vec_t>
218  // Input base table object
219  o2scl::table<vec_t> *tbase=dynamic_cast<o2scl::table_units<vec_t> *>(&t);
220  if (tbase==0) {
221  O2SCL_ERR2("Cast failed in hdf_input_data",
222  "(hdf_file &, table_units &).",o2scl::exc_efailed);
223  }
224  hdf_input_data(hf,*tbase);
225 
226  // Get unit flag
227  int uf;
228  hf.geti("unit_flag",uf);
229 
230  // If present, get units
231  if (uf>0) {
232  std::vector<std::string> units;
233  hf.gets_vec("units",units);
234  for(size_t i=0;i<units.size();i++) {
235  t.set_unit(t.get_column_name(i),units[i]);
236  }
237  }
238 
239  return;
240  }
241 
242  /// Output a \ref o2scl::hist object to a \ref hdf_file
243  void hdf_output(hdf_file &hf, o2scl::hist &h, std::string name);
244  /// Input a \ref o2scl::hist object from a \ref hdf_file
245  void hdf_input(hdf_file &hf, o2scl::hist &h, std::string name="");
246  /// Output a \ref o2scl::hist_2d object to a \ref hdf_file
247  void hdf_output(hdf_file &hf, o2scl::hist_2d &h, std::string name);
248  /// Input a \ref o2scl::hist_2d object from a \ref hdf_file
249  void hdf_input(hdf_file &hf, o2scl::hist_2d &h, std::string name="");
250  /// Output a \ref o2scl::table3d object to a \ref hdf_file
251  void hdf_output(hdf_file &hf, o2scl::table3d &h, std::string name);
252  /// Input a \ref o2scl::table3d object from a \ref hdf_file
253  void hdf_input(hdf_file &hf, o2scl::table3d &h, std::string name="");
254  /// Output a \ref o2scl::expval_scalar object to a \ref hdf_file
255  void hdf_output(hdf_file &hf, o2scl::expval_scalar &h, std::string name);
256  /// Input a \ref o2scl::expval_scalar object from a \ref hdf_file
257  void hdf_input(hdf_file &hf, o2scl::expval_scalar &h, std::string name="");
258  /// Output a \ref o2scl::expval_vector object to a \ref hdf_file
259  void hdf_output(hdf_file &hf, o2scl::expval_vector &h, std::string name);
260  /// Input a \ref o2scl::expval_vector object from a \ref hdf_file
261  void hdf_input(hdf_file &hf, o2scl::expval_vector &h, std::string name="");
262  /// Output a \ref o2scl::expval_matrix object to a \ref hdf_file
263  void hdf_output(hdf_file &hf, o2scl::expval_matrix &h, std::string name);
264  /// Input a \ref o2scl::expval_matrix object from a \ref hdf_file
265  void hdf_input(hdf_file &hf, o2scl::expval_matrix &h, std::string name="");
266  /// Output a \ref o2scl::uniform_grid object to a \ref hdf_file
267  void hdf_output(hdf_file &hf, o2scl::uniform_grid<double> &h,
268  std::string name);
269  /// Input a \ref o2scl::uniform_grid object from a \ref hdf_file
271  std::string name="");
272  /// Output a vector of \ref o2scl::contour_line objects to a \ref hdf_file
273  void hdf_output(hdf_file &hf, std::vector<o2scl::contour_line> &cl,
274  std::string name);
275  /// Input a vector of \ref o2scl::contour_line objects from a \ref hdf_file
276  void hdf_input(hdf_file &hf, std::vector<o2scl::contour_line> &cl,
277  std::string name="");
278  /// Output a vector of \ref o2scl::edge_crossings objects to a \ref hdf_file
279  void hdf_output(hdf_file &hf, std::vector<o2scl::edge_crossings> &ec,
280  std::string name);
281  /// Input a vector of \ref o2scl::edge_crossings objects from a \ref hdf_file
282  void hdf_input(hdf_file &hf, std::vector<o2scl::edge_crossings> &ec,
283  std::string name="");
284  /// Output a \ref o2scl::tensor_grid object to a \ref hdf_file
285  void hdf_output(hdf_file &hf, o2scl::tensor_grid<std::vector<double>,
286  std::vector<size_t> > &t, std::string name);
287  /// Input a \ref o2scl::tensor_grid object from a \ref hdf_file
288  void hdf_input(hdf_file &hf, o2scl::tensor_grid<std::vector<double>,
289  std::vector<size_t> > &t, std::string name="");
290 
291 }
292 
293 #endif
void clear_table()
Clear the table and the column names (but leave constants)
Definition: table.h:1991
Tensor class with arbitrary dimensions with a grid.
Definition: tensor_grid.h:46
Matrix expectation value.
Definition: expval.h:579
int geti(std::string name, int &i)
Get a integer named name.
int get_szt_def(std::string name, size_t def, size_t &i)
Get a size_t named name.
int find_group_by_type(std::string type, std::string &group_name, int verbose=0)
Look in hdf_file hf for an O<span style=&#39;position: relative; top: 0.3em; font-size: 0...
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
Definition: hdf_io.h:95
Data table table class.
Definition: table.h:49
void set_nlines(size_t il)
Set the number of lines.
Definition: table.h:467
invalid argument supplied by user
Definition: err_hnd.h:59
A two-dimensional histogram class.
Definition: hist_2d.h:103
void check_synchro() const
Return 0 if the tree and list are properly synchronized.
Definition: table.h:2322
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
int close_group(hid_t group)
Close a previously created group.
Definition: hdf_file.h:278
void set_unit(std::string scol, std::string unit)
Set the unit for column scol to unit.
Definition: table_units.h:390
Cubic spline for natural boundary conditions.
Definition: interp.h:72
int gets_fixed(std::string name, std::string &s)
Get a fixed-length string named name.
Vector expectation value.
Definition: expval.h:304
#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
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:2151
Scalar expectation value.
Definition: expval.h:181
size_t get_ncolumns() const
Return the number of columns.
Definition: table.h:449
Data table table class with units.
Definition: table_units.h:37
void new_column(std::string head)
Add a new column owned by the table table .
Definition: table.h:690
A class representing a uniform linear or logarithmic grid.
Definition: uniform_grid.h:38
A data structure containing many slices of two-dimensional data points defined on a grid...
Definition: table3d.h:78
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:96
void set(std::string scol, size_t row, double val)
Set row row of column named col to value val . .
Definition: table.h:327
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:307
size_t itype
Current interpolation type.
Definition: table.h:2720
std::string get_column_name(size_t icol) const
Returns the name of column col .
Definition: table.h:758
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:59
void clear_constants()
CLear all constants.
Definition: table.h:2018
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).