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@2815: ingo@2817: import java.io.File; ingo@2817: import java.io.IOException; ingo@2816: import java.math.BigDecimal; ingo@2816: import java.text.NumberFormat; mschaefer@8991: import java.text.ParseException; ingo@2815: import java.util.ArrayList; ingo@2815: import java.util.List; ingo@2816: import java.util.regex.Matcher; ingo@2816: import java.util.regex.Pattern; ingo@2815: ingo@2815: import org.apache.log4j.Logger; mschaefer@8989: import org.dive4elements.river.importer.ImportDepth; mschaefer@8989: import org.dive4elements.river.importer.ImportSedimentDensity; mschaefer@8989: import org.dive4elements.river.importer.ImportSedimentDensityValue; mschaefer@8989: import org.dive4elements.river.importer.common.AbstractParser; ingo@2815: ingo@2815: public class SedimentDensityParser extends LineParser { ingo@2815: ingo@2815: private static final Logger log = mschaefer@8989: Logger.getLogger(SedimentDensityParser.class); ingo@2815: teichmann@5565: public static final NumberFormat nf = mschaefer@8989: NumberFormat.getInstance(DEFAULT_LOCALE); ingo@2816: ingo@2816: public static final Pattern META_DEPTH = mschaefer@8989: Pattern.compile("^Tiefe: (\\d++)-(\\d++).*"); ingo@2816: tom@6745: public static final Pattern META_YEAR = mschaefer@8989: Pattern.compile("^Jahr: (\\d{4}).*"); tom@6745: ingo@2815: protected List sedimentDensities; ingo@2815: ingo@2815: protected ImportSedimentDensity current; ingo@2815: ingo@2817: protected String currentDescription; ingo@2817: tom@6745: protected String yearString; ingo@2815: ingo@2815: public SedimentDensityParser() { mschaefer@8989: this.sedimentDensities = new ArrayList<>(); ingo@2815: } ingo@2815: ingo@2815: ingo@2815: @Override mschaefer@8989: public void parse(final File file) throws IOException { mschaefer@8989: this.currentDescription = file.getName(); ingo@2817: ingo@2817: super.parse(file); ingo@2817: } ingo@2817: ingo@2817: ingo@2817: @Override ingo@2815: protected void reset() { mschaefer@8989: this.current = new ImportSedimentDensity(this.currentDescription); ingo@2815: } ingo@2815: ingo@2815: ingo@2815: @Override ingo@2815: protected void finish() { mschaefer@8989: if (this.current != null) { mschaefer@8989: this.sedimentDensities.add(this.current); ingo@2815: } ingo@2815: } ingo@2815: ingo@2815: ingo@2815: @Override mschaefer@8989: protected void handleLine(final int lineNum, final String line) { ingo@2816: if (line.startsWith(START_META_CHAR)) { ingo@2816: handleMetaLine(stripMetaLine(line)); ingo@2816: } ingo@2816: else { ingo@2816: handleDataLine(line); ingo@2816: } ingo@2816: } ingo@2816: ingo@2816: mschaefer@8989: protected void handleMetaLine(final String line) { tom@5441: if (handleMetaDepth(line)) { ingo@2816: return; ingo@2816: } teichmann@7376: if (handleMetaYear(line)) { teichmann@7376: return; ingo@2816: } teichmann@7376: log.warn("Unknown meta line: '" + line + "'"); ingo@2816: } ingo@2816: ingo@2816: mschaefer@8989: protected boolean handleMetaDepth(final String line) { mschaefer@8989: final Matcher m = META_DEPTH.matcher(line); ingo@2816: ingo@2816: if (m.matches()) { mschaefer@8989: final String lo = m.group(1); mschaefer@8989: final String up = m.group(2); sascha@3942: tom@8856: log.info("Found sediment density depth: " mschaefer@8989: + lo + " - " + up + " cm"); ingo@2816: ingo@2816: try { mschaefer@8989: final ImportDepth depth = new ImportDepth( mschaefer@8989: AbstractParser.parseDecimal(lo), mschaefer@8989: AbstractParser.parseDecimal(up)); ingo@2816: mschaefer@8989: this.current.setDepth(depth); ingo@2816: ingo@2816: return true; ingo@2816: } mschaefer@8991: catch (final ParseException pe) { tom@5490: log.warn("Unparseable numbers in: '" + line + "'"); ingo@2816: } ingo@2816: } ingo@3940: else { ingo@3940: log.debug("Meta line doesn't contain depth information: " + line); ingo@3940: } ingo@2816: ingo@2816: return false; ingo@2816: } ingo@2816: mschaefer@8989: protected boolean handleMetaYear(final String line) { mschaefer@8989: final Matcher m = META_YEAR.matcher(line); tom@6745: tom@6745: if (m.matches()) { mschaefer@8989: this.yearString = m.group(1); tom@6745: mschaefer@8989: log.info("Found sediment density year: " + this.yearString); tom@6745: teichmann@7376: return true; tom@6745: } teichmann@7376: teichmann@7376: log.debug("Meta line doesn't contain year: " + line); tom@6745: tom@6745: return false; tom@6745: } tom@6745: ingo@2816: mschaefer@8989: protected void handleDataLine(final String line) { mschaefer@8989: final String[] vals = line.split(SEPERATOR_CHAR); ingo@2816: ingo@2816: if (vals == null || vals.length < 3) { ingo@2816: log.warn("skip invalid data line: '" + line + "'"); ingo@2816: return; ingo@2816: } ingo@2816: tom@5507: BigDecimal km = null; tom@5507: BigDecimal shoreOffset = null; tom@5507: BigDecimal density = null; ingo@2816: try { mschaefer@8989: km = AbstractParser.parseDecimal(vals[0]); mschaefer@8989: density = AbstractParser.parseDecimal(vals[2]); teichmann@5565: if (!vals[1].isEmpty()) { mschaefer@8989: shoreOffset = AbstractParser.parseDecimal(vals[1]); teichmann@5565: } ingo@2816: } mschaefer@8991: catch (final ParseException pe) { tom@5490: log.warn("Unparseable numbers in '" + line + "'"); ingo@2816: } rrenkert@4524: teichmann@5565: if (km == null || density == null) { teichmann@5565: log.warn("SDP: No km nor density given. Skip line"); teichmann@5565: return; teichmann@5565: } tom@5507: rrenkert@4524: BigDecimal year = null; mschaefer@8989: if (this.yearString != null) { teichmann@7376: try { mschaefer@8989: year = AbstractParser.parseDecimal(this.yearString); teichmann@7376: } mschaefer@8991: catch (final ParseException pe) { teichmann@7376: log.warn("Unparseable year string"); teichmann@7376: } teichmann@7376: } rrenkert@4524: mschaefer@8989: this.current.addValue(new ImportSedimentDensityValue( mschaefer@8989: km, mschaefer@8989: shoreOffset, mschaefer@8989: density, mschaefer@8989: year, mschaefer@8989: this.currentDescription)); ingo@2815: } ingo@2815: ingo@2815: ingo@2815: public List getSedimentDensities() { mschaefer@8989: return this.sedimentDensities; ingo@2815: } ingo@2815: } ingo@2815: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :