# HG changeset patch # User Sascha L. Teichmann # Date 1308844244 0 # Node ID 078934923acbbe4f69396c94d83f00fe516466f2 # Parent 56bcf56b1fe002c3c85383822ea5cb032e789d92 New AT writer for results. flys-artifacts/trunk@2219 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 56bcf56b1fe0 -r 078934923acb flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Jun 23 13:42:47 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Jun 23 15:50:44 2011 +0000 @@ -1,3 +1,9 @@ +2011-06-23 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/exports/ATWriter.java: New. + Writer for AT files. New code because our data model differs + from Desktop-FLYS. Needs testing. + 2011-06-23 Ingo Weinzierl flys/issue157 (Diagramm: Ursprung berechnete Abflusskurve) diff -r 56bcf56b1fe0 -r 078934923acb flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java Thu Jun 23 15:50:44 2011 +0000 @@ -0,0 +1,149 @@ +package de.intevation.flys.exports; + +import java.io.IOException; +import java.io.Writer; +import java.io.PrintWriter; + +import java.util.Locale; + +import de.intevation.flys.artifacts.model.WQ; + +import org.apache.commons.math.analysis.interpolation.SplineInterpolator; + +import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; + +import org.apache.commons.math.ArgumentOutsideDomainException; + +import org.apache.log4j.Logger; + +public class ATWriter +{ + private static Logger logger = Logger.getLogger(ATWriter.class); + + public static final int COLUMNS = 10; + + public static final String EMPTY = " "; + + protected double minW; + protected double maxW; + protected double minQ; + protected double maxQ; + + protected PolynomialSplineFunction qFunc; + + public ATWriter() { + } + + public ATWriter(WQ wq) throws IllegalArgumentException { + + int maxIndex = maxIncreasingWIndex(wq); + + if (maxIndex < 1) { // Only first w can be written out. + minW = maxW = wq.getW(0); + minQ = maxQ = wq.getQ(0); + return; + } + + double [] ws = new double[maxIndex+1]; + double [] qs = new double[ws.length]; + + for (int i = 0; i < ws.length; ++i) { + ws[i] = wq.getW(i); + qs[i] = wq.getQ(i); + } + + SplineInterpolator interpolator = new SplineInterpolator(); + + qFunc = interpolator.interpolate(ws, qs); + + minW = wq.getW(0); + maxW = wq.getW(maxIndex); + minQ = wq.getQ(0); + maxQ = wq.getQ(maxIndex); + } + + public static int maxIncreasingWIndex(WQ wq) { + + int N = wq.size(); + + if (N < 2) { + return N-1; + } + + double last = wq.getW(0); + + for (int i = 1; i < N; ++i) { + double current = wq.getW(i); + if (current <= last) { + return i-1; + } + } + + return N-1; + } + + public double getQ(double w) { + + try { + return qFunc.value(w); + } + catch (ArgumentOutsideDomainException aode) { + // should not happen + logger.warn("spline interpolation failed", aode); + return w <= minW ? minQ : maxQ; + } + } + + protected static void printQ(PrintWriter out, double q) { + String format; + if (q < 1) format = " % 8.3f"; + else if (q < 10) format = " % 8.2f"; + else if (q < 100) format = " % 8.1f"; + else { + format = " % 8.0f"; + if (q > 1000) q = Math.rint(q/10)*10; + } + out.printf(Locale.US, format, q); + } + + public void write(Writer writer) throws IOException { + + PrintWriter out = new PrintWriter(writer); + + double range = COLUMNS; + + double rest = Math.abs(minW % range); + + int startW = (int)(minW - rest); + if (rest >= 1d) { + out.printf(Locale.US, "%8d", startW); + int columns = (int)Math.floor(rest); + for (int i = columns; i < COLUMNS; ++i) { + out.print(EMPTY); + } + for (int i = 0, N = COLUMNS-columns; i < N; ++i) { + printQ(out, getQ(startW + (COLUMNS-i-1))); + } + startW += COLUMNS; + } + + int col = 0; + for (int w = startW; w <= maxW; ++w) { + if (col == 0) out.printf(Locale.US, "%8d", w); + + printQ(out, getQ(w)); + + if (++col >= COLUMNS) { + out.println(); + col = 0; + } + } + + if (col > 0) { + out.println(); + } + + out.flush(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :