Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthExporter.java @ 8938:9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
author | gernotbelger |
---|---|
date | Tue, 06 Mar 2018 17:09:39 +0100 |
parents | 791714b92b5c |
children | 5d5d482da3e9 |
rev | line source |
---|---|
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 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
74 protected void writeCSVResultMetadata(final CSVWriter writer, final FlowDepthCalculationResults results, 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 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
107 protected void writeCSVGlobalMetadata(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 */ |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
147 |
8928 | 148 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
149 protected void writeCSVHeader(final CSVWriter writer, final FlowDepthCalculationResults results, final RiverInfo river) { |
8854 | 150 log.info("FlowDepthExporter.writeCSVHeader"); |
151 | |
152 final Collection<String> header = new ArrayList<>(11); | |
8877 | 153 |
8928 | 154 header.add(msg(SInfoI18NStrings.CSV_KM_HEADER)); |
155 header.add(msgUnit(CSV_FLOWDEPTH_HEADER, SInfoI18NStrings.UNIT_M)); | |
156 if (getData().isUseTkh()) { | |
157 header.add(msgUnit(CSV_FLOWDEPTHTKH_HEADER, SInfoI18NStrings.UNIT_M)); | |
158 header.add(msgUnit(CSV_TKH_HEADER, SInfoI18NStrings.UNIT_CM)); | |
8854 | 159 } |
8863 | 160 |
8928 | 161 header.add(msgUnit(SInfoI18NStrings.CSV_WATERLEVEL_HEADER, river.getWstUnit())); |
162 header.add(msgUnit(SInfoI18NStrings.CSV_DISCHARGE_HEADER, SInfoI18NStrings.UNIT_CUBIC_M)); | |
163 header.add(msg(SInfoI18NStrings.CSV_LABEL_HEADER)); | |
164 header.add(msg(SInfoI18NStrings.CSV_GAUGE_HEADER)); | |
165 header.add(msgUnit(SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, river.getWstUnit())); | |
8854 | 166 header.add(msg(CSV_SOUNDING_HEADER)); |
8928 | 167 header.add(msg(SInfoI18NStrings.CSV_LOCATION_HEADER)); |
8854 | 168 |
8877 | 169 writer.writeNext(header.toArray(new String[header.size()])); |
8854 | 170 } |
171 | |
8928 | 172 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
173 protected String[] formatCSVRow(final FlowDepthCalculationResults results, final FlowDepthRow row) { |
8928 | 174 return formatFlowDepthRow(row); |
175 } | |
176 | |
8854 | 177 /** |
178 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
8877 | 179 * |
180 * @param useTkh | |
8854 | 181 */ |
8928 | 182 private String[] formatFlowDepthRow(final FlowDepthRow row) { |
8854 | 183 |
8877 | 184 final Collection<String> lines = new ArrayList<>(11); |
8854 | 185 |
8877 | 186 // Fluss-km |
187 lines.add(getKmFormatter().format(row.getStation())); | |
188 | |
189 // Fließtiefe [m] | |
190 lines.add(getFlowDepthFormatter().format(row.getFlowDepth())); | |
191 | |
8928 | 192 if (getData().isUseTkh()) { |
8877 | 193 // Fließtiefe mit TKH [m] |
194 lines.add(getFlowDepthFormatter().format(row.getFlowDepthWithTkh())); | |
195 | |
196 // TKH [cm] | |
197 lines.add(getTkhFormatter().format(row.getTkh())); | |
198 } | |
199 | |
200 // Wasserstand [NN + m] | |
201 lines.add(getW2Formatter().format(row.getWaterlevel())); | |
202 | |
203 // Q [m³/s] | |
204 final double discharge = row.getDischarge(); | |
8884 | 205 if (Double.isNaN(discharge)) |
8877 | 206 lines.add(StringUtils.EMPTY); |
8884 | 207 else { |
8882 | 208 final double roundedDischarge = RiverUtils.roundQ(discharge); |
209 lines.add(getQFormatter().format(roundedDischarge)); | |
210 } | |
8877 | 211 |
212 // Bezeichnung | |
213 lines.add(row.getWaterlevelLabel()); | |
214 | |
215 // Bezugspegel | |
216 lines.add(row.getGauge()); | |
217 | |
218 // Mittlere Sohlhöhe [NN + m] | |
219 lines.add(getMeanBedHeighFormatter().format(row.getMeanBedHeight())); | |
220 | |
221 // Peilung/Epoche | |
222 lines.add(row.getSoundageLabel()); | |
223 | |
224 // Lage | |
225 lines.add(row.getLocation()); | |
226 | |
227 return lines.toArray(new String[lines.size()]); | |
8854 | 228 } |
8877 | 229 |
8928 | 230 @Override |
231 protected final String getJasperFile() { | |
232 return JASPER_FILE; | |
8854 | 233 } |
234 | |
8877 | 235 @Override |
8928 | 236 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final FlowDepthCalculationResults results) { |
8854 | 237 |
8894 | 238 final RiverInfo river = results.getRiver(); |
239 final String wstUnitName = river.getWstUnit(); | |
8863 | 240 |
8877 | 241 /* general metadata */ |
8928 | 242 source.addMetaData("header", msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL)); |
8877 | 243 source.addMetaData("calcMode", results.getCalcModeLabel()); |
244 | |
8928 | 245 source.addMetaData("version_label", msg(SInfoI18NStrings.CSV_META_VERSION_LABEL)); |
8863 | 246 source.addMetaData("version", FLYS.VERSION); |
247 | |
8928 | 248 source.addMetaData("user_label", msg(SInfoI18NStrings.CSV_META_USER_LABEL)); |
8863 | 249 source.addMetaData("user", results.getUser()); |
8877 | 250 |
251 final Locale locale = Resources.getLocale(this.context.getMeta()); | |
8854 | 252 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
8928 | 253 source.addMetaData("date_label", msg(SInfoI18NStrings.CSV_META_CREATION_LABEL)); |
8854 | 254 source.addMetaData("date", df.format(new Date())); |
255 | |
8928 | 256 source.addMetaData("river_label", msg(SInfoI18NStrings.CSV_META_RIVER_LABEL)); |
8863 | 257 source.addMetaData("river", river.getName()); |
8854 | 258 |
8894 | 259 final DoubleRange calcRange = results.getCalcRange(); |
260 final NumberFormat kmFormatter = getKmFormatter(); | |
261 final String rangeValue = String.format("%s - %s", kmFormatter.format(calcRange.getMinimumDouble()), kmFormatter.format(calcRange.getMaximumDouble())); | |
8928 | 262 source.addMetaData("range_label", msg(SInfoI18NStrings.CSV_META_RANGE_LABEL)); |
8863 | 263 source.addMetaData("range", rangeValue); |
8854 | 264 |
8863 | 265 /* column headings */ |
8928 | 266 source.addMetaData("station_header", msg(SInfoI18NStrings.CSV_KM_HEADER)); |
8863 | 267 source.addMetaData("flowdepth_header", msg(CSV_FLOWDEPTH_HEADER)); |
268 source.addMetaData("flowdepth_tkh_header", msg(CSV_FLOWDEPTHTKH_HEADER)); | |
269 source.addMetaData("tkh_header", msg(CSV_TKH_HEADER)); | |
8928 | 270 source.addMetaData("waterlevel_header", msg(SInfoI18NStrings.CSV_WATERLEVEL_HEADER)); |
8863 | 271 source.addMetaData("river_unit", wstUnitName); |
8928 | 272 source.addMetaData("discharge_header", msg(SInfoI18NStrings.CSV_DISCHARGE_HEADER)); |
273 source.addMetaData("waterlevel_name_header", msg(SInfoI18NStrings.CSV_LABEL_HEADER)); | |
274 source.addMetaData("gauge_header", msg(SInfoI18NStrings.CSV_GAUGE_HEADER)); | |
8863 | 275 source.addMetaData("bedheight_header", msg(CSV_MEAN_BED_HEIGHT_HEADER_SHORT)); |
276 source.addMetaData("sounding_name_header", msg(CSV_SOUNDING_HEADER)); | |
8928 | 277 source.addMetaData("location_header", msg(SInfoI18NStrings.CSV_LOCATION_HEADER)); |
8854 | 278 } |
279 | |
8928 | 280 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
281 protected String[] formatPDFRow(final FlowDepthCalculationResults results, final FlowDepthRow row) { |
8928 | 282 return formatFlowDepthRow(row); |
8877 | 283 } |
8928 | 284 } |