changeset 9459:f06e3766997f

fallunterscheidung isQ - fixWaterlevelExporter
author gernotbelger
date Tue, 28 Aug 2018 19:21:44 +0200
parents 7369d6ae3f87
children 8d6e56e57c4a
files artifacts/src/main/java/org/dive4elements/river/exports/FixWaterlevelExporter.java artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java
diffstat 2 files changed, 87 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/FixWaterlevelExporter.java	Tue Aug 28 17:05:25 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/FixWaterlevelExporter.java	Tue Aug 28 19:21:44 2018 +0200
@@ -28,6 +28,7 @@
 import org.dive4elements.river.artifacts.model.WQKms;
 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils;
 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
+import org.dive4elements.river.model.DischargeTable;
 import org.dive4elements.river.model.Gauge;
 import org.dive4elements.river.utils.Formatter;
 import org.dive4elements.river.utils.RiverUtils;
@@ -51,33 +52,43 @@
 
     @Override
     protected void writeRow4(final CSVWriter writer, final double wqkm[], final D4EArtifact flys, final Gauge gauge) {
-        final NumberFormat kmf = getKmFormatter();
-        final NumberFormat wf = getWFormatter();
-        final NumberFormat qf = getQFormatter();
+        if (!isQ()) {
+            super.writeRow4(writer, wqkm, flys, gauge);
+        } else {
+            final NumberFormat kmf = getKmFormatter();
+            final NumberFormat wf = getWFormatter();
+            final NumberFormat qf = getQFormatter();
 
-        final String waterlevel = getWaterlevel(wqkm[1], gauge);
+            final String waterlevel = getWaterlevel(wqkm[1], gauge);
 
-        writer.writeNext(new String[] { kmf.format(wqkm[2]), wf.format(wqkm[0]), waterlevel, qf.format(RiverUtils.roundQ(wqkm[1])),
-                RiverUtils.getLocationDescription(flys, wqkm[2]) });
+            writer.writeNext(new String[] { kmf.format(wqkm[2]), wf.format(wqkm[0]), waterlevel, qf.format(RiverUtils.roundQ(wqkm[1])),
+                    RiverUtils.getLocationDescription(flys, wqkm[2]) });
+        }
     }
 
     /** Write an csv-row at gauge location. */
     @Override
     protected void writeRow6(final CSVWriter writer, final double wqkm[], final String wOrQDesc, final D4EArtifact flys, final Gauge gauge) {
-        final NumberFormat kmf = getKmFormatter();
-        final NumberFormat wf = getWFormatter();
-        final NumberFormat qf = getQFormatter();
+        if (!isQ()) {
+            super.writeRow6(writer, wqkm, wOrQDesc, flys, gauge);
+        } else {
+            final NumberFormat kmf = getKmFormatter();
+            final NumberFormat wf = getWFormatter();
+            final NumberFormat qf = getQFormatter();
 
-        final String waterlevel = getWaterlevel(wqkm[1], gauge);
+            final String waterlevel = getWaterlevel(wqkm[1], gauge);
 
-        writer.writeNext(new String[] { kmf.format(wqkm[2]), wf.format(wqkm[0]), waterlevel, qf.format(RiverUtils.roundQ(wqkm[1])), wOrQDesc,
-                RiverUtils.getLocationDescription(flys, wqkm[2]), gauge.getName() });
+            writer.writeNext(new String[] { kmf.format(wqkm[2]), wf.format(wqkm[0]), waterlevel, qf.format(RiverUtils.roundQ(wqkm[1])), wOrQDesc,
+                    RiverUtils.getLocationDescription(flys, wqkm[2]), gauge.getName() });
+        }
     }
 
     private String getWaterlevel(final double discharge, final Gauge gauge) {
         final NumberFormat formatter = Formatter.getWaterlevelW(this.context);
         final Double waterlevel = this.getWforGaugeAndQ(gauge, discharge);
-        return formatter.format(waterlevel);
+        if (waterlevel != null)
+            return formatter.format(waterlevel);
+        return "";
     }
 
     private Double getWforGaugeAndQ(final Gauge gauge, final double q) {
@@ -85,8 +96,14 @@
         final String key = gauge.getName() + String.valueOf(q);
         if (!this.gaugeQ_W_Map.containsKey(key)) {
 
-            final DischargeTables dct = new DischargeTables(gauge.getRiver().getName(), gauge.getName());
-            final double[] qs = DischargeTables.getWsForQ(dct.getFirstTable(), q); // TODO: KLÄREN, welche Abflusstabelle genommen werden soll!
+            final DischargeTable dt = gauge.fetchMasterDischargeTable();
+            final double[][] table = DischargeTables.loadDischargeTableValues(dt);
+
+            final double[] qs = DischargeTables.getWsForQ(table, q);
+
+            // final DischargeTables dct = new DischargeTables(gauge.getRiver().getName(), gauge.getName());
+            // final double[] qs = DischargeTables.getWsForQ(dct.getFirstTable(), q); // TODO: KLÄREN, welche Abflusstabelle
+            // genommen werden soll!
             if (qs != null && qs.length > 0) {
                 this.gaugeQ_W_Map.put(key, qs[0]);
             }
@@ -101,22 +118,26 @@
 
     @Override
     protected void writeCSVHeader(final CSVWriter writer, final boolean atGauge, final boolean isQ) {
-        log.info("WaterlevelExporter.writeCSVHeader");
-
-        final String unit = RiverUtils.getRiver((D4EArtifact) this.master).getWstUnit().getName();
-
-        final String headerWamPegelNeu = msg("fix.export.csv.w_at_gauge");
+        if (!isQ()) {
+            super.writeCSVHeader(writer, atGauge, isQ);
+        } else {
+            log.info("WaterlevelExporter.writeCSVHeader");
 
-        if (atGauge) {
-            writer.writeNext(new String[] { msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER), msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { unit }),
-                    headerWamPegelNeu, msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER),
+            final String unit = RiverUtils.getRiver((D4EArtifact) this.master).getWstUnit().getName();
 
-                    // FIXME: use WaterlevelDescriptionBuilder instead and also remove all this duplicate code.
-                    (isQ ? msg(CSV_Q_DESC_HEADER, DEFAULT_CSV_Q_DESC_HEADER) : msg(CSV_W_DESC_HEADER, DEFAULT_CSV_W_DESC_HEADER)),
-                    msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER), msg(CSV_GAUGE_HEADER, DEFAULT_CSV_GAUGE_HEADER) });
-        } else {
-            writer.writeNext(new String[] { msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER), msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { unit }),
-                    headerWamPegelNeu, msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER), msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER) });
+            final String headerWamPegelNeu = msg("fix.export.csv.w_at_gauge");
+
+            if (atGauge) {
+                writer.writeNext(new String[] { msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER), msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { unit }),
+                        headerWamPegelNeu, msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER),
+
+                        // FIXME: use WaterlevelDescriptionBuilder instead and also remove all this duplicate code.
+                        (isQ ? msg(CSV_Q_DESC_HEADER, DEFAULT_CSV_Q_DESC_HEADER) : msg(CSV_W_DESC_HEADER, DEFAULT_CSV_W_DESC_HEADER)),
+                        msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER), msg(CSV_GAUGE_HEADER, DEFAULT_CSV_GAUGE_HEADER) });
+            } else {
+                writer.writeNext(new String[] { msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER), msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER, new Object[] { unit }),
+                        headerWamPegelNeu, msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER), msg(CSV_LOCATION_HEADER, DEFAULT_CSV_LOCATION_HEADER) });
+            }
         }
     }
 
