Search in sources :

Example 1 with Attribute

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

the class VH5Path method walk.

/**
 * find next object in sequence
 * @param hobj previous element in sequence
 * @param steps name of each step
 * @param index current step
 * @return next object path, if present
 * @throws HDF5Exception
 */
private static Object walk(Object hobj, String[] steps, int index) throws Exception {
    final boolean isLastIndex = lastIndex(index, steps);
    final String finding = steps[index];
    Group g = BeanUtils.downcast(Group.class, hobj);
    if (g != null) {
        List<HObject> ml = g.getMemberList();
        for (HObject sub : ml) {
            // String full = sub.getFullName();
            if (finding.equals(sub.getName())) {
                if (isLastIndex) {
                    return sub;
                }
                return walk(sub, steps, index + 1);
            }
        }
    }
    H5CompoundDS cds = BeanUtils.downcast(H5CompoundDS.class, hobj);
    if (cds != null) {
        cds.read();
        String[] mn = cds.getMemberNames();
        for (int i = 0; i < mn.length; i++) {
            if (finding.equals(mn[i])) {
                Object c = cds.read();
                Vector<?> vec = BeanUtils.downcast(Vector.class, c);
                if (vec != null) {
                    VCAssert.assertTrue(i < vec.size(), "Disconnect between H5CompoundDS.getMemberNames( )  and returned Vector");
                    Object child = vec.get(i);
                    if (isLastIndex) {
                        return child;
                    }
                } else {
                    throw new UnsupportedOperationException("Unsupported H5CompoundDS subtype " + className(c));
                }
            }
        }
    }
    if (isLastIndex) {
        DataFormat df = BeanUtils.downcast(DataFormat.class, hobj);
        if (df != null && df.hasAttribute()) {
            try {
                @SuppressWarnings("unchecked") List<Object> meta = df.getMetadata();
                for (Object o : meta) {
                    Attribute a = BeanUtils.downcast(Attribute.class, o);
                    if (a != null) {
                        if (finding.equals(a.getName())) {
                            return a.getValue();
                        }
                    } else {
                        lg.warn(concat(steps, finding) + " fetching metadata unexpected type " + className(o));
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(concat(steps, finding) + " fetching metadata", e);
            }
        }
    }
    return null;
}
Also used : Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) Attribute(ncsa.hdf.object.Attribute) HDF5Exception(ncsa.hdf.hdf5lib.exceptions.HDF5Exception) DataFormat(ncsa.hdf.object.DataFormat) HObject(ncsa.hdf.object.HObject) H5CompoundDS(ncsa.hdf.object.h5.H5CompoundDS)

Example 2 with Attribute

use of ncsa.hdf.object.Attribute 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
        }
    }
}
Also used : Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) 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 3 with Attribute

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

the class ChomboFileReader method readMembraneVarData.

// 
// Membrane data are stored as a UCHC (or vcell) extension to the normal Chombo Data.
// ChomboMembraneVarData was formally called VCellSolution by Fei.
// 
private static void readMembraneVarData(ChomboMeshData chomboMeshData, Group rootGroup) {
    // I added solution and extrapolated_volumes group to hold all the solutions from vcell
    String[] groups = new String[] { "solution" /*, "extrapolated_volumes"*/
    };
    for (String group : groups) {
        try {
            Group vcellGroup = Hdf5Reader.getChildGroup(rootGroup, group);
            if (vcellGroup != null) {
                List<HObject> children = vcellGroup.getMemberList();
                for (HObject c : children) {
                    if (c instanceof Dataset) {
                        Dataset dataset = (Dataset) c;
                        String name = dataset.getName();
                        List<Attribute> solAttrList = dataset.getMetadata();
                        String domain = null;
                        for (Attribute attr : solAttrList) {
                            String attrName = attr.getName();
                            if (attrName.equals("domain")) {
                                Object obj = attr.getValue();
                                domain = ((String[]) obj)[0];
                                break;
                            }
                        }
                        ChomboMembraneVarData vcellSolution = new ChomboMembraneVarData(name, domain, (double[]) dataset.read());
                        chomboMeshData.addMembraneVarData(vcellSolution);
                    }
                }
            }
        } catch (Exception ex) {
        // it is ok if there is no vcell group
        }
    }
}
Also used : Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) Attribute(ncsa.hdf.object.Attribute) ChomboDataset(org.vcell.vis.chombo.ChomboDataset) Dataset(ncsa.hdf.object.Dataset) HObject(ncsa.hdf.object.HObject) ChomboMembraneVarData(org.vcell.vis.chombo.ChomboMembraneVarData)

Example 4 with Attribute

use of ncsa.hdf.object.Attribute 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
            }
        }
    }
}
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) MBSDataGroup(cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup)

Example 5 with Attribute

use of ncsa.hdf.object.Attribute 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)

Aggregations

Attribute (ncsa.hdf.object.Attribute)12 HObject (ncsa.hdf.object.HObject)11 Group (ncsa.hdf.object.Group)10 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)8 Dataset (ncsa.hdf.object.Dataset)8 FileFormat (ncsa.hdf.object.FileFormat)8 IOException (java.io.IOException)6 FileNotFoundException (java.io.FileNotFoundException)5 File (java.io.File)4 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)4 MBSDataGroup (cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup)3 MSBDataAttribute (cbit.vcell.solvers.CartesianMeshMovingBoundary.MSBDataAttribute)3 VariableType (cbit.vcell.math.VariableType)2 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 Vector (java.util.Vector)2 H5CompoundDS (ncsa.hdf.object.h5.H5CompoundDS)2 MathFormatException (cbit.vcell.math.MathFormatException)1 Domain (cbit.vcell.math.Variable.Domain)1 HashMap (java.util.HashMap)1