[Dive4elements-commits] [PATCH 3 of 7] Updated data object and factory for sediment load

Wald Commits scm-commit at wald.intevation.org
Fri Nov 2 14:59:37 CET 2012


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1351864096 -3600
# Node ID 6a65e7ef43c0052cd723cb701afaff12161f58ee
# Parent  8ddab49ff29737decf34d697cc692aefdae40856
Updated data object and factory for sediment load.

* Added new data fields to object and cache key.
* Fixed SQL statement in factory and fill load objects correctly.
*

diff -r 8ddab49ff297 -r 6a65e7ef43c0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/StaticSedimentLoadCacheKey.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/StaticSedimentLoadCacheKey.java	Fri Nov 02 14:44:11 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/StaticSedimentLoadCacheKey.java	Fri Nov 02 14:48:16 2012 +0100
@@ -12,18 +12,21 @@
     private String river;
     private double startKm;
     private double endKm;
-    private Date date;
+    private int sYear;
+    private int eYear;
 
     public StaticSedimentLoadCacheKey(
         String river,
         double startKm,
         double endKm,
-        Date date
+        int sYear,
+        int eYear
     ) {
         this.river = river;
         this.startKm = startKm;
         this.endKm = endKm;
-        this.date = date;
+        this.sYear = sYear;
+        this.eYear = eYear;
     }
 
     public int hashCode() {
@@ -31,7 +34,8 @@
         builder.append(river);
         builder.append(startKm);
         builder.append(endKm);
-        builder.append(date);
+        builder.append(sYear);
+        builder.append(eYear);
         return builder.toHashCode();
     }
 
@@ -43,6 +47,7 @@
         return this.river == o.river &&
             this.startKm == o.startKm &&
             this.endKm == o.endKm &&
-            this.date == o.date;
+            this.sYear == o.sYear &&
+            this.eYear == o.eYear;
     }
 }
diff -r 8ddab49ff297 -r 6a65e7ef43c0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoad.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoad.java	Fri Nov 02 14:44:11 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoad.java	Fri Nov 02 14:48:16 2012 +0100
@@ -1,8 +1,13 @@
 package de.intevation.flys.artifacts.model.minfo;
 
 import gnu.trove.TDoubleArrayList;
+import gnu.trove.TDoubleByteHashMap;
+import gnu.trove.TDoubleHash;
+import gnu.trove.TDoubleHashSet;
 
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Set;
 
 import org.apache.log4j.Logger;
 
@@ -19,14 +24,10 @@
     protected Date end;
     protected boolean isEpoch;
 
-    protected TDoubleArrayList sand_values;
-    protected TDoubleArrayList fine_middle_values;
-    protected TDoubleArrayList coarse_values;
-    protected TDoubleArrayList susp_sediment_values;
-    protected TDoubleArrayList susp_sand_bed_values;
-
+    protected HashMap<Double, SedimentLoadFraction> kms;
 
     public SedimentLoad() {
+        kms = new HashMap<Double, SedimentLoadFraction>();
     }
 
     public SedimentLoad(
@@ -35,6 +36,7 @@
         Date end,
         boolean isEpoch
     ) {
+        this();
         this.description = description;
         this.start = start;
         this.end = end;
@@ -73,63 +75,95 @@
         this.isEpoch = isEpoch;
     }
 
-    public void addSandValue(double value) {
-        this.sand_values.add(value);
+    public Set<Double> getKms() {
+        return kms.keySet();
     }
 
-    public void addSandValues(TDoubleArrayList values) {
-        this.sand_values.add(values.toNativeArray());
+    public void addKm(double km, SedimentLoadFraction fraction) {
+        kms.put(km, fraction);
     }
 
-    public TDoubleArrayList getSandValues() {
-        return this.sand_values;
+    public SedimentLoadFraction getFraction(double km) {
+        if (kms.get(km) == null) {
+            return new SedimentLoadFraction();
+        }
+        return kms.get(km);
     }
 
-    public void addFineMiddleValue(double value) {
-        this.fine_middle_values.add(value);
+    public void setCoarse(double km, double coarse) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setCoarse(coarse);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setCoarse(coarse);
+            kms.put(km, f);
+        }
     }
 
-    public void addFineMiddleValues(TDoubleArrayList values) {
-        this.fine_middle_values.add(values.toNativeArray());
+    public void setFineMiddle(double km, double fine_middle) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setFine_middle(fine_middle);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setFine_middle(fine_middle);
+            kms.put(km, f);
+        }
     }
 
-    public TDoubleArrayList getFineMiddleValues() {
-        return this.fine_middle_values;
+    public void setSand(double km, double sand) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setSand(sand);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setSand(sand);
+            kms.put(km, f);
+        }
     }
 
