view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 1108:337727011de0

Removed duplicate xmlns:xlink definition. gnv-artifacts/trunk@1239 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 29 Jun 2010 08:47:46 +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