/* * Open a file through the HDF5 interface. */ static void *HDF5_Open(char *testFileName, IOR_param_t * param) { hid_t accessPropList, createPropList; hsize_t memStart[NUM_DIMS], dataSetDims[NUM_DIMS], memStride[NUM_DIMS], memCount[NUM_DIMS], memBlock[NUM_DIMS], memDataSpaceDims[NUM_DIMS]; int tasksPerDataSet; unsigned fd_mode = (unsigned)0; hid_t *fd; MPI_Comm comm; MPI_Info mpiHints = MPI_INFO_NULL; fd = (hid_t *) malloc(sizeof(hid_t)); if (fd == NULL) ERR("malloc() failed"); /* * HDF5 uses different flags than those for POSIX/MPIIO */ if (param->open == WRITE) { /* WRITE flags */ param->openFlags = IOR_TRUNC; } else { /* READ or check WRITE/READ flags */ param->openFlags = IOR_RDONLY; } /* set IOR file flags to HDF5 flags */ /* -- file open flags -- */ if (param->openFlags & IOR_RDONLY) { fd_mode |= H5F_ACC_RDONLY; } if (param->openFlags & IOR_WRONLY) { fprintf(stdout, "File write only not implemented in HDF5\n"); } if (param->openFlags & IOR_RDWR) { fd_mode |= H5F_ACC_RDWR; } if (param->openFlags & IOR_APPEND) { fprintf(stdout, "File append not implemented in HDF5\n"); } if (param->openFlags & IOR_CREAT) { fd_mode |= H5F_ACC_CREAT; } if (param->openFlags & IOR_EXCL) { fd_mode |= H5F_ACC_EXCL; } if (param->openFlags & IOR_TRUNC) { fd_mode |= H5F_ACC_TRUNC; } if (param->openFlags & IOR_DIRECT) { fprintf(stdout, "O_DIRECT not implemented in HDF5\n"); } /* set up file creation property list */ createPropList = H5Pcreate(H5P_FILE_CREATE); HDF5_CHECK(createPropList, "cannot create file creation property list"); /* set size of offset and length used to address HDF5 objects */ HDF5_CHECK(H5Pset_sizes (createPropList, sizeof(hsize_t), sizeof(hsize_t)), "cannot set property list properly"); /* set up file access property list */ accessPropList = H5Pcreate(H5P_FILE_ACCESS); HDF5_CHECK(accessPropList, "cannot create file access property list"); /* * someday HDF5 implementation will allow subsets of MPI_COMM_WORLD */ /* store MPI communicator info for the file access property list */ if (param->filePerProc) { comm = MPI_COMM_SELF; } else { comm = testComm; } SetHints(&mpiHints, param->hintsFileName); /* * note that with MP_HINTS_FILTERED=no, all key/value pairs will * be in the info object. The info object that is attached to * the file during MPI_File_open() will only contain those pairs * deemed valid by the implementation. */ /* show hints passed to file */ if (rank == 0 && param->showHints) { fprintf(stdout, "\nhints passed to access property list {\n"); ShowHints(&mpiHints); fprintf(stdout, "}\n"); } HDF5_CHECK(H5Pset_fapl_mpio(accessPropList, comm, mpiHints), "cannot set file access property list"); /* set alignment */ HDF5_CHECK(H5Pset_alignment(accessPropList, param->setAlignment, param->setAlignment), "cannot set alignment"); /* open file */ if (param->open == WRITE) { /* WRITE */ *fd = H5Fcreate(testFileName, fd_mode, createPropList, accessPropList); HDF5_CHECK(*fd, "cannot create file"); } else { /* READ or CHECK */ *fd = H5Fopen(testFileName, fd_mode, accessPropList); HDF5_CHECK(*fd, "cannot open file"); } /* show hints actually attached to file handle */ if (param->showHints || (1) /* WEL - this needs fixing */ ) { if (rank == 0 && (param->showHints) /* WEL - this needs fixing */ ) { WARN("showHints not working for HDF5"); } } else { MPI_Info mpiHintsCheck = MPI_INFO_NULL; hid_t apl; apl = H5Fget_access_plist(*fd); HDF5_CHECK(H5Pget_fapl_mpio(apl, &comm, &mpiHintsCheck), "cannot get info object through HDF5"); if (rank == 0) { fprintf(stdout, "\nhints returned from opened file (HDF5) {\n"); ShowHints(&mpiHintsCheck); fprintf(stdout, "}\n"); if (1 == 1) { /* request the MPIIO file handle and its hints */ MPI_File *fd_mpiio; HDF5_CHECK(H5Fget_vfd_handle (*fd, apl, (void **)&fd_mpiio), "cannot get MPIIO file handle"); MPI_CHECK(MPI_File_get_info (*fd_mpiio, &mpiHintsCheck), "cannot get info object through MPIIO"); fprintf(stdout, "\nhints returned from opened file (MPIIO) {\n"); ShowHints(&mpiHintsCheck); fprintf(stdout, "}\n"); } } MPI_CHECK(MPI_Barrier(testComm), "barrier error"); } /* this is necessary for resetting various parameters needed for reopening and checking the file */ newlyOpenedFile = TRUE; HDF5_CHECK(H5Pclose(createPropList), "cannot close creation property list"); HDF5_CHECK(H5Pclose(accessPropList), "cannot close access property list"); /* create property list for serial/parallel access */ xferPropList = H5Pcreate(H5P_DATASET_XFER); HDF5_CHECK(xferPropList, "cannot create transfer property list"); /* set data transfer mode */ if (param->collective) { HDF5_CHECK(H5Pset_dxpl_mpio(xferPropList, H5FD_MPIO_COLLECTIVE), "cannot set collective data transfer mode"); } else { HDF5_CHECK(H5Pset_dxpl_mpio (xferPropList, H5FD_MPIO_INDEPENDENT), "cannot set independent data transfer mode"); } /* set up memory data space for transfer */ memStart[0] = (hsize_t) 0; memCount[0] = (hsize_t) 1; memStride[0] = (hsize_t) (param->transferSize / sizeof(IOR_size_t)); memBlock[0] = (hsize_t) (param->transferSize / sizeof(IOR_size_t)); memDataSpaceDims[0] = (hsize_t) param->transferSize; memDataSpace = H5Screate_simple(NUM_DIMS, memDataSpaceDims, NULL); HDF5_CHECK(memDataSpace, "cannot create simple memory data space"); /* define hyperslab for memory data space */ HDF5_CHECK(H5Sselect_hyperslab(memDataSpace, H5S_SELECT_SET, memStart, memStride, memCount, memBlock), "cannot create hyperslab"); /* set up parameters for fpp or different dataset count */ if (param->filePerProc) { tasksPerDataSet = 1; } else { if (param->individualDataSets) { /* each task in segment has single data set */ tasksPerDataSet = 1; } else { /* share single data set across all tasks in segment */ tasksPerDataSet = param->numTasks; } } dataSetDims[0] = (hsize_t) ((param->blockSize / sizeof(IOR_size_t)) * tasksPerDataSet); /* create a simple data space containing information on size and shape of data set, and open it for access */ dataSpace = H5Screate_simple(NUM_DIMS, dataSetDims, NULL); HDF5_CHECK(dataSpace, "cannot create simple data space"); return (fd); }
int h5repack_cmp_pl(const char *fname1, const char *fname2) { int ret_value = 0; /*no need to LEAVE() on ERROR: HERR_INIT(int, SUCCEED) */ hid_t fid1=-1; /* file ID */ hid_t fid2=-1; /* file ID */ hid_t dset1=-1; /* dataset ID */ hid_t dset2=-1; /* dataset ID */ hid_t gid=-1; /* group ID */ hid_t dcpl1=-1; /* dataset creation property list ID */ hid_t dcpl2=-1; /* dataset creation property list ID */ hid_t gcplid=-1; /* group creation property list */ unsigned crt_order_flag1; /* group creation order flag */ unsigned crt_order_flag2; /* group creation order flag */ trav_table_t *trav=NULL; int ret=1; unsigned int i; /*------------------------------------------------------------------------- * open the files *------------------------------------------------------------------------- */ /* disable error reporting */ H5E_BEGIN_TRY { /* Open the files */ if ((fid1 = H5Fopen(fname1,H5F_ACC_RDONLY,H5P_DEFAULT)) < 0 ) { error_msg("<%s>: %s\n", fname1, H5FOPENERROR ); return -1; } if ((fid2 = H5Fopen(fname2,H5F_ACC_RDONLY,H5P_DEFAULT)) < 0 ) { error_msg("<%s>: %s\n", fname2, H5FOPENERROR ); H5Fclose(fid1); return -1; } /* enable error reporting */ } H5E_END_TRY; /*------------------------------------------------------------------------- * get file table list of objects *------------------------------------------------------------------------- */ trav_table_init(&trav); if(h5trav_gettable(fid1, trav) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "h5trav_gettable failed"); /*------------------------------------------------------------------------- * traverse the suppplied object list *------------------------------------------------------------------------- */ for(i = 0; i < trav->nobjs; i++) { if(trav->objs[i].type == H5TRAV_TYPE_GROUP) { if ((gid = H5Gopen2(fid1, trav->objs[i].name, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Gopen2 failed"); if ((gcplid = H5Gget_create_plist(gid)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Gget_create_plist failed"); if (H5Pget_link_creation_order(gcplid, &crt_order_flag1) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pget_link_creation_order failed"); if (H5Pclose(gcplid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); if (H5Gclose(gid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Gclose failed"); if ((gid = H5Gopen2(fid2, trav->objs[i].name, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Gopen2 failed"); if ((gcplid = H5Gget_create_plist(gid)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Gget_create_plist failed"); if (H5Pget_link_creation_order(gcplid, &crt_order_flag2) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pget_link_creation_order failed"); if (H5Pclose(gcplid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); if (H5Gclose(gid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Gclose failed"); if (crt_order_flag1 != crt_order_flag2) { error_msg("property lists for <%s> are different\n",trav->objs[i].name); HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "property lists failed"); } } else if(trav->objs[i].type == H5TRAV_TYPE_DATASET) { if((dset1 = H5Dopen2(fid1, trav->objs[i].name, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dopen2 failed"); if((dset2 = H5Dopen2(fid2, trav->objs[i].name, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dopen2 failed"); if((dcpl1 = H5Dget_create_plist(dset1)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_create_plist failed"); if((dcpl2 = H5Dget_create_plist(dset2)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_create_plist failed"); /*------------------------------------------------------------------------- * compare the property lists *------------------------------------------------------------------------- */ if((ret = H5Pequal(dcpl1, dcpl2)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pequal failed"); if(ret == 0) { error_msg("property lists for <%s> are different\n",trav->objs[i].name); HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "property lists failed"); } /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if(H5Pclose(dcpl1) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); if(H5Pclose(dcpl2) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); if(H5Dclose(dset1) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dclose failed"); if(H5Dclose(dset2) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dclose failed"); } /*if*/ } /*i*/ /*------------------------------------------------------------------------- * free *------------------------------------------------------------------------- */ trav_table_free(trav); /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ H5Fclose(fid1); H5Fclose(fid2); return ret; /*------------------------------------------------------------------------- * error *------------------------------------------------------------------------- */ done: H5E_BEGIN_TRY { H5Pclose(dcpl1); H5Pclose(dcpl2); H5Dclose(dset1); H5Dclose(dset2); H5Fclose(fid1); H5Fclose(fid2); H5Pclose(gcplid); H5Gclose(gid); trav_table_free(trav); } H5E_END_TRY; return ret_value; }
static file open(std::string const& name, unsigned int flags) { hid_t id = H5Fopen(name.c_str(), flags, H5P_DEFAULT); if (id < 0) throw std::runtime_error("H5Fopen failed"); return file(std::move(id)); }
/*------------------------------------------------------------------------- * Function: test_family_compat * * Purpose: Tests the backward compatibility for FAMILY driver. * See if we can open files created with v1.6 library. * The source file was created by the test/file_handle.c * of the v1.6 library. Then tools/misc/h5repart.c was * used to concantenated. The command was "h5repart -m 5k * family_file%05d.h5 family_v16_%05d.h5". * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * June 3, 2005 * *------------------------------------------------------------------------- */ static herr_t test_family_compat(void) { hid_t file = (-1), fapl; hid_t dset; char dname[]="dataset"; char filename[1024]; char pathname[1024], pathname_individual[1024]; char newname[1024], newname_individual[1024]; FILE *tmp_fp, *old_fp; /* Pointers to temp & old files */ int counter = 0; TESTING("FAMILY file driver backward compatibility"); /* Set property list and file name for FAMILY driver */ fapl = h5_fileaccess(); if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE2, H5P_DEFAULT) < 0) TEST_ERROR; h5_fixname(COMPAT_BASENAME, fapl, filename, sizeof filename); h5_fixname(FILENAME[3], fapl, newname, sizeof newname); pathname[0] = '\0'; HDstrcat(pathname, filename); /* The following code makes the copies of the family files in the source directory. * Since we're going to open the files with write mode, this protects the original * files. */ sprintf(newname_individual, newname, counter); sprintf(pathname_individual, pathname, counter); while (h5_make_local_copy(pathname_individual, newname_individual) >= 0) { counter++; sprintf(newname_individual, newname, counter); sprintf(pathname_individual, pathname, counter); } if ((NULL != (old_fp = HDfopen(pathname_individual,"rb"))) && (NULL != (tmp_fp = HDfopen(newname_individual,"wb")))) TEST_ERROR; /* Make sure we can open the file. Use the read and write mode to flush the * superblock. */ if((file = H5Fopen(newname, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, dname, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Open the file again to make sure it isn't corrupted. */ if((file = H5Fopen(newname, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, dname, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Fclose(file); H5Pclose(fapl); } H5E_END_TRY; return -1; } /* end test_family_compat() */
bool FWSingleOMP::run() { hdf_WGT_data.setFileName(xmlrootName); hdf_OBS_data.setFileName(xmlrootName); if (doWeights==1) { fillIDMatrix(); hdf_WGT_data.makeFile(); hdf_WGT_data.openFile(); hdf_WGT_data.addFW(0); for (int i=0;i<Weights.size();i++) hdf_WGT_data.addStep(i,Weights[i]); hdf_WGT_data.closeFW(); hdf_WGT_data.closeFile(); for(int ill=1;ill<weightLength;ill++) { transferParentsOneGeneration(); FWOneStep(); // WeightHistory.push_back(Weights); hdf_WGT_data.openFile(); hdf_WGT_data.addFW(ill); for (int i=0;i<Weights.size();i++) hdf_WGT_data.addStep(i,Weights[i]); hdf_WGT_data.closeFW(); hdf_WGT_data.closeFile(); } } else { fillIDMatrix(); // find weight length from the weight file hid_t f_file = H5Fopen(hdf_WGT_data.getFileName().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT); hsize_t numGrps = 0; H5Gget_num_objs(f_file, &numGrps); weightLength = static_cast<int> (numGrps)-1; if (H5Fclose(f_file)>-1) f_file=-1; if (verbose>0) app_log()<<" weightLength "<<weightLength<<endl; } if (verbose>0) app_log()<<" Done Computing Weights"<<endl; if (doObservables==1) { int nprops = H.sizeOfObservables();//local energy, local potnetial and all hamiltonian elements int FirstHamiltonian = H.startIndex(); // vector<vector<vector<RealType> > > savedValues; int nelectrons = W[0]->R.size(); int nfloats=OHMMS_DIM*nelectrons; // W.clearEnsemble(); makeClones(W,Psi,H); vector<ForwardWalkingData* > FWvector; for(int ip=0; ip<NumThreads; ip++) FWvector.push_back(new ForwardWalkingData(nelectrons)); if (myComm->rank()==0) hdf_OBS_data.makeFile(); hdf_float_data.openFile(fname.str()); for(int step=0;step<numSteps;step++) { hdf_float_data.setStep(step); vector<RealType> stepObservables(walkersPerBlock[step]*(nprops+2), 0); for(int wstep=0; wstep<walkersPerBlock[step];) { vector<float> ThreadsCoordinate(NumThreads*nfloats); int nwalkthread = hdf_float_data.getFloat(wstep*nfloats, (wstep+NumThreads)*nfloats, ThreadsCoordinate) / nfloats; // for(int j=0;j<ThreadsCoordinate.size();j++)cout<<ThreadsCoordinate[j]<<" "; // cout<<endl; #pragma omp parallel for for(int ip=0; ip<nwalkthread; ip++) { vector<float> SINGLEcoordinate(0); vector<float>::iterator TCB1(ThreadsCoordinate.begin()+ip*nfloats), TCB2(ThreadsCoordinate.begin()+(1+ip)*nfloats); SINGLEcoordinate.insert(SINGLEcoordinate.begin(),TCB1,TCB2); FWvector[ip]->fromFloat(SINGLEcoordinate); wClones[ip]->R=FWvector[ip]->Pos; wClones[ip]->update(); RealType logpsi(psiClones[ip]->evaluateLog(*wClones[ip])); RealType eloc=hClones[ip]->evaluate( *wClones[ip] ); hClones[ip]->auxHevaluate(*wClones[ip]); int indx=(wstep+ip)*(nprops+2); stepObservables[indx]= eloc; stepObservables[indx+1]= hClones[ip]->getLocalPotential(); for(int i=0;i<nprops;i++) stepObservables[indx+i+2] = hClones[ip]->getObservable(i) ; } wstep+=nwalkthread; for(int ip=0; ip<NumThreads; ip++) wClones[ip]->resetCollectables(); } hdf_OBS_data.openFile(); hdf_OBS_data.addStep(step, stepObservables); hdf_OBS_data.closeFile(); // savedValues.push_back(stepObservables); hdf_float_data.endStep(); if (verbose >1) cout<<"Done with step: "<<step<<endl; } } if(doDat>=1) { vector<int> Dimensions(4); hdf_WGT_data.openFile(); hdf_OBS_data.openFile(); Estimators->start(weightLength,1); int nprops; if (doObservables==1) nprops = H.sizeOfObservables()+2; else { int Noo = hdf_OBS_data.numObsStep(0); int Nwl = hdf_WGT_data.numWgtStep(0); nprops = Noo/Nwl; } for(int ill=0;ill<weightLength;ill++) { Dimensions[0]=ill; Dimensions[1]= nprops ; Dimensions[2]=numSteps; Dimensions[3]=startStep; Estimators->startBlock(1); Estimators->accumulate(hdf_OBS_data,hdf_WGT_data,Dimensions); Estimators->stopBlock(getNumberOfSamples(ill)); } hdf_OBS_data.closeFile(); hdf_WGT_data.closeFile(); Estimators->stop(); } return true; }
/*------------------------------------------------------------------------- * Function: toomany * * Purpose: Build a file with too many symbolic links * * Return: Success: 0 * * Failure: -1 * * Programmer: Quincey Koziol * Tuesday, August 9, 2005 * * Modifications: * *------------------------------------------------------------------------- */ static int toomany(hid_t fapl) { hid_t fid = (-1); /* File ID */ hid_t gid = (-1); /* Group ID */ hid_t gid2 = (-1); /* Datatype ID */ char objname[NAME_BUF_SIZE]; /* Object name */ ssize_t name_len; /* Length of object name */ char filename[NAME_BUF_SIZE]; TESTING("too many links"); /* Make certain test is valid */ /* XXX: should probably make a "generic" test that creates the proper * # of links based on this value - QAK */ HDassert(H5G_NLINKS == 16); /* Create files */ h5_fixname(FILENAME[1], fapl, filename, sizeof filename); if((fid=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl))<0) TEST_ERROR; /* Create group with short name in file (used as target for hard links) */ if((gid=H5Gcreate (fid, "final", (size_t)0))<0) TEST_ERROR; /* Create chain of hard links to existing object (no limit on #) */ if(H5Glink2(fid, "final", H5G_LINK_HARD, fid, "hard1") < 0) TEST_ERROR; if(H5Glink2(fid, "hard1", H5G_LINK_HARD, fid, "hard2") < 0) TEST_ERROR; if(H5Glink2(fid, "hard2", H5G_LINK_HARD, fid, "hard3") < 0) TEST_ERROR; if(H5Glink2(fid, "hard3", H5G_LINK_HARD, fid, "hard4") < 0) TEST_ERROR; if(H5Glink2(fid, "hard4", H5G_LINK_HARD, fid, "hard5") < 0) TEST_ERROR; if(H5Glink2(fid, "hard5", H5G_LINK_HARD, fid, "hard6") < 0) TEST_ERROR; if(H5Glink2(fid, "hard6", H5G_LINK_HARD, fid, "hard7") < 0) TEST_ERROR; if(H5Glink2(fid, "hard7", H5G_LINK_HARD, fid, "hard8") < 0) TEST_ERROR; if(H5Glink2(fid, "hard8", H5G_LINK_HARD, fid, "hard9") < 0) TEST_ERROR; if(H5Glink2(fid, "hard9", H5G_LINK_HARD, fid, "hard10") < 0) TEST_ERROR; if(H5Glink2(fid, "hard10", H5G_LINK_HARD, fid, "hard11") < 0) TEST_ERROR; if(H5Glink2(fid, "hard11", H5G_LINK_HARD, fid, "hard12") < 0) TEST_ERROR; if(H5Glink2(fid, "hard12", H5G_LINK_HARD, fid, "hard13") < 0) TEST_ERROR; if(H5Glink2(fid, "hard13", H5G_LINK_HARD, fid, "hard14") < 0) TEST_ERROR; if(H5Glink2(fid, "hard14", H5G_LINK_HARD, fid, "hard15") < 0) TEST_ERROR; if(H5Glink2(fid, "hard15", H5G_LINK_HARD, fid, "hard16") < 0) TEST_ERROR; if(H5Glink2(fid, "hard16", H5G_LINK_HARD, fid, "hard17") < 0) TEST_ERROR; if(H5Glink2(fid, "hard17", H5G_LINK_HARD, fid, "hard18") < 0) TEST_ERROR; if(H5Glink2(fid, "hard18", H5G_LINK_HARD, fid, "hard19") < 0) TEST_ERROR; if(H5Glink2(fid, "hard19", H5G_LINK_HARD, fid, "hard20") < 0) TEST_ERROR; if(H5Glink2(fid, "hard20", H5G_LINK_HARD, fid, "hard21") < 0) TEST_ERROR; /* Create chain of soft links to existing object (limited) */ if(H5Glink2(fid, "final", H5G_LINK_SOFT, fid, "soft1") < 0) TEST_ERROR; if(H5Glink2(fid, "soft1", H5G_LINK_SOFT, fid, "soft2") < 0) TEST_ERROR; if(H5Glink2(fid, "soft2", H5G_LINK_SOFT, fid, "soft3") < 0) TEST_ERROR; if(H5Glink2(fid, "soft3", H5G_LINK_SOFT, fid, "soft4") < 0) TEST_ERROR; if(H5Glink2(fid, "soft4", H5G_LINK_SOFT, fid, "soft5") < 0) TEST_ERROR; if(H5Glink2(fid, "soft5", H5G_LINK_SOFT, fid, "soft6") < 0) TEST_ERROR; if(H5Glink2(fid, "soft6", H5G_LINK_SOFT, fid, "soft7") < 0) TEST_ERROR; if(H5Glink2(fid, "soft7", H5G_LINK_SOFT, fid, "soft8") < 0) TEST_ERROR; if(H5Glink2(fid, "soft8", H5G_LINK_SOFT, fid, "soft9") < 0) TEST_ERROR; if(H5Glink2(fid, "soft9", H5G_LINK_SOFT, fid, "soft10") < 0) TEST_ERROR; if(H5Glink2(fid, "soft10", H5G_LINK_SOFT, fid, "soft11") < 0) TEST_ERROR; if(H5Glink2(fid, "soft11", H5G_LINK_SOFT, fid, "soft12") < 0) TEST_ERROR; if(H5Glink2(fid, "soft12", H5G_LINK_SOFT, fid, "soft13") < 0) TEST_ERROR; if(H5Glink2(fid, "soft13", H5G_LINK_SOFT, fid, "soft14") < 0) TEST_ERROR; if(H5Glink2(fid, "soft14", H5G_LINK_SOFT, fid, "soft15") < 0) TEST_ERROR; if(H5Glink2(fid, "soft15", H5G_LINK_SOFT, fid, "soft16") < 0) TEST_ERROR; if(H5Glink2(fid, "soft16", H5G_LINK_SOFT, fid, "soft17") < 0) TEST_ERROR; /* Close objects */ if(H5Gclose(gid)<0) TEST_ERROR; if(H5Fclose(fid)<0) TEST_ERROR; /* Open file */ if((fid=H5Fopen(filename, H5F_ACC_RDWR, fapl))<0) TEST_ERROR; /* Open object through last hard link */ if((gid = H5Gopen(fid, "hard21")) < 0) TEST_ERROR; /* Check name */ if((name_len = H5Iget_name( gid, objname, (size_t)NAME_BUF_SIZE )) < 0) TEST_ERROR if(HDstrcmp(objname, "/hard21")) TEST_ERROR /* Create object in hard-linked group */ if((gid2 = H5Gcreate(gid, "new_hard", (size_t)0)) < 0) TEST_ERROR /* Close group in hard-linked group */ if(H5Gclose(gid2) < 0) TEST_ERROR /* Close hard-linked object */ if(H5Gclose(gid) < 0) TEST_ERROR; /* Open object through too deep soft link */ H5E_BEGIN_TRY { gid = H5Gopen(fid, "soft17"); } H5E_END_TRY; if (gid >= 0) { H5_FAILED(); puts(" Should have failed for sequence of too many nested links."); goto error; } /* Open object through lesser soft link */ if((gid = H5Gopen(fid, "soft16")) < 0) TEST_ERROR; /* Check name */ if((name_len = H5Iget_name( gid, objname, (size_t)NAME_BUF_SIZE )) < 0) TEST_ERROR if(HDstrcmp(objname, "/soft16")) TEST_ERROR /* Create object in external file */ if((gid2 = H5Gcreate(gid, "new_soft", (size_t)0)) < 0) TEST_ERROR /* Close group in external file */ if(H5Gclose(gid2) < 0) TEST_ERROR /* Close external object */ if(H5Gclose(gid) < 0) TEST_ERROR; /* Close first file */ if(H5Fclose(fid)<0) TEST_ERROR; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Gclose (gid2); H5Gclose (gid); H5Fclose (fid); } H5E_END_TRY; return -1; } /* end toomany() */
/*------------------------------------------------------------------------- * Function: test_core * * Purpose: Tests the file handle interface for CORE driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * * Modifications: * * Raymond Lu * Wednesday, June 23, 2004 * Added test for H5Fget_filesize. * * Raymond Lu, 2006-11-30 * Enabled the driver to read an existing file depending on * the setting of the backing_store and file open flags. *------------------------------------------------------------------------- */ static herr_t test_core(void) { hid_t file=(-1), fapl, access_fapl = -1; char filename[1024]; void *fhandle=NULL; hsize_t file_size; int *points, *check, *p1, *p2; hid_t dset1=-1, space1=-1; hsize_t dims1[2]; int i, j, n; TESTING("CORE file driver"); /* Set property list and file name for CORE driver */ fapl = h5_fileaccess(); if(H5Pset_fapl_core(fapl, (size_t)CORE_INCREMENT, TRUE) < 0) TEST_ERROR; h5_fixname(FILENAME[1], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, H5P_DEFAULT, &fhandle) < 0) TEST_ERROR; if(fhandle==NULL) { printf("fhandle==NULL\n"); TEST_ERROR; } /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* There is no garantee the size of metadata in file is constant. * Just try to check if it's reasonable. Why is this 4KB? */ if(file_size<2*KB || file_size>6*KB) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Open the file with backing store off for read and write. * Changes won't be saved in file. */ if(H5Pset_fapl_core(fapl, (size_t)CORE_INCREMENT, FALSE) < 0) TEST_ERROR; if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Allocate memory for data set. */ points=(int*)malloc(DSET1_DIM1*DSET1_DIM2*sizeof(int)); check=(int*)malloc(DSET1_DIM1*DSET1_DIM2*sizeof(int)); /* Initialize the dset1 */ p1 = points; for(i = n = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) *p1++ = n++; /* Create the data space1 */ dims1[0] = DSET1_DIM1; dims1[1] = DSET1_DIM2; if((space1 = H5Screate_simple(2, dims1, NULL)) < 0) TEST_ERROR; /* Create the dset1 */ if((dset1 = H5Dcreate2(file, DSET1_NAME, H5T_NATIVE_INT, space1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Write the data to the dset1 */ if(H5Dwrite(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if((dset1 = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* Read the data back from dset1 */ if(H5Dread(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, check) < 0) TEST_ERROR; /* Check that the values read are the same as the values written */ p1 = points; p2 = check; for(i = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) if(*p1++ != *p2++) { H5_FAILED(); printf(" Read different values than written in data set 1.\n"); printf(" At index %d,%d\n", i, j); TEST_ERROR; } /* end if */ if(H5Dclose(dset1) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Open the file with backing store on for read and write. * Changes will be saved in file. */ if(H5Pset_fapl_core(fapl, (size_t)CORE_INCREMENT, TRUE) < 0) TEST_ERROR; if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create the dset1 */ if((dset1 = H5Dcreate2(file, DSET1_NAME, H5T_NATIVE_INT, space1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Write the data to the dset1 */ if(H5Dwrite(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if((dset1 = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* Reallocate memory for reading buffer. */ if(check) free(check); check = (int*)malloc(DSET1_DIM1 * DSET1_DIM2 * sizeof(int)); /* Read the data back from dset1 */ if(H5Dread(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, check) < 0) TEST_ERROR; /* Check that the values read are the same as the values written */ p1 = points; p2 = check; for(i = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) if(*p1++ != *p2++) { H5_FAILED(); printf(" Read different values than written in data set 1.\n"); printf(" At index %d,%d\n", i, j); TEST_ERROR; } /* end if */ /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* There is no garantee the size of metadata in file is constant. * Just try to check if it's reasonable. */ if(file_size<64*KB || file_size>256*KB) TEST_ERROR; if(H5Sclose(space1) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; if(points) free(points); if(check) free(check); h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Pclose (fapl); H5Fclose(file); } H5E_END_TRY; return -1; }
int main (void) { hid_t file, space, dset, obj, attr; /* Handles */ herr_t status; hsize_t dims[1] = {DIM0}; hobj_ref_t wdata[DIM0], /* Write buffer */ *rdata; /* Read buffer */ H5G_obj_t objtype; ssize_t size; char *name; int ndims, i; /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create a dataset with a scalar dataspace. */ space = H5Screate (H5S_SCALAR); obj = H5Dcreate (file, "DS2", H5T_STD_I32LE, space, H5P_DEFAULT); status = H5Dclose (obj); status = H5Sclose (space); /* * Create a group. */ obj = H5Gcreate (file, "G1", H5P_DEFAULT); status = H5Gclose (obj); /* * Create references to the previously created objects. Passing -1 * as space_id causes this parameter to be ignored. Other values * besides valid dataspaces result in an error. */ status = H5Rcreate (&wdata[0], file, "G1", H5R_OBJECT, -1); status = H5Rcreate (&wdata[1], file, "DS2", H5R_OBJECT, -1); /* * Create dataset with a scalar dataspace to serve as the parent * for the attribute. */ space = H5Screate (H5S_SCALAR); dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); status = H5Sclose (space); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the attribute and write the object references to it. */ attr = H5Acreate (dset, ATTRIBUTE, H5T_STD_REF_OBJ, space, H5P_DEFAULT); status = H5Awrite (attr, H5T_STD_REF_OBJ, wdata); /* * Close and release resources. */ status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the attribute has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file, dataset, and attribute. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET); attr = H5Aopen_name (dset, ATTRIBUTE); /* * Get dataspace and allocate memory for read buffer. */ space = H5Aget_space (attr); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (hobj_ref_t *) malloc (dims[0] * sizeof (hobj_ref_t)); /* * Read the data. */ status = H5Aread (attr, H5T_STD_REF_OBJ, rdata); /* * Output the data to the screen. */ for (i=0; i<dims[0]; i++) { printf ("%s[%d]:\n ->", ATTRIBUTE, i); /* * Open the referenced object, get its name and type. */ obj = H5Rdereference (dset, H5R_OBJECT, &rdata[i]); objtype = H5Rget_obj_type (dset, H5R_OBJECT, &rdata[i]); /* * Get the length of the name, allocate space, then retrieve * the name. */ size = 1 + H5Iget_name (obj, NULL, 0); if (size > 1) { name = (char *) malloc (size); size = 1 + H5Iget_name (obj, name, size); } /* * Print the object type and close the object. */ switch (objtype) { case H5G_GROUP: printf ("Group"); status = H5Gclose (obj); break; case H5G_DATASET: printf ("Dataset"); status = H5Dclose (obj); break; case H5G_TYPE: printf ("Named Datatype"); status = H5Tclose (obj); } /* * Print the name and deallocate space for the name. */ if (size > 1) { printf (": %s", name); free (name); } printf ("\n"); } /* * Close and release resources. */ free (rdata); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Fclose (file); return 0; }
/*-------------------------------------------------------------------------*/ int FTI_RecoverHDF5(FTIT_configuration* FTI_Conf, FTIT_execution* FTI_Exec, FTIT_checkpoint* FTI_Ckpt, FTIT_dataset* FTI_Data) { char str[FTI_BUFS], fn[FTI_BUFS]; snprintf(fn, FTI_BUFS, "%s/%s", FTI_Ckpt[FTI_Exec->ckptLvel].dir, FTI_Exec->meta[FTI_Exec->ckptLvel].ckptFile); if( FTI_Exec->h5SingleFile ) { snprintf( fn, FTI_BUFS, "%s/%s-ID%08d.h5", FTI_Conf->h5SingleFileDir, FTI_Conf->h5SingleFilePrefix, FTI_Exec->ckptID ); } sprintf(str, "Trying to load FTI checkpoint file (%s)...", fn); FTI_Print(str, FTI_DBUG); hid_t file_id; //Open hdf5 file if( FTI_Exec->h5SingleFile ) { hid_t plid = H5Pcreate( H5P_FILE_ACCESS ); H5Pset_fapl_mpio( plid, FTI_COMM_WORLD, MPI_INFO_NULL ); file_id = H5Fopen( fn, H5F_ACC_RDONLY, plid ); H5Pclose( plid ); } else { file_id = H5Fopen(fn, H5F_ACC_RDONLY, H5P_DEFAULT); } if (file_id < 0) { FTI_Print("Could not open FTI checkpoint file.", FTI_EROR); return FTI_NREC; } FTI_Exec->H5groups[0]->h5groupID = file_id; FTIT_H5Group* rootGroup = FTI_Exec->H5groups[0]; int i; for (i = 0; i < FTI_Exec->H5groups[0]->childrenNo; i++) { FTI_OpenGroup(FTI_Exec->H5groups[rootGroup->childrenID[i]], file_id, FTI_Exec->H5groups); } for (i = 0; i < FTI_Exec->nbVar; i++) { FTI_CreateComplexType(FTI_Data[i].type, FTI_Exec->FTI_Type); } if( FTI_Exec->h5SingleFile ) { FTI_OpenGlobalDatasets( FTI_Exec ); } for (i = 0; i < FTI_Exec->nbVar; i++) { herr_t res; if( FTI_Exec->h5SingleFile ) { res = FTI_ReadSharedFileData( FTI_Data[i] ); } else { res = FTI_ReadHDF5Var(&FTI_Data[i]); } if (res < 0) { FTI_Print("Could not read FTI checkpoint file.", FTI_EROR); int j; for (j = 0; j < FTI_Exec->H5groups[0]->childrenNo; j++) { FTI_CloseGroup(FTI_Exec->H5groups[rootGroup->childrenID[j]], FTI_Exec->H5groups); } H5Fclose(file_id); return FTI_NREC; } } for (i = 0; i < FTI_Exec->nbVar; i++) { FTI_CloseComplexType(FTI_Data[i].type, FTI_Exec->FTI_Type); } int j; for (j = 0; j < FTI_Exec->H5groups[0]->childrenNo; j++) { FTI_CloseGroup(FTI_Exec->H5groups[rootGroup->childrenID[j]], FTI_Exec->H5groups); } if( FTI_Exec->h5SingleFile ) { FTI_CloseGlobalDatasets( FTI_Exec ); } FTI_Exec->H5groups[0]->h5groupID = -1; if (H5Fclose(file_id) < 0) { FTI_Print("Could not close FTI checkpoint file.", FTI_EROR); return FTI_NREC; } FTI_Exec->reco = 0; return FTI_SCES; }
/*+++++++++++++++++++++++++ .IDENTifer SDMF_get_OrbitalDark_30 .PURPOSE obtain dark correction parameters (SDMF v3.0) .INPUT/OUTPUT call as found = SDMF_get_OrbitalDark_30( orbit, orbitPhase analogOffs, darkCurrent, analogOffsError, darkCurrentError ); input: unsigned short absOrbit : orbit number float orbitPhase : orbit phase in/output: float *analogOffs : analog offset (BU) float *darkCurrent : leakage current (BU/s) float *analogOffsError : analog offset error (BU) float *darkCurrentError : leakage current error (BU/s) .RETURNS solution found (True or False) error status passed by global variable ``nadc_stat'' .COMMENTS none -------------------------*/ bool SDMF_get_OrbitalDark_30( unsigned short absOrbit, float orbitPhase, float *analogOffs, float *darkCurrent, float *analogOffsError, float *darkCurrentError ) { register unsigned short np; hid_t fid = -1; hid_t gid = -1; bool found = FALSE; int numIndx, metaIndx; float orbvar, orbsig; float amp1[CHANNEL_SIZE], sig_amp1[CHANNEL_SIZE]; char str_msg[MAX_STRING_LENGTH]; char sdmf_db[MAX_STRING_LENGTH]; struct mtbl_simudark_rec *mtbl = NULL; const int orbit = (int) absOrbit; const int pixelRange[] = {0, CHANNEL_SIZE-1}; const long sz_chan_byte = CHANNEL_SIZE * ENVI_FLOAT; const char msg_found[] = "\n\tapplied SDMF SimuDark (v3.0) of orbit: %-d"; /* * initialize returned values */ (void) memset( analogOffs, 0, sz_chan_byte ); (void) memset( darkCurrent, 0, sz_chan_byte ); (void) memset( analogOffsError, 0, sz_chan_byte ); (void) memset( darkCurrentError, 0, sz_chan_byte ); /* * open SDMF simu-dark database */ (void) snprintf( sdmf_db, MAX_STRING_LENGTH, "%s/%s", SDMF_PATH("3.0"), "sdmf_simudark.h5" ); H5E_BEGIN_TRY { fid = H5Fopen( sdmf_db, H5F_ACC_RDONLY, H5P_DEFAULT ); } H5E_END_TRY; if ( fid < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_FILE, sdmf_db ); if ( (gid = H5Gopen( fid, "ch8", H5P_DEFAULT )) < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_GRP, "/ch8" ); /* * obtain indices relevant entries */ numIndx = 1; metaIndx = -1; (void) SDMF_get_metaIndex( gid, orbit, &numIndx, &metaIndx ); if ( numIndx == 0 ) goto done; found = TRUE; /* * read simu-dark data */ SDMF_rd_simudarkTable( gid, &numIndx, &metaIndx, &mtbl ); SDMF_rd_float_Array( gid, "ao", 1, &metaIndx, pixelRange, analogOffs ); SDMF_rd_float_Array( gid, "lc", 1, &metaIndx, pixelRange, darkCurrent ); SDMF_rd_float_Array( gid, "sig_ao", 1, &metaIndx, pixelRange, analogOffsError ); SDMF_rd_float_Array( gid, "sig_lc", 1, &metaIndx, pixelRange, darkCurrentError ); SDMF_rd_float_Array( gid, "amp1", 1, &metaIndx, pixelRange, amp1 ); SDMF_rd_float_Array( gid, "sig_amp1", 1, &metaIndx, pixelRange, sig_amp1 ); /* * calculate orbital dark for requested orbit-phase */ orbvar = (float) (cos(2 * PI * (mtbl->orbitPhase + orbitPhase)) + mtbl->amp2 * cos(4 * PI * (mtbl->phase2 + orbitPhase))); orbsig = (float) (cos(2 * PI * (mtbl->orbitPhase + orbitPhase)) + mtbl->sig_amp2 * cos(4 * PI * (mtbl->phase2 + orbitPhase))); for ( np = 0; np < CHANNEL_SIZE; np++ ) { darkCurrent[np] += orbvar * amp1[np]; darkCurrentError[np] += orbsig * sig_amp1[np]; } (void) snprintf( str_msg, SHORT_STRING_LENGTH, msg_found, mtbl->absOrbit ); NADC_ERROR( NADC_ERR_NONE, str_msg ); done: if ( mtbl != NULL ) free( mtbl ); if ( gid > 0 ) H5Gclose( gid ); if ( fid > 0 ) H5Fclose( fid ); return found; }
void cow_histogram_dumphdf5(cow_histogram *h, char *fn, char *gn) // ----------------------------------------------------------------------------- // Dumps the histogram to the HDF5 file named `fn`, under the group // `gn`/h->nickname, gn may be NULL. The function uses rank 0 to do the write. // ----------------------------------------------------------------------------- { #if (COW_HDF5) if (!(h->committed && h->sealed)) { return; } char gname[1024]; int rank = 0; if (gn) { snprintf(gname, 1024, "%s/%s", gn, h->nickname); } else { snprintf(gname, 1024, "%s", h->nickname); } #if (COW_MPI) if (cow_mpirunning()) { MPI_Comm_rank(h->comm, &rank); } #endif if (rank == 0) { // ------------------------------------------------------------------------- // The write functions assume the file is already created. Have master // create the file if it's not there already. // ------------------------------------------------------------------------- FILE *testf = fopen(fn, "r"); hid_t fid; if (testf == NULL) { fid = H5Fcreate(fn, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); } else { fclose(testf); fid = H5Fopen(fn, H5F_ACC_RDWR, H5P_DEFAULT); } if (H5Lexists_safe(fid, gname)) { printf("[%s] writing histogram as HDF5 to %s/%s (clobber existing)\n", MODULE, fn, gname); H5Gunlink(fid, gname); } else { printf("[%s] writing histogram as HDF5 to %s/%s\n", MODULE, fn, gname); } hid_t gcpl = H5Pcreate(H5P_LINK_CREATE); H5Pset_create_intermediate_group(gcpl, 1); hid_t memb = H5Gcreate(fid, gname, gcpl, H5P_DEFAULT, H5P_DEFAULT); H5Pclose(gcpl); H5Gclose(memb); H5Fclose(fid); } else { return; } // Create a group to represent this histogram, and an attribute to name it // --------------------------------------------------------------------------- hid_t fid = H5Fopen(fn, H5F_ACC_RDWR, H5P_DEFAULT); hid_t grp = H5Gopen(fid, gname, H5P_DEFAULT); if (h->fullname != NULL) { hid_t aspc = H5Screate(H5S_SCALAR); hid_t strn = H5Tcopy(H5T_C_S1); H5Tset_size(strn, strlen(h->fullname)); hid_t attr = H5Acreate(grp, "fullname", strn, aspc, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attr, strn, h->fullname); // write the full name H5Aclose(attr); H5Tclose(strn); H5Sclose(aspc); } // Create the data sets in the group: binloc (bin centers) and binval (values) // --------------------------------------------------------------------------- double *binlocX = h->binlocx; double *binlocY = h->binlocy; double *binvalV = h->binvalv; hsize_t sizeX[1] = { h->nbinsx }; hsize_t sizeY[1] = { h->nbinsy }; hsize_t SizeX[1] = { h->nbinsx + 1 }; // to hold bin edges hsize_t SizeY[1] = { h->nbinsy + 1 }; // below, cap S/F refers to bin edges hsize_t sizeZ[2] = { h->nbinsx, h->nbinsy }; hid_t fspcZ = H5Screate_simple(h->n_dims, sizeZ, NULL); if (h->n_dims >= 1) { hid_t fspcX = H5Screate_simple(1, sizeX, NULL); hid_t FspcX = H5Screate_simple(1, SizeX, NULL); hid_t dsetbinX = H5Dcreate(grp, "binlocX", H5T_NATIVE_DOUBLE, fspcX, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t dsetedgX = H5Dcreate(grp, "binedgX", H5T_NATIVE_DOUBLE, FspcX, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dsetbinX, H5T_NATIVE_DOUBLE, fspcX, fspcX, H5P_DEFAULT, binlocX); H5Dwrite(dsetedgX, H5T_NATIVE_DOUBLE, FspcX, FspcX, H5P_DEFAULT, h->bedgesx); H5Dclose(dsetbinX); H5Sclose(FspcX); H5Sclose(fspcX); } if (h->n_dims >= 2) { hid_t fspcY = H5Screate_simple(1, sizeY, NULL); hid_t FspcY = H5Screate_simple(1, SizeY, NULL); hid_t dsetbinY = H5Dcreate(grp, "binlocY", H5T_NATIVE_DOUBLE, fspcY, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t dsetedgY = H5Dcreate(grp, "binedgY", H5T_NATIVE_DOUBLE, FspcY, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dsetbinY, H5T_NATIVE_DOUBLE, fspcY, fspcY, H5P_DEFAULT, binlocY); H5Dwrite(dsetedgY, H5T_NATIVE_DOUBLE, FspcY, FspcY, H5P_DEFAULT, h->bedgesy); H5Dclose(dsetbinY); H5Sclose(FspcY); H5Sclose(fspcY); } hid_t dsetvalV = H5Dcreate(grp, "binval", H5T_NATIVE_DOUBLE, fspcZ, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dsetvalV, H5T_NATIVE_DOUBLE, fspcZ, fspcZ, H5P_DEFAULT, binvalV); H5Dclose(dsetvalV); H5Sclose(fspcZ); H5Gclose(grp); H5Fclose(fid); #endif }
int main(int argc , char **argv) { BYTE *Image; /* compression structs */ CHAR *HDFName = NULL; CHAR *GIFName = NULL; BYTE* b; BYTE GlobalPalette[256][3]; BYTE Red[256]; BYTE Green[256]; BYTE Blue[256]; int RWidth, RHeight; int ColorMapSize, InitCodeSize, Background, BitsPerPixel; int j,nc; int i; int numcols; BYTE pc2nc[256] , r1[256] , g1[256] , b1[256]; int arg_index = 2; int bool_is_image = 0; /* 0 = false , 1 = true */ char *image_name = NULL; int idx; if ( argv[1] && (strcmp("-V",argv[1])==0) ) { print_version("gif2h5"); exit(EXIT_SUCCESS); } if (argc < 4) { /* they didn't supply at least one image -- bail */ usage(); return 1; } HDFName = argv[1]; GIFName = argv[2]; /* get the options */ while (arg_index++ < argc - 1) { if (!strcmp(argv[arg_index] , "-i")) { bool_is_image = 1; continue; } if (bool_is_image) { /* allocate space to store the image name */ size_t len = strlen(argv[arg_index]); image_name = (CHAR*) malloc( len + 1); strcpy(image_name , argv[arg_index]); bool_is_image = 0; continue; } /* oops. This was not meant to happen */ usage(); goto out; } /* Do Endian Order testing and set Endian Order */ idx = 0x0001; b = (BYTE *) &idx; EndianOrder = (b[0] ? 1:0); if (!(fpGif = fopen(GIFName , "wb"))) { printf("Error opening gif file for output. Aborting.\n"); goto out; } Background = 0; { hsize_t width, height, planes; hid_t fid; char interlace[20]; hssize_t npals; hsize_t pal_dims[2]; unsigned char *pal; if ((fid = H5Fopen(HDFName , H5F_ACC_RDONLY , H5P_DEFAULT)) < 0) { fprintf(stderr , "Unable to open HDF file for input. Aborting.\n"); goto out; } /* read image */ if ( H5IMget_image_info( fid, image_name, &width, &height, &planes, interlace, &npals ) < 0 ) goto out; Image = (BYTE*) malloc( (size_t) width * (size_t) height ); if ( H5IMread_image( fid, image_name, Image ) < 0 ) goto out; if (npals) { if ( H5IMget_palette_info( fid, image_name, 0, pal_dims ) < 0 ) goto out; pal = (BYTE*) malloc( (size_t) pal_dims[0] * (size_t) pal_dims[1] ); if ( H5IMget_palette( fid, image_name, 0, pal ) < 0 ) goto out; numcols = (int) pal_dims[0]; for (i = 0, j = 0 ; i < numcols ; j+=3, i++) { GlobalPalette[i][0] = pal[j]; GlobalPalette[i][1] = pal[j+1]; GlobalPalette[i][2] = pal[j+2]; } free(pal); } H5Fclose(fid); RWidth = (int)width; RHeight = (int)height; /* * If the first image does not have a palette, I make my own global * color table Obviously this is not the best thing to do, better * steps would be: * * 1. Check for either a global palette or a global attribute called * palette * 2. Check for palettes in any of the other images. */ if (!npals) { numcols = 256; for (i = 0 ; i < numcols ; i++) { Red[i] = 255 - i; Green[i] = 255 - i; Blue[i] = 255 - i; } } else { for (i = 0 ; i < numcols ; i++) { Red[i] = GlobalPalette[i][0]; Green[i] = GlobalPalette[i][1]; Blue[i] = GlobalPalette[i][2]; } } for (i = 0; i < numcols; i++) { pc2nc[i] = r1[i] = g1[i] = b1[i] = 0; } /* compute number of unique colors */ nc = 0; for (i = 0; i < numcols; i++) { /* see if color #i is already used */ for (j = 0; j < i; j++) { if (Red[i] == Red[j] && Green[i] == Green[j] && Blue[i] == Blue[j]) break; } if (j==i) { /* wasn't found */ pc2nc[i] = nc; r1[nc] = Red[i]; g1[nc] = Green[i]; b1[nc] = Blue[i]; nc++; } else { pc2nc[i] = pc2nc[j]; } } /* figure out 'BitsPerPixel' */ for (i = 1; i < 8; i++) { if ((1<<i) >= nc) break; } BitsPerPixel = i; ColorMapSize = 1 << BitsPerPixel; if (BitsPerPixel <= 1) InitCodeSize = 2; else InitCodeSize = BitsPerPixel; if (!fpGif) { fprintf(stderr, "WriteGIF: file not open for writing\n" ); goto out; } fwrite("GIF87a", sizeof( char ), 6, fpGif); /* the GIF magic number */ putword(RWidth, fpGif); /* screen descriptor */ putword(RHeight, fpGif); i = 0x00; /* No, there is no color map */ i |= (8-1)<<4; /* OR in the color resolution (hardwired 8) */ i |= (BitsPerPixel - 1); /* OR in the # of bits per pixel */ fputc(i,fpGif); fputc(Background,fpGif); /* background color */ fputc(0, fpGif); /* future expansion byte */ /* * Put Image Descriptor * Hardwiring Left Offset and Top Offset to 0x00 */ fputc(0x2c , fpGif); putword(0x00 , fpGif); putword(0x00 , fpGif); putword(RWidth , fpGif); putword(RHeight , fpGif); /* since we always have a local color palette ... */ fputc((0x80 | (BitsPerPixel - 1)) , fpGif); for (i = 0; i < ColorMapSize; i++) { /* write out Global colormap */ fputc(r1[i], fpGif); fputc(g1[i], fpGif); fputc(b1[i], fpGif); } fputc(InitCodeSize , fpGif); i = hdfWriteGIF(fpGif , Image , 0 , RHeight , RWidth , r1, g1 , b1 , pc2nc , 256 , 8 , BitsPerPixel); fputc(0x00, fpGif); free(Image); } if (fputc(';',fpGif) == EOF) { /* Write GIF file terminator */ fprintf(stderr , "Error!"); goto out; } if (fpGif != NULL) fclose(fpGif); if (image_name != NULL) free(image_name); return 0; out: if (fpGif != NULL) fclose(fpGif); if (image_name != NULL) free(image_name); return 1; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: HDF5 user block unjammer * * Return: Success: 0 * Failure: 1 * * Programmer: * * Modifications: * *------------------------------------------------------------------------- */ int main(int argc, const char *argv[]) { void *edata; H5E_auto2_t func; hid_t ifile = -1; hid_t plist = -1; off_t fsize; hsize_t usize; htri_t testval; herr_t status; int res; h5_stat_t sbuf; h5tools_setprogname(PROGRAMNAME); h5tools_setstatus(EXIT_SUCCESS); /* Disable error reporting */ H5Eget_auto2(H5E_DEFAULT, &func, &edata); H5Eset_auto2(H5E_DEFAULT, NULL, NULL); /* Initialize h5tools lib */ h5tools_init(); if(EXIT_FAILURE == parse_command_line(argc, argv)) goto done; if (input_file == NULL) { /* no user block */ error_msg("missing arguemnt for HDF5 file input.\n"); help_ref_msg(stderr); h5tools_setstatus(EXIT_FAILURE); goto done; } testval = H5Fis_hdf5(input_file); if (testval <= 0) { error_msg("Input HDF5 file \"%s\" is not HDF\n", input_file); help_ref_msg (stderr); h5tools_setstatus(EXIT_FAILURE); goto done; } ifile = H5Fopen(input_file, H5F_ACC_RDONLY , H5P_DEFAULT); if (ifile < 0) { error_msg("Can't open input HDF5 file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } plist = H5Fget_create_plist(ifile); if (plist < 0) { error_msg("Can't get file creation plist for file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } status = H5Pget_userblock(plist, & usize); if (status < 0) { error_msg("Can't get user block for file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } status = H5Pclose(plist); HDassert(status >= 0); status = H5Fclose(ifile); HDassert(status >= 0); if (usize == 0) { /* no user block to remove: message? */ error_msg("\"%s\" has no user block: no change to file\n", input_file); h5tools_setstatus(EXIT_SUCCESS); goto done; } res = HDfstat(HDfileno(rawinstream), &sbuf); if(res < 0) { error_msg("Can't stat file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } fsize = sbuf.st_size; if (do_delete && (ub_file != NULL)) { error_msg("??\"%s\"\n", ub_file); h5tools_setstatus(EXIT_FAILURE); goto done; } if (output_file == NULL) { error_msg("unable to open output HDF5 file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; } /* copy from 0 to 'usize - 1' into ufid */ if (!do_delete) { if(copy_to_file(rawinstream, rawoutstream, 0, (ssize_t) usize) < 0) { error_msg("unable to copy user block to output file \"%s\"\n", ub_file); h5tools_setstatus(EXIT_FAILURE); goto done; } } /* copy from usize to end of file into h5fid, * starting at end of user block if present */ if(copy_to_file(rawinstream, rawdatastream, (ssize_t) usize, (ssize_t)(fsize - (ssize_t)usize)) < 0) { error_msg("unable to copy hdf5 data to output file \"%s\"\n", output_file); h5tools_setstatus(EXIT_FAILURE); goto done; } done: if(input_file) HDfree(input_file); if(output_file) HDfree(output_file); if(ub_file) { HDfree(ub_file); } h5tools_close(); return h5tools_getstatus(); }
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 ); } poDS->CreateProjections( ); poDS->SetMetadata( poDS->papszMetadata ); /* -------------------------------------------------------------------- */ /* Setup/check for pam .aux.xml. */ /* -------------------------------------------------------------------- */ poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Setup overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, ":::VIRTUAL:::" ); return( poDS ); }
/*------------------------------------------------------------------------- * Function: cklinks * * Purpose: Open the file created in the first step and check that the * links look correct. * * Return: Success: 0 * * Failure: -1 * * Programmer: Robb Matzke * Friday, August 14, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int cklinks(hid_t fapl) { hid_t file; H5G_stat_t sb1, sb2; char linkval[LINK_BUF_SIZE]; char filename[NAME_BUF_SIZE]; herr_t status; TESTING("link queries"); /* Open the file */ h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if ((file=H5Fopen(filename, H5F_ACC_RDONLY, fapl))<0) { goto error; } /* Hard link */ if (H5Gget_objinfo(file, "d1", TRUE, &sb1)<0) goto error; if (H5Gget_objinfo(file, "grp1/hard", TRUE, &sb2)<0) goto error; if (H5G_DATASET!=sb2.type) { H5_FAILED(); puts(" Unexpected object type should have been a dataset"); goto error; } if (sb1.objno[0]!=sb2.objno[0] || sb1.objno[1]!=sb2.objno[1]) { H5_FAILED(); puts(" Hard link test failed. Link seems not to point to the "); puts(" expected file location."); goto error; } /* Symbolic link */ if (H5Gget_objinfo(file, "grp1/soft", TRUE, &sb2)<0) goto error; if (H5G_DATASET!=sb2.type) { H5_FAILED(); puts(" Unexpected object type should have been a dataset"); goto error; } if (sb1.objno[0]!=sb2.objno[0] || sb1.objno[1]!=sb2.objno[1]) { H5_FAILED(); puts(" Soft link test failed. Link seems not to point to the "); puts(" expected file location."); goto error; } if (H5Gget_linkval(file, "grp1/soft", sizeof linkval, linkval)<0) { goto error; } if (HDstrcmp(linkval, "/d1")) { H5_FAILED(); puts(" Soft link test failed. Wrong link value"); goto error; } /* Dangling link */ H5E_BEGIN_TRY { status = H5Gget_objinfo(file, "grp1/dangle", TRUE, &sb2); } H5E_END_TRY; if (status>=0) { H5_FAILED(); puts(" H5Gget_objinfo() should have failed for a dangling link."); goto error; } if (H5Gget_objinfo(file, "grp1/dangle", FALSE, &sb2)<0) goto error; if (H5G_LINK!=sb2.type) { H5_FAILED(); puts(" Unexpected object type should have been a symbolic link"); goto error; } if (H5Gget_linkval(file, "grp1/dangle", sizeof linkval, linkval)<0) { goto error; } if (HDstrcmp(linkval, "foobar")) { H5_FAILED(); puts(" Dangling link test failed. Wrong link value"); goto error; } /* Recursive link */ H5E_BEGIN_TRY { status = H5Gget_objinfo(file, "grp1/recursive", TRUE, &sb2); } H5E_END_TRY; if (status>=0) { H5_FAILED(); puts(" H5Gget_objinfo() should have failed for a recursive link."); goto error; } if (H5Gget_objinfo(file, "grp1/recursive", FALSE, &sb2)<0) goto error; if (H5G_LINK!=sb2.type) { H5_FAILED(); puts(" Unexpected object type should have been a symbolic link"); goto error; } if (H5Gget_linkval(file, "grp1/recursive", sizeof linkval, linkval)<0) { goto error; } if (HDstrcmp(linkval, "/grp1/recursive")) { H5_FAILED(); puts(" Recursive link test failed. Wrong link value"); goto error; } /* Cleanup */ if (H5Fclose(file)<0) goto error; PASSED(); return 0; error: return -1; }
int main (void) { hid_t file, filetype, memtype, space, dset, attr; /* Handles */ herr_t status; hvl_t wdata[2], /* Array of vlen structures */ *rdata; /* Pointer to vlen structures */ hsize_t dims[1] = {2}; int *ptr, ndims, i, j; /* * Initialize variable-length data. wdata[0] is a countdown of * length LEN0, wdata[1] is a Fibonacci sequence of length LEN1. */ wdata[0].len = LEN0; ptr = (int *) malloc (wdata[0].len * sizeof (int)); for (i=0; i<wdata[0].len; i++) ptr[i] = wdata[0].len - i; /* 3 2 1 */ wdata[0].p = (void *) ptr; wdata[1].len = LEN1; ptr = (int *) malloc (wdata[1].len * sizeof (int)); ptr[0] = 1; ptr[1] = 1; for (i=2; i<wdata[1].len; i++) ptr[i] = ptr[i-1] + ptr[i-2]; /* 1 1 2 3 5 8 etc. */ wdata[1].p = (void *) ptr; /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create variable-length datatype for file and memory. */ filetype = H5Tvlen_create (H5T_STD_I32LE); memtype = H5Tvlen_create (H5T_NATIVE_INT); /* * Create dataset with a scalar dataspace. */ space = H5Screate (H5S_SCALAR); dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); status = H5Sclose (space); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the attribute and write the variable-length data to it */ attr = H5Acreate (dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); status = H5Awrite (attr, memtype, wdata); /* * Close and release resources. Note the use of H5Dvlen_reclaim * removes the need to manually free() the previously malloc'ed * data. */ status = H5Dvlen_reclaim (memtype, space, H5P_DEFAULT, wdata); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (filetype); status = H5Tclose (memtype); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the attribute has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file, dataset, and attribute. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET); attr = H5Aopen_name (dset, ATTRIBUTE); /* * Get dataspace and allocate memory for array of vlen structures. * This does not actually allocate memory for the vlen data, that * will be done by the library. */ space = H5Aget_space (attr); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (hvl_t *) malloc (dims[0] * sizeof (hvl_t)); /* * Create the memory datatype. */ memtype = H5Tvlen_create (H5T_NATIVE_INT); /* * Read the data. */ status = H5Aread (attr, memtype, rdata); /* * Output the variable-length data to the screen. */ for (i=0; i<dims[0]; i++) { printf ("%s[%u]:\n {",ATTRIBUTE,i); ptr = rdata[i].p; for (j=0; j<rdata[i].len; j++) { printf (" %d", ptr[j]); if ( (j+1) < rdata[i].len ) printf (","); } printf (" }\n"); } /* * Close and release resources. Note we must still free the * top-level pointer "rdata", as H5Dvlen_reclaim only frees the * actual variable-length data, and not the structures themselves. */ status = H5Dvlen_reclaim (memtype, space, H5P_DEFAULT, rdata); free (rdata); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (memtype); status = H5Fclose (file); return 0; }
/*------------------------------------------------------------------------- * Function: ck_new_links * * Purpose: Open the file created in the first step and check that the * links look correct. * * Return: Success: 0 * * Failure: -1 * * Programmer: Raymond Lu * Thursday, April 25, 2002 * * Modifications: * *------------------------------------------------------------------------- */ static int ck_new_links(hid_t fapl) { hid_t file; H5G_stat_t sb_dset, sb_hard1, sb_hard2, sb_soft1, sb_soft2; char filename[NAME_BUF_SIZE]; char linkval[LINK_BUF_SIZE]; TESTING("new link queries"); /* Open the file */ h5_fixname(FILENAME[1], fapl, filename, sizeof filename); if ((file=H5Fopen(filename, H5F_ACC_RDONLY, fapl))<0) { goto error; } /* Get hard link info */ if(H5Gget_objinfo(file, "/grp1/dataset2", TRUE, &sb_dset)<0) goto error; if(H5Gget_objinfo(file, "/grp1/hard1", TRUE, &sb_hard1)<0) goto error; if(H5Gget_objinfo(file, "/grp2/hard2", TRUE, &sb_hard2)<0) goto error; /* Check hard links */ if(H5G_DATASET!=sb_hard1.type || H5G_DATASET!=sb_hard2.type) { H5_FAILED(); puts(" Unexpected object type, should have been a dataset"); goto error; } if( sb_dset.objno[0]!=sb_hard1.objno[0] || sb_dset.objno[1]!=sb_hard1.objno[1] || sb_dset.objno[0]!=sb_hard2.objno[0] || sb_dset.objno[1]!=sb_hard2.objno[1] ) { H5_FAILED(); puts(" Hard link test failed. Link seems not to point to the "); puts(" expected file location."); goto error; } /* Get soft link info */ if(H5Gget_objinfo(file, "/grp1/soft1", TRUE, &sb_soft1)<0) goto error; if(H5Gget_objinfo(file, "/grp2/soft2", TRUE, &sb_soft2)<0) goto error; /* Check soft links */ if(H5G_DATASET!=sb_soft1.type || H5G_DATASET!=sb_soft2.type) { H5_FAILED(); puts(" Unexpected object type, should have been a dataset"); goto error; } if( sb_dset.objno[0]!=sb_soft1.objno[0] || sb_dset.objno[1]!=sb_soft1.objno[1] || sb_dset.objno[0]!=sb_soft2.objno[0] || sb_dset.objno[1]!=sb_soft2.objno[1] ) { H5_FAILED(); puts(" Soft link test failed. Link seems not to point to the "); puts(" expected file location."); goto error; } if (H5Gget_linkval(file, "grp2/soft2", sizeof linkval, linkval)<0) { goto error; } if (HDstrcmp(linkval, "/grp1/dataset2")) { H5_FAILED(); puts(" Soft link test failed. Wrong link value"); goto error; } /* Cleanup */ if(H5Fclose(file)<0) goto error; PASSED(); return 0; error: return -1; }
void _io_write(cow_dfield *f, char *fname) // ----------------------------------------------------------------------------- // This function uses a collective MPI-IO procedure to write the contents of // 'data' to the HDF5 file named 'fname', which is assumed to have been created // already. The dataset with name 'dname', which is being written to, must not // exist already. Chunking is enabled as per the ChunkSize variable, and is // disabled by default. Recommended chunk size is local subdomain size. This // will result in optimized read/write on the same decomposition layout, but // poor performance for different access patterns, for example the slabs used by // cluster-FFT functions. // // WARNING! // // All processors must define the same chunk size, the behavior of this function // is not defined otherwise. This implies that chunking should be disabled when // running on a strange number of cores, and subdomain sizes are non-uniform. // ----------------------------------------------------------------------------- { #if (COW_HDF5) cow_domain *d = f->domain; char **pnames = f->members; void *data = f->data; char *gname = f->name; int n_memb = f->n_members; int n_dims = d->n_dims; hsize_t *L_nint = d->L_nint_h5; hsize_t *G_strt = d->G_strt_h5; hsize_t *G_ntot = d->G_ntot_h5; hsize_t ndp1 = n_dims + 1; hsize_t l_nint[4]; hsize_t l_ntot[4]; hsize_t l_strt[4]; hsize_t stride[4]; for (int i=0; i<n_dims; ++i) { l_nint[i] = d->L_nint[i]; // Selection size, target and destination l_ntot[i] = d->L_ntot[i]; // Memory space total size l_strt[i] = d->L_strt[i]; // Memory space selection start stride[i] = 1; } l_nint[ndp1 - 1] = 1; l_ntot[ndp1 - 1] = n_memb; stride[ndp1 - 1] = n_memb; // The loop over processors is needed if COW_MPI support is enabled and // COW_HDF5_MPI is not. If either COW_MPI is disabled, or COW_HDF5_MPI is // enabled, then the write calls occur without the loop. // --------------------------------------------------------------------------- #if (!COW_HDF5_MPI && COW_MPI) for (int rank=0; rank<d->cart_size; ++rank) { if (rank == d->cart_rank) { #endif hid_t file = H5Fopen(fname, H5F_ACC_RDWR, d->fapl); hid_t memb = H5Gopen(file, gname, H5P_DEFAULT); hid_t mspc = H5Screate_simple(ndp1, l_ntot, NULL); hid_t fspc = H5Screate_simple(n_dims, G_ntot, NULL); for (int n=0; n<n_memb; ++n) { int dset = H5Dcreate(memb, pnames[n], H5T_NATIVE_DOUBLE, fspc, H5P_DEFAULT, d->dcpl, H5P_DEFAULT); l_strt[ndp1 - 1] = n; H5Sselect_hyperslab(mspc, H5S_SELECT_SET, l_strt, stride, l_nint, NULL); H5Sselect_hyperslab(fspc, H5S_SELECT_SET, G_strt, NULL, L_nint, NULL); H5Dwrite(dset, H5T_NATIVE_DOUBLE, mspc, fspc, d->dxpl, data); H5Dclose(dset); } H5Sclose(fspc); H5Sclose(mspc); H5Gclose(memb); H5Fclose(file); #if (!COW_HDF5_MPI && COW_MPI) } if (cow_mpirunning()) { MPI_Barrier(d->mpi_cart); } } #endif // !COW_HDF5_MPI && COW_MPI #endif }
//------------------------------------------------------------------------------ vtkPolyData *vtkAMRSimPlaParticlesReader::GetParticles( const char *file, const int blockIdx) { vtkPolyData *particles = vtkPolyData::New(); vtkPoints *positions = vtkPoints::New(); positions->SetDataTypeToDouble(); vtkPointData *pdata = particles->GetPointData(); hid_t fileIndx = H5Fopen(file, H5F_ACC_RDONLY, H5P_DEFAULT); if (fileIndx < 0) { vtkErrorMacro("Failed opening particles file!"); return NULL; } hid_t rootIndx; if (!FindBlockIndex(fileIndx, blockIdx + 1, rootIndx)) { vtkErrorMacro("Could not locate target block!"); return NULL; } // // Load the particles position arrays by name. // In SimPla the following arrays are available: // ( 1 ) particle_position_i // ( 2 ) tracer_particle_position_i // // where i \in {x,y,z}. std::vector<double> xcoords; std::vector<double> ycoords; std::vector<double> zcoords; // TODO: should we handle 2-D particle datasets? GetDoubleArrayByName(rootIndx, "particle_position_x", xcoords); GetDoubleArrayByName(rootIndx, "particle_position_y", ycoords); GetDoubleArrayByName(rootIndx, "particle_position_z", zcoords); vtkIntArray *particleTypes = vtkIntArray::SafeDownCast( this->GetParticlesTypeArray(blockIdx)); assert("Coordinate arrays must have the same size: " && (xcoords.size() == ycoords.size())); assert("Coordinate arrays must have the same size: " && (ycoords.size() == zcoords.size())); int TotalNumberOfParticles = static_cast<int>(xcoords.size()); positions->SetNumberOfPoints(TotalNumberOfParticles); vtkIdList *ids = vtkIdList::New(); ids->SetNumberOfIds(TotalNumberOfParticles); vtkIdType NumberOfParticlesLoaded = 0; for (int i = 0; i < TotalNumberOfParticles; ++i) { if ((i % this->Frequency) == 0) { if (this->CheckLocation(xcoords[i], ycoords[i], zcoords[i]) && this->CheckParticleType(i, particleTypes)) { int pidx = NumberOfParticlesLoaded; ids->InsertId(pidx, i); positions->SetPoint(pidx, xcoords[i], ycoords[i], zcoords[i]); ++NumberOfParticlesLoaded; } // END if within requested region } // END if within requested interval } // END for all particles H5Gclose(rootIndx); H5Fclose(fileIndx); ids->SetNumberOfIds(NumberOfParticlesLoaded); ids->Squeeze(); positions->SetNumberOfPoints(NumberOfParticlesLoaded); positions->Squeeze(); particles->SetPoints(positions); positions->Delete(); // Create CellArray consisting of a single polyvertex cell vtkCellArray *polyVertex = vtkCellArray::New(); polyVertex->InsertNextCell(NumberOfParticlesLoaded); for (vtkIdType idx = 0; idx < NumberOfParticlesLoaded; ++idx) polyVertex->InsertCellPoint(idx); particles->SetVerts(polyVertex); polyVertex->Delete(); // Release the particle types array particleTypes->Delete(); int numArrays = this->ParticleDataArraySelection->GetNumberOfArrays(); for (int i = 0; i < numArrays; ++i) { const char *name = this->ParticleDataArraySelection->GetArrayName(i); if (this->ParticleDataArraySelection->ArrayIsEnabled(name)) { // Note: 0-based indexing is used for loading particles this->m_pimpl_->LoadAttribute(name, blockIdx); assert("pre: particle attribute size mismatch" && (this->m_pimpl_->DataArray->GetNumberOfTuples() == TotalNumberOfParticles)); vtkDataArray *array =this->m_pimpl_->DataArray->NewInstance(); array->SetName(this->m_pimpl_->DataArray->GetName()); array->SetNumberOfTuples(NumberOfParticlesLoaded); array->SetNumberOfComponents( this->m_pimpl_->DataArray->GetNumberOfComponents()); vtkIdType numIds = ids->GetNumberOfIds(); for (vtkIdType pidx = 0; pidx < numIds; ++pidx) { vtkIdType particleIdx = ids->GetId(pidx); int numComponents = array->GetNumberOfComponents(); for (int k = 0; k < numComponents; ++k) { array->SetComponent(pidx, k, this->m_pimpl_->DataArray->GetComponent( particleIdx, k)); } // END for all components } // END for all ids of loaded particles pdata->AddArray(array); array->Delete(); } // END if the array is supposed to be loaded } // END for all particle arrays ids->Delete(); return (particles); }
void _io_read(cow_dfield *f, char *fname) { #if (COW_HDF5) cow_domain *d = f->domain; char **pnames = f->members; void *data = f->data; char *gname = f->name; int n_memb = f->n_members; int n_dims = d->n_dims; hsize_t *L_nint = d->L_nint_h5; hsize_t *G_strt = d->G_strt_h5; hsize_t *G_ntot = d->G_ntot_h5; hsize_t ndp1 = n_dims + 1; hsize_t l_nint[4]; hsize_t l_ntot[4]; hsize_t l_strt[4]; hsize_t stride[4]; for (int i=0; i<n_dims; ++i) { l_nint[i] = d->L_nint[i]; // Selection size, target and destination l_ntot[i] = d->L_ntot[i]; // Memory space total size l_strt[i] = d->L_strt[i]; // Memory space selection start stride[i] = 1; } l_nint[ndp1 - 1] = 1; l_ntot[ndp1 - 1] = n_memb; stride[ndp1 - 1] = n_memb; // The loop over processors is needed if COW_MPI support is enabled and // COW_HDF5_MPI is not. If either COW_MPI is disabled, or COW_HDF5_MPI is // enabled, then the write calls occur without the loop. // --------------------------------------------------------------------------- #if (!COW_HDF5_MPI && COW_MPI) for (int rank=0; rank<d->cart_size; ++rank) { if (rank == d->cart_rank) { #endif hid_t file = H5Fopen(fname, H5F_ACC_RDONLY, d->fapl); hid_t memb = H5Gopen(file, gname, H5P_DEFAULT); hid_t mspc = H5Screate_simple(ndp1, l_ntot, NULL); hid_t fspc = H5Screate_simple(n_dims, G_ntot, NULL); for (int n=0; n<n_memb; ++n) { hid_t dset = H5Dopen(memb, pnames[n], H5P_DEFAULT); l_strt[ndp1 - 1] = n; H5Sselect_hyperslab(mspc, H5S_SELECT_SET, l_strt, stride, l_nint, NULL); H5Sselect_hyperslab(fspc, H5S_SELECT_SET, G_strt, NULL, L_nint, NULL); H5Dread(dset, H5T_NATIVE_DOUBLE, mspc, fspc, d->dxpl, data); H5Dclose(dset); } H5Sclose(fspc); H5Sclose(mspc); H5Gclose(memb); H5Fclose(file); #if (!COW_HDF5_MPI && COW_MPI) } if (cow_mpirunning()) { MPI_Barrier(d->mpi_cart); } } #endif // !COW_HDF5_MPI && COW_MPI #endif }
/*------------------------------------------------------------------------- * Function: test_family * * Purpose: Tests the file handle interface for FAMILY driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * * Modifications: * * Raymond Lu * Wednesday, June 23, 2004 * Added test for H5Fget_filesize. * * Raymond Lu * June 2, 2005 * Added a function test_family_opens() to test different * wrong way to reopen family files. * *------------------------------------------------------------------------- */ static herr_t test_family(void) { hid_t file=(-1), fapl, fapl2=(-1), space=(-1), dset=(-1); hid_t access_fapl = -1; char filename[1024]; char dname[]="dataset"; unsigned int i, j; int *fhandle=NULL, *fhandle2=NULL; int buf[FAMILY_NUMBER][FAMILY_SIZE]; hsize_t dims[2]={FAMILY_NUMBER, FAMILY_SIZE}; hsize_t file_size; TESTING("FAMILY file driver"); /* Set property list and file name for FAMILY driver */ fapl = h5_fileaccess(); if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE, H5P_DEFAULT) < 0) TEST_ERROR; h5_fixname(FILENAME[2], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test different wrong ways to reopen family files where there's only * one member file existing. */ if(test_family_opens(filename, fapl) < 0) TEST_ERROR; /* Reopen the file with default member file size */ if(H5Pset_fapl_family(fapl, (hsize_t)H5F_FAMILY_DEFAULT, H5P_DEFAULT) < 0) TEST_ERROR; if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* The file size is supposed to be about 800 bytes right now. */ if(file_size < (KB / 2) || file_size > KB) TEST_ERROR; /* Create and write dataset */ if((space=H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; for(i=0; i<FAMILY_NUMBER; i++) for(j=0; j<FAMILY_SIZE; j++) buf[i][j] = i*10000+j; if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; /* check file handle API */ if((fapl2=H5Pcreate(H5P_FILE_ACCESS)) < 0) TEST_ERROR; if(H5Pset_family_offset(fapl2, (hsize_t)0) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle<0) TEST_ERROR; if(H5Pset_family_offset(fapl2, (hsize_t)(FAMILY_SIZE*2)) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0) TEST_ERROR; if(*fhandle2<0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* Some data has been written. The file size should be bigger(18KB+976 * bytes if int size is 4 bytes) now. */ if(sizeof(int)<=4) { if(file_size<18*KB || file_size>20*KB) TEST_ERROR; } else if(sizeof(int)>=8) { if(file_size<32*KB || file_size>40*KB) TEST_ERROR; } if(H5Sclose(space) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Pclose(fapl2) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test different wrong ways to reopen family files when there're multiple * member files existing. */ if(test_family_opens(filename, fapl) < 0) TEST_ERROR; /* Reopen the file with correct member file size. */ if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE, H5P_DEFAULT) < 0) TEST_ERROR; if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Sclose(space); H5Dclose(dset); H5Pclose (fapl2); H5Fclose(file); } H5E_END_TRY; return -1; }
static int read_data(char *fname) { char pathname[1024]; char *srcdir = getenv("srcdir"); /*where the src code is located*/ hid_t file, dataset; /* handles */ hid_t datatype; hid_t dt; double data_in[NX][NY]; /* input buffer */ double data_out[NX][NY]; /* output buffer */ int i, j; unsigned nerrors = 0; pathname[0] = '\0'; /* Generate correct name for test file by prepending the source path */ if(srcdir && ((strlen(srcdir) + strlen(fname) + 1) < sizeof(pathname))) { strcpy(pathname, srcdir); strcat(pathname, "/"); } strcat(pathname, fname); /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) { data_in[j][i] = i + j; data_out[j][i] = 0; } } /* * 0 1 2 3 4 5 * 1 2 3 4 5 6 * 2 3 4 5 6 7 * 3 4 5 6 7 8 * 4 5 6 7 8 9 */ /* * Open the file and the dataset. */ if((file = H5Fopen(pathname, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) TEST_ERROR; if((dataset = H5Dopen2(file, DATASETNAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* * Get datatype and dataspace handles and then query * dataset class, order, size, rank and dimensions. */ if((dt = H5Dget_type(dataset)) < 0) /* datatype handle */ TEST_ERROR; if((datatype = H5Tget_native_type(dt, H5T_DIR_DEFAULT)) < 0) TEST_ERROR; /* * Read data from hyperslab in the file into the hyperslab in * memory and display. */ if(H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data_out) < 0) TEST_ERROR; /* Check results */ for (j=0; j<NX; j++) { for (i=0; i<NY; i++) { if (data_out[j][i] != data_in[j][i]) { if (!nerrors++) { H5_FAILED(); printf("element [%d][%d] is %g but should have been %g\n", j, i, data_out[j][i], data_in[j][i]); } } } } /* * Close/release resources. */ H5Tclose(dt); H5Tclose(datatype); H5Dclose(dataset); H5Fclose(file); /* Failure */ if (nerrors) { printf("total of %d errors out of %d elements\n", nerrors, NX*NY); return 1; } PASSED(); return 0; error: H5E_BEGIN_TRY { H5Fclose(file); } H5E_END_TRY; return 1; }
/*------------------------------------------------------------------------- * Function: test_multi * * Purpose: Tests the file handle interface for MUTLI driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * * Modifications: * * Raymond Lu * Wednesday, June 23, 2004 * Added test for H5Fget_filesize. * *------------------------------------------------------------------------- */ static herr_t test_multi(void) { hid_t file=(-1), fapl, fapl2=(-1), dset=(-1), space=(-1); hid_t root, attr, aspace, atype; hid_t access_fapl = -1; char filename[1024]; int *fhandle2=NULL, *fhandle=NULL; hsize_t file_size; H5FD_mem_t mt, memb_map[H5FD_MEM_NTYPES]; hid_t memb_fapl[H5FD_MEM_NTYPES]; haddr_t memb_addr[H5FD_MEM_NTYPES]; const char *memb_name[H5FD_MEM_NTYPES]; char sv[H5FD_MEM_NTYPES][32]; hsize_t dims[2]={MULTI_SIZE, MULTI_SIZE}; hsize_t adims[1]={1}; char dname[]="dataset"; char meta[] = "this is some metadata on this file"; int i, j; int buf[MULTI_SIZE][MULTI_SIZE]; TESTING("MULTI file driver"); /* Set file access property list for MULTI driver */ fapl = h5_fileaccess(); HDmemset(memb_map, 0, sizeof memb_map); HDmemset(memb_fapl, 0, sizeof memb_fapl); HDmemset(memb_name, 0, sizeof memb_name); HDmemset(memb_addr, 0, sizeof memb_addr); HDmemset(sv, 0, sizeof sv); for(mt=H5FD_MEM_DEFAULT; mt<H5FD_MEM_NTYPES; H5_INC_ENUM(H5FD_mem_t,mt)) { memb_fapl[mt] = H5P_DEFAULT; memb_map[mt] = H5FD_MEM_SUPER; } memb_map[H5FD_MEM_DRAW] = H5FD_MEM_DRAW; memb_map[H5FD_MEM_BTREE] = H5FD_MEM_BTREE; memb_map[H5FD_MEM_GHEAP] = H5FD_MEM_GHEAP; sprintf(sv[H5FD_MEM_SUPER], "%%s-%c.h5", 's'); memb_name[H5FD_MEM_SUPER] = sv[H5FD_MEM_SUPER]; memb_addr[H5FD_MEM_SUPER] = 0; sprintf(sv[H5FD_MEM_BTREE], "%%s-%c.h5", 'b'); memb_name[H5FD_MEM_BTREE] = sv[H5FD_MEM_BTREE]; memb_addr[H5FD_MEM_BTREE] = HADDR_MAX/4; sprintf(sv[H5FD_MEM_DRAW], "%%s-%c.h5", 'r'); memb_name[H5FD_MEM_DRAW] = sv[H5FD_MEM_DRAW]; memb_addr[H5FD_MEM_DRAW] = HADDR_MAX/2; sprintf(sv[H5FD_MEM_GHEAP], "%%s-%c.h5", 'g'); memb_name[H5FD_MEM_GHEAP] = sv[H5FD_MEM_GHEAP]; memb_addr[H5FD_MEM_GHEAP] = HADDR_MAX*3/4; if(H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name, memb_addr, TRUE) < 0) TEST_ERROR; h5_fixname(FILENAME[4], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test wrong ways to reopen multi files */ if(test_multi_opens(filename) < 0) TEST_ERROR; /* Reopen the file */ if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create and write data set */ if((space=H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* Before any data is written, the raw data file is empty. So * the file size is only the size of b-tree + HADDR_MAX/4. */ if(file_size < HADDR_MAX/4 || file_size > HADDR_MAX/2) TEST_ERROR; if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; for(i=0; i<MULTI_SIZE; i++) for(j=0; j<MULTI_SIZE; j++) buf[i][j] = i*10000+j; if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; if((fapl2=H5Pcreate(H5P_FILE_ACCESS)) < 0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_SUPER) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle<0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_DRAW) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0) TEST_ERROR; if(*fhandle2<0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* After the data is written, the file size is huge because the * beginning of raw data file is set at HADDR_MAX/2. It's supposed * to be (HADDR_MAX/2 + 128*128*4) */ if(file_size < HADDR_MAX/2 || file_size > HADDR_MAX) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Pclose(fapl2) < 0) TEST_ERROR; /* Create and write attribute for the root group. */ if((root = H5Gopen2(file, "/", H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Attribute string. */ if((atype = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR; if(H5Tset_size(atype, strlen(meta) + 1) < 0) TEST_ERROR; if(H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) TEST_ERROR; /* Create and write attribute */ if((aspace = H5Screate_simple(1, adims, NULL)) < 0) TEST_ERROR; if((attr = H5Acreate2(root, "Metadata", atype, aspace, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Awrite(attr, atype, meta) < 0) TEST_ERROR; /* Close IDs */ if(H5Tclose(atype) < 0) TEST_ERROR; if(H5Sclose(aspace) < 0) TEST_ERROR; if(H5Aclose(attr) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Sclose(space); H5Dclose(dset); H5Pclose(fapl); H5Pclose(fapl2); H5Fclose(file); } H5E_END_TRY; return -1; }
int main (void) { hid_t file; hid_t grp; hid_t dataset, dataspace; hid_t plist; herr_t status; hsize_t dims[2]; hsize_t cdims[2]; int idx_f, idx_g; /* * Create a file. */ file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create a group in the file. */ grp = H5Gcreate(file, "/Data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset "Compressed Data" in the group using absolute * name. Dataset creation property list is modified to use * GZIP compression with the compression effort set to 6. * Note that compression can be used only when dataset is chunked. */ dims[0] = 1000; dims[1] = 20; cdims[0] = 20; cdims[1] = 20; dataspace = H5Screate_simple(RANK, dims, NULL); plist = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(plist, 2, cdims); H5Pset_deflate( plist, 6); dataset = H5Dcreate(file, "/Data/Compressed_Data", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, plist, H5P_DEFAULT); /* * Close the first dataset . */ H5Sclose(dataspace); H5Dclose(dataset); /* * Create the second dataset. */ dims[0] = 500; dims[1] = 20; dataspace = H5Screate_simple(RANK, dims, NULL); dataset = H5Dcreate(file, "/Data/Float_Data", H5T_NATIVE_FLOAT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* *Close the second dataset and file. */ H5Sclose(dataspace); H5Dclose(dataset); H5Pclose(plist); H5Gclose(grp); H5Fclose(file); /* * Now reopen the file and group in the file. */ file = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT); grp = H5Gopen(file, "Data", H5P_DEFAULT); /* * Access "Compressed_Data" dataset in the group. */ dataset = H5Dopen(grp, "Compressed_Data", H5P_DEFAULT); if( dataset < 0) printf(" Dataset 'Compressed-Data' is not found. \n"); printf("\"/Data/Compressed_Data\" dataset is open \n"); /* * Close the dataset. */ status = H5Dclose(dataset); /* * Create hard link to the Data group. */ status = H5Lcreate_hard(file, "Data", H5L_SAME_LOC, "Data_new", H5P_DEFAULT, H5P_DEFAULT); /* * We can access "Compressed_Data" dataset using created * hard link "Data_new". */ dataset = H5Dopen(file, "/Data_new/Compressed_Data", H5P_DEFAULT); if( dataset < 0) printf(" Dataset is not found. \n"); printf("\"/Data_new/Compressed_Data\" dataset is open \n"); /* * Close the dataset. */ status = H5Dclose(dataset); /* * Use iterator to see the names of the objects in the root group. */ idx_f = H5Literate(file, H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, NULL); /* * Unlink name "Data" and use iterator to see the names * of the objects in the file root direvtory. */ if(H5Ldelete(file, "Data", H5P_DEFAULT) < 0) printf(" H5Ldelete failed \n"); else printf("\"Data\" is unlinked \n"); idx_f = H5Literate(file, H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, NULL); /* * Use iterator to see the names of the objects in the group * /Data_new. */ idx_g = H5Literate_by_name(grp, "/Data_new", H5_INDEX_NAME, H5_ITER_INC, NULL, group_info, NULL, H5P_DEFAULT); /* * Close the file. */ H5Gclose(grp); H5Fclose(file); return 0; }
int main (void) { hid_t file, filetype, memtype, space, dset; /* Handles */ herr_t status; hsize_t dims[2] = {DIM0, DIM1}; phase_t wdata[DIM0][DIM1], /* Write buffer */ **rdata, /* Read buffer */ val; char *names[4] = {"SOLID", "LIQUID", "GAS", "PLASMA"}, name[NAME_BUF_SIZE]; int ndims, i, j; /* * Initialize data. */ for (i=0; i<DIM0; i++) for (j=0; j<DIM1; j++) wdata[i][j] = (phase_t) ( (i + 1) * j - j) % (int) (PLASMA + 1); /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create the enumerated datatypes for file and memory. This * process is simplified if native types are used for the file, * as only one type must be defined. */ filetype = H5Tenum_create (F_BASET); memtype = H5Tenum_create (M_BASET); for (i = (int) SOLID; i <= (int) PLASMA; i++) { /* * Insert enumerated value for memtype. */ val = (phase_t) i; status = H5Tenum_insert (memtype, names[i], &val); /* * Insert enumerated value for filetype. We must first convert * the numerical value val to the base type of the destination. */ status = H5Tconvert (M_BASET, F_BASET, 1, &val, NULL, H5P_DEFAULT); status = H5Tenum_insert (filetype, names[i], &val); } /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (2, dims, NULL); /* * Create the dataset and write the enumerated data to it. */ dset = H5Dcreate (file, DATASET, filetype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); /* * Close and release resources. */ status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (filetype); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the dataset has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). For simplicity, we do not rebuild memtype. */ /* * Open file and dataset. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET, H5P_DEFAULT); /* * Get dataspace and allocate memory for read buffer. This is a * two dimensional dataset so the dynamic allocation must be done * in steps. */ space = H5Dget_space (dset); ndims = H5Sget_simple_extent_dims (space, dims, NULL); /* * Allocate array of pointers to rows. */ rdata = (phase_t **) malloc (dims[0] * sizeof (phase_t *)); /* * Allocate space for enumerated data. */ rdata[0] = (phase_t *) malloc (dims[0] * dims[1] * sizeof (phase_t)); /* * Set the rest of the pointers to rows to the correct addresses. */ for (i=1; i<dims[0]; i++) rdata[i] = rdata[0] + i * dims[1]; /* * Read the data. */ status = H5Dread (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); /* * Output the data to the screen. */ printf ("%s:\n", DATASET); for (i=0; i<dims[0]; i++) { printf (" ["); for (j=0; j<dims[1]; j++) { /* * Get the name of the enumeration member. */ status = H5Tenum_nameof (memtype, &rdata[i][j], name, NAME_BUF_SIZE); printf (" %-6s", name); } printf ("]\n"); } /* * Close and release resources. */ free (rdata[0]); free (rdata); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (memtype); status = H5Fclose (file); return 0; }
/*! This function reads a snapshot file and distributes the data it contains * to tasks 'readTask' to 'lastTask'. */ void read_file(char *fname, int readTask, int lastTask) { int blockmaxlen; int i, n_in_file, n_for_this_task, ntask, pc, offset = 0, task; int blksize1, blksize2; MPI_Status status; FILE *fd = 0; int nall; int type; char label[4]; int nstart, bytes_per_blockelement, npart, nextblock, typelist[6]; enum iofields blocknr; #ifdef HAVE_HDF5 char buf[500]; int rank, pcsum; hid_t hdf5_file, hdf5_grp[6], hdf5_dataspace_in_file; hid_t hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataset; hsize_t dims[2], count[2], start[2]; #endif #define SKIP {my_fread(&blksize1,sizeof(int),1,fd);} #define SKIP2 {my_fread(&blksize2,sizeof(int),1,fd);} if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) { if(!(fd = fopen(fname, "r"))) { printf("can't open file `%s' for reading initial conditions.\n", fname); endrun(123); } if(All.ICFormat == 2) { SKIP; my_fread(&label, sizeof(char), 4, fd); my_fread(&nextblock, sizeof(int), 1, fd); printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3], nextblock); SKIP2; } SKIP; my_fread(&header, sizeof(header), 1, fd); SKIP2; if(blksize1 != 256 || blksize2 != 256) { printf("incorrect header format\n"); fflush(stdout); endrun(890); } } #ifdef HAVE_HDF5 if(All.ICFormat == 3) { read_header_attributes_in_hdf5(fname); hdf5_file = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT); for(type = 0; type < 6; type++) { if(header.npart[type] > 0) { sprintf(buf, "/PartType%d", type); hdf5_grp[type] = H5Gopen(hdf5_file, buf); } } } #endif for(task = readTask + 1; task <= lastTask; task++) MPI_Ssend(&header, sizeof(header), MPI_BYTE, task, TAG_HEADER, MPI_COMM_WORLD); } else MPI_Recv(&header, sizeof(header), MPI_BYTE, readTask, TAG_HEADER, MPI_COMM_WORLD, &status); if(All.TotNumPart == 0) { if(header.num_files <= 1) for(i = 0; i < 6; i++) header.npartTotal[i] = header.npart[i]; All.TotN_gas = header.npartTotal[0] + (((long long) header.npartTotalHighWord[0]) << 32); for(i = 0, All.TotNumPart = 0; i < 6; i++) { All.TotNumPart += header.npartTotal[i]; All.TotNumPart += (((long long) header.npartTotalHighWord[i]) << 32); } for(i = 0; i < 6; i++) All.MassTable[i] = header.mass[i]; All.MaxPart = All.PartAllocFactor * (All.TotNumPart / NTask); /* sets the maximum number of particles that may */ All.MaxPartSph = All.PartAllocFactor * (All.TotN_gas / NTask); /* sets the maximum number of particles that may reside on a processor */ allocate_memory(); if(RestartFlag == 2) All.Time = All.TimeBegin = header.time; } if(ThisTask == readTask) { for(i = 0, n_in_file = 0; i < 6; i++) n_in_file += header.npart[i]; printf("\nreading file `%s' on task=%d (contains %d particles.)\n" "distributing this file to tasks %d-%d\n" "Type 0 (gas): %8d (tot=%6d%09d) masstab=%g\n" "Type 1 (halo): %8d (tot=%6d%09d) masstab=%g\n" "Type 2 (disk): %8d (tot=%6d%09d) masstab=%g\n" "Type 3 (bulge): %8d (tot=%6d%09d) masstab=%g\n" "Type 4 (stars): %8d (tot=%6d%09d) masstab=%g\n" "Type 5 (bndry): %8d (tot=%6d%09d) masstab=%g\n\n", fname, ThisTask, n_in_file, readTask, lastTask, header.npart[0], (int) (header.npartTotal[0] / 1000000000), (int) (header.npartTotal[0] % 1000000000), All.MassTable[0], header.npart[1], (int) (header.npartTotal[1] / 1000000000), (int) (header.npartTotal[1] % 1000000000), All.MassTable[1], header.npart[2], (int) (header.npartTotal[2] / 1000000000), (int) (header.npartTotal[2] % 1000000000), All.MassTable[2], header.npart[3], (int) (header.npartTotal[3] / 1000000000), (int) (header.npartTotal[3] % 1000000000), All.MassTable[3], header.npart[4], (int) (header.npartTotal[4] / 1000000000), (int) (header.npartTotal[4] % 1000000000), All.MassTable[4], header.npart[5], (int) (header.npartTotal[5] / 1000000000), (int) (header.npartTotal[5] % 1000000000), All.MassTable[5]); fflush(stdout); } ntask = lastTask - readTask + 1; /* to collect the gas particles all at the beginning (in case several snapshot files are read on the current CPU) we move the collisionless particles such that a gap of the right size is created */ for(type = 0, nall = 0; type < 6; type++) { n_in_file = header.npart[type]; n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; nall += n_for_this_task; } memmove(&P[N_gas + nall], &P[N_gas], (NumPart - N_gas) * sizeof(struct particle_data)); nstart = N_gas; for(blocknr = 0; blocknr < IO_NBLOCKS; blocknr++) { if(blockpresent(blocknr)) { if(RestartFlag == 0 && blocknr > IO_U) continue; /* ignore all other blocks in initial conditions */ bytes_per_blockelement = get_bytes_per_blockelement(blocknr); blockmaxlen = ((int) (All.BufferSize * 1024 * 1024)) / bytes_per_blockelement; npart = get_particles_in_block(blocknr, &typelist[0]); if(npart > 0) { if(ThisTask == readTask) { if(All.ICFormat == 2) { SKIP; my_fread(&label, sizeof(char), 4, fd); my_fread(&nextblock, sizeof(int), 1, fd); printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3], nextblock); SKIP2; if(strncmp(label, Tab_IO_Labels[blocknr], 4) != 0) { printf("incorrect block-structure!\n"); printf("expected '%c%c%c%c' but found '%c%c%c%c'\n", label[0], label[1], label[2], label[3], Tab_IO_Labels[blocknr][0], Tab_IO_Labels[blocknr][1], Tab_IO_Labels[blocknr][2], Tab_IO_Labels[blocknr][3]); fflush(stdout); endrun(1890); } } if(All.ICFormat == 1 || All.ICFormat == 2) SKIP; } for(type = 0, offset = 0; type < 6; type++) { n_in_file = header.npart[type]; #ifdef HAVE_HDF5 pcsum = 0; #endif if(typelist[type] == 0) { n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; offset += n_for_this_task; } else { for(task = readTask; task <= lastTask; task++) { n_for_this_task = n_in_file / ntask; if((task - readTask) < (n_in_file % ntask)) n_for_this_task++; if(task == ThisTask) if(NumPart + n_for_this_task > All.MaxPart) { printf("too many particles\n"); endrun(1313); } do { pc = n_for_this_task; if(pc > blockmaxlen) pc = blockmaxlen; if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) my_fread(CommBuffer, bytes_per_blockelement, pc, fd); #ifdef HAVE_HDF5 if(All.ICFormat == 3) { get_dataset_name(blocknr, buf); hdf5_dataset = H5Dopen(hdf5_grp[type], buf); dims[0] = header.npart[type]; dims[1] = get_values_per_blockelement(blocknr); if(dims[1] == 1) rank = 1; else rank = 2; hdf5_dataspace_in_file = H5Screate_simple(rank, dims, NULL); dims[0] = pc; hdf5_dataspace_in_memory = H5Screate_simple(rank, dims, NULL); start[0] = pcsum; start[1] = 0; count[0] = pc; count[1] = get_values_per_blockelement(blocknr); pcsum += pc; H5Sselect_hyperslab(hdf5_dataspace_in_file, H5S_SELECT_SET, start, NULL, count, NULL); switch (get_datatype_in_block(blocknr)) { case 0: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT); break; case 1: hdf5_datatype = H5Tcopy(H5T_NATIVE_FLOAT); break; case 2: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT64); break; } H5Dread(hdf5_dataset, hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataspace_in_file, H5P_DEFAULT, CommBuffer); H5Tclose(hdf5_datatype); H5Sclose(hdf5_dataspace_in_memory); H5Sclose(hdf5_dataspace_in_file); H5Dclose(hdf5_dataset); } #endif } if(ThisTask == readTask && task != readTask) MPI_Ssend(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, task, TAG_PDATA, MPI_COMM_WORLD); if(ThisTask != readTask && task == ThisTask) MPI_Recv(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, readTask, TAG_PDATA, MPI_COMM_WORLD, &status); if(ThisTask == task) { empty_read_buffer(blocknr, nstart + offset, pc, type); offset += pc; } n_for_this_task -= pc; } while(n_for_this_task > 0); } } } if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) { SKIP2; if(blksize1 != blksize2) { printf("incorrect block-sizes detected!\n"); printf("Task=%d blocknr=%d blksize1=%d blksize2=%d\n", ThisTask, blocknr, blksize1, blksize2); fflush(stdout); endrun(1889); } } } } } } for(type = 0; type < 6; type++) { n_in_file = header.npart[type]; n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; NumPart += n_for_this_task; if(type == 0) N_gas += n_for_this_task; } if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) fclose(fd); #ifdef HAVE_HDF5 if(All.ICFormat == 3) { for(type = 5; type >= 0; type--) if(header.npart[type] > 0) H5Gclose(hdf5_grp[type]); H5Fclose(hdf5_file); } #endif } }
int h5repack_verify(const char *out_fname, pack_opt_t *options) { int ret_value = 0; /*no need to LEAVE() on ERROR: HERR_INIT(int, SUCCEED) */ hid_t fidout = -1; /* file ID for output file*/ hid_t did = -1; /* dataset ID */ hid_t pid = -1; /* dataset creation property list ID */ hid_t sid = -1; /* space ID */ hid_t tid = -1; /* type ID */ unsigned int i; trav_table_t *travt = NULL; int ok = 1; /* open the output file */ if((fidout = H5Fopen(out_fname, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0 ) return -1; for(i = 0; i < options->op_tbl->nelems; i++) { char* name = options->op_tbl->objs[i].path; pack_info_t *obj = &options->op_tbl->objs[i]; /*------------------------------------------------------------------------- * open *------------------------------------------------------------------------- */ if((did = H5Dopen2(fidout, name, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dopen2 failed"); if((sid = H5Dget_space(did)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_space failed"); if((pid = H5Dget_create_plist(did)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_create_plist failed"); if((tid = H5Dget_type(did)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_type failed"); /*------------------------------------------------------------------------- * filter check *------------------------------------------------------------------------- */ if(verify_filters(pid, tid, obj->nfilters, obj->filter) <= 0) ok = 0; /*------------------------------------------------------------------------- * layout check *------------------------------------------------------------------------- */ if((obj->layout != -1) && (verify_layout(pid, obj) == 0)) ok = 0; /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if(H5Pclose(pid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); if (H5Sclose(sid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Sclose failed"); if (H5Dclose(did) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dclose failed"); if (H5Tclose(tid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tclose failed"); } /*------------------------------------------------------------------------- * check for the "all" objects option *------------------------------------------------------------------------- */ if(options->all_filter == 1 || options->all_layout == 1) { /* init table */ trav_table_init(&travt); /* get the list of objects in the file */ if(h5trav_gettable(fidout, travt) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "h5trav_gettable failed"); for(i = 0; i < travt->nobjs; i++) { char *name = travt->objs[i].name; if(travt->objs[i].type == H5TRAV_TYPE_DATASET) { /*------------------------------------------------------------------------- * open *------------------------------------------------------------------------- */ if((did = H5Dopen2(fidout, name, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dopen2 failed"); if((sid = H5Dget_space(did)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_space failed"); if((pid = H5Dget_create_plist(did)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_create_plist failed"); if((tid = H5Dget_type(did)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dget_type failed"); /*------------------------------------------------------------------------- * filter check *------------------------------------------------------------------------- */ if(options->all_filter == 1) { if(verify_filters(pid, tid, options->n_filter_g, options->filter_g) <= 0) ok = 0; } /*------------------------------------------------------------------------- * layout check *------------------------------------------------------------------------- */ if(options->all_layout == 1) { pack_info_t pack; init_packobject(&pack); pack.layout = options->layout_g; pack.chunk = options->chunk_g; if(verify_layout(pid, &pack) == 0) ok = 0; } /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if (H5Pclose(pid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); if (H5Sclose(sid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Sclose failed"); if (H5Dclose(did) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Dclose failed"); if (H5Tclose(tid) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tclose failed"); } /* if */ } /* i */ /* free table */ trav_table_free(travt); } /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if (H5Fclose(fidout) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Fclose failed"); return ok; done: H5E_BEGIN_TRY { H5Pclose(pid); H5Sclose(sid); H5Dclose(did); H5Fclose(fidout); if (travt) trav_table_free(travt); } H5E_END_TRY; return ret_value; } /* h5repack_verify() */
int main (void) { hid_t file, space, dtype, dset; /* Handles */ herr_t status; hsize_t dims[1] = {DIM0}; size_t len; char wdata[DIM0*LEN], /* Write buffer */ *rdata, /* Read buffer */ str[LEN] = "OPAQUE", *tag; int ndims, i, j; /* * Initialize data. */ for (i=0; i<DIM0; i++) { for (j=0; j<LEN-1; j++) wdata[j + i * LEN] = str[j]; wdata[LEN - 1 + i * LEN] = (char) i + '0'; } /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create opaque datatype and set the tag to something appropriate. * For this example we will write and view the data as a character * array. */ dtype = H5Tcreate (H5T_OPAQUE, LEN); status = H5Tset_tag (dtype, "Character array"); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the dataset and write the opaque data to it. */ dset = H5Dcreate (file, DATASET, dtype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite (dset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); /* * Close and release resources. */ status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (dtype); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the dataset has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file and dataset. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET, H5P_DEFAULT); /* * Get datatype and properties for the datatype. Note that H5Tget_tag * allocates space for the string in tag, so we must remember to free() it * later. */ dtype = H5Dget_type (dset); len = H5Tget_size (dtype); tag = H5Tget_tag (dtype); /* * Get dataspace and allocate memory for read buffer. */ space = H5Dget_space (dset); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (char *) malloc (dims[0] * len); /* * Read the data. */ status = H5Dread (dset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); /* * Output the data to the screen. */ printf ("Datatype tag for %s is: \"%s\"\n", DATASET, tag); for (i=0; i<dims[0]; i++) { printf ("%s[%u]: ", DATASET, i); for (j=0; j<len; j++) printf ("%c", rdata[j + i * len]); printf ("\n"); } /* * Close and release resources. */ free (rdata); free (tag); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (dtype); status = H5Fclose (file); return 0; }
int main() { double *data_out; // buffer int nx = 1/DX; // hyperslab and output buffer dimensions int ny = 1/DY; // int i, j; // Allocate memory for data if((data_out = (double *)malloc(STEPS * nx * ny * sizeof(double))) == NULL) printf("Error malloc matrix data_out[%d]\n",nx * ny); // Create NetCDF file. NC_CLOBBER tells NetCDF to overwrite this file, if it already exists int ncid, retval; // if( retval = nc_create( NC_FILE_NAME_NETCDF, NC_CLOBBER|NC_NETCDF4, &ncid ) ) { if( retval = nc_create( NC_FILE_NAME_NETCDF, NC_CLOBBER, &ncid ) ) { ERR_NETCDF(retval); } // Define the x and y dimensions. NetCDF will hand back and ID for each. int x_dimid, y_dimid, t_dimid; if( retval = nc_def_dim( ncid, "x", nx, &x_dimid ) ) { ERR_NETCDF(retval); } if( retval = nc_def_dim( ncid, "y", ny, &y_dimid ) ) { ERR_NETCDF(retval); } // Define the t dimension at NetCDF. if( retval = nc_def_dim( ncid, "t", NC_UNLIMITED, &t_dimid ) ) { ERR_NETCDF(retval); } // Define coordinate variables for x and y at NetCDF int x_varid, y_varid, t_varid; if( retval = nc_def_var( ncid, "x", NC_DOUBLE, 1, &x_dimid, &x_varid ) ) { ERR_NETCDF(retval); } if( retval = nc_def_var( ncid, "y", NC_DOUBLE, 1, &y_dimid, &y_varid ) ) { ERR_NETCDF(retval); } if( retval = nc_def_var( ncid, "t", NC_DOUBLE, 1, &t_dimid, &t_varid ) ) { ERR_NETCDF(retval); } // Define the nc-variable to store temperature data. The t dimension should be the one which varies more slowly at NetCDF. int varid; int dimids[3] = { t_dimid, x_dimid, y_dimid }; if( retval = nc_def_var( ncid, "temperature", NC_DOUBLE, 3, dimids, &varid )){ ERR_NETCDF(retval); } // Write x, y, t and temperature units at NetCDF char * space_units = "meters"; char * time_units = "seconds since start of the experiment"; char * temp_units = "kelvin"; if( retval = nc_put_att_text( ncid, x_varid, "units", strlen(space_units), space_units ) ) { ERR_NETCDF(retval); } if( retval = nc_put_att_text( ncid, y_varid, "units", strlen(space_units), space_units ) ) { ERR_NETCDF(retval); } if( retval = nc_put_att_text( ncid, t_varid, "units", strlen(time_units), time_units ) ) { ERR_NETCDF(retval); } if( retval = nc_put_att_text( ncid, varid, "units", strlen(temp_units), temp_units ) ) { ERR_NETCDF(retval); } double scale_factor = 300.0; if( retval = nc_put_att_double( ncid, varid, "scale_factor", NC_DOUBLE, 1, &scale_factor ) ) { ERR_NETCDF(retval); } // End define mode: this tells NetCDF that we are done defining metadata at NetCDF if( retval = nc_enddef( ncid ) ) { ERR_NETCDF(retval); } // Write x coordinates at NetCDF size_t pos; for( pos = 0; pos < nx; ++pos ) { double x = DX*pos; if( retval = nc_put_var1_double( ncid, x_varid, &pos, &x ) ) { ERR_NETCDF(retval); } } // Write y coordinates at NetCDF for( pos = 0; pos < ny; ++pos ) { double y = DY*pos; if( retval = nc_put_var1_double( ncid, y_varid, &pos, &y ) ) { ERR_NETCDF(retval); } } // Open an existing HDF5 file for Output buffer hid_t file_id = H5Fopen(H5_FILE_NAME_HDF5, H5F_ACC_RDONLY, H5P_DEFAULT); if( file_id < 0 ) { ERR_HDF5; } // Open an existing HDF5 dataset hid_t tempD = H5Dopen(file_id, "temperature", H5P_DEFAULT); if( tempD < 0 ) { ERR_HDF5; } // Returns an identifier for a copy of the dataspace for a dataset HDF5 hid_t tempSel = H5Dget_space (tempD); /* dataspace handle */ if( tempSel < 0 ) { ERR_HDF5; } // Returns the number of dimensions in the HDF5 dataspace if successful; otherwise returns a negative value int rank; rank = H5Sget_simple_extent_ndims (tempSel); // Retrieves dataspace dimension size and maximum size HDF5 hsize_t dims_out[2]; // HDF5 dataset dimensions hid_t status_n = H5Sget_simple_extent_dims (tempSel, dims_out, NULL); if( status_n < 0 ) { ERR_HDF5; } // Display the number of dimensions in the HDF5 dataspace and the dataspace dimension size and maximum size printf("\nRank: %d\nDimensions: %lu x %lu \n", rank, (unsigned long)(dims_out[0]), (unsigned long)(dims_out[1])); // Define hyperslab in the dataset HDF5 hsize_t sel_offset_in[4] = {0,0,0,SECTION}; // The temperature value for the last interaction (STEPS) is chosen hsize_t sel_length_in[4] = {STEPS, nx, ny, 1}; H5Sselect_hyperslab( tempSel, H5S_SELECT_SET, sel_offset_in, NULL, sel_length_in, NULL ); if( tempSel < 0 ) { ERR_HDF5; } // Define the memory dataspace HDF5 hsize_t memSdim[4]={STEPS,nx,ny}; hid_t memS = H5Screate_simple( 3, memSdim, NULL ); if( memS < 0 ) { ERR_HDF5; } // Define memory HDF5 hyperslab hsize_t sel_offset_out[3] = {0,0,0}; hsize_t sel_length_out[3] = {STEPS, nx, ny}; H5Sselect_hyperslab( memS, H5S_SELECT_SET, sel_offset_out, NULL, sel_length_out, NULL ); if( memS < 0 ) { ERR_HDF5; } // Read dataset tempD data from HDF5 hyperslab in the file into the hyperslab in memory hsize_t status = H5Dread (tempD, H5T_NATIVE_DOUBLE, memS, tempSel, H5P_DEFAULT, data_out); if( status < 0 ) { ERR_HDF5; } // printf ("Data:\n "); // for( i = 0; i < nx; ++i ) { // for( j = 0; j < ny; ++j ) { // printf("%f ", data_out[i*ny+j]); // } // printf("\n "); // } // printf("\n"); // Write the data to the NETCDF file size_t corner_vector[3] = {0,0,0}; size_t edge_lengths[3] = {STEPS, nx, ny}; if(retval = nc_put_vara_double(ncid, varid, corner_vector, edge_lengths, data_out)){ ERR_NETCDF(retval); } pos = 0; double tval = 0; if( retval = nc_put_var1_double( ncid, t_varid, &pos, &tval ) ) { ERR_NETCDF(retval); } // Close the HDF5 memspace if( H5Sclose( memS ) < 0 ) { ERR_HDF5; } // Close the HDF5 dataspace if( H5Sclose( tempSel ) < 0 ) { ERR_HDF5; } // Close the HDF5 dataset if( H5Dclose( tempD ) < 0 ) { ERR_HDF5; } // Close the HDF5 file if( H5Fclose( file_id ) < 0 ) { ERR_HDF5; } // Close the file. This frees up any internal NetCDF resources associated with the file, and flushes any buffers if( retval = nc_close( ncid ) ) { ERR_NETCDF(retval); } // Free memory free(data_out); return 0; }
void* load_H5_Data_4D(const char* filename, int datatype, const char* datasetname, int W, int H, int D, int ox, int oy, int oz, int timestep, int comp, bool fit) { // fprintf(stderr, "%s:%s()\n", __FILE__, __func__); //only handle h5 data with one component for now assert(comp == 1); hid_t input_file = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT); if (input_file < 0) { fprintf(stderr, "%s:%s(): Error opening input file %s\n", __FILE__, __func__, filename); exit(1); } hid_t dataset = H5Dopen(input_file, datasetname); if (dataset < 0) { H5Eprint(NULL); H5Fclose(input_file); fprintf(stderr, "Error opening dataset on file %s\n", filename); //throw IOException(string("Error opening dataset")); exit(1); } hid_t dataspace = H5Dget_space(dataset); assert(dataspace >=0); hsize_t maxdims[4]; hsize_t dims[4]; int dimensions = H5Sget_simple_extent_dims(dataspace, dims, maxdims); assert (dimensions == 4); #ifdef _DEBUG fprintf(stderr, "data dimension(%d, %d, %d, %d)\n", (int)dims[0], (int)dims[1], (int)dims[2], (int)dims[3]); fprintf(stderr, "origin(%d,%d,%d,%d), count(%d,%d,%d,%d)\n", ox, oy, oz, timestep, W, H, D, 1); if(fit) fprintf(stderr, "read data to fit\n"); #endif hsize_t start[4]; hsize_t start_out[4]; hsize_t stride[4]; hsize_t stride_out[4]; hsize_t count[4]; hsize_t count_out[4]; hsize_t block[4]; hsize_t block_out[4]; start[0] = timestep; start_out[0] = 0; start[1] = oz; start_out[1] = 0; start[2] = oy; start_out[2] = 0; start[3] = ox; start_out[3] = 0; stride[0] = 1; stride_out[0] = 1; stride[1] = fit? dims[0]/D : 1; stride_out[1] = 1; stride[2] = fit? dims[1]/H : 1; stride_out[2] = 1; stride[3] = fit? dims[2]/W : 1; stride_out[3] = 1; count[0] = 1; count_out[0] = count[0]; count[1] = D; count_out[1] = count[1]; count[2] = H; count_out[2] = count[2]; count[3] = W; count_out[3] = count[3]; block[0] = 1; block_out[0] = 1; block[1] = 1; block_out[1] = 1; block[2] = 1; block_out[2] = 1; block[3] = 1; block_out[3] = 1; herr_t status; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block); int b = get_depth(datatype); size_t my_val = count[0] * count[1] * count[2] * count[3] * b; //printf("set to read %ld bytes data\n", my_val); assert(H5Sget_select_npoints(dataspace) * b == my_val); //allocate the output buffer void *wholedata = malloc(my_val); if(wholedata == NULL) { fprintf(stderr, "can't allocate output buffer\n"); exit(1); } if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "Cannot select data slab\n"); //throw IOException("Cannot select data slab"); exit(1); } hid_t memspace = H5Screate_simple(4, count_out, NULL); status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, start_out , stride_out, count_out, block_out); if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "Cannot select mem slab\n"); //throw IOException("Cannot select mem slab"); exit(1); } assert(H5Sget_select_npoints(memspace) * b == my_val); hid_t plist = H5Pcreate(H5P_DATASET_XFER); //is this hint really useful? status = H5Pset_buffer(plist, my_val, NULL, NULL); if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "Cannot set data buffer\n"); //throw IOException("Cannot set buffer"); exit(1); } //read data into "data" buffer, fortran ordering! hid_t mem_type; switch(datatype) { case 0: mem_type = H5T_NATIVE_CHAR; break; case 1: mem_type = H5T_NATIVE_SHORT; break; case 2: mem_type = H5T_NATIVE_FLOAT; break; //H5T_IEEE_F32LE case 3: mem_type = H5T_NATIVE_DOUBLE; break; default: mem_type = H5T_NATIVE_CHAR; } status = H5Dread (dataset, mem_type, memspace, dataspace, plist, wholedata); if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "READ ERROR!\n"); //throw IOException("Read error"); exit(1); } H5Pclose (plist); H5Sclose (memspace); H5Sclose( dataspace ); H5Dclose( dataset ); H5Fclose( input_file ); return wholedata; }