Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/RiverInfoProvider.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 | aab0c2730a25 |
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.common; | |
11 | |
12 import java.util.List; | |
13 | |
14 import org.apache.commons.lang.math.DoubleRange; | |
15 import org.dive4elements.artifacts.CallContext; | |
16 import org.dive4elements.river.artifacts.model.LocationProvider; | |
17 import org.dive4elements.river.artifacts.model.WKms; | |
18 import org.dive4elements.river.artifacts.resources.Resources; | |
19 import org.dive4elements.river.artifacts.states.WaterlevelData; | |
20 import org.dive4elements.river.model.Gauge; | |
21 import org.dive4elements.river.model.River; | |
22 import org.dive4elements.river.utils.GaugeIndex; | |
23 | |
24 /** | |
25 * @author Gernot Belger | |
26 * | |
27 */ | |
28 public final class RiverInfoProvider { | |
29 | |
30 private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; | |
31 | |
32 private final River river; | |
33 private final GaugeIndex gaugeIndex; | |
34 private final Gauge refGauge; | |
35 private final boolean showAllGauges; | |
36 private final String notinrange; | |
37 | |
38 public static RiverInfoProvider forRange(final CallContext context, final River river, final DoubleRange calcRange) { | |
39 | |
40 final List<Gauge> gauges = river.determineGauges(calcRange.getMinimumDouble(), calcRange.getMaximumDouble()); | |
41 final GaugeIndex gaugeIndex = new GaugeIndex(gauges); | |
42 | |
43 final String notinrange = Resources.getMsg(context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE); | |
44 | |
45 return new RiverInfoProvider(notinrange, river, false, gaugeIndex, null); | |
46 } | |
47 | |
48 private RiverInfoProvider(final String notinrange, final River river, final boolean showAllGauges, final GaugeIndex gaugeIndex, final Gauge refGauge) { | |
49 this.notinrange = notinrange; | |
50 this.river = river; | |
51 this.showAllGauges = showAllGauges; | |
52 this.gaugeIndex = gaugeIndex; | |
53 this.refGauge = refGauge; | |
54 } | |
55 | |
56 public RiverInfoProvider forWaterlevel(final WaterlevelData waterlevel) { | |
57 final WKms wstKms = waterlevel.getWkms(); | |
58 final Gauge waterlevelRefGauge = findReferenceGauge(wstKms); | |
59 final boolean waterlevelShowAllGauges = waterlevel.isShowAllGauges(); | |
60 | |
61 return new RiverInfoProvider(this.notinrange, this.river, waterlevelShowAllGauges, this.gaugeIndex, waterlevelRefGauge); | |
62 } | |
63 | |
64 /** | |
65 * Re-determines the reference gauge, in the same way as the WaterlevelArtifact would do it | |
66 */ | |
67 private Gauge findReferenceGauge(final WKms wkms) { | |
68 | |
69 final double[] wstFromTo = findWstFromTo(wkms); | |
70 return this.river.determineRefGauge(wstFromTo, true); | |
71 } | |
72 | |
73 private static double[] findWstFromTo(final WKms wkms) { | |
74 | |
75 final double from = wkms.getKm(0); | |
76 final double to = wkms.getKm(wkms.size() - 1); | |
77 | |
78 final boolean waterIncreasing = wkms.guessWaterIncreasing(); | |
79 if (waterIncreasing) | |
80 return new double[] { to, from }; | |
81 | |
82 return new double[] { from, to }; | |
83 } | |
84 | |
85 public String getLocation(final double km) { | |
86 return LocationProvider.getLocation(this.river.getName(), km); | |
87 } | |
88 | |
89 public String findGauge(final double km) { | |
90 // REMARK: access the gauge once only during calculation | |
91 final Gauge gauge = getGauge(km); | |
92 | |
93 return gauge == null ? this.notinrange : gauge.getName(); | |
94 } | |
95 | |
96 private Gauge getGauge(final double km) { | |
97 | |
98 // REMARK: using same logic as in WaterlevelExporter here | |
99 | |
100 if (this.showAllGauges) | |
101 return this.gaugeIndex.findGauge(km); | |
102 | |
103 if (this.refGauge.getRange().contains(km)) | |
104 return this.refGauge; | |
105 | |
106 return null; | |
107 } | |
108 | |
109 public String getReferenceGauge() { | |
110 return this.refGauge == null ? this.notinrange : this.refGauge.getName(); | |
111 } | |
112 | |
113 public River getRiver() { | |
114 return this.river; | |
115 } | |
116 } |