ASCOT5
Loading...
Searching...
No Matches
hdf5_histogram.c
Go to the documentation of this file.
1
5#include <string.h>
6#include <stdlib.h>
7#include <hdf5.h>
8#include <hdf5_hl.h>
9#include "hdf5_histogram.h"
10
43 hid_t f, const char *path, int abscissaDim, int ordinateDim,
44 int *abscissaNslots, double *abscissaMin, double *abscissaMax,
45 char **abscissaUnits, char **abscissaNames,
46 char **ordinateUnits, char **ordinateNames, double *ordinate) {
47
48 char temppath[256]; /* Helper variable */
49
50 /* Create histogram group */
51 hid_t histogram = H5Gcreate2(f, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
52 if(histogram < 0) {
53 return 1;
54 }
55
56 /* Dimensions of ordinate data to be written */
57 hsize_t dims[100];
58 dims[0] = ordinateDim;
59 for (int i=0; i<abscissaDim; i++) {
60 dims[i+1] = abscissaNslots[i];
61 }
62
63 /* Write ordinate including its names and units */
64 herr_t err;
65 err = H5LTmake_dataset_double(histogram , "ordinate", abscissaDim+1,
66 dims, ordinate);
67 if(err){
68 H5Gclose(histogram);
69 return err;
70 }
71 for(int i=0; i<ordinateDim; i++) {
72 sprintf(temppath, "name_%02d", i);
73 H5LTset_attribute_string(histogram, "ordinate", temppath,
74 ordinateNames[i]);
75 sprintf(temppath, "unit_%02d", i);
76 H5LTset_attribute_string(histogram, "ordinate", temppath,
77 ordinateUnits[i]);
78 }
79
80
81 /* Write ordinate and abscissa dimensions */
82 hsize_t dimsize = 1;
83 err = H5LTmake_dataset_int(histogram , "ordinate_ndim", 1, &dimsize,
84 &ordinateDim);
85 if(err){
86 H5Gclose(histogram);
87 return 1;
88 }
89 err = H5LTmake_dataset_int(histogram , "abscissa_ndim", 1, &dimsize,
90 &abscissaDim);
91 if(err){
92 H5Gclose(histogram);
93 return 1;
94 }
95
96 /* Write abscissae */
97 for (int i=0; i<abscissaDim; i++) {
98
99 double* abscissavec = (double *) malloc( (dims[i+1]+1)*sizeof(double) );
100 for(int j=0; j<dims[i+1]+1; j++) {
101 abscissavec[j] =
102 abscissaMin[i] + j * ( (abscissaMax[i] - abscissaMin[i])
103 / abscissaNslots[i] );
104 }
105
106 /* Vector */
107 char abscissapath[256];
108 sprintf(abscissapath, "abscissa_vec_%02d",i+1);
109 hsize_t abscissasize[1] = {abscissaNslots[i]+1};
110 err = H5LTmake_dataset_double(histogram , abscissapath, 1, abscissasize,
111 abscissavec);
112 free(abscissavec);
113 if(err){
114 H5Gclose(histogram);
115 return 1;
116 }
117
118 /* Vector length */
119 sprintf(temppath, "abscissa_nbin_%02d", i+1);
120 err = H5LTmake_dataset_int(histogram, temppath, 1, &dimsize,
121 &(abscissaNslots[i]));
122 if(err){
123 H5Gclose(histogram);
124 return 1;
125 }
126
127 /* Name and unit */
128 sprintf(temppath, "name_%02d", i);
129 H5LTset_attribute_string(histogram, abscissapath, temppath,
130 abscissaNames[i]);
131 sprintf(temppath, "unit_%02d", i);
132 H5LTset_attribute_string(histogram, abscissapath, temppath,
133 abscissaUnits[i]);
134
135 }
136
137 H5Gclose(histogram);
138
139 return 0;
140}
int hdf5_histogram_write_uniform_double(hid_t f, const char *path, int abscissaDim, int ordinateDim, int *abscissaNslots, double *abscissaMin, double *abscissaMax, char **abscissaUnits, char **abscissaNames, char **ordinateUnits, char **ordinateNames, double *ordinate)
Write a histogram with uniform grid to HDF5 file.
Header file for hdf5_histogram.c.