view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 1101:e38f93d64253

A depth in an odv export has a negative value now (issue230). gnv-artifacts/trunk@1227 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 28 Jun 2010 09:33:11 +0000
parents 5659b5c5e4b5
children f953c9a559d8
line wrap: on
line source
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();

                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 void clear();

    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