Mercurial > dive4elements > river
comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java @ 5828:dfb26b03b179
Moved directories to org.dive4elements.river
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:53:11 +0200 |
parents | flys-backend/src/main/java/de/intevation/flys/importer/parsers/FlowVelocityModelParser.java@e8469247dc6b |
children | 18619c1e7c2a |
comparison
equal
deleted
inserted
replaced
5827:e308d4ecd35a | 5828:dfb26b03b179 |
---|---|
1 package de.intevation.flys.importer.parsers; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 | |
6 import java.math.BigDecimal; | |
7 import java.text.NumberFormat; | |
8 import java.text.ParseException; | |
9 import java.util.ArrayList; | |
10 import java.util.List; | |
11 import java.util.TreeSet; | |
12 import java.util.regex.Matcher; | |
13 import java.util.regex.Pattern; | |
14 | |
15 import org.apache.log4j.Logger; | |
16 | |
17 import de.intevation.flys.importer.ImportDischargeZone; | |
18 import de.intevation.flys.importer.ImportFlowVelocityModel; | |
19 import de.intevation.flys.importer.ImportFlowVelocityModelValue; | |
20 import de.intevation.flys.utils.EpsilonComparator; | |
21 | |
22 | |
23 public class FlowVelocityModelParser extends LineParser { | |
24 | |
25 private static final Logger log = | |
26 Logger.getLogger(FlowVelocityModelParser.class); | |
27 | |
28 private static final Pattern META_REGEX = | |
29 Pattern.compile(".*Rechnung (.*) \\(Pegel (.*)\\).*"); | |
30 | |
31 private static final Pattern META_GAUGE = | |
32 Pattern.compile("(.*) Q=(\\w*)m3/s"); | |
33 | |
34 private static final Pattern META_MAINVALUE_A = | |
35 Pattern.compile("([a-zA-Z]+)+(\\d+)*"); | |
36 | |
37 private static final Pattern META_MAINVALUE_B = | |
38 Pattern.compile("(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*)"); | |
39 | |
40 private static final Pattern META_MAINVALUE_C = | |
41 Pattern.compile("([0-9]++)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*)"); | |
42 | |
43 private static final Pattern META_MAINVALUE_D = | |
44 Pattern.compile("(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*)) bis (([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))"); | |
45 | |
46 private static final Pattern META_MAINVALUE_E = | |
47 Pattern.compile("(([a-zA-Z]+)+(\\d+)*) bis (([a-zA-Z]+)+(\\d+)*)"); | |
48 | |
49 private static final NumberFormat nf = | |
50 NumberFormat.getInstance(DEFAULT_LOCALE); | |
51 | |
52 | |
53 private List<ImportFlowVelocityModel> models; | |
54 | |
55 private ImportFlowVelocityModel current; | |
56 | |
57 protected String description; | |
58 | |
59 protected TreeSet<Double> kmExists; | |
60 | |
61 | |
62 public FlowVelocityModelParser() { | |
63 models = new ArrayList<ImportFlowVelocityModel>(); | |
64 kmExists = new TreeSet<Double>(EpsilonComparator.CMP); | |
65 } | |
66 | |
67 | |
68 public List<ImportFlowVelocityModel> getModels() { | |
69 return models; | |
70 } | |
71 | |
72 @Override | |
73 public void parse(File file) throws IOException { | |
74 description = file.getName(); | |
75 | |
76 super.parse(file); | |
77 } | |
78 | |
79 @Override | |
80 protected void reset() { | |
81 current = new ImportFlowVelocityModel(description); | |
82 kmExists.clear(); | |
83 } | |
84 | |
85 | |
86 @Override | |
87 protected void finish() { | |
88 models.add(current); | |
89 | |
90 // description = null; | |
91 } | |
92 | |
93 | |
94 @Override | |
95 protected void handleLine(int lineNum, String line) { | |
96 if (line.startsWith(START_META_CHAR)) { | |
97 handleMetaLine(stripMetaLine(line)); | |
98 } | |
99 else { | |
100 handleDataLine(line); | |
101 } | |
102 } | |
103 | |
104 | |
105 protected void handleMetaLine(String line) { | |
106 Matcher m = META_REGEX.matcher(line); | |
107 | |
108 if (m.matches()) { | |
109 String mainValueStr = m.group(1); | |
110 String gaugeStr = m.group(2); | |
111 | |
112 Object[] valueData = handleMainValueString(mainValueStr); | |
113 Object[] gaugeData = handleGaugeString(gaugeStr); | |
114 | |
115 if (valueData == null || valueData.length < 2) { | |
116 log.warn("skip invalid MainValue part: '" + line + "'"); | |
117 return; | |
118 } | |
119 | |
120 if (gaugeData == null || gaugeData.length < 2) { | |
121 log.warn("skip invalid gauge part: '" + line + "'"); | |
122 return; | |
123 } | |
124 | |
125 if (log.isDebugEnabled()) { | |
126 log.debug("Found meta information:"); | |
127 log.debug(" Gauge: " + gaugeData[0]); | |
128 log.debug(" Value: " + gaugeData[1]); | |
129 log.debug(" Lower: " + valueData[0]); | |
130 log.debug(" upper: " + valueData[1]); | |
131 } | |
132 | |
133 current.setDischargeZone(new ImportDischargeZone( | |
134 (String) gaugeData[0], | |
135 (BigDecimal) gaugeData[1], | |
136 (String) valueData[0], | |
137 (String) valueData[1] | |
138 )); | |
139 } | |
140 } | |
141 | |
142 | |
143 protected Object[] handleMainValueString(String mainValueStr) { | |
144 Matcher mA = META_MAINVALUE_A.matcher(mainValueStr); | |
145 if (mA.matches()) { | |
146 String name = mA.group(0); | |
147 | |
148 return new Object[] { name, name }; | |
149 } | |
150 | |
151 Matcher mB = META_MAINVALUE_B.matcher(mainValueStr); | |
152 if (mB.matches()) { | |
153 String lower = mB.group(1); | |
154 String upper = mB.group(4); | |
155 | |
156 return new Object[] { lower, upper }; | |
157 } | |
158 | |
159 Matcher mC = META_MAINVALUE_C.matcher(mainValueStr); | |
160 if (mC.matches()) { | |
161 String facA = mC.group(1); | |
162 String nameA = mC.group(2); | |
163 String facB = mC.group(3); | |
164 String nameB = mC.group(4); | |
165 | |
166 String fac = facA != null ? facA : facB; | |
167 String name = nameA != null ? nameA : nameB; | |
168 | |
169 String mainValue = fac + " " + name; | |
170 | |
171 return new Object[] { mainValue, mainValue }; | |
172 } | |
173 | |
174 Matcher mD = META_MAINVALUE_D.matcher(mainValueStr); | |
175 if (mD.matches()) { | |
176 String loFacA = mD.group(2); | |
177 String loNameA = mD.group(3); | |
178 String loFacB = mD.group(4); | |
179 String loNameB = mD.group(5); | |
180 | |
181 String upFacA = mD.group(7); | |
182 String upNameA = mD.group(8); | |
183 String upFacB = mD.group(9); | |
184 String upNameB = mD.group(10); | |
185 | |
186 String loFac = loFacA != null ? loFacA : loFacB; | |
187 String loName = loNameA != null ? loNameA : loNameB; | |
188 | |
189 String upFac = upFacA != null ? upFacA : upFacB; | |
190 String upName = upNameA != null ? upNameA : upNameB; | |
191 | |
192 String loMainValue = loFac + " " + loName; | |
193 String upMainValue = upFac + " " + upName; | |
194 | |
195 return new Object[] { loMainValue, upMainValue }; | |
196 } | |
197 | |
198 Matcher mE = META_MAINVALUE_E.matcher(mainValueStr); | |
199 if (mE.matches()) { | |
200 String lower = mE.group(1); | |
201 String upper = mE.group(4); | |
202 | |
203 return new Object[] { lower, upper }; | |
204 } | |
205 | |
206 return null; | |
207 } | |
208 | |
209 | |
210 protected Object[] handleGaugeString(String gaugeStr) { | |
211 Matcher m = META_GAUGE.matcher(gaugeStr); | |
212 | |
213 if (m.matches()) { | |
214 String name = m.group(1); | |
215 String qStr = m.group(2); | |
216 | |
217 try { | |
218 return new Object[] { | |
219 name, | |
220 new BigDecimal(nf.parse(qStr).doubleValue()) }; | |
221 } | |
222 catch (ParseException pe) { | |
223 log.warn("Could not parse Q value: '" + qStr + "'"); | |
224 } | |
225 } | |
226 | |
227 return null; | |
228 } | |
229 | |
230 | |
231 protected void handleDataLine(String line) { | |
232 String[] cols = line.split(SEPERATOR_CHAR); | |
233 | |
234 if (cols.length < 5) { | |
235 log.warn("skip invalid data line: '" + line + "'"); | |
236 return; | |
237 } | |
238 | |
239 try { | |
240 double km = nf.parse(cols[0]).doubleValue(); | |
241 | |
242 Double key = Double.valueOf(km); | |
243 | |
244 if (kmExists.contains(key)) { | |
245 log.warn("duplicate stattion '" + km + "': -> ignored"); | |
246 return; | |
247 } | |
248 | |
249 double q = nf.parse(cols[1]).doubleValue(); | |
250 double total = nf.parse(cols[2]).doubleValue(); | |
251 double main = nf.parse(cols[3]).doubleValue(); | |
252 double stress = nf.parse(cols[4]).doubleValue(); | |
253 | |
254 current.addValue(new ImportFlowVelocityModelValue( | |
255 new BigDecimal(km), | |
256 new BigDecimal(q), | |
257 new BigDecimal(total), | |
258 new BigDecimal(main), | |
259 new BigDecimal(stress) | |
260 )); | |
261 | |
262 kmExists.add(key); | |
263 } | |
264 catch (ParseException pe) { | |
265 log.warn("Unparseable flow velocity values:", pe); | |
266 } | |
267 } | |
268 } | |
269 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |