Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/exports/WstWriter.java @ 9299:4a6cc7c6716a
uinfo.inundation_duration veg'zone select
author | gernotbelger |
---|---|
date | Wed, 25 Jul 2018 14:42:44 +0200 |
parents | e4606eae8ea5 |
children |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/WstWriter.java Wed Jul 25 11:33:48 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/exports/WstWriter.java Wed Jul 25 14:42:44 2018 +0200 @@ -12,7 +12,6 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; - import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -21,12 +20,10 @@ import java.util.Map; import java.util.TreeMap; -import org.apache.log4j.Logger; import org.apache.commons.lang.StringUtils; - +import org.apache.log4j.Logger; import org.dive4elements.river.artifacts.model.WstLine; - /** * A writer that creates WSTs. * @@ -49,7 +46,7 @@ private static Logger log = Logger.getLogger(WstWriter.class); /** The default unit that is written into the header of the WST. */ - public static final String DEFAULT_UNIT = "Wassserstand [NN + m]"; + public static final String DEFAULT_UNIT = "Wasserstand [NN + m]"; /** The lines that need to be included for the export. */ protected Map<Double, WstLine> lines; @@ -66,54 +63,58 @@ /** The last Q values. */ protected double[] qs; - /** Workaround for one use of wrongly imported files: ignore the Qs at - * all. */ + /** + * Workaround for one use of wrongly imported files: ignore the Qs at + * all. + */ protected boolean ignoreQs; /** * This constructor creates a new WstWriter with a number of Q columns. * - * @param columns The number of columns of the resulting WST. + * @param columns + * The number of columns of the resulting WST. */ - public WstWriter(int columns) { + public WstWriter(final int columns) { this(columns, false); } /** * This constructor creates a new WstWriter with a number of Q columns. * - * @param columns The number of columns of the resulting WST. - * @param workaroundIgnoreQs do not write QLines to shadow broken data. + * @param columns + * The number of columns of the resulting WST. + * @param workaroundIgnoreQs + * do not write QLines to shadow broken data. */ - public WstWriter(int columns, boolean workaroundIgnoreQs) { - this.cols = columns; - this.columnNames = new ArrayList<String>(cols); - this.lines = new HashMap<Double, WstLine>(); - this.qs = new double[cols]; - this.locale = Locale.US; - this.ignoreQs = workaroundIgnoreQs; + public WstWriter(final int columns, final boolean workaroundIgnoreQs) { + this.cols = columns; + this.columnNames = new ArrayList<>(this.cols); + this.lines = new HashMap<>(); + this.qs = new double[this.cols]; + this.locale = Locale.US; + this.ignoreQs = workaroundIgnoreQs; } - /** * This method is used to create the WST from the data that has been * inserted using add(double[]) before. - * @param out Where to write to. + * + * @param out + * Where to write to. */ - public void write(OutputStream out) { + public void write(final OutputStream out) { log.info("WstWriter.write"); - PrintWriter writer = new PrintWriter( - new BufferedWriter( - new OutputStreamWriter(out))); + final PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out))); - this.qs = new double[cols]; + this.qs = new double[this.cols]; writeHeader(writer); - Collection<WstLine> collection = new TreeMap(lines).values(); + final Collection<WstLine> collection = new TreeMap(this.lines).values(); - for (WstLine line: collection) { + for (final WstLine line : collection) { writeWLine(writer, line); } @@ -121,75 +122,74 @@ writer.close(); } - /** * This method is used to add a new line to the WST. * - * @param wqkms A 3dim double array with [W, Q, KM]. + * @param wqkms + * A 3dim double array with [W, Q, KM]. */ - public void add(double[] wqkms) { - Double km = wqkms[2]; + public void add(final double[] wqkms) { + final Double km = wqkms[2]; - WstLine line = lines.get(km); + WstLine line = this.lines.get(km); if (line == null) { line = new WstLine(km.doubleValue()); - lines.put(km, line); + this.lines.put(km, line); } line.add(wqkms[0], wqkms[1]); } + public void addCorrected(final double[] wqckms) { + final Double km = wqckms[2]; - public void addCorrected(double[] wqckms) { - Double km = wqckms[2]; - - WstLine line = lines.get(km); + WstLine line = this.lines.get(km); if (line == null) { line = new WstLine(km.doubleValue()); - lines.put(km, line); + this.lines.put(km, line); } line.add(wqckms[3], wqckms[1]); } - /** * Adds a further column name. * - * @param name The name of the new column. + * @param name + * The name of the new column. */ public void addColumn(String name) { if (name != null) { - cols++; + this.cols++; int i = 0; - String basename = name; - while (columnNames.contains(name)) { + final String basename = name; + while (this.columnNames.contains(name)) { name = basename + "_" + i++; } - columnNames.add(name); + this.columnNames.add(name); } } - /** * This method writes the header of the WST. * - * @param writer The PrintWriter that creates the output. + * @param writer + * The PrintWriter that creates the output. */ - protected void writeHeader(PrintWriter writer) { + protected void writeHeader(final PrintWriter writer) { log.debug("WstWriter.writeHeader"); - writer.println(cols); + writer.println(this.cols); writer.print("*!column-bez-text "); - List<String> quotedNames = new ArrayList<String>(columnNames.size()); + final List<String> quotedNames = new ArrayList<>(this.columnNames.size()); - for (String name: columnNames) { + for (String name : this.columnNames) { if (name.contains(" ")) { name = '"' + name + '"'; } @@ -198,12 +198,11 @@ writer.println(StringUtils.join(quotedNames, " ")); writer.print(" "); - for (String columnName: columnNames) { + for (final String columnName : this.columnNames) { if (columnName.length() > 9) { - writer.printf(locale, "%9s", - columnName.substring(columnName.length() - 9)); + writer.printf(this.locale, "%9s", columnName.substring(columnName.length() - 9)); } else { - writer.printf(locale, "%9s", columnName); + writer.printf(this.locale, "%9s", columnName); // This is weird but i was to lazy to lookup // how to do this another way. for (int i = 9 - columnName.length(); i > 0; i--) { @@ -219,62 +218,63 @@ writer.println(); } - /** * This method writes a line with W values and a certain kilometer. * - * @param writer The PrintWriter that is used to create the output. - * @param line The WstLine that should be written to the output. + * @param writer + * The PrintWriter that is used to create the output. + * @param line + * The WstLine that should be written to the output. */ - protected void writeWLine(PrintWriter writer, WstLine line) { - double km = line.getKm(); - double[] qs = line.getQs(); - int num = line.getSize(); + protected void writeWLine(final PrintWriter writer, final WstLine line) { + final double km = line.getKm(); + final double[] qs = line.getQs(); + final int num = line.getSize(); - if (!ignoreQs && dischargesChanged(qs)) { + if (!this.ignoreQs && dischargesChanged(qs)) { writeQLine(writer, qs); } - writer.printf(locale, "%8.3f", km); + writer.printf(this.locale, "%8.3f", km); for (int i = 0; i < num; i++) { - writer.printf(locale, "%9.2f", line.getW(i)); + writer.printf(this.locale, "%9.2f", line.getW(i)); } writer.println(); } - /** * Writes a discharge line (Q values) into a WST. * - * @param qs the Q values for the next range. + * @param qs + * the Q values for the next range. */ - protected void writeQLine(PrintWriter writer, double[] qs) { + protected void writeQLine(final PrintWriter writer, final double[] qs) { writer.write("*\u001f "); for (int i = 0; i < qs.length; i++) { this.qs[i] = qs[i]; - writer.printf(locale, "%9.2f", qs[i]); + writer.printf(this.locale, "%9.2f", qs[i]); } writer.println(); } - /** * This method determines if a Q has changed from the last line to the * current one. * - * @param newQs The Q values of the next line. + * @param newQs + * The Q values of the next line. * * @return true, if a Q value have changed, otherwise false. */ - protected boolean dischargesChanged(double[] newQs) { + protected boolean dischargesChanged(final double[] newQs) { // XXX maybe there is a way to do this faster - for (int i = 0; i < cols && i < qs.length && i < newQs.length; i++) { - if (Math.abs(newQs[i] - qs[i]) >= 0.001) { + for (int i = 0; i < this.cols && i < this.qs.length && i < newQs.length; i++) { + if (Math.abs(newQs[i] - this.qs[i]) >= 0.001) { return true; } } @@ -287,7 +287,7 @@ * lines are a map with km as the key and a wstline as value. */ public Map<Double, WstLine> getLines() { - return lines; + return this.lines; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :