Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java @ 4627:d5821c6f0ab0
Fixed validation in parameter matrix panel. Single selections are allowed now.
Avoid adding duplicates of user selection in parameter matrix.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Mon, 03 Dec 2012 17:25:49 +0100 |
parents | d6d16b5ab2f0 |
children |
rev | line source |
---|---|
4625
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.model; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
2 |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
3 import java.util.Map; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
4 import java.util.SortedMap; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
5 import java.util.TreeMap; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
6 |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
7 |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
8 public class MovingAverage |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
9 { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
10 |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
11 public static double[][] simple(double[][] values, double radius) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
12 TreeMap<Double, Double> map = toMap(values); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
13 double[][] result = new double[values.length][values[0].length]; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
14 int ndx = 0; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
15 for (double x: map.keySet()) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
16 SortedMap<Double, Double> range = |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
17 map.subMap(x-radius, true, x+radius, true); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
18 double avg = 0d; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
19 for (Double v: range.values()) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
20 avg += v; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
21 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
22 avg /= range.size(); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
23 result[0][ndx] = x; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
24 result[1][ndx] = avg; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
25 ndx++; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
26 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
27 return result; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
28 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
29 |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
30 public static double[][] weighted(double[][] values, double radius) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
31 TreeMap<Double, Double> map = toMap(values); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
32 double[][] result = new double[values.length][values[0].length]; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
33 int ndx = 0; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
34 for (double x: map.keySet()) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
35 double avg = 0d; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
36 double weights = 0d; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
37 for (Map.Entry<Double, Double> e: |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
38 map.subMap(x-radius, false, x+radius, false).entrySet() |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
39 ) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
40 double weight = 1d - Math.abs(x - e.getKey())/radius; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
41 avg += weight*e.getValue(); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
42 weights += weight; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
43 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
44 avg /= weights; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
45 result[0][ndx] = x; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
46 result[1][ndx] = avg; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
47 ndx++; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
48 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
49 return result; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
50 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
51 |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
52 private static TreeMap<Double, Double> toMap(double[][] values) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
53 TreeMap<Double, Double> map = new TreeMap<Double, Double>(); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
54 for(int i = 0; i < values[0].length; i++) { |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
55 map.put(values[0][i], values[1][i]); |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
56 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
57 return map; |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
58 } |
d6d16b5ab2f0
New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
59 } |