void HDFWalkerIOEngine::readAll(hid_t grp, const char* name, Communicate* comm) { int mynode=comm->rank(); int nprocs=comm->size(); vector<int> woffset(nprocs+1,0); const int RANK = 3; hsize_t offset[]={1,1,1}; hsize_t gcount[RANK],count[RANK]; hsize_t stride[]={1,1,1}; hid_t dataset = H5Dopen(grp,name); hid_t dataspace = H5Dget_space(dataset); int rank_n = H5Sget_simple_extent_ndims(dataspace); int status_n = H5Sget_simple_extent_dims(dataspace, gcount, NULL); //assign offsets and size FairDivideLow(gcount[0],nprocs,woffset); offset[0]=woffset[mynode]; offset[1] = 0; offset[2] = 0; count[0]=woffset[mynode+1]-woffset[mynode]; count[1]=gcount[1]; count[2]=gcount[2]; app_log() << " Initial walker distribution: "; std::copy(woffset.begin(),woffset.end(),ostream_iterator<int>(app_log()," ")); app_log() << endl; vector<MCWalkerConfiguration::PosType> posIn(count[0]*count[1]); hid_t memspace = H5Screate_simple(RANK, count, NULL); herr_t status = H5Sselect_hyperslab(dataspace,H5S_SELECT_SET, offset,NULL,count,NULL); #if defined(H5_HAVE_PARALLEL) xfer_plist = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(xfer_plist,H5FD_MPIO_COLLECTIVE); #else xfer_plist = H5P_DEFAULT; #endif hid_t type_id=get_h5_datatype(posIn[0][0]); status = H5Dread(dataset, type_id, memspace, dataspace, xfer_plist, &(posIn[0][0])); H5Sclose(dataspace); H5Sclose(memspace); H5Dclose(dataset); #if defined(H5_HAVE_PARALLEL) H5Pclose(xfer_plist); #endif int curWalker = W.getActiveWalkers(); if(curWalker) { W.createWalkers(count[0]); } else { W.resize(count[0],count[1]); } MCWalkerConfiguration::iterator it = W.begin()+curWalker; int ii=0; for(int iw=0; iw<count[0]; iw++) { //std::copy(Post_temp[iw],Post_temp[iw+1], (*it)->R.begin()); for(int iat=0; iat < count[1]; iat++,ii++){ (*it)->R(iat) = posIn[ii]; } ++it; } }
bool ReadHDF5file(char* filename, char* fieldname, dtype** outArray, int* dims) { #ifdef _HDF5_H // Open the file hid_t file_id; file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); //? file_id = H5Fopen(filename,H5F_ACC_RDONLY,faplist_id); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); return false; } // Open the dataset hid_t dataset_id; hid_t dataspace_id; dataset_id = H5Dopen1(file_id, fieldname); if(dataset_id < 0){ printf("ERROR: Could not open the data field %s\n",fieldname); return false; } dataspace_id = H5Dget_space(dataset_id); // Test if 2D data int ndims; ndims = H5Sget_simple_extent_ndims(dataspace_id); // Get dimensions of data set (nx, ny, nn) hsize_t* dimsl = new hsize_t[ndims]; H5Sget_simple_extent_dims(dataspace_id,dimsl,NULL); for(int i = 0;(i<ndims&&i<3);i++) dims[i] = dimsl[ndims-1-i]; //!!!!!!!! NOT SURE size_t nn = 1; for(int i = 0;i<ndims;i++) nn *= dimsl[i]; // Create space for the new data dtype* data = *outArray; if (data!=NULL) delete data;//free(data); *outArray = new dtype[nn]; data = *outArray; hid_t datatype_id; H5T_class_t dataclass; size_t size; datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); int rrr = sizeof(int); if(dataclass == H5T_FLOAT){ if (size == sizeof(float)) { float* buffer = (float *) calloc(nn, sizeof(float)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(double)) { double* buffer = (double *) calloc(nn, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown floating point type, size=%i\n",(int) size); return false; } } else if(dataclass == H5T_INTEGER){ if (size == sizeof(short)) { short* buffer = (short*) calloc(nn, sizeof(short)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(int)) { int* buffer = (int *) calloc(nn, sizeof(int)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(long)) { long* buffer = (long *) calloc(nn, sizeof(long)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown integer type, size=%i\n",(int) size); return false; } } else { printf("2dData::readHDF5: unknown HDF5 data type\n"); return false; } // Close and cleanup H5Dclose(dataset_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); return true; #endif return false; }
herr_t HDF5CreateGroupObjs(hid_t hHDF5, const char *pszObjName, void *poHObjParent) { hid_t hGroupID; /* identifier of group */ hid_t hDatasetID; /* identifier of dataset */ hsize_t nbObjs=0; /* number of objects in a group */ int nbAttrs=0; /* number of attributes in object */ unsigned idx; int n_dims; H5G_stat_t oStatbuf; hsize_t *dims=NULL; hsize_t *maxdims=NULL; hid_t datatype; hid_t dataspace; hid_t native; HDF5GroupObjects *poHchild; HDF5GroupObjects *poHparent; poHparent = ( HDF5GroupObjects * ) poHObjParent; poHchild=poHparent->poHchild; if( H5Gget_objinfo( hHDF5, pszObjName, FALSE, &oStatbuf ) < 0 ) return -1; /* -------------------------------------------------------------------- */ /* Look for next child */ /* -------------------------------------------------------------------- */ for( idx=0; idx < poHparent->nbObjs; idx++ ) { if( poHchild->pszName == NULL ) break; poHchild++; } if( idx == poHparent->nbObjs ) return -1; // all children parsed /* -------------------------------------------------------------------- */ /* Save child information */ /* -------------------------------------------------------------------- */ poHchild->pszName = CPLStrdup( pszObjName ); poHchild->nType = oStatbuf.type; poHchild->nIndex = idx; poHchild->poHparent = poHparent; poHchild->nRank = 0; poHchild->paDims = 0; poHchild->HDatatype = 0; poHchild->objno[0] = oStatbuf.objno[0]; poHchild->objno[1] = oStatbuf.objno[1]; if( poHchild->pszPath == NULL ) { poHchild->pszPath = CreatePath( poHchild ); } if( poHparent->pszPath == NULL ) { poHparent->pszPath = CreatePath( poHparent ); } switch ( oStatbuf.type ) { case H5G_LINK: poHchild->nbAttrs = 0; poHchild->nbObjs = 0; poHchild->poHchild = NULL; poHchild->nRank = 0; poHchild->paDims = 0; poHchild->HDatatype = 0; break; case H5G_GROUP: if( ( hGroupID = H5Gopen( hHDF5, pszObjName ) ) == -1 ) { printf( "Error: unable to access \"%s\" group.\n", pszObjName ); return -1; } nbAttrs = H5Aget_num_attrs( hGroupID ); H5Gget_num_objs( hGroupID, &nbObjs ); poHchild->nbAttrs= nbAttrs; poHchild->nbObjs = (int) nbObjs; poHchild->nRank = 0; poHchild->paDims = 0; poHchild->HDatatype = 0; if( nbObjs > 0 ) { poHchild->poHchild =( HDF5GroupObjects * ) CPLCalloc( (int)nbObjs, sizeof( HDF5GroupObjects ) ); memset( poHchild->poHchild, 0, (size_t) (sizeof( HDF5GroupObjects ) * nbObjs) ); } else poHchild->poHchild = NULL; if( !HDF5GroupCheckDuplicate( poHparent, oStatbuf.objno ) ) H5Giterate( hHDF5, pszObjName, NULL, HDF5CreateGroupObjs, (void*) poHchild ); else CPLDebug( "HDF5", "avoiding link looping on node '%s'.", pszObjName ); H5Gclose( hGroupID ); break; case H5G_DATASET: if( ( hDatasetID = H5Dopen( hHDF5, pszObjName ) ) == -1 ) { printf( "Error: unable to access \"%s\" dataset.\n", pszObjName ); return -1; } nbAttrs = H5Aget_num_attrs( hDatasetID ); datatype = H5Dget_type( hDatasetID ); dataspace = H5Dget_space( hDatasetID ); n_dims = H5Sget_simple_extent_ndims( dataspace ); native = H5Tget_native_type( datatype, H5T_DIR_ASCEND ); if( n_dims > 0 ) { dims = (hsize_t *) CPLCalloc( n_dims,sizeof( hsize_t ) ); maxdims = (hsize_t *) CPLCalloc( n_dims,sizeof( hsize_t ) ); } H5Sget_simple_extent_dims( dataspace, dims, maxdims ); if( maxdims != NULL ) CPLFree( maxdims ); if( n_dims > 0 ) { poHchild->nRank = n_dims; // rank of the array poHchild->paDims = dims; // dimmension of the array. poHchild->HDatatype = datatype; // HDF5 datatype } else { poHchild->nRank = -1; poHchild->paDims = NULL; poHchild->HDatatype = 0; } poHchild->nbAttrs = nbAttrs; poHchild->nbObjs = 0; poHchild->poHchild = NULL; poHchild->native = native; H5Tclose( datatype ); H5Sclose( dataspace ); H5Dclose( hDatasetID ); break; case H5G_TYPE: poHchild->nbAttrs = 0; poHchild->nbObjs = 0; poHchild->poHchild = NULL; poHchild->nRank = 0; poHchild->paDims = 0; poHchild->HDatatype = 0; break; default: break; } return 0; }
/* Given a varid, find its shape. For unlimited dimensions, return the current number of records. */ static int find_var_shape_grp(NC_GRP_INFO_T *grp, int varid, int *ndims, int *dimid, size_t *dimlen) { hid_t datasetid = 0, spaceid = 0; NC_VAR_INFO_T *var; hsize_t *h5dimlen = NULL, *h5dimlenmax = NULL; int d, dataset_ndims = 0; int retval = NC_NOERR; /* Find this var. */ for (var = grp->var; var; var = var->next) if (var->varid == varid) break; if (!var) return NC_ENOTVAR; /* Get the dimids and the ndims for this var. */ if (ndims) *ndims = var->ndims; if (dimid) for (d = 0; d < var->ndims; d++) dimid[d] = var->dimids[d]; if (dimlen) { /* If the var hasn't been created yet, its size is 0. */ if (!var->created) { for (d = 0; d < var->ndims; d++) dimlen[d] = 0; } else { /* Get the number of records in the dataset. */ if ((retval = nc4_open_var_grp2(grp, var->varid, &datasetid))) BAIL(retval); if ((spaceid = H5Dget_space(datasetid)) < 0) BAIL(NC_EHDFERR); #ifdef EXTRA_TESTS num_spaces++; #endif /* If it's a scalar dataset, it has length one. */ if (H5Sget_simple_extent_type(spaceid) == H5S_SCALAR) { dimlen[0] = 1; } else { /* Check to make sure ndims is right, then get the len of each dim in the space. */ if ((dataset_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) BAIL(NC_EHDFERR); if (ndims && dataset_ndims != *ndims) BAIL(NC_EHDFERR); if (!(h5dimlen = malloc(dataset_ndims * sizeof(hsize_t)))) BAIL(NC_ENOMEM); if (!(h5dimlenmax = malloc(dataset_ndims * sizeof(hsize_t)))) BAIL(NC_ENOMEM); if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid, h5dimlen, h5dimlenmax)) < 0) BAIL(NC_EHDFERR); LOG((5, "find_var_shape_nc: varid %d len %d max: %d", varid, (int)h5dimlen[0], (int)h5dimlenmax[0])); for (d=0; d<dataset_ndims; d++) dimlen[d] = h5dimlen[d]; } } } exit: if (spaceid > 0 && H5Sclose(spaceid) < 0) BAIL2(NC_EHDFERR); #ifdef EXTRA_TESTS num_spaces--; #endif if (h5dimlen) free(h5dimlen); if (h5dimlenmax) free(h5dimlenmax); return retval; }
int RD_verify_dataset(hid_t file_id, const char *dset_name, int ndims, int *dims, hid_t dtype, int dims_known) { hid_t dset_id, dspc_id, dtyp_id; herr_t err; /* data sizes */ hsize_t *this_dims; int i; /* get dataset */ dset_id = H5Dopen2(file_id, dset_name, H5P_DEFAULT); if (!RD_ID_ISVALID(dset_id)) return RD_ERROR_DSETNOTVALID; /* get dataspace */ dspc_id = H5Dget_space(dset_id); if (!RD_ID_ISVALID(dspc_id)) return RD_ERROR_DSPACENOTVALID; /* check number of dimensions */ if (H5Sget_simple_extent_ndims(dspc_id) != ndims) return RD_ERROR_DSETBADNDIMS; /* get dimensions */ this_dims = malloc(sizeof(*this_dims)*ndims); if (H5Sget_simple_extent_dims(dspc_id, this_dims, NULL) < 0) return RD_ERROR_DSETBADDIMS; /* copy into or check agains dims */ if (dims_known) { for (i=0; i<ndims; i++) { if (dims[i] != (int)(this_dims[i])) return RD_ERROR_DSETBADDIMS; } } else { for (i=0; i<ndims; i++) { dims[i] = (int)(this_dims[i]); } } /* clean up */ free(this_dims); /* close dataspace */ err = H5Sclose(dspc_id); if (err < 0) return RD_ERROR_DSPACENOTVALID; /* get datatype */ dtyp_id = H5Dget_type(dset_id); if (!RD_ID_ISVALID(dtyp_id)) return RD_ERROR_DTYPENOTVALID; if (H5Tequal(dtyp_id, dtype) <= 0) return RD_ERROR_DTYPENOTVALID; /* close dataset */ err = H5Tclose(dtyp_id); if (err < 0) return RD_ERROR_DTYPENOTVALID; /* close dataset */ err = H5Dclose(dset_id); if (err < 0) return RD_ERROR_DSETNOTVALID; return 0; }
static herr_t trav_attr(hid_t #ifndef H5TRAV_PRINT_SPACE H5_ATTR_UNUSED #endif /* H5TRAV_PRINT_SPACE */ obj, const char *attr_name, const H5A_info_t H5_ATTR_UNUSED *ainfo, void *_op_data) { trav_path_op_data_t *op_data = (trav_path_op_data_t *)_op_data; const char *buf = op_data->path; if((strlen(buf)==1) && (*buf=='/')) printf(" %-10s %s%s", "attribute", buf, attr_name); else printf(" %-10s %s/%s", "attribute", buf, attr_name); #ifdef H5TRAV_PRINT_SPACE if(trav_verbosity < 2) { #endif printf("\n"); #ifdef H5TRAV_PRINT_SPACE } else { hid_t attr = -1; hid_t space = -1; hsize_t size[H5S_MAX_RANK]; int ndims; int i; H5S_class_t space_type; if((attr = H5Aopen(obj, attr_name, H5P_DEFAULT))) { space = H5Aget_space(attr); /* Data space */ ndims = H5Sget_simple_extent_dims(space, size, NULL); space_type = H5Sget_simple_extent_type(space); switch(space_type) { case H5S_SCALAR: /* scalar dataspace */ printf(" scalar\n"); break; case H5S_SIMPLE: /* simple dataspace */ printf(" {"); for (i = 0; i < ndims; i++) { printf("%s" HSIZE_T_FORMAT, i?", ":"", size[i]); } printf("}\n"); break; case H5S_NULL: /* null dataspace */ printf(" null\n"); break; default: /* Unknown dataspace type */ printf(" unknown\n"); break; } /* end switch */ H5Sclose(space); H5Aclose(attr); } } #endif return(0); }
int arma_H5Sget_simple_extent_dims(hid_t space_id, hsize_t* dims, hsize_t* maxdims) { return H5Sget_simple_extent_dims(space_id, dims, maxdims); }
bool Hdf5Dataset::getMultimap(MultiMap &mMap) { hsize_t dims_out[2], count[2], offset[2]; hid_t dataspace = H5Dget_space(this->poses_dataset_); /* dataspace handle */ int rank = H5Sget_simple_extent_ndims(dataspace); herr_t status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL); herr_t status; int chunk_size, chunk_itr; if (dims_out[0] % 10) { chunk_itr = 11; } else { chunk_itr = 10; } chunk_size = (dims_out[0] / 10); offset[0] = 0; for (int it = 0; it < chunk_itr; it++) { offset[1] = 0; if ((dims_out[0] - (chunk_size * it)) / chunk_size != 0) { count[0] = chunk_size; offset[0] = chunk_size * it; } else { count[0] = (dims_out[0] - (chunk_size * it)); offset[0] = count[0]; } count[1] = 10; double data_out[count[0]][count[1]]; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); hsize_t dimsm[2]; dimsm[0] = count[0]; dimsm[1] = count[1]; hid_t memspace; memspace = H5Screate_simple(RANK_OUT, dimsm, NULL); status = H5Dread(this->poses_dataset_, H5T_NATIVE_DOUBLE, memspace, dataspace, H5P_DEFAULT, data_out); for(int i=0; i<count[0]; i++) { std::vector<double> sphere_center(3); std::vector<double> Poses(7); for(int j=0; j<3;j++) { sphere_center[j] = data_out[i][j]; } for(int k=3;k<10; k++) { Poses[k-3] = data_out[i][k]; } mMap.insert(std::make_pair(sphere_center, Poses)); } } return 0; }
void Hdf5DataReader::CommonConstructor() { if (!mDirectory.IsDir() || !mDirectory.Exists()) { EXCEPTION("Directory does not exist: " + mDirectory.GetAbsolutePath()); } std::string file_name = mDirectory.GetAbsolutePath() + mBaseName + ".h5"; FileFinder h5_file(file_name, RelativeTo::Absolute); if (!h5_file.Exists()) { EXCEPTION("Hdf5DataReader could not open " + file_name + " , as it does not exist."); } // Open the file and the main dataset mFileId = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); if (mFileId <= 0) { EXCEPTION("Hdf5DataReader could not open " << file_name << " , H5Fopen error code = " << mFileId); } mVariablesDatasetId = H5Dopen(mFileId, mDatasetName.c_str(), H5P_DEFAULT); SetMainDatasetRawChunkCache(); if (mVariablesDatasetId <= 0) { H5Fclose(mFileId); EXCEPTION("Hdf5DataReader opened " << file_name << " but could not find the dataset '" << mDatasetName.c_str() << "', H5Dopen error code = " << mVariablesDatasetId); } hid_t variables_dataspace = H5Dget_space(mVariablesDatasetId); mVariablesDatasetRank = H5Sget_simple_extent_ndims(variables_dataspace); // Get the dataset/dataspace dimensions hsize_t dataset_max_sizes[AbstractHdf5Access::DATASET_DIMS]; H5Sget_simple_extent_dims(variables_dataspace, mDatasetDims, dataset_max_sizes); for (unsigned i=1; i<AbstractHdf5Access::DATASET_DIMS; i++) // Zero is excluded since it may be unlimited { assert(mDatasetDims[i] == dataset_max_sizes[i]); } // Check if an unlimited dimension has been defined if (dataset_max_sizes[0] == H5S_UNLIMITED) { // In terms of an Unlimited dimension dataset: // * Files pre - r16738 (inc. Release 3.1 and earlier) use simply "Time" for "Data"'s unlimited variable. // * Files generated by r16738 - r18257 used "<DatasetName>_Time" for "<DatasetName>"'s unlimited variable, // - These are not to be used and there is no backwards compatibility for them, since they weren't in a release. // * Files post r18257 (inc. Release 3.2 onwards) use "<DatasetName>_Unlimited" for "<DatasetName>"'s // unlimited variable, // - a new attribute "Name" has been added to the Unlimited Dataset to allow it to assign // any name to the unlimited variable. Which can then be easily read by this class. // - if this dataset is missing we look for simply "Time" to remain backwards compatible with Releases <= 3.1 SetUnlimitedDatasetId(); hid_t timestep_dataspace = H5Dget_space(mUnlimitedDatasetId); // Get the dataset/dataspace dimensions H5Sget_simple_extent_dims(timestep_dataspace, &mNumberTimesteps, NULL); } // Get the attribute where the name of the variables are stored hid_t attribute_id = H5Aopen_name(mVariablesDatasetId, "Variable Details"); // Get attribute datatype, dataspace, rank, and dimensions hid_t attribute_type = H5Aget_type(attribute_id); hid_t attribute_space = H5Aget_space(attribute_id); hsize_t attr_dataspace_dim; H5Sget_simple_extent_dims(attribute_space, &attr_dataspace_dim, NULL); unsigned num_columns = H5Sget_simple_extent_npoints(attribute_space); char* string_array = (char *)malloc(sizeof(char)*MAX_STRING_SIZE*(int)num_columns); H5Aread(attribute_id, attribute_type, string_array); // Loop over column names and store them. for (unsigned index=0; index < num_columns; index++) { // Read the string from the raw vector std::string column_name_unit(&string_array[MAX_STRING_SIZE*index]); // Find beginning of unit definition. size_t name_length = column_name_unit.find('('); size_t unit_length = column_name_unit.find(')') - name_length - 1; std::string column_name = column_name_unit.substr(0, name_length); std::string column_unit = column_name_unit.substr(name_length+1, unit_length); mVariableNames.push_back(column_name); mVariableToColumnIndex[column_name] = index; mVariableToUnit[column_name] = column_unit; } // Release all the identifiers H5Tclose(attribute_type); H5Sclose(attribute_space); H5Aclose(attribute_id); // Free allocated memory free(string_array); // Find out if it's incomplete data H5E_BEGIN_TRY //Supress HDF5 error if the IsDataComplete name isn't there { attribute_id = H5Aopen_name(mVariablesDatasetId, "IsDataComplete"); } H5E_END_TRY; if (attribute_id < 0) { // This is in the old format (before we added the IsDataComplete attribute). // Just quit (leaving a nasty hdf5 error). return; } attribute_type = H5Aget_type(attribute_id); attribute_space = H5Aget_space(attribute_id); unsigned is_data_complete; H5Aread(attribute_id, H5T_NATIVE_UINT, &is_data_complete); // Release all the identifiers H5Tclose(attribute_type); H5Sclose(attribute_space); H5Aclose(attribute_id); mIsDataComplete = (is_data_complete == 1) ? true : false; if (is_data_complete) { return; } // Incomplete data // Read the vector thing attribute_id = H5Aopen_name(mVariablesDatasetId, "NodeMap"); attribute_type = H5Aget_type(attribute_id); attribute_space = H5Aget_space(attribute_id); // Get the dataset/dataspace dimensions unsigned num_node_indices = H5Sget_simple_extent_npoints(attribute_space); // Read data from hyperslab in the file into the hyperslab in memory mIncompleteNodeIndices.clear(); mIncompleteNodeIndices.resize(num_node_indices); H5Aread(attribute_id, H5T_NATIVE_UINT, &mIncompleteNodeIndices[0]); H5Tclose(attribute_type); H5Sclose(attribute_space); H5Aclose(attribute_id); }
herr_t H5IMget_palette_info( hid_t loc_id, const char *image_name, int pal_number, hsize_t *pal_dims ) { hid_t did; int has_pal; hid_t atid=-1; hid_t aid; hid_t asid=-1; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; hid_t pal_space_id; hsize_t pal_maxdims[2]; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(did); if(has_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 the reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = n_refs; refbuf = (hobj_ref_t*)malloc( sizeof(hobj_ref_t) * (int)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* Get the actual palette */ if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0) goto out; if ( (pal_space_id = H5Dget_space( pal_id )) < 0) goto out; if ( H5Sget_simple_extent_ndims( pal_space_id ) < 0) goto out; if ( H5Sget_simple_extent_dims( pal_space_id, pal_dims, pal_maxdims ) < 0) goto out; /* close */ if (H5Dclose(pal_id)<0) goto out; if ( H5Sclose( pal_space_id ) < 0) goto out; if ( H5Sclose( asid ) < 0) goto out; if ( H5Tclose( atid ) < 0) goto out; if ( H5Aclose( aid ) < 0) goto out; free( refbuf ); } /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); H5Sclose( asid ); H5Tclose( atid ); H5Aclose( aid ); return -1; }
/*------------------------------------------------------------------------- * Function: check_file * * Purpose: Part 2 of a two-part H5Fflush() test. * * Return: Success: 0 * * Failure: 1 * * Programmer: Leon Arber * Sept. 26, 2006. * *------------------------------------------------------------------------- */ static int check_file(char* name, hid_t fapl) { hid_t file, space, dset, groups, grp, plist; hsize_t ds_size[2]; double error; hsize_t i, j; plist = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist, H5FD_MPIO_COLLECTIVE); if((file = H5Fopen(name, H5F_ACC_RDONLY, fapl)) < 0) goto error; /* Open the dataset */ if((dset = H5Dopen2(file, "dset", H5P_DEFAULT)) < 0) goto error; if((space = H5Dget_space(dset)) < 0) goto error; if(H5Sget_simple_extent_dims(space, ds_size, NULL) < 0) goto error; assert(100==ds_size[0] && 100==ds_size[1]); /* Read some data */ if (H5Dread(dset, H5T_NATIVE_DOUBLE, space, space, plist, the_data) < 0) goto error; for (i=0; i<ds_size[0]; i++) { for (j=0; j<ds_size[1]; j++) { /* * The extra cast in the following statement is a bug workaround * for the Win32 version 5.0 compiler. * 1998-11-06 ptl */ error = fabs(the_data[i][j]-(double)(hssize_t)i/((hssize_t)j+1)); if (error>0.0001) { H5_FAILED(); printf(" dset[%lu][%lu] = %g\n", (unsigned long)i, (unsigned long)j, the_data[i][j]); printf(" should be %g\n", (double)(hssize_t)i/(hssize_t)(j+1)); goto error; } } } /* Open some groups */ if((groups = H5Gopen2(file, "some_groups", H5P_DEFAULT)) < 0) goto error; for(i = 0; i < 100; i++) { sprintf(name, "grp%02u", (unsigned)i); if((grp = H5Gopen2(groups, name, H5P_DEFAULT)) < 0) goto error; if(H5Gclose(grp) < 0) goto error; } if(H5Gclose(groups) < 0) goto error; if(H5Dclose(dset) < 0) goto error; if(H5Fclose(file) < 0) goto error; if(H5Pclose(plist) < 0) goto error; if(H5Sclose(space) < 0) goto error; return 0; error: H5E_BEGIN_TRY { H5Pclose(plist); H5Gclose(groups); H5Dclose(dset); H5Fclose(file); H5Sclose(space); } H5E_END_TRY; return 1; }
herr_t H5IMget_image_info( hid_t loc_id, const char *dset_name, hsize_t *width, hsize_t *height, hsize_t *planes, char *interlace, hssize_t *npals ) { hid_t did; hid_t sid; hsize_t dims[IMAGE24_RANK]; hid_t aid; hid_t asid; hid_t atid; H5T_class_t aclass; int has_pal; int has_attr; /*assume initially we have no palettes attached*/ *npals = 0; /* Open the dataset. */ if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "INTERLACE_MODE" on the >>image<< dataset */ has_attr = H5LT_find_attribute(did, "INTERLACE_MODE"); /* It exists, get it */ if(has_attr == 1) { if((aid = H5Aopen(did, "INTERLACE_MODE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if(H5Aread(aid, atid, interlace) < 0) goto out; if(H5Tclose(atid) < 0) goto out; if(H5Aclose(aid) < 0) goto out; } /* Get the dataspace handle */ if ( (sid = H5Dget_space( did )) < 0) goto out; /* Get dimensions */ if ( H5Sget_simple_extent_dims( sid, dims, NULL) < 0) goto out; /* Initialize the image dimensions */ if ( has_attr == 1 ) /* This is a 24 bit image */ { if ( strcmp( interlace, "INTERLACE_PIXEL" ) == 0 ) { /* Number of color planes is defined as the third dimension */ *height = dims[0]; *width = dims[1]; *planes = dims[2]; } else if ( strcmp( interlace, "INTERLACE_PLANE" ) == 0 ) { /* Number of color planes is defined as the first dimension */ *planes = dims[0]; *height = dims[1]; *width = dims[2]; } else return -1; } else /* This is a 8 bit image */ { *height = dims[0]; *width = dims[1]; *planes = 1; } /* Close */ if ( H5Sclose( sid ) < 0) goto out; /* Get number of palettes */ /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(did); if(has_pal == 1) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if((aclass = H5Tget_class(atid)) < 0) goto out; /* Check if it is really a reference */ if(aclass == H5T_REFERENCE) { /* Get the reference(s) */ if ( (asid = H5Aget_space( aid )) < 0) goto out; *npals = H5Sget_simple_extent_npoints( asid ); if ( H5Sclose( asid ) < 0) goto out; } /* H5T_REFERENCE */ if ( H5Tclose( atid ) < 0) goto out; /* Close the attribute. */ if ( H5Aclose( aid ) < 0) goto out; } /* End access to the dataset and release resources used by it. */ if ( H5Dclose( did ) < 0) goto out; return 0; out: H5Dclose( did ); H5Aclose( aid ); H5Sclose( asid ); H5Tclose( atid ); return -1; }
void XFLASH_File::Open_HDF5_Chombo(void) { hid_t handle = (hid_t)m_iFile_Handle; unsigned int nRefineLevels, numVariables, dim; unsigned int n_rBlocks ; hsize_t nB; hid_t gid, dspace, dset, attrib, varNameType; herr_t status; char component_name[MAX_STRING_LENGTH]; char level_name[MAX_STRING_LENGTH]; char tmpVarName[FR_VAR_STRING_SIZE + 1]; hid_t tmpType; // float dx; // int *buf; int idx_bbox; unsigned int i, j, k; /*prepare file struct*/ // nodetype=NULL; /*We are not going to use these. But sfocu code checks for them so make them * trivially zero*/ for( i=0; i < FR_MDIM; i++) m_uiBlock_Dimensions[i] =0; m_eFormat = FMT_HDF5_CHOMBO; /*get total number of refinement level*/ attrib= H5Aopen_name( handle, "num_levels"); status= H5Aread(attrib, H5T_NATIVE_INT, (void *)&nRefineLevels); H5Aclose(attrib); m_uiNum_Levels = nRefineLevels; /*Dimensions*/ gid= H5Gopen(handle, "Chombo_global"); attrib= H5Aopen_name( gid, "SpaceDim"); status= H5Aread(attrib, H5T_NATIVE_INT, (void *)&dim); H5Aclose(attrib); m_uiNum_Dimensions = dim; H5Gclose(gid); /*number of components*/ attrib= H5Aopen_name( handle, "num_components"); status= H5Aread(attrib, H5T_NATIVE_INT, (void *)&numVariables); H5Aclose(attrib); m_uiNum_Vars = numVariables; /*Get component names*/ varNameType = H5Tcopy(H5T_C_S1); H5Tset_size(varNameType, 4); tmpVarName[FR_VAR_STRING_SIZE ] ='\0'; m_lpszVar_Names = new char *[numVariables]; m_lpeVar_Types = new XFLASH_VAR_TYPE [numVariables]; for(i=0; i < numVariables; i++) { sprintf(component_name,"component_%d", i); attrib= H5Aopen_name( handle, component_name); status= H5Aread(attrib, varNameType, (void *)tmpVarName); H5Aclose(attrib); int iLen = strlen(tmpVarName) + 1; if (iLen > FR_VAR_STRING_SIZE) iLen = FR_VAR_STRING_SIZE; m_lpszVar_Names[i] = new char[iLen]; strncpy(m_lpszVar_Names[i], (char *)tmpVarName, FR_VAR_STRING_SIZE); m_lpszVar_Names[i][iLen - 1] = 0; m_lpeVar_Types[i] = VT_UNK; } H5Tclose(varNameType); /* Get Total Number of Blocks, refine level for each Block */ m_uiNum_Blocks = 0; m_lpuiBlocks_Per_Refine_Level = new unsigned int [nRefineLevels]; idx_bbox=0; int * buf=NULL; unsigned int uiBufSize = 0; for(i =0; i < nRefineLevels; i++) { sprintf(level_name, "level_%d", i); gid= H5Gopen(handle, level_name); dset= H5Dopen(gid, "boxes"); dspace = H5Dget_space(dset); status = H5Sget_simple_extent_dims(dspace, &nB, NULL); n_rBlocks = (int)nB; // int block0 = m_uiNum_Blocks; m_uiNum_Blocks += n_rBlocks; m_lpuiBlocks_Per_Refine_Level[i] = n_rBlocks; /*get bounding box for all the blocks on this refine level */ /* attrib= H5Aopen_name( gid, "dx"); status= H5Aread(attrib, H5T_NATIVE_FLOAT, (void *)&dx); H5Aclose(attrib); */ unsigned int uiSize = n_rBlocks * (2 * dim + PADSIZE / 4); if (uiSize > uiBufSize) { if (buf) delete [] buf; buf = new int [uiSize]; uiBufSize = uiSize; } tmpType= H5Dget_type(dset); status= H5Dread(dset, tmpType, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf); H5Tclose(tmpType); m_lpdBlock_Bounding_Box = new double [m_uiNum_Blocks * 2 * dim]; /*unroll struct into the out->bbox double array*/ for(j = 0; j < n_rBlocks; j++) { for(k=0; k < (dim *2); k++) { m_lpdBlock_Bounding_Box[idx_bbox++] = (*buf); buf++; } buf += (PADSIZE/sizeof(int)); } buf = NULL; /***clean up************/ H5Sclose(dspace); H5Dclose(dset); H5Gclose(gid); } if (buf) delete [] buf; /*All Chombo blocks are LEAF blocks*/ m_lpeBlock_Node_Type = new int[m_uiNum_Blocks]; for(j=0; j < m_uiNum_Blocks; j++) m_lpeBlock_Node_Type[j]= FR_LEAF_NODE; /* Fill size and coords array. This data set is redundant so we will fill it with zeros so that it trivially * passes some checks in the sameblock() function from the old sfocu code for * compatibility */ m_lpdBlock_Coords = new double [m_uiNum_Blocks * m_uiNum_Dimensions]; m_lpdBlock_Size = new double [m_uiNum_Blocks * m_uiNum_Dimensions]; for(j=0; j < (m_uiNum_Blocks * m_uiNum_Dimensions ); j++) { m_lpdBlock_Coords[j] = 0.; m_lpdBlock_Size[j] = 0.; } /* Fill lref array */ int idx_lref =0; m_lpiRefinement_Levels = new int [m_uiNum_Blocks]; for(j = 0; j < m_uiNum_Levels; j++) { for(k=0; k < m_lpuiBlocks_Per_Refine_Level[j]; k++) m_lpiRefinement_Levels[idx_lref++] = j; } }
int extractfromh5(struct param par, std::string vTmpFile) { hid_t vH5File = -1; hid_t vH5Dataset = -1; hid_t vH5Datatype = -1; hid_t vH5Dataspace = -1; hid_t vH5OPAQUETypeId = -1; std::string cpath, cname; std::string pathi = (std::string) par.pathi; std::string conti = (std::string) par.conti; std::string grupi = (std::string) par.grupi; std::string filei = (std::string) par.filei; cpath = pathi + "/" + conti; cname = grupi + "/" + filei; /* Open an existing file. */ vH5File = H5Fopen(cpath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); CHKRTN(vH5File < 0, FILEERR); /* Open an existing dataset. */ vH5Dataset = H5Dopen(vH5File, cname.c_str(), H5P_DEFAULT); vH5Datatype = H5Dget_type(vH5Dataset); vH5Dataspace = H5Dget_space(vH5Dataset); // TODO: The rank is hardcoded to 1 (which is sufficient // for openBIS), but it would be more generic to code // for a dynamic rank! const int rank = H5Sget_simple_extent_ndims(vH5Dataspace); if (rank != 1) { if (vH5OPAQUETypeId >= 0) H5Tclose(vH5OPAQUETypeId); if (vH5Datatype >= 0) H5Tclose(vH5Datatype); if (vH5Dataspace >= 0) H5Sclose(vH5Dataspace); if (vH5Dataset >= 0) H5Dclose(vH5Dataset); if (vH5File >= 0) H5Fclose(vH5File); CHKRTNMSG((rank != 1), "Data space rank is not '1' as expected"); } hsize_t vH5DataspaceDims[1]; H5Sget_simple_extent_dims(vH5Dataspace, vH5DataspaceDims, NULL); // Define the memory dataspace. hid_t memspace = H5Screate_simple(1, vH5DataspaceDims, NULL); // allocate memory for the dataset: std::vector<char> vDataSet(vH5DataspaceDims[0]); if (vDataSet.size() != vH5DataspaceDims[0]) { fprintf(stderr, "Could not reserve memory"); if (vH5OPAQUETypeId >= 0) H5Tclose(vH5OPAQUETypeId); if (vH5Datatype >= 0) H5Tclose(vH5Datatype); if (vH5Dataspace >= 0) H5Sclose(vH5Dataspace); if (vH5Dataset >= 0) H5Dclose(vH5Dataset); if (vH5File >= 0) H5Fclose(vH5File); return -1; } //hid_t vH5OPAQUETypeId = H5Tcreate(H5T_OPAQUE, 1); hid_t vDataTypeId = H5Dget_type(vH5Dataset); vH5OPAQUETypeId = H5Tget_native_type(vDataTypeId, H5T_DIR_ASCEND); // read the data from the dataset/dataspace: herr_t readstatus = H5Dread(vH5Dataset, vH5OPAQUETypeId, memspace, vH5Dataspace, H5P_DEFAULT, &vDataSet[0]); if (readstatus < 0) { fprintf(stderr, "Failed to read dataset from file"); if (vH5OPAQUETypeId >= 0) H5Tclose(vH5OPAQUETypeId); if (vH5Datatype >= 0) H5Tclose(vH5Datatype); if (vH5Dataspace >= 0) H5Sclose(vH5Dataspace); if (vH5Dataset >= 0) H5Dclose(vH5Dataset); if (vH5File >= 0) H5Fclose(vH5File); return -1; } if (!writefile(vTmpFile, vDataSet)) { fprintf(stderr, "Failed to write dataset to new file"); if (vH5OPAQUETypeId >= 0) H5Tclose(vH5OPAQUETypeId); if (vH5Datatype >= 0) H5Tclose(vH5Datatype); if (vH5Dataspace >= 0) H5Sclose(vH5Dataspace); if (vH5Dataset >= 0) H5Dclose(vH5Dataset); if (vH5File >= 0) H5Fclose(vH5File); return -1; } return 0; }
int rec_scan_group(hid_t grpid) { hid_t spaceid, datasetid = 0, child_grpid; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; hsize_t dims[MAX_DIMS], max_dims[MAX_DIMS]; int ndims, d; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets, ignore the rest. */ switch(obj_class) { case H5G_GROUP: if ((child_grpid = H5Gopen(grpid, obj_name)) < 0) ERR; if (rec_scan_group(child_grpid)) ERR; break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; /*printf("\nobj_name %s\n", obj_name);*/ /* Get the dimensions of this dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; if (ndims > MAX_DIMS) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /*printf("dimension scale! Hoorah for the Pirate King!\n");*/ } else { int visitor_data = 0; /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor, &visitor_data) < 0) ERR; } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: printf("found a type!\n"); break; case H5G_LINK: printf("found a link! Yikes!\n"); break; default: printf("Unknown object class %d!", obj_class); } } return 0; }
bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName, double &dfResult, bool bReportError ) { hid_t hAttr = H5Aopen_name( loc_id, pszAttrName ); dfResult = 0.0; if( hAttr < 0 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, not found.", pszAttrName ); return false; } hid_t hAttrTypeID = H5Aget_type( hAttr ); hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); /* -------------------------------------------------------------------- */ /* Confirm that we have a single element value. */ /* -------------------------------------------------------------------- */ hid_t hAttrSpace = H5Aget_space( hAttr ); hsize_t anSize[64]; int nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, anSize, NULL ); int i, nAttrElements = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElements *= (int) anSize[i]; } if( nAttrElements != 1 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, count=%d, not 1.", pszAttrName, nAttrElements ); H5Sclose( hAttrSpace ); H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return false; } /* -------------------------------------------------------------------- */ /* Read the value. */ /* -------------------------------------------------------------------- */ void *buf = (void *)CPLMalloc( H5Tget_size( hAttrNativeType )); H5Aread( hAttr, hAttrNativeType, buf ); /* -------------------------------------------------------------------- */ /* Translate to double. */ /* -------------------------------------------------------------------- */ if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) dfResult = *((int *) buf); else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) dfResult = *((float *) buf); else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) dfResult = *((double *) buf); else { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attribute %s of unsupported type for conversion to double.", pszAttrName ); CPLFree( buf ); H5Sclose( hAttrSpace ); H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return false; } CPLFree( buf ); H5Sclose( hAttrSpace ); H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return true; }
/* * Append planes, each of (1,2*chunksize,2*chunksize) to the dataset. * In other words, 4 chunks are appended to the dataset at a time. * Fill each plane with the plane number and then write it at the nth plane. * Increase the plane number and repeat till the end of dataset, when it * reaches chunksize long. End product is a (2*chunksize)^3 cube. * * Return: 0 succeed; -1 fail. */ static int write_file(void) { hid_t fid; /* File ID for new HDF5 file */ hid_t dsid; /* dataset ID */ hid_t fapl; /* File access property list */ hid_t dcpl; /* Dataset creation property list */ char *name; UC_CTYPE *buffer, *bufptr; /* data buffer */ hsize_t cz=chunksize_g; /* Chunk size */ hid_t f_sid; /* dataset file space id */ hid_t m_sid; /* memory space id */ int rank; /* rank */ hsize_t chunk_dims[3]; /* Chunk dimensions */ hsize_t dims[3]; /* Dataspace dimensions */ hsize_t memdims[3]; /* Memory space dimensions */ hsize_t start[3] = {0,0,0}, count[3]; /* Hyperslab selection values */ hbool_t disabled; /* Object's disabled status */ hsize_t i, j, k; name = filename_g; /* Open the file */ if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) return -1; if(use_swmr_g) if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) return -1; if((fid = H5Fopen(name, H5F_ACC_RDWR | (use_swmr_g ? H5F_ACC_SWMR_WRITE : 0), fapl)) < 0){ fprintf(stderr, "H5Fopen failed\n"); return -1; } /* Open the dataset of the program name */ if((dsid = H5Dopen2(fid, progname_g, H5P_DEFAULT)) < 0){ fprintf(stderr, "H5Dopen2 failed\n"); return -1; } /* Disabled mdc flushed for the dataset */ if(H5Odisable_mdc_flushes(dsid) < 0) { fprintf(stderr, "H5Odisable_mdc_flushes failed\n"); return -1; } /* Get mdc disabled status of the dataset */ if(H5Oare_mdc_flushes_disabled(dsid, &disabled) < 0) { fprintf(stderr, "H5Oare_mdc_flushes_disabled failed\n"); return -1; } else if(disabled) printf("Dataset has disabled mdc flushes.\n"); else printf("Dataset should have disabled its mdc flushes.\n"); /* Find chunksize used */ if ((dcpl = H5Dget_create_plist(dsid)) < 0){ fprintf(stderr, "H5Dget_create_plist failed\n"); return -1; } if (H5D_CHUNKED != H5Pget_layout(dcpl)){ fprintf(stderr, "storage layout is not chunked\n"); return -1; } if ((rank = H5Pget_chunk(dcpl, 3, chunk_dims)) != 3){ fprintf(stderr, "storage rank is not 3\n"); return -1; } /* verify chunk_dims against set paramenters */ if (chunk_dims[0]!= chunkdims_g[0] || chunk_dims[1] != cz || chunk_dims[2] != cz){ fprintf(stderr, "chunk size is not as expected. Got dims=(%llu,%llu,%llu)\n", (unsigned long long)chunk_dims[0], (unsigned long long)chunk_dims[1], (unsigned long long)chunk_dims[2]); return -1; } /* allocate space for data buffer 1 X dims[1] X dims[2] of UC_CTYPE */ memdims[0]=1; memdims[1] = dims_g[1]; memdims[2] = dims_g[2]; if ((buffer=(UC_CTYPE*)HDmalloc((size_t)memdims[1]*(size_t)memdims[2]*sizeof(UC_CTYPE)))==NULL) { fprintf(stderr, "malloc: failed\n"); return -1; }; /* * Get dataset rank and dimension. */ f_sid = H5Dget_space(dsid); /* Get filespace handle first. */ rank = H5Sget_simple_extent_ndims(f_sid); if (rank != UC_RANK){ fprintf(stderr, "rank(%d) of dataset does not match\n", rank); return -1; } if (H5Sget_simple_extent_dims(f_sid, dims, NULL) < 0){ fprintf(stderr, "H5Sget_simple_extent_dims got error\n"); return -1; } printf("dataset rank %d, dimensions %llu x %llu x %llu\n", rank, (unsigned long long)(dims[0]), (unsigned long long)(dims[1]), (unsigned long long)(dims[2])); /* verify that file space dims are as expected and are consistent with memory space dims */ if (dims[0] != 0 || dims[1] != memdims[1] || dims[2] != memdims[2]){ fprintf(stderr, "dataset is not empty. Got dims=(%llu,%llu,%llu)\n", (unsigned long long)dims[0], (unsigned long long)dims[1], (unsigned long long)dims[2]); return -1; } /* setup mem-space for buffer */ if ((m_sid=H5Screate_simple(rank, memdims, NULL))<0){ fprintf(stderr, "H5Screate_simple for memory failed\n"); return -1; }; /* write planes */ count[0]=1; count[1]=dims[1]; count[2]=dims[2]; for (i=0; i<nplanes_g; i++){ /* fill buffer with value i+1 */ bufptr = buffer; for (j=0; j<dims[1]; j++) for (k=0; k<dims[2]; k++) *bufptr++ = i; /* extend the dataset by one for new plane */ dims[0]=i+1; if(H5Dset_extent(dsid, dims) < 0){ fprintf(stderr, "H5Dset_extent failed\n"); return -1; } /* Get the dataset's dataspace */ if((f_sid = H5Dget_space(dsid)) < 0){ fprintf(stderr, "H5Dset_extent failed\n"); return -1; } start[0]=i; /* Choose the next plane to write */ if(H5Sselect_hyperslab(f_sid, H5S_SELECT_SET, start, NULL, count, NULL) < 0){ fprintf(stderr, "Failed H5Sselect_hyperslab\n"); return -1; } /* Write plane to the dataset */ if(H5Dwrite(dsid, UC_DATATYPE, m_sid, f_sid, H5P_DEFAULT, buffer) < 0){ fprintf(stderr, "Failed H5Dwrite\n"); return -1; } /* Flush the dataset for every "chunkplanes_g" planes */ if(!((i + 1) % (hsize_t)chunkplanes_g)) { if(H5Dflush(dsid) < 0) { fprintf(stderr, "Failed to H5Dflush dataset\n"); return -1; } } } if(H5Dflush(dsid) < 0) { fprintf(stderr, "Failed to H5Dflush dataset\n"); return -1; } /* Enable mdc flushes for the dataset */ /* Closing the dataset later will enable mdc flushes automatically if this is not done */ if(disabled) if(H5Oenable_mdc_flushes(dsid) < 0) { fprintf(stderr, "Failed to H5Oenable_mdc_flushes\n"); return -1; } /* Done writing. Free/Close all resources including data file */ HDfree(buffer); if(H5Dclose(dsid) < 0){ fprintf(stderr, "Failed to close datasete\n"); return -1; } if(H5Sclose(m_sid) < 0){ fprintf(stderr, "Failed to close memory space\n"); return -1; } if(H5Sclose(f_sid) < 0){ fprintf(stderr, "Failed to close file space\n"); return -1; } if(H5Pclose(fapl) < 0){ fprintf(stderr, "Failed to property list\n"); return -1; } if(H5Fclose(fid) < 0){ fprintf(stderr, "Failed to close file id\n"); return -1; } return 0; } /* write_file() */
int main() { printf("\n*** Checking HDF5 dimscales some more.\n"); printf("*** Creating a file with one var with one dimension scale..."); { hid_t fileid, spaceid, datasetid, dimscaleid, cparmsid; hsize_t dims[NDIMS] = {DIM1_LEN}, maxdims[NDIMS] = {H5S_UNLIMITED}; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; /* Create the space that will be used both for the dimscale and * the 1D dataset that will attach it. */ if ((spaceid = H5Screate_simple(NDIMS, dims, maxdims)) < 0) ERR; /* Modify dataset creation properties, i.e. enable chunking. */ dims[0] = 1; if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(cparmsid, NDIMS, dims) < 0) ERR; /* Create our dimension scale, as an unlimited dataset. */ if ((dimscaleid = H5Dcreate(fileid, DIMSCALE_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create a variable which uses it. */ if ((datasetid = H5Dcreate(fileid, VAR1_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(datasetid, dimscaleid, 0) < 0) ERR; if (H5DSset_label(datasetid, 0, DIMSCALE_LABEL) < 0) ERR; /* Fold up our tents. */ if (H5Dclose(dimscaleid) < 0 || H5Dclose(datasetid) < 0 || H5Sclose(spaceid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking that one var, one dimscale file can be read..."); { hid_t fileid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; char dimscale_name[NC_MAX_NAME+1]; htri_t is_scale; char label[NC_MAX_NAME+1]; int num_scales; hsize_t dims[1], maxdims[1]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj, vars_dimscale_obj; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* This should be an unlimited dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if (maxdims[0] != H5S_UNLIMITED) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale && strcmp(obj_name, DIMSCALE_NAME)) ERR; if (is_scale) { /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, dimscale_name, NC_MAX_NAME) < 0) ERR; if (strcmp(dimscale_name, NAME_ATTRIBUTE)) ERR; /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj.fileno[0] = statbuf.fileno[0]; dimscale_obj.objno[0] = statbuf.objno[0]; dimscale_obj.fileno[1] = statbuf.fileno[1]; dimscale_obj.objno[1] = statbuf.objno[1]; /*printf("statbuf.fileno = %d statbuf.objno = %d\n", statbuf.fileno, statbuf.objno);*/ } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ if (H5DSiterate_scales(datasetid, 0, NULL, alien_visitor, &vars_dimscale_obj) < 0) ERR; /*printf("vars_dimscale_obj.fileno = %d vars_dimscale_obj.objno = %d\n", vars_dimscale_obj.fileno, vars_dimscale_obj.objno);*/ if (vars_dimscale_obj.fileno[0] != dimscale_obj.fileno[0] || vars_dimscale_obj.objno[0] != dimscale_obj.objno[0] || vars_dimscale_obj.fileno[1] != dimscale_obj.fileno[1] || vars_dimscale_obj.objno[1] != dimscale_obj.objno[1]) ERR; /* There's also a label for dimension 0. */ if (H5DSget_label(datasetid, 0, label, NC_MAX_NAME) < 0) ERR; /*printf("found non-scale dataset %s, label %s\n", obj_name, label);*/ } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Sclose(spaceid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating a file with one var with two dimension scales..."); { #define LAT_LEN 3 #define LON_LEN 2 #define DIMS_2 2 #define LAT_NAME "lat" #define LON_NAME "lon" #define PRES_NAME "pres" hid_t fileid, lat_spaceid, lon_spaceid, pres_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid; hsize_t dims[DIMS_2]; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = LAT_LEN; dims[1] = LON_LEN; if ((pres_spaceid = H5Screate_simple(DIMS_2, dims, dims)) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate(fileid, LAT_NAME, H5T_NATIVE_INT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate(fileid, LON_NAME, H5T_NATIVE_INT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; /* Create a variable which uses these two dimscales. */ if ((pres_datasetid = H5Dcreate(fileid, PRES_NAME, H5T_NATIVE_FLOAT, pres_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; /* Fold up our tents. */ if (H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking that one var, two dimscales file can be read..."); { #define NDIMS2 2 hid_t fileid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; hsize_t dims[NDIMS2], maxdims[NDIMS2]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[2], vars_dimscale_obj[2]; int dimscale_cnt = 0; int d, ndims; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", */ /* obj_class, obj_name); */ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; if (ndims > NDIMS2) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; /* printf("dimscale_obj[%d].fileno = %d dimscale_obj[%d].objno = %d\n", */ /* dimscale_cnt, dimscale_obj[dimscale_cnt].fileno, dimscale_cnt, */ /* dimscale_obj[dimscale_cnt].objno); */ dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating a file with one var with two unlimited dimension scales..."); { #define U1_LEN 3 #define U2_LEN 2 #define DIMS2 2 #define U1_NAME "u1" #define U2_NAME "u2" #define VNAME "v1" hid_t fapl_id, fcpl_id, grpid, plistid, plistid2; hid_t fileid, lat_spaceid, lon_spaceid, pres_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid; hsize_t dims[DIMS2], maxdims[DIMS2], chunksize[DIMS2] = {10, 10}; hid_t spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[2], vars_dimscale_obj[2]; int dimscale_cnt = 0; int d, ndims; /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; /* Set latest_format in access propertly list. This ensures that * the latest, greatest, HDF5 versions are used in the file. */ if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This * turns on HDF5 creation ordering in the file. */ if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; /* Open the root group. */ if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = 0; maxdims[0] = H5S_UNLIMITED; if ((lat_spaceid = H5Screate_simple(1, dims, maxdims)) < 0) ERR; if ((lon_spaceid = H5Screate_simple(1, dims, maxdims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = 0; dims[1] = 0; maxdims[0] = H5S_UNLIMITED; maxdims[1] = H5S_UNLIMITED; if ((pres_spaceid = H5Screate_simple(DIMS2, dims, maxdims)) < 0) ERR; /* Set up the dataset creation property list for the two dimensions. */ if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(plistid, 1, chunksize) < 0) ERR; if (H5Pset_attr_creation_order(plistid, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate(grpid, U1_NAME, H5T_NATIVE_INT, lat_spaceid, plistid)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate(grpid, U2_NAME, H5T_NATIVE_INT, lon_spaceid, plistid)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; /* Set up the dataset creation property list for the variable. */ if ((plistid2 = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(plistid2, DIMS2, chunksize) < 0) ERR; if (H5Pset_attr_creation_order(plistid2, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; /* Create a variable which uses these two dimscales. */ if ((pres_datasetid = H5Dcreate(grpid, VNAME, H5T_NATIVE_DOUBLE, pres_spaceid, plistid2)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; /* Close down the show. */ if (H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0 || H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Pclose(plistid) < 0 || H5Pclose(plistid2) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i = 0; i < num_obj; i++) { /*Get the type (i.e. group, dataset, etc.), and the name of the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(grpid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Check the dimension lengths. */ { hid_t spaceid1; hsize_t h5dimlen[DIMS2], h5dimlenmax[DIMS2]; int dataset_ndims; /* Check U1. */ if ((datasetid = H5Dopen1(grpid, U1_NAME)) < 0) ERR; if ((spaceid1 = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid1, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 1 || h5dimlen[0] != 0 || h5dimlenmax[0] != H5S_UNLIMITED) ERR; if (H5Dclose(datasetid) || H5Sclose(spaceid1)) ERR; /* Check U2. */ if ((datasetid = H5Dopen1(grpid, U2_NAME)) < 0) ERR; if ((spaceid1 = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid1, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 1 || h5dimlen[0] != 0 || h5dimlenmax[0] != H5S_UNLIMITED) ERR; if (H5Dclose(datasetid) || H5Sclose(spaceid1)) ERR; /* Check V1. */ if ((datasetid = H5Dopen1(grpid, VNAME)) < 0) ERR; if ((spaceid1 = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid1, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 0 || h5dimlen[1] != 0 || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* All done. */ if (H5Dclose(datasetid) || H5Sclose(spaceid1)) ERR; } /* Write two hyperslabs. */ { #define NUM_VALS 3 hid_t file_spaceid, mem_spaceid; hsize_t h5dimlen[DIMS2], h5dimlenmax[DIMS2], xtend_size[DIMS2] = {1, NUM_VALS}; hsize_t start[DIMS2] = {0, 0}; hsize_t count[DIMS2] = {1, NUM_VALS}; double value[NUM_VALS]; int dataset_ndims; int i; /* Set up phony data. */ for (i = 0; i < NUM_VALS; i++) value[i] = (float)i; /* Open the dataset, check its dimlens. */ if ((datasetid = H5Dopen1(grpid, VNAME)) < 0) ERR; if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 0 || h5dimlen[1] != 0 || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* Extend the size of the dataset. */ if (H5Dextend(datasetid, xtend_size) < 0) ERR; if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; /* Check the size. */ if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 1 || h5dimlen[1] != NUM_VALS || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* Set up the file and memory spaces. */ if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) ERR; if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR; /* Write a slice of data. */ if (H5Dwrite(datasetid, H5T_NATIVE_DOUBLE, mem_spaceid, file_spaceid, H5P_DEFAULT, value) < 0) /* Check the size. */ if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 1 || h5dimlen[1] != NUM_VALS || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* Extend the size of the dataset for the second slice. */ xtend_size[0]++; if (H5Dextend(datasetid, xtend_size) < 0) ERR; if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; /* Set up the file and memory spaces for a second slice. */ start[0]++; if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) ERR; if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR; /* Write a second slice of data. */ if (H5Dwrite(datasetid, H5T_NATIVE_DOUBLE, mem_spaceid, file_spaceid, H5P_DEFAULT, value) < 0) /* Check the size again. */ if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 2 || h5dimlen[1] != NUM_VALS || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* All done. */ if (H5Dclose(datasetid) || H5Sclose(mem_spaceid) || H5Sclose(file_spaceid)) ERR; } /* Close up the shop. */ if (H5Sclose(spaceid)) ERR; if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking dimension scales with attached dimension scales..."); { #define LAT_LEN 3 #define LON_LEN 2 #define TIME_LEN 5 #define LEN_LEN 10 #define DIMS_3 3 #define NUM_DIMSCALES1 4 #define LAT_NAME "lat" #define LON_NAME "lon" #define PRES_NAME1 "z_pres" #define TIME_NAME "time" #define LEN_NAME "u_len" hid_t fileid, lat_spaceid, lon_spaceid, time_spaceid, pres_spaceid, len_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid, time_dimscaleid, len_dimscaleid; hid_t fapl_id, fcpl_id; hsize_t dims[DIMS_3]; hid_t spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; hsize_t maxdims[DIMS_3]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[NUM_DIMSCALES1], vars_dimscale_obj[NUM_DIMSCALES1]; int dimscale_cnt = 0; int d, ndims; /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; /* Set latest_format in access propertly list. This ensures that * the latest, greatest, HDF5 versions are used in the file. */ if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This * turns on HDF5 creation ordering in the file. */ if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = TIME_LEN; if ((time_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LEN_LEN; if ((len_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = LAT_LEN; dims[1] = LON_LEN; dims[2] = TIME_LEN; if ((pres_spaceid = H5Screate_simple(DIMS_3, dims, dims)) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate1(fileid, LAT_NAME, H5T_NATIVE_INT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate1(fileid, LON_NAME, H5T_NATIVE_INT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; if ((time_dimscaleid = H5Dcreate1(fileid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(time_dimscaleid, NULL) < 0) ERR; if ((len_dimscaleid = H5Dcreate1(fileid, LEN_NAME, H5T_NATIVE_INT, len_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(len_dimscaleid, NULL) < 0) ERR; /* Create a variable which uses these three dimscales. */ if ((pres_datasetid = H5Dcreate1(fileid, PRES_NAME1, H5T_NATIVE_FLOAT, pres_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; if (H5DSattach_scale(pres_datasetid, time_dimscaleid, 2) < 0) ERR; /* Attach a dimscale to a dimscale. Unfortunately, HDF5 does not * allow this. Woe is me. */ /*if (H5DSattach_scale(time_dimscaleid, len_dimscaleid, 0) < 0) ERR;*/ /* Fold up our tents. */ if (H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(time_dimscaleid) < 0 || H5Dclose(len_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(time_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Sclose(len_spaceid) < 0 || H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", */ /* obj_class, obj_name); */ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; /* printf("dimscale_obj[%d].fileno = %d dimscale_obj[%d].objno = %d\n", */ /* dimscale_cnt, dimscale_obj[dimscale_cnt].fileno, dimscale_cnt, */ /* dimscale_obj[dimscale_cnt].objno); */ dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking cration ordering of datasets which are also dimension scales..."); { #define LAT_LEN 3 #define LON_LEN 2 #define TIME_LEN 5 #define LEN_LEN 10 #define DIMS_3 3 #define NUM_DIMSCALES2 4 #define LAT_NAME "lat" #define LON_NAME "lon" #define PRES_NAME1 "z_pres" #define TIME_NAME "time" #define LEN_NAME "u_len" hid_t fileid, lat_spaceid, lon_spaceid, time_spaceid, pres_spaceid, len_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid, time_dimscaleid, len_dimscaleid; hid_t fapl_id, fcpl_id; hsize_t dims[DIMS_3]; hid_t spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; hsize_t maxdims[DIMS_3]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[NUM_DIMSCALES2], vars_dimscale_obj[NUM_DIMSCALES2]; int dimscale_cnt = 0; int d, ndims; /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; /* Set latest_format in access propertly list. This ensures that * the latest, greatest, HDF5 versions are used in the file. */ if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This * turns on HDF5 creation ordering in the file. */ if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = TIME_LEN; if ((time_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LEN_LEN; if ((len_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = LAT_LEN; dims[1] = LON_LEN; dims[2] = TIME_LEN; if ((pres_spaceid = H5Screate_simple(DIMS_3, dims, dims)) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate1(fileid, LAT_NAME, H5T_NATIVE_INT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate1(fileid, LON_NAME, H5T_NATIVE_INT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; if ((time_dimscaleid = H5Dcreate1(fileid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(time_dimscaleid, NULL) < 0) ERR; if ((len_dimscaleid = H5Dcreate1(fileid, LEN_NAME, H5T_NATIVE_INT, len_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(len_dimscaleid, NULL) < 0) ERR; /* Create a variable which uses these three dimscales. */ if ((pres_datasetid = H5Dcreate1(fileid, PRES_NAME1, H5T_NATIVE_FLOAT, pres_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; if (H5DSattach_scale(pres_datasetid, time_dimscaleid, 2) < 0) ERR; /* Attach a dimscale to a dimscale. Unfortunately, HDF5 does not * allow this. Woe is me. */ /*if (H5DSattach_scale(time_dimscaleid, len_dimscaleid, 0) < 0) ERR;*/ /* Fold up our tents. */ if (H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(time_dimscaleid) < 0 || H5Dclose(len_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(time_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Sclose(len_spaceid) < 0 || H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", */ /* obj_class, obj_name); */ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; /* printf("dimscale_obj[%d].fileno = %d dimscale_obj[%d].objno = %d\n", */ /* dimscale_cnt, dimscale_obj[dimscale_cnt].fileno, dimscale_cnt, */ /* dimscale_obj[dimscale_cnt].objno); */ dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int ImageBase::readHDF5(size_t select_img) { bool isStack = false; H5infoProvider provider = getProvider(fhdf5); // Provider name int errCode = 0; hid_t dataset; /* Dataset and datatype identifiers */ hid_t filespace; hsize_t dims[4]; // We are not going to support more than 4 dimensions, at this moment. hsize_t nobjEman; hid_t cparms; int rank; String dsname = filename.getBlockName(); // Setting default dataset name if (dsname.empty()) { dsname = provider.second; switch (provider.first) { case EMAN: // Images in stack are stored in separated groups hid_t grpid; grpid = H5Gopen(fhdf5,"/MDF/images/", H5P_DEFAULT); /*herr_t err = */ H5Gget_num_objs(grpid, &nobjEman); dsname = formatString(dsname.c_str(), IMG_INDEX(select_img)); H5Gclose(grpid); break; default: break; } } else { switch (provider.first) { case EMAN: // Images in stack are stored in separated groups nobjEman=1; break; default: break; } } dataset = H5Dopen2(fhdf5, dsname.c_str(), H5P_DEFAULT); if( dataset < 0) REPORT_ERROR(ERR_IO_NOTEXIST, formatString("readHDF5: Dataset '%s' not found",dsname.c_str())); cparms = H5Dget_create_plist(dataset); /* Get properties handle first. */ // Get dataset rank and dimension. filespace = H5Dget_space(dataset); /* Get filespace handle first. */ // rank = H5Sget_simple_extent_ndims(filespace); rank = H5Sget_simple_extent_dims(filespace, dims, NULL); // Offset only set when it is possible to access to data directly offset = (H5D_CONTIGUOUS == H5Pget_layout(cparms))? H5Dget_offset(dataset) : 0; // status = H5Dread(dataset, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, bm_out); hid_t h5datatype = H5Dget_type(dataset); // Reading byte order switch(H5Tget_order(h5datatype)) { case H5T_ORDER_ERROR: REPORT_ERROR(ERR_IO, "readHDF5: error reading endianness."); break; case H5T_ORDER_LE: swap = IsBigEndian(); break; case H5T_ORDER_BE: swap = IsLittleEndian(); break; default: REPORT_ERROR(ERR_IO, "readHDF5: unknown endianness type, maybe mixed types."); break; } DataType datatype = datatypeH5(h5datatype); MDMainHeader.setValue(MDL_DATATYPE,(int) datatype); // Setting isStack depending on provider switch (provider.first) { case MISTRAL: // rank 3 arrays are stacks isStack = true; break; // case EMAN: // Images in stack are stored in separated groups default: break; } ArrayDim aDim; size_t nDimFile; aDim.xdim = dims[rank-1]; aDim.ydim = (rank>1)?dims[rank-2]:1; aDim.zdim = (rank>3 || (rank==3 && !isStack))?dims[rank-3]:1; if ( provider.first == EMAN ) nDimFile = nobjEman; else nDimFile = ( rank<3 || !isStack )?1:dims[0] ; if (select_img > nDimFile) REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, formatString("readHDF5 (%s): Image number %lu exceeds stack size %lu", filename.c_str(), select_img, nDimFile)); aDim.ndim = replaceNsize = (select_img == ALL_IMAGES)? nDimFile :1 ; setDimensions(aDim); //Read header only if(dataMode == HEADER || (dataMode == _HEADER_ALL && aDim.ndim > 1)) return errCode; // EMAN stores each image in a separate dataset if ( provider.first == EMAN ) select_img = 1; size_t imgStart = IMG_INDEX(select_img); size_t imgEnd = (select_img != ALL_IMAGES) ? imgStart + 1 : aDim.ndim; MD.clear(); MD.resize(imgEnd - imgStart,MDL::emptyHeader); if (dataMode < DATA) // Don't read data if not necessary but read the header return errCode; if ( H5Pget_layout(cparms) == H5D_CONTIGUOUS ) //We can read it directly readData(fimg, select_img, datatype, 0); else // We read it by hyperslabs { // Allocate memory for image data (Assume xdim, ydim, zdim and ndim are already set //if memory already allocated use it (no resize allowed) mdaBase->coreAllocateReuse(); hid_t memspace; hsize_t offset[4]; // Hyperslab offset in the file hsize_t count[4]; // Size of the hyperslab in the file // Define the offset and count of the hyperslab to be read. switch (rank) { case 4: count[0] = 1; case 3: // if (stack) count[rank-3] = aDim.zdim; offset[rank-2] = 0; case 2: count[rank-2] = aDim.ydim; offset[rank-2] = 0; break; } count[rank-1] = aDim.xdim; offset[rank-1] = 0; aDim.xdim = dims[rank-1]; aDim.ydim = (rank>1)?dims[rank-2]:1; aDim.zdim = (rank == 4)?dims[1]:1; // size_t nDimFile = (rank>2)?dims[0]:1 ; // Define the memory space to read a hyperslab. memspace = H5Screate_simple(rank,count,NULL); size_t data = (size_t) this->mdaBase->getArrayPointer(); size_t pad = aDim.zyxdim*gettypesize(myT()); for (size_t idx = imgStart, imN = 0; idx < imgEnd; ++idx, ++imN) { // Set the offset of the hyperslab to be read offset[0] = idx; if ( H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL) < 0 ) REPORT_ERROR(ERR_IO_NOREAD, formatString("readHDF5: Error selecting hyperslab %d from filename %s", imgStart, filename.c_str())); // movePointerTo(ALL_SLICES,imN); // Read if ( H5Dread(dataset, H5Datatype(myT()), memspace, filespace, H5P_DEFAULT, (void*)(data + pad*imN)) < 0 ) REPORT_ERROR(ERR_IO_NOREAD,formatString("readHDF5: Error reading hyperslab %d from filename %s", imgStart, filename.c_str())); } H5Sclose(memspace); } H5Pclose(cparms); H5Sclose(filespace); H5Dclose(dataset); return errCode; }
//-***************************************************************************** AbcA::ArraySamplePtr ReadArray( AbcA::ReadArraySampleCachePtr iCache, hid_t iParent, const std::string &iName, const AbcA::DataType &iDataType, hid_t iFileType, hid_t iNativeType ) { // Dispatch string stuff. if ( iDataType.getPod() == kStringPOD ) { return ReadStringArray( iCache, iParent, iName, iDataType ); } else if ( iDataType.getPod() == kWstringPOD ) { return ReadWstringArray( iCache, iParent, iName, iDataType ); } assert( iDataType.getPod() != kStringPOD && iDataType.getPod() != kWstringPOD ); // Open the data set. hid_t dsetId = H5Dopen( iParent, iName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( dsetId >= 0, "Cannot open dataset: " << iName ); DsetCloser dsetCloser( dsetId ); // Read the data space. hid_t dspaceId = H5Dget_space( dsetId ); ABCA_ASSERT( dspaceId >= 0, "Could not get dataspace for dataSet: " << iName ); DspaceCloser dspaceCloser( dspaceId ); AbcA::ArraySample::Key key; bool foundDigest = false; // if we are caching, get the key and see if it is being used if ( iCache ) { key.origPOD = iDataType.getPod(); key.readPOD = key.origPOD; key.numBytes = Util::PODNumBytes( key.readPOD ) * H5Sget_simple_extent_npoints( dspaceId ); foundDigest = ReadKey( dsetId, "key", key ); AbcA::ReadArraySampleID found = iCache->find( key ); if ( found ) { AbcA::ArraySamplePtr ret = found.getSample(); assert( ret ); if ( ret->getDataType().getPod() != iDataType.getPod() ) { ABCA_THROW( "ERROR: Read data type for dset: " << iName << ": " << ret->getDataType() << " does not match expected data type: " << iDataType ); } // Got it! return ret; } } // Okay, we haven't found it in a cache. // Read the data type. hid_t dtypeId = H5Dget_type( dsetId ); ABCA_ASSERT( dtypeId >= 0, "Could not get datatype for dataSet: " << iName ); DtypeCloser dtypeCloser( dtypeId ); ABCA_ASSERT( EquivalentDatatypes( iFileType, dtypeId ), "File DataType clash for array dataset: " << iName ); AbcA::ArraySamplePtr ret; H5S_class_t dspaceClass = H5Sget_simple_extent_type( dspaceId ); if ( dspaceClass == H5S_SIMPLE ) { // Get the dimensions int rank = H5Sget_simple_extent_ndims( dspaceId ); ABCA_ASSERT( rank == 1, "H5Sget_simple_extent_ndims() must be 1." ); hsize_t hdim = 0; rank = H5Sget_simple_extent_dims( dspaceId, &hdim, NULL ); Dimensions dims; std::string dimName = iName + ".dims"; if ( H5Aexists( iParent, dimName.c_str() ) ) { ReadDimensions( iParent, dimName, dims ); } else { dims.setRank(1); dims[0] = hdim / iDataType.getExtent(); } ABCA_ASSERT( dims.numPoints() > 0, "Degenerate dims in Dataset read" ); // Create a buffer into which we shall read. ret = AbcA::AllocateArraySample( iDataType, dims ); assert( ret->getData() ); // And... read into it. herr_t status = H5Dread( dsetId, iNativeType, H5S_ALL, H5S_ALL, H5P_DEFAULT, const_cast<void*>( ret->getData() ) ); ABCA_ASSERT( status >= 0, "H5Dread() failed." ); } else if ( dspaceClass == H5S_NULL ) { Dimensions dims; std::string dimName = iName + ".dims"; if ( H5Aexists( iParent, dimName.c_str() ) ) { ReadDimensions( iParent, dimName, dims ); ABCA_ASSERT( dims.rank() > 0, "Degenerate rank in Dataset read" ); // Num points should be zero here. ABCA_ASSERT( dims.numPoints() == 0, "Expecting zero points in dimensions" ); } else { dims.setRank(1); dims[0] = 0; } ret = AbcA::AllocateArraySample( iDataType, dims ); } else { ABCA_THROW( "Unexpected scalar dataspace encountered." ); } // Store if there is a cache. if ( foundDigest && iCache ) { AbcA::ReadArraySampleID stored = iCache->store( key, ret ); if ( stored ) { return stored.getSample(); } } // Otherwise, just leave! ArraySamplePtr returned by AllocateArraySample // already has fancy-dan deleter built in. // I REALLY LOVE SMART PTRS. return ret; }
int main (void) { hid_t vfile, file, src_space, mem_space, vspace, vdset, dset; /* Handles */ hid_t dcpl; herr_t status; hsize_t vdsdims[3] = {4*DIM0_1, VDSDIM1, VDSDIM2}, vdsdims_max[3] = {VDSDIM0, VDSDIM1, VDSDIM2}, dims[3] = {DIM0_1, DIM1, DIM2}, extdims[3] = {2*DIM0_1, DIM1, DIM2}, chunk_dims[3] = {DIM0_1, DIM1, DIM2}, dims_max[3] = {DIM0, DIM1, DIM2}, vdsdims_out[3], vdsdims_max_out[3], start[3], /* Hyperslab parameters */ stride[3], count[3], src_count[3], block[3]; hsize_t start_out[3], /* Hyperslab parameter out */ stride_out[3], count_out[3], block_out[3]; int i, j, k; H5D_layout_t layout; /* Storage layout */ size_t num_map; /* Number of mappings */ ssize_t len; /* Length of the string; also a return value */ char *filename; char *dsetname; int wdata[DIM0_1*DIM1*DIM2]; int rdata[80][10][10]; int a_rdata[20][10][10]; /* * Create source files and datasets. This step is optional. */ for (i=0; i < PLANE_STRIDE; i++) { /* * Initialize data for i-th source dataset. */ for (j = 0; j < DIM0_1*DIM1*DIM2; j++) wdata[j] = i+1; /* * Create the source files and datasets. Write data to each dataset and * close all resources. */ file = H5Fcreate (SRC_FILE[i], H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); src_space = H5Screate_simple (RANK, dims, dims_max); dcpl = H5Pcreate(H5P_DATASET_CREATE); status = H5Pset_chunk (dcpl, RANK, chunk_dims); dset = H5Dcreate2 (file, SRC_DATASET[i], H5T_NATIVE_INT, src_space, H5P_DEFAULT, dcpl, H5P_DEFAULT); status = H5Dwrite (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); status = H5Sclose (src_space); status = H5Pclose (dcpl); status = H5Dclose (dset); status = H5Fclose (file); } vfile = H5Fcreate (VFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create VDS dataspace. */ vspace = H5Screate_simple (RANK, vdsdims, vdsdims_max); /* Create dataspaces for the source dataset. */ src_space = H5Screate_simple (RANK, dims, dims_max); /* Create VDS creation property */ dcpl = H5Pcreate (H5P_DATASET_CREATE); /* Initialize hyperslab values */ start[0] = 0; start[1] = 0; start[2] = 0; stride[0] = PLANE_STRIDE; /* we will select every fifth plane in VDS */ stride[1] = 1; stride[2] = 1; count[0] = H5S_UNLIMITED; count[1] = 1; count[2] = 1; src_count[0] = H5S_UNLIMITED; src_count[1] = 1; src_count[2] = 1; block[0] = 1; block[1] = DIM1; block[2] = DIM2; /* * Build the mappings * */ status = H5Sselect_hyperslab (src_space, H5S_SELECT_SET, start, NULL, src_count, block); for (i=0; i < PLANE_STRIDE; i++) { status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, stride, count, block); status = H5Pset_virtual (dcpl, vspace, SRC_FILE[i], SRC_DATASET[i], src_space); start[0]++; } H5Sselect_none(vspace); /* Create a virtual dataset */ vdset = H5Dcreate2 (vfile, DATASET, H5T_NATIVE_INT, vspace, H5P_DEFAULT, dcpl, H5P_DEFAULT); status = H5Sclose (vspace); status = H5Sclose (src_space); status = H5Pclose (dcpl); /* Let's get space of the VDS and its dimension; we should get 40x10x10 */ vspace = H5Dget_space (vdset); H5Sget_simple_extent_dims (vspace, vdsdims_out, vdsdims_max_out); printf ("VDS dimensions first time \n"); printf (" Current: "); for (i=0; i<RANK; i++) printf (" %d ", (int)vdsdims_out[i]); printf ("\n"); /* Let's add data to the source datasets and check new dimensions for VDS */ for (i=0; i < PLANE_STRIDE; i++) { /* * Initialize data for i-th source dataset. */ for (j = 0; j < DIM0_1*DIM1*DIM2; j++) wdata[j] = 10*(i+1); /* * Create the source files and datasets. Write data to each dataset and * close all resources. */ file = H5Fopen (SRC_FILE[i], H5F_ACC_RDWR, H5P_DEFAULT); dset = H5Dopen2 (file, SRC_DATASET[i], H5P_DEFAULT); status = H5Dset_extent (dset, extdims); src_space = H5Dget_space (dset); start[0] = DIM0_1; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = 1; block[0] = DIM0_1; block[1] = DIM1; block[2] = DIM2; mem_space = H5Screate_simple(RANK, dims, NULL); status = H5Sselect_hyperslab (src_space, H5S_SELECT_SET, start, NULL, count, block); status = H5Dwrite (dset, H5T_NATIVE_INT, mem_space, src_space, H5P_DEFAULT, wdata); status = H5Sclose (src_space); status = H5Dclose (dset); status = H5Fclose (file); } status = H5Dclose (vdset); status = H5Fclose (vfile); /* * Now we begin the read section of this example. */ /* * Open file and dataset using the default properties. */ vfile = H5Fopen (VFILE, H5F_ACC_RDONLY, H5P_DEFAULT); vdset = H5Dopen2 (vfile, DATASET, H5P_DEFAULT); /* * Get creation property list and mapping properties. */ dcpl = H5Dget_create_plist (vdset); /* * Get storage layout. */ layout = H5Pget_layout (dcpl); if (H5D_VIRTUAL == layout) printf(" Dataset has a virtual layout \n"); else printf(" Wrong layout found \n"); /* * Find number of mappings. */ status = H5Pget_virtual_count (dcpl, &num_map); printf(" Number of mappings is %lu\n", (unsigned long)num_map); /* * Get mapping parameters for each mapping. */ for (i = 0; i < (int)num_map; i++) { printf(" Mapping %d \n", i); printf(" Selection in the virtual dataset \n"); /* Get selection in the virttual dataset */ vspace = H5Pget_virtual_vspace (dcpl, (size_t)i); if (H5Sget_select_type(vspace) == H5S_SEL_HYPERSLABS) { if (H5Sis_regular_hyperslab(vspace)) { status = H5Sget_regular_hyperslab (vspace, start_out, stride_out, count_out, block_out); printf(" start = [%llu, %llu, %llu] \n", (unsigned long long)start_out[0], (unsigned long long)start_out[1], (unsigned long long)start_out[2]); printf(" stride = [%llu, %llu, %llu] \n", (unsigned long long)stride_out[0], (unsigned long long)stride_out[1], (unsigned long long)stride_out[2]); printf(" count = [%llu, %llu, %llu] \n", (unsigned long long)count_out[0], (unsigned long long)count_out[1], (unsigned long long)count_out[2]); printf(" block = [%llu, %llu, %llu] \n", (unsigned long long)block_out[0], (unsigned long long)block_out[1], (unsigned long long)block_out[2]); } } /* Get source file name */ len = H5Pget_virtual_filename (dcpl, (size_t)i, NULL, 0); filename = (char *)malloc((size_t)len*sizeof(char)+1); H5Pget_virtual_filename (dcpl, (size_t)i, filename, len+1); printf(" Source filename %s\n", filename); /* Get source dataset name */ len = H5Pget_virtual_dsetname (dcpl, (size_t)i, NULL, 0); dsetname = (char *)malloc((size_t)len*sizeof(char)+1); H5Pget_virtual_dsetname (dcpl, (size_t)i, dsetname, len+1); printf(" Source dataset name %s\n", dsetname); /* Get selection in the source dataset */ printf(" Selection in the source dataset \n"); src_space = H5Pget_virtual_srcspace (dcpl, (size_t)i); if (H5Sget_select_type(src_space) == H5S_SEL_HYPERSLABS) { if (H5Sis_regular_hyperslab(src_space)) { status = H5Sget_regular_hyperslab (src_space, start_out, stride_out, count_out, block_out); printf(" start = [%llu, %llu, %llu] \n", (unsigned long long)start_out[0], (unsigned long long)start_out[1], (unsigned long long)start_out[2]); printf(" stride = [%llu, %llu, %llu] \n", (unsigned long long)stride_out[0], (unsigned long long)stride_out[1], (unsigned long long)stride_out[2]); printf(" count = [%llu, %llu, %llu] \n", (unsigned long long)count_out[0], (unsigned long long)count_out[1], (unsigned long long)count_out[2]); printf(" block = [%llu, %llu, %llu] \n", (unsigned long long)block_out[0], (unsigned long long)block_out[1], (unsigned long long)block_out[2]); } } H5Sclose(vspace); H5Sclose(src_space); free(filename); free(dsetname); } /* * Read data from VDS. */ vspace = H5Dget_space (vdset); H5Sget_simple_extent_dims (vspace, vdsdims_out, vdsdims_max_out); printf ("VDS dimensions second time \n"); printf (" Current: "); for (i=0; i<RANK; i++) printf (" %d ", (int)vdsdims_out[i]); printf ("\n"); /* Read all VDS data */ /* EIP We should be able to do it by using H5S_ALL instead of making selection * or using H5Sselect_all from vspace. */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = 1; block[0] = vdsdims_out[0]; block[1] = vdsdims_out[1]; block[2] = vdsdims_out[2]; status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, NULL, count, block); mem_space = H5Screate_simple(RANK, vdsdims_out, NULL); status = H5Dread (vdset, H5T_NATIVE_INT, mem_space, vspace, H5P_DEFAULT, rdata); printf (" All data: \n"); for (i=0; i < (int)vdsdims_out[0]; i++) { for (j=0; j < (int)vdsdims_out[1]; j++) { printf ("(%d, %d, 0)", i, j); for (k=0; k < (int)vdsdims_out[2]; k++) printf (" %d ", rdata[i][j][k]); printf ("\n"); } } /* Read VDS, but only data mapeed to dataset a.h5 */ start[0] = 0; start[1] = 0; start[2] = 0; stride[0] = PLANE_STRIDE; stride[1] = 1; stride[2] = 1; count[0] = 2*DIM0_1; count[1] = 1; count[2] = 1; block[0] = 1; block[1] = vdsdims_out[1]; block[2] = vdsdims_out[2]; dims[0] = 2*DIM0_1; status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, stride, count, block); mem_space = H5Screate_simple(RANK, dims, NULL); status = H5Dread (vdset, H5T_NATIVE_INT, mem_space, vspace, H5P_DEFAULT, a_rdata); printf (" All data: \n"); for (i=0; i < 2*DIM0_1; i++) { for (j=0; j < (int)vdsdims_out[1]; j++) { printf ("(%d, %d, 0)", i, j); for (k=0; k < (int)vdsdims_out[2]; k++) printf (" %d ", a_rdata[i][j][k]); printf ("\n"); } } /* * Close and release resources. */ status = H5Sclose(mem_space); status = H5Pclose (dcpl); status = H5Dclose (vdset); status = H5Fclose (vfile); return 0; }
//-***************************************************************************** void ReadArray( void * iIntoLocation, hid_t iParent, const std::string &iName, const AbcA::DataType &iDataType, hid_t iType ) { // Dispatch string stuff. if ( iDataType.getPod() == kStringPOD ) { return ReadStringArray( iIntoLocation, iParent, iName, iDataType ); } else if ( iDataType.getPod() == kWstringPOD ) { return ReadWstringArray( iIntoLocation, iParent, iName, iDataType ); } assert( iDataType.getPod() != kStringPOD && iDataType.getPod() != kWstringPOD ); // Open the data set. hid_t dsetId = H5Dopen( iParent, iName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( dsetId >= 0, "Cannot open dataset: " << iName ); DsetCloser dsetCloser( dsetId ); // Read the data space. hid_t dspaceId = H5Dget_space( dsetId ); ABCA_ASSERT( dspaceId >= 0, "Could not get dataspace for dataSet: " << iName ); DspaceCloser dspaceCloser( dspaceId ); // Read the data type. hid_t dtypeId = H5Dget_type( dsetId ); ABCA_ASSERT( dtypeId >= 0, "Could not get datatype for dataSet: " << iName ); DtypeCloser dtypeCloser( dtypeId ); H5S_class_t dspaceClass = H5Sget_simple_extent_type( dspaceId ); if ( dspaceClass == H5S_SIMPLE ) { // Get the dimensions int rank = H5Sget_simple_extent_ndims( dspaceId ); ABCA_ASSERT( rank == 1, "H5Sget_simple_extent_ndims() must be 1." ); hsize_t hdim = 0; rank = H5Sget_simple_extent_dims( dspaceId, &hdim, NULL ); ABCA_ASSERT( hdim > 0, "Degenerate dims in Dataset read" ); // And... read into it. herr_t status = H5Dread( dsetId, iType, H5S_ALL, H5S_ALL, H5P_DEFAULT, iIntoLocation ); ABCA_ASSERT( status >= 0, "H5Dread() failed." ); } else if ( dspaceClass != H5S_NULL ) { ABCA_THROW( "Unexpected scalar dataspace encountered." ); } }
GDALDataset *HDF5ImageDataset::Open( GDALOpenInfo * poOpenInfo ) { int i; HDF5ImageDataset *poDS; char szFilename[2048]; if(!EQUALN( poOpenInfo->pszFilename, "HDF5:", 5 ) || strlen(poOpenInfo->pszFilename) > sizeof(szFilename) - 3 ) return NULL; /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_Update ) { CPLError( CE_Failure, CPLE_NotSupported, "The HDF5ImageDataset driver does not support update access to existing" " datasets.\n" ); return NULL; } poDS = new HDF5ImageDataset(); /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ /* printf("poOpenInfo->pszFilename %s\n",poOpenInfo->pszFilename); */ char **papszName = CSLTokenizeString2( poOpenInfo->pszFilename, ":", CSLT_HONOURSTRINGS|CSLT_PRESERVEESCAPES ); if( !((CSLCount(papszName) == 3) || (CSLCount(papszName) == 4)) ) { CSLDestroy(papszName); delete poDS; return NULL; } poDS->SetDescription( poOpenInfo->pszFilename ); /* -------------------------------------------------------------------- */ /* Check for drive name in windows HDF5:"D:\... */ /* -------------------------------------------------------------------- */ strcpy(szFilename, papszName[1]); if( strlen(papszName[1]) == 1 && papszName[3] != NULL ) { strcat(szFilename, ":"); strcat(szFilename, papszName[2]); poDS->SetSubdatasetName( papszName[3] ); } else poDS->SetSubdatasetName( papszName[2] ); CSLDestroy(papszName); papszName = NULL; if( !H5Fis_hdf5(szFilename) ) { delete poDS; return NULL; } poDS->SetPhysicalFilename( szFilename ); /* -------------------------------------------------------------------- */ /* Try opening the dataset. */ /* -------------------------------------------------------------------- */ poDS->hHDF5 = H5Fopen(szFilename, H5F_ACC_RDONLY, H5P_DEFAULT ); if( poDS->hHDF5 < 0 ) { delete poDS; return NULL; } poDS->hGroupID = H5Gopen( poDS->hHDF5, "/" ); if( poDS->hGroupID < 0 ) { poDS->bIsHDFEOS=false; delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* THIS IS AN HDF5 FILE */ /* -------------------------------------------------------------------- */ poDS->bIsHDFEOS=TRUE; poDS->ReadGlobalAttributes( FALSE ); /* -------------------------------------------------------------------- */ /* Create HDF5 Data Hierarchy in a link list */ /* -------------------------------------------------------------------- */ poDS->poH5Objects = poDS->HDF5FindDatasetObjectsbyPath( poDS->poH5RootGroup, (char *)poDS->GetSubdatasetName() ); if( poDS->poH5Objects == NULL ) { delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Retrieve HDF5 data information */ /* -------------------------------------------------------------------- */ poDS->dataset_id = H5Dopen( poDS->hHDF5,poDS->poH5Objects->pszPath ); poDS->dataspace_id = H5Dget_space( poDS->dataset_id ); poDS->ndims = H5Sget_simple_extent_ndims( poDS->dataspace_id ); poDS->dims = (hsize_t*)CPLCalloc( poDS->ndims, sizeof(hsize_t) ); poDS->maxdims = (hsize_t*)CPLCalloc( poDS->ndims, sizeof(hsize_t) ); poDS->dimensions = H5Sget_simple_extent_dims( poDS->dataspace_id, poDS->dims, poDS->maxdims ); poDS->datatype = H5Dget_type( poDS->dataset_id ); poDS->clas = H5Tget_class( poDS->datatype ); poDS->size = H5Tget_size( poDS->datatype ); poDS->address = H5Dget_offset( poDS->dataset_id ); poDS->native = H5Tget_native_type( poDS->datatype, H5T_DIR_ASCEND ); poDS->nRasterYSize=(int)poDS->dims[poDS->ndims-2]; // Y poDS->nRasterXSize=(int)poDS->dims[poDS->ndims-1]; // X alway last poDS->nBands=1; if( poDS->ndims == 3 ) poDS->nBands=(int) poDS->dims[0]; for( i = 1; i <= poDS->nBands; i++ ) { HDF5ImageRasterBand *poBand = new HDF5ImageRasterBand( poDS, i, poDS->GetDataType( poDS->native ) ); poDS->SetBand( i, poBand ); if( poBand->bNoDataSet ) poBand->SetNoDataValue( 255 ); } // CSK code in IdentifyProductType() and CreateProjections() // uses dataset metadata. poDS->SetMetadata( poDS->papszMetadata ); // Check if the hdf5 is a well known product type poDS->IdentifyProductType(); poDS->CreateProjections( ); /* -------------------------------------------------------------------- */ /* Setup/check for pam .aux.xml. */ /* -------------------------------------------------------------------- */ poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Setup overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, ":::VIRTUAL:::" ); return( poDS ); }
/// @todo extend to Wegdes (aka Prisms) void SalomeIO::read(const std::string& name, vector < vector < double> > &coords, const double Lref, std::vector<bool> &type_elem_flag) { Mesh& mesh = GetMesh(); mesh.SetLevel(0); hsize_t dims[2]; // compute number of menus =============== hid_t file_id = H5Fopen(name.c_str(),H5F_ACC_RDWR, H5P_DEFAULT); hid_t gid = H5Gopen(file_id,mesh_ensemble.c_str(),H5P_DEFAULT); hsize_t n_menus; hid_t status= H5Gget_num_objs(gid, &n_menus); // number of menus if(status !=0) { std::cout << "Number of mesh menus not found"; abort(); } // compute number of groups and number of meshes =============== std::vector<std::string> mesh_menus; std::vector<std::string> group_menus; unsigned n_groups = 0; unsigned n_meshes = 0; for (unsigned j=0; j<n_menus; j++) { char * menu_names_j = new char[max_length]; H5Gget_objname_by_idx(gid,j,menu_names_j,max_length); ///@deprecated see the HDF doc to replace this std::string tempj(menu_names_j); if (tempj.substr(0,5).compare("Group") == 0) { n_groups++; group_menus.push_back(tempj); } else if (tempj.substr(0,4).compare("Mesh") == 0) { n_meshes++; mesh_menus.push_back(tempj); } } // compute number of groups and number of meshes =============== unsigned int n_elements_b_bb = 0; // meshes ======================== for (unsigned j=0; j< n_meshes; j++) { std::string tempj = mesh_menus[j]; // dimension =============== /// @todo this determination of the dimension from the mesh file would not work with a 2D mesh embedded in 3D std::string my_mesh_name_dir = mesh_ensemble + "/" + tempj + "/" + aux_zeroone + "/" + elem_list + "/"; ///@todo here we have to loop hsize_t n_fem_type; hid_t gid = H5Gopen(file_id,my_mesh_name_dir.c_str(),H5P_DEFAULT); hid_t status0 = H5Gget_num_objs(gid, &n_fem_type); if(status0 !=0) {std::cout << "SalomeIO::read_fem_type: H5Gget_num_objs not found"; abort();} FindDimension(gid,tempj,n_fem_type); H5Gclose(gid); if (mesh.GetDimension() != n_fem_type) { std::cout << "Mismatch between dimension and number of element types" << std::endl; abort(); } // fem type =============== std::vector<std::string> el_fe_type(mesh.GetDimension()); ReadFE(file_id,el_fe_type, n_fem_type, my_mesh_name_dir); // // read NODAL COORDINATES **************** C std::string coord_dataset = mesh_ensemble + "/" + tempj + "/" + aux_zeroone + "/" + node_list + "/" + coord_list + "/"; ///@todo here we have to loop hid_t dtset = H5Dopen(file_id,coord_dataset.c_str(),H5P_DEFAULT); // SET NUMBER OF NODES hid_t filespace = H5Dget_space(dtset); /* Get filespace handle first. */ hid_t status = H5Sget_simple_extent_dims(filespace, dims, NULL); if(status ==0) std::cerr << "SalomeIO::read dims not found"; // reading xyz_med unsigned int n_nodes = dims[0]/mesh.GetDimension(); double *xyz_med = new double[dims[0]]; std::cout << " Number of nodes in med file " << n_nodes << " " << std::endl; mesh.SetNumberOfNodes(n_nodes); // SET NODE COORDINATES coords[0].resize(n_nodes); coords[1].resize(n_nodes); coords[2].resize(n_nodes); status=H5Dread(dtset,H5T_NATIVE_DOUBLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,xyz_med); H5Dclose(dtset); if (mesh.GetDimension()==3) { for (unsigned j=0; j<n_nodes; j++) { coords[0][j] = xyz_med[j]/Lref; coords[1][j] = xyz_med[j+n_nodes]/Lref; coords[2][j] = xyz_med[j+2*n_nodes]/Lref; } } else if (mesh.GetDimension()==2) { for (unsigned j=0; j<n_nodes; j++) { coords[0][j] = xyz_med[j]/Lref; coords[1][j] = xyz_med[j+n_nodes]/Lref; coords[2][j] = 0.; } } else if (mesh.GetDimension()==1) { for (unsigned j=0; j<n_nodes; j++) { coords[0][j] = xyz_med[j]/Lref; coords[1][j] = 0.; coords[2][j] = 0.; } } delete[] xyz_med; // // end read NODAL COORDINATES ************* C // // read ELEMENT/cell ******************** B std::string node_name_dir = my_mesh_name_dir + el_fe_type[mesh.GetDimension()-1] + "/" + connectivity; hid_t dtset2 = H5Dopen(file_id,node_name_dir.c_str(),H5P_DEFAULT); filespace = H5Dget_space(dtset2); status = H5Sget_simple_extent_dims(filespace, dims, NULL); if(status ==0) {std::cerr << "SalomeIO::read dims not found"; abort();} // DETERMINE NUMBER OF NODES PER ELEMENT unsigned Node_el = FindElemNodes( el_fe_type[mesh.GetDimension()-1] ); const int dim_conn = dims[0]; unsigned int n_elements = dim_conn/Node_el; // SET NUMBER OF ELEMENTS mesh.SetNumberOfElements(n_elements); int * conn_map = new int[dim_conn]; std::cout << " Number of elements in med file " << n_elements << std::endl; status=H5Dread(dtset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,conn_map); if(status !=0) {std::cout << "SalomeIO::read: connectivity not found"; abort();} H5Dclose(dtset2); mesh.el = new elem(n_elements); ///@todo check where this is going to be deleted // BOUNDARY (and BOUNDARY of the BOUNDARY in 3D) ========================= for (unsigned i=0; i < mesh.GetDimension()-1; i++) { hsize_t dims_i[2]; std::string node_name_dir_i = my_mesh_name_dir + el_fe_type[i] + "/" + dofobj_indices; hid_t dtset_i = H5Dopen(file_id,node_name_dir_i.c_str(),H5P_DEFAULT); filespace = H5Dget_space(dtset_i); hid_t status = H5Sget_simple_extent_dims(filespace, dims_i, NULL); if(status ==0) { std::cerr << "SalomeIO::read dims not found"; abort(); } n_elements_b_bb += dims_i[0]; H5Dclose(dtset_i); } // BOUNDARY ========================= for (unsigned iel=0; iel<n_elements; iel++) { mesh.el->SetElementGroup(iel,1); unsigned nve = Node_el; /// @todo this is only one element type if (nve==27) { type_elem_flag[0]=type_elem_flag[3]=true; mesh.el->AddToElementNumber(1,"Hex"); mesh.el->SetElementType(iel,HEX); } else if (nve==10) { type_elem_flag[1]=type_elem_flag[4]=true; mesh.el->AddToElementNumber(1,"Tet"); mesh.el->SetElementType(iel,TET); } else if (nve==18) { type_elem_flag[2]=type_elem_flag[3]=type_elem_flag[4]=true; mesh.el->AddToElementNumber(1,"Wedge"); mesh.el->SetElementType(iel,WEDGE); } else if (nve==9) { type_elem_flag[3]=true; mesh.el->AddToElementNumber(1,"Quad"); mesh.el->SetElementType(iel,QUAD); } else if (nve==6 && mesh.GetDimension()==2) { type_elem_flag[4]=true; mesh.el->AddToElementNumber(1,"Triangle"); mesh.el->SetElementType(iel,TRI); } else if (nve==3 && mesh.GetDimension()==1) { mesh.el->AddToElementNumber(1,"Line"); mesh.el->SetElementType(iel,LINE); } else { std::cout<<"Error! Invalid element type in reading File!"<<std::endl; std::cout<<"Error! Use a second order discretization"<<std::endl; exit(0); } for (unsigned i=0; i<nve; i++) { unsigned inode = SalomeToFemusVertexIndex[mesh.el->GetElementType(iel)][i]; mesh.el->SetElementDofIndex(iel,inode,conn_map[iel+i*n_elements] - 1u); } } // // end read ELEMENT/CELL **************** B // clean delete [] conn_map; } //end meshes mesh.el->SetElementGroupNumber(n_groups); // read GROUP **************** E //we assume that these are VOLUME groups //in general, I'd say that a group can only have ONE element type (should study the possibility of hybrid mesh) for (unsigned j=0; j<n_groups; j++) { const uint n_fe_types_for_groups = 1; // so far we have this assumption std::string tempj = group_menus[j]; std::string my_mesh_name_dir = mesh_ensemble + "/" + tempj + "/" + aux_zeroone + "/" + elem_list + "/"; ///@todo here we have to loop /// @todo check the underscores according to our naming standard // strip the first number to get the group number // strip the second number to get the group material int gr_name = atoi(tempj.substr(6,1).c_str()); int gr_mat = atoi(tempj.substr(8,1).c_str()); std::vector<std::string> el_fe_type(mesh.GetDimension()); ReadFE(file_id, el_fe_type, n_fe_types_for_groups, my_mesh_name_dir); std::string group_dataset = mesh_ensemble + "/" + tempj + "/" + aux_zeroone + "/" + elem_list + "/" + el_fe_type[mesh.GetDimension()-1] + "/" + dofobj_indices; ///@todo here we have to loop hid_t dtset = H5Dopen(file_id,group_dataset.c_str(),H5P_DEFAULT); hid_t filespace = H5Dget_space(dtset); hid_t status = H5Sget_simple_extent_dims(filespace, dims, NULL); int * elem_indices = new int[dims[0]]; status=H5Dread(dtset,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,elem_indices); for (unsigned i=0; i < dims[0]; i++) { mesh.el->SetElementGroup(elem_indices[i] -1 - n_elements_b_bb, gr_name); mesh.el->SetElementMaterial(elem_indices[i] -1 - n_elements_b_bb ,gr_mat); } H5Dclose(dtset); delete [] elem_indices; } // // end read GROUP **************** E status = H5Fclose(file_id); //loop over volume elements //extract faces // // unsigned nbcd; // // read boundary **************** D // inf.open(name.c_str()); // if (!inf) { // std::cout<<"Generic-mesh file "<< name << " cannot read boudary\n"; // exit(0); // } // for (unsigned k=0; k<nbcd; k++) { // while (str2.compare("CONDITIONS") != 0) inf >> str2; // inf >> str2; // int value; // unsigned nface; // inf >>value>>str2>>nface>>str2>>str2; // value=-value-1; // for (unsigned i=0; i<nface; i++) { // unsigned iel,iface; // inf>>iel>>str2>>iface; // iel--; // iface=SalomeIO::SalomeToFemusFaceIndex[mesh.el->GetElementType(iel)][iface-1u]; // mesh.el->SetFaceElementIndex(iel,iface,value); // } // inf >> str2; // if (str2.compare("ENDOFSECTION") != 0) { // std::cout<<"error boundary data mesh"<<std::endl; // exit(0); // } // } // inf.close(); // // end read boundary **************** D }
void cData2d::readHDF5(char* filename, char* fieldname){ // Open the file hid_t file_id; file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); return; } // Open the dataset hid_t dataset_id; hid_t dataspace_id; dataset_id = H5Dopen1(file_id, fieldname); dataspace_id = H5Dget_space(dataset_id); // Test if 2D data int ndims; ndims = H5Sget_simple_extent_ndims(dataspace_id); if(ndims != 2) { printf("2dData::readHDF5: Not 2D data set, ndims=%i\n",ndims); exit(0); } // Get dimensions of data set (nx, ny, nn) hsize_t dims[ndims]; H5Sget_simple_extent_dims(dataspace_id,dims,NULL); ny = dims[0]; nx = dims[1]; nn = 1; for(int i = 0;i<ndims;i++) nn *= dims[i]; // Create space for the new data free(data); data = NULL; data = (tData2d *) calloc(nn, sizeof(tData2d)); // Read in data after setting up a temporary buffer of the appropriate variable type // Somehow this works best when split out accordint to different data types // Fix into general form later hid_t datatype_id; H5T_class_t dataclass; size_t size; datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); if(dataclass == H5T_FLOAT){ if (size == sizeof(float)) { float* buffer = (float *) calloc(nn, sizeof(float)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(double)) { double* buffer = (double *) calloc(nn, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown floating point type, size=%i\n",(int) size); return; } } else if(dataclass == H5T_INTEGER){ if (size == sizeof(char)) { char* buffer = (char*) calloc(nn, sizeof(char)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(short)) { short* buffer = (short*) calloc(nn, sizeof(short)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(int)) { int* buffer = (int *) calloc(nn, sizeof(int)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(long)) { long* buffer = (long *) calloc(nn, sizeof(long)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown integer type, size=%lu\n",size); exit(1); } } else { printf("2dData::readHDF5: unknown HDF5 data type\n"); return; } // Close and cleanup H5Dclose(dataset_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); }
int main(void) { hid_t fid = -1; /* HDF5 file ID */ hid_t did = -1; /* dataset ID */ hid_t msid = -1; /* memory dataspace ID */ hid_t fsid = -1; /* file dataspace ID */ hsize_t start[RANK]; /* hyperslab start point */ int n_elements = 0; /* size of buffer (elements) */ size_t size = 0; /* size of buffer (bytes) */ int *buffer = NULL; /* data buffer */ int n_dims = -1; /* # dimensions in dataset */ hsize_t dims[RANK]; /* current size of dataset */ hsize_t max_dims[RANK]; /* max size of dataset */ /* Open the VDS file and dataset */ if((fid = H5Fopen(VDS_FILE_NAME, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, H5P_DEFAULT)) < 0) TEST_ERROR if((did = H5Dopen2(fid, VDS_DSET_NAME, H5P_DEFAULT)) < 0) TEST_ERROR /* Create the read buffer */ n_elements = VDS_PLANE[1] * VDS_PLANE[2]; size = n_elements * sizeof(int); if(NULL == (buffer = (int *)HDmalloc(size))) TEST_ERROR /* Create memory dataspace */ if((msid = H5Screate_simple(RANK, VDS_PLANE, NULL)) < 0) TEST_ERROR /* Read data until the dataset is full (via the writer) */ do { /* Refresh metadata */ if(H5Drefresh(did) < 0) TEST_ERROR /* Get the dataset dimensions */ if((fsid = H5Dget_space(did)) < 0) TEST_ERROR if(H5Sget_simple_extent_dims(fsid, dims, max_dims) < 0) TEST_ERROR /* Check the reported size of the VDS */ if((n_dims = H5Sget_simple_extent_ndims(fsid)) < 0) TEST_ERROR if(n_dims != RANK) TEST_ERROR if(H5Sget_simple_extent_dims(fsid, dims, max_dims) < 0) TEST_ERROR /* NOTE: Don't care what dims[0] is. */ if(dims[1] != FULL_HEIGHT) TEST_ERROR if(dims[2] != WIDTH) TEST_ERROR if(max_dims[0] != H5S_UNLIMITED) TEST_ERROR if(max_dims[1] != FULL_HEIGHT) TEST_ERROR if(max_dims[2] != WIDTH) TEST_ERROR /* Continue if there's nothing to read */ if(0 == dims[0]) { if(H5Sclose(fsid) < 0) TEST_ERROR continue; } /* Read a plane from the VDS */ /* At this time, we just make sure we can read planes without errors. */ start[0] = dims[0] - 1; start[1] = 0; start[2] = 0; if(H5Sselect_hyperslab(fsid, H5S_SELECT_SET, start, NULL, VDS_PLANE, NULL) < 0) TEST_ERROR if(H5Dread(did, H5T_NATIVE_INT, msid, fsid, H5P_DEFAULT, buffer) < 0) TEST_ERROR if(H5Sclose(fsid) < 0) TEST_ERROR } while (dims[0] < N_PLANES_TO_WRITE); /* Close file and dataset */ if(H5Sclose(msid) < 0) TEST_ERROR if(H5Dclose(did) < 0) TEST_ERROR if(H5Fclose(fid) < 0) TEST_ERROR HDfree(buffer); HDfprintf(stderr, "SWMR reader exited successfully\n"); return EXIT_SUCCESS; error: H5E_BEGIN_TRY { if(fid >= 0) (void)H5Fclose(fid); if(did >= 0) (void)H5Dclose(did); if(msid >= 0) (void)H5Sclose(msid); if(fsid >= 0) (void)H5Sclose(fsid); if(buffer != NULL) HDfree(buffer); } H5E_END_TRY HDfprintf(stderr, "ERROR: SWMR reader exited with errors\n"); return EXIT_FAILURE; } /* end main */
/** * Reads an array of double attributes from the HDF5 metadata. * It reads the attributes directly on it's binary form directly, * thus avoiding string conversions. * * Important: It allocates the memory for the attributes internally, * so the caller must free the returned array after using it. * @param pszAttrName Name of the attribute to be read. * the attribute name must be the form: * root attribute name * SUBDATASET/subdataset attribute name * @param pdfValues pointer wich will store the array of doubles read. * @param nLen it stores the length of the array read. If NULL it doesn't * inform the lenght of the array. * @return CPLErr CE_None in case of success, CE_Failure in case of failure */ CPLErr HDF5Dataset::HDF5ReadDoubleAttr(const char* pszAttrFullPath, double **pdfValues,int *nLen) { CPLErr retVal = CE_Failure; hid_t hAttrID=-1; hid_t hAttrTypeID=-1; hid_t hAttrNativeType=-1; hid_t hAttrSpace=-1; hid_t hObjAttrID=-1; hsize_t nSize[64]; unsigned int nAttrElmts; hsize_t i; unsigned int nAttrDims; size_t nSlashPos; CPLString osAttrFullPath(pszAttrFullPath); CPLString osObjName; CPLString osAttrName; //Search for the last "/" in order to get the //Path to the attribute nSlashPos = osAttrFullPath.find_last_of("/"); //If objects name have been found if(nSlashPos != CPLString::npos ) { //Split Object name (dataset, group) osObjName = osAttrFullPath.substr(0,nSlashPos); //Split attribute name osAttrName = osAttrFullPath.substr(nSlashPos+1); } else { //By default the group is root, and //the attribute is the full path osObjName = "/"; osAttrName = pszAttrFullPath; } hObjAttrID = H5Oopen( hHDF5, osObjName.c_str(),H5P_DEFAULT); if(hObjAttrID < 0) { CPLError( CE_Failure, CPLE_OpenFailed, "Object %s could not be opened\n", pszAttrFullPath); retVal = CE_Failure; } else { //Open attribute handler by name, from the object handler opened //earlier hAttrID = H5Aopen_name( hObjAttrID, osAttrName.c_str()); //Check for errors opening the attribute if(hAttrID <0) { CPLError( CE_Failure, CPLE_OpenFailed, "Attribute %s could not be opened\n", pszAttrFullPath); retVal = CE_Failure; } else { hAttrTypeID = H5Aget_type( hAttrID ); hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); hAttrSpace = H5Aget_space( hAttrID ); nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL ); if( !H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) { CPLError( CE_Failure, CPLE_OpenFailed, "Attribute %s is not of type double\n", pszAttrFullPath); retVal = CE_Failure; } else { //Get the ammount of elements nAttrElmts = 1; for( i=0; i < nAttrDims; i++ ) { //For multidimensional attributes nAttrElmts *= nSize[i]; } if(nLen != NULL) *nLen = nAttrElmts; (*pdfValues) = (double *) CPLMalloc(nAttrElmts*sizeof(double)); //Read the attribute contents if(H5Aread( hAttrID, hAttrNativeType, *pdfValues )<0) { CPLError( CE_Failure, CPLE_OpenFailed, "Attribute %s could not be opened\n", pszAttrFullPath); retVal = CE_Failure; } else { retVal = CE_None; } } H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Sclose(hAttrSpace); H5Aclose(hAttrID); } H5Oclose(hObjAttrID); } return retVal; }
int main() { printf("\n*** Checking HDF5 dimension scales.\n"); #define GRP_NAME "simple_scales" #define DIMSCALE_NAME "dimscale" #define NAME_ATTRIBUTE "Billy-Bob" #define VAR1_NAME "var1" #define VAR2_NAME "var2" #define VAR3_NAME "var3" #define DIM1_LEN 3 #define DIM2_LEN 2 #define FIFTIES_SONG "Mamma said they'll be days like this. They'll be days like this, my mamma said." printf("*** Creating simple dimension scales file..."); { hid_t fileid, grpid, dimscaleid; hid_t dimscale_spaceid, var1_spaceid, var3_spaceid; hid_t var1_datasetid, var2_datasetid, var3_datasetid; hsize_t dims[2] = {DIM1_LEN, DIM2_LEN}; hsize_t dimscale_dims[1] = {DIM1_LEN}; /* Open file and create group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Create our dimension scale. Use the built-in NAME attribute * on the dimscale. */ if ((dimscale_spaceid = H5Screate_simple(1, dimscale_dims, dimscale_dims)) < 0) ERR; if ((dimscaleid = H5Dcreate(grpid, DIMSCALE_NAME, H5T_NATIVE_INT, dimscale_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create a 1D variable which uses the dimscale. Attach a label * to this scale. */ if ((var1_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; if ((var1_datasetid = H5Dcreate(grpid, VAR1_NAME, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var1_datasetid, dimscaleid, 0) < 0) ERR; if (H5DSset_label(var1_datasetid, 0, FIFTIES_SONG) < 0) ERR; /* Create a 1D variabls that doesn't use the dimension scale. */ if ((var2_datasetid = H5Dcreate(grpid, VAR2_NAME, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; /* Create a 2D dataset which uses the scale for one of its * dimensions. */ if ((var3_spaceid = H5Screate_simple(2, dims, dims)) < 0) ERR; if ((var3_datasetid = H5Dcreate(grpid, VAR3_NAME, H5T_NATIVE_INT, var3_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var3_datasetid, dimscaleid, 0) < 0) ERR; /* Close up the shop. */ if (H5Dclose(dimscaleid) < 0 || H5Dclose(var1_datasetid) < 0 || H5Dclose(var2_datasetid) < 0 || H5Dclose(var3_datasetid) < 0 || H5Sclose(var1_spaceid) < 0 || H5Sclose(var3_spaceid) < 0 || H5Sclose(dimscale_spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* HELP! If you are reading this in the future, and time * machines have been invented, please come back to July 10, * 2005, the Java Java coffee shop in Lafayette, 8:00 am MST +- * 20 minutes. Bring back some advanced weapons systems to * destroy the sound system here, which is playing 50's rock and * roll. Do-op, do-op, la-ma la-ma, ding dong. Save me!!! (Mind * you, James Brown is a different story!) */ } SUMMARIZE_ERR; printf("*** Checking that simple dimscale file can be read..."); { hid_t fileid, grpid, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of the * object. Confusingly, this is a different type than the type * of a variable. This type might be better called "class" or * "type of type" */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /*Close the last datasetid, if one is open. */ if (datasetid > 0) { H5Dclose(datasetid); } if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale && strcmp(obj_name, DIMSCALE_NAME)) ERR; if (is_scale) { char nom_de_quincey[STR_LEN+1]; /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, nom_de_quincey, STR_LEN) < 0) ERR; if (strcmp(nom_de_quincey, NAME_ATTRIBUTE)) ERR; /*printf("found scale %s, NAME %s\n", obj_name, nom_de_quincey);*/ } else { char label[STR_LEN+1]; /* Here's how to get the number of scales attached * to the dataset. I would think that this would * return 0 scales for a dataset that doesn't have * scales, but instead it errors. So take an error * to be the same as no dimension scales. */ num_scales = H5DSget_num_scales(datasetid, 0); if (strcmp(obj_name, VAR1_NAME) == 0 && num_scales != 1) ERR; if (strcmp(obj_name, VAR2_NAME) == 0 && num_scales > 0) ERR; if (strcmp(obj_name, VAR3_NAME) == 0 && num_scales != 1) ERR; /* There's also a label for dimension 0 of var1. */ if (strcmp(obj_name, VAR1_NAME) == 0) { if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; if (strcmp(label, FIFTIES_SONG)) ERR; } } break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Dclose(datasetid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating simple dimension scales file with lots of datasets..."); #define NUM_DATASETS 500 { hid_t fileid, grpid, dimscaleid; hid_t dimscale_spaceid, var1_spaceid; hid_t var1_datasetid[NUM_DATASETS]; hsize_t dims[2] = {DIM1_LEN, DIM2_LEN}; hsize_t dimscale_dims[1] = {DIM1_LEN}; char var_name[STR_LEN + 1]; int v; /* Open file and create group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Create our dimension scale. Use the built-in NAME attribute * on the dimscale. */ if ((dimscale_spaceid = H5Screate_simple(1, dimscale_dims, dimscale_dims)) < 0) ERR; if ((dimscaleid = H5Dcreate(grpid, DIMSCALE_NAME, H5T_NATIVE_INT, dimscale_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create many 1D datasets which use the dimscale. */ if ((var1_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; for (v = 0; v < NUM_DATASETS; v++) { sprintf(var_name, "var_%d", v); if ((var1_datasetid[v] = H5Dcreate(grpid, var_name, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var1_datasetid[v], dimscaleid, 0) < 0) ERR; } /* Close up the shop. */ for (v = 0; v < NUM_DATASETS; v++) if (H5Dclose(var1_datasetid[v]) < 0) ERR; if (H5Dclose(dimscaleid) < 0 || H5Sclose(var1_spaceid) < 0 || H5Sclose(dimscale_spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating a file with an unlimited dimension scale..."); { hid_t fileid, grpid, spaceid, datasetid, dimscaleid, cparmsid; hsize_t dims[1] = {1}, maxdims[1] = {H5S_UNLIMITED}; /* Create file and group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; if ((spaceid = H5Screate_simple(1, dims, maxdims)) < 0) ERR; /* Modify dataset creation properties, i.e. enable chunking */ if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(cparmsid, 1, dims) < 0) ERR; /* Create our dimension scale, as an unlimited dataset. */ if ((dimscaleid = H5Dcreate(grpid, DIMSCALE_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create a variable which uses it. */ if ((datasetid = H5Dcreate(grpid, VAR1_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(datasetid, dimscaleid, 0) < 0) ERR; if (H5DSset_label(datasetid, 0, "dimension label") < 0) ERR; /* Close up the shop. */ if (H5Dclose(dimscaleid) < 0 || H5Dclose(datasetid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; #ifdef EXTRA_TESTS printf("*** Checking that unlimited dimscale file can be read..."); { hid_t fileid, grpid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; hsize_t dims[1], maxdims[1]; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /*Close the last datasetid, if one is open. */ if (datasetid > 0) { H5Dclose(datasetid); datasetid = 0; } /* Open the dataset. */ if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; /* This should be an unlimited dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if (maxdims[0] != H5S_UNLIMITED) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale && strcmp(obj_name, DIMSCALE_NAME)) ERR; if (is_scale) { char nom_de_quincey[STR_LEN+1]; /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, nom_de_quincey, STR_LEN) < 0) ERR; /*printf("found scale %s, NAME %s\n", obj_name, nom_de_quincey);*/ } else { char label[STR_LEN+1]; int visitor_data = 0; /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ if (H5DSiterate_scales(datasetid, 0, NULL, alien_visitor, &visitor_data) < 0) ERR; /* There's also a label for dimension 0. */ if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; /*printf("found non-scale dataset %s, label %s\n", obj_name, label);*/ } break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Dclose(datasetid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating some 3D datasets using shared dimscales..."); { #define NDIMS 3 #define TIME_DIM 0 #define LAT_DIM 1 #define LON_DIM 2 #define LAT_LEN 2 #define LON_LEN 3 #define LAT_NAME "Lat" #define LON_NAME "Lon" #define TIME_NAME "Time" #define PRES_NAME "Pressure" #define TEMP_NAME "Temperature" hid_t fileid, grpid, lat_spaceid, lon_spaceid, time_spaceid, spaceid; hid_t lat_scaleid, lon_scaleid, time_scaleid; hid_t pres_dsid, temp_dsid, cparmsid; hsize_t dims[NDIMS], max_dims[NDIMS]; /* Create file and group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Create 3 1D spaces for the 3 dimension scale datasets. Time * starts out as size 0. It's an unlimited dimension scale. */ dims[0] = 0; max_dims[0] = H5S_UNLIMITED; if ((time_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = LAT_LEN; max_dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = LON_LEN; max_dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; /* Enable chunking for unlimited time scale. */ if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; dims[TIME_DIM] = 1; if (H5Pset_chunk(cparmsid, 1, dims) < 0) ERR; /* Create our dimension scales. */ if ((time_scaleid = H5Dcreate(grpid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(time_scaleid, TIME_NAME) < 0) ERR; if ((lat_scaleid = H5Dcreate(grpid, LAT_NAME, H5T_NATIVE_FLOAT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_scaleid, LAT_NAME) < 0) ERR; if ((lon_scaleid = H5Dcreate(grpid, LON_NAME, H5T_NATIVE_FLOAT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_scaleid, LON_NAME) < 0) ERR; /* Create a space coresponding to these three dimensions. */ dims[TIME_DIM] = 0; dims[LAT_DIM] = LAT_LEN; dims[LON_DIM] = LON_LEN; max_dims[TIME_DIM] = H5S_UNLIMITED; max_dims[LAT_DIM] = LAT_LEN; max_dims[LON_DIM] = LON_LEN; if ((spaceid = H5Screate_simple(NDIMS, dims, max_dims)) < 0) ERR; /* Create two variables which use them, and attach the dimension scales. */ dims[TIME_DIM] = 1; if (H5Pset_chunk(cparmsid, NDIMS, dims) < 0) ERR; if ((pres_dsid = H5Dcreate(grpid, PRES_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(pres_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_dsid, lat_scaleid, 1) < 0) ERR; if (H5DSattach_scale(pres_dsid, lon_scaleid, 2) < 0) ERR; if (H5DSset_label(pres_dsid, TIME_DIM, TIME_NAME) < 0) ERR; if (H5DSset_label(pres_dsid, LAT_DIM, LAT_NAME) < 0) ERR; if (H5DSset_label(pres_dsid, LON_DIM, LON_NAME) < 0) ERR; if ((temp_dsid = H5Dcreate(grpid, TEMP_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(temp_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(temp_dsid, lat_scaleid, 1) < 0) ERR; if (H5DSattach_scale(temp_dsid, lon_scaleid, 2) < 0) ERR; if (H5DSset_label(temp_dsid, TIME_DIM, TIME_NAME) < 0) ERR; if (H5DSset_label(temp_dsid, LAT_DIM, LAT_NAME) < 0) ERR; if (H5DSset_label(temp_dsid, LON_DIM, LON_NAME) < 0) ERR; /* Close up the shop. */ if (H5Dclose(pres_dsid) < 0 || H5Dclose(temp_dsid) < 0 || H5Dclose(lat_scaleid) < 0 || H5Dclose(lon_scaleid) < 0 || H5Dclose(time_scaleid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking 3D datasets created with shared dimscales..."); { hid_t fileid, grpid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; hsize_t dims[NDIMS], max_dims[NDIMS]; int d; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; /*printf("\nobj_name %s\n", obj_name);*/ /* Get the dimensions of this dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { char nom_de_quincey[STR_LEN+1]; /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, nom_de_quincey, STR_LEN) < 0) ERR; /*printf("found scale %s, NAME %s id 0x%x\n", obj_name, nom_de_quincey, datasetid);*/ /* Check size depending on name. */ if ((!strcmp(obj_name, LAT_NAME) && dims[TIME_DIM] != LAT_LEN) || (!strcmp(obj_name, LON_NAME) && dims[TIME_DIM] != LON_LEN) || (!strcmp(obj_name, TIME_NAME) && max_dims[TIME_DIM] != H5S_UNLIMITED)) ERR; } else { char label[STR_LEN+1]; int visitor_data = 0; /* SHould have these dimensions... */ if (dims[TIME_DIM] != 0 || dims[LAT_DIM] != LAT_LEN || dims[LON_DIM] != LON_LEN) ERR; if (max_dims[TIME_DIM] != H5S_UNLIMITED) ERR; /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn * about them. What I want is the dataset id of each * dimscale. Then... */ for (d = 0; d < NDIMS; d++) if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor, &visitor_data) < 0) ERR; /*printf("visitor_data: 0x%x\n", visitor_data);*/ /* There's also a label for each dimension. */ if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; if (strcmp(label, TIME_NAME)) ERR; if (H5DSget_label(datasetid, 1, label, STR_LEN) < 0) ERR; if (strcmp(label, LAT_NAME)) ERR; if (H5DSget_label(datasetid, 2, label, STR_LEN) < 0) ERR; if (strcmp(label, LON_NAME)) ERR; } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating 3D datasets using shared dimscales in groups..."); { #define FATHER "Adam" #define GOOD_CHILD "Able" #define BAD_CHILD "Cain" #define DISTANCE_LEN 3 #define SMELLINESS_NAME "Smelliness" #define DISTANCE_NAME "Distance" #define TIME_NAME "Time" #define TIME_DIM 0 #define SMELLINESS_DIM 1 #define DISTANCE_DIM 2 #define GOAT_NAME "Billy_goat_gruff" #define CAMEL_NAME "Grumpy_the_camel" hid_t fileid, smelliness_spaceid, distance_spaceid, time_spaceid, spaceid; hid_t adam_grpid, able_grpid, cain_grpid; hid_t time_scaleid, smelliness_scaleid, distance_scaleid; hid_t goat_dsid, camel_dsid, cparmsid; hsize_t dims[NDIMS], max_dims[NDIMS]; /* Create file and group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((adam_grpid = H5Gcreate(fileid, FATHER, 0)) < 0) ERR; if ((able_grpid = H5Gcreate(adam_grpid, GOOD_CHILD, 0)) < 0) ERR; if ((cain_grpid = H5Gcreate(adam_grpid, BAD_CHILD, 0)) < 0) ERR; /* Create 3 1D spaces for the 3 dimension scale datasets. Time * and smelliness starts out as 0. They are unlimited dimension * scales. */ dims[0] = 0; max_dims[0] = H5S_UNLIMITED; if ((time_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = 0; max_dims[0] = H5S_UNLIMITED; if ((smelliness_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = DISTANCE_LEN; max_dims[0] = DISTANCE_LEN; if ((distance_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; /* Enable chunking for unlimited time and smelliness scale. */ if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; dims[0] = 1; if (H5Pset_chunk(cparmsid, 1, dims) < 0) ERR; /* Create our dimension scales. */ if ((time_scaleid = H5Dcreate(adam_grpid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(time_scaleid, TIME_NAME) < 0) ERR; if ((smelliness_scaleid = H5Dcreate(adam_grpid, SMELLINESS_NAME, H5T_NATIVE_FLOAT, smelliness_spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(smelliness_scaleid, SMELLINESS_NAME) < 0) ERR; if ((distance_scaleid = H5Dcreate(adam_grpid, DISTANCE_NAME, H5T_NATIVE_FLOAT, distance_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(distance_scaleid, DISTANCE_NAME) < 0) ERR; /* Create a space coresponding to these three dimensions. */ dims[TIME_DIM] = 0; dims[SMELLINESS_DIM] = 0; dims[DISTANCE_DIM] = DISTANCE_LEN; max_dims[TIME_DIM] = H5S_UNLIMITED; max_dims[SMELLINESS_DIM] = H5S_UNLIMITED; max_dims[DISTANCE_DIM] = DISTANCE_LEN; if ((spaceid = H5Screate_simple(NDIMS, dims, max_dims)) < 0) ERR; /* Set up chunking for our 3D vars. */ dims[TIME_DIM] = 1; dims[SMELLINESS_DIM] = 1; if (H5Pset_chunk(cparmsid, NDIMS, dims) < 0) ERR; /* Create two variables which use them, and attach the dimension scales. */ if ((goat_dsid = H5Dcreate(able_grpid, GOAT_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(goat_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(goat_dsid, smelliness_scaleid, 1) < 0) ERR; if (H5DSattach_scale(goat_dsid, distance_scaleid, 2) < 0) ERR; if ((camel_dsid = H5Dcreate(cain_grpid, CAMEL_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(camel_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(camel_dsid, smelliness_scaleid, 1) < 0) ERR; if (H5DSattach_scale(camel_dsid, distance_scaleid, 2) < 0) ERR; /* Close up the shop. */ if (H5Dclose(goat_dsid) < 0 || H5Dclose(camel_dsid) < 0 || H5Dclose(smelliness_scaleid) < 0 || H5Dclose(distance_scaleid) < 0 || H5Dclose(time_scaleid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(cain_grpid) < 0 || H5Gclose(able_grpid) < 0 || H5Gclose(adam_grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking 3D datasets in groups created with shared dimscales..."); { hid_t fileid, grpid; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, FATHER)) < 0) ERR; /* If we can't scan the group, crash into a flaming heap of * smoking, smoldering rubbish. */ if (rec_scan_group(grpid)) ERR; /* Close up the shop. */ if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; #endif FINAL_RESULTS; }
herr_t HDF5AttrIterate( hid_t hH5ObjID, const char *pszAttrName, void *pDS ) { hid_t hAttrID; hid_t hAttrTypeID; hid_t hAttrNativeType; hid_t hAttrSpace; char *szData = NULL; hsize_t nSize[64]; unsigned int nAttrElmts; hsize_t nAttrSize; hsize_t i; void *buf = NULL; unsigned int nAttrDims; char **papszTokens; HDF5Dataset *poDS; CPLString osKey; char *szValue = NULL; poDS = (HDF5Dataset *) pDS; // Convert "/" into "_" for the path component const char* pszPath = poDS->poH5CurrentObject->pszUnderscorePath; if(pszPath != NULL && strlen(pszPath) > 0) { papszTokens = CSLTokenizeString2( pszPath, "/", CSLT_HONOURSTRINGS ); for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i ) { if( i != 0) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy( papszTokens ); } // Convert whitespaces into "_" for the attribute name component papszTokens = CSLTokenizeString2( pszAttrName, " ", CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES ); for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i ) { if(!osKey.empty()) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy( papszTokens ); hAttrID = H5Aopen_name( hH5ObjID, pszAttrName ); hAttrTypeID = H5Aget_type( hAttrID ); hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); hAttrSpace = H5Aget_space( hAttrID ); nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL ); nAttrElmts = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElmts *= (int) nSize[i]; } if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { if ( H5Tis_variable_str(hAttrNativeType) ) { char** papszStrings; papszStrings = (char**) CPLMalloc( nAttrElmts * sizeof(char*) ); // Read the values H5Aread( hAttrID, hAttrNativeType, papszStrings ); // Concatenate all values as one string (separated by a space) CPLString osVal = papszStrings[0]; for( i=1; i < nAttrElmts; i++ ) { osVal += " "; osVal += papszStrings[i]; } szValue = (char*) CPLMalloc(osVal.length() + 1); strcpy( szValue, osVal.c_str() ); H5Dvlen_reclaim( hAttrNativeType, hAttrSpace, H5P_DEFAULT, papszStrings ); CPLFree( papszStrings ); } else { nAttrSize = H5Aget_storage_size( hAttrID ); szValue = (char*) CPLMalloc((size_t) (nAttrSize+1)); H5Aread( hAttrID, hAttrNativeType, szValue ); szValue[nAttrSize] = '\0'; } } else { if( nAttrElmts > 0 ) { buf = (void *) CPLMalloc( nAttrElmts* H5Tget_size( hAttrNativeType )); szData = (char*) CPLMalloc( 8192 ); szValue = (char*) CPLMalloc( MAX_METADATA_LEN ); szData[0] = '\0'; szValue[0] ='\0'; H5Aread( hAttrID, hAttrNativeType, buf ); } if( H5Tequal( H5T_NATIVE_CHAR, hAttrNativeType ) || H5Tequal( H5T_NATIVE_SCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c ", ((char *) buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c", ((char *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_SHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((short *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_USHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned short *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((int *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UINT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned int *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_LONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((long *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_ULONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((unsigned long *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { CPLsprintf( szData, "%.8g ", ((float *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { CPLsprintf( szData, "%.15g ", ((double *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } CPLFree( buf ); } H5Sclose(hAttrSpace); H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Aclose( hAttrID ); poDS->papszMetadata = CSLSetNameValue( poDS->papszMetadata, osKey, szValue); CPLFree( szData ); CPLFree( szValue ); return 0; }
void readSpectrumEnergyScale(cGlobal *global, char *filename) { char groupname[1024]; char fieldname[1024]; hid_t file_id; hid_t datagroup_id; hid_t dataset_id; hid_t dataspace_id; hid_t datatype_id; H5T_class_t dataclass; size_t size; int ndims; sprintf(groupname, "energySpectrum"); sprintf(fieldname, "runIntegratedEnergyScale"); // Check if an energy scale calibration file has been specified if ( strcmp(filename,"") == 0 ){ printf("spectrum energy scale calibration file path was not specified\n"); printf("spectra will be output with default (0) energy scale\n"); return; } // Check whether file exists! FILE* fp = fopen(filename, "r"); if (fp) // file exists fclose(fp); else { // file doesn't exist printf("specified energy scale calibration file does not exist: %s\n",filename); printf("spectra will be output with default (0) energy scale\n"); return; } printf("Reading energy spectrum scale calibration file:\n"); printf("\t%s\n",filename); // Open the file file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); printf("spectra will be output with default (0) energy scale\n"); return; } // Open the dataset datagroup_id = H5Gopen1(file_id, groupname); dataset_id = H5Dopen1(datagroup_id, fieldname); dataspace_id = H5Dget_space(dataset_id); // Test if correct dimensions / size ndims = H5Sget_simple_extent_ndims(dataspace_id); if(ndims != 1) { printf("the specified file does not have the correct dimensions for energy scale calibration, ndims=%i\n",ndims); printf("spectra will be output with default (0) energy scale\n"); return; } hsize_t dims[ndims]; H5Sget_simple_extent_dims(dataspace_id,dims,NULL); if (dims[0]!=1 || dims[1]!=(hsize_t)global->espectrumLength) { printf("the specified file does not have the correct dimensions for energy scale calibration\n"); printf("spectra will be output with default (0) energy scale\n"); return; } datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); double* energyscale = (double *) calloc(global->espectrumLength, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, energyscale); for(int i=0; i<global->espectrumLength; i++) { global->espectrumScale[i] = energyscale[i]; } free(energyscale); // Close and cleanup H5Dclose(dataset_id); H5Gclose(datagroup_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); printf("energy spectrum scale calibration file read successful:\n"); return; }