8854
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by Intevation GmbH |
|
3 * |
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
6 * documentation coming with Dive4Elements River for details. |
|
7 */ |
|
8 |
|
9 package org.dive4elements.river.artifacts.sinfo.flowdepth; |
|
10 |
|
11 import java.text.DateFormat; |
8894
|
12 import java.text.NumberFormat; |
8854
|
13 import java.util.ArrayList; |
|
14 import java.util.Collection; |
|
15 import java.util.Date; |
|
16 import java.util.Locale; |
|
17 |
8877
|
18 import org.apache.commons.lang.StringUtils; |
8894
|
19 import org.apache.commons.lang.math.DoubleRange; |
8854
|
20 import org.apache.log4j.Logger; |
8863
|
21 import org.dive4elements.river.FLYS; |
8854
|
22 import org.dive4elements.river.artifacts.resources.Resources; |
8928
|
23 import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings; |
|
24 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter; |
8894
|
25 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; |
8854
|
26 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; |
8894
|
27 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
|
28 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; |
8882
|
29 import org.dive4elements.river.utils.RiverUtils; |
8854
|
30 |
|
31 import au.com.bytecode.opencsv.CSVWriter; |
|
32 |
|
33 /** |
|
34 * Generates different output formats (csv, pdf) of data that resulted from a flow depths computation. |
|
35 * |
|
36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
|
37 * @author Gernot Belger |
|
38 */ |
|
39 // REMARK: must be public because its registered in generators.xml |
8928
|
40 public class FlowDepthExporter extends AbstractSInfoExporter<FlowDepthRow, FlowDepthCalculationResult, FlowDepthCalculationResults> { |
8854
|
41 |
8877
|
42 /** The log used in this exporter. */ |
8854
|
43 private static Logger log = Logger.getLogger(FlowDepthExporter.class); |
|
44 |
|
45 private static final String CSV_FLOWDEPTH_HEADER = "sinfo.export.flow_depth.csv.header.flowdepth"; |
|
46 private static final String CSV_FLOWDEPTHTKH_HEADER = "sinfo.export.flow_depth.csv.header.flowdepthTkh"; |
|
47 private static final String CSV_TKH_HEADER = "sinfo.export.flow_depth.csv.header.tkh"; |
8928
|
48 |
8863
|
49 private static final String CSV_MEAN_BED_HEIGHT_HEADER_SHORT = "sinfo.export.flow_depth.csv.header.mean_bed_height.short"; |
8854
|
50 private static final String CSV_SOUNDING_HEADER = "sinfo.export.flow_depth.csv.header.sounding"; |
8877
|
51 |
|
52 private static final String CSV_META_HEADER_SOUNDING = "sinfo.export.flow_depth.csv.meta.header.sounding"; |
|
53 |
|
54 private static final String CSV_META_HEADER_SOUNDING_YEAR = "sinfo.export.flow_depth.csv.meta.header.sounding.year"; |
|
55 |
|
56 private static final String CSV_META_HEADER_SOUNDING_TYPE = "sinfo.export.flow_depth.csv.meta.header.sounding.type"; |
|
57 |
8882
|
58 private static final String CSV_META_HEADER_SOUNDING_EVALUATOR = "sinfo.export.flow_depth.csv.meta.header.sounding.evaluator"; |
|
59 |
8877
|
60 private static final String CSV_META_HEADER_SOUNDING_PRJ = "sinfo.export.flow_depth.csv.meta.header.sounding.prj"; |
|
61 |
|
62 private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel"; |
|
63 |
|
64 private static final String CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL = "sinfo.export.flow_depth.csv.meta.header.sounding.elevationmodel.original"; |
|
65 |
|
66 private static final String JASPER_FILE = "/jasper/sinfo.flowdepth.jasper"; |
8863
|
67 |
8928
|
68 @Override |
|
69 protected Logger getLog() { |
|
70 return log; |
8863
|
71 } |
8877
|
72 |
8854
|
73 @Override |
8928
|
74 protected void writeCSVResultHeader(final CSVWriter writer, final FlowDepthCalculationResult result) { |
8877
|
75 |
|
76 /* first some specific metadata */ |
|
77 final BedHeightInfo sounding = result.getSounding(); |
|
78 final WstInfo wst = result.getWst(); |
|
79 |
|
80 // "##METADATEN PEILUNG" |
|
81 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING); |
|
82 |
|
83 // "# Jahr der Peilung: " |
|
84 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_YEAR, Integer.toString(sounding.getYear())); |
|
85 // "# Aufnahmeart: " |
|
86 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_TYPE, sounding.getType()); |
8882
|
87 // "# Auswerter: " |
|
88 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_EVALUATOR, sounding.getEvaluationBy()); |
8877
|
89 // "# Lagesystem: " |
|
90 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_PRJ, sounding.getLocationSystem()); |
|
91 // "# Höhensystem: " |
8879
|
92 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL, sounding.getCurElevationModelUnit()); |
8877
|
93 // "# ursprüngliches Höhensystem: " |
8884
|
94 writeCSVMetaEntry(writer, CSV_META_HEADER_SOUNDING_ELEVATIOIN_MODEL_ORIGINAL, sounding.getOldElevationModelUnit()); |
8877
|
95 |
|
96 // "##METADATEN WASSERSPIEGELLAGE" |
8928
|
97 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL); |
8877
|
98 // "# Bezeichnung der Wasserspiegellage: " |
8928
|
99 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, wst.getLabel()); |
8879
|
100 // "# Bezugspegel: " |
8928
|
101 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, wst.getGauge()); |
8877
|
102 // "# Jahr/Zeitraum der Wasserspiegellage: " |
8928
|
103 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_YEAR, Integer.toString(wst.getYear())); |
8877
|
104 } |
8854
|
105 |
8928
|
106 @Override |
|
107 protected final void writeCSVMeta(final CSVWriter writer, final FlowDepthCalculationResults results) { |
8854
|
108 log.info("FlowDepthExporter.writeCSVMeta"); |
|
109 |
8863
|
110 final String calcModeLabel = results.getCalcModeLabel(); |
8894
|
111 final RiverInfo river = results.getRiver(); |
8928
|
112 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_RESULT, msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel); |
8854
|
113 |
8877
|
114 // "# FLYS-Version: " |
8928
|
115 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_VERSION, msg(SInfoI18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION); |
8854
|
116 |
8877
|
117 // "# Bearbeiter: " |
8928
|
118 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_USER, msg(SInfoI18NStrings.CSV_META_USER_LABEL), results.getUser()); |
8854
|
119 |
|
120 // "# Datum der Erstellung: " |
8877
|
121 final Locale locale = Resources.getLocale(this.context.getMeta()); |
8854
|
122 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
8928
|
123 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_CREATION, msg(SInfoI18NStrings.CSV_META_CREATION_LABEL), df.format(new Date())); |
8854
|
124 |
|
125 // "# Gewässer: " |
8928
|
126 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_RIVER, msg(SInfoI18NStrings.CSV_META_RIVER_LABEL), river.getName()); |
8854
|
127 |
|
128 // "# Höhensystem des Flusses: " |
8928
|
129 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); |
8854
|
130 |
|
131 // "# Ort/Bereich (km): " |
8894
|
132 final DoubleRange calcRange = results.getCalcRange(); |
8928
|
133 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_RANGE, msg(SInfoI18NStrings.CSV_META_RANGE_LABEL), |
|
134 getKmFormatter().format(calcRange.getMinimumDouble()), |
8894
|
135 getKmFormatter().format(calcRange.getMaximumDouble())); |
8854
|
136 |
|
137 writer.writeNext(new String[] { "" }); |
|
138 } |
|
139 |
8877
|
140 /** |
8854
|
141 * Write the header, with different headings depending on whether at a |
|
142 * gauge or at a location. |
8877
|
143 * |
|
144 * @param river |
|
145 * @param useTkh |
8854
|
146 */ |
8928
|
147 @Override |
|
148 protected final void writeCSVHeader(final CSVWriter writer, final RiverInfo river) { |
8854
|
149 log.info("FlowDepthExporter.writeCSVHeader"); |
|
150 |
|
151 final Collection<String> header = new ArrayList<>(11); |
8877
|
152 |
8928
|
153 header.add(msg(SInfoI18NStrings.CSV_KM_HEADER)); |
|
154 header.add(msgUnit(CSV_FLOWDEPTH_HEADER, SInfoI18NStrings.UNIT_M)); |
|
155 if (getData().isUseTkh()) { |
|
156 header.add(msgUnit(CSV_FLOWDEPTHTKH_HEADER, SInfoI18NStrings.UNIT_M)); |
|
157 header.add(msgUnit(CSV_TKH_HEADER, SInfoI18NStrings.UNIT_CM)); |
8854
|
158 } |
8863
|
159 |
8928
|
160 header.add(msgUnit(SInfoI18NStrings.CSV_WATERLEVEL_HEADER, river.getWstUnit())); |
|
161 header.add(msgUnit(SInfoI18NStrings.CSV_DISCHARGE_HEADER, SInfoI18NStrings.UNIT_CUBIC_M)); |
|
162 header.add(msg(SInfoI18NStrings.CSV_LABEL_HEADER)); |
|
163 header.add(msg(SInfoI18NStrings.CSV_GAUGE_HEADER)); |
|
164 header.add(msgUnit(SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, river.getWstUnit())); |
8854
|
165 header.add(msg(CSV_SOUNDING_HEADER)); |
8928
|
166 header.add(msg(SInfoI18NStrings.CSV_LOCATION_HEADER)); |
8854
|
167 |
8877
|
168 writer.writeNext(header.toArray(new String[header.size()])); |
8854
|
169 } |
|
170 |
8928
|
171 @Override |
|
172 protected final String[] formatCSVRow(final FlowDepthRow row) { |
|
173 return formatFlowDepthRow(row); |
|
174 } |
|
175 |
8854
|
176 /** |
|
177 * Format a row of a flow depth result into an array of string, both used by csv and pdf |
8877
|
178 * |
|
179 * @param useTkh |
8854
|
180 */ |
8928
|
181 private String[] formatFlowDepthRow(final FlowDepthRow row) { |
8854
|
182 |
8877
|
183 final Collection<String> lines = new ArrayList<>(11); |
8854
|
184 |
8877
|
185 // Fluss-km |
|
186 lines.add(getKmFormatter().format(row.getStation())); |
|
187 |
|
188 // Fließtiefe [m] |
|
189 lines.add(getFlowDepthFormatter().format(row.getFlowDepth())); |
|
190 |
8928
|
191 if (getData().isUseTkh()) { |
8877
|
192 // Fließtiefe mit TKH [m] |
|
193 lines.add(getFlowDepthFormatter().format(row.getFlowDepthWithTkh())); |
|
194 |
|
195 // TKH [cm] |
|
196 lines.add(getTkhFormatter().format(row.getTkh())); |
|
197 } |
|
198 |
|
199 // Wasserstand [NN + m] |
|
200 lines.add(getW2Formatter().format(row.getWaterlevel())); |
|
201 |
|
202 // Q [m³/s] |
|
203 final double discharge = row.getDischarge(); |
8884
|
204 if (Double.isNaN(discharge)) |
8877
|
205 lines.add(StringUtils.EMPTY); |
8884
|
206 else { |
8882
|
207 final double roundedDischarge = RiverUtils.roundQ(discharge); |
|
208 lines.add(getQFormatter().format(roundedDischarge)); |
|
209 } |
8877
|
210 |
|
211 // Bezeichnung |
|
212 lines.add(row.getWaterlevelLabel()); |
|
213 |
|
214 // Bezugspegel |
|
215 lines.add(row.getGauge()); |
|
216 |
|
217 // Mittlere Sohlhöhe [NN + m] |
|
218 lines.add(getMeanBedHeighFormatter().format(row.getMeanBedHeight())); |
|
219 |
|
220 // Peilung/Epoche |
|
221 lines.add(row.getSoundageLabel()); |
|
222 |
|
223 // Lage |
|
224 lines.add(row.getLocation()); |
|
225 |
|
226 return lines.toArray(new String[lines.size()]); |
8854
|
227 } |
8877
|
228 |
8928
|
229 @Override |
|
230 protected final String getJasperFile() { |
|
231 return JASPER_FILE; |
8854
|
232 } |
|
233 |
8877
|
234 @Override |
8928
|
235 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final FlowDepthCalculationResults results) { |
8854
|
236 |
8894
|
237 final RiverInfo river = results.getRiver(); |
|
238 final String wstUnitName = river.getWstUnit(); |
8863
|
239 |
8877
|
240 /* general metadata */ |
8928
|
241 source.addMetaData("header", msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL)); |
8877
|
242 source.addMetaData("calcMode", results.getCalcModeLabel()); |
|
243 |
8928
|
244 source.addMetaData("version_label", msg(SInfoI18NStrings.CSV_META_VERSION_LABEL)); |
8863
|
245 source.addMetaData("version", FLYS.VERSION); |
|
246 |
8928
|
247 source.addMetaData("user_label", msg(SInfoI18NStrings.CSV_META_USER_LABEL)); |
8863
|
248 source.addMetaData("user", results.getUser()); |
8877
|
249 |
|
250 final Locale locale = Resources.getLocale(this.context.getMeta()); |
8854
|
251 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
8928
|
252 source.addMetaData("date_label", msg(SInfoI18NStrings.CSV_META_CREATION_LABEL)); |
8854
|
253 source.addMetaData("date", df.format(new Date())); |
|
254 |
8928
|
255 source.addMetaData("river_label", msg(SInfoI18NStrings.CSV_META_RIVER_LABEL)); |
8863
|
256 source.addMetaData("river", river.getName()); |
8854
|
257 |
8894
|
258 final DoubleRange calcRange = results.getCalcRange(); |
|
259 final NumberFormat kmFormatter = getKmFormatter(); |
|
260 final String rangeValue = String.format("%s - %s", kmFormatter.format(calcRange.getMinimumDouble()), kmFormatter.format(calcRange.getMaximumDouble())); |
8928
|
261 source.addMetaData("range_label", msg(SInfoI18NStrings.CSV_META_RANGE_LABEL)); |
8863
|
262 source.addMetaData("range", rangeValue); |
8854
|
263 |
8863
|
264 /* column headings */ |
8928
|
265 source.addMetaData("station_header", msg(SInfoI18NStrings.CSV_KM_HEADER)); |
8863
|
266 source.addMetaData("flowdepth_header", msg(CSV_FLOWDEPTH_HEADER)); |
|
267 source.addMetaData("flowdepth_tkh_header", msg(CSV_FLOWDEPTHTKH_HEADER)); |
|
268 source.addMetaData("tkh_header", msg(CSV_TKH_HEADER)); |
8928
|
269 source.addMetaData("waterlevel_header", msg(SInfoI18NStrings.CSV_WATERLEVEL_HEADER)); |
8863
|
270 source.addMetaData("river_unit", wstUnitName); |
8928
|
271 source.addMetaData("discharge_header", msg(SInfoI18NStrings.CSV_DISCHARGE_HEADER)); |
|
272 source.addMetaData("waterlevel_name_header", msg(SInfoI18NStrings.CSV_LABEL_HEADER)); |
|
273 source.addMetaData("gauge_header", msg(SInfoI18NStrings.CSV_GAUGE_HEADER)); |
8863
|
274 source.addMetaData("bedheight_header", msg(CSV_MEAN_BED_HEIGHT_HEADER_SHORT)); |
|
275 source.addMetaData("sounding_name_header", msg(CSV_SOUNDING_HEADER)); |
8928
|
276 source.addMetaData("location_header", msg(SInfoI18NStrings.CSV_LOCATION_HEADER)); |
8854
|
277 } |
|
278 |
8928
|
279 @Override |
|
280 protected final String[] formatPDFRow(final FlowDepthRow row) { |
|
281 return formatFlowDepthRow(row); |
8877
|
282 } |
8928
|
283 } |