Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java @ 8169:856701739443
Handle missing years in bed height differences without assuming an unknown year to be BC 1.
author | "Tom Gottfried <tom@intevation.de>" |
---|---|
date | Mon, 01 Sep 2014 09:43:55 +0200 |
parents | a709e6334c4a |
children | e4606eae8ea5 |
comparison
equal
deleted
inserted
replaced
8168:75a6ac0c73ef | 8169:856701739443 |
---|---|
33 } | 33 } |
34 | 34 |
35 public CalculationResult calculate(BedDifferencesAccess access, CallContext context) { | 35 public CalculationResult calculate(BedDifferencesAccess access, CallContext context) { |
36 logger.info("BedDiffCalculation.calculate"); | 36 logger.info("BedDiffCalculation.calculate"); |
37 | 37 |
38 String river = access.getRiverName(); | 38 this.river = access.getRiverName(); |
39 int [][] heightIds = access.extractHeightIds(context); | 39 this.heightIds = access.extractHeightIds(context); |
40 | 40 |
41 if (river == null) { | 41 BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length]; |
42 // TODO: i18n | 42 for (int i = 0; i < heightIds.length; i++) { |
43 addProblem("minfo.missing.river"); | 43 BedHeightSingleData [] pair = getHeightPair(heightIds[i]); |
44 if (pair[0].getYear() == null || pair[1].getYear() == null) { | |
45 addProblem("beddiff.missing.year"); | |
46 } | |
47 results[i] = calculateYearDifference(pair); | |
44 } | 48 } |
45 | 49 |
46 if (!hasProblems()) { | |
47 this.river = river; | |
48 this.heightIds = heightIds; | |
49 return internalCalculate(); | |
50 } | |
51 | |
52 return new CalculationResult(); | |
53 } | |
54 | |
55 private CalculationResult internalCalculate() { | |
56 BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length]; | |
57 | |
58 for (int i = 0; i < heightIds.length; i++) { | |
59 BedHeightData [] pair = getHeightPair(heightIds[i], "single"); | |
60 results[i] = calculateYearDifference(pair); | |
61 } | |
62 return new CalculationResult(results, this); | 50 return new CalculationResult(results, this); |
63 } | 51 } |
64 | 52 |
65 /** Get two BedHeights from factory. */ | 53 /** Get two BedHeights from factory. */ |
66 private static BedHeightData [] getHeightPair(int [] ids, String type) { | 54 private static BedHeightSingleData [] getHeightPair(int [] ids) { |
67 return new BedHeightData [] { | 55 return new BedHeightSingleData [] { |
68 BedHeightFactory.getHeight(type, ids[0]), | 56 (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[0]), |
69 BedHeightFactory.getHeight(type, ids[1]) | 57 (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[1]) |
70 }; | 58 }; |
71 } | 59 } |
72 | 60 |
73 private BedDiffYearResult calculateYearDifference(BedHeightData[] pair) { | 61 private BedDiffYearResult calculateYearDifference( |
62 BedHeightSingleData[] pair | |
63 ) { | |
74 logger.debug("BedDiffCalculation.calculateYearDifference"); | 64 logger.debug("BedDiffCalculation.calculateYearDifference"); |
75 BedHeightSingleData s1 = (BedHeightSingleData) pair[0]; | 65 BedHeightSingleData s1 = pair[0]; |
76 BedHeightSingleData s2 = (BedHeightSingleData) pair[1]; | 66 BedHeightSingleData s2 = pair[1]; |
77 | 67 |
78 TDoubleArrayList stations = s1.getStations(); | 68 TDoubleArrayList stations = s1.getStations(); |
79 int size = stations.size(); | 69 int size = stations.size(); |
80 | 70 |
81 TDoubleArrayList diffRes = new TDoubleArrayList(size); | 71 TDoubleArrayList diffRes = new TDoubleArrayList(size); |
86 TDoubleArrayList heights1 = new TDoubleArrayList(size); | 76 TDoubleArrayList heights1 = new TDoubleArrayList(size); |
87 TDoubleArrayList heights2 = new TDoubleArrayList(size); | 77 TDoubleArrayList heights2 = new TDoubleArrayList(size); |
88 TDoubleArrayList morphs1 = new TDoubleArrayList(size); | 78 TDoubleArrayList morphs1 = new TDoubleArrayList(size); |
89 TDoubleArrayList morphs2 = new TDoubleArrayList(size); | 79 TDoubleArrayList morphs2 = new TDoubleArrayList(size); |
90 | 80 |
91 int range = Math.abs(s1.getYear() - s2.getYear()); | 81 Integer range = null; |
82 if (s1.getYear() != null && s2.getYear() != null) { | |
83 range = Math.abs(s1.getYear() - s2.getYear()); | |
84 } | |
92 | 85 |
93 for (int i = 0; i < size; i++) { | 86 for (int i = 0; i < size; i++) { |
94 double station = stations.getQuick(i); | 87 double station = stations.getQuick(i); |
95 double h1 = s1.getHeight(station); | 88 double h1 = s1.getHeight(station); |
96 double h2 = s2.getHeight(station); | 89 double h2 = s2.getHeight(station); |
108 | 101 |
109 gap.add(Math.max( | 102 gap.add(Math.max( |
110 s1.getDataGap(station), | 103 s1.getDataGap(station), |
111 s2.getDataGap(station))); | 104 s2.getDataGap(station))); |
112 | 105 |
113 absolute.add((hDiff / range) * 100d); | 106 if (range != null) { |
107 absolute.add((hDiff / range) * 100d); | |
108 } | |
114 heights1.add(h1); | 109 heights1.add(h1); |
115 heights2.add(h2); | 110 heights2.add(h2); |
116 morphs1.add(m1); | 111 morphs1.add(m1); |
117 morphs2.add(m2); | 112 morphs2.add(m2); |
118 } | 113 } |