| 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_amuse_hdf5_dataset(hid_t group_id, char *datasetname, int itype, int maxtypes, int npartoftype[maxtypes], |
| 25 |
int ncol, int isrequired[ncol], int *j); |
| 26 |
void set_blocklabel(int *icol, char *name); |
| 27 |
void read_amuse_hdf5_data_fromc(int *icol, int *npartoftypei, double temparr[*npartoftypei],int *itype); |
| 28 |
void read_amuse_hdf5_header(char *filename, |
| 29 |
int *npart, |
| 30 |
int *ncol, |
| 31 |
int *ndim, |
| 32 |
int *ndimV, |
| 33 |
double *time, |
| 34 |
int *ierr) |
| 35 |
{ |
| 36 |
hid_t file_id; |
| 37 |
hid_t group_id, group_id1, group_id2; |
| 38 |
hid_t attrib_id; |
| 39 |
herr_t status; |
| 40 |
herr_t HDF5_error = -1; |
| 41 |
|
| 42 |
*ierr = 0; |
| 43 |
*ndim = 0; |
| 44 |
*ndimV = 0; |
| 45 |
|
| 46 |
if (debug) printf("DEBUG: opening %s \n",filename); |
| 47 |
file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); |
| 48 |
if (file_id == HDF5_error) |
| 49 |
{ printf("ERROR opening %s \n",filename); *ierr = 1; return; } |
| 50 |
|
| 51 |
char *maingroup = "particles"; |
| 52 |
/* |
| 53 |
* Open the "particles" dataset and read the number of particles attribute |
| 54 |
* |
| 55 |
*/ |
| 56 |
if (!checkfordataset(file_id,maingroup)) |
| 57 |
{ |
| 58 |
printf(" ERROR: \"%s\" dataset not found in AMUSE HDF5 file\n",maingroup); |
| 59 |
*ierr = 2; |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
#if H5_VERSION_GE(1,8,0) |
| 64 |
group_id1 = H5Gopen2(file_id,maingroup,H5P_DEFAULT); |
| 65 |
#else |
| 66 |
group_id1 = H5Gopen(file_id,maingroup); |
| 67 |
#endif |
| 68 |
if (group_id1 == HDF5_error) |
| 69 |
{ printf("ERROR opening %s data set \n",maingroup); *ierr = 2; return; } |
| 70 |
|
| 71 |
#if H5_VERSION_GE(1,8,0) |
| 72 |
group_id = H5Gopen2(group_id1,"0000000001",H5P_DEFAULT); |
| 73 |
#else |
| 74 |
group_id = H5Gopen(group_id1,"0000000001"); |
| 75 |
#endif |
| 76 |
if (group_id == HDF5_error) |
| 77 |
{ printf("ERROR opening 00000000001 data set \n"); *ierr = 2; return; } |
| 78 |
|
| 79 |
int nattrib; |
| 80 |
int i; |
| 81 |
char name[256]; |
| 82 |
nattrib = H5Aget_num_attrs(group_id); |
| 83 |
if (debug) printf("number of attributes found = %i\n",nattrib); |
| 84 |
|
| 85 |
/* |
| 86 |
* Read through all of the attributes in the header, so we |
| 87 |
* can still spit out the values even if they are not used by SPLASH |
| 88 |
*/ |
| 89 |
for(i=0; i < nattrib; i++) { |
| 90 |
attrib_id = H5Aopen_idx(group_id,i); |
| 91 |
ssize_t attr_status; |
| 92 |
attr_status = H5Aget_name(attrib_id, 256, name); |
| 93 |
|
| 94 |
hid_t type_id; |
| 95 |
type_id = H5Aget_type(attrib_id); |
| 96 |
/*type_class = H5Tget_native_type(type_id,H5T_DIR_ASCEND);*/ |
| 97 |
if (strcmp(name,"time")==0) { |
| 98 |
status = H5Aread(attrib_id,H5T_NATIVE_DOUBLE,time); |
| 99 |
} else if (strcmp(name,"number_of_particles")==0) { |
| 100 |
status = H5Aread(attrib_id,H5T_NATIVE_INT,npart); |
| 101 |
} else { |
| 102 |
if (debug) printf("DEBUG: unknown attribute %s \n",name); |
| 103 |
} |
| 104 |
|
| 105 |
if (status==HDF5_error) { |
| 106 |
printf(" ERROR reading attribute %s \n",name); |
| 107 |
} |
| 108 |
|
| 109 |
status = H5Aclose(attrib_id); |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* Now we need to get the number of data columns in the file |
| 114 |
* (from the number of datasets in the "attributes" group) |
| 115 |
*/ |
| 116 |
#if H5_VERSION_GE(1,8,0) |
| 117 |
group_id2 = H5Gopen2(group_id,"attributes",H5P_DEFAULT); |
| 118 |
#else |
| 119 |
group_id2 = H5Gopen(group_id,"attributes"); |
| 120 |
#endif |
| 121 |
if (group_id2 == HDF5_error) |
| 122 |
{ printf("ERROR opening %s data set \n","attributes"); *ierr = 2; return; } |
| 123 |
|
| 124 |
hsize_t ndatasets; |
| 125 |
status = H5Gget_num_objs(group_id2, &ndatasets); |
| 126 |
if (debug) printf("DEBUG: number of datasets = %i \n",(int)ndatasets); |
| 127 |
|
| 128 |
*ncol = (int)ndatasets; |
| 129 |
int idim; |
| 130 |
|
| 131 |
/* check that coordinates are present in file */ |
| 132 |
idim = get_rank_by_name(group_id2,"x"); |
| 133 |
if (idim <= 0) { printf("ERROR: x positions not found\n"); *ierr = 3; } |
| 134 |
|
| 135 |
idim = get_rank_by_name(group_id2,"y"); |
| 136 |
if (idim <= 0) { printf("ERROR: y positions not found\n"); *ierr = 3; } |
| 137 |
|
| 138 |
idim = get_rank_by_name(group_id2,"z"); |
| 139 |
if (idim <= 0) { |
| 140 |
printf("z positions not found, assuming file is 2D \n"); |
| 141 |
*ndim = 2; |
| 142 |
*ndimV = 2; |
| 143 |
} else { |
| 144 |
*ndim = 3; |
| 145 |
*ndimV = 3; |
| 146 |
} |
| 147 |
|
| 148 |
/* finish, close all open datasets and close file */ |
| 149 |
|
| 150 |
status = H5Gclose(group_id2); |
| 151 |
if (status == HDF5_error) |
| 152 |
{ printf("ERROR closing attributes data set \n"); *ierr = 3; return; } |
| 153 |
|
| 154 |
status = H5Gclose(group_id); |
| 155 |
if (status == HDF5_error) |
| 156 |
{ printf("ERROR closing %s data set \n",maingroup); *ierr = 3; return; } |
| 157 |
|
| 158 |
status = H5Gclose(group_id1); |
| 159 |
if (status == HDF5_error) |
| 160 |
{ printf("ERROR closing 0001 data set \n"); *ierr = 3; return; } |
| 161 |
|
| 162 |
status = H5Fclose( file_id ); |
| 163 |
if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; } |
| 164 |
if (debug) printf("DEBUG: finished header read \n"); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
void read_amuse_hdf5_data(char *filename, |
| 169 |
int maxtypes, |
| 170 |
int npartoftype[maxtypes], |
| 171 |
int ncol, |
| 172 |
int isrequired[ncol], |
| 173 |
int *ierr) |
| 174 |
{ |
| 175 |
hid_t file_id; |
| 176 |
hid_t group_id, group_id1, group_id2; |
| 177 |
herr_t status; |
| 178 |
herr_t HDF5_error = -1; |
| 179 |
char groupname[12]; |
| 180 |
char datasetname[256]; |
| 181 |
int i; |
| 182 |
|
| 183 |
if (debug) printf("DEBUG: re-opening %s \n",filename); |
| 184 |
file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); |
| 185 |
if (file_id == HDF5_error) |
| 186 |
{ printf("ERROR re-opening %s \n",filename); *ierr = 1; return; } |
| 187 |
|
| 188 |
/* open main particles group */ |
| 189 |
char *maingroup = "particles"; |
| 190 |
#if H5_VERSION_GE(1,8,0) |
| 191 |
group_id1 = H5Gopen2(file_id,maingroup,H5P_DEFAULT); |
| 192 |
#else |
| 193 |
group_id1 = H5Gopen(file_id,maingroup); |
| 194 |
#endif |
| 195 |
if (debug) printf("DEBUG: maxtypes = %i\n",maxtypes); |
| 196 |
|
| 197 |
/* read dataset for each particle type present in dump file */ |
| 198 |
int itype,iobjtype; |
| 199 |
for (itype=0;itype<maxtypes;itype++) { |
| 200 |
if (debug) printf("DEBUG: type %i npartoftype = %i\n",itype,npartoftype[itype]); |
| 201 |
if (npartoftype[itype] > 0) { |
| 202 |
/* If npartoftype[N] > 0 in header, look for dataset of the form 000000000N */ |
| 203 |
sprintf(groupname,"00000000%02i",itype+1); |
| 204 |
if (debug) printf("DEBUG: opening group %s\n",groupname); |
| 205 |
#if H5_VERSION_GE(1,8,0) |
| 206 |
group_id = H5Gopen2(group_id1,groupname,H5P_DEFAULT); |
| 207 |
#else |
| 208 |
group_id = H5Gopen(group_id1,groupname); |
| 209 |
#endif |
| 210 |
if (group_id == HDF5_error) |
| 211 |
{ printf("ERROR opening %s group \n",groupname); *ierr = 2; } |
| 212 |
else { |
| 213 |
|
| 214 |
#if H5_VERSION_GE(1,8,0) |
| 215 |
group_id2 = H5Gopen2(group_id,"attributes",H5P_DEFAULT); |
| 216 |
#else |
| 217 |
group_id2 = H5Gopen(group_id,"attributes"); |
| 218 |
#endif |
| 219 |
if (group_id2 == HDF5_error) |
| 220 |
{ printf("ERROR opening attributes group \n"); *ierr = 2; } |
| 221 |
else { |
| 222 |
|
| 223 |
hsize_t ndatasets; |
| 224 |
status = H5Gget_num_objs(group_id2, &ndatasets); |
| 225 |
if (debug) printf("DEBUG: number of datasets = %i \n",(int)ndatasets); |
| 226 |
|
| 227 |
int j = 0; |
| 228 |
/* always read particle positions first */ |
| 229 |
*ierr = read_amuse_hdf5_dataset(group_id2,"x",itype,maxtypes,npartoftype,ncol,isrequired,&j); |
| 230 |
j = 1; |
| 231 |
*ierr = read_amuse_hdf5_dataset(group_id2,"y",itype,maxtypes,npartoftype,ncol,isrequired,&j); |
| 232 |
j = 2; |
| 233 |
*ierr = read_amuse_hdf5_dataset(group_id2,"z",itype,maxtypes,npartoftype,ncol,isrequired,&j); |
| 234 |
|
| 235 |
/* read remaining datasets in the order they appear in the file */ |
| 236 |
for(i=0; i < (int)ndatasets; i++) { |
| 237 |
status = H5Gget_objname_by_idx(group_id2, i, datasetname, 256); |
| 238 |
iobjtype = H5Gget_objtype_by_idx(group_id2, i); |
| 239 |
if (strcmp(datasetname,"x")&& |
| 240 |
strcmp(datasetname,"y")&& |
| 241 |
strcmp(datasetname,"z")&& (iobjtype == H5G_DATASET)) { |
| 242 |
*ierr = read_amuse_hdf5_dataset(group_id2,datasetname,itype,maxtypes,npartoftype,ncol,isrequired,&j); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/* close "attributes" group */ |
| 247 |
H5Gclose(group_id2); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
H5Gclose(group_id); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
H5Gclose(group_id1); |
| 256 |
|
| 257 |
status = H5Fclose( file_id ); |
| 258 |
if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; } |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
int read_amuse_hdf5_dataset(hid_t group_id, |
| 263 |
char *datasetname, |
| 264 |
int itype, |
| 265 |
int maxtypes, |
| 266 |
int npartoftype[maxtypes], |
| 267 |
int ncol, |
| 268 |
int isrequired[ncol], |
| 269 |
int *j) |
| 270 |
{ |
| 271 |
hid_t dataset_id, dataspace_id, memspace_id; |
| 272 |
herr_t status; |
| 273 |
herr_t HDF5_error = -1; |
| 274 |
int ierr = 0; |
| 275 |
char name[256]; |
| 276 |
|
| 277 |
if (!checkfordataset(group_id,datasetname)) { ierr = 1; return ierr; } |
| 278 |
|
| 279 |
#if H5_VERSION_GE(1,8,0) |
| 280 |
dataset_id = H5Dopen2(group_id,datasetname,H5P_DEFAULT); |
| 281 |
#else |
| 282 |
dataset_id = H5Dopen(group_id,datasetname); |
| 283 |
#endif |
| 284 |
dataspace_id = H5Dget_space(dataset_id); |
| 285 |
int rank = get_rank(dataspace_id); |
| 286 |
int k, flag; |
| 287 |
|
| 288 |
/* do nothing if none of the columns are required */ |
| 289 |
flag = 0; |
| 290 |
for (k=0;k<rank;k++) { |
| 291 |
if (isrequired[k]) flag = 1; |
| 292 |
} |
| 293 |
if (!flag) { |
| 294 |
if (debug) printf("DEBUG: skipping %s : not required\n",datasetname); |
| 295 |
H5Dclose(dataset_id); |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
if (debug) printf("DEBUG: got %s rank %i \n",datasetname,rank); |
| 300 |
/* make a temporary array to put each column as we read it */ |
| 301 |
hsize_t nparttmp[1]; |
| 302 |
nparttmp[0] = npartoftype[itype]; |
| 303 |
memspace_id = H5Screate_simple(1,nparttmp,NULL); |
| 304 |
double *temp = 0; |
| 305 |
temp = malloc(npartoftype[itype]*sizeof(double)); |
| 306 |
|
| 307 |
if (rank==1) { |
| 308 |
*j = *j + 1; |
| 309 |
|
| 310 |
/* read column from file */ |
| 311 |
H5Dread(dataset_id,H5T_NATIVE_DOUBLE,memspace_id,dataspace_id,H5P_DEFAULT,temp); |
| 312 |
|
| 313 |
/* call Fortran back, sending values in temp array to fill into the main splash dat array */ |
| 314 |
read_amuse_hdf5_data_fromc(j,&npartoftype[itype],temp,&itype); |
| 315 |
|
| 316 |
strcpy(name,datasetname); |
| 317 |
set_blocklabel(j,name); |
| 318 |
|
| 319 |
} else { |
| 320 |
hsize_t offset[2], count[2]; |
| 321 |
|
| 322 |
for (k=1;k<=rank;k++) { |
| 323 |
*j = *j + 1; |
| 324 |
count[0] = npartoftype[itype]; |
| 325 |
count[1] = 1; |
| 326 |
offset[0] = 0; |
| 327 |
offset[1] = k-1; /* rank */ |
| 328 |
status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL); |
| 329 |
if (status == HDF5_error) { printf("ERROR creating hyperslab \n"); ierr = 4; } |
| 330 |
if (!H5Sselect_valid(dataspace_id)) { printf("ERROR selecting hyperslab \n"); ierr = 5; } |
| 331 |
|
| 332 |
/* read column from file */ |
| 333 |
if (isrequired[*j]) { |
| 334 |
H5Dread(dataset_id,H5T_NATIVE_DOUBLE,memspace_id,dataspace_id,H5P_DEFAULT,temp); |
| 335 |
|
| 336 |
/* call Fortran back, sending values in temp array to fill into the main splash dat array */ |
| 337 |
read_amuse_hdf5_data_fromc(j,&npartoftype[itype],temp,&itype); |
| 338 |
} |
| 339 |
strcpy(name,datasetname); |
| 340 |
set_blocklabel(j,name); |
| 341 |
} |
| 342 |
} |
| 343 |
free(temp); |
| 344 |
|
| 345 |
status = H5Sclose(dataspace_id); |
| 346 |
if (status == HDF5_error) { printf("ERROR closing dataspace \n"); ierr = 4; } |
| 347 |
|
| 348 |
H5Dclose(dataset_id); |
| 349 |
if (status == HDF5_error) { printf("ERROR closing dataset \n"); ierr = 7; } |
| 350 |
return ierr; |
| 351 |
} |
| 352 |
|
| 353 |
|