comparison flys-artifacts/src/main/java/org/dive4elements/river/exports/WstWriter.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/exports/WstWriter.java@d9af29a4bb85
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.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.ArrayList;
9 import java.util.Collection;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Locale;
13 import java.util.Map;
14 import java.util.TreeMap;
15
16 import org.apache.log4j.Logger;
17
18 import org.dive4elements.river.artifacts.model.WstLine;
19
20
21 /**
22 * A writer that creates WSTs.
23 *
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
25 */
26 public class WstWriter {
27
28 /** The logger used in this class. */
29 private static Logger logger = Logger.getLogger(WstWriter.class);
30
31 /** The default unit that is written into the header of the WST. */
32 public static final String DEFAULT_UNIT = "Wassserstand [NN + m]";
33
34 /** The lines that need to be included for the export. */
35 protected Map<Double, WstLine> lines;
36
37 /** The column names. */
38 protected List<String> columnNames;
39
40 /** The locale used to format the values. */
41 protected Locale locale;
42
43 /** The number of discharge columns. */
44 protected int cols;
45
46 /** The last Q values. */
47 protected double[] qs;
48
49
50
51 /**
52 * This constructor creates a new WstWriter with a number of Q columns.
53 *
54 * @param cols The number of columns of the resulting WST.
55 */
56 public WstWriter(int cols) {
57 this.columnNames = new ArrayList<String>(cols);
58 this.lines = new HashMap<Double, WstLine>();
59 this.qs = new double[cols];
60 this.locale = Locale.US;
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 * @param out Where to write to.
68 */
69 public void write(OutputStream out) {
70 logger.info("WstWriter.write");
71
72 PrintWriter writer = new PrintWriter(
73 new BufferedWriter(
74 new OutputStreamWriter(out)));
75
76 this.qs = new double[cols];
77
78 writeHeader(writer);
79
80 Collection<WstLine> collection = new TreeMap(lines).values();
81
82 for (WstLine line: collection) {
83 writeWLine(writer, line);
84 }
85
86 writer.flush();
87 writer.close();
88 }
89
90
91 /**
92 * This method is used to add a new line to the WST.
93 *
94 * @param wqkms A 3dim double array with [W,Q, KM].
95 */
96 public void add(double[] wqkms) {
97 Double km = wqkms[2];
98
99 WstLine line = lines.get(km);
100
101 if (line == null) {
102 line = new WstLine(km.doubleValue());
103 lines.put(km, line);
104 }
105
106 line.add(wqkms[0], wqkms[1]);
107 }
108
109
110 public void addCorrected(double[] wqckms) {
111 Double km = wqckms[2];
112
113 WstLine line = lines.get(km);
114
115 if (line == null) {
116 line = new WstLine(km.doubleValue());
117 lines.put(km, line);
118 }
119
120 line.add(wqckms[3], wqckms[1]);
121 }
122
123
124 /**
125 * Adds a further column name.
126 *
127 * @param name The name of the new column.
128 */
129 public void addColumn(String name) {
130 if (name != null) {
131 cols++;
132
133 String basename = name;
134
135 int i = 0;
136 while (columnNames.contains(name)) {
137 name = basename + "_" + i++;
138
139 if (name.length() > 9) {
140 name = name.substring(name.length() - 9);
141 }
142 }
143
144 columnNames.add(name);
145 }
146 }
147
148
149 /**
150 * This method writes the header of the WST.
151 *
152 * @param writer The PrintWriter that creates the output.
153 */
154 protected void writeHeader(PrintWriter writer) {
155 logger.debug("WstWriter.writeHeader");
156
157 writer.println(cols);
158 writer.print(" ");
159
160 for (String columnName: columnNames) {
161 writer.printf(locale, "%9s", columnName);
162 }
163
164 writer.println();
165
166 writer.write("* KM ");
167 writer.write(DEFAULT_UNIT);
168 writer.println();
169 }
170
171
172 /**
173 * This method writes a line with W values and a certain kilometer.
174 *
175 * @param writer The PrintWriter that is used to create the output.
176 * @param line The WstLine that should be written to the output.
177 */
178 protected void writeWLine(PrintWriter writer, WstLine line) {
179 double km = line.getKm();
180 double[] qs = line.getQs();
181 int num = line.getSize();
182
183 if (dischargesChanged(qs)) {
184 writeQLine(writer, qs);
185 }
186
187 writer.printf(locale, "%8.3f", km);
188
189 for (int i = 0; i < num; i++) {
190 writer.printf(locale, "%9.2f", line.getW(i));
191 }
192
193 writer.println();
194 }
195
196
197 /**
198 * Writes a discharge line (Q values) into a WST.
199 *
200 * @param qs the Q values for the next range.
201 */
202 protected void writeQLine(PrintWriter writer, double[] qs) {
203 writer.write("*\u001f ");
204
205 for (int i = 0; i < qs.length; i++) {
206 this.qs[i] = qs[i];
207
208 writer.printf(locale, "%9.2f", qs[i]);
209 }
210
211 writer.println();
212 }
213
214
215 /**
216 * This method determines if a Q has changed from the last line to the
217 * current one.
218 *
219 * @param newQs The Q values of the next line.
220 *
221 * @return true, if a Q value have changed, otherwise false.
222 */
223 protected boolean dischargesChanged(double[] newQs) {
224 // XXX maybe there is a way to do this faster
225 for (int i = 0; i < cols; i++) {
226 if (Math.abs(newQs[i] - qs[i]) >= 0.001) {
227 return true;
228 }
229 }
230
231 return false;
232 }
233 }
234 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org