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
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
\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
}\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
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