Search in sources :

Example 1 with HObject

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

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

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

the class Hdf5Reader method getDataTable.

public static Hdf5Reader.DataColumn[] getDataTable(Group group, String name) throws Exception {
    List<HObject> memberList = group.getMemberList();
    for (HObject member : memberList) {
        if (member.getName().equals(name)) {
            if (member instanceof H5CompoundDS) {
                H5CompoundDS compoundDataSet = (H5CompoundDS) member;
                Vector columnValueArrays = (Vector) compoundDataSet.read();
                String[] columnNames = compoundDataSet.getMemberNames();
                ArrayList<Hdf5Reader.DataColumn> dataColumns = new ArrayList<Hdf5Reader.DataColumn>();
                for (int c = 0; c < columnNames.length; c++) {
                    Object column = columnValueArrays.get(c);
                    if (column instanceof int[]) {
                        dataColumns.add(new Hdf5Reader.IntColumn(columnNames[c], (int[]) columnValueArrays.get(c)));
                    } else if (column instanceof double[]) {
                        dataColumns.add(new Hdf5Reader.DoubleColumn(columnNames[c], (double[]) columnValueArrays.get(c)));
                    } else {
                        throw new RuntimeException("unexpected type '" + column.getClass().getName() + "' for group member '" + name + "'");
                    }
                }
                return dataColumns.toArray(new Hdf5Reader.DataColumn[0]);
            } else if (member instanceof H5ScalarDS) {
                H5ScalarDS compoundDataSet = (H5ScalarDS) member;
                Object column = compoundDataSet.read();
                if (column instanceof int[]) {
                    return new Hdf5Reader.DataColumn[] { new Hdf5Reader.IntColumn("col", (int[]) column) };
                } else if (column instanceof double[]) {
                    return new Hdf5Reader.DataColumn[] { new Hdf5Reader.DoubleColumn("col", (double[]) column) };
                } else if (column instanceof long[]) {
                    return new Hdf5Reader.DataColumn[] { new Hdf5Reader.LongColumn("col", (long[]) column) };
                } else {
                    throw new RuntimeException("unexpected type '" + column.getClass().getName() + "' for group member '" + name + "'");
                }
            } else {
                throw new RuntimeException("expecting type H5CompoundDS for group member '" + name + "', found type " + member.getClass().getName());
            }
        }
    }
    throw new RuntimeException("group member '" + name + "' not found");
}
Also used : HObject(ncsa.hdf.object.HObject) ArrayList(java.util.ArrayList) H5ScalarDS(ncsa.hdf.object.h5.H5ScalarDS) HObject(ncsa.hdf.object.HObject) H5CompoundDS(ncsa.hdf.object.h5.H5CompoundDS) Vector(java.util.Vector)

Example 4 with HObject

use of ncsa.hdf.object.HObject 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 5 with HObject

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

Aggregations

HObject (ncsa.hdf.object.HObject)18 Dataset (ncsa.hdf.object.Dataset)13 Group (ncsa.hdf.object.Group)13 FileFormat (ncsa.hdf.object.FileFormat)11 IOException (java.io.IOException)10 Attribute (ncsa.hdf.object.Attribute)10 File (java.io.File)7 FileNotFoundException (java.io.FileNotFoundException)7 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)5 ArrayList (java.util.ArrayList)4 H5ScalarDS (ncsa.hdf.object.h5.H5ScalarDS)4 DataAccessException (org.vcell.util.DataAccessException)4 MBSDataGroup (cbit.vcell.solvers.CartesianMeshMovingBoundary.MBSDataGroup)3 MSBDataAttribute (cbit.vcell.solvers.CartesianMeshMovingBoundary.MSBDataAttribute)3 Vector (java.util.Vector)3 H5CompoundDS (ncsa.hdf.object.h5.H5CompoundDS)3 ExpressionException (cbit.vcell.parser.ExpressionException)2 ODESimData (cbit.vcell.solver.ode.ODESimData)2 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)2