Search in sources :

Example 6 with Dataset

use of ncsa.hdf.object.Dataset in project vcell by virtualcell.

the class DataSet method readHdf5VariableSolution.

static double[] readHdf5VariableSolution(File zipfile, String fileName, String varName) throws Exception {
    File tempFile = null;
    FileFormat solFile = null;
    try {
        tempFile = createTempHdf5File(zipfile, fileName);
        FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
        solFile = fileFormat.createInstance(tempFile.getAbsolutePath(), FileFormat.READ);
        solFile.open();
        if (varName != null) {
            String varPath = Hdf5Utils.getVarSolutionPath(varName);
            HObject solObj = FileFormat.findObject(solFile, varPath);
            if (solObj instanceof Dataset) {
                Dataset dataset = (Dataset) solObj;
                return (double[]) dataset.read();
            }
        }
    } finally {
        try {
            if (solFile != null) {
                solFile.close();
            }
            if (tempFile != null) {
                if (!tempFile.delete()) {
                    System.err.println("couldn't delete temp file " + tempFile.getAbsolutePath());
                }
            }
        } catch (Exception e) {
        // ignore
        }
    }
    return null;
}
Also used : HObject(ncsa.hdf.object.HObject) Dataset(ncsa.hdf.object.Dataset) FileFormat(ncsa.hdf.object.FileFormat) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with Dataset

use of ncsa.hdf.object.Dataset in project vcell by virtualcell.

the class DataSet method readChomboExtrapolatedValues.

static double[] readChomboExtrapolatedValues(String varName, FileFormat solFile) throws Exception {
    double[] data = null;
    if (varName != null) {
        String varPath = Hdf5Utils.getVolVarExtrapolatedValuesPath(varName);
        HObject solObj = FileFormat.findObject(solFile, varPath);
        if (solObj == null) {
            throw new IOException("Extrapolated values for variable '" + varName + "' does not exist in the results.");
        }
        if (solObj instanceof Dataset) {
            Dataset dataset = (Dataset) solObj;
            return (double[]) dataset.read();
        }
    }
    return data;
}
Also used : HObject(ncsa.hdf.object.HObject) Dataset(ncsa.hdf.object.Dataset) IOException(java.io.IOException)

Example 8 with Dataset

use of ncsa.hdf.object.Dataset in project vcell by virtualcell.

the class CartesianMeshChombo method readMeshFile.

