Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionCalculation.java @ 9157:f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
author | mschaefer |
---|---|
date | Tue, 19 Jun 2018 14:19:32 +0200 |
parents | 23945061daec |
children | 1614cb14308f |
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; | |
26 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
27 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
9067 | 28 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; |
29 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
30 import org.dive4elements.river.backend.utils.DateUtil; |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
31 import org.dive4elements.river.model.Gauge; |
9067 | 32 import org.dive4elements.river.model.River; |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
33 import org.dive4elements.river.model.sinfo.CollisionAggregateValue; |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
34 import org.dive4elements.river.model.sinfo.CollisionValue; |
9067 | 35 |
36 class CollisionCalculation { | |
37 | |
38 // private static Logger log = Logger.getLogger(FloodDurationCalculation.class); | |
39 | |
40 private final CallContext context; | |
41 | |
42 public CollisionCalculation(final CallContext context) { | |
43 this.context = context; | |
44 } | |
45 | |
46 public CalculationResult calculate(final SINFOArtifact sinfo) { | |
47 | |
48 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); | |
49 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
50 // access input data |
9067 | 51 final CollisionAccess access = new CollisionAccess(sinfo); |
52 final River river = access.getRiver(); | |
53 final RiverInfo riverInfo = new RiverInfo(river); | |
54 | |
55 final DoubleRange calcRange = access.getRange(); | |
56 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
57 // calculate results for each year or epoch |
9067 | 58 final Calculation problems = new Calculation(); |
59 | |
60 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); | |
61 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
62 final CollisionCalculationResults results = new CollisionCalculationResults(calcModeLabel, user, riverInfo, calcRange, access.getYearsHeader()); |
9067 | 63 |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
64 final Collection<ResultRow> overViewRows = new ArrayList<>(); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
65 |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
66 if (access.getYears() != null) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
67 for (final int year : access.getYears()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
68 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
|
69 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
70 else { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
71 for (final DateRange dr : access.getEpochs()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
72 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
|
73 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
74 final CollisionCalcOverviewResult overviewResult = new CollisionCalcOverviewResult(access.getYearsHeader(), overViewRows); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
75 results.addResult(overviewResult, problems); |
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 // 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
|
78 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
79 final Map<Gauge, GaugeDischargeValuesFinder> qFinders = new HashMap<>(); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
80 final Map<Gauge, GaugeDischargeZoneFinder> zoneFinders = new HashMap<>(); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
81 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
|
82 qFinders.put(gauge, GaugeDischargeValuesFinder.loadValues(gauge, problems)); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
83 zoneFinders.put(gauge, GaugeDischargeZoneFinder.loadValues(gauge, problems)); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
84 } |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
85 final Collection<ResultRow> detailsRows = new ArrayList<>(); |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
86 |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
87 // calculate secondary results for each year or epoch |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
88 if (access.getYears() != null) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
89 for (final int year : access.getYears()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
90 calculateDetails(detailsRows, infoProvider, access.getLowerKm(), access.getUpperKm(), year, year, qFinders, zoneFinders); |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
91 } |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
92 else { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
93 for (final DateRange dr : access.getEpochs()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
94 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
|
95 } |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
96 final CollisionCalcDetailResult detailResult = new CollisionCalcDetailResult("Details", detailsRows); |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
97 results.addResult(detailResult, problems); |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
98 |
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
9067
diff
changeset
|
99 return new CalculationResult(results, problems); |
9067 | 100 } |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
101 |
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 * 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
|
104 * and adds them to a ResultRow collection |
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 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
|
107 final int toYear, final boolean isEpoch) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
108 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
|
109 rows.add(ResultRow.create().putValue(GeneralResultType.station, aggregate.getStation()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
110 .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
|
111 .putValue(SInfoResultType.collisionCount, aggregate.getCount())); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
112 } |
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 * 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
|
117 * and adds them to a ResultRow collection |
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 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
|
120 final int fromYear, final int toYear, final Map<Gauge, GaugeDischargeValuesFinder> qFinders, |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
121 final Map<Gauge, GaugeDischargeZoneFinder> zoneFinders) { |
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()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
128 .putValue(GeneralResultType.date, collision.getEventDate()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
129 .putValue(SInfoResultType.collisionGaugeW, collision.getGaugeW()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
130 .putValue(SInfoResultType.gaugeLabel, collision.getGaugeName()) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
131 .putValue(SInfoResultType.discharge, qOut) |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
132 .putValue(SInfoResultType.dischargeZone, zoneFinders.get(gauge).getDischargeZone(q))); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
133 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
134 } |
9067 | 135 } |