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