view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 851:4bcf7bf4bfb7

Presentation of values in the statistic is now language specific (issue228). gnv-artifacts/trunk@967 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 21 Apr 2010 13:38:26 +0000
parents 05bf8534a35a
children dfd02f8d3602
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 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;

/**
 * This class is used to create a statistic in timeseries products.
 * 
 * @author blume
 */
public class TimeseriesStatistics 
extends      AbstractStatistics
{

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

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


    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> resultSet = (Collection<Result>)result;

        clearStatistics();

        DescriptiveStatistics lStatistics = null;
        SimpleRegression lRegression = null;
        Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
        String break1, break2, break3;
        int lSeries = 1;

        if (resultSet == null) {
            return statisticSets;
        }

        int b1Idx = -1;
        int b2Idx = -1;
        int b3Idx = -1;
        int yIdx  = -1;
        try {

            Iterator<Result> resultIterator = resultSet.iterator();
            if (resultIterator.hasNext()) {
                Result row = resultIterator.next();
                Result previousRow = row;

                if (b1Idx == -1) {
                    ResultDescriptor rd = row.getResultDescriptor();
                    b1Idx = rd.getColumnIndex("GROUP1");
                    b2Idx = rd.getColumnIndex("GROUP2");
                    b3Idx = rd.getColumnIndex("GROUP3");
                    yIdx  = rd.getColumnIndex("YORDINATE");

                    if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
                        return statisticSets;
                    }
                }
                break1 = row.getString(b1Idx);
                break2 = row.getString(b2Idx);
                break3 = row.getString(b3Idx);
                lRegression = new SimpleRegression();
                lStatistics = new DescriptiveStatistics();
                while (resultIterator.hasNext()) {

                    if (!break1.equals(row.getString(b1Idx))
                        || !break2.equals(row.getString(b2Idx))
                        || !break3.equals(row.getString(b3Idx))
                        ) {
                        String statisticsName = generateStatisticsName(
                            break1, break2, 
                            break3, parameters, 
                            measurements, dates);

                        statisticSets.add(
                            generateStatisticsValues(
                                lStatistics, 
                                lRegression,
                                statisticsName));

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

                        Double yValue = row.getDouble(yIdx);

                        if (yValue != null) {
                            lStatistics.addValue(yValue);
                            Double x = calculateXOrdinateValue(previousRow,row);
                            lRegression.addData(x, yValue);
                        }

                        break1 = row.getString(b1Idx);
                        break2 = row.getString(b2Idx);
                        break3 = row.getString(b3Idx);
                        previousRow = row;
                        row = resultIterator.next();
                        lSeries++;
                    } else {

                        Double value = row.getDouble(yIdx);
                        if (value != null) {
                            lStatistics.addValue(value.doubleValue());
                            Double x = calculateXOrdinateValue(previousRow,row);
                            lRegression.addData(x, value.doubleValue());
                        }
                        previousRow = row;
                        row = resultIterator.next();
                    }

                }

                Double yValue = row.getDouble(yIdx);

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

        return statisticSets;
    }

    /**
     * Nothing is done here.
     */
    protected void clearStatistics(){}
    

    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);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org