Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java @ 2182:5ff481ab24a1
Refactored class hierachy to integrate model for W~W.
flys-artifacts/trunk@3786 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 27 Jan 2012 10:45:34 +0000 |
parents | bda04ae1154f |
children | bcf25d8c183e |
comparison
equal
deleted
inserted
replaced
2181:38207b820dca | 2182:5ff481ab24a1 |
---|---|
1 package de.intevation.flys.artifacts.model; | 1 package de.intevation.flys.artifacts.model; |
2 | 2 |
3 import java.util.regex.Matcher; | 3 import java.util.regex.Matcher; |
4 import java.util.regex.Pattern; | 4 import java.util.regex.Pattern; |
5 | |
6 import de.intevation.flys.utils.DataUtil; | |
7 | 5 |
8 import gnu.trove.TDoubleArrayList; | 6 import gnu.trove.TDoubleArrayList; |
9 | 7 |
10 import org.apache.log4j.Logger; | 8 import org.apache.log4j.Logger; |
11 | 9 |
12 public class WQ | 10 public class WQ |
13 extends NamedObjectImpl | 11 extends W |
14 { | 12 { |
15 public static final Pattern NUMBERS_PATTERN = | 13 public static final Pattern NUMBERS_PATTERN = |
16 Pattern.compile("\\D*(\\d++.\\d*)\\D*"); | 14 Pattern.compile("\\D*(\\d++.\\d*)\\D*"); |
17 | 15 |
16 private static Logger log = Logger.getLogger(WQ.class); | |
18 | 17 |
19 private static Logger logger = Logger.getLogger(WQ.class); | 18 protected TDoubleArrayList qs; |
20 | |
21 // TODO: s/w/ws/g | |
22 protected TDoubleArrayList w; | |
23 | |
24 // TODO: s/q/qs/g | |
25 protected TDoubleArrayList q; | |
26 | 19 |
27 public WQ() { | 20 public WQ() { |
28 this(""); | 21 this(""); |
29 } | 22 } |
30 | 23 |
31 public WQ(String name) { | 24 public WQ(String name) { |
32 w = new TDoubleArrayList(); | 25 super(name); |
33 q = new TDoubleArrayList(); | 26 qs = new TDoubleArrayList(); |
34 } | 27 } |
35 | 28 |
36 public WQ(int capacity) { | 29 public WQ(int capacity) { |
37 this(capacity, ""); | 30 this(capacity, ""); |
38 } | 31 } |
39 | 32 |
40 | 33 |
41 public WQ(int capacity, String name) { | 34 public WQ(int capacity, String name) { |
42 super(name); | 35 super(capacity, name); |
43 w = new TDoubleArrayList(capacity); | 36 qs = new TDoubleArrayList(capacity); |
44 q = new TDoubleArrayList(capacity); | |
45 } | 37 } |
46 | 38 |
47 public WQ(double [] qs, double [] ws) { | 39 public WQ(double [] qs, double [] ws) { |
48 this(qs, ws, ""); | 40 this(qs, ws, ""); |
49 } | 41 } |
50 | 42 |
51 public WQ(double [] qs, double [] ws, String name) { | 43 public WQ(double [] qs, double [] ws, String name) { |
52 super(name); | 44 super(name); |
53 w = new TDoubleArrayList(ws); | 45 this.ws = new TDoubleArrayList(ws); |
54 q = new TDoubleArrayList(qs); | 46 this.qs = new TDoubleArrayList(qs); |
55 } | 47 } |
56 | 48 |
57 | 49 |
58 public Double getRawValue() { | 50 public Double getRawValue() { |
59 if (name == null || name.length() == 0) { | 51 if (name == null || name.length() == 0) { |
75 } | 67 } |
76 | 68 |
77 return null; | 69 return null; |
78 } | 70 } |
79 | 71 |
80 | |
81 public void add(double w, double q) { | 72 public void add(double w, double q) { |
82 this.w.add(w); | 73 ws.add(w); |
83 this.q.add(q); | 74 qs.add(q); |
84 } | |
85 | |
86 public int size() { | |
87 return w.size(); | |
88 } | |
89 | |
90 public double getW(int idx) { | |
91 return w.getQuick(idx); | |
92 } | 75 } |
93 | 76 |
94 public double getQ(int idx) { | 77 public double getQ(int idx) { |
95 return q.getQuick(idx); | 78 return qs.getQuick(idx); |
96 } | 79 } |
97 | 80 |
81 @Override | |
98 public double [] get(int idx) { | 82 public double [] get(int idx) { |
99 return get(idx, new double [2]); | 83 return get(idx, new double [2]); |
100 } | 84 } |
101 | 85 |
86 @Override | |
102 public double [] get(int idx, double [] dst) { | 87 public double [] get(int idx, double [] dst) { |
103 dst[0] = w.getQuick(idx); | 88 dst[0] = ws.getQuick(idx); |
104 dst[1] = q.getQuick(idx); | 89 dst[1] = qs.getQuick(idx); |
105 return dst; | 90 return dst; |
106 } | 91 } |
107 | 92 |
108 public double [] getWs() { | 93 public double [] getQs() { |
109 return w.toNativeArray(); | 94 return qs.toNativeArray(); |
110 } | 95 } |
111 | 96 |
112 public double [] getQs() { | 97 @Override |
113 return q.toNativeArray(); | |
114 } | |
115 | |
116 public static void removeNaNs(TDoubleArrayList [] arrays) { | |
117 | |
118 int dest = 0; | |
119 | |
120 int A = arrays.length; | |
121 int N = arrays[0].size(); | |
122 | |
123 OUTER: for (int i = 0; i < N; ++i) { | |
124 for (int j = 0; j < A; ++j) { | |
125 TDoubleArrayList a = arrays[j]; | |
126 double v = a.getQuick(i); | |
127 if (Double.isNaN(v)) { | |
128 continue OUTER; | |
129 } | |
130 a.setQuick(dest, v); | |
131 } | |
132 ++dest; | |
133 } | |
134 | |
135 if (dest < N) { | |
136 for (int i = 0; i < A; ++i) { | |
137 arrays[i].remove(dest, N-dest); | |
138 } | |
139 } | |
140 } | |
141 | |
142 public void removeNaNs() { | 98 public void removeNaNs() { |
143 removeNaNs(new TDoubleArrayList [] { w, q }); | 99 removeNaNs(new TDoubleArrayList [] { ws, qs }); |
144 } | |
145 | |
146 public boolean guessWaterIncreasing() { | |
147 return guessWaterIncreasing(0.05f); | |
148 } | |
149 | |
150 public boolean guessWaterIncreasing(float factor) { | |
151 return DataUtil.guessWaterIncreasing(w, factor); | |
152 } | |
153 | |
154 public int [] longestIncreasingWRangeIndices() { | |
155 return longestIncreasingWRangeIndices(new int[2]); | |
156 } | |
157 | |
158 public int [] longestIncreasingWRangeIndices(int [] bounds) { | |
159 | |
160 int N = size(); | |
161 int start = 0; | |
162 int stop = 0; | |
163 | |
164 double lastW = Double.MAX_VALUE; | |
165 | |
166 for (int i = 0; i < N; ++i) { | |
167 double v = w.getQuick(i); | |
168 if (v <= lastW) { | |
169 if (stop-start > bounds[1]-bounds[0]) { | |
170 bounds[0] = start; | |
171 bounds[1] = stop; | |
172 if (logger.isDebugEnabled()) { | |
173 logger.debug("new range: " + | |
174 bounds[0] + " - " + bounds[1] + " (" + | |
175 w.getQuick(bounds[0]) + ", " + | |
176 w.getQuick(bounds[1]) + ")"); | |
177 | |
178 } | |
179 } | |
180 start = stop = i; | |
181 } | |
182 else { | |
183 stop = i; | |
184 } | |
185 lastW = v; | |
186 } | |
187 | |
188 if (stop-start > bounds[1]-bounds[0]) { | |
189 bounds[0] = start; | |
190 bounds[1] = stop; | |
191 if (logger.isDebugEnabled()) { | |
192 logger.debug("new range @end: " + | |
193 bounds[0] + " - " + bounds[1] + " (" + | |
194 w.getQuick(bounds[0]) + ", " + | |
195 w.getQuick(bounds[1]) + ")"); | |
196 | |
197 } | |
198 } | |
199 | |
200 return bounds; | |
201 } | 100 } |
202 } | 101 } |
203 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |