comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java @ 751:8d5bd3a08dd1

merged flys-artifacts/2.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:14 +0200
parents 65dcb5891206
children 821aaceb2776
comparison
equal deleted inserted replaced
618:6fd6c223b81e 751:8d5bd3a08dd1
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 maxIndex = maxIncreasingWIndex(wq);
43
44 if (maxIndex < 1) { // Only first w can be written out.
45 minW = maxW = wq.getW(0);
46 minQ = maxQ = wq.getQ(0);
47 // constant function
48 qFunc = new PolynomialFunction(new double [] { minQ });
49 return;
50 }
51
52 double [] ws = new double[maxIndex];
53 double [] qs = new double[ws.length];
54
55 for (int i = 0; i < ws.length; ++i) {
56 ws[i] = wq.getW(i);
57 qs[i] = wq.getQ(i);
58 }
59
60 qFunc = ws.length < 3
61 ? new LinearInterpolator().interpolate(ws, qs)
62 : new SplineInterpolator().interpolate(ws, qs);
63
64 minW = wq.getW(0);
65 maxW = wq.getW(maxIndex);
66 minQ = wq.getQ(0);
67 maxQ = wq.getQ(maxIndex);
68 }
69
70 public static int maxIncreasingWIndex(WQ wq) {
71
72 int N = wq.size();
73
74 if (N < 2) {
75 return N-1;
76 }
77
78 double last = wq.getW(0);
79
80 for (int i = 1; i < N; ++i) {
81 double current = wq.getW(i);
82 if (current <= last) {
83 return i-1;
84 }
85 }
86
87 return N-1;
88 }
89
90 public double getQ(double w) {
91
92 try {
93 return qFunc.value(w);
94 }
95 catch (FunctionEvaluationException aode) {
96 // should not happen
97 logger.warn("spline interpolation failed", aode);
98 return w <= minW ? minQ : maxQ;
99 }
100 }
101
102 protected static void printQ(PrintWriter out, double q) {
103 String format;
104 if (q < 1d) format = " % 8.3f";
105 else if (q < 10d) format = " % 8.2f";
106 else if (q < 100d) format = " % 8.1f";
107 else {
108 format = " % 8.0f";
109 if (q > 1000d) q = Math.rint(q/10d)*10d;
110 }
111 out.printf(Locale.US, format, q);
112 }
113
114 public void write(Writer writer) throws IOException {
115
116 PrintWriter out = new PrintWriter(writer);
117
118 double rest = Math.abs(minW % COLUMNS);
119
120 double startW = Math.round(minW*10.0)/10.0;
121 if (rest >= 1d) {
122 startW -= 0.1;
123 out.printf(Locale.US, "%8d", (int)Math.round(startW*100.0));
124 int columns = (int)Math.floor(rest);
125 for (int i = columns; i < COLUMNS; ++i) {
126 out.print(EMPTY);
127 }
128 for (int i = COLUMNS-columns; i < COLUMNS; ++i) {
129 printQ(out, getQ(startW + i*0.01));
130 }
131 out.println();
132 startW += 0.1;
133 }
134
135 int col = 0;
136 for (double w = startW; w <= maxW; w += 0.01) {
137 if (col == 0) {
138 out.printf(Locale.US, "%8d", (int)Math.round(w*100.0));
139 }
140
141 printQ(out, getQ(w));
142
143 if (++col >= COLUMNS) {
144 out.println();
145 col = 0;
146 }
147 }
148
149 if (col > 0) {
150 out.println();
151 }
152
153 out.flush();
154 }
155 }
156 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org