comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQDay.java @ 7494:4de4b19b6be6

Fixed half broken interpolation code for lines to 'Dauerlinie'.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 01 Nov 2013 13:25:54 +0100
parents 8b614d152a79
children 5e38e2924c07
comparison
equal deleted inserted replaced
7493:8cdc86327149 7494:4de4b19b6be6
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the 5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.artifacts.model; 9 package org.dive4elements.river.artifacts.model;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14
15 import org.dive4elements.river.artifacts.math.Linear;
10 16
11 import gnu.trove.TIntArrayList; 17 import gnu.trove.TIntArrayList;
12 18
13 /** 19 /**
14 * This class represents a pool of data triples that consists of 'W', 'Q' and 20 * This class represents a pool of data triples that consists of 'W', 'Q' and
17 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 23 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
18 */ 24 */
19 public class WQDay 25 public class WQDay
20 extends WQ 26 extends WQ
21 { 27 {
28 public static final Comparator<double []> FIRST_CMP = new Comparator<double []>() {
29 @Override
30 public int compare(double [] a, double [] b) {
31 double diff = a[0] - b[0];
32 if (diff < 0d) return -1;
33 if (diff > 0d) return +1;
34 return 0;
35 }
36 };
37
38 public static final double EPSILON = 1e-4;
39
40
22 protected TIntArrayList days; 41 protected TIntArrayList days;
23 42
24 public WQDay() { 43 public WQDay() {
25 super(""); 44 super("");
26 days = new TIntArrayList(); 45 days = new TIntArrayList();
50 69
51 public int getDay(int idx) { 70 public int getDay(int idx) {
52 return days.getQuick(idx); 71 return days.getQuick(idx);
53 } 72 }
54 73
74 private static final Double interpolateX(ArrayList<double []> dxs, double x) {
75
76 Collections.sort(dxs, FIRST_CMP);
77
78 if (Math.abs(x - dxs.get(0)[1]) < EPSILON) {
79 return dxs.get(0)[0];
80 }
81
82 for (int i = 1, S = dxs.size(); i < S; ++i) {
83 double [] curr = dxs.get(i);
84 if (Math.abs(x - curr[1]) < EPSILON) {
85 return curr[0];
86 }
87
88 double [] prev = dxs.get(i-1);
89 double x1 = Math.min(prev[1], curr[1]);
90 double x2 = Math.max(prev[1], curr[1]);
91 if (x > x1 && x < x2) {
92 return Linear.linear(
93 x,
94 prev[1], curr[1],
95 prev[0], curr[0]);
96 }
97 }
98
99 return null;
100 }
101
102 public Double interpolateDayByW(double w) {
103
104 int S = days.size();
105
106 if (S == 0) {
107 return null;
108 }
109
110 ArrayList<double[]> dws = new ArrayList<double[]>(S);
111
112 for (int i = 0; i < S; ++i) {
113 dws.add(new double[] { getDay(i), getW(i) });
114 }
115
116 return interpolateX(dws, w);
117 }
118
119 public Double interpolateDayByQ(double q) {
120
121 int S = days.size();
122
123 if (S == 0) {
124 return null;
125 }
126
127 ArrayList<double[]> dqs = new ArrayList<double[]>(S);
128
129 for (int i = 0; i < S; ++i) {
130 dqs.add(new double[] { getDay(i), getQ(i) });
131 }
132
133 return interpolateX(dqs, q);
134 }
135
55 @Override 136 @Override
56 public void removeNaNs() { 137 public void removeNaNs() {
57 138
58 int dest = 0; 139 int dest = 0;
59 int N = ws.size(); 140 int N = ws.size();

http://dive4elements.wald.intevation.org