public static CartesianMeshChombo readMeshFile(File chomboMeshFile) throws Exception {
    CartesianMeshChombo chomboMesh = new CartesianMeshChombo();
    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 meshFile = null;
    try {
        meshFile = fileFormat.createInstance(chomboMeshFile.getAbsolutePath(), FileFormat.READ);
        meshFile.open();
        DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) meshFile.getRootNode();
        Group rootGroup = (Group) rootNode.getUserObject();
        Group meshGroup = (Group) rootGroup.getMemberList().get(0);
        List<Attribute> meshAttrList = meshGroup.getMetadata();
        for (Attribute attr : meshAttrList) {
            String attrName = attr.getName();
            MeshAttribute mattr = null;
            try {
                mattr = MeshAttribute.valueOf(attrName);
            } catch (IllegalArgumentException ex) {
            }
            if (mattr == null) {
                // if not found, then we don't care about this attribute
                logger.debug("mesh attribute " + attrName + " is not defined in Java");
                continue;
            }
            Object value = attr.getValue();
            switch(mattr) {
                case dimension:
                    chomboMesh.dimension = ((int[]) value)[0];
                    break;
                case numLevels:
                    chomboMesh.numLevels = ((int[]) value)[0];
                    break;
                case viewLevel:
                    chomboMesh.viewLevel = ((int[]) value)[0];
                    break;
                case refineRatios:
                    chomboMesh.refineRatios = (int[]) value;
                    break;
                case Dx:
                case extent:
                case Nx:
                case origin:
                    // these 4 has format of {};
                    String[] valueStrArray = (String[]) value;
                    String value0 = valueStrArray[0];
                    StringTokenizer st = new StringTokenizer(value0, "{,} ");
                    int numTokens = st.countTokens();
                    // we need 3 for 3d
                    double[] values = new double[Math.max(3, numTokens)];
                    for (int i = 0; i < Math.min(3, numTokens); ++i) {
                        String token = st.nextToken();
                        values[i] = Double.parseDouble(token);
                    }
                    switch(mattr) {
                        case Dx:
                            chomboMesh.dx = new double[3];
                            System.arraycopy(values, 0, chomboMesh.dx, 0, values.length);
                            break;
                        case extent:
                            chomboMesh.extent = new Extent(values[0], values[1], values[2] == 0 ? 1 : values[2]);
                            break;
                        case Nx:
                            chomboMesh.size = new ISize((int) values[0], (int) values[1], values[2] == 0 ? 1 : (int) values[2]);
                            break;
                        case origin:
                            chomboMesh.origin = new Origin(values[0], values[1], values[2]);
                            break;
                    }
                    break;
            }
        }
        List<HObject> memberList = meshGroup.getMemberList();
        for (HObject member : memberList) {
            if (!(member instanceof Dataset)) {
                continue;
            }
            Dataset dataset = (Dataset) member;
            Vector vectValues = (Vector) dataset.read();
            String name = dataset.getName();
            MeshDataSet mdataset = null;
            try {
                mdataset = MeshDataSet.valueOfName(name);
            } catch (IllegalArgumentException ex) {
                logger.debug("mesh dataset " + name + " is not defined in Java");
            }
            if (mdataset == null) {
                // if not found, then we don't care about this dataset
                continue;
            }
            switch(mdataset) {
                case vertices:
                    collectVertices(chomboMesh, vectValues);
                    break;
                case segments:
                    collect2dSegments(chomboMesh, vectValues);
                    break;
                case structures:
                    collectStructures(chomboMesh, vectValues);
                    break;
                case featurephasevols:
                    collectFeaturePhaseVols(chomboMesh, vectValues);
                    break;
                case membraneids:
                    collectMembraneIds(chomboMesh, vectValues);
                    break;
                case membrane_elements:
                case membrane_elements_old:
                    collectMembraneElements(chomboMesh, vectValues);
                    break;
                case surface_triangles:
                    collect3dSurfaceTriangles(chomboMesh, vectValues);
                    break;
                case slice_view:
                    collect3dSliceView(chomboMesh, vectValues);
                    break;
            }
        }
    } finally {
        if (meshFile != null) {
            meshFile.close();
        }
    }
    // set neighbors to membrane elements
    if (chomboMesh.dimension == 2 && chomboMesh.membraneElements != null) {
        for (int i = 0; i < chomboMesh.membraneElements.length; ++i) {
            MembraneElement me = chomboMesh.membraneElements[i];
            me.setConnectivity(chomboMesh.segments[i].prevNeigbhor, chomboMesh.segments[i].nextNeigbhor, -1, -1);
        }
    }
    return chomboMesh;
}
Also used : Origin(org.vcell.util.Origin) Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Attribute(ncsa.hdf.object.Attribute) Extent(org.vcell.util.Extent) ISize(org.vcell.util.ISize) Dataset(ncsa.hdf.object.Dataset) FileFormat(ncsa.hdf.object.FileFormat) IOException(java.io.IOException) MathFormatException(cbit.vcell.math.MathFormatException) StringTokenizer(java.util.StringTokenizer) HObject(ncsa.hdf.object.HObject) Vector(java.util.Vector)

Example 9 with Dataset

