]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Ability to make skipped & partial reads for multiple items at the same time.
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 7 Aug 2012 11:29:52 +0000 (11:29 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 7 Aug 2012 11:29:52 +0000 (11:29 +0000)
refs #3544

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25395 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java
org.simantics.modelica/src/org/simantics/modelica/reader/ResultFileReader.java

index 9571ee34909d1a33087cb3c69c8b0e2951bebd7d..491701710dc757b74725ec0a10d0cd0de9fb2072 100644 (file)
@@ -110,6 +110,31 @@ public class MatFileReader implements ResultFileReader {
 \r
        }\r
        \r
+       @Override\r
+       public double[][] readData(List<String> items, int start, int count,\r
+                       int skip) throws IOException {\r
+               readBytes = 0;\r
+               \r
+               List<Integer> indexes = new ArrayList<Integer>(items.size());\r
+               for (String item : items) {\r
+                       Integer index = indices.get(item);\r
+                       if (index == null)\r
+                               throw new IOException("Unknown item: " + item);\r
+                       indexes.add(index);\r
+               }\r
+               \r
+               InputStream in = new FileInputStream(file);\r
+               skip(dataMark, in);\r
+               \r
+               \r
+               \r
+               Object data = readRows(dataHeader, indexes,start,count,skip, in);\r
+               \r
+               in.close();\r
+               \r
+               return (double[][])data;\r
+       }\r
+       \r
        \r
        private void readVariables() throws IOException {\r
                InputStream in = new FileInputStream(file);\r
@@ -415,6 +440,112 @@ public class MatFileReader implements ResultFileReader {
        return vs;\r
     }\r
     \r
+       \r
+       private double[][] readRows(MatrixHeader header, List<Integer> unSortedRows, int start, int count, int skip, InputStream in)throws IOException {\r
+\r
+               \r
+               ;\r
+               \r
+               if (header.type != 0)\r
+               throw new IOException("Only double type supported");\r
+               for (int row : unSortedRows)\r
+                       if (infoData[row * 4] != 2)\r
+                               throw new IOException();  // this is checked in initialization phase.\r
+               \r
+               \r
+               \r
+               int size = header.columns;\r
+               int rows = header.rows;\r
+               \r
+               if (start+count*(skip+1) > size)\r
+                       throw new IndexOutOfBoundsException(); // TODO: we could just read available data, instead of throwing exception.\r
+               \r
+               int usc[] = new int[unSortedRows.size()];\r
+               int uc[] = new int[unSortedRows.size()];\r
+               double vs[][] = new double[unSortedRows.size()][];\r
+               for (int i = 0; i < unSortedRows.size(); i++) {\r
+                       int row = unSortedRows.get(i);\r
+                       vs[i] = new double[count];\r
+                       usc[i] = infoData[row * 4 + 1];\r
+                       uc[i] = usc[i] > 0 ? usc[i]-1 : 1-usc[i];\r
+               }\r
+               \r
+               Map<Integer, Integer> cToRow = new HashMap<Integer, Integer>();\r
+               List<Integer> sortedCs = new ArrayList<Integer>();\r
+               for (int i = 0; i < unSortedRows.size(); i++) {\r
+                       cToRow.put(uc[i], unSortedRows.get(i));\r
+                       sortedCs.add(uc[i]);\r
+               }\r
+               \r
+               List<Integer> sortedRows = new ArrayList<Integer>();\r
+               int sc[] = new int[unSortedRows.size()];\r
+               int c[] = new int[unSortedRows.size()];\r
+               \r
+               Collections.sort(sortedCs);\r
+               for (int i = 0; i < unSortedRows.size(); i++) {\r
+                       int row = cToRow.get(sortedCs.get(i));\r
+                       sortedRows.add(row);\r
+                       sc[i] = usc[unSortedRows.indexOf(row)];\r
+                       c[i] = uc[unSortedRows.indexOf(row)];\r
+               }\r
+       \r
+               \r
+               \r
+//             skip((c+start*rows) * 8, in);\r
+//     for (int j = 0; j < v.length; ++j) {\r
+//             \r
+//             double d = getDouble(in);\r
+//             v[j] = d;\r
+//             skip((rows*skip + rows-1)*8,in);\r
+//             }\r
+//\r
+//     \r
+//      if(sc < 0)\r
+//             for(int j=0;j<v.length;++j)\r
+//                 v[j] = -v[j];\r
+//         return v;\r
+               \r
+               skip((start*rows) * 8, in);\r
+       skip(c[0] * 8, in);\r
+       for (int j = 0; j < count; ++j) {\r
+               for (int index = 0; index < sortedRows.size(); index++) {\r
+                               \r
+                               double d = getDouble(in);\r
+                               vs[index][j] = d;\r
+                       \r
+                               if (index < sortedRows.size() - 1) {\r
+                                       skip((c[index+1] - c[index] -1 )*8, in);\r
+                               } else {\r
+                                       skip((rows - c[index]-1 + c[0])*8,in);\r
+                               }\r
+                               //skip((rows-1)*8,in);\r
+               }\r
+               if (skip > 0)\r
+                       skip((skip*rows)*8,in);\r
+               }\r
+\r
+       \r
+       for (int i = 0; i < sortedRows.size(); i++) {\r
+               if(sc[i] < 0)\r
+             for(int j=0;j<vs[i].length;++j)\r
+                 vs[i][j] = -vs[i][j];\r
+         \r
+       }\r
+       for (int i = 0; i < sortedRows.size(); i++) {\r
+               int row = sortedRows.get(i);\r
+               int rRow = unSortedRows.get(i);\r
+               if (row != rRow) {\r
+                       int ri = sortedRows.indexOf(rRow);\r
+                       double v[] = vs[i];\r
+                       vs[i] = vs[ri];\r
+                       vs[ri] = v;\r
+                       sortedRows.set(i, rRow);\r
+                       sortedRows.set(ri, row);\r
+               }\r
+       }\r
+       \r
+       return vs;\r
+    }\r
     private void skip(int skipBytes, InputStream in) throws IOException {\r
        int skip = 0; \r
        while (skip < skipBytes) {\r
index 9292f486c86b4d2ac1e4b17279562164fa4a28c6..2369d49b5c1c01291fc0c0557fbb2c06a77765b6 100644 (file)
@@ -45,5 +45,7 @@ public interface ResultFileReader {
         * @throws IOException\r
         */\r
        public double[][] readData(List<String> items) throws IOException ;\r
+       \r
+       public double[][] readData(List<String> items, int start, int count, int skip) throws IOException ;\r
 \r
 }\r