comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsFinder.java @ 8915:d9dbf0b74bc2

Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
author gernotbelger
date Wed, 28 Feb 2018 17:27:15 +0100
parents
children 11bf13cf0463
comparison
equal deleted inserted replaced
8914:e3519c3e7a0a 8915:d9dbf0b74bc2
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
2 * Software engineering by
3 * Björnsen Beratende Ingenieure GmbH
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
5 *
6 * This file is Free Software under the GNU AGPL (>=v3)
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
8 * documentation coming with Dive4Elements River for details.
9 */
10 package org.dive4elements.river.artifacts.sinfo.tkhstate;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.NavigableMap;
18 import java.util.TreeMap;
19
20 import org.apache.commons.lang.math.DoubleRange;
21 import org.dive4elements.river.artifacts.math.Linear;
22 import org.dive4elements.river.artifacts.model.Calculation;
23 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo;
24 import org.dive4elements.river.model.BedHeight;
25 import org.dive4elements.river.model.BedHeightValue;
26 import org.dive4elements.river.model.River;
27
28 /**
29 * Provides bed heigts for vcarious calculations.
30 *
31 * @author Gernot Belger
32 */
33 public final class BedHeightsFinder {
34
35 private final BedHeightInfo info;
36
37 private final NavigableMap<Double, BedHeightValue> values;
38
39 /**
40 * Create specific bed heights used in tkh-calculation
41 *
42 * @param problems
43 */
44 public static Collection<BedHeightsFinder> createTkhBedHeights(final River river, final Calculation problems, final DoubleRange range) {
45 // FIXME: determine relevant bed-heights by river: read from some configuration file
46 // '3' is already the right one for demo-model == '"DGM-2004_Epoche-2-SOBEK"'
47 final int bedheightId = 3;
48
49 final Collection<BedHeight> bedHeights = Collections.singletonList(BedHeight.getBedHeightById(bedheightId));
50
51 // TODO: check for overlapping ranges... and provide a warning message, else we get problems later
52
53 final List<BedHeightsFinder> result = new ArrayList<>(bedHeights.size());
54
55 for (final BedHeight bedHeight : bedHeights) {
56 result.add(createBedHeights(bedHeight, range));
57 }
58
59 return result;
60 }
61
62 /**
63 * Creates a {@link BedHeightsFinder} for a dataset from the database, specified by its id.
64 *
65 * @return <code>null</code> if no bed height with the given id exists.
66 */
67 public static BedHeightsFinder forId(final int id, final DoubleRange range) {
68
69 final BedHeight bedHeight = BedHeight.getBedHeightById(id);
70 if (bedHeight == null)
71 return null;
72
73 return BedHeightsFinder.createBedHeights(bedHeight, range);
74 }
75
76 /**
77 * Create a finder for a given bed height.
78 *
79 * @param range
80 */
81 private static BedHeightsFinder createBedHeights(final BedHeight bedHeight, final DoubleRange range) {
82
83 // FIXME: sort by station, but in what direction?
84 // FIXME: using river.getKmUp()?
85 final NavigableMap<Double, BedHeightValue> values = new TreeMap<>();
86
87 for (final BedHeightValue bedHeightValue : bedHeight.getValues()) {
88 final Double station = bedHeightValue.getStation();
89 if (station != null && range.containsDouble(station)) {
90
91 if (bedHeightValue.getHeight() != null)
92 values.put(station, bedHeightValue);
93 }
94 }
95
96 final BedHeightInfo info = BedHeightInfo.from(bedHeight);
97
98 return new BedHeightsFinder(info, values);
99 }
100
101 private BedHeightsFinder(final BedHeightInfo info, final NavigableMap<Double, BedHeightValue> values) {
102 this.info = info;
103 this.values = values;
104 }
105
106 public BedHeightInfo getInfo() {
107 return this.info;
108 }
109
110 public Collection<Double> getStations() {
111 return this.values.keySet();
112 }
113
114 public double getMeanBedHeight(final double km) {
115
116 if (this.values.containsKey(km))
117 return this.values.get(km).getHeight();
118
119 final Entry<Double, BedHeightValue> floorEntry = this.values.floorEntry(km);
120 final Entry<Double, BedHeightValue> ceilingEntry = this.values.ceilingEntry(km);
121
122 if (floorEntry == null || ceilingEntry == null)
123 return Double.NaN;
124
125 final double floorKm = floorEntry.getKey();
126 final double floorHeight = floorEntry.getValue().getHeight();
127 final double ceilKm = ceilingEntry.getKey();
128 final double ceilHeight = ceilingEntry.getValue().getHeight();
129
130 // FIXME: check if we always want that...
131
132 return Linear.linear(km, floorKm, ceilKm, floorHeight, ceilHeight);
133 }
134 }

http://dive4elements.wald.intevation.org