comparison backend/src/main/java/org/dive4elements/river/importer/parsers/PorosityParser.java @ 7840:02711de579cc

Added model, parser and importer for porosities.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 30 Apr 2014 14:11:29 +0200
parents
children 8c6ed23ce315 cdef048c4ac5
comparison
equal deleted inserted replaced
7839:3f6b9fae1637 7840:02711de579cc
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 org.dive4elements.river.importer.ImportDepth;
12 import org.dive4elements.river.importer.ImportPorosity;
13 import org.dive4elements.river.importer.ImportPorosityValue;
14 import org.dive4elements.river.importer.ImportSedimentDensity;
15 import org.dive4elements.river.importer.ImportSedimentDensityValue;
16 import org.dive4elements.river.importer.ImportTimeInterval;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import java.math.BigDecimal;
22
23 import java.text.NumberFormat;
24 import java.text.ParseException;
25
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.apache.log4j.Logger;
34
35 public class PorosityParser extends LineParser {
36
37 private static final Logger log =
38 Logger.getLogger(PorosityParser.class);
39
40 public static final NumberFormat nf =
41 NumberFormat.getInstance(DEFAULT_LOCALE);
42
43 public static final Pattern META_DEPTH =
44 Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
45
46 public static final Pattern META_TIMEINTERVAL =
47 Pattern.compile("^Zeitraum: (\\d{4})-(\\d{4}).*");
48
49 protected List<ImportPorosity> porosities;
50
51 protected ImportPorosity current;
52
53 protected String currentDescription;
54
55 public PorosityParser() {
56 porosities = new ArrayList<ImportPorosity>();
57 }
58
59
60 @Override
61 public void parse(File file) throws IOException {
62 currentDescription = file.getName();
63
64 super.parse(file);
65 }
66
67
68 @Override
69 protected void reset() {
70 current = new ImportPorosity(currentDescription);
71 }
72
73
74 @Override
75 protected void finish() {
76 if (current != null) {
77 porosities.add(current);
78 }
79 }
80
81
82 @Override
83 protected void handleLine(int lineNum, String line) {
84 if (line.startsWith(START_META_CHAR)) {
85 handleMetaLine(stripMetaLine(line));
86 }
87 else {
88 handleDataLine(line);
89 }
90 }
91
92
93 protected void handleMetaLine(String line) {
94 if (handleMetaDepth(line)) {
95 return;
96 }
97 if (handleMetaTimeInterval(line)) {
98 return;
99 }
100 log.warn("Unknown meta line: '" + line + "'");
101 }
102
103 protected boolean handleMetaTimeInterval(String line) {
104 Matcher m = META_TIMEINTERVAL.matcher(line);
105
106 if (m.matches()) {
107 String lo = m.group(1);
108 String up = m.group(2);
109
110 log.debug("Found time interval: " + lo + " - " + up);
111
112 try {
113 int lower = Integer.valueOf(lo);
114 int upper = Integer.valueOf(up);
115
116 Date fromYear = LineParser.getStartDateFromYear(lower);
117 Date toYear = LineParser.getEndDateFromYear(upper);
118
119 current.setTimeInterval(new ImportTimeInterval(fromYear, toYear));
120 }
121 catch (NumberFormatException e) {
122 log.warn("PP: could not parse timeinterval", e);
123 }
124
125 return true;
126 }
127
128 return false;
129 }
130
131 protected boolean handleMetaDepth(String line) {
132 Matcher m = META_DEPTH.matcher(line);
133
134 if (m.matches()) {
135 String lo = m.group(1);
136 String up = m.group(2);
137
138 log.info("Found porosity depth: " + lo + " - " + up + " cm");
139
140 try {
141 ImportDepth depth = new ImportDepth(
142 new BigDecimal(nf.parse(lo).doubleValue()),
143 new BigDecimal(nf.parse(up).doubleValue())
144 );
145
146 current.setDepth(depth);
147
148 return true;
149 }
150 catch (ParseException pe) {
151 log.warn("Unparseable numbers in: '" + line + "'");
152 }
153 }
154 else {
155 log.debug("Meta line doesn't contain depth information: " + line);
156 }
157
158 return false;
159 }
160
161 protected void handleDataLine(String line) {
162 String[] vals = line.split(SEPERATOR_CHAR);
163 log.debug("handle line: " + line);
164
165 if (vals == null || vals.length < 3) {
166 log.warn("skip invalid data line: '" + line + "'");
167 return;
168 }
169
170 BigDecimal km = null;
171 BigDecimal shoreOffset = null;
172 BigDecimal porosity = null;
173 try {
174 km = new BigDecimal(nf.parse(vals[0]).doubleValue());
175 porosity = new BigDecimal(nf.parse(vals[2]).doubleValue());
176 if (!vals[1].isEmpty()) {
177 shoreOffset = new BigDecimal(nf.parse(vals[1]).doubleValue());
178 }
179 }
180 catch (ParseException pe) {
181 log.warn("Unparseable numbers in '" + line + "'");
182 }
183
184 if (km == null || porosity == null) {
185 log.warn("PP: No km nor porosity given. Skip line");
186 return;
187 }
188 log.debug("add new value.");
189 current.addValue(new ImportPorosityValue(
190 km,
191 shoreOffset,
192 porosity,
193 currentDescription));
194 }
195
196
197 public List<ImportPorosity> getPorosities() {
198 return porosities;
199 }
200 }
201 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org