comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/FlowVelocityModelParser.java @ 2833:5b54a648f702

Finished flow velocity data import: finished parsing meta data of model files and repaired broken HQL statements. flys-backend/trunk@4259 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 18 Apr 2012 08:54:55 +0000
parents ac13e466a55e
children f63b39799d2d
comparison
equal deleted inserted replaced
2832:ac5bd90697c1 2833:5b54a648f702
6 import java.util.ArrayList; 6 import java.util.ArrayList;
7 import java.util.List; 7 import java.util.List;
8 import java.util.regex.Matcher; 8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern; 9 import java.util.regex.Pattern;
10 10
11 import java.text.ParseException;
12
13 import org.apache.log4j.Logger; 11 import org.apache.log4j.Logger;
14 12
13 import de.intevation.flys.importer.ImportDischargeZone;
15 import de.intevation.flys.importer.ImportFlowVelocityModel; 14 import de.intevation.flys.importer.ImportFlowVelocityModel;
16 import de.intevation.flys.importer.ImportFlowVelocityModelValue; 15 import de.intevation.flys.importer.ImportFlowVelocityModelValue;
17 16
18 17
19 public class FlowVelocityModelParser extends LineParser { 18 public class FlowVelocityModelParser extends LineParser {
21 private static final Logger log = 20 private static final Logger log =
22 Logger.getLogger(FlowVelocityModelParser.class); 21 Logger.getLogger(FlowVelocityModelParser.class);
23 22
24 private static final Pattern META_REGEX = 23 private static final Pattern META_REGEX =
25 Pattern.compile(".*Rechnung (.*) \\(Pegel (.*)\\).*"); 24 Pattern.compile(".*Rechnung (.*) \\(Pegel (.*)\\).*");
25
26 private static final Pattern META_GAUGE =
27 Pattern.compile("(.*) Q=(\\w*)m3/s");
28
29 private static final Pattern META_MAINVALUE_A =
30 Pattern.compile("([a-zA-Z]+)+(\\d+)*");
31
32 private static final Pattern META_MAINVALUE_B =
33 Pattern.compile("(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*)");
34
35 private static final Pattern META_MAINVALUE_C =
36 Pattern.compile("([0-9]++)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*)");
37
38 private static final Pattern META_MAINVALUE_D =
39 Pattern.compile("(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*)) bis (([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))");
40
41 private static final Pattern META_MAINVALUE_E =
42 Pattern.compile("(([a-zA-Z]+)+(\\d+)*) bis (([a-zA-Z]+)+(\\d+)*)");
26 43
27 private static final NumberFormat nf = 44 private static final NumberFormat nf =
28 NumberFormat.getInstance(DEFAULT_LOCALE); 45 NumberFormat.getInstance(DEFAULT_LOCALE);
29 46
30 47
63 handleDataLine(line); 80 handleDataLine(line);
64 } 81 }
65 } 82 }
66 83
67 84
68 public void handleMetaLine(String line) { 85 protected void handleMetaLine(String line) {
69 Matcher m = META_REGEX.matcher(line); 86 Matcher m = META_REGEX.matcher(line);
70 87
71 if (m.matches()) { 88 if (m.matches()) {
72 String zoneStr = m.group(1); 89 String mainValueStr = m.group(1);
73 String gaugeStr = m.group(2); 90 String gaugeStr = m.group(2);
74 91
75 log.debug("Found zone string: '" + zoneStr + "'"); 92 Object[] valueData = handleMainValueString(mainValueStr);
76 log.debug("Found gauge string: '" + gaugeStr + "'"); 93 Object[] gaugeData = handleGaugeString(gaugeStr);
77 94
78 // TODO Do something with these information 95 if (valueData == null || valueData.length < 2) {
79 } 96 log.warn("skip invalid MainValue part: '" + line + "'");
80 } 97 return;
81 98 }
82 99
83 public void handleDataLine(String line) { 100 if (gaugeData == null || gaugeData.length < 2) {
101 log.warn("skip invalid gauge part: '" + line + "'");
102 return;
103 }
104
105 if (log.isDebugEnabled()) {
106 log.debug("Found meta information:");
107 log.debug(" Gauge: " + gaugeData[0]);
108 log.debug(" Value: " + gaugeData[1]);
109 log.debug(" Lower: " + valueData[0]);
110 log.debug(" upper: " + valueData[1]);
111 }
112
113 current.setDischargeZone(new ImportDischargeZone(
114 (String) gaugeData[0],
115 (BigDecimal) gaugeData[1],
116 (String) valueData[0],
117 (String) valueData[1]
118 ));
119 }
120 }
121
122
123 protected Object[] handleMainValueString(String mainValueStr) {
124 Matcher mA = META_MAINVALUE_A.matcher(mainValueStr);
125 if (mA.matches()) {
126 String name = mA.group(0);
127
128 return new Object[] { name, name };
129 }
130
131 Matcher mB = META_MAINVALUE_B.matcher(mainValueStr);
132 if (mB.matches()) {
133 String lower = mB.group(1);
134 String upper = mB.group(4);
135
136 return new Object[] { lower, upper };
137 }
138
139 Matcher mC = META_MAINVALUE_C.matcher(mainValueStr);
140 if (mC.matches()) {
141 String facA = mC.group(1);
142 String nameA = mC.group(2);
143 String facB = mC.group(3);
144 String nameB = mC.group(4);
145
146 String fac = facA != null ? facA : facB;
147 String name = nameA != null ? nameA : nameB;
148
149 String mainValue = fac + " " + name;
150
151 return new Object[] { mainValue, mainValue };
152 }
153
154 Matcher mD = META_MAINVALUE_D.matcher(mainValueStr);
155 if (mD.matches()) {
156 String loFacA = mD.group(2);
157 String loNameA = mD.group(3);
158 String loFacB = mD.group(4);
159 String loNameB = mD.group(5);
160
161 String upFacA = mD.group(7);
162 String upNameA = mD.group(8);
163 String upFacB = mD.group(9);
164 String upNameB = mD.group(10);
165
166 String loFac = loFacA != null ? loFacA : loFacB;
167 String loName = loNameA != null ? loNameA : loNameB;
168
169 String upFac = upFacA != null ? upFacA : upFacB;
170 String upName = upNameA != null ? upNameA : upNameB;
171
172 String loMainValue = loFac + " " + loName;
173 String upMainValue = upFac + " " + upName;
174
175 return new Object[] { loMainValue, upMainValue };
176 }
177
178 Matcher mE = META_MAINVALUE_E.matcher(mainValueStr);
179 if (mE.matches()) {
180 String lower = mE.group(1);
181 String upper = mE.group(4);
182
183 return new Object[] { lower, upper };
184 }
185
186 return null;
187 }
188
189
190 protected Object[] handleGaugeString(String gaugeStr) {
191 Matcher m = META_GAUGE.matcher(gaugeStr);
192
193 if (m.matches()) {
194 String name = m.group(1);
195 String qStr = m.group(2);
196
197 try {
198 return new Object[] {
199 name,
200 new BigDecimal(nf.parse(qStr).doubleValue()) };
201 }
202 catch (ParseException pe) {
203 log.warn("Error while parsing Q value: '" + qStr + "'");
204 }
205 }
206
207 return null;
208 }
209
210
211 protected void handleDataLine(String line) {
84 String[] cols = line.split(SEPERATOR_CHAR); 212 String[] cols = line.split(SEPERATOR_CHAR);
85 213
86 if (cols.length < 5) { 214 if (cols.length < 5) {
87 log.warn("skip invalid data line: '" + line + "'"); 215 log.warn("skip invalid data line: '" + line + "'");
88 return; 216 return;

http://dive4elements.wald.intevation.org