-    public void addCoarseValue(double value) {
-        this.coarse_values.add(value);
+    public void setSuspSand(double km, double susp_sand) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setSusp_sand(susp_sand);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setSusp_sand(susp_sand);
+            kms.put(km, f);
+        }
     }
 
-    public void addCoarseValues(TDoubleArrayList values) {
-        this.coarse_values.add(values.toNativeArray());
+    public void setSuspSandBed(double km, double susp_sand_bed) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setSusp_sand_bed(susp_sand_bed);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setSusp_sand_bed(susp_sand_bed);
+            kms.put(km, f);
+        }
     }
 
-    public TDoubleArrayList getCoarseValues() {
-        return this.coarse_values;
+    public void setSuspSediment(double km, double susp_sediment) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setSusp_sediment(susp_sediment);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setSusp_sediment(susp_sediment);
+            kms.put(km, f);
+        }
     }
 
-    public void addSuspSedimentValue(double value) {
-        this.susp_sediment_values.add(value);
-    }
-
-    public void addSuspSedimentValues(TDoubleArrayList values) {
-        this.susp_sediment_values.add(values.toNativeArray());
-    }
-
-   public TDoubleArrayList getSuspSedimentValues() {
-        return this.susp_sediment_values;
-    }
-
-    public void addSuspSandBedValue(double value) {
-        this.susp_sand_bed_values.add(value);
-    }
-
-    public void addSuspSandBedValues(TDoubleArrayList values) {
-        this.susp_sand_bed_values.add(values.toNativeArray());
-    }
-
-    public TDoubleArrayList getSuspSandBedValues() {
-        return this.susp_sand_bed_values;
+    public void setTotal(double km, double total) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setTotal(total);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setTotal(total);
+            kms.put(km, f);
+        }
     }
 }
diff -r 8ddab49ff297 -r 6a65e7ef43c0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java	Fri Nov 02 14:44:11 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java	Fri Nov 02 14:48:16 2012 +0100
@@ -58,32 +58,34 @@
         "SELECT" +
         "       sy.description AS description, " +
         "       ti.start_time AS year, " +
-        "       syv.value AS load " +
+        "       syv.value AS load, " +
+        "       syv.station AS km " +
         "   FROM     sediment_yield sy " +
         "       JOIN rivers r ON sy.river_id = r.id " +
         "       JOIN time_intervals ti ON sy.time_interval_id = ti.id " +
-        "       JOIN sediment_yield_vales syv ON sy.id = syv.sediment_yield_id " +
+        "       JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " +
         "       JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " +
         "   WHERE   r.name = :name " +
         "       AND ti.start_time BETWEEN :begin AND :end " +
-        "       AND ti_stop_time IS NULL " +
+        "       AND ti.stop_time IS NULL " +
         "       AND gf.name = :grain " +
         "       AND syv.station BETWEEN :startKm AND :endKm";
 
     public static final String SQL_SELECT_EPOCHS_DATA =
         "SELECT" +
-        "       sy.description AS description," +
-        "       ti.start_time AS year," +
-        "       syv.value AS load" +
+        "       sy.description AS description, " +
+        "       ti.start_time AS year, " +
+        "       syv.value AS load, " +
+        "       syv.station AS km " +
         "   FROM     sediment_yield sy" +
         "       JOIN rivers r ON sy.river_id = r.id " +
         "       JOIN time_intervals ti ON sy.time_interval_id = ti.id" +
-        "       JOIN sediment_yield_vales syv ON sy.id = syv.sediment_yield_id" +
+        "       JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id" +
         "       JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id" +
         "   WHERE   r.name = :name" +
         "       AND ti.start_time BETWEEN :sbegin AND :send" +
-        "       AND ti_stop_time IS NOT NULL" +
-        "       AND ti_stop_time BETWEEN :ebegin AND :eend" +
+        "       AND ti.stop_time IS NOT NULL" +
+        "       AND ti.stop_time BETWEEN :ebegin AND :eend" +
         "       AND gf.name = :grain " +
         "       AND syv.station BETWEEN :startKm AND :endKm";
 
