Search in sources :

Example 1 with SmoldynKeyword

use of org.vcell.solver.smoldyn.SmoldynVCellMapper.SmoldynKeyword in project vcell by virtualcell.

the class ASCIIExporter method exportParticleData.

/**
 * Insert the method's description here.
 * Creation date: (1/12/00 5:00:28 PM)
 * @return cbit.vcell.export.server.ExportOutput[]
 * @param dsc cbit.vcell.server.DataSetController
 * @param timeSpecs cbit.vcell.export.server.TimeSpecs
 * @throws IOException
 */
private List<ExportOutput> exportParticleData(OutputContext outputContext, long jobID, User user, DataServerImpl dataServerImpl, ExportSpecs exportSpecs, ASCIISpecs asciiSpecs, FileDataContainerManager fileDataContainerManager) throws DataAccessException, IOException {
    VCDataIdentifier vcdID = exportSpecs.getVCDataIdentifier();
    TimeSpecs timeSpecs = exportSpecs.getTimeSpecs();
    String simID = vcdID.getID();
    String dataType = ".csv";
    // in switched format, how many rows for each particle
    final int N_PARTICLE_PIECES = 1;
    // get parameters
    boolean switchRowsColumns = asciiSpecs.getSwitchRowsColumns();
    double[] allTimes = timeSpecs.getAllTimes();
    int beginIndex = timeSpecs.getBeginTimeIndex();
    int endIndex = timeSpecs.getEndTimeIndex();
    ParticleDataBlock particleDataBlk = dataServerImpl.getParticleDataBlock(user, vcdID, allTimes[beginIndex]);
    VariableSpecs vs = exportSpecs.getVariableSpecs();
    VCAssert.assertValid(vs);
    String[] vnames = vs.getVariableNames();
    if (vnames.length == 0) {
        throw new IllegalArgumentException("No variables selected");
    }
    // need array for SimulationDescription, later
    final String[] currentVariableName = new String[1];
    ArrayList<ExportOutput> rval = new ArrayList<>(vnames.length);
    Set<String> species = particleDataBlk.getSpecies();
    ParticleProgress particleProgress = null;
    for (String vcellName : vnames) {
        String smoldynSpecies = null;
        for (int i = 0; smoldynSpecies == null && i < SMOLDYN_KEYWORDS_USED.length; i++) {
            SmoldynKeyword kw = SMOLDYN_KEYWORDS_USED[i];
            String smoldynName = SmoldynVCellMapper.vcellToSmoldyn(vcellName, kw);
            if (species.contains(smoldynName)) {
                smoldynSpecies = smoldynName;
            }
        }
        if (smoldynSpecies == null) {
            throw new DataAccessException("Unable to find match for variable name " + vcellName + " in " + StringUtils.join(species, ", "));
        }
        List<Coordinate> particles = particleDataBlk.getCoordinates(smoldynSpecies);
        int numberOfParticles = particles.size();
        int numberOfTimes = endIndex - beginIndex + 1;
        if (particleProgress != null) {
            particleProgress.nextName();
        } else {
            particleProgress = new ParticleProgress(jobID, vcdID, vnames.length, numberOfTimes, numberOfParticles);
        }
        // now make csv formatted data
        StringBuilder header = new StringBuilder();
        StringBuilder[] dataLines = null;
        final int NUMBER_HEADING_LINES = SwitchedRowsHeading.DATA.ordinal();
        if (switchRowsColumns) {
            dataLines = stringBuilderArray(numberOfParticles * N_PARTICLE_PIECES + NUMBER_HEADING_LINES);
            dataLines[SwitchedRowsHeading.TIME.ordinal()].append("Time,");
            final String particleLine = "Particle" + StringUtils.repeat(",x,y,z", numberOfTimes);
            dataLines[SwitchedRowsHeading.PARTICLE.ordinal()].append(particleLine);
            dataLines[SwitchedRowsHeading.XYZ.ordinal()].append("#,");
            // "first data line"
            final int FDL = SwitchedRowsHeading.DATA.ordinal();
            for (int i = 0; i < numberOfParticles; i++) {
                dataLines[FDL + N_PARTICLE_PIECES * i].append(i);
                dataLines[FDL + N_PARTICLE_PIECES * i].append(',');
            }
        } else {
            dataLines = stringBuilderArray(numberOfTimes);
        }
        currentVariableName[0] = vcellName;
        SimulationDescription simulationDescription = new SimulationDescription(outputContext, user, dataServerImpl, vcdID, false, currentVariableName);
        header.append(simulationDescription.getHeader(dataType));
        if (switchRowsColumns) {
        // implemented using first few data lines
        } else {
            header.append(",Time\n");
            header.append("Particle #,,");
            for (int k = 0; k < numberOfParticles; k++) {
                header.append(k + StringUtils.repeat(',', N_PARTICLE_PIECES));
            }
            header.append("\n,,");
            for (int k = 0; k < numberOfParticles; k++) {
                header.append("x,y,z,");
            }
        }
        final char COMMA = ',';
        int nextTimeIndex = particleProgress.nextTimeIndex(0, false);
        for (int i = beginIndex; i <= endIndex; i++) {
            particleDataBlk = dataServerImpl.getParticleDataBlock(user, vcdID, allTimes[i]);
            particles = particleDataBlk.getCoordinates(smoldynSpecies);
            if (i >= nextTimeIndex) {
                nextTimeIndex = particleProgress.nextTimeIndex(i, true);
            }
            if (switchRowsColumns) {
                // "first data line"
                final int FDL = SwitchedRowsHeading.DATA.ordinal();
                StringBuilder timeSb = dataLines[SwitchedRowsHeading.TIME.ordinal()];
                timeSb.append(allTimes[i]);
                timeSb.append(",,,");
                for (int j = 0; j < numberOfParticles; j++) {
                    StringBuilder sb = dataLines[FDL + N_PARTICLE_PIECES * j];
                    Coordinate coordinate = particles.get(j);
                    sb.append(coordinate.getX());
                    sb.append(COMMA);
                    sb.append(coordinate.getY());
                    sb.append(COMMA);
                    sb.append(coordinate.getZ());
                    sb.append(COMMA);
                }
            } else {
                StringBuilder particleSb = dataLines[i - beginIndex];
                particleSb.append(COMMA);
                particleSb.append(allTimes[i]);
                particleSb.append(COMMA);
                for (int j = 0; j < numberOfParticles; j++) {
                    Coordinate coordinate = particles.get(j);
                    particleSb.append(coordinate.getX());
                    particleSb.append(COMMA);
                    particleSb.append(coordinate.getY());
                    particleSb.append(COMMA);
                    particleSb.append(coordinate.getZ());
                }
            }
        }
        particleProgress.endOfTimes();
        final String dataID = vcellName + "_Particles";
        ExportOutput exportOutputCSV = new ExportOutput(true, dataType, simID, dataID, fileDataContainerManager);
        fileDataContainerManager.append(exportOutputCSV.getFileDataContainerID(), header.toString());
        int nextDataIndex = particleProgress.nextDataIndex(0, false);
        StringBuilder all = new StringBuilder();
        for (int i = 0; i < dataLines.length; i++) {
            final char NEWLINE = '\n';
            all.append(dataLines[i]);
            // kill reference to allow garbage collection
            dataLines[i] = null;
            all.append(NEWLINE);
            if (i >= nextDataIndex) {
                nextDataIndex = particleProgress.nextDataIndex(i, true);
            }
        }
        fileDataContainerManager.append(exportOutputCSV.getFileDataContainerID(), all.toString());
        rval.add(exportOutputCSV);
    }
    return rval;
}
Also used : ArrayList(java.util.ArrayList) SmoldynKeyword(org.vcell.solver.smoldyn.SmoldynVCellMapper.SmoldynKeyword) SinglePoint(cbit.vcell.geometry.SinglePoint) Coordinate(org.vcell.util.Coordinate) VCDataIdentifier(org.vcell.util.document.VCDataIdentifier) DataAccessException(org.vcell.util.DataAccessException) ParticleDataBlock(cbit.vcell.simdata.ParticleDataBlock)

Aggregations

SinglePoint (cbit.vcell.geometry.SinglePoint)1 ParticleDataBlock (cbit.vcell.simdata.ParticleDataBlock)1 ArrayList (java.util.ArrayList)1 SmoldynKeyword (org.vcell.solver.smoldyn.SmoldynVCellMapper.SmoldynKeyword)1 Coordinate (org.vcell.util.Coordinate)1 DataAccessException (org.vcell.util.DataAccessException)1 VCDataIdentifier (org.vcell.util.document.VCDataIdentifier)1