# HG changeset patch # User Tim Englich # Date 1254126028 0 # Node ID 13402ac8d8fe6b96c4592fd2078abbe687497fbe # Parent 6a825a184883ee0dbb77d98817fe8cc97f66cd63 Put the Statisticscalulation to the Outmethod of the OutputTransition. gnv-artifacts/trunk@140 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 6a825a184883 -r 13402ac8d8fe gnv-artifacts/Changelog --- a/gnv-artifacts/Changelog Mon Sep 28 07:32:03 2009 +0000 +++ b/gnv-artifacts/Changelog Mon Sep 28 08:20:28 2009 +0000 @@ -1,3 +1,11 @@ +2009-09-28 Tim Englich + + * src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java Edited: + Moved the calculating-Methods from the Conmstructor to an separat Method. + Switched the ResultContainer from Array to Collection + * src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java (out) Edited: + Added the Output-Variante for generating Statistics + 2009-09-28 Tim Englich * pom.xml Edited: diff -r 6a825a184883 -r 13402ac8d8fe gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java Mon Sep 28 07:32:03 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java Mon Sep 28 08:20:28 2009 +0000 @@ -46,19 +46,22 @@ /** * Default Logging instance */ - private static Logger sLogger = Logger.getLogger(TimeseriesStatistics.class); - private static boolean sDebug = sLogger.isDebugEnabled(); + private static Logger log = Logger.getLogger(TimeseriesStatistics.class); + private static boolean sDebug = log.isDebugEnabled(); - private Statistic[] mStatistics = null; + private Collection statistics = null; - - public TimeseriesStatistics(Collection resultSet) throws StatisticsException { + public TimeseriesStatistics(){ + super(); + } + + public Collection calculateStatistics(Collection resultSet) throws StatisticsException { DescriptiveStatistics lStatistics = null; SimpleRegression lRegression = null; - ArrayList lStats = new ArrayList(); + statistics = new ArrayList(); int break1, break2; int lSeries = 1; - if (sDebug) sLogger.debug("TimeseriesStatistics() lImageFile != null"); + if (sDebug) log.debug("TimeseriesStatistics() lImageFile != null"); try{ Iterator resultIterator = resultSet.iterator(); @@ -72,10 +75,8 @@ if (break1 != row.getInteger("GROUP1") || break2 != row.getInteger("GROUP2")) { - //lStatistics = createDescriptiveStatisticsObject(pResults,mStart,mEnd); - //lRegression = createSimpleRegressionObject(pResults,mStart,mEnd); - - addStatisticsValues(lStatistics, lRegression, lStats, lSeries); + + addStatisticsValues(lStatistics, lRegression, lSeries); lStatistics.clear(); lRegression.clear(); @@ -97,17 +98,18 @@ } } - addStatisticsValues(lStatistics, lRegression, lStats, lSeries); + addStatisticsValues(lStatistics, lRegression,lSeries); lStatistics.clear(); lRegression.clear(); - mStatistics = new Statistic[lStats.size()]; - mStatistics = lStats.toArray(mStatistics); } } catch (Exception e){ - sLogger.error(e.getMessage(), e); + log.error(e.getMessage(), e); } + + return statistics; } + /** * @param lStatistics * @param lRegression @@ -115,29 +117,20 @@ * @param lSeries */ private void addStatisticsValues(DescriptiveStatistics lStatistics, - SimpleRegression lRegression, ArrayList lStats, - int lSeries) { - lStats.add(new Statistic("gnviewer.statistics.series",(double)lSeries)); - lStats.add(new Statistic("gnviewer.statistics.descriptive.arithmeticMean", lStatistics.getMean())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.kurtosis", lStatistics.getKurtosis())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.max", lStatistics.getMax())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.min", lStatistics.getMin())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.n", lStatistics.getN())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.percentile.90", lStatistics.getPercentile(90))); - lStats.add(new Statistic("gnviewer.statistics.descriptive.percentile.75", lStatistics.getPercentile(75))); - lStats.add(new Statistic("gnviewer.statistics.descriptive.percentile.50", lStatistics.getPercentile(50))); - lStats.add(new Statistic("gnviewer.statistics.descriptive.percentile.10", lStatistics.getPercentile(10))); - lStats.add(new Statistic("gnviewer.statistics.descriptive.deviation", lStatistics.getStandardDeviation())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.variance", lStatistics.getVariance())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.intercept", lRegression.getIntercept())); - lStats.add(new Statistic("gnviewer.statistics.descriptive.slope", lRegression.getSlope())); + 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())); } - - /** - * - * @return - */ - public Statistic[] getStatistics() { - return mStatistics; - } } diff -r 6a825a184883 -r 13402ac8d8fe gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java Mon Sep 28 07:32:03 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java Mon Sep 28 08:20:28 2009 +0000 @@ -11,20 +11,35 @@ import java.util.Collection; import java.util.Iterator; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import org.apache.log4j.Logger; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; import au.com.bytecode.opencsv.CSVWriter; - +import de.intevation.artifactdatabase.XMLUtils; import de.intevation.gnv.chart.ChartFactory; import de.intevation.gnv.chart.ChartLabels; import de.intevation.gnv.chart.ChartStyle; import de.intevation.gnv.chart.exception.TechnicalChartException; import de.intevation.gnv.geobackend.base.Result; +import de.intevation.gnv.statistics.Statistic; +import de.intevation.gnv.statistics.TimeseriesStatistics; +import de.intevation.gnv.statistics.exception.StatisticsException; import de.intevation.gnv.transition.InputData; import de.intevation.gnv.transition.OutputTransitionBase; import de.intevation.gnv.transition.describedata.KeyValueDescibeData; import de.intevation.gnv.transition.describedata.NamedCollection; import de.intevation.gnv.transition.exception.TransitionException; +import de.intevation.gnv.utils.ArtifactXMLUtilities; /** * @author Tim Englich @@ -112,6 +127,12 @@ log.error("No Data given for generation an CSV-File."); throw new TransitionException("No Data given for generation an CSV-File."); } + }else if (outputMode.equalsIgnoreCase("statistics")){ + log.debug("Statistics will be generated."); + TimeseriesStatistics tss = new TimeseriesStatistics(); + Collection statistics = tss.calculateStatistics(this.chartResult); + Document doc = this.writeStatistics2XML(statistics); + this.writeDocument2OutputStream(doc, outputStream); } } catch (IOException e) { log.error(e,e); @@ -119,8 +140,47 @@ } catch (TechnicalChartException e) { log.error(e,e); throw new TransitionException(e); + }catch (StatisticsException e) { + log.error(e,e); + throw new TransitionException(e); } } + + protected void writeDocument2OutputStream(Document document, OutputStream os){ + + try { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(document); + StreamResult result = new StreamResult(os); + transformer.transform(source, result); + } catch (TransformerConfigurationException e) { + log.error(e,e); + } catch (TransformerFactoryConfigurationError e) { + log.error(e,e); + }catch (TransformerException e) { + log.error(e,e); + } + } + + protected Document writeStatistics2XML(Collection statistic){ + ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); + Document doc = XMLUtils.newDocument(); + if (statistic != null){ + Node statisticResults = xmlUtilities.createArtifactElement(doc, "statistic-values"); + doc.appendChild(statisticResults); + Iterator it = statistic.iterator(); + while (it.hasNext()){ + Statistic s = it.next(); + Element result = xmlUtilities.createArtifactElement(doc, "statistic"); + result.setAttribute("name", s.getKey()); + result.setAttribute("value", s.getStringValue()); + statisticResults.appendChild(result); + } + + } + return doc; + } protected String getSelectedFeatureName(){