[haskell] use the right version of the hkl library >= 5.0.0.3381-1~
[hkl.git] / binoculars / hkl-binoculars-hdf5.c
blob8ddd6c450e8cad4a1a535569b0f962b5faff47c5
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2023 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
22 #include <hdf5.h>
24 #include "hkl/ccan/array_size/array_size.h"
25 #include "hkl/hkl-macros-private.h"
26 #include "hkl-binoculars-private.h"
28 static hid_t create_dataspace_from_axes(const darray_axis *axes)
30 HklBinocularsAxis *axis;
31 hid_t hid;
32 darray(hsize_t) dims;
34 darray_init(dims);
35 darray_foreach(axis, *axes){
36 if(axis_size(axis) > 1)
37 darray_append(dims, axis_size(axis));
40 hid = H5Screate_simple(darray_size(dims), &darray_item(dims, 0), NULL);
41 darray_free(dims);
43 return hid;
46 static herr_t save_string(hid_t group_id, const char *name, const char* config)
48 hid_t dataset_id;
49 hid_t dataspace_id;
50 herr_t status;
51 hsize_t dims[] = {1};
53 hid_t filetype = H5Tcopy (H5T_C_S1);
54 status = H5Tset_size (filetype, strlen(config));
55 hid_t memtype = H5Tcopy (H5T_C_S1);
56 status = H5Tset_size (memtype, strlen(config));
59 dataspace_id = H5Screate_simple(1, dims, NULL);
60 dataset_id = H5Dcreate (group_id, "config",
61 filetype, dataspace_id,
62 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
63 status = H5Dwrite (dataset_id, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, config);
65 status = H5Dclose(dataset_id);
66 status = H5Sclose(dataspace_id);
67 status = H5Tclose(memtype);
68 status = H5Tclose(filetype);
70 return status;
73 void hkl_binoculars_cube_save_hdf5(const char *fn,
74 const char *config,
75 const HklBinocularsCube *self)
77 double axis_idx = 0;
78 hid_t file_id;
79 hid_t groupe_id;
80 hid_t groupe_axes_id;
81 hid_t dataset_id;
82 hid_t dataspace_id;
83 herr_t status;
84 HklBinocularsAxis *axis;
86 file_id = H5Fcreate(fn, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
88 groupe_id = H5Gcreate(file_id, "binoculars",
89 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
91 // config
92 status = save_string(groupe_id, "config", config);
94 // axes
95 groupe_axes_id = H5Gcreate(groupe_id, "axes",
96 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
98 darray_foreach(axis, self->axes){
99 double *arr;
100 hid_t dataspace_id;
101 hid_t dataset_id;
102 hsize_t dims[] = {6};
104 if(axis_size(axis) > 1){
105 arr = hkl_binoculars_axis_array(axis);
106 /* the arr[0] contains the axis index expected
107 by the binoculars gui. This value must
108 start from zero, so compute this index
109 using an external counter instead of using
110 the original index stored in the axis
111 array. */
112 arr[0] = axis_idx++;
113 dataspace_id = H5Screate_simple(ARRAY_SIZE(dims), dims, NULL);
114 dataset_id = H5Dcreate(groupe_axes_id, axis->name,
115 H5T_NATIVE_DOUBLE, dataspace_id,
116 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
117 status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE,
118 H5S_ALL, H5S_ALL,
119 H5P_DEFAULT, arr);
120 status = H5Dclose(dataset_id);
121 status = H5Sclose(dataspace_id);
122 free(arr);
126 status = H5Gclose(groupe_axes_id);
128 // create count dataset
129 dataspace_id = create_dataspace_from_axes(&self->axes);
130 dataset_id = H5Dcreate(groupe_id, "counts",
131 H5T_NATIVE_UINT32, dataspace_id,
132 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
133 status = H5Dwrite(dataset_id, H5T_NATIVE_UINT32,
134 H5S_ALL, H5S_ALL,
135 H5P_DEFAULT, self->photons);
136 status = H5Dclose(dataset_id);
137 status = H5Sclose(dataspace_id);
139 // create contributions dataset
140 dataspace_id = create_dataspace_from_axes(&self->axes);
141 dataset_id = H5Dcreate(groupe_id, "contributions",
142 H5T_NATIVE_UINT32, dataspace_id,
143 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
144 status = H5Dwrite(dataset_id, H5T_NATIVE_UINT32,
145 H5S_ALL, H5S_ALL,
146 H5P_DEFAULT, self->contributions);
147 status = H5Dclose(dataset_id);
148 status = H5Sclose(dataspace_id);
150 // terminate access and free identifiers
151 status = H5Gclose(groupe_id);
152 status = H5Fclose(file_id);
154 hkl_assert(status >= 0);