@@ -108,7 +110,7 @@
         }
 
         StaticSedimentLoadCacheKey key =
-            new StaticSedimentLoadCacheKey(river, startKm, endKm, null);
+            new StaticSedimentLoadCacheKey(river, startKm, endKm, 0, 0);
 
         Element element = cache.get(key);
 
@@ -121,7 +123,7 @@
             getSedimentLoadsUncached(river, type, startKm, endKm);
 
         if (values != null && key != null) {
-            log.debug("Store static sediment loads  values in cache.");
+            log.debug("Store static sediment load values in cache.");
             element = new Element(key, values);
             cache.put(element);
         }
@@ -133,8 +135,8 @@
         String type,
         double startKm,
         double endKm,
-        Date startDate,
-        Date endDate
+        int syear,
+        int eyear
     ) {
         log.debug("SedimentLoadFactory.getLoadWithData");
         Cache cache = CacheFactory.getCache(LOAD_DATA_CACHE_NAME);
@@ -146,12 +148,12 @@
                 type,
                 startKm,
                 endKm,
-                startDate,
-                endDate);
+                syear,
+                eyear);
         }
 
         StaticSedimentLoadCacheKey key =
-            new StaticSedimentLoadCacheKey(river, startKm, endKm, startDate);
+            new StaticSedimentLoadCacheKey(river, startKm, endKm, syear, eyear);
 
         Element element = cache.get(key);
 
@@ -160,8 +162,13 @@
             return (SedimentLoad)element.getValue();
         }
 
