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 "../consts.h"
10#include "../diag/hist.h"
11#include "hdf5_histogram.h"
12
16int hdf5_hist_write(hid_t f, char* path, histogram* hist) {
17 int ordinate_dim = 1;
18 int abscissa_dim = 0;
19 for(int i = 0; i < HIST_ALLDIM; i++) {
20 abscissa_dim += hist->axes[i].n ? 1 : 0;
21 }
22
23 int* abscissa_n_slots = (int*) malloc(abscissa_dim * sizeof(int));
24 double* abscissa_min = (double*) malloc(abscissa_dim * sizeof(double));
25 double* abscissa_max = (double*) malloc(abscissa_dim * sizeof(double));
26 char** abscissa_names = (char**) malloc(abscissa_dim * sizeof(char*));
27 char** abscissa_units = (char**) malloc(abscissa_dim * sizeof(char*));
28
29 int k = 0;
30 for(int i = 0; i < HIST_ALLDIM; i++) {
31 if(!hist->axes[i].n) {
32 continue;
33 }
34 abscissa_min[k] = hist->axes[i].min;
35 abscissa_max[k] = hist->axes[i].max;
36 abscissa_n_slots[k] = hist->axes[i].n;
37
38 switch (hist->axes[i].name)
39 {
40 case R:
41 abscissa_names[k] = "r";
42 abscissa_units[k] = "m";
43 break;
44 case PHI:
45 abscissa_names[k] = "phi";
46 abscissa_units[k] = "deg";
47 abscissa_min[k] *= 180.0/CONST_PI;
48 abscissa_max[k] *= 180.0/CONST_PI;
49 break;
50 case Z:
51 abscissa_names[k] = "z";
52 abscissa_units[k] = "m";
53 break;
54 case RHO:
55 abscissa_names[k] = "rho";
56 abscissa_units[k] = "1";
57 break;
58 case THETA:
59 abscissa_names[k] = "theta";
60 abscissa_units[k] = "deg";
61 abscissa_min[k] *= 180.0/CONST_PI;
62 abscissa_max[k] *= 180.0/CONST_PI;
63 break;
64 case PPAR:
65 abscissa_names[k] = "ppar";
66 abscissa_units[k] = "kg*m/s";
67 break;
68 case PPERP:
69 abscissa_names[k] = "pperp";
70 abscissa_units[k] = "kg*m/s";
71 break;
72 case PR:
73 abscissa_names[k] = "pr";
74 abscissa_units[k] = "kg*m/s";
75 break;
76 case PPHI:
77 abscissa_names[k] = "pphi";
78 abscissa_units[k] = "kg*m/s";
79 break;
80 case PZ:
81 abscissa_names[k] = "pz";
82 abscissa_units[k] = "kg*m/s";
83 break;
84 case EKIN:
85 abscissa_names[k] = "ekin";
86 abscissa_units[k] = "eV";
87 abscissa_min[k] /= CONST_E;
88 abscissa_max[k] /= CONST_E;
89 break;
90 case XI:
91 abscissa_names[k] = "pitch";
92 abscissa_units[k] = "1";
93 break;
94 case MU:
95 abscissa_names[k] = "mu";
96 abscissa_units[k] = "T/eV";
97 break;
98 case PTOR:
99 abscissa_names[k] = "ptor";
100 abscissa_units[k] = "kg*m/s";
101 break;
102 case TIME:
103 abscissa_names[k] = "time";
104 abscissa_units[k] = "s";
105 break;
106 case CHARGE:
107 abscissa_names[k] = "charge";
108 abscissa_units[k] = "e";
109 break;
110 }
111 k++;
112 }
113 char* ordinate_names[] = { "distribution" };
114 char* ordinate_units[] = { "s/(m^5*kg^2*deg*e)" };
115
116 /* Create a group for this distribution and write the data in it */
118 f, path, abscissa_dim, ordinate_dim, abscissa_n_slots, abscissa_min,
119 abscissa_max, abscissa_units, abscissa_names, ordinate_units,
120 ordinate_names, hist->bins);
121 free(abscissa_n_slots);
122 free(abscissa_min);
123 free(abscissa_max);
124 free(abscissa_names);
125 free(abscissa_units);
126
127 return retval;
128}
129
162 hid_t f, const char *path, int abscissaDim, int ordinateDim,
163 int *abscissaNslots, double *abscissaMin, double *abscissaMax,
164 char **abscissaUnits, char **abscissaNames,
165 char **ordinateUnits, char **ordinateNames, double *ordinate) {
166
167 char temppath[256]; /* Helper variable */
168
169 /* Create histogram group */
170 hid_t histogram = H5Gcreate2(f, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
171 if(histogram < 0) {
172 return 1;
173 }
174
175 /* Dimensions of ordinate data to be written */
176 hsize_t dims[100];
177 dims[0] = ordinateDim;
178 for (int i=0; i<abscissaDim; i++) {
179 dims[i+1] = abscissaNslots[i];
180 }
181
182 /* Write ordinate including its names and units */
183 herr_t err;
184 err = H5LTmake_dataset_double(histogram , "ordinate", abscissaDim+1,
185 dims, ordinate);
186 if(err){
187 H5Gclose(histogram);
188 return err;
189 }
190 for(int i=0; i<ordinateDim; i++) {
191 sprintf(temppath, "name_%02d", i);
192 H5LTset_attribute_string(histogram, "ordinate", temppath,
193 ordinateNames[i]);
194 sprintf(temppath, "unit_%02d", i);
195 H5LTset_attribute_string(histogram, "ordinate", temppath,
196 ordinateUnits[i]);
197 }
198
199
200 /* Write ordinate and abscissa dimensions */
201 hsize_t dimsize = 1;
202 err = H5LTmake_dataset_int(histogram , "ordinate_ndim", 1, &dimsize,
203 &ordinateDim);
204 if(err){
205 H5Gclose(histogram);
206 return 1;
207 }
208 err = H5LTmake_dataset_int(histogram , "abscissa_ndim", 1, &dimsize,
209 &abscissaDim);
210 if(err){
211 H5Gclose(histogram);
212 return 1;
213 }
214
215 /* Write abscissae */
216 for (int i=0; i<abscissaDim; i++) {
217
218 double* abscissavec = (double *) malloc( (dims[i+1]+1)*sizeof(double) );
219 for(int j=0; j<dims[i+1]+1; j++) {
220 abscissavec[j] =
221 abscissaMin[i] + j * ( (abscissaMax[i] - abscissaMin[i])
222 / abscissaNslots[i] );
223 }
224
225 /* Vector */
226 char abscissapath[256];
227 sprintf(abscissapath, "abscissa_vec_%02d",i+1);
228 hsize_t abscissasize[1] = {abscissaNslots[i]+1};
229 err = H5LTmake_dataset_double(histogram , abscissapath, 1, abscissasize,
230 abscissavec);
231 free(abscissavec);
232 if(err){
233 H5Gclose(histogram);
234 return 1;
235 }
236
237 /* Vector length */
238 sprintf(temppath, "abscissa_nbin_%02d", i+1);
239 err = H5LTmake_dataset_int(histogram, temppath, 1, &dimsize,
240 &(abscissaNslots[i]));
241 if(err){
242 H5Gclose(histogram);
243 return 1;
244 }
245
246 /* Name and unit */
247 sprintf(temppath, "name_%02d", i);
248 H5LTset_attribute_string(histogram, abscissapath, temppath,
249 abscissaNames[i]);
250 sprintf(temppath, "unit_%02d", i);
251 H5LTset_attribute_string(histogram, abscissapath, temppath,
252 abscissaUnits[i]);
253
254 }
255
256 H5Gclose(histogram);
257
258 return 0;
259}
Header file containing physical and mathematical constants.
#define CONST_PI
pi
Definition consts.h:11
#define CONST_E
Elementary charge [C].
Definition consts.h:35
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.
int hdf5_hist_write(hid_t f, char *path, histogram *hist)
Write a histogram object to HDF5 file.
Header file for hdf5_histogram.c.
Header file for hist.c.
@ R
Definition hist.h:18
@ XI
Definition hist.h:29
@ THETA
Definition hist.h:22
@ PZ
Definition hist.h:27
@ PPHI
Definition hist.h:26
@ RHO
Definition hist.h:21
@ PHI
Definition hist.h:19
@ PPERP
Definition hist.h:24
@ PR
Definition hist.h:25
@ MU
Definition hist.h:30
@ PPAR
Definition hist.h:23
@ Z
Definition hist.h:20
@ EKIN
Definition hist.h:28
@ PTOR
Definition hist.h:31
@ TIME
Definition hist.h:32
@ CHARGE
Definition hist.h:33
real min
Definition hist.h:41
real max
Definition hist.h:42
hist_coordinate name
Definition hist.h:40
size_t n
Definition hist.h:43
Histogram parameters.
Definition hist.h:52
hist_axis axes[HIST_ALLDIM]
Definition hist.h:53
real * bins
Definition hist.h:56