Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveExporter.java @ 6305:40c77429319d
Update PDF/CSV export for discharge curves
- Unit is now dynamic, also adds gauge and datum
- Chose output unit depending on weather we are at a gauge or not
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Wed, 12 Jun 2013 16:10:56 +0200 |
parents | a0078e5e3b39 |
children | cb045ccba9b2 |
comparison
equal
deleted
inserted
replaced
6304:152e1ba8e5bd | 6305:40c77429319d |
---|---|
38 import org.dive4elements.river.artifacts.D4EArtifact; | 38 import org.dive4elements.river.artifacts.D4EArtifact; |
39 | 39 |
40 import org.dive4elements.river.artifacts.access.RangeAccess; | 40 import org.dive4elements.river.artifacts.access.RangeAccess; |
41 import org.dive4elements.river.artifacts.model.CalculationResult; | 41 import org.dive4elements.river.artifacts.model.CalculationResult; |
42 import org.dive4elements.river.artifacts.model.GaugesFactory; | 42 import org.dive4elements.river.artifacts.model.GaugesFactory; |
43 import org.dive4elements.river.artifacts.model.WQ; | |
43 import org.dive4elements.river.artifacts.model.WQKms; | 44 import org.dive4elements.river.artifacts.model.WQKms; |
44 import org.dive4elements.river.artifacts.model.WKmsJRDataSource; | 45 import org.dive4elements.river.artifacts.model.WKmsJRDataSource; |
45 import org.dive4elements.river.artifacts.resources.Resources; | 46 import org.dive4elements.river.artifacts.resources.Resources; |
46 | 47 |
47 import org.dive4elements.river.model.Gauge; | 48 import org.dive4elements.river.model.Gauge; |
49 import org.dive4elements.river.model.River; | |
50 | |
48 import org.dive4elements.river.utils.RiverUtils; | 51 import org.dive4elements.river.utils.RiverUtils; |
49 import org.dive4elements.river.utils.Formatter; | 52 import org.dive4elements.river.utils.Formatter; |
50 | 53 |
51 | 54 |
52 /** | 55 /** |
66 | 69 |
67 public static final String DEFAULT_CSV_W_HEADER = "W [NN + m]"; | 70 public static final String DEFAULT_CSV_W_HEADER = "W [NN + m]"; |
68 public static final String DEFAULT_CSV_Q_HEADER = "Q [m\u00b3/s]"; | 71 public static final String DEFAULT_CSV_Q_HEADER = "Q [m\u00b3/s]"; |
69 | 72 |
70 public static final String PDF_HEADER_MODE = "export.computed.discharge.pdf.mode"; | 73 public static final String PDF_HEADER_MODE = "export.computed.discharge.pdf.mode"; |
74 public static final String PDF_HEADER_CALC_MODE = "export.computed.discharge.pdf.calc.mode"; | |
71 public static final String JASPER_FILE = "export.computed.discharge.pdf.file"; | 75 public static final String JASPER_FILE = "export.computed.discharge.pdf.file"; |
72 | 76 |
73 protected List<WQKms> data; | 77 protected List<WQKms> data; |
78 | |
79 protected String wUnit; | |
80 protected String riverUnit; | |
81 protected String gaugeName; | |
82 protected double gaugeDatum; | |
83 protected boolean isCalculated; | |
74 | 84 |
75 | 85 |
76 public void init(Document request, OutputStream out, CallContext context) { | 86 public void init(Document request, OutputStream out, CallContext context) { |
77 logger.debug("ComputedDischargeCurveExporter.init"); | 87 logger.debug("ComputedDischargeCurveExporter.init"); |
78 | 88 |
85 @Override | 95 @Override |
86 protected void addData(Object d) { | 96 protected void addData(Object d) { |
87 if (d instanceof CalculationResult) { | 97 if (d instanceof CalculationResult) { |
88 d = ((CalculationResult)d).getData(); | 98 d = ((CalculationResult)d).getData(); |
89 } | 99 } |
100 WQKms referenceWQ = null; // used for gauge / unit observations | |
90 if (d instanceof WQKms[]){ | 101 if (d instanceof WQKms[]){ |
91 data.addAll(Arrays.asList((WQKms [])d)); | 102 data.addAll(Arrays.asList((WQKms [])d)); |
92 return; | 103 // If there is a unit mix in this list |
104 // we are screwed anyway. | |
105 referenceWQ = ((WQKms[])d)[0]; | |
93 } | 106 } |
94 else if (d instanceof WQKms) { | 107 else if (d instanceof WQKms) { |
95 data.add((WQKms)d); | 108 data.add((WQKms)d); |
109 referenceWQ = (WQKms)d; | |
110 } else { | |
111 logger.warn("Can't add data for export. Unkown data type " + | |
112 d.getClass().getName()); | |
96 return; | 113 return; |
97 } | 114 } |
98 logger.warn("Can't add data for csv export. Unkown data type " + | 115 logger.debug("Andre 1"); |
99 d.getClass().getName()); | 116 if (referenceWQ != null) { |
100 } | 117 wUnit = referenceWQ.getWUnit(); |
101 | 118 D4EArtifact arti = (D4EArtifact)master; |
119 River river = RiverUtils.getRiver(arti); | |
120 riverUnit = river.getWstUnit().getName(); | |
121 RangeAccess rangeAccess = new RangeAccess(arti); | |
122 | |
123 double[] kms = rangeAccess.getKmRange(); | |
124 | |
125 Gauge gauge = river.determineGaugeByPosition(kms[0]); | |
126 logger.debug("Kms 0: " + kms[0]); | |
127 if (Math.abs(kms[0] - gauge.getStation().doubleValue()) < 1e-4) { | |
128 logger.debug("At Gauge"); | |
129 gaugeName = gauge.getName(); | |
130 gaugeDatum = gauge.getDatum().doubleValue(); | |
131 | |
132 // Assumption is that values at a gauge that are in the meter | |
133 // reference system are calculated. | |
134 isCalculated = wUnit.equals("m"); | |
135 | |
136 // Now convert the data to cm because we are at gauge | |
137 List<WQKms> newData = new ArrayList<WQKms>(); | |
138 for (WQKms d2: data) { | |
139 newData.add(new WQKms(d2.getKms(), | |
140 WQ.getFixedWQforExportAtGauge((WQ)d2, gauge.getDatum()))); | |
141 } | |
142 data = newData; // All hail the garbage collector | |
143 wUnit = "cm"; | |
144 logger.debug("Setting gauge / datum to: " + gaugeName + " / " + gaugeDatum); | |
145 } else { | |
146 gaugeName = ""; | |
147 gaugeDatum = Double.NaN; | |
148 } | |
149 } | |
150 } | |
102 | 151 |
103 protected void writeCSVData(CSVWriter writer) { | 152 protected void writeCSVData(CSVWriter writer) { |
104 logger.info("ComputedDischargeCurveExporter.writeData"); | 153 logger.info("ComputedDischargeCurveExporter.writeData"); |
105 | 154 |
106 writeCSVHeader(writer); | 155 writeCSVHeader(writer); |
126 | 175 |
127 | 176 |
128 protected void writeCSVHeader(CSVWriter writer) { | 177 protected void writeCSVHeader(CSVWriter writer) { |
129 logger.debug("ComputedDischargeCurveExporter.writeCSVHeader"); | 178 logger.debug("ComputedDischargeCurveExporter.writeCSVHeader"); |
130 | 179 |
131 String unit = RiverUtils.getRiver((D4EArtifact) master).getWstUnit().getName(); | |
132 RangeAccess access = new RangeAccess((D4EArtifact)master); | 180 RangeAccess access = new RangeAccess((D4EArtifact)master); |
133 double[] km = access.getLocations(); | 181 double[] km = access.getLocations(); |
134 String header = | 182 String header = |
135 msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { unit }); | 183 msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { wUnit }); |
136 | 184 |
137 if (km != null) { | |
138 List<Gauge>gauges = GaugesFactory.getGauges(access.getRiver()); | |
139 for (Gauge g: gauges) { | |
140 if (Math.abs(g.getStation().doubleValue() - km[0]) < 0.0001d) { | |
141 header = "W [cm]"; | |
142 break; | |
143 } | |
144 } | |
145 } | |
146 writer.writeNext(new String[] { | 185 writer.writeNext(new String[] { |
147 header, | 186 header, |
148 msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER) | 187 msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER) |
149 }); | 188 }); |
150 } | 189 } |
154 * Returns the number formatter for W values. | 193 * Returns the number formatter for W values. |
155 * | 194 * |
156 * @return the number formatter for W values. | 195 * @return the number formatter for W values. |
157 */ | 196 */ |
158 protected NumberFormat getWFormatter() { | 197 protected NumberFormat getWFormatter() { |
198 if (wUnit.equals("cm")) { | |
199 return Formatter.getFormatter(context, 0, 0); | |
200 } | |
159 return Formatter.getComputedDischargeW(context); | 201 return Formatter.getComputedDischargeW(context); |
160 } | 202 } |
161 | 203 |
162 | 204 |
163 /** | 205 /** |
208 | 250 |
209 protected void addMetaData(WKmsJRDataSource source) { | 251 protected void addMetaData(WKmsJRDataSource source) { |
210 CallMeta meta = context.getMeta(); | 252 CallMeta meta = context.getMeta(); |
211 | 253 |
212 D4EArtifact flys = (D4EArtifact) master; | 254 D4EArtifact flys = (D4EArtifact) master; |
255 source.addMetaData("gauge", gaugeName); | |
256 if (!Double.isNaN(gaugeDatum)) { | |
257 NumberFormat mf = Formatter.getMeterFormat(context); | |
258 source.addMetaData("datum", mf.format(gaugeDatum) + " " + riverUnit); | |
259 } else { | |
260 source.addMetaData("datum", ""); | |
261 } | |
213 | 262 |
214 source.addMetaData ("river", RiverUtils.getRivername(flys)); | 263 source.addMetaData ("river", RiverUtils.getRivername(flys)); |
215 | 264 |
216 Locale locale = Resources.getLocale(meta); | 265 Locale locale = Resources.getLocale(meta); |
217 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); | 266 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
218 | 267 |
219 source.addMetaData("date", df.format(new Date())); | 268 source.addMetaData("date", df.format(new Date())); |
269 | |
270 source.addMetaData("wUnit", wUnit.equals("m") ? riverUnit : "cm"); | |
220 | 271 |
221 RangeAccess rangeAccess = new RangeAccess(flys); | 272 RangeAccess rangeAccess = new RangeAccess(flys); |
222 double[] kms = rangeAccess.getKmRange(); | 273 double[] kms = rangeAccess.getKmRange(); |
223 source.addMetaData("range", String.valueOf(kms[0])); | 274 source.addMetaData("range", String.valueOf(kms[0])); |
224 | 275 |
225 source.addMetaData("calculation", Resources.getMsg( | 276 if (isCalculated) { |
226 locale, | 277 source.addMetaData("calculation", Resources.getMsg( |
227 PDF_HEADER_MODE, | 278 locale, |
228 "Computed Discharge")); | 279 PDF_HEADER_CALC_MODE, |
280 "Computed Discharge")); | |
281 } else { | |
282 source.addMetaData("calculation", Resources.getMsg( | |
283 locale, | |
284 PDF_HEADER_MODE, | |
285 "Discharge")); | |
286 } | |
229 } | 287 } |
230 | 288 |
231 protected void addWQData(WKmsJRDataSource source) { | 289 protected void addWQData(WKmsJRDataSource source) { |
232 NumberFormat wf = getWFormatter(); | 290 NumberFormat wf = getWFormatter(); |
233 NumberFormat qf = getQFormatter(); | 291 NumberFormat qf = getQFormatter(); |