Page MenuHomec4science

hdf5io.c
No OneTemporary

File Metadata

Created
Mon, Jun 10, 22:02

hdf5io.c

#include <hdf5.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "hdf5io.h"
/********************************************************
High level hdf5 level functions
*********************************************************/
/*******************************
read Attribute
*******************************/
char * readAttributeAsString(hid_t group,char *name)
{
hid_t attr;
hid_t memtype;
herr_t status;
char **string;
char *output;
int l;
attr = H5Aopen( group , name, H5P_DEFAULT);
string = (char **) malloc (1 * sizeof (char *));
memtype = H5Tcopy (H5T_C_S1);
status = H5Tset_size(memtype, H5T_VARIABLE);
status = H5Aread (attr, memtype,string);
//printf ("%s = %s\n",name, string[0]);
/* copy to output */
l = strlen(string[0])+1;
output = (char *) malloc(l*sizeof(char));
memcpy(output, string[0], l);
free(string);
status = H5Aclose(attr);
return output;
}
int readAttributeAsInt(hid_t group,char *name)
{
hid_t attr;
herr_t status;
long output;
attr = H5Aopen( group , name, H5P_DEFAULT);
status = H5Aread(attr,H5T_STD_I64LE,&output);
status = H5Aclose(attr);
return (int)output;
}
double readAttributeAsDouble(hid_t group,char *name)
{
hid_t attr;
herr_t status;
double output;
attr = H5Aopen( group , name, H5P_DEFAULT);
status = H5Aread(attr,H5T_IEEE_F64LE,&output);
status = H5Aclose(attr);
return output;
}
char ** readAttributeAsArrayString(hid_t group,char *name)
{
hid_t attr;
hid_t filetype;
hid_t space;
hid_t memtype;
herr_t status;
size_t sdim;
hsize_t dims[1];
int ndims;
int i;
char **rdata;
char **output;
attr = H5Aopen( group , name, H5P_DEFAULT);
filetype = H5Aget_type (attr);
sdim = H5Tget_size (filetype);
sdim++; /* Make room for null terminator */
space = H5Aget_space (attr);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
rdata = (char **) malloc (dims[0] * sizeof (char *));
rdata[0] = (char *) malloc (dims[0] * sdim * sizeof (char));
for (i=1; i<dims[0]; i++)
rdata[i] = rdata[0] + i * sdim;
memtype = H5Tcopy (H5T_C_S1);
status = H5Tset_size (memtype, sdim);
status = H5Aread (attr, memtype, rdata[0]);
//for (i=0; i<dims[0]; i++)
// printf (">>[%d]: %s<<\n", i, rdata[i]);
/* copy to output */
output = (char **) malloc (dims[0] * sizeof (char *));
output[0] = (char *) malloc (dims[0] * sdim * sizeof (char));
for (i=1; i<dims[0]; i++)
output[i] = output[0] + i * sdim;
for (i=0; i<dims[0]; i++)
memcpy(output[i], rdata[i], sdim);
free (rdata[0]);
free (rdata);
status = H5Aclose (attr);
status = H5Sclose (space);
return output;
}
double * readAttributeAsArrayDouble(hid_t group,char *name)
{
hid_t attr;
hid_t space;
herr_t status;
hsize_t dims[1];
int ndims;
double *rdata;
double *output;
attr = H5Aopen( group , name, H5P_DEFAULT);
space = H5Aget_space (attr);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
rdata = (double *) malloc (dims[0] * sizeof (double *));
status = H5Aread (attr, H5T_NATIVE_DOUBLE, rdata);
//for (i=0; i<dims[0]; i++)
// printf (">>[%d]: %g<<\n", i, rdata[i]);
/* copy to output */
output = (double *) malloc (dims[0] * sizeof (double *));
memcpy(output, rdata, dims[0]* sizeof (double));
free (rdata);
status = H5Aclose (attr);
status = H5Sclose (space);
return output;
}
double ** readAttributeAsArray2dDouble(hid_t group,char *name)
{
hid_t attr;
hid_t space;
herr_t status;
hsize_t dims[2];
int ndims;
int i;
double **rdata;
double **output;
attr = H5Aopen( group , name, H5P_DEFAULT);
space = H5Aget_space (attr);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
rdata = (double **) malloc (dims[0] * sizeof (double *));
rdata[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double));
for (i=1; i<dims[0]; i++)
rdata[i] = rdata[0] + i * dims[1];
status = H5Aread (attr, H5T_NATIVE_DOUBLE, rdata[0]);
//for (i=0; i<dims[0]; i++)
// printf (">>[%d]: %g<<\n", i, rdata[i]);
/* copy to output */
output = (double **) malloc (dims[0] * sizeof (double *));
output[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double));
for (i=1; i<dims[0]; i++)
output[i] = output[0] + i * dims[1];
for (i=0; i<dims[0]; i++)
memcpy(output[i], rdata[i], dims[1]);
free (rdata[0]);
free (rdata);
status = H5Aclose (attr);
status = H5Sclose (space);
return output;
}
/*******************************
read Dataset
*******************************/
int readDataSetAsInt(hid_t group,char *name)
{
hid_t attr;
herr_t status;
long output;
attr = H5Dopen( group , name, H5P_DEFAULT);
//status = H5Dread(attr,H5T_STD_I64LE,H5S_ALL, H5S_ALL, H5P_DEFAULT,&output);
status = H5Dread(attr,H5T_NATIVE_INT,H5S_ALL, H5S_ALL, H5P_DEFAULT,&output);
status = H5Dclose(attr);
return (int)output;
}
double readDataSetAsDouble(hid_t group,char *name)
{
hid_t attr;
herr_t status;
double output;
attr = H5Dopen( group , name, H5P_DEFAULT);
status = H5Dread(attr,H5T_IEEE_F64LE,H5S_ALL, H5S_ALL, H5P_DEFAULT,&output);
status = H5Dclose(attr);
return output;
}
double * readDatasetAsArrayDouble(hid_t group,char *name)
{
hid_t dset;
hid_t space;
herr_t status;
hsize_t dims[1];
int ndims;
double *rdata;
double *output;
dset = H5Dopen( group , name, H5P_DEFAULT);
space = H5Dget_space (dset);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
rdata = (double *) malloc (dims[0] * sizeof (double *));
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,rdata);
//for (i=0; i<dims[0]; i++)
// printf (">>[%d]: %g<<\n", i, rdata[i]);
/* copy to output */
output = (double *) malloc (dims[0] * sizeof (double *));
memcpy(output, rdata, dims[0]* sizeof (double));
free (rdata);
status = H5Dclose (dset);
status = H5Sclose (space);
return output;
}
double ** readDatasetAsArray2DDouble_v0(hid_t group,char *name)
{
hid_t dset;
hid_t space;
herr_t status;
hsize_t dims[2];
int ndims;
int i,j;
double **rdata;
double **output;
dset = H5Dopen( group , name, H5P_DEFAULT);
space = H5Dget_space (dset);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
rdata = (double **) malloc (dims[0] * sizeof (double *));
rdata[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double));
for (i=1; i<dims[0]; i++)
rdata[i] = rdata[0] + i * dims[1];
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]);
//for (i=0; i<dims[0]; i++) {
// printf (" [");
// for (j=0; j<dims[1]; j++)
// printf (" %6.4f", rdata[i][j]);
// printf ("]\n");
//}
/* copy to output */
output = (double **) malloc (dims[0] * sizeof (double *));
output[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double));
for (i=1; i<dims[0]; i++)
output[i] = output[0] + i * dims[1];
memcpy(output[0], rdata[0], dims[0] * dims[1] * sizeof (double));
free (rdata[0]);
free (rdata);
status = H5Dclose (dset);
status = H5Sclose (space);
return output;
}
double ** old_readDatasetAsArray2dDouble(hid_t group,char *name,int *idims)
{
hid_t dset;
hid_t space;
herr_t status;
hsize_t dims[2];
int ndims;
int i;
double **rdata;
double **output;
dset = H5Dopen( group , name, H5P_DEFAULT);
space = H5Dget_space (dset);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
rdata = (double **) malloc (dims[0] * sizeof (double *));
rdata[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double));
for (i=1; i<dims[0]; i++)
rdata[i] = rdata[0] + i * dims[1];
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]);
//for (i=0; i<dims[0]; i++) {
// printf (" [");
// for (j=0; j<dims[1]; j++)
// printf (" %6.4f", rdata[i][j]);
// printf ("]\n");
//}
/* copy to output */
output = (double **) malloc (dims[0] * sizeof (double *));
output[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double));
for (i=1; i<dims[0]; i++)
output[i] = output[0] + i * dims[1];
memcpy(output[0], rdata[0], dims[0] * dims[1] * sizeof (double));
free (rdata[0]);
free (rdata);
status = H5Dclose (dset);
status = H5Sclose (space);
idims[0]=(int)dims[0];
idims[1]=(int)dims[1];
return output;
}
double *** old_readDatasetAsArray3dDouble(hid_t group,char *name,int *idims)
{
hid_t dset;
hid_t space;
herr_t status;
hsize_t dims[3];
int ndims;
int i,j,k;
//double ***rdata;
double ***output=NULL;
dset = H5Dopen( group , name, H5P_DEFAULT);
space = H5Dget_space (dset);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
/*
rdata = (double ***) malloc (dims[0] * sizeof (double **));
for (i=0; i<dims[0]; i++)
{
rdata[i] = (double **) malloc (dims[1] * sizeof (double *));
for (j=0; j<dims[1]; j++)
rdata[i][j] = (double *) malloc(dims[2]*sizeof(double));
}
*/
/*
rdata = (double ***) malloc (dims[0] * sizeof (double **));
for(i=0;i<dims[0];i++)
{
rdata[i] = (double **) malloc (dims[1] * sizeof (double *));
rdata[i][0] = (double *) malloc (dims[1] * dims[2] * sizeof (double));
//for (j=1; j<dims[1]; j++)
// rdata[i][j] = rdata[0] + j * dims[1];
}
for(i=0;i<dims[0];i++)
for (j=1; j<dims[1]; j++)
for (j=1; j<dims[1]; j++)
rdata[i][j] = rdata[0] + j * (dims[0]*dims[1]) + k * dims[2];
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]);
*/
double buf[dims[0]][dims[1]][dims[2]];
//double *buf;
//buf = (double *) malloc (dims[0]*dims[1]*dims[2] * sizeof (double));
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
double** temp1;
double* temp2;
temp1 = (double**)malloc(dims[0]*dims[1]*sizeof(double*));
temp2 = (double*)malloc(dims[0]*dims[1]*dims[2]*sizeof(double));
for(i=0, j=0; i<dims[0]*dims[1]; i++, j++){
temp1[i] = &(temp2[j*dims[2]]);
}
output = (double***)malloc(dims[0]*sizeof(double**));
for(i=0, j=0; j<dims[0]; i++, j++){
output[i] = &(temp1[j*dims[1]]);
}
for(i=0; i<dims[0]; i++){
for(j=0; j<dims[1]; j++){
for(k=0; k<dims[2]; k++){
output[i][j][k] = buf[i][j][k];
}
}
}
//for (i=0; i<dims[0]; i++) {
// printf (" [");
// for (j=0; j<dims[1]; j++)
// printf (" %6.4f", rdata[i][j]);
// printf ("]\n");
//}
//free (rdata[0]);
//free (rdata);
status = H5Dclose (dset);
status = H5Sclose (space);
idims[0]=(int)dims[0];
idims[1]=(int)dims[1];
idims[2]=(int)dims[2];
return output;
}
int readDatasetAsArray2dDouble(hid_t group,char *name,int *idims,double output[MAXX2D][MAXY2D])
{
hid_t dset;
hid_t space;
herr_t status;
hsize_t dims[2];
int ndims;
int i,j;
//double **rdata;
//double **output;
dset = H5Dopen( group , name, H5P_DEFAULT);
space = H5Dget_space (dset);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
double buf[dims[0]][dims[1]];
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
for(i=0; i<dims[0]; i++){
for(j=0; j<dims[1]; j++){
output[i][j] = buf[i][j];
}
}
//for (i=0; i<dims[0]; i++) {
// printf (" [");
// for (j=0; j<dims[1]; j++)
// printf (" %6.4f", rdata[i][j]);
// printf ("]\n");
//}
//free (rdata[0]);
//free (rdata);
status = H5Dclose (dset);
status = H5Sclose (space);
idims[0]=(int)dims[0];
idims[1]=(int)dims[1];
return 0;
}
int readDatasetAsArray3dDouble(hid_t group,char *name,int *idims, double output[MAXX3D][MAXY3D][MAXZ3D])
{
hid_t dset;
hid_t space;
herr_t status;
hsize_t dims[3];
int ndims;
int i,j,k;
//double ***rdata;
//double ***output=NULL;
dset = H5Dopen( group , name, H5P_DEFAULT);
space = H5Dget_space (dset);
ndims = H5Sget_simple_extent_dims (space, dims, NULL);
/*
rdata = (double ***) malloc (dims[0] * sizeof (double **));
for (i=0; i<dims[0]; i++)
{
rdata[i] = (double **) malloc (dims[1] * sizeof (double *));
for (j=0; j<dims[1]; j++)
rdata[i][j] = (double *) malloc(dims[2]*sizeof(double));
}
*/
/*
rdata = (double ***) malloc (dims[0] * sizeof (double **));
for(i=0;i<dims[0];i++)
{
rdata[i] = (double **) malloc (dims[1] * sizeof (double *));
rdata[i][0] = (double *) malloc (dims[1] * dims[2] * sizeof (double));
//for (j=1; j<dims[1]; j++)
// rdata[i][j] = rdata[0] + j * dims[1];
}
for(i=0;i<dims[0];i++)
for (j=1; j<dims[1]; j++)
for (j=1; j<dims[1]; j++)
rdata[i][j] = rdata[0] + j * (dims[0]*dims[1]) + k * dims[2];
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]);
*/
double buf[dims[0]][dims[1]][dims[2]];
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
for(i=0; i<dims[0]; i++){
for(j=0; j<dims[1]; j++){
for(k=0; k<dims[2]; k++){
output[i][j][k] = buf[i][j][k];
}
}
}
//for (i=0; i<dims[0]; i++) {
// printf (" [");
// for (j=0; j<dims[1]; j++)
// printf (" %6.4f", rdata[i][j]);
// printf ("]\n");
//}
//free (rdata[0]);
//free (rdata);
status = H5Dclose (dset);
status = H5Sclose (space);
idims[0]=(int)dims[0];
idims[1]=(int)dims[1];
idims[2]=(int)dims[2];
return 0;
}
/*******************************
read group
*******************************/
struct ElementsDataStruct readGroupAsElementsData(hid_t parent,char *name)
{
struct ElementsDataStruct Edata;
hid_t group;
herr_t status;
group = H5Gopen( parent , name, H5P_DEFAULT);
Edata.nelts = readAttributeAsInt(group,"nelts");
Edata.elts = readAttributeAsArrayString(group,"elts");
Edata.data = readAttributeAsArrayDouble(group,"data");
status = H5Gclose (group);
return Edata;
}

Event Timeline