All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hdf_io.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, 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  /** \brief Input a \ref o2scl::table object from a \ref hdf_file
51 
52  \todo Removed default value for \c name for compiling at nersc
53  */
54  template<class vec_t>
55  void hdf_input(hdf_file &hf, o2scl::table<vec_t> &t, std::string name) {
56 
57  // If no name specified, find name of first group of specified type
58  if (name.length()==0) {
59  hf.find_group_by_type(hf,"table",name);
60  if (name.length()==0) {
61  O2SCL_ERR2("No object of type table found in ",
62  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
63  }
64  }
65 
66  // Open main group
67  hid_t top=hf.get_current_id();
68  hid_t group=hf.open_group(name);
69  hf.set_current_id(group);
70 
71  // Input the table data
72  hdf_input_data(hf,t);
73 
74  // Close group
75  hf.close_group(group);
76 
77  // Return location to previous value
78  hf.set_current_id(top);
79 
80  return;
81  }
82 
83  /** \brief Internal function for outputting a \ref o2scl::table object
84  */
85  void hdf_output_data(hdf_file &hf, o2scl::table<> &t);
86 
87  /** \brief Internal function for inputting a \ref o2scl::table object
88  */
89  template<class vec_t>
91  hid_t group=hf.get_current_id();
92 
93  // Clear previous data
94  t.clear_table();
95  t.clear_constants();
96 
97  // Check typename
98  std::string type2;
99  hf.gets_fixed("o2scl_type",type2);
100  if (type2!="table") {
101  O2SCL_ERR2("Typename in HDF group does not match ",
102  "class in o2scl_hdf::hdf_input().",o2scl::exc_einval);
103  }
104 
105  // Storage
106  std::vector<std::string> cnames, cols;
107  typedef boost::numeric::ublas::vector<double> ubvector;
108  ubvector cvalues;
109 
110  // Get constants
111  hf.gets_vec("con_names",cnames);
112  hf.getd_vec_copy("con_values",cvalues);
113  if (cnames.size()!=cvalues.size()) {
114  O2SCL_ERR2("Size mismatch between constant names and values ",
115  "in o2scl_hdf::hdf_input().",o2scl::exc_einval);
116  }
117  for(size_t i=0;i<cnames.size();i++) {
118  t.add_constant(cnames[i],cvalues[i]);
119  }
120 
121  // Get column names
122  hf.gets_vec("col_names",cols);
123  for(size_t i=0;i<cols.size();i++) {
124  t.new_column(cols[i]);
125  }
126 
127  // Get number of lines
128  int nlines2;
129  hf.geti("nlines",nlines2);
130  t.set_nlines(nlines2);
131 
132  // Output the interpolation type
133  hf.get_szt_def("itype",o2scl::itp_cspline,t.itype);
134 
135  // Open data group
136  hid_t group2=hf.open_group("data");
137  hf.set_current_id(group2);
138 
139  if (nlines2>0) {
140 
141  // Get data
142  for(size_t i=0;i<t.get_ncolumns();i++) {
143  ubvector vtmp(nlines2);
144  hf.getd_vec_copy(t.get_column_name(i),vtmp);
145  for(int j=0;j<nlines2;j++) {
146  t.set(t.get_column_name(i),j,vtmp[j]);
147  }
148  }
149 
150  }
151 
152  // Close groups
153  hf.close_group(group2);
154 
155  hf.set_current_id(group);
156 
157  // Check that input created a valid table
158  t.check_synchro();
159 
160  return;
161  }
162 
163  /** \brief Output a \ref o2scl::table_units object to a \ref hdf_file
164  */
165  void hdf_output(hdf_file &hf, o2scl::table_units<> &t,
166  std::string name);
167 
168  /** \brief Input a \ref o2scl::table_units object from a \ref hdf_file
169 
170  \todo Removed default value for \c name for compiling at nersc
171  */
172  template<class vec_t>
174  std::string name) {
175 
176  // If no name specified, find name of first group of specified type
177  if (name.length()==0) {
178  hf.find_group_by_type(hf,"table",name);
179  if (name.length()==0) {
180  O2SCL_ERR2("No object of type table found in ",
181  "o2scl_hdf::hdf_input().",o2scl::exc_efailed);
182  }
183  }
184 
185  // Open main group
186  hid_t top=hf.get_current_id();
187  hid_t group=hf.open_group(name);
188  hf.set_current_id(group);
189 
190  // Input the table_units data
191  hdf_input_data(hf,t);
192 
193  // Close group
194  hf.close_group(group);
195 
196  // Return location to previous value
197  hf.set_current_id(top);
198 
199  return;
200  }
201 
202  /** \brief Internal function for outputting a \ref o2scl::table_units object
203  */
204  void hdf_output_data(hdf_file &hf, o2scl::table_units<> &t);
205 
206  /** \brief Internal function for inputting a \ref o2scl::table_units object
207  */
208  template<class vec_t>
210  // Input base table object
211  o2scl::table<vec_t> *tbase=dynamic_cast<o2scl::table_units<vec_t> *>(&t);
212  if (tbase==0) {
213  O2SCL_ERR2("Cast failed in hdf_output_data",
214  "(hdf_file &, table_units &).",o2scl::exc_efailed);
215  }
216  hdf_input_data(hf,*tbase);
217 
218  // Get unit flag
219  int uf;
220  hf.geti("unit_flag",uf);
221 
222  // If present, get units
223  if (uf>0) {
224  std::vector<std::string> units;
225  hf.gets_vec("units",units);
226  for(size_t i=0;i<units.size();i++) {
227  t.set_unit(t.get_column_name(i),units[i]);
228  }
229  }
230 
231  return;
232  }
233 
234  /// Output a \ref o2scl::hist object to a \ref hdf_file
235  void hdf_output(hdf_file &hf, o2scl::hist &h, std::string name);
236  /// Input a \ref o2scl::hist object from a \ref hdf_file
237  void hdf_input(hdf_file &hf, o2scl::hist &h, std::string name="");
238  /// Output a \ref o2scl::hist_2d object to a \ref hdf_file
239  void hdf_output(hdf_file &hf, o2scl::hist_2d &h, std::string name);
240  /// Input a \ref o2scl::hist_2d object from a \ref hdf_file
241  void hdf_input(hdf_file &hf, o2scl::hist_2d &h, std::string name="");
242  /// Output a \ref o2scl::table3d object to a \ref hdf_file
243  void hdf_output(hdf_file &hf, o2scl::table3d &h, std::string name);
244  /// Input a \ref o2scl::table3d object from a \ref hdf_file
245  void hdf_input(hdf_file &hf, o2scl::table3d &h, std::string name="");
246  /// Output a \ref o2scl::tensor_grid object to a \ref hdf_file
247  void hdf_output(hdf_file &hf, o2scl::tensor_grid &h, std::string name);
248  /// Input a \ref o2scl::tensor_grid object from a \ref hdf_file
249  void hdf_input(hdf_file &hf, o2scl::tensor_grid &h, std::string name="");
250  /// Output a \ref o2scl::expval_scalar object to a \ref hdf_file
251  void hdf_output(hdf_file &hf, o2scl::expval_scalar &h, std::string name);
252  /// Input a \ref o2scl::expval_scalar object from a \ref hdf_file
253  void hdf_input(hdf_file &hf, o2scl::expval_scalar &h, std::string name="");
254  /// Output a \ref o2scl::expval_vector object to a \ref hdf_file
255  void hdf_output(hdf_file &hf, o2scl::expval_vector &h, std::string name);
256  /// Input a \ref o2scl::expval_vector object from a \ref hdf_file
257  void hdf_input(hdf_file &hf, o2scl::expval_vector &h, std::string name="");
258  /// Output a \ref o2scl::expval_matrix object to a \ref hdf_file
259  void hdf_output(hdf_file &hf, o2scl::expval_matrix &h, std::string name);
260  /// Input a \ref o2scl::expval_matrix object from a \ref hdf_file
261  void hdf_input(hdf_file &hf, o2scl::expval_matrix &h, std::string name="");
262  /// Output a \ref o2scl::uniform_grid object to a \ref hdf_file
263  void hdf_output(hdf_file &hf, o2scl::uniform_grid<double> &h,
264  std::string name);
265  /// Input a \ref o2scl::uniform_grid object from a \ref hdf_file
266  void hdf_input(hdf_file &hf, o2scl::uniform_grid<double> &h,
267  std::string name="");
268  /// Output a vector of \ref o2scl::contour_line objects to a \ref hdf_file
269  void hdf_output(hdf_file &hf, std::vector<o2scl::contour_line> &cl,
270  std::string name);
271  /// Input a vector of \ref o2scl::contour_line objects from a \ref hdf_file
272  void hdf_input(hdf_file &hf, std::vector<o2scl::contour_line> &cl,
273  std::string name="");
274  /// Output a vector of \ref o2scl::edge_crossings objects to a \ref hdf_file
275  void hdf_output(hdf_file &hf, std::vector<o2scl::edge_crossings> &ec,
276  std::string name);
277  /// Input a vector of \ref o2scl::edge_crossings objects from a \ref hdf_file
278  void hdf_input(hdf_file &hf, std::vector<o2scl::edge_crossings> &ec,
279  std::string name="");
280 
281 }
282 
283 #endif
void clear_table()
Clear the table and the column names (but leave constants)
Definition: table.h:1867
Tensor class with arbitrary dimensions with a grid.
Definition: tensor_grid.h:95
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.
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
Definition: hdf_io.h:90
Data table table class.
Definition: table.h:47
void set_nlines(size_t il)
Set the number of lines.
Definition: table.h:442
invalid argument supplied by user
Definition: err_hnd.h:59
A two-dimensional histogram class.
Definition: hist_2d.h:103
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
void check_synchro() const
Return 0 if the tree and list are properly synchronized.
Definition: table.h:2181
int close_group(hid_t group)
Close a previously created group.
Definition: hdf_file.h:266
std::string get_column_name(size_t icol) const
Returns the name of column col .
Definition: table.h:715
void set_unit(std::string scol, std::string unit)
Set the unit for column scol to unit.
Definition: table_units.h:331
Cubic spline for natural boundary conditions.
Definition: interp.h:50
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
void set_current_id(hid_t cur)
Set the current working id.
int find_group_by_type(hdf_file &hf, std::string type, std::string &group_name, int verbose=0)
Look in hdf_file hf for an O<span style='position: relative; top: 0.3em; font-size: 0...
virtual void add_constant(std::string name, double val)
Add a constant, or if the constant already exists, change its value.
Definition: table.h:2030
Scalar expectation value.
Definition: expval.h:181
size_t get_ncolumns() const
Return the number of columns.
Definition: table.h:424
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:657
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:76
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl...
Definition: hdf_file.h:93
void set(std::string scol, size_t row, double val)
Set row row of column named col to value val . .
Definition: table.h:317
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:295
size_t itype
Current interpolation type.
Definition: table.h:2781
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:55
void clear_constants()
CLear all constants.
Definition: table.h:1895
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).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..