Mercurial > dive4elements > river
changeset 650:6695e03e9b82
Added parser for segments
flys-artifacts/trunk@2043 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 01 Jun 2011 17:01:44 +0000 |
parents | 44175d4720f8 |
children | e6cecb661bff |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java |
diffstat | 3 files changed, 105 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Wed Jun 01 09:57:22 2011 +0000 +++ b/flys-artifacts/ChangeLog Wed Jun 01 17:01:44 2011 +0000 @@ -1,3 +1,11 @@ +2011-06-01 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + * src/main/java/de/intevation/flys/artifacts/model/Segment.java: + New. Parse segments only once. + + * src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: + Expose new parser to artifacts. TODO: Use it. + 2011-06-01 Sascha L. Teichmann <sascha.teichmann@intevation.de> * src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java Wed Jun 01 09:57:22 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java Wed Jun 01 17:01:44 2011 +0000 @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -37,8 +38,11 @@ import de.intevation.flys.model.River; import de.intevation.flys.artifacts.context.FLYSContext; + import de.intevation.flys.artifacts.model.DischargeTables; import de.intevation.flys.artifacts.model.RiverFactory; +import de.intevation.flys.artifacts.model.Segment; + import de.intevation.flys.artifacts.states.DefaultState; import de.intevation.flys.artifacts.states.LocationDistanceSelect; @@ -746,6 +750,20 @@ } } + public List<Segment> getSegments() { + StateData wqValues = getData("wq_values"); + if (wqValues == null) { + logger.warn("no wq_values given"); + return Collections.emptyList(); + } + String input = (String)wqValues.getValue(); + if (input == null || (input = input.trim()).length() == 0) { + logger.warn("wq_values are empty"); + return Collections.emptyList(); + } + return Segment.parseSegments(input); + } + /** * Returns the Qs for a number of Ws. This method makes use of
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java Wed Jun 01 17:01:44 2011 +0000 @@ -0,0 +1,79 @@ +package de.intevation.flys.artifacts.model; + +import java.util.List; +import java.util.ArrayList; + +import java.io.Serializable; + +import org.apache.log4j.Logger; + +import gnu.trove.TDoubleArrayList; + +public class Segment +implements Serializable +{ + private static Logger logger = Logger.getLogger(Segment.class); + + protected double from; + protected double to; + protected double [] values; + + public Segment() { + } + + public Segment(double from, double to, double [] values) { + this.from = from; + this.to = to; + this.values = values; + } + + public boolean isUp() { + return from < to; + } + + public String toString() { + StringBuilder sb = new StringBuilder("Segment: ["); + sb.append("from: ").append(from).append("; to: ") + .append(to).append("; values: ("); + for (int i = 0; i < values.length; ++i) { + if (i > 0) sb.append(", "); + sb.append(values[i]); + } + sb.append(")]"); + return sb.toString(); + } + + public static List<Segment> parseSegments(String input) { + + ArrayList<Segment> segments = new ArrayList<Segment>(); + + TDoubleArrayList vs = new TDoubleArrayList(); + + for (String segmentStr: input.split(":")) { + String [] parts = segmentStr.split(";"); + if (parts.length < 3) { + logger.warn("invalid segment: '" + segmentStr + "'"); + continue; + } + try { + double from = Double.parseDouble(parts[0].trim()); + double to = Double.parseDouble(parts[1].trim()); + + vs.clear(); + + for (String valueStr: parts[2].split(",")) { + vs.add(Double.parseDouble(valueStr.trim())); + } + + double [] values = vs.toNativeArray(); + segments.add(new Segment(from, to, values)); + } + catch (NumberFormatException nfe) { + logger.warn("invalid segment: '" + segmentStr + "'"); + } + } + + return segments; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :