comparison flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java @ 200:88048d4f6e4d

Add a hack to repair the units in WST files extraction. flys-backend/trunk@1542 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Mar 2011 10:46:58 +0000
parents ed38839a6b08
children 3169b559ca3c
comparison
equal deleted inserted replaced
199:ed38839a6b08 200:88048d4f6e4d
14 14
15 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
16 16
17 import de.intevation.flys.utils.StringUtil; 17 import de.intevation.flys.utils.StringUtil;
18 18
19 import java.util.regex.Pattern;
20 import java.util.regex.Matcher;
21
19 public class WstParser 22 public class WstParser
20 { 23 {
21 private static Logger log = Logger.getLogger(WstParser.class); 24 private static Logger log = Logger.getLogger(WstParser.class);
22 25
23 public static final String COLUMN_BEZ_TEXT = "column-bez-text"; 26 public static final String COLUMN_BEZ_TEXT = "column-bez-text";
26 public static final String COLUMN_DATUM = "column-datum"; 29 public static final String COLUMN_DATUM = "column-datum";
27 30
28 public static final Double UNDEFINED_ZERO = Double.valueOf(0.0); 31 public static final Double UNDEFINED_ZERO = Double.valueOf(0.0);
29 32
30 public static final String ENCODING = "ISO-8859-1"; 33 public static final String ENCODING = "ISO-8859-1";
34
35 public static final Pattern UNIT_COMMENT =
36 Pattern.compile("\\*\\s*[kK][mM]\\s+(.+)");
37
38 public static final Pattern UNIT =
39 Pattern.compile("[^\\[]*\\[([^]]+)\\].*");
31 40
32 public WstParser() { 41 public WstParser() {
33 } 42 }
34 43
35 public void parse(File file) throws IOException { 44 public void parse(File file) throws IOException {
62 boolean bFirstComment = true; 71 boolean bFirstComment = true;
63 boolean columnHeaderChecked = false; 72 boolean columnHeaderChecked = false;
64 73
65 double lastKm = Double.MAX_VALUE; 74 double lastKm = Double.MAX_VALUE;
66 75
67 String einheit = "Wassserstand [NN + m]"; 76 String einheit = "Wasserstand [NN + m]";
68 77
69 HashMap<String, Double> oldEscapeLine = null; 78 HashMap<String, Double> oldEscapeLine = null;
70 79
71 while ((input = in.readLine()) != null) { 80 while ((input = in.readLine()) != null) {
72 String line = input; 81 String line = input;
165 daten = StringUtil.splitQuoted(spezial, '"'); 174 daten = StringUtil.splitQuoted(spezial, '"');
166 } 175 }
167 continue; 176 continue;
168 } 177 }
169 178
179 if (line.length() < 11) {
180 continue;
181 }
182
170 if (line.startsWith("*")) { 183 if (line.startsWith("*")) {
171 if (bFirstComment && line.length() >= 11) { 184 Matcher m = UNIT_COMMENT.matcher(line);
172 String yAxis = line.substring(10).trim(); 185 if (m.matches()) {
173 if (yAxis.length() > 0) { 186 log.debug("unit comment found");
174 einheit = yAxis; 187 // XXX: This hack is needed because desktop
175 } 188 // FLYS is broken figuring out the unit
176 bFirstComment = false; 189 String [] units = m.group(1).split("\\s{2,}");
190 m = UNIT.matcher(units[0]);
191 einheit = m.matches() ? m.group(1) : units[0];
192 log.debug("unit: " + einheit);
177 } 193 }
178 continue; 194 continue;
179 } 195 }
180 196
181 if (firstAbfluesse != null) { 197 if (firstAbfluesse != null) {
185 if (lsBezeichner[i] == null 201 if (lsBezeichner[i] == null
186 || lsBezeichner[i].length() == 0) { 202 || lsBezeichner[i].length() == 0) {
187 double q = firstAbfluesse[i]; 203 double q = firstAbfluesse[i];
188 if (q < 0.001) { 204 if (q < 0.001) {
189 lsBezeichner[i] = 205 lsBezeichner[i] =
190 "<unbekannt#" + unknownCount + ">"; 206 "<unbekannt #" + unknownCount + ">";
191 ++unknownCount; 207 ++unknownCount;
192 } 208 }
193 else { 209 else {
194 lsBezeichner[i] = "Q="+format(q); 210 lsBezeichner[i] = "Q="+format(q);
195 } 211 }
196 } 212 }
197 lsHeader[i] = lsBezeichner[i] + " " + einheit; 213 lsHeader[i] = lsBezeichner[i];
198 } 214 }
199 columnHeaderChecked = true; 215 columnHeaderChecked = true;
200 } 216 }
201 217
202 Double [] data = 218 Double [] data =
274 protected void addInterval( 290 protected void addInterval(
275 double from, 291 double from,
276 double to, 292 double to,
277 Map<String, Double> values 293 Map<String, Double> values
278 ) { 294 ) {
279 log.debug("addInterval: " + from + " " + to);
280 if (values == null) { 295 if (values == null) {
281 return; 296 return;
297 }
298 if (log.isDebugEnabled()) {
299 log.debug("addInterval: " + from + " " + to);
300 StringBuilder sb = new StringBuilder();
301 for (Map.Entry<String, Double> entry: values.entrySet()) {
302 if (sb.length() > 0) {
303 sb.append("; ");
304 }
305 sb.append('"').append(entry.getKey())
306 .append("\": ")
307 .append(entry.getValue());
308 }
309 log.debug("entries: " + sb.toString());
282 } 310 }
283 } 311 }
284 312
285 private static final Double [] parseLineAsDouble( 313 private static final Double [] parseLineAsDouble(
286 String line, 314 String line,

http://dive4elements.wald.intevation.org