comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java @ 1190:f514894ec2fd

merged flys-artifacts/2.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:17 +0200
parents 821aaceb2776
children 7d11ad5a52d5
comparison
equal deleted inserted replaced
917:b48c36076e17 1190:f514894ec2fd
1 package de.intevation.flys.exports;
2
3 import java.io.IOException;
4 import java.io.Writer;
5 import java.io.PrintWriter;
6
7 import java.util.Locale;
8
9 import de.intevation.flys.artifacts.model.WQ;
10
11 import org.apache.commons.math.analysis.UnivariateRealFunction;
12
13 import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
14 import org.apache.commons.math.analysis.interpolation.LinearInterpolator;
15
16 import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
17
18 import org.apache.commons.math.FunctionEvaluationException;
19
20 import org.apache.log4j.Logger;
21
22 public class ATWriter
23 {
24 private static Logger logger = Logger.getLogger(ATWriter.class);
25
26 public static final int COLUMNS = 10;
27
28 public static final String EMPTY = " ";
29
30 protected double minW;
31 protected double maxW;
32 protected double minQ;
33 protected double maxQ;
34
35 protected UnivariateRealFunction qFunc;
36
37 public ATWriter() {
38 }
39
40 public ATWriter(WQ wq) throws IllegalArgumentException {
41
42 int [] bounds = wq.longestIncreasingWRangeIndices();
43
44 if (logger.isDebugEnabled()) {
45 logger.debug("exporting w between indices " +
46 bounds[0] + " and " + bounds[1] + " (" +
47 wq.getW(bounds[0]) + ", " + wq.getW(bounds[1]));
48 }
49
50 if (bounds[1]-bounds[0] < 1) { // Only first w can be written out.
51 minW = maxW = wq.getW(bounds[0]);
52 minQ = maxQ = wq.getQ(bounds[0]);
53 // constant function
54 qFunc = new PolynomialFunction(new double [] { minQ });
55 return;
56 }
57
58 double [] ws = new double[bounds[1]-bounds[0]];
59 double [] qs = new double[ws.length];
60
61 for (int i = 0; i < ws.length; ++i) {
62 int idx = bounds[0]+i;
63 ws[i] = wq.getW(idx);
64 qs[i] = wq.getQ(idx);
65 }
66
67 qFunc = ws.length < 3
68 ? new LinearInterpolator().interpolate(ws, qs)
69 : new SplineInterpolator().interpolate(ws, qs);
70
71 minW = wq.getW(bounds[0]);
72 maxW = wq.getW(bounds[1]);
73 minQ = wq.getQ(bounds[0]);
74 maxQ = wq.getQ(bounds[1]);
75 }
76
77 public double getQ(double w) {
78
79 try {
80 return qFunc.value(w);
81 }
82 catch (FunctionEvaluationException aode) {
83 // should not happen
84 logger.warn("spline interpolation failed", aode);
85 return w <= minW ? minQ : maxQ;
86 }
87 }
88
89 protected static void printQ(PrintWriter out, double q) {
90 String format;
91 if (q < 1d) format = " % 8.3f";
92 else if (q < 10d) format = " % 8.2f";
93 else if (q < 100d) format = " % 8.1f";
94 else {
95 format = " % 8.0f";
96 if (q > 1000d) q = Math.rint(q/10d)*10d;
97 }
98 out.printf(Locale.US, format, q);
99 }
100
101 public void write(Writer writer) throws IOException {
102
103 PrintWriter out = new PrintWriter(writer);
104
105 double rest = Math.abs(minW % COLUMNS);
106
107 double startW = Math.round(minW*10.0)/10.0;
108 if (rest >= 1d) {
109 startW -= 0.1;
110 out.printf(Locale.US, "%8d", (int)Math.round(startW*100.0));
111 int columns = (int)Math.floor(rest);
112 for (int i = columns; i < COLUMNS; ++i) {
113 out.print(EMPTY);
114 }
115 for (int i = COLUMNS-columns; i < COLUMNS; ++i) {
116 printQ(out, getQ(startW + i*0.01));
117 }
118 out.println();
119 startW += 0.1;
120 }
121
122 int col = 0;
123 for (double w = startW; w <= maxW; w += 0.01) {
124 if (col == 0) {
125 out.printf(Locale.US, "%8d", (int)Math.round(w*100.0));
126 }
127
128 printQ(out, getQ(w));
129
130 if (++col >= COLUMNS) {
131 out.println();
132 col = 0;
133 }
134 }
135
136 if (col > 0) {
137 out.println();
138 }
139
140 out.flush();
141 }
142 }
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org