Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelParser.java @ 5811:67e07c29dd29
ImportTimeInterval: Verbosity increased and fixed bug
in getPeer when stoptime is null.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 24 Apr 2013 14:49:00 +0200 |
parents | b3dd14fc13a6 |
children |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import java.io.File; import java.io.IOException; import java.math.BigDecimal; 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.ImportRange; import de.intevation.flys.importer.ImportWst; import de.intevation.flys.importer.ImportWstColumn; import de.intevation.flys.importer.ImportWstColumnValue; import de.intevation.flys.importer.ImportWstQRange; /** * Parse CSV Waterlevel files. * As these waterlevels are probably used in fixation analysis * only, functionality to export them to "fixation"-wsts * has been added (the ImportWaterlevel*- stuff is actually * not needed to do so.) */ 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<ImportWst> waterlevels; private ImportWst current; /** The Waterlevel-Wst s will always have but one column. */ private ImportWstColumn column; /** The current (incomplete) Q Range. */ private ImportWstQRange currentQRange; /** The current (incomplete) km range for Q Range. */ private ImportRange currentRange; private String currentDescription; public WaterlevelParser() { waterlevels = new ArrayList<ImportWst>(); } public List<ImportWst> getWaterlevels() { return waterlevels; } @Override public void parse(File file) throws IOException { currentDescription = file.getName(); super.parse(file); } @Override protected void reset() { currentQRange = null; current = new ImportWst(currentDescription); current.setNumberColumns(1); column = current.getColumn(0); column.setName(currentDescription); column.setDescription(currentDescription); current.setKind(6); } @Override protected void finish() { if (current != null) { if (currentQRange != null) { List<ImportWstColumnValue> cValues = column.getColumnValues(); // Set end of range to last station. currentRange.setB(cValues.get(cValues.size() -1).getPosition()); currentQRange.setRange(currentRange); column.addColumnQRange(currentQRange); } 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); try { if (currentQRange != null) { // Finish off the last one. List<ImportWstColumnValue> cValues = column.getColumnValues(); // Set end of range to last station. currentRange.setB(cValues.get(cValues.size() -1).getPosition()); currentQRange.setRange(currentRange); column.addColumnQRange(currentQRange); } currentQRange = new ImportWstQRange(null, new BigDecimal(nf.parse(valueStr).doubleValue())); currentRange = new ImportRange(); log.debug("Found new Q range: Q=" + valueStr); return true; } catch (ParseException pe) { log.warn("Unparseable 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 { // Store the value and remember the position for QRange, if needed. Double station = nf.parse(cols[0]).doubleValue(); Double value = nf.parse(cols[1]).doubleValue(); BigDecimal stationBD = new BigDecimal(station); column.addColumnValue(stationBD, new BigDecimal(value)); if (currentRange.getA() == null) { currentRange.setA(stationBD); } } catch (ParseException pe) { log.warn("Unparseable number in data row: " + line); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :