diff backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/DepthEvolutionParser.java @ 8971:50416a0df385

Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author mschaefer
date Tue, 03 Apr 2018 10:18:30 +0200
parents
children ae76f618d990
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/DepthEvolutionParser.java	Tue Apr 03 10:18:30 2018 +0200
@@ -0,0 +1,197 @@
+/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by
+ *  Björnsen Beratende Ingenieure GmbH
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * 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.sinfo.parsers;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.river.importer.Config;
+import org.dive4elements.river.importer.ImportRiver;
+import org.dive4elements.river.importer.common.AbstractParser;
+import org.dive4elements.river.importer.common.ParsingState;
+import org.dive4elements.river.importer.sinfo.importitem.DepthEvolutionKmLineImport;
+import org.dive4elements.river.importer.sinfo.importitem.DepthEvolutionSeriesImport;
+import org.dive4elements.river.model.sinfo.DepthEvolution;
+import org.dive4elements.river.model.sinfo.DepthEvolutionValue;
+
+/**
+ * Reads and parses a depth evolution file
+ *
+ * @author Matthias Schäfer
+ *
+ */
+public class DepthEvolutionParser extends AbstractParser<DepthEvolution, DepthEvolutionValue, DepthEvolutionKmLineImport, DepthEvolutionSeriesImport> {
+
+    /***** FIELDS *****/
+
+    private static final Logger log = Logger.getLogger(DepthEvolutionParser.class);
+
+    protected static final Pattern META_REFERENCE_YEAR = Pattern.compile("^#\\sBezugsjahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);
+
+    protected static final Pattern META_START_YEAR = Pattern.compile("^#\\sAusgangsjahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);
+
+    private static final Pattern META_CURR_SOUNDING = Pattern.compile("^#\\sAktuelle Peilung\\s*/\\s*Epoche:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
+
+    private static final Pattern META_OLD_SOUNDING = Pattern.compile("^#\\sHistorische Peilung\\s*/\\s*Epoche:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
+
+    private static final Pattern META_CURR_WSP = Pattern.compile("^#\\sAktuelle Wasserspiegellage:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
+
+    private static final Pattern META_OLD_WSP = Pattern.compile("^#\\sHistorische Wasserspiegellage:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
+
+
+    /***** CONSTRUCTORS *****/
+
+    public DepthEvolutionParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
+        super(importPath, rootRelativePath, river);
+    }
+
+
+    /***** METHODS *****/
+
+    @Override
+    protected Logger getLog() {
+        return log;
+    }
+
+    /**
+     * Whether this import type shall be skipped
+     */
+    public static boolean shallSkip() {
+        return Config.INSTANCE.skipSInfoDepthEvolution();
+    }
+
+    /**
+     * Creates a list of parsers for all depth_evolution import files in a directory
+     */
+    public static List<DepthEvolutionParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
+        final List<DepthEvolutionParser> parsers = new ArrayList<>();
+        for (final File file : listFiles(importDir, ".csv"))
+            parsers.add(new DepthEvolutionParser(file, new File(relativeDir, file.getName()), river));
+        return parsers;
+    }
+
+    @Override
+    protected boolean handleMetaOther() {
+        if (handleMetaStartYear())
+            return true;
+        else if (handleMetaReferenceYear())
+            return true;
+        else if (handleMetaCurrSounding())
+            return true;
+        else if (handleMetaOldSounding())
+            return true;
+        else if (handleMetaCurrGlw())
+            return true;
+        else if (handleMetaOldGlw())
+            return true;
+        else
+            return false;
+    }
+
+    private boolean handleMetaStartYear() {
+        final Matcher m = META_START_YEAR.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_START_YEAR);
+            this.seriesHeader.setStart_year(Integer.parseInt(m.group(1)));
+            return true;
+        }
+        return false;
+    }
+
+    private boolean handleMetaReferenceYear() {
+        final Matcher m = META_REFERENCE_YEAR.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_REFERENCE_YEAR);
+            this.seriesHeader.setReference_year(Integer.parseInt(m.group(1)));
+            return true;
+        }
+        return false;
+    }
+
+    private boolean handleMetaCurrSounding() {
+        final Matcher m = META_CURR_SOUNDING.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_CURR_SOUNDING);
+            this.seriesHeader.setCurr_sounding(parseMetaInfo(m.group(1).trim()));
+            return true;
+        }
+        return false;
+    }
+
+    private boolean handleMetaOldSounding() {
+        final Matcher m = META_OLD_SOUNDING.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_OLD_SOUNDING);
+            this.seriesHeader.setOld_sounding(parseMetaInfo(m.group(1).trim()));
+            return true;
+        }
+        return false;
+    }
+
+    private boolean handleMetaCurrGlw() {
+        final Matcher m = META_CURR_WSP.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_CURR_WSP);
+            this.seriesHeader.setCurr_glw(parseMetaInfo(m.group(1).trim()));
+            return true;
+        }
+        return false;
+    }
+
+    private boolean handleMetaOldGlw() {
+        final Matcher m = META_OLD_WSP.matcher(this.currentLine);
+        if (m.matches()) {
+            this.metaPatternsMatched.add(META_OLD_WSP);
+            this.seriesHeader.setOld_glw(parseMetaInfo(m.group(1).trim()));
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    protected boolean handleMetaColumnTitles() {
+        if (super.handleMetaColumnTitles()) {
+            if (!this.metaPatternsMatched.contains(META_START_YEAR) || !this.metaPatternsMatched.contains(META_REFERENCE_YEAR)
+                    || !this.metaPatternsMatched.contains(META_CURR_SOUNDING) || !this.metaPatternsMatched.contains(META_OLD_SOUNDING)
+                    || !this.metaPatternsMatched.contains(META_CURR_WSP) || !this.metaPatternsMatched.contains(META_OLD_WSP)) {
+                logError("One or more of the required meta infos are missing");
+                this.headerParsingState = ParsingState.STOP;
+            }
+            return true;
+        }
+        else
+            return false;
+    }
+
+    @Override
+    protected DepthEvolutionSeriesImport createSeriesImport(final String filename) {
+        return new DepthEvolutionSeriesImport(filename);
+    }
+
+    @Override
+    protected DepthEvolutionKmLineImport createKmLineImport(final Double km, final String[] values) {
+        if (parseDoubleWithNull(values[1]) == null) {
+            logError("Invalid total change in line " + this.in.getLineNumber());
+            return null;
+        }
+        if (parseDoubleWithNull(values[2]) == null) {
+            logError("Invalid change per year in line " + this.in.getLineNumber());
+            return null;
+        }
+        // cm to m
+        return new DepthEvolutionKmLineImport(km, parseDoubleWithNull(values[1]).doubleValue() / 100.0,
+                parseDoubleWithNull(values[2]).doubleValue() / 100.0);
+    }
+}

http://dive4elements.wald.intevation.org