changeset 3328:a41f279a66e2

Added parser and import classes to import MINFO sq relations. flys-backend/trunk@4646 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 13 Jun 2012 06:22:04 +0000
parents bf8d9a4f4cd4
children cc8fc6b29649
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/Config.java flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelation.java flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java
diffstat 6 files changed, 313 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Tue Jun 12 13:50:29 2012 +0000
+++ b/flys-backend/ChangeLog	Wed Jun 13 06:22:04 2012 +0000
@@ -1,3 +1,20 @@
+2012-06-13  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportSQRelation.java:
+	  New classes used during the import process of MINFO sq relations.
+
+	* src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java:
+	  New line parser that reads MINFO specifc sq relation files.
+
+	* src/main/java/de/intevation/flys/importer/Config.java: Added system
+	  property to skip parsing sq relations.
+
+	    "-Dflys.backend.importer.skip.sq.relation"
+
+	* src/main/java/de/intevation/flys/importer/ImportRiver.java: Parse MINFO
+	  sq relations.
+
 2012-06-12  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/ImportRiver.java: Cleared some
--- a/flys-backend/src/main/java/de/intevation/flys/importer/Config.java	Tue Jun 12 13:50:29 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/Config.java	Wed Jun 13 06:22:04 2012 +0000
@@ -65,6 +65,9 @@
     public static final String SKIP_WATERLEVEL_DIFFERENCES =
         "flys.backend.importer.skip.waterlevel.differences";
 
+    public static final String SKIP_SQ_RELATION =
+        "flys.backend.importer.skip.sq.relation";
+
 
     public static final Config INSTANCE = new Config();
 
@@ -154,5 +157,9 @@
     public boolean skipWaterlevelDifferences() {
         return Boolean.getBoolean(SKIP_WATERLEVEL_DIFFERENCES);
     }
