Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 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 | |
children | 5659b5c5e4b5 |
line wrap: on
line diff
--- /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 :