view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 97:fce237b6fdb8

Bug fixed. Now all Values will be used to calculate the Statistics gnv-artifacts/trunk@143 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Mon, 28 Sep 2009 09:53:17 +0000
parents 13402ac8d8fe
children 156db25ad4b4
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.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.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);
    private static boolean sDebug = log.isDebugEnabled();

    private Collection<Statistic> statistics = null;

    public TimeseriesStatistics(){
        super();
    }
    
    public Collection<Statistic> calculateStatistics(Collection<Result> resultSet) throws StatisticsException {
        DescriptiveStatistics lStatistics = null;
        SimpleRegression lRegression = null;
        statistics = new ArrayList<Statistic>();
        int break1, break2;
        int lSeries = 1;
        try{
            
            Iterator<Result> resultIterator = resultSet.iterator();
            if (resultIterator.hasNext()){
                Result row = resultIterator.next();
                break1 = row.getInteger("GROUP1");
                break2 = row.getInteger("GROUP2");
            lRegression = new SimpleRegression();
            lStatistics = DescriptiveStatistics.newInstance();
            while (resultIterator.hasNext()) {
                
                if (break1 != row.getInteger("GROUP1")
                        || break2 != row.getInteger("GROUP2")) {
                    addStatisticsValues(lStatistics, lRegression, lSeries);
                    
                    lStatistics.clear();
                    lRegression.clear();
                    
                    lStatistics.addValue(row.getDouble("YORDINATE"));
                    Double x = new Double( (row.getDate("XORDINATE")).getTime()/1000/3600) ;
                    lRegression.addData(x ,row.getDouble("YORDINATE"));
                    
                    break1 = row.getInteger("GROUP1");
                    break2 = row.getInteger("GROUP2");
                    row = resultIterator.next();
                    lSeries ++;     
                }
                else{
                    
                    lStatistics.addValue(row.getDouble("YORDINATE"));
                    Double x = new Double( (row.getDate("XORDINATE")).getTime()/1000/3600) ;
                    lRegression.addData(x,row.getDouble("YORDINATE"));
                    row = resultIterator.next();
                }
                
            }
            addStatisticsValues(lStatistics, lRegression,lSeries);
            lStatistics.clear();
            lRegression.clear();
            }
        }
        catch (Exception e){
            log.error(e.getMessage(), e);
        }
        
        return statistics;
    }
    
    /**
     * @param lStatistics
     * @param lRegression
     * @param lStats
     * @param lSeries
     */
    private void addStatisticsValues(DescriptiveStatistics lStatistics,
            SimpleRegression lRegression, int lSeries) {
        statistics.add(new Statistic("gnviewer.statistics.series",(double)lSeries));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.arithmeticMean", lStatistics.getMean()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.kurtosis", lStatistics.getKurtosis()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.max", lStatistics.getMax()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.min", lStatistics.getMin()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.n", lStatistics.getN()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.percentile.90", lStatistics.getPercentile(90)));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.percentile.75", lStatistics.getPercentile(75)));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.percentile.50", lStatistics.getPercentile(50)));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.percentile.10", lStatistics.getPercentile(10)));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.deviation", lStatistics.getStandardDeviation()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.variance", lStatistics.getVariance()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.intercept", lRegression.getIntercept()));
        statistics.add(new Statistic("gnviewer.statistics.descriptive.slope", lRegression.getSlope()));
    } 
}

http://dive4elements.wald.intevation.org