/** Create or retrieve a dataset for an event input. The dataset path will be /data/event/{class}/{srcFinfo}/{id}_{dataIndex}_{fieldIndex}. path : {source_object_id}.{source_field_name} TODO: check the returned hid_t and show appropriate error messages. */ hid_t NSDFWriter::getEventDataset(string srcPath, string srcField) { string eventSrcPath = srcPath + string("/") + srcField; map< string, hid_t >::iterator it = eventSrcDataset_.find(eventSrcPath); if (it != eventSrcDataset_.end()){ return it->second; } ObjId source(srcPath); herr_t status; htri_t exists = -1; string className = Field<string>::get(source, "className"); string path = EVENTPATH + string("/") + className + string("/") + srcField; hid_t container = require_group(filehandle_, path); stringstream dsetname; dsetname << source.id.value() <<"_" << source.dataIndex << "_" << source.fieldIndex; hid_t dataset = createDoubleDataset(container, dsetname.str().c_str()); classFieldToEvent_[className + "/" + srcField].push_back(dataset); classFieldToEventSrc_[className + "/" + srcField].push_back(srcPath); status = writeScalarAttr<string>(dataset, "source", srcPath); assert(status >= 0); status = writeScalarAttr<string>(dataset, "field", srcField); assert(status >= 0); eventSrcDataset_[eventSrcPath] = dataset; return dataset; }
/** Traverse the path of an object in HDF5 file, checking existence of groups in the path and creating them if required. */ hid_t HDF5DataWriter::getDataset(string path) { if (filehandle_ < 0){ return -1; } herr_t status = H5Eset_auto2(H5E_DEFAULT, NULL, NULL); // Create the groups corresponding to this path string::size_type lastslash = path.find_last_of("/"); vector<string> pathTokens; moose::tokenize(path, "/", pathTokens); hid_t prev_id = filehandle_; hid_t id = -1; for ( unsigned int ii = 0; ii < pathTokens.size()-1; ++ii ){ // check if object exists htri_t exists = H5Lexists(prev_id, pathTokens[ii].c_str(), H5P_DEFAULT); if (exists > 0){ // try to open existing group id = H5Gopen2(prev_id, pathTokens[ii].c_str(), H5P_DEFAULT); } else if (exists == 0) { // If that fails, try to create a group id = H5Gcreate2(prev_id, pathTokens[ii].c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } if ((exists < 0) || (id < 0)){ // Failed to open/create a group, print the // offending path (for debugging; the error is // perhaps at the level of hdf5 or file system). cerr << "Error: failed to open/create group: "; for (unsigned int jj = 0; jj <= ii; ++jj){ cerr << "/" << pathTokens[jj]; } cerr << endl; prev_id = -1; } if (prev_id >= 0 && prev_id != filehandle_){ // Successfully opened/created new group, close the old group status = H5Gclose(prev_id); assert( status >= 0 ); } prev_id = id; } string name = pathTokens[pathTokens.size()-1]; htri_t exists = H5Lexists(prev_id, name.c_str(), H5P_DEFAULT); hid_t dataset_id = -1; if (exists > 0){ dataset_id = H5Dopen2(prev_id, name.c_str(), H5P_DEFAULT); } else if (exists == 0){ dataset_id = createDoubleDataset(prev_id, name); } else { cerr << "Error: H5Lexists returned " << exists << " for path \"" << path << "\"" << endl; } return dataset_id; }