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-2016 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>
24 //#undef H5_USE_16_API
29 #define ROOT "/nfs/ruche-sixs/sixs-soleil/com-sixs/2015/Shutdown4-5/XpadAu111/"
30 #define FILENAME "align_FLY2_omega_00045.nxs"
32 #define DATASET_IMAGE "com_113934/scan_data/xpad_image"
33 #define DATASET_MU "com_113934/scan_data/UHV_MU"
34 #define DATASET_OMEGA "com_113934/scan_data/UHV_OMEGA"
35 #define DATASET_DELTA "com_113934/scan_data/UHV_DELTA"
36 #define DATASET_GAMMA "com_113934/scan_data/UHV_GAMMA"
37 #define DATASET_UB "com_113934/SIXS/I14-C-CX2__EX__DIFF-UHV__#1/UB"
38 #define DATASET_WAVELENGTH "com_113934/SIXS/Monochromator/wavelength"
39 #define DATASET_DIFFRACTOMETER_TYPE "com_113934/SIXS/I14-C-CX2__EX__DIFF-UHV__#1/type"
41 /* Display all the node informations */
43 static herr_t
attribute_info(hid_t location_id
, const char *attr_name
, const H5A_info_t
*ainfo
, void *op_data
)
45 printf(" Attribute: %d %s\n", location_id
, attr_name
);
50 static herr_t
file_info(hid_t loc_id
, const char *name
, const H5L_info_t
*info
, void *opdata
)
56 * Get type of the object and display its name and type.
57 * The name of the object is passed to this function by
58 * the Library. Some magic :-)
60 H5Oget_info_by_name(loc_id
, name
, &statbuf
, H5P_DEFAULT
);
61 switch (statbuf
.type
) {
62 case H5O_TYPE_UNKNOWN
:
63 printf(" Object with name %s is an unknown type\n", name
);
66 printf(" Object with name %s is a group\n", name
);
68 case H5O_TYPE_DATASET
:
69 printf(" Object with name %s is a dataset\n", name
);
71 case H5O_TYPE_NAMED_DATATYPE
:
72 printf(" Object with name %s is a named datatype\n", name
);
75 printf(" Unable to identify an object ");
78 H5Aiterate_by_name(loc_id
, name
, H5_INDEX_NAME
, H5_ITER_NATIVE
, &n
, attribute_info
, NULL
, H5P_DEFAULT
);
83 typedef struct _HklDataframeSixsUhv HklDataframeSixsUhv
;
85 struct _HklDataframeSixsUhv
{
97 static HklDataframeSixsUhv
hkl_h5_open(const char *filename
)
101 HklDataframeSixsUhv dataframe
= {0};
103 /* is it an hdf5 file */
104 if (H5Fis_hdf5(filename
) == 0)
107 h5file_id
= H5Fopen (filename
, H5F_ACC_RDONLY
, H5P_DEFAULT
);
111 /* display all the node informations */
112 /* status = H5Lvisit(h5file_id, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, file_info, NULL); */
114 /* return dataframe; */
116 dataframe
.file
= h5file_id
;
117 dataframe
.mu
= H5Dopen (h5file_id
, DATASET_MU
, H5P_DEFAULT
);
118 dataframe
.omega
= H5Dopen (h5file_id
, DATASET_OMEGA
, H5P_DEFAULT
);
119 dataframe
.delta
= H5Dopen (h5file_id
, DATASET_DELTA
, H5P_DEFAULT
);
120 dataframe
.gamma
= H5Dopen (h5file_id
, DATASET_GAMMA
, H5P_DEFAULT
);
122 dataframe
.ub
= H5Dopen (h5file_id
, DATASET_UB
, H5P_DEFAULT
);
123 dataframe
.wavelength
= H5Dopen (h5file_id
, DATASET_WAVELENGTH
, H5P_DEFAULT
);
124 dataframe
.dtype
= H5Dopen (h5file_id
, DATASET_DIFFRACTOMETER_TYPE
, H5P_DEFAULT
);
129 static hssize_t
check_ndims(hid_t dataset_id
, int expected
, hid_t
*space_id
)
131 *space_id
= H5Dget_space (dataset_id
);
132 return H5Sget_simple_extent_ndims (*space_id
) == expected
;
135 static int hkl_h5_len(const HklDataframeSixsUhv
*dataframe
)
137 hid_t space_id
= H5Dget_space (dataframe
->mu
);
138 return H5Sget_simple_extent_npoints(space_id
);
142 static int hkl_h5_is_valid(const HklDataframeSixsUhv
*dataframe
)
148 /* check the dimensionnality of the axes */
149 res
&= check_ndims(dataframe
->mu
, 1, &space_id
);
150 n
= H5Sget_simple_extent_npoints(space_id
);
152 /* check the dimensionnality of all the axes */
153 res
&= check_ndims(dataframe
->omega
, 1, &space_id
);
154 res
&= n
== H5Sget_simple_extent_npoints(space_id
);
156 res
&= check_ndims(dataframe
->delta
, 1, &space_id
);
157 res
&= n
== H5Sget_simple_extent_npoints(space_id
);
159 res
&= check_ndims(dataframe
->gamma
, 1, &space_id
);
160 res
&= n
== H5Sget_simple_extent_npoints(space_id
);
165 static herr_t
hkl_h5_close(const HklDataframeSixsUhv
*dataframe
)
167 H5Dclose(dataframe
->dtype
);
168 H5Dclose(dataframe
->wavelength
);
169 H5Dclose(dataframe
->ub
);
170 H5Dclose(dataframe
->gamma
);
171 H5Dclose(dataframe
->delta
);
172 H5Dclose(dataframe
->omega
);
173 H5Dclose(dataframe
->mu
);
175 return H5Fclose (dataframe
->file
);
178 typedef struct _HklDataframe HklDataframe
;
180 struct _HklDataframe
{
183 const HklDataframeSixsUhv
*_dataframe
;
186 static const HklDataframe
hkl_dataframe_first(const HklDataframeSixsUhv
*dataframe
)
191 frame
.len
= hkl_h5_len(dataframe
);
192 frame
._dataframe
= dataframe
;
197 static int hkl_dataframe_done(const HklDataframe dataframe
)
199 return dataframe
.i
< dataframe
.len
;
202 static const HklDataframe
hkl_dataframe_next(const HklDataframe dataframe
)
204 HklDataframe frame
= dataframe
;
212 static herr_t
get_position(hid_t dataset_id
, int idx
, double *position
)
220 mem_type_id
= H5Dget_type(dataset_id
);
222 /* Get the dataspace handle */
223 if ( (space_id
= H5Dget_space( dataset_id
)) < 0 )
226 /* Define a hyperslab in the dataset of the size of the records */
229 if ( H5Sselect_hyperslab(space_id
, H5S_SELECT_SET
, offset
, NULL
, count
, NULL
) < 0 )
232 /* Create a memory dataspace handle */
233 if ( (mem_space_id
= H5Screate_simple( 1, count
, NULL
)) < 0 )
236 if ( H5Dread(dataset_id
, mem_type_id
, mem_space_id
, space_id
, H5P_DEFAULT
, position
) < 0 )
239 /* Terminate access to the memory dataspace */
240 if ( H5Sclose( mem_space_id
) < 0 )
243 /* Terminate access to the dataspace */
244 if ( H5Sclose( space_id
) < 0 )
249 H5Tclose(mem_type_id
);
253 static herr_t
hkl_dataframe_geometry_get(const HklDataframe dataframe
, HklGeometry
**geometry
)
260 /* create the HklGeometry */
261 if((*geometry
) == NULL
){
266 /* read the diffractometer type from the hdf5 file */
267 datatype
= H5Dget_type(dataframe
._dataframe
->dtype
);
268 n
= H5Tget_size(datatype
);
270 status
= H5Dread(dataframe
._dataframe
->dtype
,
275 /* remove the last "\n" char */
278 factory
= hkl_factory_get_by_name(name
, NULL
);
279 *geometry
= hkl_factory_create_new_geometry(factory
);
285 /* read the wavelength double */
286 /* TODO check the right size */
287 /* TODO how to obtain the unit of the wavelength */
288 datatype
= H5Dget_type(dataframe
._dataframe
->wavelength
);
289 status
= H5Dread(dataframe
._dataframe
->wavelength
,
292 H5P_DEFAULT
, &wavelength
);
294 hkl_geometry_wavelength_set(*geometry
, wavelength
, HKL_UNIT_USER
, NULL
);
297 /* read the axis positions of the ith dataframe */
298 /* check how to decide about the dataset connection and the hkl axes connection */
299 /* TODO check the right size */
300 /* TODO how to obtain the unit of the axes position */
301 if (get_position(dataframe
._dataframe
->mu
,
302 dataframe
.i
, &axes
[0]) < 0)
304 if (get_position(dataframe
._dataframe
->omega
,
305 dataframe
.i
, &axes
[1]) < 0)
307 if (get_position(dataframe
._dataframe
->gamma
,
308 dataframe
.i
, &axes
[2]) < 0)
310 if (get_position(dataframe
._dataframe
->delta
,
311 dataframe
.i
, &axes
[3]) < 0)
314 hkl_geometry_axis_values_set(*geometry
, axes
, 4, HKL_UNIT_USER
, NULL
);
315 /* hkl_geometry_fprintf(stdout, *geometry); */
316 /* fprintf(stdout, "\n"); */
324 int main (int argc
, char ** argv
)
326 const char *filename
= ROOT FILENAME
;
327 HklDataframeSixsUhv dataframe_h5
= hkl_h5_open(filename
);
328 int res
= hkl_h5_is_valid(&dataframe_h5
);
329 /* fprintf(stdout, "h5file is valid : %d\n", res); */
330 HklGeometry
*geometry
= NULL
;
332 for(HklDataframe dataframe
= hkl_dataframe_first(&dataframe_h5
);
333 hkl_dataframe_done(dataframe
);
334 dataframe
= hkl_dataframe_next(dataframe
))
336 hkl_dataframe_geometry_get(dataframe
, &geometry
);
337 /* fprintf(stdout, " %d", dataframe.i); */
340 hkl_geometry_free(geometry
);
341 hkl_h5_close(&dataframe_h5
);