diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java @ 8033:5e3f4b4fcb28

New way to loaded sediment loads from database and cache it. The data structure a way more straight forward. TODO: Implement calculation on this basis.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 15 Jul 2014 12:47:52 +0200
parents
children b6e7cfcabf2c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Tue Jul 15 12:47:52 2014 +0200
@@ -0,0 +1,207 @@
+/* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+package org.dive4elements.river.artifacts.model.minfo;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.NavigableMap;
+import java.util.TreeMap;
+
+import org.dive4elements.river.utils.EpsilonComparator;
+
+public class SedimentLoadData implements Serializable
+{
+    public static final double STATION_EPS = 0.0001;
+
+    public static final int GF_COARSE        = 0;
+    public static final int GF_FINE_MIDDLE   = 1;
+    public static final int GF_SAND          = 2;
+    public static final int GF_SUSP_SAND     = 3;
+    public static final int GF_SUSP_SAND_BED = 4;
+    public static final int GF_SUSP_SEDIMENT = 5;
+    public static final int GF_MAX           = 5;
+
+    public static final int grainFractionIndex(String name) {
+        if ("coarse".equals(name))             return GF_COARSE;
+        if ("fine_middle".equals(name))        return GF_FINE_MIDDLE;
+        if ("sand".equals(name))               return GF_SAND;
+        if ("susp_sand".equals(name))          return GF_SUSP_SAND;
+        if ("susp_sand_bed".equals(name))      return GF_SUSP_SAND_BED;
+        if ("suspended_sediment".equals(name)) return GF_SUSP_SEDIMENT;
+        return -1;
+    }
+
+    public static class Value implements Serializable {
+
+        private double value;
+
+        private Load load;
+
+        public Value() {
+        }
+
+        public Value(Load load, double value) {
+            this.load = load;
+            this.value = value;
+        }
+
+        public double getValue() {
+            return value;
+        }
+
+        public Load getLoad() {
+            return load;
+        }
+    } // class Value
+
+
+    public static class Load implements Serializable {
+
+        private int id;
+
+        private String description;
+
+        private Date startTime;
+        private Date stopTime;
+
+        public Load() {
+        }
+
+        public Load(int id, String description, Date startTime, Date stopTime) {
+            this.id          = id;
+            this.description = description;
+            this.startTime   = startTime;
+            this.stopTime    = stopTime;
+        }
+
+        public int getId() {
+            return id;
+        }
+
+        public String getDescription() {
+            return description;
+        }
+
+        public Date getStartTime() {
+            return startTime;
+        }
+
+        public Date getStopTime() {
+            return stopTime;
+        }
+
+        public boolean isEpoch() {
+            return startTime != null && stopTime != null;
+        }
+    } // class SedimentLoad
+
+    public static class Station implements Serializable {
+
+        public static final int BED_LOAD  = 0;
+        public static final int SUSPENDED = 1;
+
+        public interface Visitor {
+            boolean accept(Station station);
+            boolean accept(int grainFraction);
+            boolean visit(Value value, int grainFraction);
+        }
+
+        private double station;
+
+        private int type;
+
+        private List<List<Value>> grainFractions;
+
+        public Station() {
+            this(BED_LOAD, 0.0);
+        }
+
+        public Station(int type, double station) {
+            grainFractions = new ArrayList<List<Value>>(GF_MAX+1);
+            for (int i = 0; i < GF_MAX+1; ++i) {
+                grainFractions.add(new ArrayList<Value>());
+            }
+            this.type = type;
+            this.station = station;
+        }
+
+        public double getStation() {
+            return station;
+        }
+
+        public int getType() {
+            return type;
+        }
+
+        public void addValue(int grainFraction, Value value) {
+            grainFractions.get(grainFraction).add(value);
+        }
+
+        public boolean hasGrainFraction(String grainFraction) {
+            return hasGrainFraction(grainFractionIndex(grainFraction));
+        }
+
+        public boolean hasGrainFraction(int grainFraction) {
+            List<Value> values = grainFractions.get(grainFraction);
+            return !values.isEmpty();
+        }
+
+        public void visit(Visitor visitor) {
+            if (!visitor.accept(this)) {
+                return;
+            }
+
+            for (int i = 0; i < GF_MAX+1; ++i) {
+                List<Value> values = grainFractions.get(i);
+                if (values.isEmpty() || !visitor.accept(i)) {
+                    continue;
+                }
+                for (Value value: values) {
+                    if (!visitor.visit(value, i)) {
+                        break;
+                    }
+                }
+            }
+        }
+
+        public boolean inside(double a, double b) {
+            return station >= a && station <= b;
+        }
+    } // class Station
+
+
+    private TreeMap<Double, List<Station>> stations;
+
+    public SedimentLoadData() {
+        stations = new TreeMap<Double, List<Station>>(EpsilonComparator.CMP);
+    }
+
+    public void addStation(Station station) {
+        Double key = station.getStation();
+        List<Station> sts = stations.get(key);
+        if (sts == null) {
+            sts = new ArrayList<Station>(2);
+            stations.put(key, sts);
+        }
+        sts.add(station);
+    }
+
+
+    public NavigableMap<Double, List<Station>> range(double from, double to) {
+
+        if (from > to) {
+            double t = from; from = to; to = t;
+        }
+
+        return stations.subMap(from, true, to, true);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org