Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java @ 732:39d191f011dc
ATWriter: Results are now in cm. Made it more robust against corner cases.
flys-artifacts/trunk@2224 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 23 Jun 2011 21:44:24 +0000 |
parents | 078934923acb |
children | 65dcb5891206 |
comparison
equal
deleted
inserted
replaced
731:5ddb1b568f38 | 732:39d191f011dc |
---|---|
6 | 6 |
7 import java.util.Locale; | 7 import java.util.Locale; |
8 | 8 |
9 import de.intevation.flys.artifacts.model.WQ; | 9 import de.intevation.flys.artifacts.model.WQ; |
10 | 10 |
11 import org.apache.commons.math.analysis.UnivariateRealFunction; | |
12 | |
11 import org.apache.commons.math.analysis.interpolation.SplineInterpolator; | 13 import org.apache.commons.math.analysis.interpolation.SplineInterpolator; |
14 import org.apache.commons.math.analysis.interpolation.LinearInterpolator; | |
12 | 15 |
13 import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; | 16 import org.apache.commons.math.analysis.polynomials.PolynomialFunction; |
14 | 17 |
15 import org.apache.commons.math.ArgumentOutsideDomainException; | 18 import org.apache.commons.math.FunctionEvaluationException; |
16 | 19 |
17 import org.apache.log4j.Logger; | 20 import org.apache.log4j.Logger; |
18 | 21 |
19 public class ATWriter | 22 public class ATWriter |
20 { | 23 { |
21 private static Logger logger = Logger.getLogger(ATWriter.class); | 24 private static Logger logger = Logger.getLogger(ATWriter.class); |
22 | 25 |
23 public static final int COLUMNS = 10; | 26 public static final int COLUMNS = 10; |
24 | 27 |
25 public static final String EMPTY = " "; | 28 public static final String EMPTY = " "; |
26 | 29 |
27 protected double minW; | 30 protected double minW; |
28 protected double maxW; | 31 protected double maxW; |
29 protected double minQ; | 32 protected double minQ; |
30 protected double maxQ; | 33 protected double maxQ; |
31 | 34 |
32 protected PolynomialSplineFunction qFunc; | 35 protected UnivariateRealFunction qFunc; |
33 | 36 |
34 public ATWriter() { | 37 public ATWriter() { |
35 } | 38 } |
36 | 39 |
37 public ATWriter(WQ wq) throws IllegalArgumentException { | 40 public ATWriter(WQ wq) throws IllegalArgumentException { |
39 int maxIndex = maxIncreasingWIndex(wq); | 42 int maxIndex = maxIncreasingWIndex(wq); |
40 | 43 |
41 if (maxIndex < 1) { // Only first w can be written out. | 44 if (maxIndex < 1) { // Only first w can be written out. |
42 minW = maxW = wq.getW(0); | 45 minW = maxW = wq.getW(0); |
43 minQ = maxQ = wq.getQ(0); | 46 minQ = maxQ = wq.getQ(0); |
47 // constant function | |
48 qFunc = new PolynomialFunction(new double [] { minQ }); | |
44 return; | 49 return; |
45 } | 50 } |
46 | 51 |
47 double [] ws = new double[maxIndex+1]; | 52 double [] ws = new double[maxIndex+1]; |
48 double [] qs = new double[ws.length]; | 53 double [] qs = new double[ws.length]; |
50 for (int i = 0; i < ws.length; ++i) { | 55 for (int i = 0; i < ws.length; ++i) { |
51 ws[i] = wq.getW(i); | 56 ws[i] = wq.getW(i); |
52 qs[i] = wq.getQ(i); | 57 qs[i] = wq.getQ(i); |
53 } | 58 } |
54 | 59 |
55 SplineInterpolator interpolator = new SplineInterpolator(); | 60 qFunc = ws.length < 3 |
56 | 61 ? new LinearInterpolator().interpolate(ws, qs) |
57 qFunc = interpolator.interpolate(ws, qs); | 62 : new SplineInterpolator().interpolate(ws, qs); |
58 | 63 |
59 minW = wq.getW(0); | 64 minW = wq.getW(0); |
60 maxW = wq.getW(maxIndex); | 65 maxW = wq.getW(maxIndex); |
61 minQ = wq.getQ(0); | 66 minQ = wq.getQ(0); |
62 maxQ = wq.getQ(maxIndex); | 67 maxQ = wq.getQ(maxIndex); |
85 public double getQ(double w) { | 90 public double getQ(double w) { |
86 | 91 |
87 try { | 92 try { |
88 return qFunc.value(w); | 93 return qFunc.value(w); |
89 } | 94 } |
90 catch (ArgumentOutsideDomainException aode) { | 95 catch (FunctionEvaluationException aode) { |
91 // should not happen | 96 // should not happen |
92 logger.warn("spline interpolation failed", aode); | 97 logger.warn("spline interpolation failed", aode); |
93 return w <= minW ? minQ : maxQ; | 98 return w <= minW ? minQ : maxQ; |
94 } | 99 } |
95 } | 100 } |
96 | 101 |
97 protected static void printQ(PrintWriter out, double q) { | 102 protected static void printQ(PrintWriter out, double q) { |
98 String format; | 103 String format; |
99 if (q < 1) format = " % 8.3f"; | 104 if (q < 1d) format = " % 8.3f"; |
100 else if (q < 10) format = " % 8.2f"; | 105 else if (q < 10d) format = " % 8.2f"; |
101 else if (q < 100) format = " % 8.1f"; | 106 else if (q < 100d) format = " % 8.1f"; |
102 else { | 107 else { |
103 format = " % 8.0f"; | 108 format = " % 8.0f"; |
104 if (q > 1000) q = Math.rint(q/10)*10; | 109 if (q > 1000d) q = Math.rint(q/10d)*10d; |
105 } | 110 } |
106 out.printf(Locale.US, format, q); | 111 out.printf(Locale.US, format, q); |
107 } | 112 } |
108 | 113 |
109 public void write(Writer writer) throws IOException { | 114 public void write(Writer writer) throws IOException { |
110 | 115 |
111 PrintWriter out = new PrintWriter(writer); | 116 PrintWriter out = new PrintWriter(writer); |
112 | 117 |
113 double range = COLUMNS; | 118 double rest = Math.abs(minW % COLUMNS); |
114 | 119 |
115 double rest = Math.abs(minW % range); | 120 double startW = Math.round(minW*10.0)/10.0; |
116 | |
117 int startW = (int)(minW - rest); | |
118 if (rest >= 1d) { | 121 if (rest >= 1d) { |
119 out.printf(Locale.US, "%8d", startW); | 122 startW -= 0.1; |
123 out.printf(Locale.US, "%8d", (int)Math.round(startW*100.0)); | |
120 int columns = (int)Math.floor(rest); | 124 int columns = (int)Math.floor(rest); |
121 for (int i = columns; i < COLUMNS; ++i) { | 125 for (int i = columns; i < COLUMNS; ++i) { |
122 out.print(EMPTY); | 126 out.print(EMPTY); |
123 } | 127 } |
124 for (int i = 0, N = COLUMNS-columns; i < N; ++i) { | 128 for (int i = COLUMNS-columns; i < COLUMNS; ++i) { |
125 printQ(out, getQ(startW + (COLUMNS-i-1))); | 129 printQ(out, getQ(startW + i*0.01)); |
126 } | 130 } |
127 startW += COLUMNS; | 131 out.println(); |
132 startW += 0.1; | |
128 } | 133 } |
129 | 134 |
130 int col = 0; | 135 int col = 0; |
131 for (int w = startW; w <= maxW; ++w) { | 136 for (double w = startW; w <= maxW; w += 0.01) { |
132 if (col == 0) out.printf(Locale.US, "%8d", w); | 137 if (col == 0) { |
138 out.printf(Locale.US, "%8d", (int)Math.round(w*100.0)); | |
139 } | |
133 | 140 |
134 printQ(out, getQ(w)); | 141 printQ(out, getQ(w)); |
135 | 142 |
136 if (++col >= COLUMNS) { | 143 if (++col >= COLUMNS) { |
137 out.println(); | 144 out.println(); |