andre@8581: /* Copyright (C) 2011, 2012, 2013, 2015 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.exports.minfo; ingo@3785: ingo@3785: import java.io.IOException; ingo@3785: import java.io.OutputStream; ingo@3785: import java.text.DateFormat; ingo@3785: import java.text.NumberFormat; ingo@3785: import java.util.Arrays; teichmann@7254: import java.util.ArrayList; ingo@3785: import java.util.List; ingo@3785: ingo@3785: import org.apache.log4j.Logger; ingo@3785: ingo@3785: import au.com.bytecode.opencsv.CSVWriter; teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.BedQualityResult; andre@8587: import org.dive4elements.river.artifacts.model.minfo.BedQualityResultValue; teichmann@5831: import org.dive4elements.river.exports.AbstractExporter; teichmann@5831: import org.dive4elements.river.utils.Formatter; ingo@3785: andre@8581: import org.dive4elements.river.artifacts.access.RangeAccess; andre@8581: import org.dive4elements.river.artifacts.D4EArtifact; andre@8581: ingo@3785: ingo@3785: public class BedQualityExporter ingo@3785: extends AbstractExporter ingo@3785: { teichmann@8202: /** Private log. */ teichmann@8202: private static Logger log = Logger.getLogger(BedQualityExporter.class); ingo@3785: felix@7610: private static final String CSV_HEADER_KM = felix@7610: "export.minfo.bedquality.km"; andre@8587: private static final String CSV_HEADER_BASE = andre@8587: "export.minfo.bedquality"; ingo@3785: ingo@3785: private BedQualityResult[] results; ingo@3785: teichmann@7077: public BedQualityExporter() { ingo@3785: results = new BedQualityResult[0]; ingo@3785: } ingo@3785: felix@7627: /** Create double[] containing the data for rows in csv. */ felix@7611: private List createDataRows() { andre@8581: andre@8581: double[] kms = new RangeAccess((D4EArtifact) master).getKmSteps(); felix@7625: ingo@3785: int cols = 1; andre@8587: for (BedQualityResult result: results) { andre@8653: for (BedQualityResultValue value :result.getValues()) { andre@8653: if (value.isInterpolateable()) { andre@8653: /* Only add results that can be interpolated */ andre@8653: cols++; andre@8653: } andre@8653: } ingo@3785: } andre@8640: if (cols == 1) { andre@8640: return new ArrayList(); andre@8640: } ingo@3785: andre@8581: List rows = new ArrayList(kms.length); andre@8581: for (double km: kms) { ingo@3785: double[] row = new double[cols]; ingo@3785: row[0] = km; andre@8595: int resultOffset = 1; andre@8587: for (BedQualityResult result: results) { andre@8595: int i = resultOffset; andre@8587: for (BedQualityResultValue value: result.getValues()) { andre@8653: if (value.isInterpolateable()) { andre@8653: row[i++] = value.getDataInterpolated(km); andre@8653: } ingo@3785: } andre@8595: resultOffset = i; ingo@3785: } ingo@3785: rows.add(row); ingo@3785: } felix@7611: felix@7611: return rows; felix@7611: } felix@7611: felix@7611: @Override felix@7611: protected void writeCSVData(CSVWriter writer) throws IOException { felix@7611: writeCSVHeader(writer); felix@7611: felix@7611: NumberFormat nf = Formatter.getFormatter(context, 1, 3); felix@7611: felix@7611: for (double[] d : createDataRows()) { teichmann@8202: log.debug("row + " + Arrays.toString(d)); teichmann@7254: List cells = new ArrayList(d.length); ingo@3785: for (int i = 0; i < d.length; i++) { ingo@3785: if (!Double.isNaN(d[i])) { ingo@3785: cells.add(nf.format(d[i])); ingo@3785: } ingo@3785: else { ingo@3785: cells.add(""); ingo@3785: } ingo@3785: } ingo@3785: writer.writeNext(cells.toArray(new String[cells.size()])); ingo@3785: } ingo@3785: } ingo@3785: ingo@3785: @Override ingo@3785: protected void writePDF(OutputStream out) { ingo@3785: // TODO Auto-generated method stub ingo@3785: ingo@3785: } ingo@3785: ingo@3785: @Override ingo@3785: protected void addData(Object data) { teichmann@8202: log.debug("addData()"); ingo@3785: if (!(data instanceof CalculationResult)) { teichmann@8202: log.warn("Invalid data type."); ingo@3785: return; ingo@3785: } ingo@3785: Object[] d = (Object[])((CalculationResult)data).getData(); ingo@3785: ingo@3785: if (!(d instanceof BedQualityResult[])) { teichmann@8202: log.warn("Invalid result object."); ingo@3785: return; ingo@3785: } ingo@3785: results = (BedQualityResult[])d; ingo@3785: } ingo@3785: ingo@3785: protected void writeCSVHeader(CSVWriter writer) { teichmann@8202: log.debug("writeCSVHeader()"); ingo@3785: teichmann@7254: List header = new ArrayList(); andre@8587: if (results == null) { andre@8587: writer.writeNext(header.toArray(new String[header.size()])); andre@8587: return; andre@8587: } andre@8587: andre@8587: header.add(msg(CSV_HEADER_KM, "km")); andre@8587: DateFormat df = Formatter.getDateFormatter(context.getMeta(), "dd.MM.yyyy"); andre@8587: for (BedQualityResult result: results) { andre@8587: String d1 = df.format(result.getDateRange().getFrom()); andre@8587: String d2 = df.format(result.getDateRange().getTo()); andre@8587: for (BedQualityResultValue value: result.getValues()) { andre@8653: if (!value.isInterpolateable()) { andre@8653: continue; andre@8653: } andre@8587: String i18n; andre@8587: if (value.isDiameterResult()) { andre@8587: i18n = CSV_HEADER_BASE + ".diameter." + value.getType(); andre@8587: header.add(msg(i18n, i18n) + andre@8656: " - " + value.getName().toUpperCase() + " - " + d1 + "-" + d2); andre@8587: } else { andre@8587: i18n = CSV_HEADER_BASE + "." + value.getName() + "." + value.getType(); andre@8587: header.add(msg(i18n, i18n) + " - " + d1 + "-" + d2); ingo@3785: } ingo@3785: } ingo@3785: } ingo@3785: writer.writeNext(header.toArray(new String[header.size()])); ingo@3785: } ingo@3785: }