]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #3544
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 6 Jul 2012 12:28:23 +0000 (12:28 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 6 Jul 2012 12:28:23 +0000 (12:28 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25282 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 314d1ae8933b446f92a1bb46dc03a202f06c4ccf..9571ee34909d1a33087cb3c69c8b0e2951bebd7d 100644 (file)
@@ -39,6 +39,13 @@ public class MatFileReader implements ResultFileReader {
                return names;\r
        }\r
        \r
+       \r
+       public int getCount(String item) {\r
+               return dataHeader.columns;\r
+       }\r
+       /**\r
+        * Reads all data of a given item\r
+        */\r
        public double[] readData(String item) throws IOException {\r
                readBytes = 0;\r
                Integer index = indices.get(item);\r
@@ -47,7 +54,7 @@ public class MatFileReader implements ResultFileReader {
                InputStream in = new FileInputStream(file);\r
                skip(dataMark, in);\r
                \r
-               Object data = readColumn(dataHeader, index, in);\r
+               Object data = readRow(dataHeader, index, in);\r
                \r
                in.close();\r
                \r
@@ -55,6 +62,54 @@ public class MatFileReader implements ResultFileReader {
 \r
        }\r
        \r
+       /**\r
+        * Reads data of a given item\r
+        * @param item name of a item.\r
+        * @param start index of starting sample.\r
+        * @param count number of samples to read.\r
+        * @param skip number of samples to skip. 0 : all data is read. 1: every second sample is read.\r
+        * @return\r
+        * @throws IOException\r
+        */\r
+       public double[] readData(String item, int start, int count, int skip) throws IOException {\r
+               readBytes = 0;\r
+               Integer index = indices.get(item);\r
+               if (index == null)\r
+                       throw new IOException("Unknown item: " + item);\r
+               InputStream in = new FileInputStream(file);\r
+               skip(dataMark, in);\r
+               \r
+               Object data = readRow(dataHeader, index, start, count, skip, in);\r
+               \r
+               in.close();\r
+               \r
+               return (double[])data;\r
+\r
+       }\r
+       \r
+       public double[][] readData(List<String> items) throws IOException {\r
+               readBytes = 0;\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, in);\r
+               \r
+               in.close();\r
+               \r
+               return (double[][])data;\r
+\r
+       }\r
+       \r
        \r
        private void readVariables() throws IOException {\r
                InputStream in = new FileInputStream(file);\r
@@ -215,8 +270,7 @@ public class MatFileReader implements ResultFileReader {
         }\r
     }\r
     \r
-       private double[] readColumn(MatrixHeader header, int column, InputStream in)throws IOException {\r
-               boolean fast = true;\r
+       private double[] readRow(MatrixHeader header, int column, InputStream in)throws IOException {\r
 \r
                if (header.type != 0)\r
                throw new IOException("Only double type supported");\r
@@ -229,32 +283,137 @@ public class MatFileReader implements ResultFileReader {
                int sc = infoData[column * 4 + 1];\r
                int c = sc > 0 ? sc-1 : 1-sc;\r
                \r
-       if (fast) {\r
 \r
-               skip(c * 8, in);\r
-               for (int j = 0; j < v.length; ++j) {\r
-                       \r
-                       double d = getDouble(in);\r
-                       v[j] = d;\r
-                       skip((rows-1)*8,in);\r
-                       }\r
 \r
-       } else {\r
-               DoubleMatrix values = (DoubleMatrix)readMatrix(header, in);\r
+       skip(c * 8, in);\r
+       for (int j = 0; j < v.length; ++j) {\r
                \r
-               double[] valueData = values.data;\r
+               double d = getDouble(in);\r
+               v[j] = d;\r
+               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
+       \r
+       private double[] readRow(MatrixHeader header, int row, int start, int count, int skip, InputStream in)throws IOException {\r
+\r
+               if (header.type != 0)\r
+               throw new IOException("Only double type supported");\r
+               \r
+               if (infoData[row * 4] != 2)\r
+                       throw new IOException();  // this is checked in initialization phase.\r
+               int size = header.columns;\r
+               if (start+count*(skip+1) > size)\r
+                       throw new IndexOutOfBoundsException(); // TODO: we could just read available data, instead of throwing exception.\r
+               int rows = header.rows;\r
+               double[] v = new double[count];\r
+               int sc = infoData[row * 4 + 1];\r
+               int c = sc > 0 ? sc-1 : 1-sc;\r
+               \r
 \r
 \r
-              for(int j=0;j<v.length;++j) {\r
-                  v[j] = valueData[rows * j + c];\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
+       \r
         if(sc < 0)\r
              for(int j=0;j<v.length;++j)\r
                  v[j] = -v[j];\r
          return v;\r
     }\r
+       \r
+       private double[][] readRows(MatrixHeader header, List<Integer> unSortedRows, 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
+               int size = header.columns;\r
+               int rows = header.rows;\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[size];\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
+\r
+       skip(c[0] * 8, in);\r
+       for (int j = 0; j < size; ++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
+               }\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
     \r
     private void skip(int skipBytes, InputStream in) throws IOException {\r
        int skip = 0; \r
index 008074dae1e10574e9c2501b2e33dedc2e3333d0..9292f486c86b4d2ac1e4b17279562164fa4a28c6 100644 (file)
@@ -5,7 +5,45 @@ import java.util.List;
 \r
 public interface ResultFileReader {\r
        \r
+       /**\r
+        * List items in the file.\r
+        * @return\r
+        */\r
        public List<String> getNames();\r
+       \r
+       /**\r
+        * Return number of samples.\r
+        * @param item\r
+        * @return\r
+        */\r
+       public int getCount(String item);\r
+       \r
+       /**\r
+        *  Reads all data of a given item\r
+        * @param item\r
+        * @return\r
+        * @throws IOException\r
+        */\r
        public double[] readData(String item) throws IOException ;\r
+       \r
+\r
+       /**\r
+        * Reads data of a given item\r
+        * @param item name of a item.\r
+        * @param start index of starting sample.\r
+        * @param count number of samples to read.\r
+        * @param skip number of samples to skip. 0 : all data is read. 1: every second sample is read.\r
+        * @return\r
+        * @throws IOException\r
+        */\r
+       public double[] readData(String item, int start, int count, int skip) throws IOException ;\r
+       \r
+       /**\r
+        * Reads all data of given items.\r
+        * @param items names of the items.\r
+        * @return\r
+        * @throws IOException\r
+        */\r
+       public double[][] readData(List<String> items) throws IOException ;\r
 \r
 }\r