@@ -125,22 +146,27 @@
 
         log.debug("write PDF");
 
-        final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource();
-        final String jasperFile = "/jasper/templates/fix_waterlevel.jrxml"; // "/jasper/fix_waterlevel_en.jasper");
+        if (!isQ()) {
+            super.writePDF(out);
+        } else {
 
-        addMetaData(source);
-        try {
-            final List<String[]> sorted = getRows(); // Custom Result could be nice, too...
-            for (final String[] list : sorted) {
-                source.addData(list);
+            final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource();
+            final String jasperFile = "/jasper/templates/fix_waterlevel.jrxml"; // "/jasper/fix_waterlevel_en.jasper");
+
+            addMetaData(source);
+            try {
+                final List<String[]> sorted = getRows(); // Custom Result could be nice, too...
+                for (final String[] list : sorted) {
+                    source.addData(list);
+                }
+
+                final JasperReporter reporter = new JasperReporter();
+                reporter.addReport(jasperFile, source);
+                reporter.exportPDF(out);
             }
-
-            final JasperReporter reporter = new JasperReporter();
-            reporter.addReport(jasperFile, source);
-            reporter.exportPDF(out);
-        }
-        catch (final JRException je) {
-            log.warn("Error generating PDF Report!", je);
+            catch (final JRException je) {
+                log.warn("Error generating PDF Report!", je);
+            }
         }
     }
 
@@ -208,7 +234,7 @@
         return list;
     }
 
