tom@8056: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde tom@8056: * Software engineering by Intevation GmbH tom@8056: * tom@8056: * This file is Free Software under the GNU AGPL (>=v3) tom@8056: * and comes with ABSOLUTELY NO WARRANTY! Check out the tom@8056: * documentation coming with Dive4Elements River for details. tom@8056: */ tom@8056: tom@8056: package org.dive4elements.river.importer.parsers; tom@8056: tom@8056: import java.text.ParseException; tom@8056: tom@8056: import java.util.ArrayList; tom@8056: import java.util.List; tom@8056: tom@8056: import org.apache.log4j.Logger; tom@8056: tom@8056: import org.dive4elements.river.importer.ImporterSession; tom@8056: import org.dive4elements.river.importer.ImportSedimentLoad; tom@8056: import org.dive4elements.river.importer.ImportSedimentLoadValue; tom@8056: tom@8056: import org.dive4elements.river.model.MeasurementStation; tom@8056: tom@8056: /** Parses sediment load longitudinal section files. */ tom@8056: public class SedimentLoadParser extends AbstractSedimentLoadParser { tom@8056: private static final Logger log = tom@8056: Logger.getLogger(SedimentLoadParser.class); tom@8056: tom@8056: tom@8056: public static final String MEASUREMENT_TYPE_BEDLOAD = "Geschiebe"; tom@8056: tom@8056: public static final String MEASUREMENT_TYPE_SUSP = "Schwebstoff"; tom@8056: tom@8056: public static final String GRAINFRACTION_NAME_SUSP = "suspended_sediment"; tom@8056: tom@8056: public static final String GRAINFRACTION_NAME_TOTAL = "total"; tom@8056: tom@8056: protected List sedimentLoads; tom@8056: tom@8056: protected ImportSedimentLoad[] current; tom@8056: tom@8056: protected String rivername; tom@8056: tom@8056: public SedimentLoadParser() { tom@8056: sedimentLoads = new ArrayList(); tom@8056: } tom@8056: tom@8056: public SedimentLoadParser(String rivername) { tom@8056: sedimentLoads = new ArrayList(); tom@8056: this.rivername = rivername; tom@8056: } tom@8056: tom@8056: tom@8056: tom@8056: @Override tom@8056: protected void reset() { tom@8056: current = null; tom@8056: grainFraction = null; tom@8056: } tom@8056: tom@8056: tom@8056: @Override tom@8056: protected void finish() { tom@8056: if (current != null) { tom@8056: for (ImportSedimentLoad isy: current) { tom@8056: sedimentLoads.add(isy); tom@8056: } tom@8056: } tom@8056: tom@8056: description = null; tom@8056: } tom@8056: tom@8056: tom@8056: @Override tom@8056: protected void handleMetaLine(String line) throws LineParserException { 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: private void initializeSedimentLoadValues(String[] vals, tom@8056: MeasurementStation m) throws ParseException { tom@8056: for (int i = 1, n = columnNames.length-1; i < n; i++) { tom@8056: String curVal = vals[i]; tom@8056: tom@8056: if (curVal != null && curVal.length() > 0) { tom@8056: current[i-1].addValue(new ImportSedimentLoadValue( tom@8056: m, nf.parse(curVal).doubleValue() tom@8056: )); tom@8056: } tom@8056: } tom@8056: } tom@8056: tom@8056: @Override tom@8056: protected void handleDataLine(String line) { tom@8056: String[] vals = line.split(SEPERATOR_CHAR); tom@8056: tom@8056: if (vals == null || vals.length < columnNames.length-1) { tom@8056: log.warn("SLP: skip invalid data line: '" + line + "'"); tom@8056: return; tom@8056: } tom@8056: tom@8056: try { tom@8056: Double km = nf.parse(vals[0]).doubleValue(); tom@8056: tom@8056: List ms = tom@8056: ImporterSession.getInstance().getMeasurementStations( tom@8056: rivername, km); tom@8056: tom@8056: String gfn = grainFraction.getPeer().getName(); tom@8056: tom@8056: if (ms != null && !ms.isEmpty()) { tom@8056: tom@8056: // Check for measurement station at km fitting grain fraction tom@8056: for (MeasurementStation m : ms) { tom@8056: if (gfn.equals(GRAINFRACTION_NAME_TOTAL)) { tom@8056: // total load can be at any station type tom@8056: initializeSedimentLoadValues(vals, m); tom@8056: return; tom@8056: } tom@8056: if (gfn.equals(GRAINFRACTION_NAME_SUSP) && tom@8056: m.getMeasurementType().equals(MEASUREMENT_TYPE_SUSP)) { tom@8056: // susp. sediment can only be at respective stations tom@8056: initializeSedimentLoadValues(vals, m); tom@8056: return; tom@8056: } tom@8056: if (!gfn.equals(GRAINFRACTION_NAME_SUSP) && tom@8056: m.getMeasurementType().equals(MEASUREMENT_TYPE_BEDLOAD)) { tom@8056: /** anything but total load and susp. sediment tom@8056: can only be at bed load measurement stations */ tom@8056: initializeSedimentLoadValues(vals, m); tom@8056: return; tom@8056: } tom@8056: } tom@8056: log.error("SLP: No measurement station at km " + km + tom@8056: " fitting grain fraction " + gfn + tom@8056: " on river " + rivername); tom@8056: return; tom@8056: } tom@8056: else { tom@8056: log.error("SLP: No measurement station at km " + km + tom@8056: " on river " + rivername); tom@8056: return; tom@8056: } tom@8056: } tom@8056: catch (ParseException pe) { tom@8056: log.warn("SLP: unparseable number in data row '" + line + "':", pe); tom@8056: } tom@8056: } tom@8056: tom@8056: tom@8056: @Override tom@8056: protected void initializeSedimentLoads() { tom@8056: // skip first column (Fluss-km) and last column (Hinweise) tom@8056: current = new ImportSedimentLoad[columnNames.length-2]; tom@8056: tom@8056: Integer kind; tom@8056: tom@8056: if (inputFile.getAbsolutePath().contains("amtliche Epochen")) { tom@8056: kind = new Integer(1); tom@8056: } tom@8056: else { tom@8056: kind = new Integer(0); tom@8056: } tom@8056: tom@8056: for (int i = 0, n = columnNames.length; i < n-2; i++) { tom@8056: current[i] = new ImportSedimentLoad( tom@8056: grainFraction, tom@8056: getTimeInterval(columnNames[i+1]), tom@8059: sqTimeInterval, tom@8056: description, tom@8056: kind); tom@8056: } tom@8056: } tom@8056: tom@8056: tom@8056: public List getSedimentLoads() { tom@8056: return sedimentLoads; tom@8056: } tom@8056: } tom@8056: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :