view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 364:2413273f1c13

Workarround: Store lower and upper bounds of data while iterating over all data and set the max range of axes with these information. JFreeCharts method NumberAxis.setAutoRange(true) doesn't seem to work properly. gnv-artifacts/trunk@439 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Dec 2009 11:58:44 +0000
parents 59ad8f37a590
children 04a242c67fe6
line wrap: on
line source
/**
 * Title:           TimeseriesStatistics, $Header: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v 1.3 2008/08/18 14:50:33 drewnak Exp $
 * Source:          $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v $
 * created by:      Stefan Blume (blume)
 * erstellt am:     06.12.2007
 * Copyright:       con terra GmbH, 2005
 *
 * modified by:     $Author: drewnak $
 * modified on:     $Date: 2008/08/18 14:50:33 $
 * Version:         $Revision: 1.3 $
 * TAG:             $Name:  $
 * locked from:     $Locker:  $
 * CVS State:       $State: Exp $
 * Project:         $ProjectName$
 */
package de.intevation.gnv.statistics;

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;

import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.state.describedata.KeyValueDescibeData;
import de.intevation.gnv.statistics.exception.StatisticsException;

/**
 * The class <code>TimeseriesStatistics</code> fulfills the following purposes:
 * <ol>
 * <li></li>
 * </ol>
 * 
 * @author blume
 * @author Tim Englich <tim.englich@intevation.de>
 * @version 1.0
 * @serial 1.0
 * @see
 * @since 06.12.2007 18:02:27
 */
public class TimeseriesStatistics implements Statistics {

    /**
     * Default Logging instance
     */
    private static Logger log = Logger.getLogger(TimeseriesStatistics.class);

    /**
     * Constructor
     */
    public TimeseriesStatistics() {
        super();
    }

    /**
     * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection)
     */
    public Collection<StatisticSet> calculateStatistics(
                                                     Collection<Result> resultSet,
                                                     Collection<KeyValueDescibeData> parameters,
                                                     Collection<KeyValueDescibeData> measurements,
                                                     Collection<KeyValueDescibeData> dates
                                                     )
                                                     throws StatisticsException {
        this.clearStatistics();
        DescriptiveStatistics lStatistics = null;
        SimpleRegression lRegression = null;
        Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
        String break1, break2, break3;
        int lSeries = 1;
        try {

            Iterator<Result> resultIterator = resultSet.iterator();
            if (resultIterator.hasNext()) {
                Result row = resultIterator.next();
                Result previousRow = row;
                break1 = row.getString("GROUP1");
                break2 = row.getString("GROUP2");
                break3 = row.getString("GROUP3");
                lRegression = new SimpleRegression();
                lStatistics = new DescriptiveStatistics();
                while (resultIterator.hasNext()) {

                    if (!break1.equals(row.getString("GROUP1"))
                        || !break2.equals(row.getString("GROUP2"))
                        || !break3.equals(row.getString("GROUP3"))
                        ) {
                        String statisticsName = this.generateStatisticsName(break1, break2, 
                                                                            break3, parameters, 
                                                                            measurements, dates);
                        statisticSets.add(this.generateStatisticsValues(lStatistics, 
                                                                        lRegression,
                                                                        statisticsName));

                        lStatistics.clear();
                        lRegression.clear();
                        
                        this.clearStatistics();

                        lStatistics.addValue(row.getDouble("YORDINATE"));
                        Double x = this.calculateXOrdinateValue(previousRow,row);
                        lRegression.addData(x, row.getDouble("YORDINATE"));

                        break1 = row.getString("GROUP1");
                        break2 = row.getString("GROUP2");
                        break3 = row.getString("GROUP3");
                        previousRow = row;
                        row = resultIterator.next();
                        lSeries++;
                    } else {

                        lStatistics.addValue(row.getDouble("YORDINATE"));
                        Double x = this.calculateXOrdinateValue(previousRow, row);
                        lRegression.addData(x, row.getDouble("YORDINATE"));
                        previousRow = row;
                        row = resultIterator.next();
                    }

                }
                
                lStatistics.addValue(row.getDouble("YORDINATE"));
                Double x = this.calculateXOrdinateValue(previousRow, row);
                lRegression.addData(x, row.getDouble("YORDINATE"));
                
                String statisticsName = this.generateStatisticsName(break1, break2, 
                                                                    break3, parameters, 
                                                                    measurements, dates);
                
                statisticSets.add(this.generateStatisticsValues(lStatistics, 
                                                                lRegression, 
                                                                statisticsName));
                lStatistics.clear();
                lRegression.clear();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return statisticSets;
    }

    protected void clearStatistics(){}
    
    /**
     * 
     * @param break1
     * @param break2
     * @param break3
     * @param parameters
     * @param measurements
     * @param dates
     * @return
     */
    protected String generateStatisticsName(String break1, 
                                            String break2,
                                            String break3,
                                            Collection<KeyValueDescibeData> parameters,
                                            Collection<KeyValueDescibeData> measurements,
                                            Collection<KeyValueDescibeData> dates){
        log.debug("TimeseriesStatistics.generateStatisticsName");
        return this.findValueTitle(parameters,break1)+ " "+
               this.findValueTitle(measurements,break2) + "m";
    }
    
    
    protected String findValueTitle(Collection<KeyValueDescibeData> values,
                                  String id) {
        log.debug("TimeseriesStatistics.findValueTitle "+ id);
        if (values != null) {
            Iterator<KeyValueDescibeData> it = values.iterator();
            while (it.hasNext()) {
                KeyValueDescibeData data = it.next();
                if (id.equals(data.getKey())) {
                    return data.getValue();
                }
            }
        }
        return "";
    }
    protected double calculateXOrdinateValue(Result previousRow, Result row) throws SQLException {
        return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600);
    }

    /**
     * @param lStatistics
     * @param lRegression
     * @param lStats
     * @param lSeries
     */
    private StatisticSet generateStatisticsValues(DescriptiveStatistics lStatistics,
                                     SimpleRegression lRegression, String  statisticName) {
        
        StatisticSet statisticSet = new StatisticSet(statisticName);
        
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.arithmeticMean", lStatistics
                        .getMean()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.kurtosis", lStatistics
                        .getKurtosis()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.max",
                lStatistics.getMax()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.min",
                lStatistics.getMin()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.n",
                lStatistics.getN()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.90", lStatistics
                        .getPercentile(90)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.75", lStatistics
                        .getPercentile(75)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.50", lStatistics
                        .getPercentile(50)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.10", lStatistics
                        .getPercentile(10)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.deviation", lStatistics
                        .getStandardDeviation()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.variance", lStatistics
                        .getVariance()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.intercept", lRegression
                        .getIntercept()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.slope",
                lRegression.getSlope()));
        return statisticSet;
    }
}

http://dive4elements.wald.intevation.org