[Dive4elements-commits] [PATCH 13 of 13] Parse and store year in sediment densities

Wald Commits scm-commit at wald.intevation.org
Wed Nov 14 17:26:39 CET 2012


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1352910295 -3600
# Node ID be9e28cff0c4721943f6baa4070ce24952377b3f
# Parent  504cd580178516374134d7641548285fd57a1d3a
Parse and store year in sediment densities.

* Year is parsed ('guessed') from description column in .csv file.

diff -r 504cd5801785 -r be9e28cff0c4 flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensityValue.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensityValue.java	Wed Nov 14 17:19:54 2012 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentDensityValue.java	Wed Nov 14 17:24:55 2012 +0100
@@ -25,16 +25,20 @@
 
     protected BigDecimal density;
 
+    private BigDecimal year;
+
     protected String description;
 
 
     public ImportSedimentDensityValue(
         BigDecimal station,
         BigDecimal density,
+        BigDecimal year,
         String     description
     ) {
         this.station     = station;
         this.density     = density;
+        this.year        = year;
         this.description = description;
     }
 
@@ -57,11 +61,13 @@
                 "   sedimentDensity=:sedimentDensity and " +
                 "   station=:station and " +
                 "   density=:density and " +
+                "   year=:year and " +
                 "   description=:description");
 
             query.setParameter("sedimentDensity", sedimentDensity);
             query.setParameter("station", station);
             query.setParameter("density", density);
+            query.setParameter("year", year);
             query.setParameter("description", description);
 
             List<SedimentDensityValue> values = query.list();
@@ -72,6 +78,7 @@
                     sedimentDensity,
                     station,
                     density,
+                    year,
                     description);
 
                 session.save(peer);
diff -r 504cd5801785 -r be9e28cff0c4 flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java	Wed Nov 14 17:19:54 2012 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java	Wed Nov 14 17:24:55 2012 +0100
@@ -5,10 +5,13 @@
 
 import java.math.BigDecimal;
 
+import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.text.ParseException;
 
 import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -19,6 +22,7 @@
 import de.intevation.flys.importer.ImportSedimentDensity;
 import de.intevation.flys.importer.ImportSedimentDensityValue;
 import de.intevation.flys.importer.ImportUnit;
+import de.intevation.flys.utils.DateGuesser;
 
 
 public class SedimentDensityParser extends LineParser {
@@ -26,6 +30,7 @@
     private static final Logger log =
         Logger.getLogger(SedimentDensityParser.class);
 
+    private int densitsyColumn = 1;
 
     public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
 
@@ -36,7 +41,6 @@
     public static final Pattern META_DEPTH =
         Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
 
-
     protected List<ImportSedimentDensity> sedimentDensities;
 
     protected ImportSedimentDensity current;
@@ -89,12 +93,27 @@
         else if (handleMetaDepth(line)) {
             return;
         }
+        else if (handleMetaColumns(line)) {
+            return;
+        }
         else {
             log.warn("Unknown meta line: '" + line + "'");
         }
     }
 
 
+    private boolean handleMetaColumns(String line) {
+        String[] columns = line.split(";");
+        for (int i = 0; i < columns.length; i++) {
+            if (columns[i].contains("Sedimentdichte")) {
+                this.densitsyColumn = i;
+                return true;
+            }
+        }
+        return false;
+    }
+
+
     protected boolean handleMetaUnit(String line) {
         Matcher m = META_UNIT.matcher(line);
 
@@ -151,19 +170,42 @@
             return;
         }
 
+        BigDecimal km;
+        BigDecimal density;
         try {
-            BigDecimal km      = new BigDecimal(nf.parse(vals[0]).doubleValue());
-            BigDecimal density = new BigDecimal(nf.parse(vals[1]).doubleValue());
+            km      = new BigDecimal(nf.parse(vals[0]).doubleValue());
+            density = new BigDecimal(nf.parse(vals[this.densitsyColumn]).doubleValue());
 
-            current.addValue(new ImportSedimentDensityValue(
-                km,
-                density,
-                vals[2])
-            );
         }
         catch (ParseException pe) {
             log.warn("Error while parsing numbers in '" + line + "'");
+            return;
         }
+
+        BigDecimal year = null;
+        try {
+            year =
+                new BigDecimal(nf.parse(vals[vals.length - 1]).doubleValue());
+        }
+        catch(ParseException pe) {
+            try {
+                Date d = DateGuesser.guessDate(vals[vals.length - 1]);
+                Calendar c = Calendar.getInstance();
+                c.setTime(d);
+                year = new BigDecimal(c.get(Calendar.YEAR));
+            }
+            catch (IllegalArgumentException iae) {
+                log.warn("Error while parsing date in '" + line + "'");
+                return;
+            }
+        }
+
+        current.addValue(new ImportSedimentDensityValue(
+            km,
+            density,
+            year,
+            vals[vals.length - 1])
+        );
     }
 
 
diff -r 504cd5801785 -r be9e28cff0c4 flys-backend/src/main/java/de/intevation/flys/model/SedimentDensityValue.java
--- a/flys-backend/src/main/java/de/intevation/flys/model/SedimentDensityValue.java	Wed Nov 14 17:19:54 2012 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/model/SedimentDensityValue.java	Wed Nov 14 17:24:55 2012 +0100
@@ -24,6 +24,7 @@
 
     private BigDecimal station;
     private BigDecimal density;
+    private BigDecimal year;
 
     private String description;
 
@@ -36,11 +37,13 @@
         SedimentDensity sedimentDensity,
         BigDecimal      station,
         BigDecimal      density,
+        BigDecimal      year,
         String          desc
     ) {
         this.sedimentDensity = sedimentDensity;
         this.station         = station;
         this.density         = density;
+        this.year            = year;
         this.description     = desc;
     }
 
@@ -97,5 +100,14 @@
     public void setDescription(String description) {
         this.description = description;
     }
+
+    @Column(name = "year")
+    public BigDecimal getYear() {
+        return year;
+    }
+
+    public void setYear(BigDecimal year) {
+        this.year = year;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list