comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java @ 3814:8083f6384023

merged flys-artifacts/pre2.6-2012-01-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:56 +0200
parents 03fbf1b30e72
children bda04ae1154f
comparison
equal deleted inserted replaced
1491:2a00f4849738 3814:8083f6384023
1 package de.intevation.flys.artifacts.model;
2
3 import de.intevation.flys.utils.DataUtil;
4
5 import gnu.trove.TDoubleArrayList;
6
7 import org.apache.log4j.Logger;
8
9 public class WQ
10 extends NamedObjectImpl
11 {
12 private static Logger logger = Logger.getLogger(WQ.class);
13
14 // TODO: s/w/ws/g
15 protected TDoubleArrayList w;
16
17 // TODO: s/q/qs/g
18 protected TDoubleArrayList q;
19
20 public WQ() {
21 this("");
22 }
23
24 public WQ(String name) {
25 w = new TDoubleArrayList();
26 q = new TDoubleArrayList();
27 }
28
29 public WQ(int capacity) {
30 this(capacity, "");
31 }
32
33
34 public WQ(int capacity, String name) {
35 super(name);
36 w = new TDoubleArrayList(capacity);
37 q = new TDoubleArrayList(capacity);
38 }
39
40 public WQ(double [] qs, double [] ws) {
41 this(qs, ws, "");
42 }
43
44 public WQ(double [] qs, double [] ws, String name) {
45 super(name);
46 w = new TDoubleArrayList(ws);
47 q = new TDoubleArrayList(qs);
48 }
49
50 public void add(double w, double q) {
51 this.w.add(w);
52 this.q.add(q);
53 }
54
55 public int size() {
56 return w.size();
57 }
58
59 public double getW(int idx) {
60 return w.getQuick(idx);
61 }
62
63 public double getQ(int idx) {
64 return q.getQuick(idx);
65 }
66
67 public double [] get(int idx) {
68 return get(idx, new double [2]);
69 }
70
71 public double [] get(int idx, double [] dst) {
72 dst[0] = w.getQuick(idx);
73 dst[1] = q.getQuick(idx);
74 return dst;
75 }
76
77 public double [] getWs() {
78 return w.toNativeArray();
79 }
80
81 public double [] getQs() {
82 return q.toNativeArray();
83 }
84
85 public static void removeNaNs(TDoubleArrayList [] arrays) {
86
87 int dest = 0;
88
89 int A = arrays.length;
90 int N = arrays[0].size();
91
92 OUTER: for (int i = 0; i < N; ++i) {
93 for (int j = 0; j < A; ++j) {
94 TDoubleArrayList a = arrays[j];
95 double v = a.getQuick(i);
96 if (Double.isNaN(v)) {
97 continue OUTER;
98 }
99 a.setQuick(dest, v);
100 }
101 ++dest;
102 }
103
104 if (dest < N) {
105 for (int i = 0; i < A; ++i) {
106 arrays[i].remove(dest, N-dest);
107 }
108 }
109 }
110
111 public void removeNaNs() {
112 removeNaNs(new TDoubleArrayList [] { w, q });
113 }
114
115 public boolean guessWaterIncreasing() {
116 return guessWaterIncreasing(0.05f);
117 }
118
119 public boolean guessWaterIncreasing(float factor) {
120 return DataUtil.guessWaterIncreasing(w, factor);
121 }
122
123 public int [] longestIncreasingWRangeIndices() {
124 return longestIncreasingWRangeIndices(new int[2]);
125 }
126
127 public int [] longestIncreasingWRangeIndices(int [] bounds) {
128
129 int N = size();
130 int start = 0;
131 int stop = 0;
132
133 double lastW = Double.MAX_VALUE;
134
135 for (int i = 0; i < N; ++i) {
136 double v = w.getQuick(i);
137 if (v <= lastW) {
138 if (stop-start > bounds[1]-bounds[0]) {
139 bounds[0] = start;
140 bounds[1] = stop;
141 if (logger.isDebugEnabled()) {
142 logger.debug("new range: " +
143 bounds[0] + " - " + bounds[1] + " (" +
144 w.getQuick(bounds[0]) + ", " +
145 w.getQuick(bounds[1]) + ")");
146
147 }
148 }
149 start = stop = i;
150 }
151 else {
152 stop = i;
153 }
154 lastW = v;
155 }
156
157 if (stop-start > bounds[1]-bounds[0]) {
158 bounds[0] = start;
159 bounds[1] = stop;
160 if (logger.isDebugEnabled()) {
161 logger.debug("new range @end: " +
162 bounds[0] + " - " + bounds[1] + " (" +
163 w.getQuick(bounds[0]) + ", " +
164 w.getQuick(bounds[1]) + ")");
165
166 }
167 }
168
169 return bounds;
170 }
171 }
172 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org