9499
|
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.model.river; |
|
11 |
|
12 import java.util.HashMap; |
|
13 import java.util.Map; |
|
14 |
|
15 import org.apache.commons.lang.math.DoubleRange; |
|
16 import org.dive4elements.artifacts.CallContext; |
|
17 import org.dive4elements.river.artifacts.model.WstValueTable; |
|
18 import org.dive4elements.river.artifacts.model.WstValueTable.QPosition; |
|
19 import org.dive4elements.river.artifacts.model.WstValueTableFactory; |
|
20 import org.dive4elements.river.model.Gauge; |
|
21 import org.dive4elements.river.model.MainValue; |
|
22 import org.dive4elements.river.model.MainValueType.MainValueTypeKey; |
|
23 import org.dive4elements.river.model.River; |
|
24 |
|
25 /** |
|
26 * @author Domenico Nardi Tironi |
|
27 */ |
|
28 public final class MainWstValuesCalculator { |
|
29 |
|
30 private final WstValueTable wst; |
|
31 |
|
32 private final Map<String, MainValueQPosition> positions; |
|
33 |
|
34 private static class MainValueQPosition { |
|
35 |
|
36 private final Map<Gauge, QPosition> gaugePositions = new HashMap<>(); |
|
37 private QPosition refGaugePositions = null; |
|
38 } |
|
39 |
|
40 public static MainWstValuesCalculator forRiver(final CallContext context, final River river, final DoubleRange calcRange, final String... mainValueNames) { |
|
41 |
|
42 final RiverInfoProvider info = RiverInfoProvider.forRange(context, river, calcRange); |
|
43 |
|
44 return forRiverInfo(info, mainValueNames); |
|
45 } |
|
46 |
|
47 public static MainWstValuesCalculator forRiverInfo(final RiverInfoProvider info, final String... mainValueNames) { |
|
48 |
|
49 final WstValueTable wst = WstValueTableFactory.getTable(info.getRiver()); |
|
50 |
|
51 final Map<String, MainValueQPosition> positions = calculatePositions(info, wst, mainValueNames); |
|
52 |
|
53 return new MainWstValuesCalculator(wst, positions); |
|
54 } |
|
55 |
|
56 private static Map<String, MainValueQPosition> calculatePositions(final RiverInfoProvider info, final WstValueTable wst, final String[] mainValueNames) { |
|
57 |
|
58 boolean isFirstGauge = true; |
|
59 |
|
60 final Map<String, MainValueQPosition> positions = new HashMap<>(); |
|
61 |
|
62 for (final String mainValue : mainValueNames) |
|
63 positions.put(mainValue.toUpperCase(), new MainValueQPosition()); |
|
64 |
|
65 for (final Gauge gauge : info.getGauges()) { |
|
66 |
|
67 for (final MainValueQPosition position : positions.values()) |
|
68 position.gaugePositions.put(gauge, null); |
|
69 |
|
70 final double gaugeKm = gauge.getStation().doubleValue(); |
|
71 for (final MainValue mv : MainValue.getValuesOfGaugeAndType(gauge, MainValueTypeKey.Q)) { |
|
72 |
|
73 final MainValueQPosition position = positions.get(mv.getMainValue().getName().toUpperCase()); |
|
74 if (position != null) { |
|
75 final QPosition qPosition = wst.getQPosition(gaugeKm, mv.getValue().doubleValue()); |
|
76 position.gaugePositions.put(gauge, qPosition); |
|
77 |
|
78 if (isFirstGauge) |
|
79 position.refGaugePositions = qPosition; |
|
80 } |
|
81 } |
|
82 |
|
83 isFirstGauge = false; |
|
84 } |
|
85 |
|
86 return positions; |
|
87 } |
|
88 |
|
89 private MainWstValuesCalculator(final WstValueTable wst, final Map<String, MainValueQPosition> positions) { |
|
90 this.wst = wst; |
|
91 this.positions = positions; |
|
92 } |
|
93 |
|
94 public boolean hasPosition(final String mainValueName) { |
|
95 |
|
96 final MainValueQPosition position = this.positions.get(mainValueName); |
|
97 if (position == null) |
|
98 throw new IllegalArgumentException(); |
|
99 |
|
100 return position.refGaugePositions != null; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Interpolates the W for a station with a fixed (virtual) wst column position |
|
105 */ |
|
106 public double interpolateW(final double station, final String mainValueName) { |
|
107 |
|
108 final MainValueQPosition mainValuePosition = this.positions.get(mainValueName.toUpperCase()); |
|
109 |
|
110 if (mainValuePosition.refGaugePositions == null) |
|
111 return Double.NaN; |
|
112 |
|
113 return this.wst.interpolateW(station, mainValuePosition.refGaugePositions); |
|
114 } |
|
115 } |