Xmipp  v3.23.11-Nereus
xmipp_hdf5.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: joaquin Oton (joton@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "xmipp_hdf5.h"
27 #include "xmipp_strings.h"
28 #include "xmipp_error.h"
29 
30 struct H5TreeInfo
31 {
32  std::string rootname;
33  std::ostream * out;
34 };
35 
36 herr_t showObjectInfo(hid_t objId, const char *name, void *op_data)
37 {
38  H5TreeInfo &h5Info = *((H5TreeInfo*)op_data);
39  std::ostream &out = *(h5Info.out);
40 
41  hsize_t nobj;
42  herr_t err=0;
43  hid_t grpid, dsid;
44 
45  H5G_stat_t statbuf;
46 
47  // Print the object name
48  out << formatString("%s%s - ", h5Info.rootname.c_str(), name);
49 
50  H5Gget_objinfo(objId, name, 0, &statbuf);
51  /*
52  * process each object according to its type
53  */
54  switch(statbuf.type)
55  {
56  case H5G_LINK:
57  out << " SYM_LINK:\n";
58  // do_link(gid,memb_name);
59  break;
60  case H5G_GROUP:
61  {
62  String rootname = h5Info.rootname;
63  h5Info.rootname += (String)name + "/";
64 
65  grpid = H5Gopen(objId,name, H5P_DEFAULT);
66  err = H5Gget_num_objs(grpid, &nobj);
67  out << formatString("Group {%d elements}\n", nobj);
68 
69  H5Giterate(objId, name, NULL, showObjectInfo, &h5Info);
70  h5Info.rootname = rootname;
71  break;
72  }
73  case H5G_DATASET:
74  out << "Dataset {";
75  dsid = H5Dopen(objId, name, H5P_DEFAULT);
76  hsize_t dims[4];
77  hid_t filespace;
78  int rank;
79  filespace = H5Dget_space(dsid); /* Get filespace handle first. */
80  rank = H5Sget_simple_extent_dims(filespace, dims, NULL);
81 
82  for (int k = 0; k < rank-1; ++k)
83  out << dims[k] << ", ";
84 
85  out << dims[rank-1] << "}\n";
86  H5Dclose(dsid);
87  break;
88  case H5G_TYPE:
89  out << " Data type:\n";
90  // idType = H5Topen(objId,memb_name, H5P_DEFAULT);
91  // H5Tclose(idType);
92  break;
93  default:
94  out << " unknown?\n";
95  break;
96  }
97  return err;
98 }
99 
100 
101 
102 std::map<String, H5infoProvider > createProviderMap()
103 {
104  std::map<String, H5infoProvider > m;
105  m["NXtomo"] = std::make_pair(MISTRAL, "/NXtomo/instrument/sample/data");
106  m["TomoNormalized"] = std::make_pair(MISTRAL, "/TomoNormalized/TomoNormalized");
107  m["MDF"] = std::make_pair(EMAN, "/MDF/images/%i/image");
108  return m;
109 }
110 
111 
112 void XmippH5File::openFile(const H5std_string& name, unsigned int flags,
113  const H5::FileAccPropList& access_plist)
114 {
115  if ( isHdf5(name.c_str()) )
116  H5::H5File::openFile(name.c_str(), flags, access_plist);
117  else
118  REPORT_ERROR(ERR_IMG_UNKNOWN, formatString("XmippH5File: Format of %s is not HDF5.",name.c_str()));
119 }
120 
121 bool XmippH5File::checkDataset(const char* dsname) const
122 {
123  H5::DataSet dataset;
124  // Open the dataset
125  try
126  {
127  dataset = openDataSet(dsname);
128  }
129  catch (H5::Exception &h5e)
130  {
131  return false;
132  }
133  dataset.close();
134  return true;
135 }
136 
137 int XmippH5File::getDataset(const char* dsname, Matrix1D<double> &data, bool reportError) const
138 {
139  H5::DataSet dataset;
140  // Open the dataset
141  try
142  {
143  dataset = openDataSet(dsname);
144  }
145  catch (H5::Exception &h5e)
146  {
147  if ( reportError )
148  REPORT_ERROR(ERR_ARG_MISSING,formatString("getDataset: %s dataset " \
149  "does not exist in file %s.", dsname, this->getFileName().c_str()));
150  // std::cerr << "getDataset Error: " << h5e.getCDetailMsg() << std::endl;
151  return -1;
152  }
153 
154  //Get dataspace of the dataset.
155  H5::DataSpace filespace = dataset.getSpace();
156 
157  // Check the number of dimensions in the dataspace is one.
158  if (filespace.getSimpleExtentNdims()!= 1 )
159  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("getDataset: Dataset %s has "\
160  "more than 1 dimension", dsname));
161 
162  // Get the size of the dimension in the dataspace
163  hsize_t dim[1];
164  filespace.getSimpleExtentDims(dim);
165 
166 
167  hsize_t offset[1]; // Hyperslab offset in the file
168  // hsize_t count[1]; // Size of the hyperslab in the file
169 
170  // Define the offset and count of the hyperslab to be read.
171  offset[0] = 0;
172  // count[0] = dim[0];
173 
174  filespace.selectHyperslab( H5S_SELECT_SET, dim, offset );
175 
176  // Allocate space for data and define the memspace
177  data.resizeNoCopy((int)*dim);
178 
179  H5::DataSpace memspace(1, dim);
180 
181  // Read data from hyperslab in the file into the hyperslab in memory
182  dataset.read(MATRIX1D_ARRAY(data), H5::PredType::NATIVE_DOUBLE, memspace, filespace);
183 
184  filespace.close();
185  memspace.close();
186  dataset.close();
187 
188  return 0;
189 }
190 
191 void XmippH5File::showTree(std::ostream &out)
192 {
193  H5TreeInfo h5Info;
194 
195  h5Info.out = &out;
196  h5Info.rootname = "";
197 
198  this->iterateElems("/", NULL, showObjectInfo, &h5Info);
199 }
200 
201 
203 {
204  H5infoProvider provider;
205 
206  size_t maxSize = 1024;
207  char groupName[1024];
208  char memName[1024];
209 
210  hid_t gid;
211  ssize_t len;
212 
213  gid = H5Gopen(fhdf5,"/", H5P_DEFAULT);
214 
215  len = H5Iget_name(gid, groupName, maxSize);
216 
217  if (len == 0)
218  REPORT_ERROR(ERR_VALUE_EMPTY, "rwHDF5: Empty structure in file.");
219 
220  len = H5Gget_objname_by_idx(gid, 0, memName, maxSize);
221 
222  H5Gclose(gid);
223 
224  typedef std::map<String, H5infoProvider >::const_iterator it_type;
225 
226  for ( it_type it = H5ProviderMap.begin(); it != H5ProviderMap.end(); it++)
227  {
228  if ( strcmp(memName,it->first.c_str() ) == 0 )
229  return it->second;
230  }
231 
232  return std::make_pair(NONE , "");
233 
234 // REPORT_ERROR(ERR_IO, "rwHDF5: Unknown file provider. Default dataset unknown.");
235 
236 }
Argument missing.
Definition: xmipp_error.h:114
std::string rootname
Definition: xmipp_hdf5.cpp:32
Empty value.
Definition: xmipp_error.h:194
void showTree(std::ostream &out=std::cout)
Definition: xmipp_hdf5.cpp:191
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void openFile(const H5std_string &name, unsigned int flags, const H5::FileAccPropList &access_plist=H5::FileAccPropList::DEFAULT)
Definition: xmipp_hdf5.cpp:112
Unknown image type.
Definition: xmipp_error.h:130
std::pair< H5FileProvider, String > H5infoProvider
Definition: xmipp_hdf5.h:51
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
bool checkDataset(const char *dsname) const
Definition: xmipp_hdf5.cpp:121
Incorrect argument received.
Definition: xmipp_error.h:113
std::ostream * out
Definition: xmipp_hdf5.cpp:33
H5infoProvider getProvider(hid_t fhdf5)
Definition: xmipp_hdf5.cpp:202
const std::map< String, H5infoProvider > H5ProviderMap
Definition: xmipp_hdf5.h:56
int m
#define len
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
herr_t showObjectInfo(hid_t objId, const char *name, void *op_data)
Definition: xmipp_hdf5.cpp:36
std::string String
Definition: xmipp_strings.h:34
String formatString(const char *format,...)
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
std::map< String, H5infoProvider > createProviderMap()
Definition: xmipp_hdf5.cpp:102
int getDataset(const char *dsname, Matrix1D< double > &data, bool reportError=true) const
Definition: xmipp_hdf5.cpp:137