changeset 1077:1728aac87717

Added statistics for vectorial timeseries. gnv-artifacts/trunk@1179 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 09 Jun 2010 07:04:03 +0000
parents dc9727a67d41
children 5659b5c5e4b5
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesVectorOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesVectorStatistics.java gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java
diffstat 4 files changed, 174 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Jun 08 14:34:31 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Wed Jun 09 07:04:03 2010 +0000
@@ -1,3 +1,17 @@
+2010-06-09  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/statistics/VectorStatistics.java: This is
+	  an abstract base class for statistics of vectorial parameters. There are
+	  two methods that need to be overriden by subclasses.
+
+	* src/main/java/de/intevation/gnv/statistics/TimeseriesVectorStatistics.java:
+	  A concrete statistics class for timeseries statistics of vectorial
+	  parameters.
+
+	* src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesVectorOutputState.java:
+	  Added a new method that returns an instance of TimeseriesVectorStatistics
+	  to create a statistic for this product type.
+
 2010-06-08  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshVectorOutputState.java:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesVectorOutputState.java	Tue Jun 08 14:34:31 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesVectorOutputState.java	Wed Jun 09 07:04:03 2010 +0000
@@ -22,6 +22,9 @@
 
 import de.intevation.gnv.state.exception.StateException;
 
+import de.intevation.gnv.statistics.Statistics;
+import de.intevation.gnv.statistics.TimeseriesVectorStatistics;
+
 import java.io.IOException;
 import java.io.OutputStream;
 
@@ -178,5 +181,10 @@
 
         export.create(profile, outputStream, result);
     }
+
+
+    protected Statistics getStatisticsGenerator() {
+        return new TimeseriesVectorStatistics();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesVectorStatistics.java	Wed Jun 09 07:04:03 2010 +0000
@@ -0,0 +1,30 @@
+package de.intevation.gnv.statistics;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+
+import java.sql.SQLException;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class TimeseriesVectorStatistics
+extends      VectorStatistics
+{
+    public TimeseriesVectorStatistics() {
+    }
+
+
+    protected int getValueIdx(ResultDescriptor rd) {
+        return rd.getColumnIndex("YORDINATE");
+    }
+
+
+    protected double calculateXOrdinateValue(Result prev, Result row)
+    throws SQLException
+    {
+        return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java	Wed Jun 09 07:04:03 2010 +0000
@@ -0,0 +1,122 @@
+package de.intevation.gnv.statistics;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+
+import de.intevation.gnv.state.describedata.KeyValueDescibeData;
+
+import de.intevation.gnv.statistics.exception.StatisticsException;
+
+import java.sql.SQLException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
+
+import org.apache.commons.math.stat.regression.SimpleRegression;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public abstract class VectorStatistics
+extends      AbstractStatistics
+{
+    private static Logger logger =
+        Logger.getLogger(VectorStatistics.class);
+
+    private ResultDescriptor rd;
+
+
+    public VectorStatistics() {
+    }
+
+
+    public Collection<StatisticSet> calculateStatistics(
+        Object                          result,
+        Collection<KeyValueDescibeData> parameters,
+        Collection<KeyValueDescibeData> measurements,
+        Collection<KeyValueDescibeData> dates
+    ) throws StatisticsException {
+        if (!(result instanceof Collection))
+            return new ArrayList<StatisticSet>();
+
+        Collection<Result>     results = (Collection<Result>) result;
+        Collection<StatisticSet> stats = new ArrayList<StatisticSet>();
+
+        SimpleRegression lRegression      = new SimpleRegression();
+        DescriptiveStatistics lStatistics = new DescriptiveStatistics();
+
+        Result previous   = null;
+        Result row        = null;
+        String seriesName = null;
+        int valueIdx      = -1;
+        int seriesIdx     = -1;
+
+        Iterator<Result> iter = results.iterator();
+
+        while (iter.hasNext()) {
+            row      = iter.next();
+            previous = previous == null ? row : previous;
+
+            if (rd == null || seriesIdx == -1) {
+                rd        = row.getResultDescriptor();
+                valueIdx  = getValueIdx(rd);
+                seriesIdx = rd.getColumnIndex("SERIES");
+            }
+
+            if (seriesName != null
+            && !row.getString(seriesIdx).equals(seriesName))
+            {
+                stats.add(generateStatisticsValues(
+                    lStatistics, lRegression, seriesName));
+
+                lStatistics.clear();
+                lRegression.clear();
+            }
+
+            Double yValue = getValue(row, valueIdx);
+
+            if (yValue != null) {
+                lStatistics.addValue(yValue);
+
+                try {
+                    double x = calculateXOrdinateValue(previous, row);
+                    lRegression.addData(x, yValue);
+                }
+                catch (SQLException sqle) {
+                    logger.warn(sqle, sqle);
+                }
+            }
+
+            seriesName = row.getString(seriesIdx);
+            previous   = row;
+        }
+
+        if (seriesName != null && row != null) {
+            stats.add(generateStatisticsValues(
+                lStatistics, lRegression, seriesName));
+
+            lStatistics.clear();
+            lRegression.clear();
+        }
+
+        return stats;
+    }
+
+
+    protected Double getValue(Result row, int idx) {
+        return row.getDouble(idx);
+    }
+
+
+    abstract protected int getValueIdx(ResultDescriptor rd);
+
+    abstract protected double calculateXOrdinateValue(Result prev, Result now)
+    throws SQLException;
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org