Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/WstWriter.java @ 446:c0bec245f608
Implemented writer that creates WSTs and enabled the WaterlevelExporter to create those.
flys-artifacts/trunk@1938 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 17 May 2011 16:31:07 +0000 |
parents | |
children | 5606ba4139e0 |
comparison
equal
deleted
inserted
replaced
445:a7947972fdeb | 446:c0bec245f608 |
---|---|
1 package de.intevation.flys.exports; | |
2 | |
3 import java.io.BufferedWriter; | |
4 import java.io.OutputStream; | |
5 import java.io.OutputStreamWriter; | |
6 import java.io.PrintWriter; | |
7 | |
8 import java.util.Collection; | |
9 import java.util.HashMap; | |
10 import java.util.Locale; | |
11 import java.util.Map; | |
12 import java.util.TreeMap; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 | |
16 import de.intevation.flys.artifacts.model.WstLine; | |
17 | |
18 | |
19 /** | |
20 * A writer that creates WSTs. | |
21 * | |
22 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
23 */ | |
24 public class WstWriter { | |
25 | |
26 /** The logger used in this class.*/ | |
27 private static Logger logger = Logger.getLogger(WstWriter.class); | |
28 | |
29 | |
30 /** The default unit that is written into the header of the WST.*/ | |
31 public static final String DEFAULT_UNIT = "Wassserstand [NN + m]"; | |
32 | |
33 | |
34 /** The lines that need to be included for the export.*/ | |
35 protected Map<Double, WstLine> lines; | |
36 | |
37 /** The locale used to format the values.*/ | |
38 protected Locale locale; | |
39 | |
40 /** The number of discharge columns.*/ | |
41 protected int cols; | |
42 | |
43 /** The last Q values.*/ | |
44 protected double[] qs; | |
45 | |
46 | |
47 | |
48 /** | |
49 * This constructor creates a new WstWriter with an OutputStream and a | |
50 * number of Q columns. | |
51 * | |
52 * @param out The output stream where the WST is written to. | |
53 * @param cols The number of columns of the resulting WST. | |
54 */ | |
55 public WstWriter(int cols) { | |
56 this.cols = cols; | |
57 this.qs = new double[cols]; | |
58 this.locale = Locale.US; | |
59 this.lines = new HashMap<Double, WstLine>(); | |
60 | |
61 } | |
62 | |
63 | |
64 /** | |
65 * This method is used to create the WST from the data that has been | |
66 * inserted using add(double[]) before. | |
67 */ | |
68 public void write(OutputStream out) { | |
69 logger.info("WstWriter.write"); | |
70 | |
71 PrintWriter writer = new PrintWriter( | |
72 new BufferedWriter( | |
73 new OutputStreamWriter(out))); | |
74 | |
75 writeHeader(writer); | |
76 | |
77 Collection<WstLine> collection = new TreeMap(lines).values(); | |
78 | |
79 for (WstLine line: collection) { | |
80 writeWLine(writer, line); | |
81 } | |
82 | |
83 writer.flush(); | |
84 writer.close(); | |
85 } | |
86 | |
87 | |
88 /** | |
89 * This method is used to add a new line to the WST. | |
90 * | |
91 * @param wqkms A 3dim double array with [W,Q, KM]. | |
92 */ | |
93 public void add(double[] wqkms) { | |
94 Double km = wqkms[2]; | |
95 | |
96 WstLine line = lines.get(km); | |
97 | |
98 if (line == null) { | |
99 line = new WstLine(km.doubleValue()); | |
100 lines.put(km, line); | |
101 } | |
102 | |
103 line.add(wqkms[0], wqkms[1]); | |
104 } | |
105 | |
106 | |
107 /** | |
108 * This method writes the header of the WST. | |
109 * | |
110 * @param writer The PrintWriter that creates the output. | |
111 */ | |
112 protected void writeHeader(PrintWriter writer) { | |
113 logger.debug("WstWriter.writeHeader"); | |
114 | |
115 writer.println(cols); | |
116 | |
117 // TODO WRITE DISCHARGE COLUMNS | |
118 | |
119 writer.write("* KM "); | |
120 writer.write(DEFAULT_UNIT); | |
121 writer.println(); | |
122 } | |
123 | |
124 | |
125 /** | |
126 * This method writes a line with W values and a certain kilometer. | |
127 * | |
128 * @param writer The PrintWriter that is used to create the output. | |
129 * @param line The WstLine that should be written to the output. | |
130 */ | |
131 protected void writeWLine(PrintWriter writer, WstLine line) { | |
132 double km = line.getKm(); | |
133 double[] qs = line.getQs(); | |
134 int num = line.getSize(); | |
135 | |
136 if (dischargesChanged(qs)) { | |
137 writeQLine(writer, qs); | |
138 } | |
139 | |
140 writer.printf(locale, "%8.3f", km); | |
141 | |
142 for (int i = 0; i < num; i++) { | |
143 writer.printf(locale, "%9.2f", line.getW(i)); | |
144 } | |
145 | |
146 writer.println(); | |
147 } | |
148 | |
149 | |
150 /** | |
151 * Writes a discharge line (Q values) into a WST. | |
152 * | |
153 * @param qs the Q values for the next range. | |
154 */ | |
155 protected void writeQLine(PrintWriter writer, double[] qs) { | |
156 writer.write("*\u001f "); | |
157 | |
158 for (int i = 0; i < cols; i++) { | |
159 this.qs[i] = qs[i]; | |
160 | |
161 writer.printf(locale, "%9.2f", qs[i]); | |
162 } | |
163 | |
164 writer.println(); | |
165 } | |
166 | |
167 | |
168 /** | |
169 * This method determines if a Q has changed from the last line to the | |
170 * current one. | |
171 * | |
172 * @param newQs The Q values of the next line. | |
173 * | |
174 * @return true, if a Q value have changed, otherwise false. | |
175 */ | |
176 protected boolean dischargesChanged(double[] newQs) { | |
177 // XXX maybe there is a way to do this faster | |
178 for (int i = 0; i < cols; i++) { | |
179 if (Math.abs(newQs[i] - qs[i]) >= 0.001) { | |
180 return true; | |
181 } | |
182 } | |
183 | |
184 return false; | |
185 } | |
186 } | |
187 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |