comparison backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java @ 8025:c915e99d9e52

Renamed SedimentYield to SedimentLoadLS and SedimentYieldValue to SedimentLoadLSValue. Adjust the names of the parsers and the rest of the glue.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 09 Jul 2014 18:13:13 +0200
parents
children 6954ac9b7591
comparison
equal deleted inserted replaced
8024:963ede7b32bb 8025:c915e99d9e52
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.importer.parsers;
10
11 import java.io.File;
12 import java.io.IOException;
13
14 import java.text.NumberFormat;
15 import java.text.ParseException;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.apache.log4j.Logger;
23
24 import org.dive4elements.river.importer.ImportGrainFraction;
25 import org.dive4elements.river.importer.ImportSedimentYield;
26 import org.dive4elements.river.importer.ImportSedimentLoadLSValue;
27 import org.dive4elements.river.importer.ImportTimeInterval;
28 import org.dive4elements.river.importer.ImportUnit;
29 import org.dive4elements.river.model.GrainFraction;
30 import org.dive4elements.river.utils.DateUtil;
31
32
33 /** Parses Sediment Yield files. */
34 public class SedimentLoadLSParser extends LineParser {
35
36 private static final Logger log =
37 Logger.getLogger(SedimentLoadLSParser.class);
38
39
40 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
41
42
43 public static final String FRAKTION_START = "Fraktion:";
44
45 public static final String FRACTION_COARSE_STR =
46 ".*Grobkorn.*";
47
48 public static final String FRACTION_FINE_MIDDLE_STR =
49 ".*Fein.Mittel.Kies.*";
50
51 public static final String FRACTION_SAND =
52 ".*Sand.*";
53
54 public static final String FRACTION_SUSP_SAND =
55 ".*susp.Sand.*";
56
57 public static final String FRACTION_SUSP_SAND_BED =
58 ".*bettbild.Anteil.susp.Sand.*";
59
60 public static final String FRACTION_SUSP_SAND_BED_EPOCH =
61 ".*susp.Sand.bettbildAnteil.*";
62
63 public static final String FRACTION_SUSPENDED_SEDIMENT =
64 ".*Schwebstoff.*";
65
66 public static final String FRACTION_TOTAL =
67 ".*gesamt.*";
68
69
70 public static final Pattern TIMEINTERVAL_SINGLE =
71 Pattern.compile("\\D*([0-9]+?)\\D*");
72
73 public static final Pattern TIMEINTERVAL_EPOCH =
74 Pattern.compile("\\D*([0-9]+?)\\s*-\\s*([0-9]+?)\\D*");
75
76 public static final Pattern META_FRACTION =
77 Pattern.compile("^Fraktion: (.*)");
78
79 public static final Pattern META_UNIT =
80 Pattern.compile("^Einheit: \\[(.*)\\].*");
81
82 public static final Pattern META_COLUMN_NAMES =
83 Pattern.compile("^Fluss-km.*");
84
85 public static final Pattern META_GRAIN_FRACTION_A =
86 Pattern.compile("\\D*(([0-9]+?,[0-9]+?)\\s*-|([0-9]++)\\s*-)(([0-9]+?,[0-9]+?)|([0-9]++))\\s*([a-zA-Z]+?)\\W*\\D*");
87
88 public static final Pattern META_GRAIN_FRACTION_B =
89 Pattern.compile("(<|>){1}\\s*(\\w++)\\s*(([0-9]+?,[0-9]+?)\\s*-|([0-9]++)\\s*-)(([0-9]+?,[0-9]+?)|([0-9]++))\\s*([a-zA-Z]+?)");
90
91 public static final Pattern META_GRAIN_FRACTION_C =
92 Pattern.compile("(<|>){1}\\s*((([0-9]+?,[0-9]+?)|([0-9]++))\\s*(\\w+))");
93
94
95 protected List<ImportSedimentYield> sedimentYields;
96
97 protected ImportSedimentYield[] current;
98
99 protected ImportGrainFraction grainFraction;
100
101 protected ImportUnit unit;
102
103 protected String description;
104
105 protected String[] columnNames;
106
107
108 public SedimentLoadLSParser() {
109 sedimentYields = new ArrayList<ImportSedimentYield>();
110 }
111
112
113 @Override
114 public void parse(File file) throws IOException {
115 description = file.getName();
116
117 super.parse(file);
118 }
119
120
121 @Override
122 protected void reset() {
123 current = null;
124 grainFraction = null;
125 unit = null;
126 }
127
128
129 @Override
130 protected void finish() {
131 if (current != null) {
132 for (ImportSedimentYield isy: current) {
133 sedimentYields.add(isy);
134 }
135 }
136
137 description = null;
138 }
139
140
141 @Override
142 protected void handleLine(int lineNum, String line) {
143 if (line.startsWith(START_META_CHAR)) {
144 handleMetaLine(stripMetaLine(line));
145 }
146 else {
147 handleDataLine(line);
148 }
149 }
150
151
152 protected void handleMetaLine(String line) {
153 if (handleMetaUnit(line)) {
154 return;
155 }
156 else if (handleMetaFraction(line)) {
157 return;
158 }
159 else if (handleColumnNames(line)) {
160 return;
161 }
162 else {
163 log.warn("SYP: Unknown meta line: '" + line + "'");
164 }
165 }
166
167
168 protected boolean handleMetaUnit(String line) {
169 Matcher m = META_UNIT.matcher(line);
170
171 if (m.matches()) {
172 unit = new ImportUnit(m.group(1));
173 return true;
174 }
175
176 return false;
177 }
178
179
180 public boolean handleMetaFraction(String line) {
181 Matcher m = META_FRACTION.matcher(line);
182
183 if (m.matches()) {
184 String tmp = m.group(1);
185
186 this.grainFraction = buildGrainFraction(tmp);
187
188 return true;
189 }
190 else if (line.startsWith(FRAKTION_START)) {
191 String newLine = line.replace(FRAKTION_START, "").trim();
192 if (newLine.length() == 0) {
193 log.debug("Found total grain fraction.");
194 this.grainFraction = new ImportGrainFraction(GrainFraction.UNKNOWN);
195
196 return true;
197 }
198 }
199
200 return false;
201 }
202
203
204 public boolean handleColumnNames(String line) {
205 Matcher m = META_COLUMN_NAMES.matcher(line);
206
207 if (m.matches()) {
208 columnNames = line.split(SEPERATOR_CHAR);
209
210 initializeSedimentYields();
211
212 return true;
213 }
214
215 return false;
216 }
217
218
219 protected void handleDataLine(String line) {
220 String[] vals = line.split(SEPERATOR_CHAR);
221
222 if (vals == null || vals.length < columnNames.length-1) {
223 log.warn("SYP: skip invalid data line: '" + line + "'");
224 return;
225 }
226
227 try {
228 Double km = nf.parse(vals[0]).doubleValue();
229
230 for (int i = 1, n = columnNames.length-1; i < n; i++) {
231 String curVal = vals[i];
232
233 if (curVal != null && curVal.length() > 0) {
234 current[i-1].addValue(new ImportSedimentLoadLSValue(
235 km, nf.parse(vals[i]).doubleValue()
236 ));
237 }
238 }
239 }
240 catch (ParseException pe) {
241 log.warn("SYP: unparseable number in data row '" + line + "':", pe);
242 }
243 }
244
245
246 /** Initialize SedimentYields from columns, set the kind
247 * with respect to file location (offical epoch or not?) */
248 private void initializeSedimentYields() {
249 // skip first column (Fluss-km) and last column (Hinweise)
250 current = new ImportSedimentYield[columnNames.length-2];
251
252 Integer kind;
253
254 if (inputFile.getAbsolutePath().contains("amtliche Epochen")) {
255 kind = new Integer(1);
256 }
257 else {
258 kind = new Integer(0);
259 }
260
261 for (int i = 0, n = columnNames.length; i < n-2; i++) {
262 current[i] = new ImportSedimentYield(this.description);
263 current[i].setTimeInterval(getTimeInterval(columnNames[i+1]));
264 current[i].setUnit(unit);
265 current[i].setGrainFraction(grainFraction);
266 current[i].setKind(kind);
267 }
268 }
269
270
271 private ImportTimeInterval getTimeInterval(String column) {
272 try {
273 Matcher a = TIMEINTERVAL_EPOCH.matcher(column);
274 if (a.matches()) {
275 int yearA = nf.parse(a.group(1)).intValue();
276 int yearB = nf.parse(a.group(2)).intValue();
277
278 return new ImportTimeInterval(
279 DateUtil.getStartDateFromYear(yearA),
280 DateUtil.getEndDateFromYear(yearB)
281 );
282 }
283
284 Matcher b = TIMEINTERVAL_SINGLE.matcher(column);
285 if (b.matches()) {
286 int year = nf.parse(b.group(1)).intValue();
287
288 return new ImportTimeInterval(DateUtil.getStartDateFromYear(year));
289 }
290
291 log.warn("SYP: Unknown time interval string: '" + column + "'");
292 }
293 catch (ParseException pe) {
294 log.warn("SYP: Could not parse years: " + column, pe);
295 }
296
297 return null;
298 }
299
300
301 private ImportGrainFraction buildGrainFraction(String gfStr) {
302 Matcher a = META_GRAIN_FRACTION_A.matcher(gfStr);
303 if (a.matches()) {
304 String lowerA = a.group(2);
305 String lowerB = a.group(3);
306
307 String upperA = a.group(4);
308 String upperB = a.group(5);
309
310 String lower = lowerA != null ? lowerA : lowerB;
311 String upper = upperA != null ? upperA : upperB;
312
313 try {
314 return new ImportGrainFraction(
315 getGrainFractionTypeName(this.description),
316 nf.parse(lower).doubleValue(),
317 nf.parse(upper).doubleValue()
318 );
319 }
320 catch (ParseException pe) {
321 log.warn("SYP: Could not parse ranges of: '" + gfStr + "'");
322 }
323 }
324
325 Matcher b = META_GRAIN_FRACTION_B.matcher(gfStr);
326 if (b.matches()) {
327 String lowerA = b.group(4);
328 String lowerB = b.group(5);
329 String upperA = b.group(6);
330 String upperB = b.group(7);
331
332 String lower = lowerA != null ? lowerA : lowerB;
333 String upper = upperA != null ? upperA : upperB;
334
335 try {
336 return new ImportGrainFraction(
337 getGrainFractionTypeName(this.description),
338 nf.parse(lower).doubleValue(),
339 nf.parse(upper).doubleValue()
340 );
341 }
342 catch (ParseException pe) {
343 log.warn("SYP: Could not parse ranges of: '" + gfStr + "'");
344 }
345 }
346
347 Matcher c = META_GRAIN_FRACTION_C.matcher(gfStr);
348 if (c.matches()) {
349 String oper = c.group(1);
350 String valueStr = c.group(3);
351
352 try {
353 Double value = nf.parse(valueStr).doubleValue();
354
355 if (oper.equals(">")) {
356 return new ImportGrainFraction(
357 getGrainFractionTypeName(this.description),
358 value,
359 null
360 );
361 }
362 else {
363 return new ImportGrainFraction(
364 getGrainFractionTypeName(this.description),
365 null,
366 value
367 );
368 }
369 }
370 catch (ParseException pe) {
371 log.warn("SYP: Could not parse ranges of: '" + gfStr + "'");
372 }
373 }
374
375 log.warn("SYP: Unknown grain fraction: '" + gfStr + "'");
376 return new ImportGrainFraction(GrainFraction.UNKNOWN);
377 }
378
379
380 public static String getGrainFractionTypeName(String filename) {
381 if (Pattern.matches(FRACTION_COARSE_STR, filename)) {
382 return GrainFraction.COARSE;
383 }
384 else if (Pattern.matches(FRACTION_FINE_MIDDLE_STR, filename)) {
385 return GrainFraction.FINE_MIDDLE;
386 }
387 else if (Pattern.matches(FRACTION_SUSP_SAND_BED, filename) ||
388 Pattern.matches(FRACTION_SUSP_SAND_BED_EPOCH, filename)) {
389 return GrainFraction.SUSP_SAND_BED;
390 }
391 else if (Pattern.matches(FRACTION_SUSP_SAND, filename)) {
392 return GrainFraction.SUSP_SAND;
393 }
394 else if (Pattern.matches(FRACTION_SAND, filename)) {
395 return GrainFraction.SAND;
396 }
397 else if (Pattern.matches(FRACTION_SUSPENDED_SEDIMENT, filename)) {
398 return GrainFraction.SUSPENDED_SEDIMENT;
399 }
400 else if (Pattern.matches(FRACTION_TOTAL, filename)) {
401 return GrainFraction.TOTAL;
402 }
403 else {
404 log.warn("SYP: Unknown grain fraction '" + filename + "'");
405 return GrainFraction.UNKNOWN;
406 }
407 }
408
409
410 public List<ImportSedimentYield> getSedimentYields() {
411 return sedimentYields;
412 }
413 }
414 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org