comparison flys-artifacts/src/main/java/de/intevation/flys/exports/WstWriter.java @ 462:ebf049a1eb53

merged flys-artifacts/2.3.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:11 +0200
parents 5606ba4139e0
children 72bcbc308501
comparison
equal deleted inserted replaced
441:c4c8137e8f0e 462:ebf049a1eb53
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.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 de.intevation.flys.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
32 /** The default unit that is written into the header of the WST.*/
33 public static final String DEFAULT_UNIT = "Wassserstand [NN + m]";
34
35
36 /** The lines that need to be included for the export.*/
37 protected Map<Double, WstLine> lines;
38
39 /** The column names.*/
40 protected List<String> columnNames;
41
42 /** The locale used to format the values.*/
43 protected Locale locale;
44
45 /** The number of discharge columns.*/
46 protected int cols;
47
48 /** The last Q values.*/
49 protected double[] qs;
50
51
52
53 /**
54 * This constructor creates a new WstWriter with an OutputStream and a
55 * number of Q columns.
56 *
57 * @param out The output stream where the WST is written to.
58 * @param cols The number of columns of the resulting WST.
59 */
60 public WstWriter(int cols) {
61 this.columnNames = new ArrayList<String>(cols);
62 this.lines = new HashMap<Double, WstLine>();
63 this.qs = new double[cols];
64 this.locale = Locale.US;
65 this.cols = cols;
66
67 }
68
69
70 /**
71 * This method is used to create the WST from the data that has been
72 * inserted using add(double[]) before.
73 */
74 public void write(OutputStream out) {
75 logger.info("WstWriter.write");
76
77 PrintWriter writer = new PrintWriter(
78 new BufferedWriter(
79 new OutputStreamWriter(out)));
80
81 writeHeader(writer);
82
83 Collection<WstLine> collection = new TreeMap(lines).values();
84
85 for (WstLine line: collection) {
86 writeWLine(writer, line);
87 }
88
89 writer.flush();
90 writer.close();
91 }
92
93
94 /**
95 * This method is used to add a new line to the WST.
96 *
97 * @param wqkms A 3dim double array with [W,Q, KM].
98 */
99 public void add(double[] wqkms) {
100 Double km = wqkms[2];
101
102 WstLine line = lines.get(km);
103
104 if (line == null) {
105 line = new WstLine(km.doubleValue());
106 lines.put(km, line);
107 }
108
109 line.add(wqkms[0], wqkms[1]);
110 }
111
112
113 /**
114 * Adds a further column name.
115 *
116 * @param name The name of the new column.
117 */
118 public void addColumn(String name) {
119 if (name != null) {
120 columnNames.add(name);
121 }
122 }
123
124
125 /**
126 * This method writes the header of the WST.
127 *
128 * @param writer The PrintWriter that creates the output.
129 */
130 protected void writeHeader(PrintWriter writer) {
131 logger.debug("WstWriter.writeHeader");
132
133 writer.println(cols);
134 writer.print(" ");
135
136 for (String columnName: columnNames) {
137 writer.printf(locale, "%9s", columnName);
138 }
139
140 writer.println();
141
142 writer.write("* KM ");
143 writer.write(DEFAULT_UNIT);
144 writer.println();
145 }
146
147
148 /**
149 * This method writes a line with W values and a certain kilometer.
150 *
151 * @param writer The PrintWriter that is used to create the output.
152 * @param line The WstLine that should be written to the output.
153 */
154 protected void writeWLine(PrintWriter writer, WstLine line) {
155 double km = line.getKm();
156 double[] qs = line.getQs();
157 int num = line.getSize();
158
159 if (dischargesChanged(qs)) {
160 writeQLine(writer, qs);
161 }
162
163 writer.printf(locale, "%8.3f", km);
164
165 for (int i = 0; i < num; i++) {
166 writer.printf(locale, "%9.2f", line.getW(i));
167 }
168
169 writer.println();
170 }
171
172
173 /**
174 * Writes a discharge line (Q values) into a WST.
175 *
176 * @param qs the Q values for the next range.
177 */
178 protected void writeQLine(PrintWriter writer, double[] qs) {
179 writer.write("*\u001f ");
180
181 for (int i = 0; i < cols; i++) {
182 this.qs[i] = qs[i];
183
184 writer.printf(locale, "%9.2f", qs[i]);
185 }
186
187 writer.println();
188 }
189
190
191 /**
192 * This method determines if a Q has changed from the last line to the
193 * current one.
194 *
195 * @param newQs The Q values of the next line.
196 *
197 * @return true, if a Q value have changed, otherwise false.
198 */
199 protected boolean dischargesChanged(double[] newQs) {
200 // XXX maybe there is a way to do this faster
201 for (int i = 0; i < cols; i++) {
202 if (Math.abs(newQs[i] - qs[i]) >= 0.001) {
203 return true;
204 }
205 }
206
207 return false;
208 }
209 }
210 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org