changeset 4546:af1938d4e957

New field in sediment load object and calculation. * Factory reads total load value. * New field in sediment load object. * State adds new facet to generate output for this type of data.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 15 Nov 2012 17:44:07 +0100
parents bb8b1c182913
children 5bda0e119f7c
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoad.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFraction.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadResult.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/minfo/SedimentLoadCalculate.java
diffstat 5 files changed, 99 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoad.java	Thu Nov 15 17:38:48 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoad.java	Thu Nov 15 17:44:07 2012 +0100
@@ -17,7 +17,6 @@
     protected Date end;
     protected boolean isEpoch;
 
-    /** Maps kms. */
     protected HashMap<Double, SedimentLoadFraction> kms;
 
     public SedimentLoad() {
@@ -150,6 +149,17 @@
         }
     }
 
+    public void setLoadTotal(double km, double total) {
+        if (kms.containsKey(km)) {
+            kms.get(km).setLoadTotal(total);
+        }
+        else {
+            SedimentLoadFraction f = new SedimentLoadFraction();
+            f.setLoadTotal(total);
+            kms.put(km, f);
+        }
+    }
+
     public void setTotal(double km, double total) {
         if (kms.containsKey(km)) {
             kms.get(km).setTotal(total);
@@ -205,5 +215,13 @@
         }
         return false;
     }
+
+    public boolean hasTotalLoad() {
+        for (SedimentLoadFraction slf : kms.values()) {
+            if (slf.getLoadTotal() > 0d) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java	Thu Nov 15 17:38:48 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFactory.java	Thu Nov 15 17:44:07 2012 +0100
@@ -2,6 +2,7 @@
 
 import gnu.trove.TDoubleArrayList;
 
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
@@ -12,6 +13,7 @@
 import org.apache.log4j.Logger;
 import org.hibernate.SQLQuery;
 import org.hibernate.Session;
+import org.hibernate.exception.SQLGrammarException;
 import org.hibernate.type.StandardBasicTypes;
 
 import de.intevation.flys.artifacts.cache.CacheFactory;
@@ -75,18 +77,17 @@
         "SELECT" +
         "       sy.description AS description, " +
         "       ti.start_time AS startYear, " +
-        "       ti.stop_time AS endYear, " +
         "       syv.value AS load, " +
-        "       syv.station AS km " +
-        "   FROM     sediment_yield sy" +
+        "       syv.station AS km," +
+        "       ti.stop_time AS endYear " +
+        "   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_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" +
+        "       JOIN time_intervals ti ON sy.time_interval_id = ti.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 BETWEEN :ebegin AND :eend " +
         "       AND gf.name = :grain " +
         "       AND syv.station BETWEEN :startKm AND :endKm";
 
@@ -258,9 +259,9 @@
         SQLQuery sqlQuery = null;
 
         Calendar start = Calendar.getInstance();
-        start.set(syear, 1, 1);
+        start.set(syear - 1, 11, 31);
         Calendar end = Calendar.getInstance();
-        end.set(syear, 12, 31);
+        end.set(syear, 11, 30);
 
         if (type.equals("year") || type.equals("epoch")) {
             sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLES_DATA)
@@ -293,16 +294,15 @@
         }
         else if (type.equals("off_epoch")) {
             Calendar toStart = Calendar.getInstance();
-            toStart.set(eyear, 1, 1);
+            toStart.set(eyear - 1, 11, 31);
             Calendar toEnd = Calendar.getInstance();
-            toEnd.set(eyear, 12, 31);
-
+            toEnd.set(eyear, 11, 30);
             sqlQuery = session.createSQLQuery(SQL_SELECT_EPOCHS_DATA)
                 .addScalar("description", StandardBasicTypes.STRING)
                 .addScalar("startYear", StandardBasicTypes.DATE)
-                .addScalar("stopYear", StandardBasicTypes.DATE)
                 .addScalar("load", StandardBasicTypes.DOUBLE)
-                .addScalar("km", StandardBasicTypes.DOUBLE);
+                .addScalar("km", StandardBasicTypes.DOUBLE)
+                .addScalar("endYear", StandardBasicTypes.DATE);
             sqlQuery.setString("name", river);
             sqlQuery.setDouble("startKm", startKm);
             sqlQuery.setDouble("endKm", endKm);
@@ -312,19 +312,21 @@
             sqlQuery.setDate("eend", toEnd.getTime());
             sqlQuery.setString("grain", "total");
 
-            List<Object []> results = sqlQuery.list();
+            List<Object[]> results = null;
+            results = sqlQuery.list();
 
             SedimentLoad load = new SedimentLoad();
             Object[] row = results.get(0);
             load = new SedimentLoad(
                     (String) row[0],
                     (Date) row[1],
-                    null,
-                    false);
+                    (Date) row[4],
+                    true);
             TDoubleArrayList kms = new TDoubleArrayList();
             for (int i = 0; i < results.size(); i++) {
                 row = results.get(i);
                 kms.add((Double)row[3]);
+                load.setLoadTotal((Double)row[3], (Double)row[2]);
             }
             getValues("coarse", sqlQuery, load);
             getValues("fine_middle", sqlQuery, load);
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFraction.java	Thu Nov 15 17:38:48 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFraction.java	Thu Nov 15 17:44:07 2012 +0100
@@ -12,6 +12,7 @@
     double susp_sand;
     double susp_sand_bed;
     double susp_sediment;
+    double loadTotal;
     double total;
 
     public SedimentLoadFraction() {
@@ -21,6 +22,7 @@
         susp_sand = 0d;
         susp_sand_bed = 0d;
         susp_sediment = 0d;
+        loadTotal = 0d;
     }
 
     public double getSand() {
@@ -78,4 +80,12 @@
     public void setTotal(double total) {
         this.total = total;
     }
+
+    public double getLoadTotal() {
+        return loadTotal;
+    }
+
+    public void setLoadTotal(double total) {
+        this.loadTotal = total;
+    }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadResult.java	Thu Nov 15 17:38:48 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadResult.java	Thu Nov 15 17:44:07 2012 +0100
@@ -157,6 +157,22 @@
         };
     }
 
