int_f nh5sget_select_elem_pointlist_c( hid_t_f *space_id ,hsize_t_f * startpoint, hsize_t_f * numpoints, hsize_t_f * buf) { int ret_value = -1; hid_t c_space_id; hsize_t c_num_points; hsize_t c_startpoint,* c_buf; int i, rank; c_space_id = *space_id; c_num_points = (hsize_t)* numpoints; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0 ) return ret_value; c_startpoint = (hsize_t)*startpoint; c_buf = (hsize_t*)malloc(sizeof(hsize_t)*(size_t)(c_num_points*rank)); if (!c_buf) return ret_value; ret_value = H5Sget_select_elem_pointlist(c_space_id, c_startpoint, c_num_points, c_buf); for (i = c_num_points*rank-1; i >= 0; i--) { buf[i] = (hsize_t_f)(c_buf[i]+1); } if (ret_value >= 0 ) ret_value = 0; HDfree(c_buf); return ret_value; }
int_f nh5sget_select_elem_pointlist_c( hid_t_f *space_id ,hsize_t_f * startpoint, hsize_t_f * numpoints, hsize_t_f * buf) { int ret_value = -1; hid_t c_space_id; hsize_t c_num_points; hsize_t c_startpoint,* c_buf; hsize_t i, i1; int rank; int j,i2; c_space_id = *space_id; c_num_points = (hsize_t)* numpoints; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0 ) return ret_value; c_startpoint = (hsize_t)*startpoint; c_buf = (hsize_t*)malloc(sizeof(hsize_t)*(size_t)(c_num_points*rank)); if (!c_buf) return ret_value; ret_value = H5Sget_select_elem_pointlist(c_space_id, c_startpoint, c_num_points, c_buf); /* re-arrange the return buffer to account for Fortran ordering of 2D arrays */ /* and add 1 to account for array's starting at one in Fortran */ i2 = 0; for( i = 0; i < c_num_points; i++) { i1 = rank*(i+1); for(j = 0; j < rank; j++) { buf[i2] = (hsize_t_f)(c_buf[i1-1]+1); i2 = i2 + 1; i1 = i1 - 1; } } if (ret_value >= 0 ) ret_value = 0; HDfree(c_buf); return ret_value; }
/*------------------------------------------------------------------------- * Function: h5tools_str_dump_region_points * * Purpose: Prints information about a dataspace region by appending * the information to the specified string. * * Return: none * * In/Out: * h5tools_context_t *ctx * h5tools_str_t *str *------------------------------------------------------------------------- */ void h5tools_str_dump_region_points(h5tools_str_t *str, hid_t region, const h5tool_format_t *info, h5tools_context_t *ctx) { hssize_t npoints; hsize_t alloc_size; hsize_t *ptdata; int ndims = H5Sget_simple_extent_ndims(region); /* * This function fails if the region does not have points. */ H5E_BEGIN_TRY { npoints = H5Sget_select_elem_npoints(region); } H5E_END_TRY; /* Print point information */ if (npoints > 0) { int i; alloc_size = npoints * ndims * sizeof(ptdata[0]); assert(alloc_size == (hsize_t) ((size_t) alloc_size)); /*check for overflow*/ ptdata = malloc((size_t) alloc_size); H5_CHECK_OVERFLOW(npoints, hssize_t, hsize_t); H5Sget_select_elem_pointlist(region, (hsize_t)0, (hsize_t)npoints, ptdata); for (i = 0; i < npoints; i++) { int j; h5tools_str_append(str, info->dset_ptformat_pre, i ? "," OPTIONAL_LINE_BREAK " " : "", (unsigned long)i); for (j = 0; j < ndims; j++) h5tools_str_append(str, "%s%lu", j ? "," : "(", (unsigned long) (ptdata[i * ndims + j])); h5tools_str_append(str, ")"); } free(ptdata); } /* end if (npoints > 0) */ }
int main(void) { hid_t fid1; /* HDF5 File IDs */ hid_t dset1, /* Dataset ID */ dset2; /* Dereferenced dataset ID */ hid_t sid1, /* Dataspace ID #1 */ sid2; /* Dataspace ID #2 */ hsize_t * coords; /* Coordinate buffer */ hsize_t low[SPACE2_RANK]; /* Selection bounds */ hsize_t high[SPACE2_RANK]; /* Selection bounds */ hdset_reg_ref_t *rbuf; /* buffer to to read disk */ int *drbuf; /* Buffer for reading numeric data from disk */ int i, j; /* counting variables */ herr_t ret; /* Generic return value */ /* Output message about test being performed */ /* Allocate write & read buffers */ rbuf=malloc(sizeof(hdset_reg_ref_t)*SPACE1_DIM1); drbuf=calloc(sizeof(int),SPACE2_DIM1*SPACE2_DIM2); /* Open the file */ fid1 = H5Fopen(FILE2, H5F_ACC_RDWR, H5P_DEFAULT); /* Open the dataset */ dset1=H5Dopen(fid1,"/Dataset1"); /* Read selection from disk */ ret=H5Dread(dset1,H5T_STD_REF_DSETREG,H5S_ALL,H5S_ALL,H5P_DEFAULT,rbuf); /* Try to open objects */ dset2 = H5Rdereference(dset1,H5R_DATASET_REGION,&rbuf[0]); /* Check information in referenced dataset */ sid1 = H5Dget_space(dset2); ret=H5Sget_simple_extent_npoints(sid1); printf(" Number of elements in the dataset is : %d\n",ret); /* Read from disk */ ret=H5Dread(dset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,drbuf); for(i=0; i<SPACE2_DIM1; i++) { for (j=0; j<SPACE2_DIM2; j++) printf (" %d ", drbuf[i*SPACE2_DIM2+j]); printf("\n"); } /* Get the hyperslab selection */ sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[0]); /* Verify correct hyperslab selected */ ret = H5Sget_select_npoints(sid2); printf(" Number of elements in the hyperslab is : %d \n", ret); ret = H5Sget_select_hyper_nblocks(sid2); coords=malloc(ret*SPACE2_RANK*sizeof(hsize_t)*2); /* allocate space for the hyperslab blocks */ ret = H5Sget_select_hyper_blocklist(sid2,0,ret,coords); printf(" Hyperslab coordinates are : \n"); printf (" ( %lu , %lu ) ( %lu , %lu ) \n", \ (unsigned long)coords[0],(unsigned long)coords[1],(unsigned long)coords[2],(unsigned long)coords[3]); free(coords); ret = H5Sget_select_bounds(sid2,low,high); /* Close region space */ ret = H5Sclose(sid2); /* Get the element selection */ sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[1]); /* Verify correct elements selected */ ret = H5Sget_select_elem_npoints(sid2); printf(" Number of selected elements is : %d\n", ret); /* Allocate space for the element points */ coords= malloc(ret*SPACE2_RANK*sizeof(hsize_t)); ret = H5Sget_select_elem_pointlist(sid2,0,ret,coords); printf(" Coordinates of selected elements are : \n"); for (i=0; i<2*NPOINTS; i=i+2) printf(" ( %lu , %lu ) \n", (unsigned long)coords[i],(unsigned long)coords[i+1]); free(coords); ret = H5Sget_select_bounds(sid2,low,high); /* Close region space */ ret = H5Sclose(sid2); /* Close first space */ ret = H5Sclose(sid1); /* Close dereferenced Dataset */ ret = H5Dclose(dset2); /* Close Dataset */ ret = H5Dclose(dset1); /* Close file */ ret = H5Fclose(fid1); /* Free memory buffers */ free(rbuf); free(drbuf); return 0; }
void H5ReferenceData::printData(std::ostream & os, const unsigned int pos, const unsigned int indentLevel) const { char * cdata = static_cast<char *>(data) + offset + pos * (stride ? stride : dataSize); void ** ref = &(((void **)cdata)[0]); hid_t file = getFile().getH5Id(); hid_t obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref); if (obj < 0) { os << "NULL"; return; } ssize_t size = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0); char * name = 0; H5O_info_t info; if (size != -1) { name = new char[size + 1]; H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, size + 1); if (datasetReference == H5R_OBJECT) { H5Oget_info(obj, &info); H5Oclose(obj); switch (info.type) { case H5O_TYPE_GROUP: os << "GROUP "; break; case H5O_TYPE_DATASET: os << "DATASET "; break; case H5O_TYPE_NAMED_DATATYPE: os << "DATATYPE "; break; case H5O_TYPE_UNKNOWN: default: if (size != -1) { delete[] name; } throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object")); } os << (haddr_t)(*ref) << " " << name; } else { hid_t space = H5Rget_region(file, H5R_DATASET_REGION, ref); hssize_t npoints = H5Sget_select_elem_npoints(space); hsize_t ndims = H5Sget_simple_extent_dims(space, 0, 0); H5Oclose(obj); os << "DATASET " << name << " {"; if (npoints >= 0) { const hsize_t N = ndims * npoints; hsize_t * buf = new hsize_t[N]; herr_t err = H5Sget_select_elem_pointlist(space, 0, npoints, buf); for (hssize_t i = 0; i < (hssize_t)N; i += ndims) { os << "("; for (unsigned int j = 0; j < ndims - 1; j++) { os << buf[i + j] << ","; } os << buf[i + ndims - 1] << ")"; if (i != N - ndims) { os << ", "; } else { os << "}"; } } delete[] buf; } else { hssize_t nblocks = H5Sget_select_hyper_nblocks(space); if (nblocks >= 0) { const hsize_t N = 2 * ndims * nblocks; hsize_t * buf = new hsize_t[N]; herr_t err = H5Sget_select_hyper_blocklist(space, 0, nblocks, buf); for (hssize_t i = 0; i < (hssize_t)N; i += 2 * ndims) { os << "("; for (unsigned int j = 0; j < ndims - 1; j++) { os << buf[i + j] << ","; } os << buf[i + ndims - 1] << ")-("; for (unsigned int j = 0; j < ndims - 1; j++) { os << buf[i + ndims + j] << ","; } os << buf[i + 2 * ndims - 1] << ")"; if (i != N - 2 * ndims) { os << ", "; } else { os << "}"; } } delete[] buf; } } H5Sclose(space); } delete[] name; } }