From: luukkainen Date: Fri, 6 Jul 2012 12:28:23 +0000 (+0000) Subject: refs #3544 X-Git-Tag: simantics-1.7~16 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=219a46b97508ec80a063f976216563d7998d5ea0;p=simantics%2Fsysdyn.git refs #3544 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25282 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java b/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java index 314d1ae8..9571ee34 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java +++ b/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java @@ -39,6 +39,13 @@ public class MatFileReader implements ResultFileReader { return names; } + + public int getCount(String item) { + return dataHeader.columns; + } + /** + * Reads all data of a given item + */ public double[] readData(String item) throws IOException { readBytes = 0; Integer index = indices.get(item); @@ -47,7 +54,7 @@ public class MatFileReader implements ResultFileReader { InputStream in = new FileInputStream(file); skip(dataMark, in); - Object data = readColumn(dataHeader, index, in); + Object data = readRow(dataHeader, index, in); in.close(); @@ -55,6 +62,54 @@ public class MatFileReader implements ResultFileReader { } + /** + * Reads data of a given item + * @param item name of a item. + * @param start index of starting sample. + * @param count number of samples to read. + * @param skip number of samples to skip. 0 : all data is read. 1: every second sample is read. + * @return + * @throws IOException + */ + public double[] readData(String item, int start, int count, int skip) throws IOException { + readBytes = 0; + Integer index = indices.get(item); + if (index == null) + throw new IOException("Unknown item: " + item); + InputStream in = new FileInputStream(file); + skip(dataMark, in); + + Object data = readRow(dataHeader, index, start, count, skip, in); + + in.close(); + + return (double[])data; + + } + + public double[][] readData(List items) throws IOException { + readBytes = 0; + List indexes = new ArrayList(items.size()); + for (String item : items) { + Integer index = indices.get(item); + if (index == null) + throw new IOException("Unknown item: " + item); + indexes.add(index); + } + + InputStream in = new FileInputStream(file); + skip(dataMark, in); + + + + Object data = readRows(dataHeader, indexes, in); + + in.close(); + + return (double[][])data; + + } + private void readVariables() throws IOException { InputStream in = new FileInputStream(file); @@ -215,8 +270,7 @@ public class MatFileReader implements ResultFileReader { } } - private double[] readColumn(MatrixHeader header, int column, InputStream in)throws IOException { - boolean fast = true; + private double[] readRow(MatrixHeader header, int column, InputStream in)throws IOException { if (header.type != 0) throw new IOException("Only double type supported"); @@ -229,32 +283,137 @@ public class MatFileReader implements ResultFileReader { int sc = infoData[column * 4 + 1]; int c = sc > 0 ? sc-1 : 1-sc; - if (fast) { - skip(c * 8, in); - for (int j = 0; j < v.length; ++j) { - - double d = getDouble(in); - v[j] = d; - skip((rows-1)*8,in); - } - } else { - DoubleMatrix values = (DoubleMatrix)readMatrix(header, in); + skip(c * 8, in); + for (int j = 0; j < v.length; ++j) { - double[] valueData = values.data; + double d = getDouble(in); + v[j] = d; + skip((rows-1)*8,in); + } + + + if(sc < 0) + for(int j=0;j size) + throw new IndexOutOfBoundsException(); // TODO: we could just read available data, instead of throwing exception. + int rows = header.rows; + double[] v = new double[count]; + int sc = infoData[row * 4 + 1]; + int c = sc > 0 ? sc-1 : 1-sc; + - for(int j=0;j unSortedRows, InputStream in)throws IOException { + + + ; + + if (header.type != 0) + throw new IOException("Only double type supported"); + for (int row : unSortedRows) + if (infoData[row * 4] != 2) + throw new IOException(); // this is checked in initialization phase. + int size = header.columns; + int rows = header.rows; + int usc[] = new int[unSortedRows.size()]; + int uc[] = new int[unSortedRows.size()]; + double vs[][] = new double[unSortedRows.size()][]; + for (int i = 0; i < unSortedRows.size(); i++) { + int row = unSortedRows.get(i); + vs[i] = new double[size]; + usc[i] = infoData[row * 4 + 1]; + uc[i] = usc[i] > 0 ? usc[i]-1 : 1-usc[i]; + } + + Map cToRow = new HashMap(); + List sortedCs = new ArrayList(); + for (int i = 0; i < unSortedRows.size(); i++) { + cToRow.put(uc[i], unSortedRows.get(i)); + sortedCs.add(uc[i]); + } + + List sortedRows = new ArrayList(); + int sc[] = new int[unSortedRows.size()]; + int c[] = new int[unSortedRows.size()]; + + Collections.sort(sortedCs); + for (int i = 0; i < unSortedRows.size(); i++) { + int row = cToRow.get(sortedCs.get(i)); + sortedRows.add(row); + sc[i] = usc[unSortedRows.indexOf(row)]; + c[i] = uc[unSortedRows.indexOf(row)]; + } + + + + + skip(c[0] * 8, in); + for (int j = 0; j < size; ++j) { + for (int index = 0; index < sortedRows.size(); index++) { + + double d = getDouble(in); + vs[index][j] = d; + + if (index < sortedRows.size() - 1) { + skip((c[index+1] - c[index] -1 )*8, in); + } else { + skip((rows - c[index]-1 + c[0])*8,in); + } + //skip((rows-1)*8,in); + } + } + + + for (int i = 0; i < sortedRows.size(); i++) { + if(sc[i] < 0) + for(int j=0;j getNames(); + + /** + * Return number of samples. + * @param item + * @return + */ + public int getCount(String item); + + /** + * Reads all data of a given item + * @param item + * @return + * @throws IOException + */ public double[] readData(String item) throws IOException ; + + + /** + * Reads data of a given item + * @param item name of a item. + * @param start index of starting sample. + * @param count number of samples to read. + * @param skip number of samples to skip. 0 : all data is read. 1: every second sample is read. + * @return + * @throws IOException + */ + public double[] readData(String item, int start, int count, int skip) throws IOException ; + + /** + * Reads all data of given items. + * @param items names of the items. + * @return + * @throws IOException + */ + public double[][] readData(List items) throws IOException ; }