Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionCalculation.java @ 9176:1614cb14308f
Work on calculations for S-Info flood duration workflow
author | mschaefer |
---|---|
date | Mon, 25 Jun 2018 19:21:11 +0200 |
parents | f9bb5d0a6ff3 |
children | b4402594213b |
rev | line source |
---|---|
9067 | 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.collision; | |
11 | |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
12 import java.util.ArrayList; |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
13 import java.util.Collection; |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
14 import java.util.HashMap; |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
15 import java.util.Map; |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
16 |
9067 | 17 import org.apache.commons.lang.math.DoubleRange; |
18 import org.dive4elements.artifacts.CallContext; | |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
19 import org.dive4elements.river.artifacts.common.GeneralResultType; |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
20 import org.dive4elements.river.artifacts.common.ResultRow; |
9067 | 21 import org.dive4elements.river.artifacts.model.Calculation; |
22 import org.dive4elements.river.artifacts.model.CalculationResult; | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
23 import org.dive4elements.river.artifacts.model.DateRange; |
9067 | 24 import org.dive4elements.river.artifacts.resources.Resources; |
25 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; | |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
26 import org.dive4elements.river.artifacts.sinfo.common.GaugeDischargeValuesFinder; |
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
27 import org.dive4elements.river.artifacts.sinfo.common.GaugeMainValueNameFinder; |
9067 | 28 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
29 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
9067 | 30 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; |
31 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
32 import org.dive4elements.river.backend.utils.DateUtil; |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
33 import org.dive4elements.river.model.Gauge; |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
34 import org.dive4elements.river.model.MainValueType.MainValueTypeKey; |
9067 | 35 import org.dive4elements.river.model.River; |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
36 import org.dive4elements.river.model.sinfo.CollisionAggregateValue; |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
37 import org.dive4elements.river.model.sinfo.CollisionValue; |
9067 | 38 |
39 class CollisionCalculation { | |
40 | |
41 // private static Logger log = Logger.getLogger(FloodDurationCalculation.class); | |
42 | |
43 private final CallContext context; | |
44 | |
45 public CollisionCalculation(final CallContext context) { | |
46 this.context = context; | |
47 } | |
48 | |
49 public CalculationResult calculate(final SINFOArtifact sinfo) { | |
50 | |
51 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); | |
52 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
53 // access input data |
9067 | 54 final CollisionAccess access = new CollisionAccess(sinfo); |
55 final River river = access.getRiver(); | |
56 final RiverInfo riverInfo = new RiverInfo(river); | |
57 | |
58 final DoubleRange calcRange = access.getRange(); | |
59 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
60 // calculate results for each year or epoch |
9067 | 61 final Calculation problems = new Calculation(); |
62 | |
63 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); | |
64 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
65 final CollisionCalculationResults results = new CollisionCalculationResults(calcModeLabel, user, riverInfo, calcRange, access.getYearsHeader()); |
9067 | 66 |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
67 final Collection<ResultRow> overViewRows = new ArrayList<>(); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
68 |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
69 if (access.getYears() != null) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
70 for (final int year : access.getYears()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
71 calculateOverview(overViewRows, river, access.getLowerKm(), access.getUpperKm(), year, year, false); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
72 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
73 else { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
74 for (final DateRange dr : access.getEpochs()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
75 calculateOverview(overViewRows, river, access.getLowerKm(), access.getUpperKm(), dr.getFromYear(), dr.getToYear(), true); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
76 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
77 final CollisionCalcOverviewResult overviewResult = new CollisionCalcOverviewResult(access.getYearsHeader(), overViewRows); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
78 results.addResult(overviewResult, problems); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
79 |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
80 // create q-for-w-finders for all gauges of the calculation km range |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
81 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
82 final Map<Gauge, GaugeDischargeValuesFinder> qFinders = new HashMap<>(); |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
83 final Map<Gauge, GaugeMainValueNameFinder> zoneFinders = new HashMap<>(); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
84 for (final Gauge gauge : river.determineGauges(calcRange.getMinimumDouble(), calcRange.getMaximumDouble())) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
85 qFinders.put(gauge, GaugeDischargeValuesFinder.loadValues(gauge, problems)); |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
86 zoneFinders.put(gauge, GaugeMainValueNameFinder.loadValues(MainValueTypeKey.Q, gauge, problems)); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
87 } |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
88 final Collection<ResultRow> detailsRows = new ArrayList<>(); |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
89 |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
90 // calculate secondary results for each year or epoch |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
91 if (access.getYears() != null) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
92 for (final int year : access.getYears()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
93 calculateDetails(detailsRows, infoProvider, access.getLowerKm(), access.getUpperKm(), year, year, qFinders, zoneFinders); |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
94 } |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
95 else { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
96 for (final DateRange dr : access.getEpochs()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
97 calculateDetails(detailsRows, infoProvider, access.getLowerKm(), access.getUpperKm(), dr.getFromYear(), dr.getToYear(), qFinders, zoneFinders); |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
98 } |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
99 final CollisionCalcDetailResult detailResult = new CollisionCalcDetailResult("Details", detailsRows); |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
100 results.addResult(detailResult, problems); |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
101 |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
102 return new CalculationResult(results, problems); |
9067 | 103 } |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
104 |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
105 /** |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
106 * Calculates the collision counts for a km range of a river and a year or year range (epoch), |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
107 * and adds them to a ResultRow collection |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
108 */ |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
109 private void calculateOverview(final Collection<ResultRow> rows, final River river, final double fromKm, final double toKm, final int fromYear, |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
110 final int toYear, final boolean isEpoch) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
111 for (final CollisionAggregateValue aggregate : CollisionAggregateValue.getValuesByKm(river, fromKm, toKm, fromYear, toYear)) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
112 rows.add(ResultRow.create().putValue(GeneralResultType.station, aggregate.getStation()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
113 .putValue(SInfoResultType.years, (isEpoch? String.format("%d-%d", fromYear, toYear) : Integer.toString(fromYear))) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
114 .putValue(SInfoResultType.collisionCount, aggregate.getCount())); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
115 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
116 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
117 |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
118 /** |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
119 * Calculates the collision details for a km range of a river and a year or year range (epoch), |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
120 * and adds them to a ResultRow collection |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
121 */ |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
122 private void calculateDetails(final Collection<ResultRow> rows, final RiverInfoProvider riverInfo, final double fromKm, final double toKm, |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
123 final int fromYear, final int toYear, final Map<Gauge, GaugeDischargeValuesFinder> qFinders, |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
124 final Map<Gauge, GaugeMainValueNameFinder> zoneFinders) { |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
125 for (final CollisionValue collision : CollisionValue.getValues(riverInfo.getRiver(), fromKm, toKm, DateUtil.getStartDateFromYear(fromYear), |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
126 DateUtil.getEndDateFromYear(toYear))) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
127 final Gauge gauge = riverInfo.getGauge(collision.getStation(), true); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
128 final double q = qFinders.get(gauge).getDischarge(collision.getGaugeW()); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
129 final double qOut = Double.isInfinite(q) ? Double.NaN : q; |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
130 rows.add(ResultRow.create().putValue(GeneralResultType.station, collision.getStation()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
131 .putValue(GeneralResultType.date, collision.getEventDate()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
132 .putValue(SInfoResultType.collisionGaugeW, collision.getGaugeW()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
133 .putValue(SInfoResultType.gaugeLabel, collision.getGaugeName()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
134 .putValue(SInfoResultType.discharge, qOut) |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9157
diff
changeset
|
135 .putValue(SInfoResultType.dischargeZone, zoneFinders.get(gauge).getZoneName(q))); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
136 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
137 } |
9067 | 138 } |