comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/W.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
children 672a41efe222
comparison
equal deleted inserted replaced
2181:38207b820dca 2182:5ff481ab24a1
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 W
10 extends NamedObjectImpl
11 {
12 private static Logger log = Logger.getLogger(W.class);
13
14 protected TDoubleArrayList ws;
15
16 public W() {
17 }
18
19 public W(String name) {
20 super(name);
21 }
22
23 public W(int capacity) {
24 this(capacity, "");
25 }
26
27 public W(int capacity, String name) {
28 super(name);
29 ws = new TDoubleArrayList(capacity);
30 }
31
32 public void add(double value) {
33 ws.add(value);
34 }
35
36 public int size() {
37 return ws.size();
38 }
39
40 public double getW(int idx) {
41 return ws.getQuick(idx);
42 }
43
44 public double [] getWs() {
45 return ws.toNativeArray();
46 }
47
48 public double [] get(int idx) {
49 return get(idx, new double [1]);
50 }
51
52 public double [] get(int idx, double [] dst) {
53 dst[0] = ws.getQuick(idx);
54 return dst;
55 }
56
57 public static void removeNaNs(TDoubleArrayList [] arrays) {
58
59 int dest = 0;
60
61 int A = arrays.length;
62 int N = arrays[0].size();
63
64 OUTER: for (int i = 0; i < N; ++i) {
65 for (int j = 0; j < A; ++j) {
66 TDoubleArrayList a = arrays[j];
67 double v = a.getQuick(i);
68 if (Double.isNaN(v)) {
69 continue OUTER;
70 }
71 a.setQuick(dest, v);
72 }
73 ++dest;
74 }
75
76 if (dest < N) {
77 for (int i = 0; i < A; ++i) {
78 arrays[i].remove(dest, N-dest);
79 }
80 }
81 }
82
83 public void removeNaNs() {
84 removeNaNs(new TDoubleArrayList [] { ws });
85 }
86
87 public boolean guessWaterIncreasing() {
88 return guessWaterIncreasing(0.05f);
89 }
90
91 public boolean guessWaterIncreasing(float factor) {
92 return DataUtil.guessWaterIncreasing(ws, factor);
93 }
94
95 public int [] longestIncreasingWRangeIndices() {
96 return longestIncreasingWRangeIndices(new int[2]);
97 }
98
99 public int [] longestIncreasingWRangeIndices(int [] bounds) {
100
101 int N = size();
102 int start = 0;
103 int stop = 0;
104
105 double lastW = Double.MAX_VALUE;
106
107 for (int i = 0; i < N; ++i) {
108 double v = ws.getQuick(i);
109 if (v <= lastW) {
110 if (stop-start > bounds[1]-bounds[0]) {
111 bounds[0] = start;
112 bounds[1] = stop;
113 if (log.isDebugEnabled()) {
114 log.debug("new range: " +
115 bounds[0] + " - " + bounds[1] + " (" +
116 ws.getQuick(bounds[0]) + ", " +
117 ws.getQuick(bounds[1]) + ")");
118
119 }
120 }
121 start = stop = i;
122 }
123 else {
124 stop = i;
125 }
126 lastW = v;
127 }
128
129 if (stop-start > bounds[1]-bounds[0]) {
130 bounds[0] = start;
131 bounds[1] = stop;
132 if (log.isDebugEnabled()) {
133 log.debug("new range @end: " +
134 bounds[0] + " - " + bounds[1] + " (" +
135 ws.getQuick(bounds[0]) + ", " +
136 ws.getQuick(bounds[1]) + ")");
137 }
138 }
139
140 return bounds;
141 }
142 }
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org