-        SedimentLoad values =
-            getSedimentLoadWithDataUncached(river, type, startKm, endKm, startDate, endDate);
+        SedimentLoad values = getSedimentLoadWithDataUncached(
+            river,
+            type,
+            startKm,
+            endKm,
+            syear,
+            eyear);
 
         if (values != null && key != null) {
             log.debug("Store static bed height values in cache.");
@@ -243,58 +250,52 @@
         String type,
         double startKm,
         double endKm,
-        Date sdate,
-        Date edate
+        int syear,
+        int eyear
     ) {
-        log.debug("SedimentLoadFactory.getBedHeightUncached");
+        log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached");
 
         Session session = SessionHolder.HOLDER.get();
         SQLQuery sqlQuery = null;
 
-        Calendar cal = Calendar.getInstance();
-        cal.setTime(sdate);
-        int year = cal.get(Calendar.YEAR);
-        cal.set(year, 1, 1);
+        Calendar start = Calendar.getInstance();
+        start.set(syear, 1, 1);
         Calendar end = Calendar.getInstance();
-        end.set(year, 12, 31);
+        end.set(syear, 12, 31);
 
-        if (type.equals("single")) {
+        if (type.equals("year") || type.equals("epoch")) {
             sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA)
                 .addScalar("description", StandardBasicTypes.STRING)
                 .addScalar("year", StandardBasicTypes.DATE)
-                .addScalar("load", StandardBasicTypes.DOUBLE);
+                .addScalar("load", StandardBasicTypes.DOUBLE)
+                .addScalar("km", StandardBasicTypes.DOUBLE);
             sqlQuery.setString("name", river);
             sqlQuery.setDouble("startKm", startKm);
             sqlQuery.setDouble("endKm", endKm);
-            sqlQuery.setDate("begin", cal.getTime());
+            sqlQuery.setDate("begin", start.getTime());
             sqlQuery.setDate("end", end.getTime());
             sqlQuery.setString("grain", "total");
             List<Object []> results = sqlQuery.list();
             SedimentLoad load = new SedimentLoad();
-            if (results.size() != 1) {
-                // should not happen. throw some exception.
-                return new SedimentLoad();
-            }
             Object[] row = results.get(0);
             load = new SedimentLoad(
                     (String) row[0],
                     (Date) row[1],
                     null,
                     false);
-            load.addCoarseValues(getValues("coarse", sqlQuery));
-            load.addFineMiddleValues(getValues("fine_middle", sqlQuery));
-            load.addSandValues(getValues("sand", sqlQuery));
-            load.addSuspSandBedValues(getValues("suspended_sediment", sqlQuery));
-            load.addSuspSandBedValues(getValues("susp_sand_bed", sqlQuery));
+            getValues("coarse", sqlQuery, load);
+            getValues("fine_middle", sqlQuery, load);
+            getValues("sand", sqlQuery, load);
+            getValues("suspended_sediment", sqlQuery, load);
+            getValues("susp_sand_bed", sqlQuery, load);
+            getValues("susp_sand", sqlQuery, load);
             return load;
         }
-        else if (type.equals("epoch")) {
-            Calendar send = Calendar.getInstance();
-            send.setTime(edate);
-            int eyear = send.get(Calendar.YEAR);
-            send.set(year, 1, 1);
-            Calendar eend = Calendar.getInstance();
-            eend.set(eyear, 12, 31);
+        else if (type.equals("off_epoch")) {
+            Calendar toStart = Calendar.getInstance();
+            toStart.set(eyear, 1, 1);
+            Calendar toEnd = Calendar.getInstance();
+            toEnd.set(eyear, 12, 31);
 
             sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS)
                 .addScalar("description", StandardBasicTypes.STRING)
@@ -304,30 +305,32 @@
             sqlQuery.setString("name", river);
             sqlQuery.setDouble("startKm", startKm);
             sqlQuery.setDouble("endKm", endKm);
-            sqlQuery.setDate("sbegin", cal.getTime());
+            sqlQuery.setDate("sbegin", start.getTime());
             sqlQuery.setDate("sbegin", end.getTime());
-            sqlQuery.setDate("ebegin",send.getTime());
-            sqlQuery.setDate("eend", eend.getTime());
+            sqlQuery.setDate("ebegin",toStart.getTime());
+            sqlQuery.setDate("eend", toEnd.getTime());
             sqlQuery.setString("grain", "total");
 
             List<Object []> results = sqlQuery.list();
 
             SedimentLoad load = new SedimentLoad();
-            if (results.size() != 1) {
-                // should not happen. throw some exception.
-                return new SedimentLoad();
-            }
             Object[] row = results.get(0);
             load = new SedimentLoad(
                     (String) row[0],
                     (Date) row[1],
                     null,
                     false);
-            load.addCoarseValues(getValues("coarse", sqlQuery));
-            load.addFineMiddleValues(getValues("fine_middle", sqlQuery));
-            load.addSandValues(getValues("sand", sqlQuery));
-            load.addSuspSandBedValues(getValues("suspended_sediment", sqlQuery));
-            load.addSuspSandBedValues(getValues("susp_sand_bed", sqlQuery));
+            TDoubleArrayList kms = new TDoubleArrayList();
+            for (int i = 0; i < results.size(); i++) {
+                row = results.get(i);
+                kms.add((Double)row[3]);
+            }
+            getValues("coarse", sqlQuery, load);
+            getValues("fine_middle", sqlQuery, load);
+            getValues("sand", sqlQuery, load);
+            getValues("suspended_sediment", sqlQuery, load);
+            getValues("susp_sand_bed", sqlQuery, load);
+            getValues("susp_sand", sqlQuery, load);
             return load;
         }
         return new SedimentLoad();
@@ -336,17 +339,38 @@
     /**
      * 
      */
-    protected static TDoubleArrayList getValues (
+    protected static void getValues (
         String fraction,
-        SQLQuery query
+        SQLQuery query,
+        SedimentLoad load
     ) {
         query.setString("grain", fraction);
         List<Object[]> results = query.list();
-        TDoubleArrayList values = new TDoubleArrayList();
         for (int i = 0; i < results.size(); i++) {
             Object[] row = results.get(i);
-            values.add(((Double)row[2]).doubleValue());
+            double km = (Double)row[3];
+            double v = -1;
+            if (row[2] != null) {
+                v = ((Double)row[2]).doubleValue();
+            }
+            if (fraction.equals("coarse")) {
+                load.setCoarse(km, v);
+            }
+            else if (fraction.equals("sand")) {
+                load.setSand(km, v);
+            }
+            else if (fraction.equals("fine_middle")) {
+                load.setFineMiddle(km, v);
+            }
+            else if (fraction.equals("suspended_sediment")) {
+                load.setSuspSediment(km, v);
+            }
+            else if (fraction.equals("sups_sand")) {
+                load.setSuspSand(km, v);
+            }
+            else if (fraction.equals("susp_sand_bed")) {
+                load.setSuspSandBed(km, v);
+            }
         }
-        return values;
     }
 }


More information about the Dive4elements-commits mailing list