changeset 8134:dfcc96deebd8

Make SedimentloadExporter work with the new result scheme. Pretty much untested.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 25 Aug 2014 20:02:28 +0200
parents 45b1d71110c3
children 05aca5659f32
files artifacts/src/main/java/org/dive4elements/river/exports/minfo/SedimentLoadExporter.java
diffstat 1 files changed, 82 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/minfo/SedimentLoadExporter.java	Mon Aug 25 20:01:44 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/minfo/SedimentLoadExporter.java	Mon Aug 25 20:02:28 2014 +0200
@@ -10,7 +10,7 @@
 
 import java.util.LinkedList;
 import java.util.List;
-import java.util.TreeSet;
+import java.util.TreeMap;
 
 import java.io.OutputStream;
 import java.io.IOException;
@@ -21,9 +21,8 @@
 
 import org.dive4elements.river.artifacts.access.SedimentLoadAccess;
 import org.dive4elements.river.artifacts.model.CalculationResult;
-import org.dive4elements.river.artifacts.model.minfo.SedimentLoadLSData;
-import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFraction;
-import org.dive4elements.river.artifacts.model.minfo.SedimentLoadResult;
+import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult.Fraction;
+import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataResult;
 
 import org.dive4elements.river.artifacts.D4EArtifact;
 
@@ -71,29 +70,72 @@
     public static final String CSV_TOTAL =
         "export.sedimentload_ls.csv.header.total";
 
+    /* Header is: suspended_sediment, susp_sand, susp_sand_bed, sand,
+     * fine_middle, coarse, total */
+    private static final String[] FRACTION_ORDER = {
+        "suspended_sediment",
+        "susp_sand",
+        "susp_sand_bed",
+        "sand",
+        "fine_middle",
+        "coarse",
+        "total"
+    };
 
     /** Collected results. */
