changeset 6217:e213b538f78c

Added exporter for bed height differences.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 06 Jun 2013 16:41:27 +0200
parents 9fdb2b59b992
children 359adab7f27d
files artifacts/src/main/java/org/dive4elements/river/exports/minfo/BedDifferenceExporter.java
diffstat 1 files changed, 89 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/minfo/BedDifferenceExporter.java	Thu Jun 06 16:41:27 2013 +0200
@@ -0,0 +1,89 @@
+package org.dive4elements.river.exports.minfo;
+
+import gnu.trove.TDoubleArrayList;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.text.NumberFormat;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.river.artifacts.model.CalculationResult;
+import org.dive4elements.river.artifacts.model.minfo.BedDifferencesResult;
+import org.dive4elements.river.exports.AbstractExporter;
+import org.dive4elements.river.utils.Formatter;
+import org.w3c.dom.Document;
+
+import au.com.bytecode.opencsv.CSVWriter;
+
+
+public class BedDifferenceExporter
+extends AbstractExporter
+{
+
+    /** Private logger. */
+    private static Logger logger =
+        Logger.getLogger(BedDifferenceExporter.class);
+
+    private static final String CSV_HEADER_KM = "export.minfo.beddifference.km";
+    private static final String CSV_HEADER_DIFF =
+        "export.minfo.beddifference.diff";
+
+    private BedDifferencesResult[] results;
+
+    @Override
+    public void init(Document request, OutputStream out, CallContext context) {
+        logger.debug("BedQualityExporter.init");
+        super.init(request, out, context);
+        results = new BedDifferencesResult[0];
+    }
+    @Override
+    protected void writeCSVData(CSVWriter writer) throws IOException {
+        writeCSVHeader(writer);
+
+        NumberFormat kmf = Formatter.getCalculationKm(context.getMeta());
+        NumberFormat mf = Formatter.getMeterFormat(context);
+        for (int i = 0; i < results.length; i++) {
+            BedDifferencesResult result = results[i];
+            double[][] kms = result.getDifferencesData();
+            for (int j = 0; j < kms[0].length; j++) {
+                writer.writeNext(new String[] {
+                    kmf.format(kms[0][j]), mf.format(kms[1][j])});
+            }
+        }
+    }
+
+    @Override
+    protected void writePDF(OutputStream out) {
+        // TODO implement me!
+
+    }
+
+    @Override
+    protected void addData(Object data) {
+        if (!(data instanceof CalculationResult)) {
+            logger.warn("Invalid data type.");
+            return;
+        }
+        Object[] d = (Object[])((CalculationResult)data).getData();
+
+        if (!(d instanceof BedDifferencesResult[])) {
+            logger.warn("Invalid result object.");
+            return;
+        }
+        results = (BedDifferencesResult[])d;
+    }
+
+    protected void writeCSVHeader(CSVWriter writer) {
+        logger.debug("writeCSVHeader()");
+
+        List<String> header = new LinkedList<String>();
+        if (results != null)  {
+            header.add(msg(CSV_HEADER_KM, "km"));
+            header.add(msg(CSV_HEADER_DIFF, "m"));
+        }
+        writer.writeNext(header.toArray(new String[header.size()]));
+    }
+}

http://dive4elements.wald.intevation.org