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.text.ParseException; 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; ingo@2845: felix@6319: import org.dive4elements.river.importer.ImportTimeInterval; teichmann@5829: import org.dive4elements.river.importer.ImportUnit; ingo@2844: teichmann@5829: import org.dive4elements.river.importer.ImportRange; 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; teichmann@8187: import org.dive4elements.river.backend.utils.DateUtil; 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 = ingo@2845: NumberFormat.getInstance(DEFAULT_LOCALE); ingo@2845: ingo@2845: private static final Pattern META_Q_RANGE = ingo@2845: Pattern.compile("Abfluss\\s\\[(.*)\\];(.*)"); ingo@2845: ingo@2845: public static final Pattern META_UNIT = ingo@2845: Pattern.compile("^Einheit: \\[(.*)\\].*"); ingo@2845: tom@7377: public static final double INTERVAL_GAP = 0.00001d; tom@7377: felix@5240: private 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() { felix@5240: waterlevels = new ArrayList(); ingo@2844: } ingo@2844: ingo@2844: felix@5240: public List getWaterlevels() { ingo@2844: return waterlevels; ingo@2844: } ingo@2844: ingo@2844: ingo@2844: @Override ingo@2845: public void parse(File file) throws IOException { ingo@2845: currentDescription = file.getName(); ingo@2845: ingo@2845: super.parse(file); ingo@2845: } ingo@2845: ingo@2845: ingo@2845: @Override ingo@2844: protected void reset() { felix@5240: currentQRange = null; felix@5240: current = new ImportWst(currentDescription); felix@5264: current.setNumberColumns(1); felix@5264: column = current.getColumn(0); felix@5264: column.setName(currentDescription); felix@5264: column.setDescription(currentDescription); felix@6319: felix@6319: // Try to extract and set the TimeInterval. felix@6319: Matcher m = WaterlevelDifferencesParser.YEARS_IN_COLUMN.matcher(currentDescription); felix@6319: felix@6319: if (m.matches()) { felix@6319: int startYear = Integer.parseInt(m.group(1)); felix@6319: int endYear = Integer.parseInt(m.group(2)); felix@6319: ImportTimeInterval time = new ImportTimeInterval( tom@7927: DateUtil.getStartDateFromYear(startYear), tom@7927: DateUtil.getEndDateFromYear(endYear) felix@6319: ); felix@6319: column.setTimeInterval(time); felix@6319: } else { felix@6319: log.debug("No time interval in column header found: " + currentDescription); felix@6319: } felix@6319: felix@6282: current.setKind(7); ingo@2844: } ingo@2844: ingo@2844: ingo@2844: @Override ingo@2844: protected void finish() { ingo@2845: if (current != null) { felix@5240: if (currentQRange != null) { felix@5240: List cValues = 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 tom@7377: BigDecimal lastStation = cValues.get(cValues.size() -1).getPosition(); tom@7377: if (lastStation.compareTo(currentRange.getA()) == 0) { teichmann@7883: currentRange.setB(new BigDecimal(lastStation.doubleValue() tom@7377: + INTERVAL_GAP)); tom@7377: } tom@7377: else { tom@7377: currentRange.setB(lastStation); tom@7377: } tom@7377: felix@5240: currentQRange.setRange(currentRange); felix@5240: column.addColumnQRange(currentQRange); ingo@2845: } ingo@2844: ingo@2845: waterlevels.add(current); ingo@2845: } ingo@2844: } ingo@2844: ingo@2844: @Override ingo@4193: protected void handleLine(int lineNum, 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: ingo@2845: private void handleMetaLine(String meta) { ingo@2845: Matcher m = META_UNIT.matcher(meta); ingo@2845: ingo@2845: if (m.matches()) { ingo@2845: String unit = m.group(1); ingo@2845: log.debug("Found unit: '" + unit + "'"); ingo@2845: ingo@2845: current.setUnit(new ImportUnit(unit)); ingo@2845: } ingo@2845: } ingo@2845: ingo@2845: ingo@2845: private boolean handleQRange(String line) { ingo@2845: Matcher m = META_Q_RANGE.matcher(line); ingo@2845: ingo@2845: if (m.matches()) { ingo@2845: String unitStr = m.group(1); ingo@2845: String valueStr = m.group(2); felix@5240: try { felix@5240: if (currentQRange != null) { felix@5240: // Finish off the last one. felix@5240: List cValues = column.getColumnValues(); felix@5240: // Set end of range to last station. felix@5240: currentRange.setB(cValues.get(cValues.size() -1).getPosition()); felix@5240: currentQRange.setRange(currentRange); felix@5240: column.addColumnQRange(currentQRange); ingo@2845: } felix@5240: currentQRange = new ImportWstQRange(null, felix@5240: new BigDecimal(nf.parse(valueStr).doubleValue())); felix@5240: currentRange = new ImportRange(); ingo@2845: ingo@2845: log.debug("Found new Q range: Q=" + valueStr); ingo@2845: ingo@2845: return true; ingo@2845: } ingo@2845: catch (ParseException 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: ingo@2845: private void handleDataLine(String line) { ingo@2845: 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. ingo@2845: Double station = nf.parse(cols[0]).doubleValue(); ingo@2845: Double value = nf.parse(cols[1]).doubleValue(); ingo@2845: felix@5240: BigDecimal stationBD = new BigDecimal(station); felix@5240: felix@5240: column.addColumnValue(stationBD, new BigDecimal(value)); felix@5240: felix@5240: if (currentRange.getA() == null) { felix@5240: currentRange.setA(stationBD); felix@5240: } ingo@2845: } ingo@2845: catch (ParseException 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 :