-    private SedimentLoadResult[] results;
+    private SedimentLoadDataResult result;
 
     /** Empty constructor. */
     public SedimentLoadExporter() {
-        results = new SedimentLoadResult[0];
     }
 
     /** Process all stored data and write csv. */
     @Override
     protected void writeCSVData(CSVWriter writer) throws IOException {
+        if (result == null) {
+            return;
+        }
         writeCSVHeader(writer);
 
-        for (SedimentLoadResult result: results) {
-            String years = (result.getEndYear() == 0)
-                    ? result.getStartYear() + " "
-                    : result.getStartYear() + "-" + result.getEndYear();
-            SedimentLoadLSData load = result.getLoad();
-            // Put load.getName()+load.getDescription()}); somewhere?
-            for (double km: new TreeSet<Double>(load.getKms())) {
-                SedimentLoadFraction fraction = load.getFraction(km);
-                writeRecord(writer, km, years, fraction);
+        /* Prepare the values. The order of the fractions is given by the
+         * header and thus static. */
+
+        /* The result is ordered by the peridods. For each period there is
+         * then a map of km - fractions pairs which are the actual result. */
+
+        TreeMap <String, TreeMap <Double, Double[]>> result_map =
+            new TreeMap<String, TreeMap<Double, Double[]>>();
+        int i = 0;
+        for (String name: FRACTION_ORDER) {
+            for (Fraction fract: result.getFractionsByName(name)) {
+                String period = fract.getPeriod();
+                TreeMap<Double, Double[]> cur_map;
+                if (result_map.containsKey(period)) {
+                    cur_map = result_map.get(period);
+                } else {
+                    cur_map = new TreeMap<Double, Double[]>();
+                    result_map.put(period, cur_map);
+                }
+                double[][] values = fract.getData();
+                for (int j = 0; j < values[0].length; j++) {
+                    Double km = values[0][j];
+                    Double val = values[1][j];
+                    Double[] old = cur_map.get(km);
+                    if (old == null) {
+                        old = new Double[FRACTION_ORDER.length];
+                        for (int k = 0; k < old.length; k++) {
+                            old [k] = Double.NaN;
+                        }
+                    }
+                    old [i] = val;
+                    cur_map.put(km, old);
+                }
+            }
+        }
+        for (String period: result_map.keySet()) {
+            TreeMap<Double, Double[]> cur_map = result_map.get(period);
+            for (Double km: cur_map.keySet()) {
+                writeRecord(writer, km, period, cur_map.get(km));
             }
         }
     }
@@ -120,7 +162,7 @@
         CSVWriter writer,
         double km,
         String years,
-        SedimentLoadFraction fraction
+        Double[] fractions
     ) {
         // year, total, susp sed, susp sandbed suspsand, sand, finemiddle, coarse
         NumberFormat kmf = Formatter.getCalculationKm(context.getMeta());
@@ -128,13 +170,13 @@
         writer.writeNext(new String[] {
             kmf.format(km),
             years,
-            numberToString(valf, fraction.getSuspSediment()),
-            numberToString(valf, fraction.getSuspSand()),
-            numberToString(valf, fraction.getSuspSandBed()),
-            numberToString(valf, fraction.getSand()),
-            numberToString(valf, fraction.getFineMiddle()),
-            numberToString(valf, fraction.getCoarse()),
-            nonZeroToString(valf, fraction.getTotal())
+            numberToString(valf,  fractions[0]),
+            numberToString(valf,  fractions[1]),
+            numberToString(valf,  fractions[2]),
+            numberToString(valf,  fractions[3]),
+            numberToString(valf,  fractions[4]),
+            numberToString(valf,  fractions[5]),
+            nonZeroToString(valf, fractions[6])
         });
     }
 
@@ -143,23 +185,21 @@
         logger.debug("writeCSVHeader()");
 
         List<String> header = new LinkedList<String>();
-        if (results != null) {
-            SedimentLoadAccess access =
-                new SedimentLoadAccess((D4EArtifact) master);
-
-            String unit = " [" + msg("state.minfo." +
-                    access.getUnit(), "translation missing") + "]";
+        SedimentLoadAccess access =
+            new SedimentLoadAccess((D4EArtifact) master);
 
-            header.add(msg(CSV_KM,          "km"));
-            header.add(msg(CSV_YEAR,        "Jahr"));
-            header.add(msg(CSV_SUSP_SEDIMENT, "Schwebst.") + unit);
-            header.add(msg(CSV_SUSP_SAND,   "Susp.Sand") + unit);
-            header.add(msg(CSV_SUSP_SAND_BB, "Susp.Sand(BB)") + unit);
-            header.add(msg(CSV_SAND,        "Sand") + unit);
-            header.add(msg(CSV_FINEMIDDLE,  "Kies(f+m)") + unit);
-            header.add(msg(CSV_COARSE,      "Kies(g)") + unit);
-            header.add(msg(CSV_TOTAL,       "Gesamt") + unit);
-        }
+        String unit = " [" + msg("state.minfo." +
+                access.getUnit(), "translation missing") + "]";
+
+        header.add(msg(CSV_KM,          "km"));
+        header.add(msg(CSV_YEAR,        "Jahr"));
+        header.add(msg(CSV_SUSP_SEDIMENT, "Schwebst.") + unit);
+        header.add(msg(CSV_SUSP_SAND,   "Susp.Sand") + unit);
+        header.add(msg(CSV_SUSP_SAND_BB, "Susp.Sand(BB)") + unit);
+        header.add(msg(CSV_SAND,        "Sand") + unit);
+        header.add(msg(CSV_FINEMIDDLE,  "Kies(f+m)") + unit);
+        header.add(msg(CSV_COARSE,      "Kies(g)") + unit);
+        header.add(msg(CSV_TOTAL,       "Gesamt") + unit);
         writer.writeNext(header.toArray(new String[header.size()]));
     }
 
@@ -171,14 +211,14 @@
             logger.warn("Invalid data type.");
             return;
         }
-        Object[] d = (Object[])((CalculationResult)data).getData();
+        Object d = ((CalculationResult)data).getData();
 
-        if (!(d instanceof SedimentLoadResult[])) {
+        if (!(d instanceof SedimentLoadDataResult)) {
             logger.warn("Invalid result object.");
             return;
         }
         logger.debug("addData: Data added.");
-        results = (SedimentLoadResult[])d;
+        result = (SedimentLoadDataResult)d;
     }
 
     /** Write PDF to outputstream (not implemented yet). */

http://dive4elements.wald.intevation.org