Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java @ 4447:a5993b69439b
Add processor to be able to generate curves for WKms values
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Wed, 07 Nov 2012 15:52:25 +0100 |
parents | f63b39799d2d |
children | 255898799ed9 |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import java.io.File; import java.io.IOException; import java.text.NumberFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import de.intevation.flys.importer.ImportUnit; import de.intevation.flys.importer.ImportWaterlevel; import de.intevation.flys.importer.ImportWaterlevelQRange; import de.intevation.flys.importer.ImportWaterlevelValue; public class WaterlevelParser extends LineParser { private static final Logger log = Logger.getLogger(WaterlevelParser.class); private static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); private static final Pattern META_Q_RANGE = Pattern.compile("Abfluss\\s\\[(.*)\\];(.*)"); public static final Pattern META_UNIT = Pattern.compile("^Einheit: \\[(.*)\\].*"); private List<ImportWaterlevel> waterlevels; private ImportWaterlevel current; private ImportWaterlevelQRange currentQ; private String currentDescription; public WaterlevelParser() { waterlevels = new ArrayList<ImportWaterlevel>(); } public List<ImportWaterlevel> getWaterlevels() { return waterlevels; } @Override public void parse(File file) throws IOException { currentDescription = file.getName(); super.parse(file); } @Override protected void reset() { currentQ = null; current = new ImportWaterlevel(currentDescription); } @Override protected void finish() { if (current != null) { if (currentQ != null) { current.addValue(currentQ); } waterlevels.add(current); } } @Override protected void handleLine(int lineNum, String line) { if (line.startsWith(START_META_CHAR)) { handleMetaLine(stripMetaLine(line)); return; } else if (handleQRange(line)) { return; } else { handleDataLine(line); return; } } private void handleMetaLine(String meta) { Matcher m = META_UNIT.matcher(meta); if (m.matches()) { String unit = m.group(1); log.debug("Found unit: '" + unit + "'"); current.setUnit(new ImportUnit(unit)); } } private boolean handleQRange(String line) { Matcher m = META_Q_RANGE.matcher(line); if (m.matches()) { String unitStr = m.group(1); String valueStr = m.group(2); if (currentQ != null) { if (current != null) { current.addValue(currentQ); } else { // this should never happen log.warn("Try to add Q range without waterlevel!"); } } try { log.debug("Found new Q range: Q=" + valueStr); currentQ = new ImportWaterlevelQRange( nf.parse(valueStr).doubleValue()); return true; } catch (ParseException pe) { log.warn("Error while parsing Q range: '" + line + "'"); } } return false; } private void handleDataLine(String line) { String[] cols = line.split(SEPERATOR_CHAR); if (cols == null || cols.length < 2) { log.warn("skip invalid waterlevel line: '" + line + "'"); return; } try { Double station = nf.parse(cols[0]).doubleValue(); Double value = nf.parse(cols[1]).doubleValue(); currentQ.addValue(new ImportWaterlevelValue(station, value)); } catch (ParseException pe) { log.warn("Error while parsing number values: '" + line + "'"); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :