Search in sources :

Example 6 with HObject

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

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

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

the class SimResultsViewer method createODEDataViewer.

/**
 * Insert the method's description here.
 * Creation date: (6/11/2004 2:33:44 PM)
 * @return javax.swing.JPanel
 * @throws DataAccessException
 */
private DataViewer createODEDataViewer() throws DataAccessException {
    odeDataViewer = new ODEDataViewer();
    odeDataViewer.setSimulation(getSimulation());
    ODESolverResultSet odesrs = ((ODEDataManager) dataManager).getODESolverResultSet();
    odeDataViewer.setOdeSolverResultSet(odesrs);
    odeDataViewer.setNFSimMolecularConfigurations(((ODEDataManager) dataManager).getNFSimMolecularConfigurations());
    odeDataViewer.setVcDataIdentifier(dataManager.getVCDataIdentifier());
    if (getSimulation() != null) {
        String ownerName = generateHDF5DescrOwner(getSimulation());
        odeDataViewer.setHDF5DescriptionText(ownerName + ":" + simulation.getName());
    }
    // odeDataViewer.setXVarName(odeDataViewer.getODESolverPlotSpecificationPanel1().getXAxisComboBox_frm().getSelectedItem().toString());
    // 
    // Example code for reading stats data from Stochastic multitrial non-histogram
    // 
    FileFormat hdf5FileFormat = null;
    File to = null;
    try {
        if (odeDataViewer.getOdeSolverResultSet() instanceof ODESimData) {
            byte[] hdf5FileBytes = ((ODESimData) odeDataViewer.getOdeSolverResultSet()).getHdf5FileBytes();
            if (hdf5FileBytes != null) {
                to = File.createTempFile("odeStats_" + getSimulation().getSimulationInfo().getAuthoritativeVCSimulationIdentifier(), ".hdf5");
                Files.write(hdf5FileBytes, to);
                FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
                if (fileFormat == null) {
                    throw new Exception("Cannot find HDF5 FileFormat.");
                }
                // open the file with read-only access
                hdf5FileFormat = fileFormat.createInstance(to.getAbsolutePath(), FileFormat.READ);
                // open the file and retrieve the file structure
                hdf5FileFormat.open();
                Group root = (Group) ((javax.swing.tree.DefaultMutableTreeNode) hdf5FileFormat.getRootNode()).getUserObject();
                List<HObject> postProcessMembers = ((Group) root).getMemberList();
                for (HObject nextHObject : postProcessMembers) {
                    // System.out.println(nextHObject.getName()+"\n"+nextHObject.getClass().getName());
                    H5ScalarDS h5ScalarDS = (H5ScalarDS) nextHObject;
                    h5ScalarDS.init();
                    try {
                        long[] dims = h5ScalarDS.getDims();
                        System.out.println("---" + nextHObject.getName() + " " + nextHObject.getClass().getName() + " Dimensions=" + Arrays.toString(dims));
                        Object obj = h5ScalarDS.read();
                        if (dims.length == 2) {
                            // dims[0]=numTimes (will be the same as 'SimTimes' data length)
                            // dims[1]=numVars (will be the same as 'VarNames' data length)
                            // if name='StatMean' this is the same as the default data saved in the odeSolverresultSet
                            double[] columns = new double[(int) dims[1]];
                            for (int row = 0; row < dims[0]; row++) {
                                System.arraycopy(obj, row * columns.length, columns, 0, columns.length);
                                System.out.println(Arrays.toString(columns));
                            }
                        } else {
                            if (obj instanceof double[]) {
                                System.out.println(Arrays.toString((double[]) obj));
                            } else {
                                System.out.println(Arrays.toString((String[]) obj));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (hdf5FileFormat != null) {
            try {
                hdf5FileFormat.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        if (to != null) {
            try {
                to.delete();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    return odeDataViewer;
}
Also used : Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) ODESimData(cbit.vcell.solver.ode.ODESimData) H5ScalarDS(ncsa.hdf.object.h5.H5ScalarDS) FileFormat(ncsa.hdf.object.FileFormat) DataAccessException(org.vcell.util.DataAccessException) UserCancelException(org.vcell.util.UserCancelException) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODEDataManager(cbit.vcell.simdata.ODEDataManager) HObject(ncsa.hdf.object.HObject) File(java.io.File)

Example 9 with HObject

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

the class ODEDataInterfaceImpl method extractColumnMin.

@Override
public double[] extractColumnMin(String columnName) throws ExpressionException, ObjectNotFoundException {
    FileFormat hdf5FileFormat = null;
    File to = null;
    try {
        ODESolverResultSet osrs = getOdeSolverResultSet();
        if (osrs instanceof ODESimData) {
            byte[] hdf5FileBytes = ((ODESimData) getOdeSolverResultSet()).getHdf5FileBytes();
            if (hdf5FileBytes != null) {
                to = File.createTempFile("odeStats_" + simulationModelInfo.getSimulationName(), ".hdf5");
                Files.write(hdf5FileBytes, to);
                FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
                if (fileFormat == null) {
                    throw new Exception("Cannot find HDF5 FileFormat.");
                }
                // open the file with read-only access
                hdf5FileFormat = fileFormat.createInstance(to.getAbsolutePath(), FileFormat.READ);
                // open the file and retrieve the file structure
                hdf5FileFormat.open();
                Group root = (Group) ((javax.swing.tree.DefaultMutableTreeNode) hdf5FileFormat.getRootNode()).getUserObject();
                List<HObject> postProcessMembers = ((Group) root).getMemberList();
                for (HObject nextHObject : postProcessMembers) {
                    System.out.println(nextHObject.getName() + "   " + nextHObject.getClass().getName());
                    H5ScalarDS h5ScalarDS = (H5ScalarDS) nextHObject;
                    h5ScalarDS.init();
                    try {
                        long[] dims = h5ScalarDS.getDims();
                        System.out.println("---" + nextHObject.getName() + " " + nextHObject.getClass().getName() + " Dimensions=" + Arrays.toString(dims));
                        Object obj = h5ScalarDS.read();
                        if (dims.length == 2) {
                            double[] columns = new double[(int) dims[1]];
                            for (int row = 0; row < dims[0]; row++) {
                                System.arraycopy(obj, row * columns.length, columns, 0, columns.length);
                                System.out.println(Arrays.toString(columns));
                            }
                            return null;
                        // return columns;
                        } else {
                            return null;
                        }
                    } catch (Exception e) {
                        return null;
                    }
                }
            }
        }
    } catch (Exception e) {
    } finally {
        if (hdf5FileFormat != null) {
            try {
                hdf5FileFormat.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        if (to != null) {
            try {
                to.delete();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    return null;
}
Also used : Group(ncsa.hdf.object.Group) HObject(ncsa.hdf.object.HObject) ODESimData(cbit.vcell.solver.ode.ODESimData) H5ScalarDS(ncsa.hdf.object.h5.H5ScalarDS) FileFormat(ncsa.hdf.object.FileFormat) ExpressionException(cbit.vcell.parser.ExpressionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) HObject(ncsa.hdf.object.HObject) File(java.io.File)

Example 10 with HObject

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

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