H5Object & H5ReferenceData::getData(const unsigned int size, const unsigned int * index) const { char * cdata = static_cast<char *>(data) + offset; void ** ref = 0; unsigned int pos = 0; unsigned int cumprod = 1; H5O_info_t info; H5Object * hobj; hid_t obj; hid_t file; ssize_t ssize; char * name = 0; std::string _name; for (unsigned int i = 0; i < size; i++) { pos += cumprod * index[i]; } if (pos >= totalSize) { throw H5Exception(__LINE__, __FILE__, _("Invalid index.")); } cdata += pos * (stride ? stride : dataSize); file = getFile().getH5Id(); ref = &(((void **)cdata)[0]); obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref); if (obj < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open object at the given position.")); } ssize = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0); name = new char[ssize + 1]; H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, ssize + 1); _name = std::string(name); delete[] name; H5Oget_info(obj, &info); switch (info.type) { case H5O_TYPE_GROUP: hobj = new H5Group(getParent(), obj, _name); break; case H5O_TYPE_DATASET: hobj = new H5Dataset(getParent(), obj, _name); break; case H5O_TYPE_NAMED_DATATYPE: hobj = new H5Type(getParent(), obj, _name); break; case H5O_TYPE_UNKNOWN: default: H5Oclose(obj); throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object")); } return *hobj; }
/* * Class: hdf_hdf5lib_H5 * Method: H5Rget_name * Signature: (JI[B[Ljava/lang/String;J)J */ JNIEXPORT jlong JNICALL Java_hdf_hdf5lib_H5_H5Rget_1name(JNIEnv *env, jclass clss, jlong loc_id, jint ref_type, jbyteArray ref, jobjectArray name, jlong size) { jlong ret_val = -1; jbyte *refP; jboolean isCopy; char *aName = NULL; jstring str; size_t bs; bs = (size_t)size; if (bs <= 0) { h5badArgument(env, "H5Rget_name: size <= 0"); } /* end if */ else if (ref == NULL) { h5nullArgument(env, "H5Rget_name: ref is NULL"); } /* end else if */ else { if ((ref_type == H5R_OBJECT) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_OBJ_REF_BUF_SIZE) { h5badArgument(env, "H5Rget_name: obj ref input array != H5R_OBJ_REF_BUF_SIZE"); } /* end if */ else if ((ref_type == H5R_DATASET_REGION) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_DSET_REG_REF_BUF_SIZE) { h5badArgument(env, "H5Rget_name: region ref input array != H5R_DSET_REG_REF_BUF_SIZE"); } /* end else if */ else { refP = (jbyte *)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy); if (refP == NULL) { h5JNIFatalError(env, "H5Rget_name: ref not pinned"); } /* end if */ else { aName = (char*)HDmalloc(sizeof(char)*bs); if (aName == NULL) { ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT); h5outOfMemory(env, "H5Rget_name: malloc failed"); } /* end if */ else { ret_val = (jlong)H5Rget_name((hid_t)loc_id, (H5R_type_t)ref_type, refP, aName, bs) ; ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT); if (ret_val < 0) { HDfree(aName); h5libraryError(env); } /* end if */ else { str = ENVPTR->NewStringUTF(ENVPAR aName); ENVPTR->SetObjectArrayElement(ENVPAR name, 0, str); HDfree(aName); } /* end else */ } /* end else */ } /* end else */ } /* end else */ } /* end else */ return ret_val; } /* end Java_hdf_hdf5lib_H5_H5Rget_1name */
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; } }
int main(void) { hid_t file_id; /* file identifier */ hid_t space_id; /* dataspace identifiers */ hid_t spacer_id; hid_t dsetv_id; /*dataset identifiers*/ hid_t dsetr_id; hsize_t dims[2] = {2,9}; hsize_t dimsr[1] = {2}; int rank = 2; int rankr =1; herr_t status; hdset_reg_ref_t ref[2]; hdset_reg_ref_t ref_out[2]; int data[2][9] = {{1,1,2,3,3,4,5,5,6},{1,2,2,3,4,4,5,6,6}}; int data_out[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}}; hsize_t start[2]; hsize_t count[2]; hsize_t coord[2][3] = {{0, 0, 1}, {6, 0, 8}}; unsigned num_points = 3; int i, j; size_t name_size1, name_size2; char buf1[10], buf2[10]; /* * Create file with default file access and file creation properties. */ file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataspace for datasets. */ space_id = H5Screate_simple(rank, dims, NULL); spacer_id = H5Screate_simple(rankr, dimsr, NULL); /* * Create integer dataset. */ dsetv_id = H5Dcreate2(file_id, dsetnamev, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Write data to the dataset. */ status = H5Dwrite(dsetv_id, H5T_NATIVE_INT, H5S_ALL , H5S_ALL, H5P_DEFAULT,data); status = H5Dclose(dsetv_id); /* * Dataset with references. */ dsetr_id = H5Dcreate2(file_id, dsetnamer, H5T_STD_REF_DSETREG, spacer_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create a reference to the hyperslab. */ start[0] = 0; start[1] = 3; count[0] = 2; count[1] = 3; status = H5Sselect_hyperslab(space_id, H5S_SELECT_SET, start, NULL, count, NULL); status = H5Rcreate(&ref[0], file_id, dsetnamev, H5R_DATASET_REGION, space_id); /* * Create a reference to elements selection. */ status = H5Sselect_none(space_id); status = H5Sselect_elements(space_id, H5S_SELECT_SET, num_points, (const hsize_t *)coord); status = H5Rcreate(&ref[1], file_id, dsetnamev, H5R_DATASET_REGION, space_id); /* * Write dataset with the references. */ status = H5Dwrite(dsetr_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT,ref); /* * Close all objects. */ status = H5Sclose(space_id); status = H5Sclose(spacer_id); status = H5Dclose(dsetr_id); status = H5Fclose(file_id); /* * Reopen the file to read selections back. */ file_id = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT); /* * Reopen the dataset with object references and read references * to the buffer. */ dsetr_id = H5Dopen2(file_id, dsetnamer, H5P_DEFAULT); status = H5Dread(dsetr_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, ref_out); /* * Dereference the first reference. */ dsetv_id = H5Rdereference2(dsetr_id, H5P_DEFAULT, H5R_DATASET_REGION, &ref_out[0]); /* * Get name of the dataset the first region reference points to * using H5Rget_name */ name_size1 = H5Rget_name(dsetr_id, H5R_DATASET_REGION, &ref_out[0], (char*)buf1, 10); printf(" Dataset's name (returned by H5Rget_name) the reference points to is %s, name length is %d\n", buf1, (int)name_size1); /* * Get name of the dataset the first region reference points to * using H5Iget_name */ name_size2 = H5Iget_name(dsetv_id, (char*)buf2, 10); printf(" Dataset's name (returned by H5Iget_name) the reference points to is %s, name length is %d\n", buf2, (int)name_size2); space_id = H5Rget_region(dsetr_id, H5R_DATASET_REGION,&ref_out[0]); /* * Read and display hyperslab selection from the dataset. */ status = H5Dread(dsetv_id, H5T_NATIVE_INT, H5S_ALL, space_id, H5P_DEFAULT, data_out); printf("Selected hyperslab: "); for (i = 0; i <= 1; i++) { printf("\n"); for (j = 0; j <= 8; j++) printf("%d ", data_out[i][j]); } printf("\n"); /* * Close dataspace and the dataset. */ status = H5Sclose(space_id); status = H5Dclose(dsetv_id); /* * Initialize data_out array again to get point selection. */ for (i = 0; i <= 1; i++) for (j = 0; j <= 8; j++) data_out[i][j] = 0; /* * Dereference the second reference. */ dsetv_id = H5Rdereference2(dsetr_id, H5P_DEFAULT, H5R_DATASET_REGION, &ref_out[1]); space_id = H5Rget_region(dsetv_id, H5R_DATASET_REGION,&ref_out[1]); /* * Read selected data from the dataset. */ status = H5Dread(dsetv_id, H5T_NATIVE_INT, H5S_ALL, space_id, H5P_DEFAULT, data_out); printf("Selected points: "); for (i = 0; i <= 1; i++) { printf("\n"); for (j = 0; j <= 8; j++) printf("%d ", data_out[i][j]); } printf("\n"); /* * Close dataspace and the dataset. */ status = H5Sclose(space_id); status = H5Dclose(dsetv_id); status = H5Dclose(dsetr_id); status = H5Fclose(file_id); return 0; }