-    protected List<String[]> getRows2(final WQKms wqkms, final boolean atGauge, final boolean isQ) {
+    private List<String[]> getRows2(final WQKms wqkms, final boolean atGauge, final boolean isQ) {
         log.debug("WaterlevelExporter.addWKmsData"); // OLD CODE :-/
 
         final List<String[]> list = new ArrayList<>();
--- a/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java	Tue Aug 28 17:05:25 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java	Tue Aug 28 19:21:44 2018 +0200
@@ -32,6 +32,7 @@
 import org.dive4elements.river.artifacts.access.FixRealizingAccess;
 import org.dive4elements.river.artifacts.access.IsOfficialAccess;
 import org.dive4elements.river.artifacts.access.RangeAccess;
+import org.dive4elements.river.artifacts.bundu.BUNDUArtifact;
 import org.dive4elements.river.artifacts.model.CalculationResult;
 import org.dive4elements.river.artifacts.model.ConstantWQKms;
 import org.dive4elements.river.artifacts.model.Segment;
@@ -75,6 +76,8 @@
 
     public static final String CSV_Q_HEADER = "common.export.csv.header.q";
 
+    private Boolean IS_Q = null;
+
     /**
      * @deprecated Use {@link WaterlevelDescriptionBuilder} instead.
      */
@@ -308,15 +311,23 @@
         }
     }
 
+    protected final boolean isQ() {
+        if (this.IS_Q == null) {
+            final WQ_MODE mode = RiverUtils.getWQMode((D4EArtifact) this.master);
+            final boolean isQ = mode == WQ_MODE.QGAUGE || mode == WQ_MODE.QFREE;
+            this.IS_Q = isQ;
+        }
+        return this.IS_Q != null ? this.IS_Q : true;
+    }
+
     @Override
     public void writeCSVData(final CSVWriter writer) {
         log.info("WaterlevelExporter.writeData");
-
         final WQ_MODE mode = RiverUtils.getWQMode((D4EArtifact) this.master);
         final boolean atGauge = mode == WQ_MODE.QGAUGE || mode == WQ_MODE.WGAUGE;
-        final boolean isQ = mode == WQ_MODE.QGAUGE || mode == WQ_MODE.QFREE;
+
         final RiverUtils.WQ_INPUT input = RiverUtils.getWQInputMode((D4EArtifact) this.master);
-
+        final boolean isQ = isQ();
         writeCSVMeta(writer);
         writeCSVHeader(writer, atGauge, isQ);
 
@@ -591,7 +602,8 @@
 
         final Gauge gauge = rangeAccess.getRiver().determineRefGauge(rangeAccess.getKmRange(), rangeAccess.isRange());
 
-        final String gaugeName = gauge.getName();
+        final String gaugeName = gauge.getName(); // RANDOMLY CHRASHES HERE, gauge is randomly null (especially on heavy load) - on production system as well
+                                                  // (it seems to)
         String desc = "";
         final String notinrange = msg(CSV_NOT_IN_GAUGE_RANGE, DEFAULT_CSV_NOT_IN_GAUGE_RANGE);
         List<Segment> segments = null;
@@ -603,7 +615,7 @@
 
         desc = getDesc(wqkms, isQ);
 
-        if (flys instanceof FixationArtifact) {
+        if (flys instanceof FixationArtifact || flys instanceof BUNDUArtifact) {
             // Get W/Q input per gauge for this case.
             final FixRealizingAccess fixAccess = new FixRealizingAccess(flys);
             segments = fixAccess.getSegments();

http://dive4elements.wald.intevation.org