+    public double[][] getTotalLoadData() {
+        Set<Double> kms = this.load.getKms();
+        TDoubleArrayList k = new TDoubleArrayList();
+        TDoubleArrayList ss = new TDoubleArrayList();
+        for (double km : kms) {
+            if (load.getFraction(km).getLoadTotal() > 0d) {
+                k.add(km);
+                ss.add(load.getFraction(km).getLoadTotal());
+            }
+        }
+        return new double [][] {
+            k.toNativeArray(),
+            ss.toNativeArray()
+        };
+    }
+
     public boolean hasCoarseData() {
         return getCoarseData()[0].length > 0;
     }
@@ -181,6 +197,10 @@
         return getSuspSedimentData()[0].length > 0;
     }
 
+    public boolean hasTotalLoadData() {
+        return getTotalLoadData()[0].length > 0;
+    }
+
     public boolean hasTotalData() {
         return getTotalData()[0].length > 0;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/minfo/SedimentLoadCalculate.java	Thu Nov 15 17:38:48 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/minfo/SedimentLoadCalculate.java	Thu Nov 15 17:44:07 2012 +0100
@@ -37,6 +37,7 @@
     public static final String I18N_FACET_SEDIMENTLOAD_SUSPSAND = "facet.sedimentload.susp_sand";
     public static final String I18N_FACET_SEDIMENTLOAD_SUSPSANDBED = "facet.sediemntload.susp_sand_bed";
     public static final String I18N_FACET_SEDIMENTLOAD_SUSPSEDIMENT = "facet.sedimentload.susp_sediment";
+    public static final String I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD = "facet.sedimentload.total_load";
     public static final String I18N_FACET_SEDIMENTLOAD_TOTAL = "facet.sedimentload.total";
 
     @Override
@@ -278,6 +279,19 @@
                     hash));
 
             }
+            if (res.hasTotalLoadData()) {
+                newFacets.add(new SedimentLoadFacet(
+                    idx,
+                    SEDIMENT_LOAD_TOTAL_LOAD,
+                    Resources.getMsg(
+                        meta,
+                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD,
+                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD) +
+                        " - " + res.getStartYear() + "-" + res.getEndYear(),
+                    ComputeType.ADVANCE,
+                    stateId,
+                    hash));
+            }
             if (res.hasTotalData()) {
                 newFacets.add(new SedimentLoadFacet(
                     idx,
@@ -387,6 +401,19 @@
                     hash));
 
             }
+            if (res.hasTotalLoadData()) {
+                newFacets.add(new SedimentLoadFacet(
+                    idx,
+                    SEDIMENT_LOAD_TOTAL_LOAD,
+                    Resources.getMsg(
+                        meta,
+                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD,
+                        I18N_FACET_SEDIMENTLOAD_TOTAL_LOAD) +
+                        " - " + res.getStartYear() + "-" + res.getEndYear(),
+                    ComputeType.ADVANCE,
+                    stateId,
+                    hash));
+            }
             if (res.hasTotalData()) {
                 newFacets.add(new SedimentLoadFacet(
                     idx,

http://dive4elements.wald.intevation.org