1 package org.catacomb.dataview.formats;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6
7 import org.catacomb.report.E;
8
9 import java.util.StringTokenizer;
10
11 import java.util.ArrayList;
12
13 public class MeshDataReader implements DataReader {
14
15 File file;
16
17 double[][][] mesh;
18
19 double[][][] data;
20
21 double[] frameValues;
22
23 String[] specieNames;
24
25 int nvar = 0;
26
27 DataReader coReader;
28
29 public MeshDataReader(File f, DataReader dr) {
30 file = f;
31 coReader = dr;
32 }
33
34
35 public boolean canRead(String line) {
36 boolean ret = false;
37 if (line.startsWith("volumeGrid") || line.startsWith("gridConcentrations")) {
38 ret = true;
39 }
40 return ret;
41 }
42
43
44
45 public void read() {
46 ArrayList<double[][]> gcal = new ArrayList<double[][]>();
47 ArrayList<Double> gridTimes = new ArrayList<Double>();
48 specieNames = null;
49 try {
50 BufferedReader br = new BufferedReader(new FileReader(file));
51 br.readLine();
52
53 while (br.ready()) {
54 String line = br.readLine();
55 if (line.trim().length() == 0) {
56
57
58 } else if (line.startsWith("volumeGrid")) {
59 StringTokenizer st = new StringTokenizer(line, " ");
60 st.nextToken();
61 int nel = Integer.parseInt(st.nextToken());
62 E.info("reading the mesh " + nel);
63 readMesh(br, nel);
64
65
66 } else if (line.startsWith("gridConcentrations")) {
67 StringTokenizer st = new StringTokenizer(line, " ");
68 st.nextToken();
69 int nel = Integer.parseInt(st.nextToken());
70 int nspec = Integer.parseInt(st.nextToken());
71 double v = Double.parseDouble(st.nextToken());
72
73 gridTimes.add(new Double(v));
74 if (specieNames == null) {
75 specieNames = new String[nspec];
76 for (int i = 0; i < nspec; i++) {
77 specieNames[i] = st.nextToken();
78 }
79 }
80 gcal.add(readGridConcs(br, nel, nspec));
81
82 if (nvar <= 0) {
83 nvar = nspec;
84 }
85
86 } else if (coReader != null && coReader.canRead(line)) {
87 coReader.readBlock(line, br);
88
89 } else {
90 E.error("unknown content type " + line);
91 break;
92 }
93 }
94
95 } catch (Exception ex) {
96 E.error("Read error " + ex);
97 ex.printStackTrace();
98 }
99
100 int ng = gcal.size();
101 data = new double[ng][][];
102 frameValues = new double[ng];
103 for (int i = 0; i < ng; i++) {
104 frameValues[i] = gridTimes.get(i).doubleValue();
105 data[i] = gcal.get(i);
106 }
107 }
108
109
110
111 private void readMesh(BufferedReader br, int nel) {
112 mesh = new double[nel][][];
113 try {
114 for (int ilin = 0; ilin < nel; ilin++) {
115 String line = br.readLine();
116 double[] dat = parseLine(line);
117 int n = dat.length / 3;
118 mesh[ilin] = new double[3][n];
119 for (int i = 0; i < n; i++) {
120 mesh[ilin][0][i] = dat[3 * i];
121 mesh[ilin][1][i] = dat[3 * i + 1];
122 mesh[ilin][2][i] = dat[3 * i + 2];
123 }
124 }
125 } catch (Exception ex) {
126 E.error("ex " + ex);
127 }
128 }
129
130
131 private double[][] readGridConcs(BufferedReader br, int nel, int nspec) {
132 double[][] ret = new double[nspec][nel];
133 try {
134 for (int ilin = 0; ilin < nel; ilin++) {
135 String line = br.readLine();
136 double[] dl = parseLine(line);
137 for (int i = 0; i < nspec && i < dl.length; i++) {
138 ret[i][ilin] = dl[i];
139 }
140 }
141 } catch (Exception ex) {
142 E.error("ex " + ex);
143 }
144 return ret;
145 }
146
147
148
149
150 private double[] parseLine(String s) {
151 StringTokenizer st = new StringTokenizer(s, " ()");
152 int n = st.countTokens();
153
154 double[] ret = new double[n];
155 for (int i = 0; i < n; i++) {
156 ret[i] = Double.parseDouble(st.nextToken());
157 }
158 return ret;
159 }
160
161
162 public double[][][] getMesh() {
163 return mesh;
164 }
165
166
167 public double[][][] getData() {
168 return data;
169 }
170
171
172
173
174 public double[] getFrameValues() {
175 return frameValues;
176 }
177
178
179 public String[] getValueNames() {
180 String[] ret = specieNames;
181
182 if (ret == null) {
183 ret = new String[nvar];
184 for (int i = 0; i < nvar; i++) {
185 ret[i] = "species " + i;
186 }
187 }
188 return ret;
189 }
190
191
192 public void readBlock(String line, BufferedReader br) {
193
194
195 }
196
197
198
199 }