annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionCalculation.java @ 9318:7b2b086e45f0

collision pdf details, ResultType refactoring, bezugswst result
author gernotbelger
date Fri, 27 Jul 2018 10:11:23 +0200
parents 4f411c6ee3ae
children 504f63512379
rev   line source
9067
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
2 * Software engineering by
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
5 *
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
9 */
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
10 package org.dive4elements.river.artifacts.sinfo.collision;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
17 import org.apache.commons.lang.math.DoubleRange;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
21 import org.dive4elements.river.artifacts.model.Calculation;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
24 import org.dive4elements.river.artifacts.resources.Resources;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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;
9202
b4402594213b More work on calculations and output for S-Info flood duration workflow (chart types 1 and 2)
mschaefer
parents: 9176
diff changeset
27 import org.dive4elements.river.artifacts.sinfo.common.GaugeMainValueFinder;
9067
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
30 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
38
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
39 class CollisionCalculation {
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
40
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
41 // private static Logger log = Logger.getLogger(FloodDurationCalculation.class);
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
42
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
43 private final CallContext context;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
44
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
45 public CollisionCalculation(final CallContext context) {
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
46 this.context = context;
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
47 }
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
48
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
49 public CalculationResult calculate(final SINFOArtifact sinfo) {
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
50
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
51 final String user = CalculationUtils.findArtifactUser(this.context, sinfo);
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
52
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
53 // access input data
9067
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
54 final CollisionAccess access = new CollisionAccess(sinfo);
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
55 final River river = access.getRiver();
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
56 final RiverInfo riverInfo = new RiverInfo(river);
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
57
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
58 final DoubleRange calcRange = access.getRange();
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
61 final Calculation problems = new Calculation();
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
62
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
63 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name());
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
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);
9318
7b2b086e45f0 collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents: 9204
diff changeset
72 } else {
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
73 for (final DateRange dr : access.getEpochs())
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
74 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
75 }
9204
4f411c6ee3ae Individual facets/themes for single years i the S-Info collisions output
mschaefer
parents: 9202
diff changeset
76 final CollisionCalcOverviewResult overviewResult = new CollisionCalcOverviewResult(access.getYearsHeader(), access.getYears(), overViewRows);
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
77 results.addResult(overviewResult, problems);
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
78
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
79 // 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
80 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange);
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
81 final Map<Gauge, GaugeDischargeValuesFinder> qFinders = new HashMap<>();
9202
b4402594213b More work on calculations and output for S-Info flood duration workflow (chart types 1 and 2)
mschaefer
parents: 9176
diff changeset
82 final Map<Gauge, GaugeMainValueFinder> zoneFinders = new HashMap<>();
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
83 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
84 qFinders.put(gauge, GaugeDischargeValuesFinder.loadValues(gauge, problems));
9202
b4402594213b More work on calculations and output for S-Info flood duration workflow (chart types 1 and 2)
mschaefer
parents: 9176
diff changeset
85 zoneFinders.put(gauge, GaugeMainValueFinder.loadValues(MainValueTypeKey.Q, gauge, problems));
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
86 }
9150
23945061daec gigantic refactoring: exporter, result, results
gernotbelger
parents: 9067
diff changeset
87 final Collection<ResultRow> detailsRows = new ArrayList<>();
23945061daec gigantic refactoring: exporter, result, results
gernotbelger
parents: 9067
diff changeset
88
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
89 // calculate secondary results for each year or epoch
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
90 if (access.getYears() != null) {
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
91 for (final int year : access.getYears())
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
92 calculateDetails(detailsRows, infoProvider, access.getLowerKm(), access.getUpperKm(), year, year, qFinders, zoneFinders);
9318
7b2b086e45f0 collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents: 9204
diff changeset
93 } else {
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
94 for (final DateRange dr : access.getEpochs())
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
95 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
96 }
23945061daec gigantic refactoring: exporter, result, results
gernotbelger
parents: 9067
diff changeset
97 final CollisionCalcDetailResult detailResult = new CollisionCalcDetailResult("Details", detailsRows);
23945061daec gigantic refactoring: exporter, result, results
gernotbelger
parents: 9067
diff changeset
98 results.addResult(detailResult, problems);
23945061daec gigantic refactoring: exporter, result, results
gernotbelger
parents: 9067
diff changeset
99
23945061daec gigantic refactoring: exporter, result, results
gernotbelger
parents: 9067
diff changeset
100 return new CalculationResult(results, problems);
9067
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
101 }
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
102
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
103 /**
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
104 * 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
105 * and adds them to a ResultRow collection
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
106 */
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
107 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
108 final int toYear, final boolean isEpoch) {
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
109 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
110 rows.add(ResultRow.create().putValue(GeneralResultType.station, aggregate.getStation())
9318
7b2b086e45f0 collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents: 9204
diff changeset
111 .putValue(SInfoResultType.years, (isEpoch ? String.format("%d-%d", fromYear, toYear) : Integer.toString(fromYear)))
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
112 .putValue(SInfoResultType.collisionCount, aggregate.getCount()));
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
113 }
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
114 }
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 * 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
118 * and adds them to a ResultRow collection
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
119 */
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
120 private void calculateDetails(final Collection<ResultRow> rows, final RiverInfoProvider riverInfo, final double fromKm, final double toKm,
9318
7b2b086e45f0 collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents: 9204
diff changeset
121 final int fromYear, final int toYear, final Map<Gauge, GaugeDischargeValuesFinder> qFinders, final Map<Gauge, GaugeMainValueFinder> zoneFinders) {
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
122 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
123 DateUtil.getEndDateFromYear(toYear))) {
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
124 final Gauge gauge = riverInfo.getGauge(collision.getStation(), true);
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
125 final double q = qFinders.get(gauge).getDischarge(collision.getGaugeW());
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
126 final double qOut = Double.isInfinite(q) ? Double.NaN : q;
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
127 rows.add(ResultRow.create().putValue(GeneralResultType.station, collision.getStation())
9318
7b2b086e45f0 collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents: 9204
diff changeset
128 .putValue(GeneralResultType.dateShort, collision.getEventDate()).putValue(SInfoResultType.collisionGaugeW, collision.getGaugeW())
7b2b086e45f0 collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents: 9204
diff changeset
129 .putValue(GeneralResultType.gaugeLabelCm, collision.getGaugeName()).putValue(SInfoResultType.dischargeLong, qOut)
9202
b4402594213b More work on calculations and output for S-Info flood duration workflow (chart types 1 and 2)
mschaefer
parents: 9176
diff changeset
130 .putValue(SInfoResultType.dischargeZone, zoneFinders.get(gauge).findZoneName(q)));
9157
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
131 }
f9bb5d0a6ff3 Added the S-Info collision calculation and chart output
mschaefer
parents: 9150
diff changeset
132 }
9067
2ed3824a3d53 sinfo access collision, floodDuration
gernotbelger
parents:
diff changeset
133 }

http://dive4elements.wald.intevation.org