teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer.parsers; ingo@2844: ingo@2845: import java.io.File; ingo@2845: import java.io.IOException; felix@5049: import java.math.BigDecimal; ingo@2845: import java.text.NumberFormat; ingo@2845: import java.util.ArrayList; ingo@2844: import java.util.List; ingo@2845: import java.util.regex.Matcher; ingo@2845: import java.util.regex.Pattern; ingo@2844: ingo@2845: import org.apache.log4j.Logger; mschaefer@8989: import org.dive4elements.river.backend.utils.DateUtil; mschaefer@8989: import org.dive4elements.river.importer.ImportRange; felix@6319: import org.dive4elements.river.importer.ImportTimeInterval; teichmann@5829: import org.dive4elements.river.importer.ImportUnit; teichmann@5829: import org.dive4elements.river.importer.ImportWst; teichmann@5829: import org.dive4elements.river.importer.ImportWstColumn; teichmann@5829: import org.dive4elements.river.importer.ImportWstColumnValue; teichmann@5829: import org.dive4elements.river.importer.ImportWstQRange; mschaefer@8989: import org.dive4elements.river.importer.common.AbstractParser; ingo@2844: felix@5049: felix@5049: /** felix@5049: * Parse CSV Waterlevel files. felix@5049: * As these waterlevels are probably used in fixation analysis felix@5049: * only, functionality to export them to "fixation"-wsts felix@5049: * has been added (the ImportWaterlevel*- stuff is actually felix@5049: * not needed to do so.) felix@5049: */ ingo@2844: public class WaterlevelParser extends LineParser { ingo@2844: ingo@2845: private static final Logger log = Logger.getLogger(WaterlevelParser.class); ingo@2845: ingo@2845: private static final NumberFormat nf = mschaefer@8989: NumberFormat.getInstance(DEFAULT_LOCALE); ingo@2845: ingo@2845: private static final Pattern META_Q_RANGE = mschaefer@8989: Pattern.compile("Abfluss\\s\\[(.*)\\];(.*)"); ingo@2845: ingo@2845: public static final Pattern META_UNIT = mschaefer@8989: Pattern.compile("^Einheit: \\[(.*)\\].*"); ingo@2845: mschaefer@8989: public static final BigDecimal INTERVAL_GAP = new BigDecimal("0.00001"); tom@7377: mschaefer@8989: private final List waterlevels; ingo@2844: felix@5240: private ImportWst current; felix@5240: felix@5240: /** The Waterlevel-Wst s will always have but one column. */ felix@5240: private ImportWstColumn column; ingo@2845: felix@5240: /** The current (incomplete) Q Range. */ felix@5240: private ImportWstQRange currentQRange; felix@5240: felix@5240: /** The current (incomplete) km range for Q Range. */ felix@5240: private ImportRange currentRange; felix@5240: ingo@2845: private String currentDescription; ingo@2845: ingo@2845: ingo@2844: public WaterlevelParser() { mschaefer@8989: this.waterlevels = new ArrayList<>(); ingo@2844: } ingo@2844: ingo@2844: felix@5240: public List getWaterlevels() { mschaefer@8989: return this.waterlevels; ingo@2844: } ingo@2844: ingo@2844: ingo@2844: @Override mschaefer@8989: public void parse(final File file) throws IOException { mschaefer@8989: this.currentDescription = file.getName(); ingo@2845: ingo@2845: super.parse(file); ingo@2845: } ingo@2845: ingo@2845: ingo@2845: @Override ingo@2844: protected void reset() { mschaefer@8989: this.currentQRange = null; mschaefer@8989: this.current = new ImportWst(this.currentDescription); mschaefer@8989: this.current.setNumberColumns(1); mschaefer@8989: this.column = this.current.getColumn(0); mschaefer@8989: this.column.setName(this.currentDescription); mschaefer@8989: this.column.setDescription(this.currentDescription); felix@6319: felix@6319: // Try to extract and set the TimeInterval. mschaefer@8989: final Matcher m = WaterlevelDifferencesParser.YEARS_IN_COLUMN.matcher( mschaefer@8989: this.currentDescription); felix@6319: felix@6319: if (m.matches()) { mschaefer@8989: final int startYear = Integer.parseInt(m.group(1)); mschaefer@8989: final int endYear = Integer.parseInt(m.group(2)); mschaefer@8989: final ImportTimeInterval time = new ImportTimeInterval( mschaefer@8989: DateUtil.getStartDateFromYear(startYear), mschaefer@8989: DateUtil.getEndDateFromYear(endYear) mschaefer@8989: ); mschaefer@8989: this.column.setTimeInterval(time); felix@6319: } else { tom@8856: log.debug("No time interval in column header found: " mschaefer@8989: + this.currentDescription); felix@6319: } felix@6319: mschaefer@8989: this.current.setKind(7); ingo@2844: } ingo@2844: ingo@2844: ingo@2844: @Override ingo@2844: protected void finish() { mschaefer@8989: if (this.current != null) { mschaefer@8989: if (this.currentQRange != null) { mschaefer@8989: final List cValues = this.column.getColumnValues(); tom@7377: // Set end of range to last station tom@7377: // or expand range to minimal length in case it would be 0 teichmann@7883: // TODO: should otherwise be extended to teichmann@7883: // (first station of next range - INTERVAL_GAP), tom@7377: // assuming always ascending stations mschaefer@8989: final BigDecimal lastStation = cValues.get(cValues.size() -1) mschaefer@8989: .getPosition(); mschaefer@8989: if (lastStation.compareTo(this.currentRange.getA()) == 0) { mschaefer@8989: this.currentRange.setB(lastStation.add(INTERVAL_GAP)); tom@7377: } tom@7377: else { mschaefer@8989: this.currentRange.setB(lastStation); tom@7377: } tom@7377: mschaefer@8989: this.currentQRange.setRange(this.currentRange); mschaefer@8989: this.column.addColumnQRange(this.currentQRange); ingo@2845: } ingo@2844: mschaefer@8989: this.waterlevels.add(this.current); ingo@2845: } ingo@2844: } ingo@2844: ingo@2844: @Override mschaefer@8989: protected void handleLine(final int lineNum, final String line) { ingo@2845: if (line.startsWith(START_META_CHAR)) { ingo@2845: handleMetaLine(stripMetaLine(line)); ingo@2845: return; ingo@2845: } ingo@2845: else if (handleQRange(line)) { ingo@2845: return; ingo@2845: } ingo@2845: else { ingo@2845: handleDataLine(line); ingo@2845: return; ingo@2845: } ingo@2845: } ingo@2844: ingo@2845: mschaefer@8989: private void handleMetaLine(final String meta) { mschaefer@8989: final Matcher m = META_UNIT.matcher(meta); ingo@2845: ingo@2845: if (m.matches()) { mschaefer@8989: final String unit = m.group(1); ingo@2845: log.debug("Found unit: '" + unit + "'"); ingo@2845: mschaefer@8989: this.current.setUnit(new ImportUnit(unit)); ingo@2845: } ingo@2845: } ingo@2845: ingo@2845: mschaefer@8989: private boolean handleQRange(final String line) { mschaefer@8989: final Matcher m = META_Q_RANGE.matcher(line); ingo@2845: ingo@2845: if (m.matches()) { mschaefer@8989: final String unitStr = m.group(1); mschaefer@8989: final String valueStr = m.group(2); felix@5240: try { mschaefer@8989: if (this.currentQRange != null) { felix@5240: // Finish off the last one. mschaefer@8989: final List cValues = this.column mschaefer@8989: .getColumnValues(); felix@5240: // Set end of range to last station. mschaefer@8989: this.currentRange.setB(cValues.get(cValues.size() -1) mschaefer@8989: .getPosition()); mschaefer@8989: this.currentQRange.setRange(this.currentRange); mschaefer@8989: this.column.addColumnQRange(this.currentQRange); ingo@2845: } mschaefer@8989: this.currentQRange = new ImportWstQRange(null, mschaefer@8989: AbstractParser.parseDecimal(valueStr)); mschaefer@8989: this.currentRange = new ImportRange(); ingo@2845: ingo@2845: log.debug("Found new Q range: Q=" + valueStr); ingo@2845: ingo@2845: return true; ingo@2845: } mschaefer@8989: catch (final NumberFormatException pe) { tom@5490: log.warn("Unparseable Q range: '" + line + "'"); ingo@2845: } ingo@2845: } ingo@2845: ingo@2845: return false; ingo@2845: } ingo@2845: ingo@2845: mschaefer@8989: private void handleDataLine(final String line) { mschaefer@8989: final String[] cols = line.split(SEPERATOR_CHAR); ingo@2845: ingo@2845: if (cols == null || cols.length < 2) { ingo@2845: log.warn("skip invalid waterlevel line: '" + line + "'"); ingo@2845: return; ingo@2845: } ingo@2845: ingo@2845: try { felix@5240: // Store the value and remember the position for QRange, if needed. mschaefer@8989: final BigDecimal station = AbstractParser.parseDecimal(cols[0]); mschaefer@8989: final BigDecimal value = AbstractParser.parseDecimal(cols[1]); ingo@2845: mschaefer@8989: this.column.addColumnValue(station, value); felix@5240: mschaefer@8989: if (this.currentRange.getA() == null) { mschaefer@8989: this.currentRange.setA(station); felix@5240: } ingo@2845: } mschaefer@8989: catch (final NumberFormatException pe) { tom@5490: log.warn("Unparseable number in data row: " + line); ingo@2845: } ingo@2844: } ingo@2844: } ingo@2844: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :