teichmann@8025: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@8025: * Software engineering by Intevation GmbH teichmann@8025: * teichmann@8025: * This file is Free Software under the GNU AGPL (>=v3) teichmann@8025: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@8025: * documentation coming with Dive4Elements River for details. teichmann@8025: */ teichmann@8025: teichmann@8025: package org.dive4elements.river.importer.parsers; teichmann@8025: teichmann@8025: import java.text.ParseException; teichmann@8025: teichmann@8025: import java.util.ArrayList; teichmann@8025: import java.util.List; tom@8056: import java.util.regex.Matcher; tom@8056: import java.util.regex.Pattern; teichmann@8025: teichmann@8025: import org.apache.log4j.Logger; teichmann@8025: teichmann@8031: import org.dive4elements.river.importer.ImportSedimentLoadLS; teichmann@8025: import org.dive4elements.river.importer.ImportSedimentLoadLSValue; teichmann@8025: import org.dive4elements.river.importer.ImportUnit; tom@8032: tom@8032: tom@8032: /** Parses sediment load longitudinal section files. */ tom@8043: public class SedimentLoadLSParser extends AbstractSedimentLoadParser { teichmann@8025: teichmann@8025: private static final Logger log = teichmann@8025: Logger.getLogger(SedimentLoadLSParser.class); teichmann@8025: teichmann@8025: tom@8056: public static final Pattern META_UNIT = tom@8056: Pattern.compile("^Einheit: \\[(.*)\\].*"); tom@8056: tom@8056: tom@8032: protected List sedimentLoadLSs; teichmann@8025: teichmann@8031: protected ImportSedimentLoadLS[] current; teichmann@8025: tom@8056: protected ImportUnit unit; tom@8056: teichmann@8025: teichmann@8025: public SedimentLoadLSParser() { tom@8032: sedimentLoadLSs = new ArrayList(); teichmann@8025: } teichmann@8025: teichmann@8025: teichmann@8025: @Override teichmann@8025: protected void reset() { teichmann@8025: current = null; teichmann@8025: grainFraction = null; teichmann@8025: unit = null; teichmann@8025: } teichmann@8025: teichmann@8025: teichmann@8025: @Override teichmann@8025: protected void finish() { teichmann@8025: if (current != null) { teichmann@8031: for (ImportSedimentLoadLS isy: current) { tom@8032: sedimentLoadLSs.add(isy); teichmann@8025: } teichmann@8025: } teichmann@8025: teichmann@8025: description = null; teichmann@8025: } teichmann@8025: teichmann@8025: teichmann@8025: @Override tom@8056: protected void handleMetaLine(String line) throws LineParserException { tom@8056: if (handleMetaUnit(line)) { tom@8056: return; tom@8056: } tom@8056: if (handleMetaFraction(line)) { tom@8056: return; tom@8056: } tom@8056: if (handleMetaFractionName(line)) { tom@8056: return; tom@8056: } tom@8059: if (handleMetaSQTimeInterval(line)) { tom@8059: return; tom@8059: } tom@8056: if (handleColumnNames(line)) { tom@8056: return; tom@8056: } tom@8056: log.warn("ASLP: Unknown meta line: '" + line + "'"); tom@8056: } tom@8056: tom@8056: tom@8056: protected boolean handleMetaUnit(String line) { tom@8056: Matcher m = META_UNIT.matcher(line); tom@8056: tom@8056: if (m.matches()) { tom@8056: unit = new ImportUnit(m.group(1)); tom@8056: return true; tom@8056: } tom@8056: tom@8056: return false; tom@8056: } tom@8056: tom@8056: tom@8056: @Override teichmann@8025: protected void handleDataLine(String line) { teichmann@8025: String[] vals = line.split(SEPERATOR_CHAR); teichmann@8025: teichmann@8025: if (vals == null || vals.length < columnNames.length-1) { tom@8032: log.warn("SLLSP: skip invalid data line: '" + line + "'"); teichmann@8025: return; teichmann@8025: } teichmann@8025: teichmann@8025: try { teichmann@8025: Double km = nf.parse(vals[0]).doubleValue(); teichmann@8025: teichmann@8025: for (int i = 1, n = columnNames.length-1; i < n; i++) { teichmann@8025: String curVal = vals[i]; teichmann@8025: teichmann@8025: if (curVal != null && curVal.length() > 0) { teichmann@8025: current[i-1].addValue(new ImportSedimentLoadLSValue( teichmann@8025: km, nf.parse(vals[i]).doubleValue() teichmann@8025: )); teichmann@8025: } teichmann@8025: } teichmann@8025: } teichmann@8025: catch (ParseException pe) { tom@8032: log.warn("SLLSP: unparseable number in data row '" + line + "':", pe); teichmann@8025: } teichmann@8025: } teichmann@8025: teichmann@8025: tom@8043: @Override tom@8043: protected void initializeSedimentLoads() { teichmann@8025: // skip first column (Fluss-km) and last column (Hinweise) teichmann@8031: current = new ImportSedimentLoadLS[columnNames.length-2]; teichmann@8025: teichmann@8025: Integer kind; teichmann@8025: teichmann@8025: if (inputFile.getAbsolutePath().contains("amtliche Epochen")) { teichmann@8025: kind = new Integer(1); teichmann@8025: } teichmann@8025: else { teichmann@8025: kind = new Integer(0); teichmann@8025: } teichmann@8025: teichmann@8025: for (int i = 0, n = columnNames.length; i < n-2; i++) { teichmann@8031: current[i] = new ImportSedimentLoadLS(this.description); teichmann@8025: current[i].setTimeInterval(getTimeInterval(columnNames[i+1])); tom@8059: current[i].setSQTimeInterval(sqTimeInterval); teichmann@8025: current[i].setUnit(unit); teichmann@8025: current[i].setGrainFraction(grainFraction); teichmann@8025: current[i].setKind(kind); teichmann@8025: } teichmann@8025: } teichmann@8025: teichmann@8025: tom@8032: public List getSedimentLoadLSs() { tom@8032: return sedimentLoadLSs; teichmann@8025: } teichmann@8025: } teichmann@8025: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :