Debian Codegraph @

src/read_data_gadget_hdf5_utils.c

raw
1 /*
2 * This subroutine performs the calls to the HDF5 library for the
3 * GADGET data read
4 *
5 * Easier to do it this way and link with c than to try to link against
6 * the Fortran interface (in the latter case the modules must
7 * have been compiled with the *exact* compiler used to compile splash
8 * which is a real pain).
9 *
10 */
11 #include <stdlib.h>
12 #include <assert.h>
13 #include <string.h>
14 #include <hdf5.h>
15 #include "hdf5_helper_utils.h"
16
17 static int debug = 0;
18
19 /*int checkfordataset(hid_t file_id, char *datasetname);
20 * int get_rank(hid_t dataspace_id);
21 * int get_rank_by_name(hid_t group_id, char *name);
22 */
23
24 int read_gadgethdf5_dataset(hid_t group_id, char *datasetname, int itype, int maxtypes, int npartoftype[maxtypes],
25 int i0[maxtypes], int ncol, int isrequired[ncol], int *id, int *j, int *iblock);
26 void set_blocklabel_gadget(int *icol, int *irank, char *name);
27 void set_header_label_gadget(int *icol, char *name);
28 void read_gadgethdf5_data_fromc(int *icol, int *npartoftypei, double temparr[*npartoftypei],
29 int id[*npartoftypei], int *itype, int *i0, char *name, int *iblock);
30 void get_vel_info(hid_t group_id, char *name, int *ndimV);
31 void get_mass_info(hid_t group_id, char *name, int *rank);
32
33 void read_gadget_hdf5_header(char *filename,
34 int maxtypes,
35 int maxhdr,
36 int *npartoftype[maxtypes],
37 double *massoftype[maxtypes],
38 double *time,
39 double *redshift,
40 double *headervals[maxhdr],
41 int *iFlagSfr,
42 int *iFlagFeedback,
43 int *Nall[maxtypes],
44 int *iFlagCool,
45 int *igotids,
46 int *ndim,
47 int *ndimV,
48 int *nfiles,
49 int *ncol,
50 int *ierr)
51 {
52 hid_t file_id;
53 hid_t group_id, dataset_id;
54 hid_t attrib_id, dataspace_id;
55 herr_t status;
56 herr_t HDF5_error = -1;
57
58 *ierr = 0;
59 *igotids = 0;
60
61 if (debug)
62 printf("DEBUG: opening %s \n", filename);
63
64 file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
65 if (file_id == HDF5_error)
66 {
67 printf("ERROR opening %s \n", filename);
68 *ierr = 1;
69 return;
70 }
71 /*
72 * Open the "Header" dataset and read the header information
73 *
74 */
75 if (!checkfordataset(file_id, "Header"))
76 {
77 printf(" ERROR: \"Header\" dataset not found in GADGET HDF5 file\n");
78 *ierr = 2;
79 return;
80 }
81
82 #if H5_VERSION_GE(1, 8, 0)
83 group_id = H5Gopen2(file_id, "Header", H5P_DEFAULT);
84 #else
85 group_id = H5Gopen(file_id, "Header");
86 #endif
87 if (group_id == HDF5_error)
88 {
89 printf("ERROR opening Header data set \n");
90 *ierr = 2;
91 return;
92 }
93
94 int nattrib;
95 int i;
96 char name[256], maindataset[256];
97 char namevels[256], namemass[256];
98 nattrib = H5Aget_num_attrs(group_id);
99
100 /*
101 * Read through all of the attributes in the header, so we
102 * can still spit out the values even if they are not used by SPLASH
103 */
104 double BoxSize, HubbleParam, Omega0, OmegaLambda;
105 int iFlagStellarAge, iFlagMetals;
106 for (i = 0; i < nattrib; i++)
107 {
108 attrib_id = H5Aopen_idx(group_id, i);
109 ssize_t attr_status;
110 attr_status = H5Aget_name(attrib_id, 256, name);
111
112 hid_t type_id;
113 type_id = H5Aget_type(attrib_id);
114 /*type_class = H5Tget_native_type(type_id,H5T_DIR_ASCEND);*/
115 if (strcmp(name, "Time") == 0)
116 {
117 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, time);
118 }
119 else if (strcmp(name, "MassTable") == 0)
120 {
121 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, massoftype);
122 /*printf(" Masses = %i %f \n",maxtypes,massoftype[1]);*/
123 }
124 else if (strcmp(name, "NumPart_ThisFile") == 0)
125 {
126 status = H5Aread(attrib_id, H5T_NATIVE_INT, npartoftype);
127 }
128 else if (strcmp(name, "NumPart_Total") == 0)
129 {
130 status = H5Aread(attrib_id, H5T_NATIVE_INT, Nall);
131 }
132 else if (strcmp(name, "Redshift") == 0)
133 {
134 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, redshift);
135 }
136 else if (strcmp(name, "NumFilesPerSnapshot") == 0)
137 {
138 status = H5Aread(attrib_id, H5T_NATIVE_INT, nfiles);
139 }
140 else if (strcmp(name, "Flag_Sfr") == 0)
141 {
142 status = H5Aread(attrib_id, H5T_NATIVE_INT, iFlagSfr);
143 }
144 else if (strcmp(name, "Flag_Cooling") == 0)
145 {
146 status = H5Aread(attrib_id, H5T_NATIVE_INT, iFlagCool);
147 }
148 else if (strcmp(name, "Flag_Feedback") == 0)
149 {
150 status = H5Aread(attrib_id, H5T_NATIVE_INT, iFlagFeedback);
151 }
152 else if (strcmp(name, "BoxSize") == 0)
153 {
154 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, &BoxSize);
155 }
156 else if (strcmp(name, "HubbleParam") == 0)
157 {
158 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, &HubbleParam);
159 }
160 else if (strcmp(name, "Omega0") == 0)
161 {
162 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, &Omega0);
163 }
164 else if (strcmp(name, "OmegaLambda") == 0)
165 {
166 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, &OmegaLambda);
167 }
168 else if (strcmp(name, "Flag_StellarAge") == 0)
169 {
170 status = H5Aread(attrib_id, H5T_NATIVE_INT, &iFlagStellarAge);
171 }
172 else if (strcmp(name, "Flag_Metals") == 0)
173 {
174 status = H5Aread(attrib_id, H5T_NATIVE_INT, &iFlagMetals);
175 }
176 else if (strcmp(name, "Time_GYR") == 0)
177 {
178 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, time);
179 }
180 else
181 {
182 if (debug)
183 printf("DEBUG: unknown attribute %s \n", name);
184 }
185
186 H5T_class_t type_class = H5Tget_class(type_id);
187
188 if (type_class == H5T_INTEGER || type_class == H5T_FLOAT || type_class == H5T_NATIVE_DOUBLE) // Only read numeric types
189 {
190 // Read attribute and automatically convert to double
191 status = H5Aread(attrib_id, H5T_NATIVE_DOUBLE, &headervals[i]);
192
193 if (status == HDF5_error)
194 {
195 printf(" ERROR reading attribute %s \n", name);
196 } else {
197 int icol = i;
198 set_header_label_gadget(&icol, name);
199 }
200 }
201 else
202 {
203 if (debug)
204 printf("DEBUG: Attribute %s is not numeric and will be skipped.\n", name);
205 }
206
207 if (status == HDF5_error)
208 {
209 printf(" ERROR reading attribute %s \n", name);
210 }
211
212 status = H5Aclose(attrib_id);
213 }
214
215 status = H5Gclose(group_id);
216 if (status == HDF5_error)
217 {
218 printf("ERROR closing Header data set \n");
219 *ierr = 3;
220 return;
221 }
222
223 i = -1;
224 int got = 0;
225 while (!got && i < 5)
226 {
227 i++;
228 sprintf(maindataset, "PartType%i", i);
229 got = checkfordataset(file_id, maindataset);
230 if (!got)
231 {
232 if (i == 0)
233 {
234 printf(" WARNING: no gas particles found in GADGET HDF5 file\n");
235 }
236 else
237 {
238 printf(" WARNING: \"%s\" dataset not found in GADGET HDF5 file\n", maindataset);
239 }
240 }
241 }
242 if (!got)
243 {
244 printf(" ERROR: No PartType dataset found in GADGET HDF5 file\n");
245 *ierr = 2;
246 return;
247 }
248 if (debug)
249 printf("DEBUG: main dataset= %s \n", maindataset);
250
251 /*
252 * Now we need to get the number of data columns in the file
253 * (from the number of datasets in the "PartType0" group)
254 */
255 #if H5_VERSION_GE(1, 8, 0)
256 group_id = H5Gopen2(file_id, maindataset, H5P_DEFAULT);
257 #else
258 group_id = H5Gopen(file_id, maindataset);
259 #endif
260 if (group_id == HDF5_error)
261 {
262 printf("ERROR opening %s data set \n", maindataset);
263 *ierr = 2;
264 return;
265 }
266
267 hsize_t ndatasets;
268 status = H5Gget_num_objs(group_id, &ndatasets);
269 if (debug)
270 printf("DEBUG: number of datasets = %i \n", (int)ndatasets);
271
272 *ncol = 0;
273 *ndim = 0;
274 *ndimV = 0;
275 int rank = 0;
276 int j = 0;
277
278 rank = get_rank_by_name(group_id, "ParticleIDs");
279 if (rank == 1)
280 *igotids = 1;
281 if (debug)
282 printf("DEBUG: got IDs = %i\n", *igotids);
283
284 strcpy(name, "Coordinates");
285 *ndim = get_rank_by_name(group_id, name);
286 set_blocklabel_gadget(&j, ndim, name);
287 *ncol = *ncol + *ndim;
288 if (*ndim > 0)
289 {
290 j++;
291 }
292 else
293 {
294 printf("ERROR: %s dataset not found\n", name);
295 *ierr = 3;
296 return;
297 }
298
299 get_vel_info(group_id, namevels, ndimV);
300 set_blocklabel_gadget(&j, ndimV, "Velocities");
301 *ncol = *ncol + *ndimV;
302 if (*ndimV > 0)
303 {
304 j++;
305 }
306 else
307 {
308 printf("ERROR: Velocities not found in file\n");
309 *ierr = 3;
310 return;
311 }
312
313 get_mass_info(group_id, namemass, &rank);
314 if (rank == 0)
315 {
316 printf(" WARNING: Particle mass array not found in file\n");
317 }
318 else
319 {
320 set_blocklabel_gadget(&j, &rank, "Masses");
321 *ncol = *ncol + rank;
322 if (rank > 0)
323 j++;
324 }
325
326 if (*ndim == 0 || *ndimV == 0)
327 {
328 printf("ERROR: got ndim = %i, ndimV = %i\n", *ndim, *ndimV);
329 *ierr = 3;
330 return;
331 }
332
333 int itype;
334 for (i = 0; i < (int)ndatasets; i++)
335 {
336 status = H5Gget_objname_by_idx(group_id, i, name, 256);
337 itype = H5Gget_objtype_by_idx(group_id, i);
338 /*if (debug) printf("DEBUG: checking %s\n",name);*/
339 /* Should not try to open it if object is not a dataset */
340 if (itype == H5G_DATASET)
341 {
342 #if H5_VERSION_GE(1, 8, 0)
343 dataset_id = H5Dopen2(group_id, name, H5P_DEFAULT);
344 #else
345 dataset_id = H5Dopen(group_id, name);
346 #endif
347 dataspace_id = H5Dget_space(dataset_id);
348 rank = get_rank(dataspace_id);
349
350 if (strcmp(name, "ParticleIDs") &&
351 strcmp(name, "Coordinates") &&
352 strcmp(name, namevels) &&
353 strcmp(name, namemass))
354 {
355 if (debug)
356 printf("DEBUG: storing %s x %i \n", name, rank);
357 /* Send the dataset names back to Fortran
358 * one by one, so they can be filled into
359 * the array as appropriate */
360 set_blocklabel_gadget(&j, &rank, name);
361 *ncol = *ncol + rank;
362 if (rank > 0)
363 j++;
364 }
365 else
366 {
367 if (debug)
368 printf("DEBUG: ignoring %s \n", name);
369 }
370 status = H5Dclose(dataset_id);
371 }
372 else
373 {
374 if (debug)
375 printf("DEBUG: skipping %s as it is not a dataset\n", name);
376 }
377 }
378
379 status = H5Gclose(group_id);
380
381 status = H5Fclose(file_id);
382 if (status == HDF5_error)
383 {
384 printf("ERROR closing file \n");
385 *ierr = 7;
386 }
387 if (debug)
388 printf("DEBUG: finished header read \n");
389 }
390
391 void read_gadget_hdf5_data(char *filename,
392 int maxtypes,
393 int npartoftype[maxtypes],
394 int ncol,
395 int isrequired[ncol],
396 int i0[maxtypes],
397 int *ierr)
398 {
399 hid_t file_id;
400 hid_t group_id;
401 herr_t status;
402 herr_t HDF5_error = -1;
403 char groupname[12];
404 char datasetname[256], namevels[256], namemass[256];
405
406 int i, ndimV, rank;
407 int *id;
408
409 if (debug)
410 printf("DEBUG: re-opening %s \n", filename);
411 file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
412 if (file_id == HDF5_error)
413 {
414 printf("ERROR re-opening %s \n", filename);
415 *ierr = 1;
416 return;
417 }
418
419 /* read dataset for each particle type present in dump file */
420 int itype, iobjtype;
421 for (itype = 0; itype < maxtypes; itype++)
422 {
423 if (npartoftype[itype] > 0)
424 {
425 /* If npartoftype[N] > 0 in header, look for dataset of the form PartTypeN */
426 sprintf(groupname, "PartType%i", itype);
427 if (debug)
428 printf("DEBUG: opening group %s\n", groupname);
429 #if H5_VERSION_GE(1, 8, 0)
430 group_id = H5Gopen2(file_id, groupname, H5P_DEFAULT);
431 #else
432 group_id = H5Gopen(file_id, groupname);
433 #endif
434 if (group_id == HDF5_error)
435 {
436 printf("ERROR opening %s group \n", groupname);
437 *ierr = 2;
438 }
439 else
440 {
441 hsize_t ndatasets;
442 status = H5Gget_num_objs(group_id, &ndatasets);
443 if (debug)
444 printf("DEBUG: number of datasets = %i \n", (int)ndatasets);
445
446 /* get names of velocity and particle mass datasets */
447 get_vel_info(group_id, namevels, &ndimV);
448 get_mass_info(group_id, namemass, &rank);
449
450 /* read particle ID */
451 int k = 0;
452 int m = 0;
453 id = malloc(npartoftype[itype] * sizeof(int));
454 *ierr = read_gadgethdf5_dataset(group_id, "ParticleIDs", itype, maxtypes, npartoftype, i0, ncol, isrequired, id, &k, &m);
455 /* set all IDs to zero if not read */
456 if (*ierr != 0)
457 printf("DEBUG: error from ID read = %i, rank = %i \n", *ierr, k);
458 if (*ierr != 0 || k != 1)
459 {
460 for (k = 0; k < npartoftype[itype]; k++)
461 {
462 id[k] = 0;
463 }
464 };
465
466 int j = 0;
467 int iblock = 0;
468 /* read datasets common to all particle types first */
469 *ierr = read_gadgethdf5_dataset(group_id, "Coordinates", itype, maxtypes, npartoftype, i0, ncol, isrequired, id, &j, &iblock);
470 *ierr = read_gadgethdf5_dataset(group_id, namevels, itype, maxtypes, npartoftype, i0, ncol, isrequired, id, &j, &iblock);
471 *ierr = read_gadgethdf5_dataset(group_id, namemass, itype, maxtypes, npartoftype, i0, ncol, isrequired, id, &j, &iblock);
472
473 /* read remaining datasets in the order they appear in the file */
474 for (i = 0; i < (int)ndatasets; i++)
475 {
476 status = H5Gget_objname_by_idx(group_id, i, datasetname, 256);
477 iobjtype = H5Gget_objtype_by_idx(group_id, i);
478 if (strcmp(datasetname, "ParticleIDs") &&
479 strcmp(datasetname, "Coordinates") &&
480 strcmp(datasetname, namevels) &&
481 strcmp(datasetname, namemass) && (iobjtype == H5G_DATASET))
482 {
483 *ierr = read_gadgethdf5_dataset(group_id, datasetname, itype, maxtypes, npartoftype, i0, ncol, isrequired, id, &j, &iblock);
484 }
485 }
486
487 free(id);
488 H5Gclose(group_id);
489 }
490 }
491 }
492
493 status = H5Fclose(file_id);
494 if (status == HDF5_error)
495 {
496 printf("ERROR closing file \n");
497 *ierr = 7;
498 }
499 }
500
501 int read_gadgethdf5_dataset(hid_t group_id,
502 char *datasetname,
503 int itype,
504 int maxtypes,
505 int npartoftype[maxtypes],
506 int i0[maxtypes],
507 int ncol,
508 int isrequired[ncol],
509 int *id,
510 int *j,
511 int *iblock)
512 {
513 hid_t dataset_id, dataspace_id, memspace_id;
514 herr_t status;
515 herr_t HDF5_error = -1;
516 int ierr = 0;
517
518 if (!checkfordataset(group_id, datasetname))
519 {
520 ierr = 1;
521 return ierr;
522 }
523
524 #if H5_VERSION_GE(1, 8, 0)
525 dataset_id = H5Dopen2(group_id, datasetname, H5P_DEFAULT);
526 #else
527 dataset_id = H5Dopen(group_id, datasetname);
528 #endif
529 dataspace_id = H5Dget_space(dataset_id);
530 int rank = get_rank(dataspace_id);
531 int k, flag;
532
533 /* do nothing if none of the columns are required */
534 flag = 0;
535 for (k = *j; k < *j + rank; k++)
536 {
537 if (isrequired[k])
538 flag = 1;
539 }
540
541 if (!flag)
542 {
543 if (debug)
544 printf("DEBUG: skipping %s : not required\n", datasetname);
545 H5Dclose(dataset_id);
546 *j = *j + rank;
547 if (rank > 0) *iblock += 1;
548 return 0;
549 }
550
551 if (debug)
552 printf("DEBUG: got %s rank %i \n", datasetname, rank);
553 /* make a temporary array to put each column as we read it */
554 hsize_t nparttmp[1];
555 nparttmp[0] = npartoftype[itype];
556 memspace_id = H5Screate_simple(1, nparttmp, NULL);
557 double *temp = 0;
558 temp = malloc(npartoftype[itype] * sizeof(double));
559
560 if (rank > 0) *iblock = *iblock + 1;
561
562 if (rank == 1)
563 {
564 *j = *j + 1;
565
566 if (!strcmp(datasetname, "ParticleIDs"))
567 {
568 /* read particle IDs from file */
569 H5Dread(dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id, H5P_DEFAULT, id);
570 }
571 else
572 {
573 /* read column from file */
574 H5Dread(dataset_id, H5T_NATIVE_DOUBLE, memspace_id, dataspace_id, H5P_DEFAULT, temp);
575
576 /* call Fortran back, sending values in temp array to fill into the main splash dat array */
577 read_gadgethdf5_data_fromc(j,&npartoftype[itype],temp,id,&itype,&i0[itype],datasetname,iblock);
578 }
579 }
580 else
581 {
582 hsize_t offset[2], count[2];
583
584 for (k = 1; k <= rank; k++)
585 {
586 *j = *j + 1;
587 count[0] = npartoftype[itype];
588 count[1] = 1;
589 offset[0] = 0;
590 offset[1] = k - 1; /* rank */
591 status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
592 if (status == HDF5_error)
593 {
594 printf("ERROR creating hyperslab \n");
595 ierr = 4;
596 }
597 if (!H5Sselect_valid(dataspace_id))
598 {
599 printf("ERROR selecting hyperslab \n");
600 ierr = 5;
601 }
602
603 /* read column from file */
604 if (isrequired[*j - 1])
605 {
606 H5Dread(dataset_id, H5T_NATIVE_DOUBLE, memspace_id, dataspace_id, H5P_DEFAULT, temp);
607
608 /* call Fortran back, sending values in temp array to fill into the main splash dat array */
609 read_gadgethdf5_data_fromc(j,&npartoftype[itype],temp,id,&itype,&i0[itype],datasetname,iblock);
610 }
611 else
612 {
613 if (debug)
614 printf("DEBUG: skipping %s in COLUMN %i \n", datasetname, *j);
615 }
616 }
617 }
618 free(temp);
619
620 status = H5Sclose(dataspace_id);
621 if (status == HDF5_error)
622 {
623 printf("ERROR closing dataspace \n");
624 ierr = 4;
625 }
626
627 H5Dclose(dataset_id);
628 if (status == HDF5_error)
629 {
630 printf("ERROR closing dataset \n");
631 ierr = 7;
632 }
633 return ierr;
634 }
635
636 void get_vel_info(hid_t group_id, char *name, int *ndimV)
637 {
638 strcpy(name, "Velocities");
639 *ndimV = get_rank_by_name(group_id, name);
640 /* If "Velocities" not found, try "Velocity" */
641 if (*ndimV <= 0)
642 {
643 strcpy(name, "Velocity");
644 *ndimV = get_rank_by_name(group_id, name);
645 }
646 return;
647 }
648 /*
649 * utility function to find particle mass dataset and rank
650 */
651 void get_mass_info(hid_t group_id, char *name, int *rank)
652 {
653 strcpy(name, "Masses");
654 *rank = get_rank_by_name(group_id, name);
655 /* If "Masses" not found, try "Mass" */
656 if (*rank <= 0)
657 {
658 strcpy(name, "Mass");
659 *rank = get_rank_by_name(group_id, name);
660 }
661 return;
662 }