+
+    public boolean skipSQRelation() {
+        return Boolean.getBoolean(SKIP_SQ_RELATION);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Jun 12 13:50:29 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Wed Jun 13 06:22:04 2012 +0000
@@ -16,6 +16,7 @@
 import de.intevation.flys.importer.parsers.PegelGltParser;
 import de.intevation.flys.importer.parsers.SedimentDensityParser;
 import de.intevation.flys.importer.parsers.SedimentYieldParser;
+import de.intevation.flys.importer.parsers.SQRelationParser;
 import de.intevation.flys.importer.parsers.WaterlevelDifferencesParser;
 import de.intevation.flys.importer.parsers.WaterlevelParser;
 import de.intevation.flys.importer.parsers.WstParser;
@@ -96,6 +97,8 @@
 
     public static final String MINFO_WATERLEVEL_DIFF_DIR = "Wasserspiegeldifferenzen";
 
+    public static final String MINFO_SQ_DIR = "Feststofftransport-Abfluss-Beziehung";
+
 
     protected String name;
 
@@ -139,6 +142,8 @@
 
     protected List<ImportWaterlevelDifference> waterlevelDiffs;
 
+    protected List<ImportSQRelation> sqRelations;
+
     protected ImportWst wst;
 
     protected ImportUnit wstUnit;
@@ -162,6 +167,7 @@
         sedimentYields            = new ArrayList<ImportSedimentYield>();
         waterlevels               = new ArrayList<ImportWaterlevel>();
         waterlevelDiffs           = new ArrayList<ImportWaterlevelDifference>();
+        sqRelations               = new ArrayList<ImportSQRelation>();
     }
 
     public ImportRiver(
@@ -232,6 +238,7 @@
         parseSedimentYield();
         parseWaterlevels();
         parseWaterlevelDifferences();
+        parseSQRelation();
     }
 
     public void parseFloodProtection() throws IOException {
@@ -362,13 +369,13 @@
 
 
     protected void parseFlowVelocity() throws IOException {
-        log.debug("Parse flow velocity");
-
         if (Config.INSTANCE.skipFlowVelocity()) {
             log.info("skip parsing flow velocity");
             return;
         }
 
+        log.debug("Parse flow velocity");
+
         File minfoDir   = getMinfoDir();
         File flowDir    = new File(minfoDir, FLOW_VELOCITY_DIR);
         File modelDir   = new File(flowDir, FLOW_VELOCITY_MODEL);
@@ -521,6 +528,36 @@
     }
 
 
+    protected void parseSQRelation() throws IOException {
+        if (Config.INSTANCE.skipSQRelation()) {
+            log.info("skip sq relation");
+            return;
+        }
+
+        log.info("Parse sq relations");
+
+        File minfo = getMinfoDir();
+        File sqDir = new File(minfo, MINFO_SQ_DIR);
+
+        File[] files = sqDir.listFiles();
+
+        if (files == null) {
+            log.warn("Cannot read directory '" + sqDir + "'");
+            return;
+        }
+
+        SQRelationParser parser = new SQRelationParser();
+
+        for (File file: files) {
+            parser.parse(file);
+        }
+
+        sqRelations = parser.getSQRelations();
+
+        log.debug("Parsed " + sqRelations.size() + " SQ relations.");
+    }
+
+
     protected void parseBedHeightSingles(File dir) throws IOException {
         log.debug("Parse bed height singles");
 
@@ -892,6 +929,7 @@
         storeSedimentYield();
         storeWaterlevels();
         storeWaterlevelDifferences();
+        storeSQRelations();
     }
 
     public void storeWstUnit() {
@@ -1186,6 +1224,15 @@
     }
 
 
+    public void storeSQRelations() {
+        if (!Config.INSTANCE.skipSQRelation()) {
+            log.info("store sq relations");
+
+            // TODO
+        }
+    }
+
+
     public void storeAnnotations() {
         if (!Config.INSTANCE.skipAnnotations()) {
             River river = getPeer();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelation.java	Wed Jun 13 06:22:04 2012 +0000
@@ -0,0 +1,62 @@
+package de.intevation.flys.importer;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.exception.ConstraintViolationException;
+
+import de.intevation.flys.model.River;
+import de.intevation.flys.model.SQRelation;
+
+
+public class ImportSQRelation {
+
+    private static Logger log = Logger.getLogger(ImportSQRelation.class);
+
+
+    private ImportTimeInterval timeInterval;
+
+    private List<ImportSQRelationValue> values;
+
+    private SQRelation peer;
+
+
+    public ImportSQRelation() {
+        this.values = new ArrayList<ImportSQRelationValue>();
+    }
+
+
+    public void storeDependencies(River river)
+    throws SQLException, ConstraintViolationException
+    {
+        log.info("store dependencies");
+        log.warn("TODO: IMPLEMENT 'storeDependencies()'");
+    }
+
+
+    public SQRelation getPeer() {
+        log.debug("getPeer()");
+
+        if (peer == null) {
+            // TODO
+        }
+
+        return peer;
+    }
+
+
+    public void setTimeInterval(ImportTimeInterval timeInterval) {
+        this.timeInterval = timeInterval;
+    }
+
+
+    public void addValue(ImportSQRelationValue value) {
+        if (value != null) {
+            this.values.add(value);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java	Wed Jun 13 06:22:04 2012 +0000
@@ -0,0 +1,64 @@
+package de.intevation.flys.importer;
+
+import java.sql.SQLException;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.exception.ConstraintViolationException;
+
+import de.intevation.flys.model.SQRelation;
+import de.intevation.flys.model.SQRelationValue;
+
+
+public class ImportSQRelationValue {
+
+    private static Logger log = Logger.getLogger(ImportSQRelationValue.class);
+
+
+    private SQRelationValue peer;
+
+    private String parameter;
+    private String fraction;
+    private String function;
+
+    private double km;
+    private double a;
+    private double b;
+
+
+    public ImportSQRelationValue(
+        String parameter,
+        String fraction,
+        String function,
+        double km,
+        double a,
+        double b
+    ) {
+        this.parameter = parameter;
+        this.fraction  = fraction;
+        this.function  = function;
+        this.km        = km;
+        this.a         = a;
+        this.b         = b;
+    }
+
+
+    public void storeDependencies(SQRelation owner)
+    throws SQLException, ConstraintViolationException
+    {
+        log.info("store dependencies");
+        log.warn("TODO: IMPLEMENT 'storeDependencies()'");
+    }
+
+
+    public SQRelationValue getPeer() {
+        log.debug("getPeer()");
+
+        if (peer == null) {
+            // TODO
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java	Wed Jun 13 06:22:04 2012 +0000
@@ -0,0 +1,114 @@
+package de.intevation.flys.importer.parsers;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.ImportSQRelation;
+import de.intevation.flys.importer.ImportSQRelationValue;
+import de.intevation.flys.importer.ImportTimeInterval;
+
+
+public class SQRelationParser extends LineParser {
+
+    private static final Logger log =
+        Logger.getLogger(SQRelationParser.class);
+
+    private static final Pattern TIMERANGE_REGEX =
+        Pattern.compile(".*Zeitraum.*\\s(\\w*)-(\\w*).*");
+
+    private static final NumberFormat nf =
+        NumberFormat.getInstance(DEFAULT_LOCALE);
+
+
+    private List<ImportSQRelation> relations;
+
+    private ImportSQRelation current;
+
+
+    public SQRelationParser() {
+        relations = new ArrayList<ImportSQRelation>();
+    }
+
+
+    public List<ImportSQRelation> getSQRelations() {
+        return relations;
+    }
+
+    @Override
+    protected void reset() {
+        current = new ImportSQRelation();
+    }
+
+
+    @Override
+    protected void finish() {
+        relations.add(current);
+    }
+
+
+    @Override
+    protected void handleLine(String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    protected void handleMetaLine(String line) {
+        Matcher m = TIMERANGE_REGEX.matcher(line);
+
+        if (m.matches()) {
+            String lo = m.group(1);
+            String hi = m.group(2);
+
+            log.debug("Found timerange " + lo + " - " + hi);
+
+            try {
+                int low  = nf.parse(lo).intValue();
+                int high = nf.parse(hi).intValue();
+
+                current.setTimeInterval(new ImportTimeInterval(
+                    getDateFromYear(low),
+                    getDateFromYear(high)
+                ));
+            }
+            catch (ParseException nfe) {
+                log.warn("Cannot parse time range.", nfe);
+            }
+        }
+    }
+
+
+    protected void handleDataLine(String line) {
+        String[] cols = line.split(SEPERATOR_CHAR);
+
+        if (cols.length < 8) {
+            log.warn("skip invalid data line: '" + line + "'");
+            return;
+        }
+
+        try {
+            current.addValue(new ImportSQRelationValue(
+                cols[1],
+                cols[2],
+                cols[4],
+                nf.parse(cols[3]).doubleValue(),
+                nf.parse(cols[6]).doubleValue(),
+                nf.parse(cols[7]).doubleValue()
+            ));
+        }
+        catch (ParseException pe) {
+            log.warn("Error while parsing sq relation row: '" + line + "'", pe);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org