use of ncsa.hdf.object.Dataset 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();
        List<HObject> solGroups = rootGroup.getMemberList();
        for (HObject memberGroup : solGroups) {
            if (memberGroup instanceof Group && memberGroup.getName().equals("solution")) {
                Group solGroup = (Group) memberGroup;
                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));
                }
                break;
            }
        }
    } 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
        }
    }
}
Also used : MBSDataGroup(cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup) Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) MSBDataAttribute(cbit.vcell.solvers.CartesianMeshMovingBoundary.MSBDataAttribute) Attribute(ncsa.hdf.object.Attribute) Dataset(ncsa.hdf.object.Dataset) FileFormat(ncsa.hdf.object.FileFormat) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HObject(ncsa.hdf.object.HObject) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 10 with Dataset

use of ncsa.hdf.object.Dataset in project vcell by virtualcell.

the class DataSet method readMBSDataMetadata.

private void readMBSDataMetadata() throws Exception {
    FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
    FileFormat solFile = 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");
        }
        // find any timeGroup
        Group timeGroup = null;
        for (Object member : solutionGroup.getMemberList()) {
            String memberName = ((HObject) member).getName();
            if (member instanceof Group && memberName.startsWith("time")) {
                timeGroup = (Group) member;
                break;
            }
        }
        if (timeGroup == null) {
            throw new Exception("No time group found");
        }
        // find all the datasets in that time group
        for (Object member : timeGroup.getMemberList()) {
            if (member instanceof Dataset) {
                List<Attribute> solAttrList = ((Dataset) member).getMetadata();
                int size = 0;
                String varName = null;
                VariableType varType = null;
                for (Attribute attr : solAttrList) {
                    String attrName = attr.getName();
                    Object attrValue = attr.getValue();
                    if (attrName.equals(MSBDataAttribute.name.name())) {
                        varName = ((String[]) attrValue)[0];
                    } else if (attrName.equals(MSBDataAttribute.size.name())) {
                        size = ((int[]) attrValue)[0];
                    } else if (attrName.equals(MSBDataAttribute.type.name())) {
                        String vt = ((String[]) attrValue)[0];
                        if (vt.equals(MSBDataAttributeValue.Point.name())) {
                            varType = VariableType.POINT_VARIABLE;
                        } else if (vt.equals(MSBDataAttributeValue.Volume.name())) {
                            varType = VariableType.VOLUME;
                        } else if (vt.equals(MSBDataAttributeValue.PointSubDomain.name())) {
                        // Position for PointSubdomain
                        }
                    }
                }
                if (varType == VariableType.VOLUME) {
                    // only display volume
                    dataBlockList.addElement(DataBlock.createDataBlock(varName, varType.getType(), size, 0));
                }
                if (varType == VariableType.POINT_VARIABLE) {
                    // only display volume
                    dataBlockList.addElement(DataBlock.createDataBlock(varName, varType.getType(), size, 0));
                }
            }
        }
    } finally {
        if (solFile != null) {
            try {
                solFile.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : MBSDataGroup(cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup) Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) VariableType(cbit.vcell.math.VariableType) MSBDataAttribute(cbit.vcell.solvers.CartesianMeshMovingBoundary.MSBDataAttribute) Attribute(ncsa.hdf.object.Attribute) Dataset(ncsa.hdf.object.Dataset) FileFormat(ncsa.hdf.object.FileFormat) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HObject(ncsa.hdf.object.HObject) MBSDataGroup(cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup)

Aggregations

Dataset (ncsa.hdf.object.Dataset)15 HObject (ncsa.hdf.object.HObject)13 Group (ncsa.hdf.object.Group)12 IOException (java.io.IOException)11 FileFormat (ncsa.hdf.object.FileFormat)11 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)8 Attribute (ncsa.hdf.object.Attribute)8 FileNotFoundException (java.io.FileNotFoundException)7 File (java.io.File)5 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)5 MBSDataGroup (cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup)3 MSBDataAttribute (cbit.vcell.solvers.CartesianMeshMovingBoundary.MSBDataAttribute)3 DataAccessException (org.vcell.util.DataAccessException)3 Extent (org.vcell.util.Extent)3 ISize (org.vcell.util.ISize)3 Origin (org.vcell.util.Origin)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 Vector (java.util.Vector)2 Datatype (ncsa.hdf.object.Datatype)2