comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhExporter.java @ 8928:791714b92b5c

Basically implemented SINFO-Tkh Exports
author gernotbelger
date Thu, 01 Mar 2018 18:49:34 +0100
parents
children 9c02733a1b3c
comparison
equal deleted inserted replaced
8927:04ad2cfce559 8928:791714b92b5c
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.tkhstate;
10
11 import java.text.DateFormat;
12 import java.text.NumberFormat;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.Locale;
17
18 import org.apache.commons.lang.math.DoubleRange;
19 import org.apache.log4j.Logger;
20 import org.dive4elements.river.FLYS;
21 import org.dive4elements.river.artifacts.resources.Resources;
22 import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings;
23 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter;
24 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource;
25 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
26 import org.dive4elements.river.artifacts.sinfo.util.WstInfo;
27 import org.dive4elements.river.utils.RiverUtils;
28
29 import au.com.bytecode.opencsv.CSVWriter;
30
31 /**
32 * Generates different output formats (csv, pdf) of data that resulted from a tkh computation.
33 *
34 * @author Gernot Belger
35 */
36 // REMARK: must be public because its registered in generators.xml
37 public class TkhExporter extends AbstractSInfoExporter<TkhResultRow, TkhCalculationResult, TkhCalculationResults> {
38
39 /** The log used in this exporter. */
40 private static Logger log = Logger.getLogger(TkhExporter.class);
41
42 private static final String CSV_META_CALCULATION_FORMULA = "sinfo.export.tkh.calculation.formula";
43
44 private static final String CSV_TKH_HEADER = "sinfo.export.tkh.csv.header.tkh";
45
46 private static final String CSV_TKHKIND_HEADER = "sinfo.export.tkh.csv.header.tkhkind";
47
48 private static final String PREFIX_TKH_KIND = "sinfo.export.tkh.soilkind.";
49
50 private static final String CSV_MEAN_BED_HEIGHT_HEADER_SHORT = "sinfo.export.flow_depth.csv.header.mean_bed_height.short";
51
52 private static final String JASPER_FILE = "/jasper/sinfo.flowdepth.jasper";
53
54 @Override
55 protected Logger getLog() {
56 return log;
57 }
58
59 @Override
60 protected final void writeCSVMeta(final CSVWriter writer, final TkhCalculationResults results) {
61 log.info("TkhExporter.writeCSVMeta");
62
63 final String calcModeLabel = results.getCalcModeLabel();
64 final RiverInfo river = results.getRiver();
65 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_RESULT, msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL), river.getName(), calcModeLabel);
66
67 // "# FLYS-Version: "
68 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_VERSION, msg(SInfoI18NStrings.CSV_META_VERSION_LABEL), FLYS.VERSION);
69
70 // "# Bearbeiter: "
71 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_USER, msg(SInfoI18NStrings.CSV_META_USER_LABEL), results.getUser());
72
73 // "# Datum der Erstellung: "
74 final Locale locale = Resources.getLocale(this.context.getMeta());
75 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
76 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_CREATION, msg(SInfoI18NStrings.CSV_META_CREATION_LABEL), df.format(new Date()));
77
78 // "# Gewässer: "
79 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_RIVER, msg(SInfoI18NStrings.CSV_META_RIVER_LABEL), river.getName());
80
81 // "# Höhensystem des Flusses: "
82 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit());
83
84 // "# Ort/Bereich (km): "
85 final DoubleRange calcRange = results.getCalcRange();
86 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_RANGE, msg(SInfoI18NStrings.CSV_META_RANGE_LABEL),
87 getKmFormatter().format(calcRange.getMinimumDouble()), getKmFormatter().format(calcRange.getMaximumDouble()));
88
89 // "# Berechnungsgrundlage: Gleichung nach GILL (1971)"
90 writeCSVMetaEntry(writer, CSV_META_CALCULATION_FORMULA);
91
92 writer.writeNext(new String[] { "" });
93 }
94
95 /**
96 * Write the header, with different headings depending on whether at a gauge or at a location.
97 */
98 @Override
99 protected final void writeCSVHeader(final CSVWriter writer, final RiverInfo river) {
100 log.info("TkhExporter.writeCSVHeader");
101
102 final Collection<String> header = new ArrayList<>(11);
103
104 header.add(msg(SInfoI18NStrings.CSV_KM_HEADER));
105 header.add(msgUnit(CSV_TKH_HEADER, SInfoI18NStrings.UNIT_CM));
106 header.add(msgUnit(CSV_TKHKIND_HEADER, SInfoI18NStrings.UNIT_CM));
107 header.add(msgUnit(SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, river.getWstUnit()));
108
109 header.add(msgUnit(SInfoI18NStrings.CSV_WATERLEVEL_HEADER, river.getWstUnit()));
110 header.add(msgUnit(SInfoI18NStrings.CSV_DISCHARGE_HEADER, SInfoI18NStrings.UNIT_CUBIC_M));
111 header.add(msg(SInfoI18NStrings.CSV_LABEL_HEADER));
112 header.add(msg(SInfoI18NStrings.CSV_GAUGE_HEADER));
113 header.add(msg(SInfoI18NStrings.CSV_LOCATION_HEADER));
114
115 writer.writeNext(header.toArray(new String[header.size()]));
116 }
117
118 @Override
119 protected void writeCSVResultHeader(final CSVWriter writer, final TkhCalculationResult result) {
120
121 /* first some specific metadata */
122 final WstInfo wst = result.getWst();
123
124 // "##METADATEN WASSERSPIEGELLAGE"
125 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL);
126 // "# Bezeichnung der Wasserspiegellage: "
127 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, wst.getLabel());
128 // "# Bezugspegel: "
129 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, wst.getGauge());
130 // "# Jahr/Zeitraum der Wasserspiegellage: "
131 writeCSVMetaEntry(writer, SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_YEAR, Integer.toString(wst.getYear()));
132 }
133
134 @Override
135 protected final String[] formatCSVRow(final TkhResultRow row) {
136 return formatRow(row);
137 }
138
139 /**
140 * Format a row of a flow depth result into an array of string, both used by csv and pdf
141 *
142 * @param useTkh
143 */
144 private String[] formatRow(final TkhResultRow row) {
145
146 final Collection<String> lines = new ArrayList<>(11);
147
148 // Fluss-km
149 lines.add(getKmFormatter().format(row.getStation()));
150
151 // TKH [cm]
152 lines.add(getTkhFormatter().format(row.getTkh()));
153
154 // Einteilung der Gewässersohle (starr/mobil)
155 lines.add(msg(PREFIX_TKH_KIND + row.getTkhKind().name()));
156
157 // Mittlere Sohlhöhe [NN + m]
158 lines.add(getMeanBedHeighFormatter().format(row.getMeanBedHeight()));
159
160 // Wasserstand [NN + m]
161 lines.add(getW2Formatter().format(row.getWaterlevel()));
162
163 // Q [m³/s]
164 final double discharge = row.getDischarge();
165 final double roundedDischarge = RiverUtils.roundQ(discharge);
166 lines.add(getQFormatter().format(roundedDischarge));
167
168 // Bezeichnung
169 lines.add(row.getWaterlevelLabel());
170
171 // Bezugspegel
172 lines.add(row.getGauge());
173
174 // Lage
175 lines.add(row.getLocation());
176
177 return lines.toArray(new String[lines.size()]);
178 }
179
180 @Override
181 protected final String getJasperFile() {
182 return JASPER_FILE;
183 }
184
185 @Override
186 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final TkhCalculationResults results) {
187
188 final RiverInfo river = results.getRiver();
189 final String wstUnitName = river.getWstUnit();
190
191 /* general metadata */
192 source.addMetaData("header", msg(SInfoI18NStrings.CSV_META_HEADER_RESULT_LABEL));
193 source.addMetaData("calcMode", results.getCalcModeLabel());
194
195 source.addMetaData("version_label", msg(SInfoI18NStrings.CSV_META_VERSION_LABEL));
196 source.addMetaData("version", FLYS.VERSION);
197
198 source.addMetaData("user_label", msg(SInfoI18NStrings.CSV_META_USER_LABEL));
199 source.addMetaData("user", results.getUser());
200
201 final Locale locale = Resources.getLocale(this.context.getMeta());
202 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
203 source.addMetaData("date_label", msg(SInfoI18NStrings.CSV_META_CREATION_LABEL));
204 source.addMetaData("date", df.format(new Date()));
205
206 source.addMetaData("river_label", msg(SInfoI18NStrings.CSV_META_RIVER_LABEL));
207 source.addMetaData("river", river.getName());
208 source.addMetaData("river_unit", wstUnitName);
209
210 final DoubleRange calcRange = results.getCalcRange();
211 final NumberFormat kmFormatter = getKmFormatter();
212 final String rangeValue = String.format("%s - %s", kmFormatter.format(calcRange.getMinimumDouble()), kmFormatter.format(calcRange.getMaximumDouble()));
213 source.addMetaData("range_label", msg(SInfoI18NStrings.CSV_META_RANGE_LABEL));
214 source.addMetaData("range", rangeValue);
215
216 /* column headings */
217 source.addMetaData("station_header", msg(SInfoI18NStrings.CSV_KM_HEADER));
218 source.addMetaData("tkh_header", msg(CSV_TKH_HEADER));
219 source.addMetaData("bedheight_header", msg(CSV_MEAN_BED_HEIGHT_HEADER_SHORT));
220 source.addMetaData("waterlevel_header", msg(SInfoI18NStrings.CSV_WATERLEVEL_HEADER));
221 source.addMetaData("discharge_header", msg(SInfoI18NStrings.CSV_DISCHARGE_HEADER));
222 source.addMetaData("waterlevel_name_header", msg(SInfoI18NStrings.CSV_LABEL_HEADER));
223 source.addMetaData("gauge_header", msg(SInfoI18NStrings.CSV_GAUGE_HEADER));
224 source.addMetaData("location_header", msg(SInfoI18NStrings.CSV_LOCATION_HEADER));
225 }
226
227 @Override
228 protected final String[] formatPDFRow(final TkhResultRow row) {
229 return formatRow(row);
230 }
231 }

http://dive4elements.wald.intevation.org