Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java @ 474:5d920695a7f0
Import of q ranges of wst files was totally broken.
flys-backend/trunk@1715 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 18 Apr 2011 11:06:08 +0000 |
parents | d37ccb04ab5d |
children | f9653fc8ef7b |
comparison
equal
deleted
inserted
replaced
473:70de916d3e3a | 474:5d920695a7f0 |
---|---|
28 public static final String COLUMN_BEZ_TEXT = "column-bez-text"; | 28 public static final String COLUMN_BEZ_TEXT = "column-bez-text"; |
29 public static final String COLUMN_BEZ_BREITE = "column-bez-breite"; | 29 public static final String COLUMN_BEZ_BREITE = "column-bez-breite"; |
30 public static final String COLUMN_QUELLE = "column-quelle"; | 30 public static final String COLUMN_QUELLE = "column-quelle"; |
31 public static final String COLUMN_DATUM = "column-datum"; | 31 public static final String COLUMN_DATUM = "column-datum"; |
32 | 32 |
33 public static final Double UNDEFINED_ZERO = Double.valueOf(0.0); | 33 public static final BigDecimal UNDEFINED_ZERO = |
34 new BigDecimal(0.0); | |
35 public static final BigDecimal MIN_RANGE = | |
36 new BigDecimal(-Double.MAX_VALUE); | |
37 public static final BigDecimal MAX_RANGE = | |
38 new BigDecimal(Double.MAX_VALUE); | |
34 | 39 |
35 public static final String ENCODING = "ISO-8859-1"; | 40 public static final String ENCODING = "ISO-8859-1"; |
36 | 41 |
37 public static final Pattern UNIT_COMMENT = | 42 public static final Pattern UNIT_COMMENT = |
38 Pattern.compile("\\*\\s*[kK][mM]\\s+(.+)"); | 43 Pattern.compile("\\*\\s*[kK][mM]\\s+(.+)"); |
68 | 73 |
69 String input; | 74 String input; |
70 boolean first = true; | 75 boolean first = true; |
71 int columnCount = 0; | 76 int columnCount = 0; |
72 | 77 |
73 String [] lsHeader = null; | |
74 String [] lsBezeichner = null; | 78 String [] lsBezeichner = null; |
75 String [] langBezeichner = null; | 79 String [] langBezeichner = null; |
76 int [] colNaWidths = null; | 80 int [] colNaWidths = null; |
77 String [] quellen = null; | 81 String [] quellen = null; |
78 String [] daten = null; | 82 String [] daten = null; |
79 double [] aktAbfluesse = null; | 83 |
80 double [] firstAbfluesse = null; | 84 BigDecimal [] aktAbfluesse = null; |
81 | 85 BigDecimal [] firstAbfluesse = null; |
82 double minKm = Double.MAX_VALUE; | 86 |
83 double maxKm = -Double.MAX_VALUE; | 87 BigDecimal minKm = MAX_RANGE; |
88 BigDecimal maxKm = MIN_RANGE; | |
84 | 89 |
85 boolean columnHeaderChecked = false; | 90 boolean columnHeaderChecked = false; |
86 | 91 |
87 double lastKm = Double.MAX_VALUE; | |
88 | |
89 String einheit = "Wasserstand [NN + m]"; | 92 String einheit = "Wasserstand [NN + m]"; |
90 | 93 |
91 HashMap<String, Double> oldEscapeLine = null; | 94 //HashMap<String, Double> oldEscapeLine = null; |
92 | 95 |
93 while ((input = in.readLine()) != null) { | 96 while ((input = in.readLine()) != null) { |
94 String line = input; | 97 String line = input; |
95 if (first) { // fetch number of columns | 98 if (first) { // fetch number of columns |
96 if ((line = line.trim()).length() == 0) { | 99 if ((line = line.trim()).length() == 0) { |
101 if (columnCount <= 0) { | 104 if (columnCount <= 0) { |
102 throw new NumberFormatException( | 105 throw new NumberFormatException( |
103 "number columns <= 0"); | 106 "number columns <= 0"); |
104 } | 107 } |
105 log.debug("Number of columns: " + columnCount); | 108 log.debug("Number of columns: " + columnCount); |
109 wst.setNumberColumns(columnCount); | |
106 lsBezeichner = new String[columnCount]; | 110 lsBezeichner = new String[columnCount]; |
107 lsHeader = new String[columnCount]; | |
108 aktAbfluesse = new double[columnCount]; | |
109 } | 111 } |
110 catch (NumberFormatException nfe) { | 112 catch (NumberFormatException nfe) { |
111 log.warn(nfe); | 113 log.warn(nfe); |
112 continue; | 114 continue; |
113 } | 115 } |
116 } | 118 } |
117 | 119 |
118 line = line.replace(',', '.'); | 120 line = line.replace(',', '.'); |
119 | 121 |
120 if (line.startsWith("*\u001f")) { | 122 if (line.startsWith("*\u001f")) { |
121 Double [] data = | 123 BigDecimal [] data = |
122 parseLineAsDouble(line, columnCount, false, true); | 124 parseLineAsDouble(line, columnCount, false, true); |
123 | 125 |
124 if (oldEscapeLine != null) { | 126 if (aktAbfluesse != null) { |
125 addInterval(minKm, maxKm, oldEscapeLine); | 127 addInterval(minKm, maxKm, aktAbfluesse); |
126 minKm = Double.MAX_VALUE; | 128 minKm = MAX_RANGE; |
127 maxKm = -Double.MAX_VALUE; | 129 maxKm = MIN_RANGE; |
128 } | 130 } |
129 | 131 |
130 oldEscapeLine = new HashMap<String, Double>(); | 132 aktAbfluesse = new BigDecimal[columnCount]; |
131 for (int i = 0; i < columnCount; ++i) { | 133 log.debug("new q range: " + columnCount); |
132 if (lsHeader[i] != null) { | 134 for (int i = 0; i < Math.min(columnCount, data.length); ++i) { |
133 oldEscapeLine.put(lsHeader[i], data[i]); | 135 if (data[i] != null) { |
134 } | 136 log.debug(" column: " + data[i]); |
135 } | 137 aktAbfluesse[i] = data[i]; |
136 | 138 } |
137 for (int i = Math.min(data.length, aktAbfluesse.length)-1; | |
138 i >= 0; --i) { | |
139 aktAbfluesse[i] = data[i].doubleValue(); | |
140 } | 139 } |
141 | 140 |
142 if (firstAbfluesse == null) { | 141 if (firstAbfluesse == null) { |
143 firstAbfluesse = (double [])aktAbfluesse.clone(); | 142 firstAbfluesse = (BigDecimal [])aktAbfluesse.clone(); |
144 } | 143 } |
145 continue; | 144 continue; |
146 } | 145 } |
147 | 146 |
148 if (line.startsWith("*!")) { | 147 if (line.startsWith("*!")) { |
208 } | 207 } |
209 | 208 |
210 if (firstAbfluesse != null) { | 209 if (firstAbfluesse != null) { |
211 if (!columnHeaderChecked) { | 210 if (!columnHeaderChecked) { |
212 int unknownCount = 0; | 211 int unknownCount = 0; |
213 for (int i = 0; i < lsHeader.length; ++i) { | 212 for (int i = 0; i < lsBezeichner.length; ++i) { |
214 if (lsBezeichner[i] == null | 213 if (lsBezeichner[i] == null |
215 || lsBezeichner[i].length() == 0) { | 214 || lsBezeichner[i].length() == 0) { |
216 double q = firstAbfluesse[i]; | 215 double q = firstAbfluesse[i].doubleValue(); |
217 if (q < 0.001) { | 216 if (q < 0.001) { |
218 lsBezeichner[i] = | 217 lsBezeichner[i] = |
219 "<unbekannt #" + unknownCount + ">"; | 218 "<unbekannt #" + unknownCount + ">"; |
220 ++unknownCount; | 219 ++unknownCount; |
221 } | 220 } |
222 else { | 221 else { |
223 lsBezeichner[i] = "Q="+format(q); | 222 lsBezeichner[i] = "Q="+format(q); |
224 } | 223 } |
225 } | 224 } |
226 lsHeader[i] = lsBezeichner[i]; | 225 wst.getColumn(i).setName(lsBezeichner[i]); |
227 } | 226 } |
228 columnHeaderChecked = true; | 227 columnHeaderChecked = true; |
229 } | 228 } |
230 | 229 |
231 Double [] data = | 230 BigDecimal [] data = |
232 parseLineAsDouble(line, columnCount, true, false); | 231 parseLineAsDouble(line, columnCount, true, false); |
233 | 232 |
234 double kaem = data[0]; | 233 BigDecimal kaem = data[0]; |
235 | 234 |
236 if (kaem < minKm) { | 235 if (kaem.compareTo(minKm) < 0) { |
237 minKm = kaem; | 236 minKm = kaem; |
238 } | 237 } |
239 if (kaem > maxKm) { | 238 if (kaem.compareTo(maxKm) > 0) { |
240 maxKm = kaem; | 239 maxKm = kaem; |
241 } | 240 } |
242 | |
243 lastKm = kaem; | |
244 | 241 |
245 // extract values | 242 // extract values |
246 for (int i = 0; i < columnCount; ++i) { | 243 for (int i = 0; i < columnCount; ++i) { |
247 addValue(kaem, data[i].doubleValue(), lsBezeichner[i]); | 244 addValue(kaem, data[i], i); |
248 } | 245 } |
249 | 246 |
250 } | 247 } |
251 else { // firstAbfluesse == null | 248 else { // firstAbfluesse == null |
252 if (langBezeichner != null) { | 249 if (langBezeichner != null) { |
278 } | 275 } |
279 } | 276 } |
280 } | 277 } |
281 | 278 |
282 } | 279 } |
283 addInterval(minKm, maxKm, oldEscapeLine); | 280 addInterval(minKm, maxKm, aktAbfluesse); |
284 } | 281 } |
285 finally { | 282 finally { |
286 if (in != null) { | 283 if (in != null) { |
287 in.close(); | 284 in.close(); |
288 } | 285 } |
289 } | 286 } |
290 } | 287 } |
291 | 288 |
292 protected void addValue(double km, double w, String columnName) { | 289 protected void addValue(BigDecimal km, BigDecimal w, int index) { |
293 ImportWstColumn column = wst.getColumn(columnName); | 290 if (w != null) { |
294 column.addColumnValue(new BigDecimal(km), new BigDecimal(w)); | 291 ImportWstColumn column = wst.getColumn(index); |
292 column.addColumnValue(km, w); | |
293 } | |
295 } | 294 } |
296 | 295 |
297 private static final NumberFormat NF = getNumberFormat(); | 296 private static final NumberFormat NF = getNumberFormat(); |
298 | 297 |
299 private static final NumberFormat getNumberFormat() { | 298 private static final NumberFormat getNumberFormat() { |
306 protected static String format(double value) { | 305 protected static String format(double value) { |
307 return NF.format(value); | 306 return NF.format(value); |
308 } | 307 } |
309 | 308 |
310 protected void addInterval( | 309 protected void addInterval( |
311 double from, | 310 BigDecimal from, |
312 double to, | 311 BigDecimal to, |
313 Map<String, Double> values | 312 BigDecimal [] values |
314 ) { | 313 ) { |
315 log.debug("addInterval: " + from + " " + to); | 314 log.debug("addInterval: " + from + " " + to); |
316 | 315 |
317 if (values == null) { | 316 if (values == null || from == MAX_RANGE) { |
318 return; | 317 return; |
319 } | 318 } |
320 | 319 |
321 if (from > to) { double t = from; from = to; to = t; } | 320 if (to.compareTo(from) < 0) { |
322 | 321 BigDecimal t = from; from = to; to = t; |
323 ImportRange range = new ImportRange( | 322 } |
324 new BigDecimal(from), | 323 |
325 new BigDecimal(to)); | 324 ImportRange range = new ImportRange(from, to); |
326 | 325 |
327 for (Map.Entry<String, Double> entry: values.entrySet()) { | 326 for (int i = 0; i < values.length; ++i) { |
328 BigDecimal q = new BigDecimal(entry.getValue()); | 327 ImportWstColumn column = wst.getColumn(i); |
329 ImportWstQRange wstQRange = new ImportWstQRange(range, q); | 328 ImportWstQRange wstQRange = new ImportWstQRange(range, values[i]); |
330 | |
331 String columnName = entry.getKey(); | |
332 ImportWstColumn column = wst.getColumn(columnName); | |
333 column.addColumnQRange(wstQRange); | 329 column.addColumnQRange(wstQRange); |
334 } | 330 } |
335 } | 331 } |
336 | 332 |
337 private static final Double [] parseLineAsDouble( | 333 private static final BigDecimal [] parseLineAsDouble( |
338 String line, | 334 String line, |
339 int count, | 335 int count, |
340 boolean bStation, | 336 boolean bStation, |
341 boolean bParseEmptyAsZero | 337 boolean bParseEmptyAsZero |
342 ) { | 338 ) { |
343 String [] tokens = parseLine(line, count, bStation); | 339 String [] tokens = parseLine(line, count, bStation); |
344 | 340 |
345 Double [] doubles = new Double[tokens.length]; | 341 BigDecimal [] doubles = new BigDecimal[tokens.length]; |
346 | 342 |
347 for (int i = 0; i < doubles.length; ++i) { | 343 for (int i = 0; i < doubles.length; ++i) { |
348 String token = tokens[i].trim(); | 344 String token = tokens[i].trim(); |
349 if (token.length() != 0) { | 345 if (token.length() != 0) { |
350 doubles[i] = Double.valueOf(token); | 346 doubles[i] = new BigDecimal(token); |
351 } | 347 } |
352 else if (bParseEmptyAsZero) { | 348 else if (bParseEmptyAsZero) { |
353 doubles[i] = UNDEFINED_ZERO; | 349 doubles[i] = UNDEFINED_ZERO; |
354 } | 350 } |
355 } | 351 } |