Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java @ 729:078934923acb
New AT writer for results.
flys-artifacts/trunk@2219 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 23 Jun 2011 15:50:44 +0000 |
parents | |
children | 39d191f011dc |
comparison
equal
deleted
inserted
replaced
728:56bcf56b1fe0 | 729:078934923acb |
---|---|
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.interpolation.SplineInterpolator; | |
12 | |
13 import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; | |
14 | |
15 import org.apache.commons.math.ArgumentOutsideDomainException; | |
16 | |
17 import org.apache.log4j.Logger; | |
18 | |
19 public class ATWriter | |
20 { | |
21 private static Logger logger = Logger.getLogger(ATWriter.class); | |
22 | |
23 public static final int COLUMNS = 10; | |
24 | |
25 public static final String EMPTY = " "; | |
26 | |
27 protected double minW; | |
28 protected double maxW; | |
29 protected double minQ; | |
30 protected double maxQ; | |
31 | |
32 protected PolynomialSplineFunction qFunc; | |
33 | |
34 public ATWriter() { | |
35 } | |
36 | |
37 public ATWriter(WQ wq) throws IllegalArgumentException { | |
38 | |
39 int maxIndex = maxIncreasingWIndex(wq); | |
40 | |
41 if (maxIndex < 1) { // Only first w can be written out. | |
42 minW = maxW = wq.getW(0); | |
43 minQ = maxQ = wq.getQ(0); | |
44 return; | |
45 } | |
46 | |
47 double [] ws = new double[maxIndex+1]; | |
48 double [] qs = new double[ws.length]; | |
49 | |
50 for (int i = 0; i < ws.length; ++i) { | |
51 ws[i] = wq.getW(i); | |
52 qs[i] = wq.getQ(i); | |
53 } | |
54 | |
55 SplineInterpolator interpolator = new SplineInterpolator(); | |
56 | |
57 qFunc = interpolator.interpolate(ws, qs); | |
58 | |
59 minW = wq.getW(0); | |
60 maxW = wq.getW(maxIndex); | |
61 minQ = wq.getQ(0); | |
62 maxQ = wq.getQ(maxIndex); | |
63 } | |
64 | |
65 public static int maxIncreasingWIndex(WQ wq) { | |
66 | |
67 int N = wq.size(); | |
68 | |
69 if (N < 2) { | |
70 return N-1; | |
71 } | |
72 | |
73 double last = wq.getW(0); | |
74 | |
75 for (int i = 1; i < N; ++i) { | |
76 double current = wq.getW(i); | |
77 if (current <= last) { | |
78 return i-1; | |
79 } | |
80 } | |
81 | |
82 return N-1; | |
83 } | |
84 | |
85 public double getQ(double w) { | |
86 | |
87 try { | |
88 return qFunc.value(w); | |
89 } | |
90 catch (ArgumentOutsideDomainException aode) { | |
91 // should not happen | |
92 logger.warn("spline interpolation failed", aode); | |
93 return w <= minW ? minQ : maxQ; | |
94 } | |
95 } | |
96 | |
97 protected static void printQ(PrintWriter out, double q) { | |
98 String format; | |
99 if (q < 1) format = " % 8.3f"; | |
100 else if (q < 10) format = " % 8.2f"; | |
101 else if (q < 100) format = " % 8.1f"; | |
102 else { | |
103 format = " % 8.0f"; | |
104 if (q > 1000) q = Math.rint(q/10)*10; | |
105 } | |
106 out.printf(Locale.US, format, q); | |
107 } | |
108 | |
109 public void write(Writer writer) throws IOException { | |
110 | |
111 PrintWriter out = new PrintWriter(writer); | |
112 | |
113 double range = COLUMNS; | |
114 | |
115 double rest = Math.abs(minW % range); | |
116 | |
117 int startW = (int)(minW - rest); | |
118 if (rest >= 1d) { | |
119 out.printf(Locale.US, "%8d", startW); | |
120 int columns = (int)Math.floor(rest); | |
121 for (int i = columns; i < COLUMNS; ++i) { | |
122 out.print(EMPTY); | |
123 } | |
124 for (int i = 0, N = COLUMNS-columns; i < N; ++i) { | |
125 printQ(out, getQ(startW + (COLUMNS-i-1))); | |
126 } | |
127 startW += COLUMNS; | |
128 } | |
129 | |
130 int col = 0; | |
131 for (int w = startW; w <= maxW; ++w) { | |
132 if (col == 0) out.printf(Locale.US, "%8d", w); | |
133 | |
134 printQ(out, getQ(w)); | |
135 | |
136 if (++col >= COLUMNS) { | |
137 out.println(); | |
138 col = 0; | |
139 } | |
140 } | |
141 | |
142 if (col > 0) { | |
143 out.println(); | |
144 } | |
145 | |
146 out.flush(); | |
147 } | |
148 } | |
149 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |