use of ncsa.hdf.object.FileFormat in project vcell by virtualcell.
the class H5FileStructure method run.
// public static void main(String args[]) throws Exception {
@Test
public void run() {
// create the file and add groups ans dataset into the file
try {
// createFile();
// retrieve an instance of H5File
FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
if (fileFormat == null) {
System.err.println("Cannot find HDF5 FileFormat.");
return;
}
// open the file with read-only access
FileFormat testFile = fileFormat.createInstance(fname, FileFormat.READ);
if (testFile == null) {
System.err.println("Failed to open file: " + fname);
return;
}
// open the file and retrieve the file structure
testFile.open();
Group root = (Group) ((javax.swing.tree.DefaultMutableTreeNode) testFile.getRootNode()).getUserObject();
printGroup(root, "");
// close file resource
testFile.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of ncsa.hdf.object.FileFormat in project vcell by virtualcell.
the class H5FileStructure method createFile.
/**
* create the file and add groups and dataset into the file, which is the
* same as javaExample.H5DatasetCreate
*
* @see javaExample.HDF5DatasetCreate
* @throws Exception
*/
private static void createFile() throws Exception {
// retrieve an instance of H5File
FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
if (fileFormat == null) {
System.err.println("Cannot find HDF5 FileFormat.");
return;
}
// create a new file with a given file name.
H5File testFile = (H5File) fileFormat.createFile(fname, FileFormat.FILE_CREATE_DELETE);
if (testFile == null) {
System.err.println("Failed to create file:" + fname);
return;
}
// open the file and retrieve the root group
testFile.open();
Group root = (Group) ((javax.swing.tree.DefaultMutableTreeNode) testFile.getRootNode()).getUserObject();
// create groups at the root
Group g1 = testFile.createGroup("integer arrays", root);
Group g2 = testFile.createGroup("float arrays", root);
// create 2D 32-bit (4 bytes) integer dataset of 20 by 10
Datatype dtype = testFile.createDatatype(Datatype.CLASS_INTEGER, 4, Datatype.NATIVE, Datatype.NATIVE);
Dataset dataset = testFile.createScalarDS("2D 32-bit integer 20x10", g1, dtype, dims2D, null, null, 0, null);
// create 3D 8-bit (1 byte) unsigned integer dataset of 20 by 10 by 5
dtype = testFile.createDatatype(Datatype.CLASS_INTEGER, 1, Datatype.NATIVE, Datatype.SIGN_NONE);
dataset = testFile.createScalarDS("3D 8-bit unsigned integer 20x10x5", g1, dtype, dims3D, null, null, 0, null);
// create 2D 64-bit (8 bytes) double dataset of 20 by 10
dtype = testFile.createDatatype(Datatype.CLASS_FLOAT, 8, Datatype.NATIVE, -1);
dataset = testFile.createScalarDS("2D 64-bit double 20x10", g2, dtype, dims2D, null, null, 0, null);
// create 3D 32-bit (4 bytes) float dataset of 20 by 10 by 5
dtype = testFile.createDatatype(Datatype.CLASS_FLOAT, 4, Datatype.NATIVE, -1);
dataset = testFile.createScalarDS("3D 32-bit float 20x10x5", g2, dtype, dims3D, null, null, 0, null);
// close file resource
testFile.close();
}
use of ncsa.hdf.object.FileFormat in project vcell by virtualcell.
the class DataSet method readHdf5SolutionMetaData.
private void readHdf5SolutionMetaData(InputStream is) throws Exception {
File tempFile = null;
FileFormat solFile = null;
try {
tempFile = createTempHdf5File(is);
FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
solFile = fileFormat.createInstance(tempFile.getAbsolutePath(), FileFormat.READ);
solFile.open();
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) solFile.getRootNode();
Group rootGroup = (Group) rootNode.getUserObject();
Group solGroup = (Group) rootGroup.getMemberList().get(0);
List<HObject> memberList = solGroup.getMemberList();
for (HObject member : memberList) {
if (!(member instanceof Dataset)) {
continue;
}
Dataset dataset = (Dataset) member;
String dsname = dataset.getName();
int vt = -1;
String domain = null;
List<Attribute> solAttrList = dataset.getMetadata();
for (Attribute attr : solAttrList) {
String attrName = attr.getName();
if (attrName.equals("variable type")) {
Object obj = attr.getValue();
vt = ((int[]) obj)[0];
} else if (attrName.equals("domain")) {
Object obj = attr.getValue();
domain = ((String[]) obj)[0];
}
}
long[] dims = dataset.getDims();
String varName = domain == null ? dsname : domain + Variable.COMBINED_IDENTIFIER_SEPARATOR + dsname;
dataBlockList.addElement(DataBlock.createDataBlock(varName, vt, (int) dims[0], 0));
}
} finally {
try {
if (solFile != null) {
solFile.close();
}
if (tempFile != null) {
if (!tempFile.delete()) {
System.err.println("couldn't delete temp file " + tempFile);
}
}
} catch (Exception e) {
// ignore
}
}
}
use of ncsa.hdf.object.FileFormat in project vcell by virtualcell.
the class CartesianMeshMovingBoundary method readMeshFile.
public static CartesianMeshMovingBoundary readMeshFile(File meshFile) throws Exception {
CartesianMeshMovingBoundary mesh = new CartesianMeshMovingBoundary();
if (H5.H5open() < 0) {
throw new Exception("H5.H5open() failed");
}
FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
if (fileFormat == null) {
throw new Exception("FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5) failed, returned null.");
}
FileFormat meshH5File = null;
try {
meshH5File = fileFormat.createInstance(meshFile.getAbsolutePath(), FileFormat.READ);
meshH5File.open();
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) meshH5File.getRootNode();
Group rootGroup = (Group) rootNode.getUserObject();
Group meshGroup = null;
for (Object member : rootGroup.getMemberList()) {
if (member instanceof Group) {
Group g = (Group) member;
if (g.getName().equals(Group_Mesh))
;
{
meshGroup = g;
break;
}
}
}
if (meshGroup == null) {
throw new Exception(Group_Mesh + " group not found in mesh");
}
for (Object member : meshGroup.getMemberList()) {
if (member instanceof Dataset) {
Dataset ds = (Dataset) member;
Object data = ds.getData();
MeshDataset mds = MeshDataset.valueOf(ds.getName());
switch(mds) {
case dimension:
mesh.dimension = ((int[]) data)[0];
break;
case extent:
{
double[] darr = (double[]) data;
mesh.extent = new Extent(darr[0], darr[1], 0.5);
break;
}
case origin:
{
double[] darr = (double[]) data;
mesh.origin = new Origin(darr[0], darr[1], 0.5);
break;
}
case size:
{
int[] iarr = (int[]) data;
mesh.size = new ISize(iarr[0], iarr[1], 1);
break;
}
}
}
}
} finally {
if (meshH5File != null) {
meshH5File.close();
}
}
return mesh;
}
use of ncsa.hdf.object.FileFormat in project vcell by virtualcell.
the class DataSet method readMBSData.
private double[] readMBSData(String varName, Double time) throws Exception {
FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
FileFormat solFile = null;
double[] data = null;
try {
solFile = fileFormat.createInstance(fileName, FileFormat.READ);
solFile.open();
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) solFile.getRootNode();
Group rootGroup = (Group) rootNode.getUserObject();
Group solutionGroup = null;
for (Object member : rootGroup.getMemberList()) {
String memberName = ((HObject) member).getName();
if (member instanceof Group) {
MBSDataGroup group = MBSDataGroup.valueOf(memberName);
if (group == MBSDataGroup.Solution) {
solutionGroup = (Group) member;
break;
}
}
}
if (solutionGroup == null) {
throw new Exception("Group " + MBSDataGroup.Solution + " not found");
}
int varIndex = -1;
int size = 0;
for (int i = 0; i < dataBlockList.size(); ++i) {
DataBlock dataBlock = dataBlockList.get(i);
if (dataBlock.getVarName().equals(varName)) {
varIndex = i;
size = dataBlock.getSize();
break;
}
}
if (varIndex == -1) {
throw new Exception("Variable " + varName + " not found");
}
// find time group for that time
Group timeGroup = null;
for (Object member : solutionGroup.getMemberList()) {
if (member instanceof Group) {
Group group = (Group) member;
List<Attribute> dsAttrList = group.getMetadata();
Attribute timeAttribute = null;
for (Attribute attr : dsAttrList) {
if (attr.getName().equals(MSBDataAttribute.time.name())) {
timeAttribute = attr;
break;
}
}
if (timeAttribute != null) {
double t = ((double[]) timeAttribute.getValue())[0];
if (Math.abs(t - time) < 1e-8) {
timeGroup = group;
break;
}
}
}
}
if (timeGroup == null) {
throw new Exception("No time group found for time=" + time);
}
// find variable dataset
Dataset varDataset = null;
for (Object member : timeGroup.getMemberList()) {
if (member instanceof Dataset) {
List<Attribute> dsAttrList = ((Dataset) member).getMetadata();
String var = null;
for (Attribute attr : dsAttrList) {
if (attr.getName().equals(MSBDataAttribute.name.name())) {
var = ((String[]) attr.getValue())[0];
break;
}
}
if (var != null && var.equals(varName)) {
varDataset = (Dataset) member;
break;
}
}
}
if (varDataset == null) {
throw new Exception("Data for Variable " + varName + " at time " + time + " not found");
}
data = new double[size];
System.arraycopy((double[]) varDataset.getData(), 0, data, 0, size);
return data;
} finally {
if (solFile != null) {
try {
solFile.close();
} catch (Exception e) {
// ignore
}
}
}
}
Aggregations