/*---------------------------------------------------------------------------- * Name: h5rcreate_region_c * Purpose: Call H5Rcreate to create a reference to dataset region * region * Inputs: loc_id - file or group identifier * name - name of the dataset * namelen - name length * space_id - dataset space identifier * Outputs: ref - reference to the dataset region * Returns: 0 on success, -1 on failure * Programmer: Elena Pourmal * Wednesday, December 1, 1999 * Modifications: *---------------------------------------------------------------------------*/ int_f nh5rcreate_region_c (int_f *ref, hid_t_f *loc_id, _fcd name, int_f *namelen, hid_t_f *space_id) { int ret_value = -1; hid_t c_loc_id; hid_t c_space_id; int ret_value_c; char *c_name; int c_namelen; hdset_reg_ref_t ref_c; /* * Convert FORTRAN name to C name */ c_namelen = *namelen; c_name = (char *)HD5f2cstring(name, c_namelen); if (c_name == NULL) return ret_value; /* * Call H5Rcreate function. */ c_loc_id = *loc_id; c_space_id = *space_id; ret_value_c = H5Rcreate(&ref_c, c_loc_id, c_name, H5R_DATASET_REGION, c_space_id); HDfree(c_name); if (ret_value_c >= 0) { HDmemcpy (ref, &ref_c, H5R_DSET_REG_REF_BUF_SIZE); ret_value = 0; } return ret_value; }
/*---------------------------------------------------------------------------- * Name: h5rcreate_object_c * Purpose: Call H5Rcreate to create a reference to an object * Inputs: loc_id - file or group identifier * name - name of the dataset * namelen - name length * Outputs: ref - reference to the object * Returns: 0 on success, -1 on failure * Programmer: Elena Pourmal * Wednesday, December 1, 1999 * Modifications: *---------------------------------------------------------------------------*/ int_f nh5rcreate_object_c (haddr_t_f *ref, hid_t_f *loc_id, _fcd name, int_f *namelen) { int ret_value = -1; hid_t c_loc_id; int ret_value_c; char *c_name; int c_namelen; hobj_ref_t ref_c; /* * Convert FORTRAN name to C name */ c_namelen = *namelen; c_name = (char *)HD5f2cstring(name, c_namelen); if (c_name == NULL) return ret_value; /* * Call H5Rcreate function. */ c_loc_id = *loc_id; ret_value_c = H5Rcreate(&ref_c, c_loc_id, c_name, H5R_OBJECT, -1); HDfree(c_name); if (ret_value_c >= 0) { *ref=(haddr_t_f)ref_c; ret_value = 0; } return ret_value; }
void DCDataSet::createReference(hid_t refGroup, hid_t srcGroup, DCDataSet &srcDataSet) throw (DCException) { if (opened) throw DCException(getExceptionString("createReference: dataset is already open")); if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT)) throw DCException(getExceptionString("createReference: this reference already exists")); getLogicalSize().set(srcDataSet.getLogicalSize()); this->ndims = srcDataSet.getNDims(); if (H5Rcreate(®ionRef, srcGroup, srcDataSet.getName().c_str(), H5R_OBJECT, -1) < 0) throw DCException(getExceptionString("createReference: failed to create region reference")); hsize_t ndims = 1; dataspace = H5Screate_simple(1, &ndims, NULL); if (dataspace < 0) throw DCException(getExceptionString("createReference: failed to create dataspace for reference")); dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_OBJ, dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT); if (dataset < 0) throw DCException(getExceptionString("createReference: failed to create dataset for reference")); if (H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, dsetWriteProperties, ®ionRef) < 0) throw DCException(getExceptionString("createReference: failed to write reference")); isReference = true; opened = true; }
//-------------------------------------------------------------------------- // Function: H5Object::p_reference (protected) // Purpose Creates a reference to an HDF5 object or a dataset region. // Parameters // name - IN: Name of the object to be referenced // dataspace - IN: Dataspace with selection // ref_type - IN: Type of reference; default to \c H5R_DATASET_REGION // Exception H5::IdComponentException // Programmer Binh-Minh Ribler - May, 2004 //-------------------------------------------------------------------------- void H5Object::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const { herr_t ret_value = H5Rcreate(ref, getId(), name, ref_type, space_id); if (ret_value < 0) { throw IdComponentException("", "H5Rcreate failed"); } }
//-------------------------------------------------------------------------- // Function: H5Location::p_reference (protected) // Purpose Creates a reference to an HDF5 object or a dataset region. // Parameters // name - IN: Name of the object to be referenced // dataspace - IN: Dataspace with selection // ref_type - IN: Type of reference; default to \c H5R_DATASET_REGION // Exception H5::ReferenceException // Programmer Binh-Minh Ribler - May, 2004 //-------------------------------------------------------------------------- void H5Location::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const { herr_t ret_value = H5Rcreate(ref, getId(), name, ref_type, space_id); if (ret_value < 0) { throw ReferenceException(inMemFunc("reference"), "H5Rcreate failed"); } }
void NSDFWriter::createEventMap() { herr_t status; hid_t eventMapContainer = require_group(filehandle_, MAPEVENTSRC); // Open the container for the event maps // Create the Datasets themselves (one for each field - each row // for one object). for (map< string, vector < string > >::iterator ii = classFieldToEventSrc_.begin(); ii != classFieldToEventSrc_.end(); ++ii){ vector < string > pathTokens; tokenize(ii->first, "/", pathTokens); string className = pathTokens[0]; string fieldName = pathTokens[1]; hid_t classGroup = require_group(eventMapContainer, className); hid_t strtype = H5Tcopy(H5T_C_S1); status = H5Tset_size(strtype, H5T_VARIABLE); // create file space hid_t ftype = H5Tcreate(H5T_COMPOUND, sizeof(hvl_t) +sizeof(hobj_ref_t)); status = H5Tinsert(ftype, "source", 0, strtype); status = H5Tinsert(ftype, "data", sizeof(hvl_t), H5T_STD_REF_OBJ); hsize_t dims[1] = {ii->second.size()}; hid_t space = H5Screate_simple(1, dims, NULL); // The dataset for mapping is named after the field hid_t ds = H5Dcreate2(classGroup, fieldName.c_str(), ftype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Sclose(space); map_type * buf = (map_type*)calloc(ii->second.size(), sizeof(map_type)); // Populate the buffer entries with source uid and data // reference for (unsigned int jj = 0; jj < ii->second.size(); ++jj){ buf->source = ii->second[jj].c_str(); char * dsname = (char*)calloc(256, sizeof(char)); ssize_t size = H5Iget_name(classFieldToEvent_[ii->first][jj], dsname, 255); if (size > 255){ free(dsname); dsname = (char*)calloc(size, sizeof(char)); size = H5Iget_name(classFieldToEvent_[ii->first][jj], dsname, 255); } status = H5Rcreate(&(buf->data), filehandle_, dsname, H5R_OBJECT, -1); free(dsname); assert(status >= 0); } // create memory space hid_t memtype = H5Tcreate(H5T_COMPOUND, sizeof(map_type)); status = H5Tinsert(memtype, "source", HOFFSET(map_type, source), strtype); status = H5Tinsert(memtype, "data", HOFFSET(map_type, data), H5T_STD_REF_OBJ); status = H5Dwrite(ds, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf); free(buf); status = H5Tclose(strtype); status = H5Tclose(ftype); status = H5Tclose(memtype); status = H5Dclose(ds); } }
/* * Class: hdf_hdf5lib_H5 * Method: H5Rcreate * Signature: ([BJLjava/lang/String;IJ)I */ JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5Rcreate(JNIEnv *env, jclass clss, jbyteArray ref, jlong loc_id, jstring name, jint ref_type, jlong space_id) { const char *rName; herr_t status = -1; jbyte *refP; jboolean isCopy2; PIN_JAVA_STRING(name, rName, -1); if (ref == NULL) { UNPIN_JAVA_STRING(name, rName); h5nullArgument( env, "H5Rcreate: ref is NULL"); } /* end if */ else { if ((ref_type == H5R_OBJECT) && ENVPTR->GetArrayLength(ENVPAR ref) != H5R_OBJ_REF_BUF_SIZE) { UNPIN_JAVA_STRING(name, rName); h5badArgument( env, "H5Rcreate: 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) { UNPIN_JAVA_STRING(name, rName); h5badArgument( env, "H5Rcreate: region ref input array != H5R_DSET_REG_REF_BUF_SIZE"); } /* end else if */ else if ((ref_type != H5R_OBJECT) && (ref_type != H5R_DATASET_REGION)) { UNPIN_JAVA_STRING(name, rName); h5badArgument( env, "H5Rcreate: ref_type unknown type "); } /* end else if */ else { refP = (jbyte*)ENVPTR->GetByteArrayElements(ENVPAR ref, &isCopy2); if (refP == NULL) { UNPIN_JAVA_STRING(name, rName); h5JNIFatalError(env, "H5Rcreate: ref not pinned"); } /* end if */ else { status = H5Rcreate(refP, (hid_t)loc_id, rName, (H5R_type_t)ref_type, (hid_t)space_id); UNPIN_JAVA_STRING(name, rName); if (status < 0) { ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, JNI_ABORT); h5libraryError(env); } /* end if */ else { ENVPTR->ReleaseByteArrayElements(ENVPAR ref, refP, 0); } /* end else */ } /* end else */ } /* end else */ } /* end else */ return (jint)status; } /* end Java_hdf_hdf5lib_H5_H5Rcreate */
void DCDataSet::createReference(hid_t refGroup, hid_t srcGroup, DCDataSet &srcDataSet, Dimensions count, Dimensions offset, Dimensions stride) throw (DCException) { if (opened) throw DCException(getExceptionString("createReference: dataset is already open")); if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT)) throw DCException(getExceptionString("createReference: this reference already exists")); getLogicalSize().set(count); this->ndims = srcDataSet.getNDims(); count.swapDims(this->ndims); offset.swapDims(this->ndims); stride.swapDims(this->ndims); // select region hyperslab in source dataset if (H5Sselect_hyperslab(srcDataSet.getDataSpace(), H5S_SELECT_SET, offset.getPointer(), stride.getPointer(), count.getPointer(), NULL) < 0 || H5Sselect_valid(srcDataSet.getDataSpace()) <= 0) throw DCException(getExceptionString("createReference: failed to select hyperslap for reference")); if (H5Rcreate(®ionRef, srcGroup, srcDataSet.getName().c_str(), H5R_DATASET_REGION, srcDataSet.getDataSpace()) < 0) throw DCException(getExceptionString("createReference: failed to create region reference")); hsize_t ndims = 1; dataspace = H5Screate_simple(1, &ndims, NULL); if (dataspace < 0) throw DCException(getExceptionString("createReference: failed to create dataspace for reference")); dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_DSETREG, dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT); if (dataset < 0) throw DCException(getExceptionString("createReference: failed to create dataset for reference")); if (H5Dwrite(dataset, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, dsetWriteProperties, ®ionRef) < 0) throw DCException(getExceptionString("createReference: failed to write reference")); isReference = true; opened = true; }
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; }
int main(void) { hid_t fid; /* File, group, datasets, datatypes */ hid_t gid_a; /* and dataspaces identifiers */ hid_t did_b, sid_b, tid_b; hid_t did_r, tid_r, sid_r; H5O_type_t obj_type; herr_t status; hobj_ref_t *wbuf; /* buffer to write to disk */ hobj_ref_t *rbuf; /* buffer to read from disk */ hsize_t dim_r[1]; hsize_t dim_b[2]; /* * Create a file using default properties. */ fid = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create group "A" in the file. */ gid_a = H5Gcreate2(fid, "A", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset "B" in the file. */ dim_b[0] = 2; dim_b[1] = 6; sid_b = H5Screate_simple(2, dim_b, NULL); did_b = H5Dcreate2(fid, "B", H5T_NATIVE_FLOAT, sid_b, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset "R" to store references to the objects "A" and "B". */ dim_r[0] = 2; sid_r = H5Screate_simple(1, dim_r, NULL); tid_r = H5Tcopy(H5T_STD_REF_OBJ); did_r = H5Dcreate2(fid, "R", tid_r, sid_r, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Allocate write and read buffers. */ wbuf = (hobj_ref_t *)malloc(sizeof(hobj_ref_t) * 2); rbuf = (hobj_ref_t *)malloc(sizeof(hobj_ref_t) * 2); /* * Create references to the group "A" and dataset "B" * and store them in the wbuf. */ H5Rcreate(&wbuf[0], fid, "A", H5R_OBJECT, (hid_t)-1); H5Rcreate(&wbuf[1], fid, "B", H5R_OBJECT, (hid_t)-1); /* * Write dataset R using default transfer properties. */ status = H5Dwrite(did_r, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf); /* * Close all objects. */ H5Gclose(gid_a); H5Sclose(sid_b); H5Dclose(did_b); H5Tclose(tid_r); H5Sclose(sid_r); H5Dclose(did_r); H5Fclose(fid); /* * Reopen the file. */ fid = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT); /* * Open and read dataset "R". */ did_r = H5Dopen2(fid, "R", H5P_DEFAULT); status = H5Dread(did_r, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf); /* * Find the type of referenced objects. */ status = H5Rget_obj_type2(did_r, H5R_OBJECT, &rbuf[0], &obj_type); if(obj_type == H5O_TYPE_GROUP) printf("First dereferenced object is a group. \n"); status = H5Rget_obj_type2(did_r, H5R_OBJECT, &rbuf[1], &obj_type); if(obj_type == H5O_TYPE_DATASET) printf("Second dereferenced object is a dataset. \n"); /* * Get datatype of the dataset "B" */ did_b = H5Rdereference2(did_r, H5P_DEFAULT, H5R_OBJECT, &rbuf[1]); tid_b = H5Dget_type(did_b); if(H5Tequal(tid_b, H5T_NATIVE_FLOAT)) printf("Datatype of the dataset is H5T_NATIVE_FLOAT.\n"); printf("\n"); /* * Close all objects and free memory buffers. */ H5Dclose(did_r); H5Dclose(did_b); H5Tclose(tid_b); H5Fclose(fid); free(rbuf); free(wbuf); return 0; }
/***************************************************************************** This function generates attributes, groups, and datasets of many types. Parameters: fname: file_name. ngrps: number of top level groups. ndsets: number of datasets. attrs: number of attributes. nrow: number of rows in a dataset. chunk: chunk size (single number). vlen: max vlen size. comp: use latest format. latest: use gzip comnpression. Return: Non-negative on success/Negative on failure Programmer: Peter Cao <*****@*****.**>, Jan. 2013 ****************************************************************************/ herr_t create_perf_test_file(const char *fname, int ngrps, int ndsets, int nattrs, hsize_t nrows, hsize_t dim0, hsize_t chunk, int vlen, int compressed, int latest) { int i, j, k; hid_t fid, sid_null, sid_scalar, sid_1d, sid_2d, did, aid, sid_2, sid_large, fapl=H5P_DEFAULT, dcpl=H5P_DEFAULT, gid1, gid2, cmp_tid, tid_str, tid_enum, tid_array_f, tid_vlen_i, tid_vlen_s; char name[32], tmp_name1[32], tmp_name2[32], tmp_name3[32]; hsize_t dims[1]={dim0}, dims2d[2]={dim0, (dim0/4+1)}, dims_array[1]={FIXED_LEN}, dim1[1]={2}; char *enum_names[4] = {"SOLID", "LIQUID", "GAS", "PLASMA"}; test_comp_t *buf_comp=NULL, *buf_comp_large=NULL; int *buf_int=NULL; float (*buf_float_a)[FIXED_LEN]=NULL; double **buf_double2d=NULL; hvl_t *buf_vlen_i=NULL; char (*buf_str)[FIXED_LEN]; char **buf_vlen_s=NULL; hobj_ref_t buf_ref[2]; hdset_reg_ref_t buf_reg_ref[2]; size_t offset, len; herr_t status; char *names[NTYPES] = { "int", "ulong", "float", "double", "fixed string", "enum", "fixed float array", "vlen int array", "vlen strings"}; hid_t types[NTYPES] = { H5T_NATIVE_INT, H5T_NATIVE_UINT64, H5T_NATIVE_FLOAT, H5T_NATIVE_DOUBLE, tid_str, tid_enum, tid_array_f, tid_vlen_i, tid_vlen_s}; hsize_t coords[4][2] = { {0, 1}, {3, 5}, {1, 0}, {2, 4}}, start=0, stride=1, count=1; if (nrows < NROWS) nrows = NROWS; if (ngrps<NGROUPS) ngrps=NGROUPS; if (ndsets<NDSETS) ndsets=NDSETS; if (nattrs<NATTRS) nattrs=NATTRS; if (dim0<DIM0) dim0=DIM0; if (chunk>dim0) chunk=dim0/4; if (chunk<1) chunk = 1; if (vlen<1) vlen = MAXVLEN; /* create fixed string datatype */ types[4] = tid_str = H5Tcopy (H5T_C_S1); H5Tset_size (tid_str, FIXED_LEN); /* create enum datatype */ types[5] = tid_enum = H5Tenum_create(H5T_NATIVE_INT); for (i = (int) SOLID; i <= (int) PLASMA; i++) { phase_t val = (phase_t) i; status = H5Tenum_insert (tid_enum, enum_names[i], &val); } /* create float array datatype */ types[6] = tid_array_f = H5Tarray_create (H5T_NATIVE_FLOAT, 1, dims_array); /* create variable length integer datatypes */ types[7] = tid_vlen_i = H5Tvlen_create (H5T_NATIVE_INT); /* create variable length string datatype */ types[8] = tid_vlen_s = H5Tcopy (H5T_C_S1); H5Tset_size (tid_vlen_s, H5T_VARIABLE); /* create compound datatypes */ cmp_tid = H5Tcreate (H5T_COMPOUND, sizeof (test_comp_t)); offset = 0; for (i=0; i<NTYPES-2; i++) { H5Tinsert(cmp_tid, names[i], offset, types[i]); offset += H5Tget_size(types[i]); } H5Tinsert(cmp_tid, names[7], offset, types[7]); offset += sizeof (hvl_t); H5Tinsert(cmp_tid, names[8], offset, types[8]); /* create dataspace */ sid_1d = H5Screate_simple (1, dims, NULL); sid_2d = H5Screate_simple (2, dims2d, NULL); sid_2 = H5Screate_simple (1, dim1, NULL); sid_large = H5Screate_simple (1, &nrows, NULL); sid_null = H5Screate (H5S_NULL); sid_scalar = H5Screate (H5S_SCALAR); /* create fid access property */ fapl = H5Pcreate (H5P_FILE_ACCESS); H5Pset_libver_bounds (fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST); /* create dataset creation property */ dcpl = H5Pcreate (H5P_DATASET_CREATE); /* set dataset chunk */ if (chunk>0) { H5Pset_chunk (dcpl, 1, &chunk); } /* set dataset compression */ if (compressed) { if (chunk<=0) { chunk = dim0/10+1;; H5Pset_chunk (dcpl, 1, &chunk); } H5Pset_shuffle (dcpl); H5Pset_deflate (dcpl, 6); } /* allocate buffers */ buf_comp = (test_comp_t *)calloc(dim0, sizeof(test_comp_t)); buf_comp_large = (test_comp_t *)calloc(nrows, sizeof(test_comp_t)); buf_int = (int *)calloc(dim0, sizeof(int)); buf_float_a = malloc(dim0*sizeof(*buf_float_a)); buf_vlen_i = (hvl_t *)calloc(dim0, sizeof (hvl_t)); buf_vlen_s = (char **)calloc(dim0, sizeof(char *)); buf_str = malloc(dim0*sizeof (*buf_str)); /* allocate array of doulbe pointers */ buf_double2d = (double **)calloc(dims2d[0],sizeof(double *)); /* allocate a contigous chunk of memory for the data */ buf_double2d[0] = (double *)calloc( dims2d[0]*dims2d[1],sizeof(double) ); /* assign memory city to pointer array */ for (i=1; i <dims2d[0]; i++) buf_double2d[i] = buf_double2d[0]+i*dims2d[1]; /* fill buffer values */ len = 1; for (i=0; i<dims[0]; i++) { buf_comp[i].i = buf_int[i] = i-2147483648; buf_comp[i].l = 0xffffffffffffffff-i; buf_comp[i].f = 1.0/(i+1.0); buf_comp[i].d = 987654321.0*i+1.0/(i+1.0); buf_comp[i].e = (phase_t) (i % (int) (PLASMA + 1)); for (j=0; j<FIXED_LEN; j++) { buf_comp[i].f_array[j] = buf_float_a[i][j] = i*100+j; buf_str[i][j] = 'a' + (i%26); } buf_str[i][FIXED_LEN-1] = 0; strcpy(buf_comp[i].s, buf_str[i]); len = (1-cos(i/8.0))/2*vlen+1; if (!i) len = vlen; buf_vlen_i[i].len = len; buf_vlen_i[i].p = (int *)calloc(len, sizeof(int)); for (j=0; j<len; j++) ((int*)(buf_vlen_i[i].p))[j] = i*100+j; buf_comp[i].i_vlen = buf_vlen_i[i]; buf_vlen_s[i] = (char *)calloc(len, sizeof(char)); for (j=0; j<len-1; j++) buf_vlen_s[i][j] = j%26+'A'; buf_comp[i].s_vlen = buf_vlen_s[i]; for (j=0; j<dims2d[1]; j++) buf_double2d[i][j] = i+j/10000.0; } for (i=0; i<nrows; i++) { buf_comp_large[i].i = i-2147483648; buf_comp_large[i].l = 0xffffffffffffffff-i; buf_comp_large[i].f = 1.0/(i+1.0); buf_comp_large[i].d = 987654321.0*i+1.0/(i+1.0); buf_comp_large[i].e = (phase_t) (i % (int) (PLASMA + 1)); for (j=0; j<FIXED_LEN-1; j++) { buf_comp_large[i].f_array[j] = i*100+j; buf_comp_large[i].s[j] = 'a' + (i%26); } len = i%vlen+1; buf_comp_large[i].i_vlen.len = len; buf_comp_large[i].i_vlen.p = (int *)calloc(len, sizeof(int)); for (j=0; j<len; j++) ((int*)(buf_comp_large[i].i_vlen.p))[j] = i*100+j; buf_comp_large[i].s_vlen = (char *)calloc(i+2, sizeof(char)); for (j=0; j<i+1; j++) (buf_comp_large[i].s_vlen)[j] = j%26+'A'; } /* create file */ if (latest) fid = H5Fcreate (fname, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); else fid = H5Fcreate (fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); add_attrs(fid, 0); sprintf(name, "a cmp ds of %d rows", nrows); did = H5Dcreate (fid, name, cmp_tid, sid_large, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, cmp_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_comp_large); add_attrs(did, 0); H5Dclose(did); // /* add attributes*/ gid1 = H5Gcreate (fid, "attributes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (nattrs<1) nattrs = 1; i=0; while (i<nattrs) i += add_attrs(gid1, i); H5Gclose(gid1); /* add many sub groups to a group*/ gid1 = H5Gcreate (fid, "groups", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); add_attrs(gid1, 0); for (i=0; i<ngrps; i++) { /* create sub groups */ sprintf(name, "g%02d", i); gid2 = H5Gcreate (gid1, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (i<10) add_attrs(gid2, 0); H5Gclose(gid2); } H5Gclose(gid1); /* add many datasets to a group */ gid1 = H5Gcreate (fid, "datasets", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); add_attrs(gid1, 0); for (j=0; j<ndsets; j+=12) { /* 1 add a null dataset */ sprintf(name, "%05d null dataset", j); did = H5Dcreate (gid1, name, H5T_STD_I32LE, sid_null, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (!j) add_attrs(did, j); H5Dclose(did); /* 2 add scalar int point */ sprintf(name, "%05d scalar int point", j); did = H5Dcreate (gid1, name, H5T_NATIVE_INT, sid_scalar, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &j); if (!j) add_attrs(did, j); H5Dclose(did); /* 3 scalar vlen string */ sprintf(name, "%05d scalar vlen string", j); did = H5Dcreate (gid1, name, tid_vlen_s, sid_scalar, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, tid_vlen_s, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buf_vlen_s[0]); if (!j) add_attrs(did, j); H5Dclose(did); /* 4 add fixed-length float array */ sprintf(name, "%05d fixed-length float array", j); did = H5Dcreate (gid1, name, tid_array_f, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_array_f, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_float_a); if (!j) add_attrs(did, j); H5Dclose(did); /* 5 add fixed-length strings */ sprintf(name, "%05d fixed-length strings", j); did = H5Dcreate (gid1, name, tid_str, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_str, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_str); if (!j) add_attrs(did, j); H5Dclose(did); /* 6 add compound data */ sprintf(name, "%05d compund data", j); did = H5Dcreate (gid1, name, cmp_tid, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, cmp_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_comp); if (!j) add_attrs(did, j); H5Dclose(did); /* 7 add 2D double */ sprintf(name, "%05d 2D double", j); strcpy (tmp_name1, name); did = H5Dcreate (gid1, name, H5T_NATIVE_DOUBLE, sid_2d, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_double2d[0]); if (!j) add_attrs(did, j); H5Dclose(did); /* 8 add 1D int array */ sprintf(name, "%05d 1D int array", j); did = H5Dcreate (gid1, name, H5T_NATIVE_INT, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_int); if (!j) add_attrs(did, j); H5Dclose(did); /* 9 add vlen int array */ sprintf(name, "%05d vlen int array", j); strcpy (tmp_name2, name); did = H5Dcreate (gid1, name, tid_vlen_i, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_vlen_i, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_vlen_i); if (!j) add_attrs(did, j); H5Dclose(did); /* 10 add vlen strings */ sprintf(name, "%05d vlen strings", j); strcpy (tmp_name3, name); did = H5Dcreate (gid1, name, tid_vlen_s, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_vlen_s, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_vlen_s); if (!j) add_attrs(did, j); H5Dclose(did); /* 11 add object refs */ H5Rcreate(&buf_ref[0],gid1, ".", H5R_OBJECT, -1); H5Rcreate(&buf_ref[1],gid1, tmp_name3, H5R_OBJECT, -1); sprintf(name, "%05d obj refs", j); did = H5Dcreate (gid1, name, H5T_STD_REF_OBJ, sid_2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_ref); if (!j) add_attrs(did, j); H5Dclose(did); /* 12 add region refs */ H5Sselect_elements (sid_2d, H5S_SELECT_SET, 4, coords[0]); H5Rcreate(&buf_reg_ref[0],gid1, tmp_name1, H5R_DATASET_REGION, sid_2d); H5Sselect_none(sid_2d); count = dims[0]/2+1; H5Sselect_hyperslab (sid_1d, H5S_SELECT_SET, &start, &stride, &count,NULL); H5Rcreate(&buf_reg_ref[1],gid1, tmp_name2, H5R_DATASET_REGION, sid_1d); H5Sselect_none(sid_1d); sprintf(name, "%05d region refs", j); did = H5Dcreate (gid1, name, H5T_STD_REF_DSETREG, sid_2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_reg_ref); if (!j) add_attrs(did, j); H5Dclose(did); } H5Gclose(gid1); H5Tclose (tid_array_f); H5Tclose (tid_vlen_i); H5Tclose (tid_vlen_s); H5Tclose (tid_enum); H5Tclose (tid_str); H5Tclose (cmp_tid); H5Pclose (dcpl); H5Pclose (fapl); H5Sclose (sid_1d); H5Sclose (sid_2d); H5Sclose (sid_2); H5Sclose (sid_large); H5Sclose (sid_null); H5Sclose (sid_scalar); H5Fclose (fid); for (i=0; i<dims[0]; i++) { if (buf_vlen_i[i].p) free(buf_vlen_i[i].p); if (buf_vlen_s[i]) free(buf_vlen_s[i]); } for (i=0; i<nrows; i++) { if (buf_comp_large[i].i_vlen.p) free(buf_comp_large[i].i_vlen.p); if (buf_comp_large[i].s_vlen) free(buf_comp_large[i].s_vlen); } free (buf_comp); free (buf_comp_large); free (buf_int); free (buf_float_a); free (buf_double2d[0]); free (buf_double2d); free (buf_str); free(buf_vlen_i); free(buf_vlen_s); return 0; }
/* adds different types of attributes to an object. returns the number of attributes added to the objects. */ int add_attrs(hid_t oid, int idx) { char name[32]; int i0, i1, i2, j, nattrs=0; hid_t aid, tid, tid1, sid; hvl_t i_vlen[4]; hobj_ref_t ref; zipcode_t cmp_data[4]; unsigned int i = 0xffffffff; long long l = -2147483647; float f = 123456789.987654321; double d = 987654321.123456789; char *s[7] = {"Parting", "is such", "sweeter", "sorrow."}; float f_array[4] = {1.0, 2.22, 3.333, 4.444}; char *s_vlen[4] = {"Parting", "is such", "sweet", "sorrow."}; hsize_t dims1[1]={1}, dims2[1]={4}, dims3[2]={3,5}; int int3d[4][3][5]; size_t offset = 0; for (i0=0; i0<4; i0++) { i_vlen[i0].len = (i0+1); i_vlen[i0].p = (int *)calloc(i_vlen[i0].len, sizeof(int)); for (j=0; j<i_vlen[i0].len; j++) ((int *)i_vlen[i0].p)[j] = i0*100+j; for (i1=0; i1<3; i1++) { for (i2=0; i2<5; i2++) int3d[i0][i1][i2] = i0*i1-i1*i2+i0*i2; } } cmp_data[0].zipcode = 01001; cmp_data[0].city = "Agawam, Massachusetts"; cmp_data[1].zipcode = 99950; cmp_data[1].city = "Ketchikan, Alaska"; cmp_data[2].zipcode = 00501; cmp_data[2].city = "Holtsville, New York"; cmp_data[3].zipcode = 61820; cmp_data[3].city = "Champaign, Illinois"; /* 1 scalar point */ sid = H5Screate (H5S_SCALAR); sprintf(name, "%05d scalar int", idx); nattrs += add_attr(oid, name, H5T_NATIVE_UINT, sid, &i); sprintf(name, "%05d scalar ulong", idx); nattrs += add_attr(oid, name, H5T_NATIVE_INT64, sid, &l); sprintf(name, "%05d scalar str", idx); tid = H5Tcopy (H5T_C_S1); H5Tset_size (tid, H5T_VARIABLE); nattrs += add_attr(oid, name, tid, sid, &s[2]); H5Tclose(tid); H5Sclose(sid); /* 4 single point */ sid = H5Screate_simple (1, dims1, NULL); H5Rcreate(&ref, oid, ".", H5R_OBJECT, -1); sprintf(name, "%05d single float", idx); nattrs += add_attr(oid, name, H5T_NATIVE_FLOAT, sid, &f); sprintf(name, "%05d single double", idx); nattrs += add_attr(oid, name, H5T_NATIVE_DOUBLE, sid, &d); sprintf(name, "%05d single obj_ref", idx); nattrs += add_attr(oid, name, H5T_STD_REF_OBJ, sid, &ref); H5Sclose(sid); /* 7 fixed length 1D array */ sid = H5Screate_simple (1, dims1, NULL); tid = H5Tarray_create (H5T_NATIVE_FLOAT, 1, dims2); sprintf(name, "%05d array float", idx); nattrs += add_attr(oid, name, tid, sid, &f_array[0]); H5Tclose(tid); tid = H5Tcopy (H5T_C_S1); H5Tset_size (tid, strlen(s[0])+1); tid1 = H5Tarray_create (tid, 1, dims2); sprintf(name, "%05d array str", idx); nattrs += add_attr(oid, name, tid1, sid, s); H5Tclose(tid1); H5Tclose(tid); H5Sclose(sid); /* 9 fixed length 2D int arrays */ sid = H5Screate_simple (1, dims2, NULL); tid = H5Tarray_create (H5T_NATIVE_INT, 2, dims3); sprintf(name, "%05d array int 2D", idx); nattrs += add_attr(oid, name, tid, sid, int3d[0][0]); H5Tclose(tid); H5Sclose(sid); /* 10 variable length arrays */ sid = H5Screate_simple (1, dims2, NULL); tid = H5Tcopy (H5T_C_S1); H5Tset_size (tid, H5T_VARIABLE); sprintf(name, "%05d vlen strings", idx); nattrs += add_attr(oid, name, tid, sid, s_vlen); H5Tclose(tid); tid = H5Tvlen_create (H5T_NATIVE_INT);; sprintf(name, "%05d vlen int array", idx); nattrs += add_attr(oid, name, tid, sid, i_vlen); H5Tclose(tid); H5Sclose(sid); /* 12 compound data */ sid = H5Screate_simple (1, dims2, NULL); tid = H5Tcreate (H5T_COMPOUND, sizeof (zipcode_t)); tid1 = H5Tcopy (H5T_C_S1); H5Tset_size (tid1, H5T_VARIABLE); H5Tinsert (tid, "zip code", 0, H5T_NATIVE_INT); offset += sizeof(H5T_NATIVE_INT); H5Tinsert (tid, "City", offset, tid1); offset += sizeof(char *); sprintf(name, "%05d compound data", idx); nattrs += add_attr(oid, name, tid, sid, cmp_data); H5Tclose(tid1); H5Tclose(tid); H5Sclose(sid); for (i0=0; i0<4; i0++) free(i_vlen[i0].p); return nattrs; }
herr_t H5IMlink_palette( hid_t loc_id, const char *image_name, const char *pal_name ) { hid_t did; hid_t atid=-1; hid_t aid=-1; hid_t asid=-1; hobj_ref_t ref; /* write a new reference */ hobj_ref_t *refbuf; /* buffer to read references */ hssize_t n_refs; hsize_t dim_ref; int ok_pal; /* check the arguments */ if (image_name == NULL) return -1; if (pal_name == NULL) return -1; /* The image dataset may or may not have the attribute "PALETTE" * First we try to open to see if it is already there; if not, it is created. * If it exists, the array of references is extended to hold the reference * to the new palette */ /* First we get the image id */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ ok_pal = H5LT_find_attribute( did, "PALETTE" ); /*------------------------------------------------------------------------- * It does not exist. We create the attribute and one reference *------------------------------------------------------------------------- */ if(ok_pal == 0 ) { if((asid = H5Screate(H5S_SCALAR)) < 0) goto out; /* Create the attribute type for the reference */ if((atid = H5Tcopy(H5T_STD_REF_OBJ)) < 0) goto out; /* Create the attribute "PALETTE" to be attached to the image*/ if((aid = H5Acreate2(did, "PALETTE", atid, asid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto out; /* Create a reference. The reference is created on the local id. */ if(H5Rcreate(&ref, loc_id, pal_name, H5R_OBJECT, (hid_t)-1) < 0) goto out; /* Write the attribute with the reference */ if(H5Awrite(aid, atid, &ref) < 0) goto out; /* close */ if(H5Sclose(asid) < 0) goto out; if(H5Tclose(atid) < 0) goto out; if(H5Aclose(aid) < 0) goto out; } /*------------------------------------------------------------------------- * The attribute already exists, open it *------------------------------------------------------------------------- */ else if(ok_pal == 1) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if(H5Tget_class(atid) < 0) goto out; /* Get and save the old reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = (hsize_t)n_refs + 1; refbuf = (hobj_ref_t*)HDmalloc( sizeof(hobj_ref_t) * (size_t)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* The attribute must be deleted, in order to the new one can reflect the changes*/ if(H5Adelete(did, "PALETTE") < 0) goto out; /* Create a new reference for this palette. */ if ( H5Rcreate( &ref, loc_id, pal_name, H5R_OBJECT, (hid_t)-1 ) < 0) goto out; refbuf[n_refs] = ref; /* Create the data space for the new references */ if(H5Sclose(asid) < 0) goto out; if((asid = H5Screate_simple(1, &dim_ref, NULL)) < 0) goto out; /* Create the attribute again with the changes of space */ if(H5Aclose(aid) < 0) goto out; if((aid = H5Acreate2(did, "PALETTE", atid, asid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto out; /* Write the attribute with the new references */ if(H5Awrite(aid, atid, refbuf) < 0) goto out; /* close */ if(H5Sclose(asid) < 0) goto out; if(H5Tclose(atid) < 0) goto out; if(H5Aclose(aid) < 0) goto out; HDfree( refbuf ); } /* ok_pal == 1 */ /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); H5Sclose( asid ); H5Tclose( atid ); H5Aclose( aid ); return -1; }
int main (void) { hid_t file, space, dset, obj, attr; /* Handles */ herr_t status; hsize_t dims[1] = {DIM0}; hobj_ref_t wdata[DIM0], /* Write buffer */ *rdata; /* Read buffer */ H5G_obj_t objtype; ssize_t size; char *name; int ndims, i; /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create a dataset with a scalar dataspace. */ space = H5Screate (H5S_SCALAR); obj = H5Dcreate (file, "DS2", H5T_STD_I32LE, space, H5P_DEFAULT); status = H5Dclose (obj); status = H5Sclose (space); /* * Create a group. */ obj = H5Gcreate (file, "G1", H5P_DEFAULT); status = H5Gclose (obj); /* * Create references to the previously created objects. Passing -1 * as space_id causes this parameter to be ignored. Other values * besides valid dataspaces result in an error. */ status = H5Rcreate (&wdata[0], file, "G1", H5R_OBJECT, -1); status = H5Rcreate (&wdata[1], file, "DS2", H5R_OBJECT, -1); /* * Create dataset with a scalar dataspace to serve as the parent * for the attribute. */ space = H5Screate (H5S_SCALAR); dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); status = H5Sclose (space); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the attribute and write the object references to it. */ attr = H5Acreate (dset, ATTRIBUTE, H5T_STD_REF_OBJ, space, H5P_DEFAULT); status = H5Awrite (attr, H5T_STD_REF_OBJ, wdata); /* * Close and release resources. */ status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the attribute has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file, dataset, and attribute. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET); attr = H5Aopen_name (dset, ATTRIBUTE); /* * Get dataspace and allocate memory for read buffer. */ space = H5Aget_space (attr); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (hobj_ref_t *) malloc (dims[0] * sizeof (hobj_ref_t)); /* * Read the data. */ status = H5Aread (attr, H5T_STD_REF_OBJ, rdata); /* * Output the data to the screen. */ for (i=0; i<dims[0]; i++) { printf ("%s[%d]:\n ->", ATTRIBUTE, i); /* * Open the referenced object, get its name and type. */ obj = H5Rdereference (dset, H5R_OBJECT, &rdata[i]); objtype = H5Rget_obj_type (dset, H5R_OBJECT, &rdata[i]); /* * Get the length of the name, allocate space, then retrieve * the name. */ size = 1 + H5Iget_name (obj, NULL, 0); if (size > 1) { name = (char *) malloc (size); size = 1 + H5Iget_name (obj, name, size); } /* * Print the object type and close the object. */ switch (objtype) { case H5G_GROUP: printf ("Group"); status = H5Gclose (obj); break; case H5G_DATASET: printf ("Dataset"); status = H5Dclose (obj); break; case H5G_TYPE: printf ("Named Datatype"); status = H5Tclose (obj); } /* * Print the name and deallocate space for the name. */ if (size > 1) { printf (": %s", name); free (name); } printf ("\n"); } /* * Close and release resources. */ free (rdata); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Fclose (file); return 0; }
/*------------------------------------------------------------------------- * Function: gen_region_ref * * Purpose: Generate dataset region references * * Programmer: Jonathan Kim (Feb 23, 2010) *------------------------------------------------------------------------*/ static herr_t gen_region_ref(hid_t loc_id) { hid_t sid=0, oid1=0, oid2=0; int status; herr_t ret = SUCCEED; char data[3][16] = {"The quick brown", "fox jumps over ", "the 5 lazy dogs"}; hsize_t dims2[2] = {3,16}; hsize_t coords[4][2] = { {0,1}, {2,11}, {1,0}, {2,4} }; hdset_reg_ref_t rr_data[2]; hsize_t start[2] = {0,0}; hsize_t stride[2] = {2,11}; hsize_t count[2] = {2,2}; hsize_t block[2] = {1,3}; hsize_t dims1[1] = {2}; sid = H5Screate_simple (2, dims2, NULL); if (sid < 0) { fprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* create normal dataset which is refered */ oid2 = H5Dcreate2 (loc_id, REG_REF_DS2, H5T_STD_I8LE, sid, H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT); if (oid2 < 0) { fprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* write values to dataset */ status = H5Dwrite (oid2, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* select elements space for reference */ status = H5Sselect_elements (sid, H5S_SELECT_SET, 4, coords[0]); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Sselect_elements failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* create region reference from elements space */ status = H5Rcreate (&rr_data[0], loc_id, REG_REF_DS2, H5R_DATASET_REGION, sid); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* select hyperslab space for reference */ status = H5Sselect_hyperslab (sid, H5S_SELECT_SET, start, stride, count, block); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Sselect_hyperslab failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* create region reference from hyperslab space */ status = H5Rcreate (&rr_data[1], loc_id, REG_REF_DS2, H5R_DATASET_REGION, sid); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } H5Sclose (sid); /* Create dataspace. */ sid = H5Screate_simple (1, dims1, NULL); if (sid < 0) { fprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* create region reference dataset */ oid1 = H5Dcreate2 (loc_id, REG_REF_DS1, H5T_STD_REF_DSETREG, sid, H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT); if (oid1 < 0) { fprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /* write data as region references */ status = H5Dwrite (oid1, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, rr_data); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } out: if (oid1 > 0) H5Dclose (oid1); if (oid2 > 0) H5Dclose (oid2); if (sid > 0) H5Sclose (sid); return ret; }
/*------------------------------------------------------------------------- * Function: gen_obj_ref * * Purpose: Generate object references to dataset and group * * Programmer: Jonathan Kim (Feb 23, 2010) *------------------------------------------------------------------------*/ static herr_t gen_obj_ref(hid_t loc_id) { hid_t sid=0, oid=0; hsize_t dims1[1]={3}; hsize_t dims2[1]={2}; int data[3] = {10,20,30}; int status; /*--------------------- * create obj references to the previously created objects. * Passing -1 as reference is an object.*/ hobj_ref_t or_data[2]; /* write buffer */ herr_t ret = SUCCEED; /*-------------- * add dataset */ sid = H5Screate_simple(1, dims1, NULL); if (sid < 0) { fprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } oid = H5Dcreate2 (loc_id, OBJ_REF_DS, H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (oid < 0) { fprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } status = H5Dwrite(oid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } H5Dclose(oid); H5Sclose(sid); /*-------------- * add group */ oid = H5Gcreate2 (loc_id, OBJ_REF_GRP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (oid < 0) { fprintf(stderr, "Error: %s %d> H5Gcreate2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } H5Gclose(oid); status = H5Rcreate (&or_data[0], loc_id, OBJ_REF_DS, H5R_OBJECT, -1); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } status = H5Rcreate (&or_data[1], loc_id, OBJ_REF_GRP, H5R_OBJECT, -1); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } sid = H5Screate_simple (1, dims2, NULL); if (sid < 0) { fprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } oid = H5Dcreate2 (loc_id, "Dset_OBJREF", H5T_STD_REF_OBJ, sid, H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT); if (oid < 0) { fprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } status = H5Dwrite(oid, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, or_data); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } out: if(oid > 0) H5Dclose(oid); if(sid > 0) H5Sclose(sid); return ret; }
int main(void) { hid_t fid1; /* HDF5 File IDs */ hid_t dataset; /* Dataset ID */ hid_t group; /* Group ID */ hid_t sid1; /* Dataspace ID */ hid_t tid1; /* Datatype ID */ hsize_t dims1[] = {SPACE1_DIM1}; hobj_ref_t *wbuf; /* buffer to write to disk */ int *tu32; /* Temporary pointer to int data */ int i; /* counting variables */ const char *write_comment="Foo!"; /* Comments for group */ herr_t ret; /* Generic return value */ /* Compound datatype */ typedef struct s1_t { unsigned int a; unsigned int b; float c; } s1_t; /* Allocate write buffers */ wbuf=(hobj_ref_t *)malloc(sizeof(hobj_ref_t)*SPACE1_DIM1); tu32=malloc(sizeof(int)*SPACE1_DIM1); /* Create file */ fid1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create dataspace for datasets */ sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL); /* Create a group */ group=H5Gcreate(fid1,"Group1",-1); /* Set group's comment */ ret=H5Gset_comment(group,".",write_comment); /* Create a dataset (inside Group1) */ dataset=H5Dcreate(group,"Dataset1",H5T_STD_U32LE,sid1,H5P_DEFAULT); for(i=0; i<SPACE1_DIM1; i++) tu32[i] = i*3; /* Write selection to disk */ ret=H5Dwrite(dataset,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,tu32); /* Close Dataset */ ret = H5Dclose(dataset); /* Create another dataset (inside Group1) */ dataset=H5Dcreate(group,"Dataset2",H5T_NATIVE_UCHAR,sid1,H5P_DEFAULT); /* Close Dataset */ ret = H5Dclose(dataset); /* Create a datatype to refer to */ tid1 = H5Tcreate (H5T_COMPOUND, sizeof(s1_t)); /* Insert fields */ ret=H5Tinsert (tid1, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT); ret=H5Tinsert (tid1, "b", HOFFSET(s1_t,b), H5T_NATIVE_INT); ret=H5Tinsert (tid1, "c", HOFFSET(s1_t,c), H5T_NATIVE_FLOAT); /* Save datatype for later */ ret=H5Tcommit (group, "Datatype1", tid1); /* Close datatype */ ret = H5Tclose(tid1); /* Close group */ ret = H5Gclose(group); /* Create a dataset to store references */ dataset=H5Dcreate(fid1,"Dataset3",H5T_STD_REF_OBJ,sid1,H5P_DEFAULT); /* Create reference to dataset */ ret = H5Rcreate(&wbuf[0],fid1,"/Group1/Dataset1",H5R_OBJECT,-1); /* Create reference to dataset */ ret = H5Rcreate(&wbuf[1],fid1,"/Group1/Dataset2",H5R_OBJECT,-1); /* Create reference to group */ ret = H5Rcreate(&wbuf[2],fid1,"/Group1",H5R_OBJECT,-1); /* Create reference to named datatype */ ret = H5Rcreate(&wbuf[3],fid1,"/Group1/Datatype1",H5R_OBJECT,-1); /* Write selection to disk */ ret=H5Dwrite(dataset,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,H5P_DEFAULT,wbuf); /* Close disk dataspace */ ret = H5Sclose(sid1); /* Close Dataset */ ret = H5Dclose(dataset); /* Close file */ ret = H5Fclose(fid1); free(wbuf); free(tu32); return 0; }
int main (void) { hid_t file, space, memspace, dset, dset2, attr; /* Handles */ herr_t status; hsize_t dims[1] = {DIM0}, dims2[2] = {DS2DIM0, DS2DIM1}, coords[4][2] = { {0, 1}, {2, 11}, {1, 0}, {2, 4} }, start[2] = {0, 0}, stride[2] = {2, 11}, count[2] = {2, 2}, block[2] = {1, 3}; hssize_t npoints; hdset_reg_ref_t wdata[DIM0], /* Write buffer */ *rdata; /* Read buffer */ ssize_t size; char wdata2[DS2DIM0][DS2DIM1] = {"The quick brown", "fox jumps over ", "the 5 lazy dogs"}, *rdata2, *name; int ndims, i; /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create a dataset with character data. */ space = H5Screate_simple (2, dims2, NULL); dset2 = H5Dcreate (file, DATASET2, H5T_STD_I8LE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite (dset2, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2); /* * Create reference to a list of elements in dset2. */ status = H5Sselect_elements (space, H5S_SELECT_SET, 4, coords[0]); status = H5Rcreate (&wdata[0], file, DATASET2, H5R_DATASET_REGION, space); /* * Create reference to a hyperslab in dset2, close dataspace. */ status = H5Sselect_hyperslab (space, H5S_SELECT_SET, start, stride, count, block); status = H5Rcreate (&wdata[1], file, DATASET2, H5R_DATASET_REGION, space); status = H5Sclose (space); /* * Create dataset with a null dataspace to serve as the parent for * the attribute. */ space = H5Screate (H5S_NULL); dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Sclose (space); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the attribute and write the region references to it. */ attr = H5Acreate (dset, ATTRIBUTE, H5T_STD_REF_DSETREG, space, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attr, H5T_STD_REF_DSETREG, wdata); /* * Close and release resources. */ status = H5Aclose (attr); status = H5Dclose (dset); status = H5Dclose (dset2); status = H5Sclose (space); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the attribute has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file, dataset, and attribute. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET, H5P_DEFAULT); attr = H5Aopen (dset, ATTRIBUTE, H5P_DEFAULT); /* * Get dataspace and allocate memory for read buffer. */ space = H5Aget_space (attr); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (hdset_reg_ref_t *) malloc (dims[0] * sizeof (hdset_reg_ref_t)); status = H5Sclose (space); /* * Read the data. */ status = H5Aread (attr, H5T_STD_REF_DSETREG, rdata); /* * Output the data to the screen. */ for (i=0; i<dims[0]; i++) { printf ("%s[%d]:\n ->", ATTRIBUTE, i); /* * Open the referenced object, retrieve its region as a * dataspace selection. */ dset2 = H5Rdereference (dset, H5P_DEFAULT, H5R_DATASET_REGION, &rdata[i]); space = H5Rget_region (dset, H5R_DATASET_REGION, &rdata[i]); /* * Get the length of the object's name, allocate space, then * retrieve the name. */ size = 1 + H5Iget_name (dset2, NULL, 0); name = (char *) malloc (size); size = H5Iget_name (dset2, name, size); /* * Allocate space for the read buffer. We will only allocate * enough space for the selection, plus a null terminator. The * read buffer will be 1-dimensional. */ npoints = H5Sget_select_npoints (space); rdata2 = (char *) malloc (npoints + 1); /* * Read the dataset region, and add a null terminator so we can * print it as a string. */ memspace = H5Screate_simple (1, (hsize_t *) &npoints, NULL); status = H5Dread (dset2, H5T_NATIVE_CHAR, memspace, space, H5P_DEFAULT, rdata2); rdata2[npoints] = '\0'; /* * Print the name and region data, close and release resources. */ printf (" %s: %s\n", name, rdata2); free (rdata2); free (name); status = H5Sclose (space); status = H5Sclose (memspace); status = H5Dclose (dset2); } /* * Close and release resources. */ free (rdata); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Fclose (file); return 0; }
ObjectReference Object::reference () const { ObjectReference ref; Exception::check ("H5Rcreate", H5Rcreate (&ref.value (), handle (), ".", H5R_OBJECT, -1)); return ref; }
int do_copy_refobjs(hid_t fidin, hid_t fidout, trav_table_t *travt, pack_opt_t *options) /* repack options */ { hid_t grp_in=(-1); /* read group ID */ hid_t grp_out=(-1); /* write group ID */ hid_t dset_in=(-1); /* read dataset ID */ hid_t dset_out=(-1); /* write dataset ID */ hid_t type_in=(-1); /* named type ID */ hid_t dcpl_id=(-1); /* dataset creation property list ID */ hid_t space_id=(-1); /* space ID */ hid_t ftype_id=(-1); /* file data type ID */ hid_t mtype_id=(-1); /* memory data type ID */ size_t msize; /* memory size of memory type */ hsize_t nelmts; /* number of elements in dataset */ int rank; /* rank of dataset */ hsize_t dims[H5S_MAX_RANK]; /* dimensions of dataset */ unsigned int i, j; int k; /*------------------------------------------------------------------------- * browse *------------------------------------------------------------------------- */ for ( i = 0; i < travt->nobjs; i++) { switch ( travt->objs[i].type ) { /*------------------------------------------------------------------------- * H5G_GROUP *------------------------------------------------------------------------- */ case H5G_GROUP: /*------------------------------------------------------------------------- * copy referenced objects in attributes *------------------------------------------------------------------------- */ if ((grp_out=H5Gopen(fidout,travt->objs[i].name))<0) goto error; if((grp_in = H5Gopen (fidin,travt->objs[i].name))<0) goto error; if (copy_refs_attr(grp_in,grp_out,options,travt,fidout)<0) goto error; if (H5Gclose(grp_out)<0) goto error; if (H5Gclose(grp_in)<0) goto error; /*------------------------------------------------------------------------- * check for hard links *------------------------------------------------------------------------- */ if (travt->objs[i].nlinks) { for ( j=0; j<travt->objs[i].nlinks; j++) { H5Glink(fidout, H5G_LINK_HARD, travt->objs[i].name, travt->objs[i].links[j].new_name); } } break; /*------------------------------------------------------------------------- * H5G_DATASET *------------------------------------------------------------------------- */ case H5G_DATASET: if ((dset_in=H5Dopen(fidin,travt->objs[i].name))<0) goto error; if ((space_id=H5Dget_space(dset_in))<0) goto error; if ((ftype_id=H5Dget_type (dset_in))<0) goto error; if ((dcpl_id=H5Dget_create_plist(dset_in))<0) goto error; if ( (rank=H5Sget_simple_extent_ndims(space_id))<0) goto error; if ( H5Sget_simple_extent_dims(space_id,dims,NULL)<0) goto error; nelmts=1; for (k=0; k<rank; k++) nelmts*=dims[k]; if ((mtype_id=h5tools_get_native_type(ftype_id))<0) goto error; if ((msize=H5Tget_size(mtype_id))==0) goto error; /*------------------------------------------------------------------------- * check if the dataset creation property list has filters that * are not registered in the current configuration * 1) the external filters GZIP and SZIP might not be available * 2) the internal filters might be turned off *------------------------------------------------------------------------- */ if (h5tools_canreadf((NULL),dcpl_id)==1) { /*------------------------------------------------------------------------- * test for a valid output dataset *------------------------------------------------------------------------- */ dset_out = FAIL; /*------------------------------------------------------------------------- * object references are a special case * we cannot just copy the buffers, but instead we recreate the reference *------------------------------------------------------------------------- */ if (H5Tequal(mtype_id, H5T_STD_REF_OBJ)) { H5G_obj_t1 obj_type; hid_t refobj_id; hobj_ref_t *refbuf=NULL; /* buffer for object references */ hobj_ref_t *buf=NULL; const char* refname; unsigned u; /*------------------------------------------------------------------------- * read to memory *------------------------------------------------------------------------- */ if (nelmts) { buf=(void *) HDmalloc((unsigned)(nelmts*msize)); if ( buf==NULL){ printf( "cannot read into memory\n" ); goto error; } if (H5Dread(dset_in,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf)<0) goto error; if ((obj_type = H5Rget_obj_type(dset_in,H5R_OBJECT,buf))<0) goto error; refbuf=HDcalloc((unsigned)nelmts,msize); if ( refbuf==NULL){ printf( "cannot allocate memory\n" ); goto error; } for ( u=0; u<nelmts; u++) { H5E_BEGIN_TRY { if ((refobj_id = H5Rdereference(dset_in,H5R_OBJECT,&buf[u]))<0) continue; } H5E_END_TRY; /* get the name. a valid name could only occur in the second traversal of the file */ if ((refname=MapIdToName(refobj_id,travt))!=NULL) { /* create the reference, -1 parameter for objects */ if (H5Rcreate(&refbuf[u],fidout,refname,H5R_OBJECT,-1)<0) goto error; if(options->verbose) { printf(FORMAT_OBJ,"dset",travt->objs[i].name ); printf("object <%s> object reference created to <%s>\n", travt->objs[i].name, refname); } }/*refname*/ close_obj(obj_type,refobj_id); }/* u */ }/*nelmts*/ /*------------------------------------------------------------------------- * create/write dataset/close *------------------------------------------------------------------------- */ if ((dset_out=H5Dcreate(fidout,travt->objs[i].name,mtype_id,space_id,dcpl_id))<0) goto error; if (nelmts) { if (H5Dwrite(dset_out,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,refbuf)<0) goto error; } if (buf) free(buf); if (refbuf) free(refbuf); }/*H5T_STD_REF_OBJ*/ /*------------------------------------------------------------------------- * dataset region references *------------------------------------------------------------------------- */ else if (H5Tequal(mtype_id, H5T_STD_REF_DSETREG)) { H5G_obj_t1 obj_type; hid_t refobj_id; hdset_reg_ref_t *refbuf=NULL; /* input buffer for region references */ hdset_reg_ref_t *buf=NULL; /* output buffer */ const char* refname; unsigned u; /*------------------------------------------------------------------------- * read input to memory *------------------------------------------------------------------------- */ if (nelmts) { buf=(void *) HDmalloc((unsigned)(nelmts*msize)); if ( buf==NULL){ printf( "cannot read into memory\n" ); goto error; } if (H5Dread(dset_in,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf)<0) goto error; if ((obj_type = H5Rget_obj_type(dset_in,H5R_DATASET_REGION,buf))<0) goto error; /*------------------------------------------------------------------------- * create output *------------------------------------------------------------------------- */ refbuf=HDcalloc(sizeof(hdset_reg_ref_t),(size_t)nelmts); /*init to zero */ if ( refbuf==NULL){ printf( "cannot allocate memory\n" ); goto error; } for ( u=0; u<nelmts; u++) { H5E_BEGIN_TRY { if ((refobj_id = H5Rdereference(dset_in,H5R_DATASET_REGION,&buf[u]))<0) continue; } H5E_END_TRY; /* get the name. a valid name could only occur in the second traversal of the file */ if ((refname=MapIdToName(refobj_id,travt))!=NULL) { hid_t region_id; /* region id of the referenced dataset */ if ((region_id = H5Rget_region(dset_in,H5R_DATASET_REGION,&buf[u]))<0) goto error; /* create the reference, we need the space_id */ if (H5Rcreate(&refbuf[u],fidout,refname,H5R_DATASET_REGION,region_id)<0) goto error; if (H5Sclose(region_id)<0) goto error; if(options->verbose) { printf(FORMAT_OBJ,"dset",travt->objs[i].name ); printf("object <%s> region reference created to <%s>\n", travt->objs[i].name, refname); } }/*refname*/ close_obj(obj_type,refobj_id); }/* u */ }/*nelmts*/ /*------------------------------------------------------------------------- * create/write dataset/close *------------------------------------------------------------------------- */ if ((dset_out=H5Dcreate(fidout,travt->objs[i].name,mtype_id,space_id,dcpl_id))<0) goto error; if (nelmts) { if (H5Dwrite(dset_out,mtype_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,refbuf)<0) goto error; } if (buf) free(buf); if (refbuf) free(refbuf); } /* H5T_STD_REF_DSETREG */ /*------------------------------------------------------------------------- * not references, open previously created object in 1st traversal *------------------------------------------------------------------------- */ else { if ((dset_out=H5Dopen(fidout,travt->objs[i].name))<0) goto error; } assert(dset_out!=FAIL); /*------------------------------------------------------------------------- * copy referenced objects in attributes *------------------------------------------------------------------------- */ if (copy_refs_attr(dset_in,dset_out,options,travt,fidout)<0) goto error; /*------------------------------------------------------------------------- * check for hard links *------------------------------------------------------------------------- */ if (travt->objs[i].nlinks) { for ( j=0; j<travt->objs[i].nlinks; j++){ H5Glink(fidout, H5G_LINK_HARD, travt->objs[i].name, travt->objs[i].links[j].new_name); } } if (H5Dclose(dset_out)<0) goto error; }/*can_read*/
/* * test_objnames * Tests that UTF-8 can be used for object names in the file. * Tests groups, datasets, named datatypes, and soft links. * Note that this test doesn't actually mark the names as being * in UTF-8. At the time this test was written, that feature * didn't exist in HDF5, and when the character encoding property * was added to links it didn't change how they were stored in the file, * -JML 2/2/2006 */ void test_objnames(hid_t fid, const char* string) { hid_t grp_id, grp1_id, grp2_id, grp3_id; hid_t type_id, dset_id, space_id; char read_buf[MAX_STRING_LENGTH]; char path_buf[MAX_PATH_LENGTH]; hsize_t dims=1; hobj_ref_t obj_ref; herr_t ret; /* Create a group with a UTF-8 name */ grp_id = H5Gcreate2(fid, string, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp_id, FAIL, "H5Gcreate2"); /* Set a comment on the group to test that we can access the group * Also test that UTF-8 comments can be read. */ ret = H5Oset_comment_by_name(fid, string, string, H5P_DEFAULT); CHECK(ret, FAIL, "H5Oset_comment_by_name"); ret = H5Oget_comment_by_name(fid, string, read_buf, (size_t)MAX_STRING_LENGTH, H5P_DEFAULT); CHECK(ret, FAIL, "H5Oget_comment_by_name"); ret = H5Gclose(grp_id); CHECK(ret, FAIL, "H5Gclose"); VERIFY(HDstrcmp(string, read_buf), 0, "strcmp"); /* Create a new dataset with a UTF-8 name */ grp1_id = H5Gcreate2(fid, GROUP1_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp1_id, FAIL, "H5Gcreate2"); space_id = H5Screate_simple(RANK, &dims, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dset_id = H5Dcreate2(grp1_id, string, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(dset_id, FAIL, "H5Dcreate2"); /* Make sure that dataset can be opened again */ ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); dset_id = H5Dopen2(grp1_id, string, H5P_DEFAULT); CHECK(ret, FAIL, "H5Dopen2"); ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Gclose(grp1_id); CHECK(ret, FAIL, "H5Gclose"); /* Do the same for a named datatype */ grp2_id = H5Gcreate2(fid, GROUP2_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp2_id, FAIL, "H5Gcreate2"); type_id = H5Tcreate(H5T_OPAQUE, (size_t)1); CHECK(type_id, FAIL, "H5Tcreate"); ret = H5Tcommit2(grp2_id, string, type_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(type_id, FAIL, "H5Tcommit2"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); type_id = H5Topen2(grp2_id, string, H5P_DEFAULT); CHECK(type_id, FAIL, "H5Topen2"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); /* Don't close the group -- use it to test that object references * can refer to objects named in UTF-8 */ space_id = H5Screate_simple(RANK, &dims, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dset_id = H5Dcreate2(grp2_id, DSET3_NAME, H5T_STD_REF_OBJ, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Dcreate2"); /* Create reference to named datatype */ ret = H5Rcreate(&obj_ref, grp2_id, string, H5R_OBJECT, -1); CHECK(ret, FAIL, "H5Rcreate"); /* Write selection and read it back*/ ret = H5Dwrite(dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &obj_ref); CHECK(ret, FAIL, "H5Dwrite"); ret = H5Dread(dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &obj_ref); CHECK(ret, FAIL, "H5Dread"); /* Ensure that we can open named datatype using object reference */ type_id = H5Rdereference2(dset_id, H5P_DEFAULT, H5R_OBJECT, &obj_ref); CHECK(type_id, FAIL, "H5Rdereference2"); ret = H5Tcommitted(type_id); VERIFY(ret, 1, "H5Tcommitted"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); ret = H5Gclose(grp2_id); CHECK(ret, FAIL, "H5Gclose"); /* Create "group3". Build a hard link from group3 to group2, which has * a datatype with the UTF-8 name. Create a soft link in group3 * pointing through the hard link to the datatype. Give the soft * link a name in UTF-8. Ensure that the soft link works. */ grp3_id = H5Gcreate2(fid, GROUP3_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp3_id, FAIL, "H5Gcreate2"); ret = H5Lcreate_hard(fid, GROUP2_NAME, grp3_id, GROUP2_NAME, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Lcreate_hard"); HDstrcpy(path_buf, GROUP2_NAME); HDstrcat(path_buf, "/"); HDstrcat(path_buf, string); ret = H5Lcreate_hard(grp3_id, path_buf, H5L_SAME_LOC, string, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Lcreate_hard"); /* Open named datatype using soft link */ type_id = H5Topen2(grp3_id, string, H5P_DEFAULT); CHECK(type_id, FAIL, "H5Topen2"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); ret = H5Gclose(grp3_id); CHECK(ret, FAIL, "H5Gclose"); }