Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveExporter.java @ 9312:740d65e4aa14
Q [m³/s] one message
author | gernotbelger |
---|---|
date | Thu, 26 Jul 2018 15:54:20 +0200 |
parents | 5e38e2924c07 |
children |
comparison
equal
deleted
inserted
replaced
9311:7c7f73e5e01e | 9312:740d65e4aa14 |
---|---|
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.exports; | 9 package org.dive4elements.river.exports; |
10 | 10 |
11 import java.io.OutputStream; | 11 import java.io.OutputStream; |
12 import java.text.DateFormat; | |
12 import java.text.NumberFormat; | 13 import java.text.NumberFormat; |
13 import java.util.ArrayList; | 14 import java.util.ArrayList; |
14 import java.util.Arrays; | 15 import java.util.Arrays; |
16 import java.util.Date; | |
17 import java.util.HashMap; | |
15 import java.util.List; | 18 import java.util.List; |
19 import java.util.Locale; | |
16 import java.util.Map; | 20 import java.util.Map; |
17 import java.util.HashMap; | |
18 import java.util.Date; | |
19 import java.text.DateFormat; | |
20 import java.util.Locale; | |
21 | 21 |
22 import org.apache.log4j.Logger; | 22 import org.apache.log4j.Logger; |
23 import org.dive4elements.artifacts.CallMeta; | |
24 import org.dive4elements.artifacts.common.utils.Config; | |
25 import org.dive4elements.river.artifacts.D4EArtifact; | |
26 import org.dive4elements.river.artifacts.access.RangeAccess; | |
27 import org.dive4elements.river.artifacts.model.CalculationResult; | |
28 import org.dive4elements.river.artifacts.model.WKmsJRDataSource; | |
29 import org.dive4elements.river.artifacts.model.WQ; | |
30 import org.dive4elements.river.artifacts.model.WQKms; | |
31 import org.dive4elements.river.artifacts.resources.Resources; | |
32 import org.dive4elements.river.model.Gauge; | |
33 import org.dive4elements.river.model.River; | |
34 import org.dive4elements.river.utils.Formatter; | |
35 import org.dive4elements.river.utils.RiverUtils; | |
23 | 36 |
24 import au.com.bytecode.opencsv.CSVWriter; | 37 import au.com.bytecode.opencsv.CSVWriter; |
25 | 38 import net.sf.jasperreports.engine.JRException; |
26 import net.sf.jasperreports.engine.JasperExportManager; | 39 import net.sf.jasperreports.engine.JasperExportManager; |
27 import net.sf.jasperreports.engine.JasperFillManager; | 40 import net.sf.jasperreports.engine.JasperFillManager; |
28 import net.sf.jasperreports.engine.JasperPrint; | 41 import net.sf.jasperreports.engine.JasperPrint; |
29 import net.sf.jasperreports.engine.JRException; | |
30 | |
31 import org.dive4elements.artifacts.common.utils.Config; | |
32 | |
33 import org.dive4elements.artifacts.CallMeta; | |
34 | |
35 import org.dive4elements.river.artifacts.D4EArtifact; | |
36 | |
37 import org.dive4elements.river.artifacts.access.RangeAccess; | |
38 import org.dive4elements.river.artifacts.model.CalculationResult; | |
39 import org.dive4elements.river.artifacts.model.WQ; | |
40 import org.dive4elements.river.artifacts.model.WQKms; | |
41 import org.dive4elements.river.artifacts.model.WKmsJRDataSource; | |
42 import org.dive4elements.river.artifacts.resources.Resources; | |
43 | |
44 import org.dive4elements.river.model.Gauge; | |
45 import org.dive4elements.river.model.River; | |
46 | |
47 import org.dive4elements.river.utils.RiverUtils; | |
48 import org.dive4elements.river.utils.Formatter; | |
49 | 42 |
50 /** | 43 /** |
51 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 44 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
52 */ | 45 */ |
53 public class ComputedDischargeCurveExporter extends AbstractExporter { | 46 public class ComputedDischargeCurveExporter extends AbstractExporter { |
54 | 47 |
55 /** The log used in this exporter.*/ | 48 /** The log used in this exporter. */ |
56 private static Logger log = | 49 private static Logger log = Logger.getLogger(ComputedDischargeCurveExporter.class); |
57 Logger.getLogger(ComputedDischargeCurveExporter.class); | 50 |
58 | 51 public static final String CSV_W_HEADER = "export.computed.discharge.curve.csv.header.w"; |
59 public static final String CSV_W_HEADER = | 52 |
60 "export.computed.discharge.curve.csv.header.w"; | 53 public static final String CSV_Q_HEADER = "common.export.csv.header.q"; |
61 | 54 |
62 public static final String CSV_Q_HEADER = | 55 public static final String DEFAULT_CSV_W_HEADER = "W [NN + m]"; |
63 "export.computed.discharge.curve.csv.header.q"; | 56 public static final String DEFAULT_CSV_Q_HEADER = "Q [m\u00b3/s]"; |
64 | 57 |
65 public static final String DEFAULT_CSV_W_HEADER = "W [NN + m]"; | 58 public static final String PDF_HEADER_MODE = "export.computed.discharge.pdf.mode"; |
66 public static final String DEFAULT_CSV_Q_HEADER = "Q [m\u00b3/s]"; | 59 public static final String PDF_HEADER_CALC_MODE = "export.computed.discharge.pdf.calc.mode"; |
67 | 60 public static final String JASPER_FILE = "export.computed.discharge.pdf.file"; |
68 public static final String PDF_HEADER_MODE = | |
69 "export.computed.discharge.pdf.mode"; | |
70 public static final String PDF_HEADER_CALC_MODE = | |
71 "export.computed.discharge.pdf.calc.mode"; | |
72 public static final String JASPER_FILE = | |
73 "export.computed.discharge.pdf.file"; | |
74 | 61 |
75 protected List<WQKms> data; | 62 protected List<WQKms> data; |
76 | 63 |
77 protected String wUnit; | 64 protected String wUnit; |
78 protected String riverUnit; | 65 protected String riverUnit; |
79 protected String gaugeName; | 66 protected String gaugeName; |
80 protected double gaugeDatum; | 67 protected double gaugeDatum; |
81 protected Date validSince; | 68 protected Date validSince; |
82 | 69 |
83 public ComputedDischargeCurveExporter() { | 70 public ComputedDischargeCurveExporter() { |
84 data = new ArrayList<WQKms>(); | 71 this.data = new ArrayList<>(); |
85 } | 72 } |
86 | 73 |
87 @Override | 74 @Override |
88 protected void addData(Object d) { | 75 protected void addData(Object d) { |
89 if (d instanceof CalculationResult) { | 76 if (d instanceof CalculationResult) { |
90 d = ((CalculationResult)d).getData(); | 77 d = ((CalculationResult) d).getData(); |
91 } | 78 } |
92 WQKms referenceWQ = null; // used for gauge / unit observations | 79 WQKms referenceWQ = null; // used for gauge / unit observations |
93 if (d instanceof WQKms[]){ | 80 if (d instanceof WQKms[]) { |
94 data.addAll(Arrays.asList((WQKms [])d)); | 81 this.data.addAll(Arrays.asList((WQKms[]) d)); |
95 // If there is a unit mix in this list | 82 // If there is a unit mix in this list |
96 // we are screwed anyway. | 83 // we are screwed anyway. |
97 referenceWQ = ((WQKms[])d)[0]; | 84 referenceWQ = ((WQKms[]) d)[0]; |
98 } | 85 } else if (d instanceof WQKms) { |
99 else if (d instanceof WQKms) { | 86 this.data.add((WQKms) d); |
100 data.add((WQKms)d); | 87 referenceWQ = (WQKms) d; |
101 referenceWQ = (WQKms)d; | |
102 } else { | 88 } else { |
103 log.warn("Can't add data for export. Unkown data type " + | 89 log.warn("Can't add data for export. Unkown data type " + d.getClass().getName()); |
104 d.getClass().getName()); | |
105 return; | 90 return; |
106 } | 91 } |
107 if (referenceWQ != null) { | 92 if (referenceWQ != null) { |
108 D4EArtifact arti = (D4EArtifact)master; | 93 final D4EArtifact arti = (D4EArtifact) this.master; |
109 River river = RiverUtils.getRiver(arti); | 94 final River river = RiverUtils.getRiver(arti); |
110 riverUnit = river.getWstUnit().getName(); | 95 this.riverUnit = river.getWstUnit().getName(); |
111 RangeAccess rangeAccess = new RangeAccess(arti); | 96 final RangeAccess rangeAccess = new RangeAccess(arti); |
112 | 97 |
113 double[] kms = rangeAccess.getKmRange(); | 98 final double[] kms = rangeAccess.getKmRange(); |
114 | 99 |
115 Gauge gauge = river.determineGaugeAtStation(kms[0]); | 100 final Gauge gauge = river.determineGaugeAtStation(kms[0]); |
116 if (gauge != null) { | 101 if (gauge != null) { |
117 wUnit = "cm"; | 102 this.wUnit = "cm"; |
118 gaugeName = gauge.getName(); | 103 this.gaugeName = gauge.getName(); |
119 gaugeDatum = gauge.getDatum().doubleValue(); | 104 this.gaugeDatum = gauge.getDatum().doubleValue(); |
120 | 105 |
121 // Now convert the data to cm because we are at gauge | 106 // Now convert the data to cm because we are at gauge |
122 List<WQKms> newData = new ArrayList<WQKms>(); | 107 final List<WQKms> newData = new ArrayList<>(); |
123 for (WQKms d2: data) { | 108 for (final WQKms d2 : this.data) { |
124 newData.add(new WQKms(d2.getKms(), | 109 newData.add(new WQKms(d2.getKms(), WQ.getFixedWQforExportAtGauge(d2, gauge.getDatum()))); |
125 WQ.getFixedWQforExportAtGauge( | |
126 (WQ)d2, | |
127 gauge.getDatum() | |
128 ))); | |
129 } | 110 } |
130 data = newData; // All hail the garbage collector | 111 this.data = newData; // All hail the garbage collector |
131 | 112 |
132 validSince = gauge.fetchMasterDischargeTable() | 113 this.validSince = gauge.fetchMasterDischargeTable().getTimeInterval().getStartTime(); |
133 .getTimeInterval().getStartTime(); | |
134 } else { | 114 } else { |
135 gaugeName = ""; | 115 this.gaugeName = ""; |
136 validSince = null; | 116 this.validSince = null; |
137 gaugeDatum = Double.NaN; | 117 this.gaugeDatum = Double.NaN; |
138 } | 118 } |
139 } | 119 } |
140 } | 120 } |
141 | 121 |
142 protected void writeCSVData(CSVWriter writer) { | 122 @Override |
123 protected void writeCSVData(final CSVWriter writer) { | |
143 log.info("ComputedDischargeCurveExporter.writeData"); | 124 log.info("ComputedDischargeCurveExporter.writeData"); |
144 | 125 |
145 writeCSVHeader(writer); | 126 writeCSVHeader(writer); |
146 | 127 |
147 NumberFormat wf = getWFormatter(); | 128 final NumberFormat wf = getWFormatter(); |
148 NumberFormat qf = getQFormatter(); | 129 final NumberFormat qf = getQFormatter(); |
149 | 130 |
150 double[] res = new double[3]; | 131 double[] res = new double[3]; |
151 | 132 |
152 for (WQKms wqkms: data) { | 133 for (final WQKms wqkms : this.data) { |
153 int size = wqkms.size(); | 134 final int size = wqkms.size(); |
154 | 135 |
155 for (int i = 0; i < size; i++) { | 136 for (int i = 0; i < size; i++) { |
156 res = wqkms.get(i, res); | 137 res = wqkms.get(i, res); |
157 | 138 |
158 writer.writeNext(new String[] { | 139 writer.writeNext(new String[] { wf.format(res[0]), qf.format(res[1]) }); |
159 wf.format(res[0]), | |
160 qf.format(res[1]) | |
161 }); | |
162 } | 140 } |
163 } | 141 } |
164 } | 142 } |
165 | 143 |
166 | 144 protected void writeCSVHeader(final CSVWriter writer) { |
167 protected void writeCSVHeader(CSVWriter writer) { | |
168 log.debug("ComputedDischargeCurveExporter.writeCSVHeader"); | 145 log.debug("ComputedDischargeCurveExporter.writeCSVHeader"); |
169 | 146 |
170 RangeAccess access = new RangeAccess((D4EArtifact)master); | 147 final RangeAccess access = new RangeAccess((D4EArtifact) this.master); |
171 double[] km = access.getLocations(); | 148 final double[] km = access.getLocations(); |
172 // If we are not at gauge (cm) use the river unit | 149 // If we are not at gauge (cm) use the river unit |
173 String realUnit = "cm".equals(wUnit) ? "cm" : riverUnit; | 150 final String realUnit = "cm".equals(this.wUnit) ? "cm" : this.riverUnit; |
174 String header = | 151 final String header = msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { realUnit }); |
175 msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] {realUnit}); | 152 |
176 | 153 writer.writeNext(new String[] { header, msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER) }); |
177 writer.writeNext(new String[] { | 154 } |
178 header, | |
179 msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER) | |
180 }); | |
181 } | |
182 | |
183 | 155 |
184 /** | 156 /** |
185 * Returns the number formatter for W values. | 157 * Returns the number formatter for W values. |
186 * | 158 * |
187 * @return the number formatter for W values. | 159 * @return the number formatter for W values. |
188 */ | 160 */ |
161 @Override | |
189 protected NumberFormat getWFormatter() { | 162 protected NumberFormat getWFormatter() { |
190 if ("cm".equals(wUnit)) { | 163 if ("cm".equals(this.wUnit)) { |
191 return Formatter.getFormatter(context, 0, 0); | 164 return Formatter.getFormatter(this.context, 0, 0); |
192 } | 165 } |
193 return Formatter.getComputedDischargeW(context); | 166 return Formatter.getComputedDischargeW(this.context); |
194 } | 167 } |
195 | |
196 | 168 |
197 /** | 169 /** |
198 * Returns the number formatter for Q values. | 170 * Returns the number formatter for Q values. |
199 * | 171 * |
200 * @return the number formatter for Q values. | 172 * @return the number formatter for Q values. |
201 */ | 173 */ |
174 @Override | |
202 protected NumberFormat getQFormatter() { | 175 protected NumberFormat getQFormatter() { |
203 return Formatter.getComputedDischargeQ(context); | 176 return Formatter.getComputedDischargeQ(this.context); |
204 } | 177 } |
205 | 178 |
206 | 179 @Override |
207 @Override | 180 protected void writePDF(final OutputStream out) { |
208 protected void writePDF(OutputStream out) { | 181 final WKmsJRDataSource source = createJRData(); |
209 WKmsJRDataSource source = createJRData(); | 182 |
210 | 183 final String jasperFile = Resources.getMsg(this.context.getMeta(), JASPER_FILE, "/jasper/computed-discharge_en.jasper"); |
211 String jasperFile = Resources.getMsg( | 184 final String confPath = Config.getConfigDirectory().toString(); |
212 context.getMeta(), | 185 |
213 JASPER_FILE, | 186 final Map parameters = new HashMap(); |
214 "/jasper/computed-discharge_en.jasper"); | |
215 String confPath = Config.getConfigDirectory().toString(); | |
216 | |
217 Map parameters = new HashMap(); | |
218 parameters.put("ReportTitle", "Exported Data"); | 187 parameters.put("ReportTitle", "Exported Data"); |
219 try { | 188 try { |
220 JasperPrint print = JasperFillManager.fillReport( | 189 final JasperPrint print = JasperFillManager.fillReport(confPath + jasperFile, parameters, source); |
221 confPath + jasperFile, | |
222 parameters, | |
223 source); | |
224 JasperExportManager.exportReportToPdfStream(print, out); | 190 JasperExportManager.exportReportToPdfStream(print, out); |
225 } | 191 } |
226 catch(JRException je) { | 192 catch (final JRException je) { |
227 log.warn("Error generating PDF Report!"); | 193 log.warn("Error generating PDF Report!"); |
228 je.printStackTrace(); | 194 je.printStackTrace(); |
229 } | 195 } |
230 } | 196 } |
231 | 197 |
232 | |
233 protected WKmsJRDataSource createJRData() { | 198 protected WKmsJRDataSource createJRData() { |
234 WKmsJRDataSource source = new WKmsJRDataSource(); | 199 final WKmsJRDataSource source = new WKmsJRDataSource(); |
235 | 200 |
236 addMetaData(source); | 201 addMetaData(source); |
237 addWQData(source); | 202 addWQData(source); |
238 | 203 |
239 return source; | 204 return source; |
240 } | 205 } |
241 | 206 |
242 | 207 protected void addMetaData(final WKmsJRDataSource source) { |
243 protected void addMetaData(WKmsJRDataSource source) { | 208 final CallMeta meta = this.context.getMeta(); |
244 CallMeta meta = context.getMeta(); | 209 |
245 | 210 final D4EArtifact flys = (D4EArtifact) this.master; |
246 D4EArtifact flys = (D4EArtifact) master; | 211 source.addMetaData("gauge", this.gaugeName); |
247 source.addMetaData("gauge", gaugeName); | 212 if (!Double.isNaN(this.gaugeDatum)) { |
248 if (!Double.isNaN(gaugeDatum)) { | 213 final NumberFormat mf = Formatter.getMeterFormat(this.context); |
249 NumberFormat mf = Formatter.getMeterFormat(context); | 214 source.addMetaData("datum", mf.format(this.gaugeDatum) + " " + this.riverUnit); |
250 source.addMetaData( | |
251 "datum", mf.format(gaugeDatum) + " " + riverUnit); | |
252 } else { | 215 } else { |
253 source.addMetaData("datum", ""); | 216 source.addMetaData("datum", ""); |
254 } | 217 } |
255 | 218 |
256 source.addMetaData ("river", RiverUtils.getRivername(flys)); | 219 source.addMetaData("river", RiverUtils.getRivername(flys)); |
257 | 220 |
258 Locale locale = Resources.getLocale(meta); | 221 final Locale locale = Resources.getLocale(meta); |
259 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); | 222 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
260 | 223 |
261 source.addMetaData("date", df.format(new Date())); | 224 source.addMetaData("date", df.format(new Date())); |
262 | 225 |
263 source.addMetaData("wUnit", "cm".equals(wUnit) ? "cm" : riverUnit); | 226 source.addMetaData("wUnit", "cm".equals(this.wUnit) ? "cm" : this.riverUnit); |
264 | 227 |
265 RangeAccess rangeAccess = new RangeAccess(flys); | 228 final RangeAccess rangeAccess = new RangeAccess(flys); |
266 double[] kms = rangeAccess.getKmRange(); | 229 final double[] kms = rangeAccess.getKmRange(); |
267 source.addMetaData("range", | 230 source.addMetaData("range", Formatter.getCalculationKm(this.context.getMeta()).format(kms[0])); |
268 Formatter.getCalculationKm(context.getMeta()).format(kms[0])); | 231 |
269 | 232 if (!"cm".equals(this.wUnit)) { |
270 if (!"cm".equals(wUnit)) { | |
271 source.addMetaData("valid_since", ""); | 233 source.addMetaData("valid_since", ""); |
272 source.addMetaData("calculation", Resources.getMsg( | 234 source.addMetaData("calculation", Resources.getMsg(locale, PDF_HEADER_CALC_MODE, "Computed Discharge")); |
273 locale, | |
274 PDF_HEADER_CALC_MODE, | |
275 "Computed Discharge")); | |
276 } else { | 235 } else { |
277 source.addMetaData( | 236 source.addMetaData("valid_since", this.validSince == null ? "" : df.format(this.validSince)); |
278 "valid_since", | 237 source.addMetaData("calculation", Resources.getMsg(locale, PDF_HEADER_MODE, "Discharge")); |
279 validSince == null ? "" : df.format(validSince)); | 238 } |
280 source.addMetaData("calculation", Resources.getMsg( | 239 } |
281 locale, | 240 |
282 PDF_HEADER_MODE, | 241 protected void addWQData(final WKmsJRDataSource source) { |
283 "Discharge")); | 242 final NumberFormat wf = getWFormatter(); |
284 } | 243 final NumberFormat qf = getQFormatter(); |
285 } | |
286 | |
287 protected void addWQData(WKmsJRDataSource source) { | |
288 NumberFormat wf = getWFormatter(); | |
289 NumberFormat qf = getQFormatter(); | |
290 | 244 |
291 double[] res = new double[3]; | 245 double[] res = new double[3]; |
292 | 246 |
293 for (WQKms wqkms: data) { | 247 for (final WQKms wqkms : this.data) { |
294 int size = wqkms.size(); | 248 final int size = wqkms.size(); |
295 | 249 |
296 for (int i = 0; i < size; i++) { | 250 for (int i = 0; i < size; i++) { |
297 res = wqkms.get(i, res); | 251 res = wqkms.get(i, res); |
298 | 252 |
299 source.addData(new String[] { | 253 source.addData(new String[] { "", // Empty, the WKmsJRDtasource stores km here. |
300 "", // Empty, the WKmsJRDtasource stores km here. | 254 wf.format(res[0]), qf.format(res[1]) }); |
301 wf.format(res[0]), | |
302 qf.format(res[1]) | |
303 }); | |
304 } | 255 } |
305 } | 256 } |
306 } | 257 } |
307 } | 258 } |
308 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 259 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |