Mercurial > dive4elements > river
diff backend/src/main/java/org/dive4elements/river/importer/parsers/PorosityParser.java @ 7840:02711de579cc
Added model, parser and importer for porosities.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Wed, 30 Apr 2014 14:11:29 +0200 |
parents | |
children | 8c6ed23ce315 cdef048c4ac5 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/PorosityParser.java Wed Apr 30 14:11:29 2014 +0200 @@ -0,0 +1,201 @@ +/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU AGPL (>=v3) + * and comes with ABSOLUTELY NO WARRANTY! Check out the + * documentation coming with Dive4Elements River for details. + */ + +package org.dive4elements.river.importer.parsers; + +import org.dive4elements.river.importer.ImportDepth; +import org.dive4elements.river.importer.ImportPorosity; +import org.dive4elements.river.importer.ImportPorosityValue; +import org.dive4elements.river.importer.ImportSedimentDensity; +import org.dive4elements.river.importer.ImportSedimentDensityValue; +import org.dive4elements.river.importer.ImportTimeInterval; + +import java.io.File; +import java.io.IOException; + +import java.math.BigDecimal; + +import java.text.NumberFormat; +import java.text.ParseException; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +public class PorosityParser extends LineParser { + + private static final Logger log = + Logger.getLogger(PorosityParser.class); + + public static final NumberFormat nf = + NumberFormat.getInstance(DEFAULT_LOCALE); + + public static final Pattern META_DEPTH = + Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*"); + + public static final Pattern META_TIMEINTERVAL = + Pattern.compile("^Zeitraum: (\\d{4})-(\\d{4}).*"); + + protected List<ImportPorosity> porosities; + + protected ImportPorosity current; + + protected String currentDescription; + + public PorosityParser() { + porosities = new ArrayList<ImportPorosity>(); + } + + + @Override + public void parse(File file) throws IOException { + currentDescription = file.getName(); + + super.parse(file); + } + + + @Override + protected void reset() { + current = new ImportPorosity(currentDescription); + } + + + @Override + protected void finish() { + if (current != null) { + porosities.add(current); + } + } + + + @Override + protected void handleLine(int lineNum, String line) { + if (line.startsWith(START_META_CHAR)) { + handleMetaLine(stripMetaLine(line)); + } + else { + handleDataLine(line); + } + } + + + protected void handleMetaLine(String line) { + if (handleMetaDepth(line)) { + return; + } + if (handleMetaTimeInterval(line)) { + return; + } + log.warn("Unknown meta line: '" + line + "'"); + } + + protected boolean handleMetaTimeInterval(String line) { + Matcher m = META_TIMEINTERVAL.matcher(line); + + if (m.matches()) { + String lo = m.group(1); + String up = m.group(2); + + log.debug("Found time interval: " + lo + " - " + up); + + try { + int lower = Integer.valueOf(lo); + int upper = Integer.valueOf(up); + + Date fromYear = LineParser.getStartDateFromYear(lower); + Date toYear = LineParser.getEndDateFromYear(upper); + + current.setTimeInterval(new ImportTimeInterval(fromYear, toYear)); + } + catch (NumberFormatException e) { + log.warn("PP: could not parse timeinterval", e); + } + + return true; + } + + return false; + } + + protected boolean handleMetaDepth(String line) { + Matcher m = META_DEPTH.matcher(line); + + if (m.matches()) { + String lo = m.group(1); + String up = m.group(2); + + log.info("Found porosity depth: " + lo + " - " + up + " cm"); + + try { + ImportDepth depth = new ImportDepth( + new BigDecimal(nf.parse(lo).doubleValue()), + new BigDecimal(nf.parse(up).doubleValue()) + ); + + current.setDepth(depth); + + return true; + } + catch (ParseException pe) { + log.warn("Unparseable numbers in: '" + line + "'"); + } + } + else { + log.debug("Meta line doesn't contain depth information: " + line); + } + + return false; + } + + protected void handleDataLine(String line) { + String[] vals = line.split(SEPERATOR_CHAR); + log.debug("handle line: " + line); + + if (vals == null || vals.length < 3) { + log.warn("skip invalid data line: '" + line + "'"); + return; + } + + BigDecimal km = null; + BigDecimal shoreOffset = null; + BigDecimal porosity = null; + try { + km = new BigDecimal(nf.parse(vals[0]).doubleValue()); + porosity = new BigDecimal(nf.parse(vals[2]).doubleValue()); + if (!vals[1].isEmpty()) { + shoreOffset = new BigDecimal(nf.parse(vals[1]).doubleValue()); + } + } + catch (ParseException pe) { + log.warn("Unparseable numbers in '" + line + "'"); + } + + if (km == null || porosity == null) { + log.warn("PP: No km nor porosity given. Skip line"); + return; + } + log.debug("add new value."); + current.addValue(new ImportPorosityValue( + km, + shoreOffset, + porosity, + currentDescription)); + } + + + public List<ImportPorosity> getPorosities() { + return porosities; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :