ASCOT5
Loading...
Searching...
No Matches
hdf5_helpers.c
Go to the documentation of this file.
1
5#include <stdio.h>
6#include <unistd.h>
7#include <string.h>
8#include <stdlib.h>
9#include <math.h>
10#include <hdf5.h>
11#include <hdf5_hl.h>
12#include "../ascot5.h"
13#include "../print.h"
14
18void hdf5_init(void) {
19 H5Eset_auto1(NULL, NULL);
20}
21
26hid_t hdf5_create(const char* filename) {
27 hid_t file;
28 file = H5Fcreate(filename, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
29 return file;
30}
31
36hid_t hdf5_open(const char *filename) {
37 hid_t file;
38 file = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT);
39 return file;
40}
41
46hid_t hdf5_open_ro(const char *filename) {
47 hid_t file;
48 file = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
49 return file;
50}
51
56herr_t hdf5_close(hid_t file_id) {
57 herr_t err;
58 err = H5Fclose(file_id);
59 return err;
60}
61
66hid_t hdf5_create_group(hid_t loc, const char* path) {
67 const char* start;
68 if(path[0] == '/') {
69 start = path + 1;
70 } else {
71 start = path;
72 }
73
74 const char* end = strstr(start, "/");
75
76 if(end == NULL) {
77 hid_t g = H5Gcreate2(loc, start, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
78 return g;
79 } else {
80 char* group = (char*) malloc((strlen(path) + 1) * sizeof(char));
81 strncpy(group, start, end-start);
82 group[end-start] = '\0';
83
84 /* Open or create parent group */
85 hid_t g = H5Gopen2(loc, group, H5P_DEFAULT);
86 if(g < 0) {
87 g = H5Gcreate2(loc, group, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
88 }
89 free(group);
90 hid_t g0 = hdf5_create_group(g, end);
91 H5Gclose(g);
92 return g0;
93 }
94}
95
100herr_t hdf5_find_group(hid_t loc, const char* path) {
101 return H5Gget_objinfo (loc, path, 0, NULL);
102}
103
112char* hdf5_generate_qid_path(const char* original, char* qid, char* path) {
113 strcpy(path, original);
114 char* ptr = strstr(path,"XXXXXXXXXX");
115
116 for(int i = 0; i < 10; i++) {
117 ptr[i] = qid[i];
118 }
119
120 return path;
121}
122
131char* hdf5_gen_path(const char* original, char* qid, char* path) {
132 strcpy(path, original);
133 char* ptr = strstr(path,"XXXXXXXXXX");
134
135 for(int i = 0; i < 10; i++) {
136 ptr[i] = qid[i];
137 }
138
139 return path;
140}
141
166int hdf5_read_double(const char* var, real* ptr, hid_t file, char* qid,
167 const char* errfile, int errline) {
168 char temp[256];
169 if( H5LTread_dataset_double(file, hdf5_gen_path(var, qid, temp), ptr) < 0 ){
170 print_err("Error: could not read HDF5 dataset %s FILE %s LINE %d\n",
171 temp, errfile, errline);
172 return 1;
173 }
174 return 0;
175}
176
201int hdf5_read_int(const char* var, int* ptr, hid_t file, char* qid,
202 const char* errfile, int errline) {
203 char temp[256];
204 if( H5LTread_dataset_int(file, hdf5_gen_path(var, qid, temp), ptr) < 0 ){
205 print_err("Error: could not read HDF5 dataset %s FILE %s LINE %d\n",
206 temp, errfile, errline);
207 return 1;
208 }
209 return 0;
210}
211
236int hdf5_read_long(const char* var, long* ptr, hid_t file, char* qid,
237 const char* errfile, int errline) {
238 char temp[256];
239 if( H5LTread_dataset_long(file, hdf5_gen_path(var, qid, temp), ptr) < 0 ){
240 print_err("Error: could not read HDF5 dataset %s FILE %s LINE %d\n",
241 temp, errfile, errline);
242 return 1;
243 }
244 return 0;
245}
246
254herr_t hdf5_write_string_attribute(hid_t loc, const char* path,
255 const char* attrname, const char* string) {
256 herr_t err;
257
258 hid_t grp = H5Gopen(loc, path, H5P_DEFAULT);
259 hid_t aid = H5Screate(H5S_SCALAR);
260 hid_t atype = H5Tcopy(H5T_C_S1);
261 H5Tset_size(atype, strlen(string));
262 H5Tset_strpad(atype,H5T_STR_NULLPAD);
263
264 hid_t attr = H5Aopen(grp, attrname, H5P_DEFAULT);
265
266 /* Delete existing attribute with a same name */
267 if(attr > 0) {
268 H5Adelete(grp, attrname);
269 H5Aclose(attr);
270 }
271
272 attr = H5Acreate2(grp, attrname, atype, aid, H5P_DEFAULT, H5P_DEFAULT);
273
274 err = H5Awrite(attr, atype, string);
275 if(err) {return err;}
276
277 err = H5Sclose(aid);
278 if(err) {return err;}
279
280 err = H5Tclose(atype);
281 if(err) {return err;}
282
283 err = H5Aclose(attr);
284 if(err) {return err;}
285
286 err = H5Gclose(grp);
287 if(err) {return err;}
288
289 return 0;
290}
291
296 const char* datasetname,
297 int length, double* data) {
298 /* Create the data space with unlimited dimensions. */
299 hsize_t dim[1] = {length};
300 hsize_t maxdim[1] = {H5S_UNLIMITED};
301 hid_t dataspace = H5Screate_simple(1, dim, maxdim);
302
303 /* Modify dataset creation properties, i.e. enable chunking */
304 hsize_t chunk_dim[1] = {(int)ceil(length/2.0)};
305 hid_t prop = H5Pcreate(H5P_DATASET_CREATE);
306 H5Pset_chunk (prop, 1, chunk_dim);
307
308 /* Create a new dataset within the file using chunk creation properties. */
309 hid_t dataset = H5Dcreate2(group, datasetname, H5T_IEEE_F64LE, dataspace,
310 H5P_DEFAULT, prop, H5P_DEFAULT);
311
312 /* Write data to dataset */
313 int err = 0;
314 if( H5Dwrite(dataset, H5T_IEEE_F64LE, H5S_ALL, H5S_ALL,
315 H5P_DEFAULT, data) < 0) {
316 err = -1;
317 }
318 H5Dclose(dataset);
319 H5Pclose(prop);
320 H5Sclose(dataspace);
321
322 return err;
323}
324
325
330 const char* datasetname,
331 int length, long* data) {
332 /* Create the data space with unlimited dimensions. */
333 hsize_t dim[1] = {length};
334 hsize_t maxdim[1] = {H5S_UNLIMITED};
335 hid_t dataspace = H5Screate_simple(1, dim, maxdim);
336
337 /* Modify dataset creation properties, i.e. enable chunking */
338 hsize_t chunk_dim[1] = {(int)ceil(length/2.0)};
339 hid_t prop = H5Pcreate(H5P_DATASET_CREATE);
340 H5Pset_chunk (prop, 1, chunk_dim);
341
342 /* Create a new dataset within the file using chunk creation properties. */
343 hid_t dataset = H5Dcreate2(group, datasetname, H5T_STD_I64LE, dataspace,
344 H5P_DEFAULT, prop, H5P_DEFAULT);
345
346 /* Write data to dataset */
347 int err = 0;
348 if( H5Dwrite(dataset, H5T_STD_I64LE, H5S_ALL, H5S_ALL,
349 H5P_DEFAULT, data) < 0 ) {
350 err = -1;
351 }
352 H5Dclose(dataset);
353 H5Pclose(prop);
354 H5Sclose(dataspace);
355
356 return err;
357}
358
363 const char* datasetname,
364 int length, int* data) {
365 /* Create the data space with unlimited dimensions. */
366 hsize_t dim[1] = {length};
367 hsize_t maxdim[1] = {H5S_UNLIMITED};
368 hid_t dataspace = H5Screate_simple(1, dim, maxdim);
369
370 /* Modify dataset creation properties, i.e. enable chunking */
371 hsize_t chunk_dim[1] = {(int)ceil(length/2.0)};
372 hid_t prop = H5Pcreate(H5P_DATASET_CREATE);
373 H5Pset_chunk (prop, 1, chunk_dim);
374
375 /* Create a new dataset within the file using chunk creation properties. */
376 hid_t dataset = H5Dcreate2(group, datasetname, H5T_STD_I32LE, dataspace,
377 H5P_DEFAULT, prop, H5P_DEFAULT);
378
379 /* Write data to dataset */
380 int err = 0;
381 if( H5Dwrite(dataset, H5T_STD_I32LE, H5S_ALL, H5S_ALL,
382 H5P_DEFAULT, data) < 0 ) {
383 err = -1;
384 }
385 H5Dclose(dataset);
386 H5Pclose(prop);
387 H5Sclose(dataspace);
388
389 return err;
390}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
hid_t hdf5_create_group(hid_t loc, const char *path)
Create a group (with parent groups if necessary). Returns a handle to the group. Negative on failure.
herr_t hdf5_write_string_attribute(hid_t loc, const char *path, const char *attrname, const char *string)
Write string attribute with null-padding.
herr_t hdf5_find_group(hid_t loc, const char *path)
Checks if given group exists within given hdf5 file. Negative value is returned if the group doesn't ...
herr_t hdf5_write_extendible_dataset_long(hid_t group, const char *datasetname, int length, long *data)
Create and write to an extendible dataset for long data.
herr_t hdf5_write_extendible_dataset_int(hid_t group, const char *datasetname, int length, int *data)
Create and write to an extendible dataset int data.
int hdf5_read_double(const char *var, real *ptr, hid_t file, char *qid, const char *errfile, int errline)
Read double-valued data from ASCOT5 HDF5 file.
int hdf5_read_long(const char *var, long *ptr, hid_t file, char *qid, const char *errfile, int errline)
Read long-valued data from ASCOT5 HDF5 file.
hid_t hdf5_create(const char *filename)
Create an hdf5 file, fail if file exists. A negative value is returned on failure.
herr_t hdf5_close(hid_t file_id)
Close access to given hdf5 file identifier. A negative value is returned on failure.
char * hdf5_gen_path(const char *original, char *qid, char *path)
Generate a valid path from a given template and qid.
void hdf5_init(void)
Initialize hdf5, right now just disables automatic error messages.
hid_t hdf5_open_ro(const char *filename)
Open a hdf5 file for read only. A negative value is returned on failure.
herr_t hdf5_write_extendible_dataset_double(hid_t group, const char *datasetname, int length, double *data)
Create and write to an extendible dataset for double data.
hid_t hdf5_open(const char *filename)
Open a hdf5 file for reading and writing. A negative value is returned on failure.
int hdf5_read_int(const char *var, int *ptr, hid_t file, char *qid, const char *errfile, int errline)
Read int-valued data from ASCOT5 HDF5 file.
char * hdf5_generate_qid_path(const char *original, char *qid, char *path)
Generate a valid path from a given template and qid.
Header file for math.c.
Macros for printing console output.
#define print_err(...)
Print to standard error.
Definition print.h:42