Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhExporter.java @ 8946:5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
author | gernotbelger |
---|---|
date | Tue, 13 Mar 2018 18:49:33 +0100 |
parents | 9c02733a1b3c |
children | a4f1ac81f26d |
rev | line source |
---|---|
8928 | 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.util.ArrayList; | |
12 import java.util.Collection; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings; | |
16 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter; | |
17 import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource; | |
18 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
19 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
20 import org.dive4elements.river.utils.RiverUtils; | |
21 | |
22 import au.com.bytecode.opencsv.CSVWriter; | |
23 | |
24 /** | |
25 * Generates different output formats (csv, pdf) of data that resulted from a tkh computation. | |
26 * | |
27 * @author Gernot Belger | |
28 */ | |
29 // REMARK: must be public because its registered in generators.xml | |
30 public class TkhExporter extends AbstractSInfoExporter<TkhResultRow, TkhCalculationResult, TkhCalculationResults> { | |
31 | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
32 private static enum ExportMode { |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
33 pdf, csv |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
34 } |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
35 |
8928 | 36 /** The log used in this exporter. */ |
37 private static Logger log = Logger.getLogger(TkhExporter.class); | |
38 | |
39 private static final String CSV_META_CALCULATION_FORMULA = "sinfo.export.tkh.calculation.formula"; | |
40 | |
41 private static final String CSV_TKH_HEADER = "sinfo.export.tkh.csv.header.tkh"; | |
42 | |
43 private static final String CSV_TKHKIND_HEADER = "sinfo.export.tkh.csv.header.tkhkind"; | |
44 | |
45 private static final String PREFIX_TKH_KIND = "sinfo.export.tkh.soilkind."; | |
46 | |
47 private static final String CSV_MEAN_BED_HEIGHT_HEADER_SHORT = "sinfo.export.flow_depth.csv.header.mean_bed_height.short"; | |
48 | |
49 private static final String JASPER_FILE = "/jasper/sinfo.flowdepth.jasper"; | |
50 | |
51 @Override | |
52 protected Logger getLog() { | |
53 return log; | |
54 } | |
55 | |
56 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
57 protected void writeCSVGlobalMetadata(final CSVWriter writer, final TkhCalculationResults results) { |
8928 | 58 log.info("TkhExporter.writeCSVMeta"); |
59 | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
60 super.writeCSVGlobalMetadataDefaults(writer, results); |
8928 | 61 |
62 // "# Berechnungsgrundlage: Gleichung nach GILL (1971)" | |
63 writeCSVMetaEntry(writer, CSV_META_CALCULATION_FORMULA); | |
64 | |
65 writer.writeNext(new String[] { "" }); | |
66 } | |
67 | |
68 /** | |
69 * Write the header, with different headings depending on whether at a gauge or at a location. | |
70 */ | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
71 |
8928 | 72 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
73 protected void writeCSVHeader(final CSVWriter writer, final TkhCalculationResults results, final RiverInfo river) { |
8928 | 74 log.info("TkhExporter.writeCSVHeader"); |
75 | |
76 final Collection<String> header = new ArrayList<>(11); | |
77 | |
78 header.add(msg(SInfoI18NStrings.CSV_KM_HEADER)); | |
79 header.add(msgUnit(CSV_TKH_HEADER, SInfoI18NStrings.UNIT_CM)); | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
80 header.add(msg(CSV_TKHKIND_HEADER)); |
8928 | 81 header.add(msgUnit(SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, river.getWstUnit())); |
82 | |
83 header.add(msgUnit(SInfoI18NStrings.CSV_WATERLEVEL_HEADER, river.getWstUnit())); | |
84 header.add(msgUnit(SInfoI18NStrings.CSV_DISCHARGE_HEADER, SInfoI18NStrings.UNIT_CUBIC_M)); | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
85 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
86 final String descriptionHeader = results.getDescriptionHeader(); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
87 if (descriptionHeader != null) |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
88 header.add(msg(descriptionHeader)); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
89 |
8928 | 90 header.add(msg(SInfoI18NStrings.CSV_GAUGE_HEADER)); |
91 header.add(msg(SInfoI18NStrings.CSV_LOCATION_HEADER)); | |
92 | |
93 writer.writeNext(header.toArray(new String[header.size()])); | |
94 } | |
95 | |
96 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
97 // FIXME: rename |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
98 protected void writeCSVResultMetadata(final CSVWriter writer, final TkhCalculationResults results, final TkhCalculationResult result) { |
8928 | 99 |
100 final WstInfo wst = result.getWst(); | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
101 super.writeCSVWaterlevelMetadata(writer, wst); |
8928 | 102 |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
103 // TODO: |
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
104 // "# W/Pegel [cm]: " (nur bei Eingabe des Wasserstands am Pegel) |
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
105 // "# Q (m³/s): " (nur bei Eingabe des Durchflusses) |
8928 | 106 } |
107 | |
108 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
109 protected String[] formatCSVRow(final TkhCalculationResults results, final TkhResultRow row) { |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
110 return formatRow(results, row, ExportMode.csv); |
8928 | 111 } |
112 | |
113 /** | |
114 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
115 * | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
116 * @param results |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
117 * |
8928 | 118 * @param useTkh |
119 */ | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
120 private String[] formatRow(final TkhCalculationResults results, final TkhResultRow row, final ExportMode mode) { |
8928 | 121 |
122 final Collection<String> lines = new ArrayList<>(11); | |
123 | |
124 // Fluss-km | |
125 lines.add(getKmFormatter().format(row.getStation())); | |
126 | |
127 // TKH [cm] | |
128 lines.add(getTkhFormatter().format(row.getTkh())); | |
129 | |
130 // Einteilung der Gewässersohle (starr/mobil) | |
131 lines.add(msg(PREFIX_TKH_KIND + row.getTkhKind().name())); | |
132 | |
133 // Mittlere Sohlhöhe [NN + m] | |
134 lines.add(getMeanBedHeighFormatter().format(row.getMeanBedHeight())); | |
135 | |
136 // Wasserstand [NN + m] | |
137 lines.add(getW2Formatter().format(row.getWaterlevel())); | |
138 | |
139 // Q [m³/s] | |
140 final double discharge = row.getDischarge(); | |
141 final double roundedDischarge = RiverUtils.roundQ(discharge); | |
142 lines.add(getQFormatter().format(roundedDischarge)); | |
143 | |
144 // Bezeichnung | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
145 // REMARK: always export this column in pdf-mode, because WInfo also does it (no need for two jasper-templates). |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
146 if (results.getDescriptionHeader() != null || mode == ExportMode.pdf) |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
147 lines.add(row.getWaterlevelLabel()); |
8928 | 148 |
149 // Bezugspegel | |
150 lines.add(row.getGauge()); | |
151 | |
152 // Lage | |
153 lines.add(row.getLocation()); | |
154 | |
155 return lines.toArray(new String[lines.size()]); | |
156 } | |
157 | |
158 @Override | |
159 protected final String getJasperFile() { | |
160 return JASPER_FILE; | |
161 } | |
162 | |
163 @Override | |
164 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final TkhCalculationResults results) { | |
165 | |
166 /* general metadata */ | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
167 super.addJRMetaDataDefaults(source, results); |
8928 | 168 |
169 /* column headings */ | |
170 source.addMetaData("station_header", msg(SInfoI18NStrings.CSV_KM_HEADER)); | |
171 source.addMetaData("tkh_header", msg(CSV_TKH_HEADER)); | |
172 source.addMetaData("bedheight_header", msg(CSV_MEAN_BED_HEIGHT_HEADER_SHORT)); | |
173 source.addMetaData("waterlevel_header", msg(SInfoI18NStrings.CSV_WATERLEVEL_HEADER)); | |
174 source.addMetaData("discharge_header", msg(SInfoI18NStrings.CSV_DISCHARGE_HEADER)); | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
175 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
176 // REMARK: actually the column makes no sense if description header is null. But (software symmetry...) WINFO also |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
177 // writes an empty column into the pdf in that case (most probably to avoid the need for two jasper templates). |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
178 final String descriptionHeader = results.getDescriptionHeader(); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
179 final String waterlevelNameHeader = descriptionHeader == null ? msg(SInfoI18NStrings.CSV_LABEL_HEADER) : descriptionHeader; |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
180 source.addMetaData("waterlevel_name_header", waterlevelNameHeader); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
181 |
8928 | 182 source.addMetaData("gauge_header", msg(SInfoI18NStrings.CSV_GAUGE_HEADER)); |
183 source.addMetaData("location_header", msg(SInfoI18NStrings.CSV_LOCATION_HEADER)); | |
184 } | |
185 | |
186 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
187 protected String[] formatPDFRow(final TkhCalculationResults results, final TkhResultRow row) { |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
188 return formatRow(results, row, ExportMode.pdf); |
8928 | 189 } |
190 } |