Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhExporter.java @ 9105:ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
author | gernotbelger |
---|---|
date | Tue, 29 May 2018 11:36:42 +0200 |
parents | 7134a4c7d1b6 |
children | 23945061daec |
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 | |
9006 | 11 import java.io.OutputStream; |
8928 | 12 import java.util.ArrayList; |
13 import java.util.Collection; | |
14 | |
15 import org.apache.log4j.Logger; | |
8996 | 16 import org.dive4elements.river.artifacts.common.GeneralResultType; |
9006 | 17 import org.dive4elements.river.artifacts.common.JasperReporter; |
18 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource; | |
8996 | 19 import org.dive4elements.river.artifacts.common.ResultRow; |
8928 | 20 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter; |
8948 | 21 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
8928 | 22 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
23 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
24 | |
25 import au.com.bytecode.opencsv.CSVWriter; | |
9006 | 26 import net.sf.jasperreports.engine.JRException; |
8928 | 27 |
28 /** | |
29 * Generates different output formats (csv, pdf) of data that resulted from a tkh computation. | |
30 * | |
31 * @author Gernot Belger | |
32 */ | |
33 // REMARK: must be public because its registered in generators.xml | |
8948 | 34 public class TkhExporter extends AbstractSInfoExporter<TkhCalculationResult, TkhCalculationResults> { |
8928 | 35 |
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 | |
9006 | 41 private static final String JASPER_FILE = "/jasper/templates/sinfo.tkh.jrxml"; |
8928 | 42 |
43 @Override | |
44 protected Logger getLog() { | |
45 return log; | |
46 } | |
47 | |
48 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
49 protected void writeCSVGlobalMetadata(final CSVWriter writer, final TkhCalculationResults results) { |
8928 | 50 log.info("TkhExporter.writeCSVMeta"); |
51 | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
52 super.writeCSVGlobalMetadataDefaults(writer, results); |
8928 | 53 |
54 // "# Berechnungsgrundlage: Gleichung nach GILL (1971)" | |
55 writeCSVMetaEntry(writer, CSV_META_CALCULATION_FORMULA); | |
56 } | |
57 | |
58 /** | |
59 * Write the header, with different headings depending on whether at a gauge or at a location. | |
60 */ | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
61 |
8928 | 62 @Override |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
63 protected void writeCSVHeader(final CSVWriter writer, final TkhCalculationResults results, final RiverInfo river) { |
8928 | 64 log.info("TkhExporter.writeCSVHeader"); |
65 | |
66 final Collection<String> header = new ArrayList<>(11); | |
67 | |
8996 | 68 header.add(msg(GeneralResultType.station.getCsvHeader())); |
8948 | 69 header.add(msgUnit(SInfoResultType.tkh.getCsvHeader(), SInfoResultType.tkh.getUnit())); |
70 header.add(msg(SInfoResultType.soilkind.getCsvHeader())); | |
71 header.add(msgUnit(SInfoResultType.meanBedHeight.getCsvHeader(), river.getWstUnit())); | |
72 header.add(msgUnit(SInfoResultType.waterlevel.getCsvHeader(), river.getWstUnit())); | |
73 header.add(msgUnit(SInfoResultType.discharge.getCsvHeader(), SInfoResultType.discharge.getUnit())); | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
74 |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
75 final String descriptionHeader = results.getDescriptionHeader(); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
76 if (descriptionHeader != null) |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
77 header.add(msg(descriptionHeader)); |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
78 |
8948 | 79 header.add(msg(SInfoResultType.gaugeLabel.getCsvHeader())); |
80 header.add(msg(SInfoResultType.location.getCsvHeader())); | |
8928 | 81 |
82 writer.writeNext(header.toArray(new String[header.size()])); | |
83 } | |
84 | |
85 @Override | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
86 protected void writeCSVResultMetadata(final CSVWriter writer, final TkhCalculationResults results, final TkhCalculationResult result) { |
8928 | 87 |
88 final WstInfo wst = result.getWst(); | |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
89 super.writeCSVWaterlevelMetadata(writer, wst); |
9105
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
90 |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
91 // REAMRK: |
8946
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
92 // "# W/Pegel [cm]: " (nur bei Eingabe des Wasserstands am Pegel) |
5d5d482da3e9
Implementing SINFO - FlowDepthMinMax calculation
gernotbelger
parents:
8938
diff
changeset
|
93 // "# Q (m³/s): " (nur bei Eingabe des Durchflusses) |
9105
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
94 // WaterlevelEXporter does this |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
95 // final WaterlevelDescriptionBuilder descBuilder = new WaterlevelDescriptionBuilder((D4EArtifact) this.master, |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
96 // this.context); |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
97 // final String metadata = descBuilder.getMetadata(); |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
98 // BUT: |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
99 // - the WINFO results do not contain this info per wst |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
100 // - the WaterlevelExporter prints this in the global header for all waterlevels, simply based on the input fields |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
101 // - instead we would want tis information per waterlevel |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
102 // - the metadata 'Bezeichnung WST' contains exactly the wanted data |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
103 |
ae3565385e6a
Some finetuning an Tkh calculation, preparation for wsp calculation with bed height stations only
gernotbelger
parents:
9006
diff
changeset
|
104 writer.writeNext(new String[] { "" }); // break line |
8928 | 105 } |
106 | |
107 /** | |
108 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
109 * | |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
110 * @param results |
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
111 * |
8928 | 112 * @param useTkh |
113 */ | |
8996 | 114 @Override |
115 protected String[] formatRow(final TkhCalculationResults results, final ResultRow row, final ExportMode mode) { | |
8928 | 116 |
117 final Collection<String> lines = new ArrayList<>(11); | |
118 | |
8996 | 119 lines.add(row.exportValue(this.context, GeneralResultType.station)); |
8948 | 120 lines.add(row.exportValue(this.context, SInfoResultType.tkh)); |
121 lines.add(row.exportValue(this.context, SInfoResultType.soilkind)); | |
122 lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight)); | |
123 lines.add(row.exportValue(this.context, SInfoResultType.waterlevel)); | |
124 lines.add(row.exportValue(this.context, SInfoResultType.discharge)); | |
8928 | 125 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
126 // 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
|
127 if (results.getDescriptionHeader() != null || mode == ExportMode.pdf) |
8948 | 128 lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel)); |
8928 | 129 |
8948 | 130 lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel)); |
131 lines.add(row.exportValue(this.context, SInfoResultType.location)); | |
8928 | 132 |
133 return lines.toArray(new String[lines.size()]); | |
134 } | |
135 | |
136 @Override | |
137 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final TkhCalculationResults results) { | |
138 | |
139 /* general metadata */ | |
8996 | 140 super.addJRMetaData(source, results); |
8928 | 141 |
8996 | 142 source.addMetaData("calculation_label", msg("sinfo.export.flow_depth.pdf.meta.calculation.label")); |
143 source.addMetaData("calculation_name", msg("sinfo.export.flow_depth.pdf.meta.calculation.name")); | |
144 | |
8928 | 145 /* column headings */ |
8996 | 146 source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta())); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
147 source.addMetaData("tkh_header", SInfoResultType.tkh.getPdfHeader(this.context.getMeta())); |
8984 | 148 source.addMetaData("tkhkind_header", SInfoResultType.soilkind.getPdfHeader(this.context.getMeta())); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
149 source.addMetaData("bedheight_header", SInfoResultType.meanBedHeight.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
150 source.addMetaData("waterlevel_header", SInfoResultType.waterlevel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
151 source.addMetaData("discharge_header", SInfoResultType.discharge.getPdfHeader(this.context.getMeta())); |
8996 | 152 |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
153 // 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
|
154 // 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
|
155 final String descriptionHeader = results.getDescriptionHeader(); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
156 final String waterlevelNameHeader = descriptionHeader == null ? SInfoResultType.waterlevelLabel.getPdfHeader(this.context.getMeta()) |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
157 : descriptionHeader; |
8938
9c02733a1b3c
Work on Sinfo-tkh - exports; using same logic for wst-description as winfo
gernotbelger
parents:
8928
diff
changeset
|
158 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
|
159 |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
160 source.addMetaData("gauge_header", SInfoResultType.gaugeLabel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
161 source.addMetaData("location_header", SInfoResultType.location.getPdfHeader(this.context.getMeta())); |
8928 | 162 } |
163 | |
9006 | 164 @Override |
165 protected void writePDF(final OutputStream out) { | |
166 try { | |
167 final MetaAndTableJRDataSource source = createJRData(this.data); | |
168 | |
169 final JasperReporter reporter = new JasperReporter(); | |
170 reporter.addReport(JASPER_FILE, source); | |
171 reporter.exportPDF(out); | |
172 } | |
173 catch (final JRException je) { | |
174 getLog().warn("Error generating PDF Report!", je); | |
175 } | |
176 } | |
8928 | 177 } |