Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeRange.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 08230c76cd92 |
children | c44ff50f4970 |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import java.util.ArrayList; | |
6 import java.util.HashMap; | |
7 import java.util.List; | |
8 import java.util.Map; | |
9 | |
10 import org.apache.log4j.Logger; | |
11 | |
12 public class GaugeRange | |
13 extends Range | |
14 { | |
15 private static Logger log = Logger.getLogger(GaugeRange.class); | |
16 | |
17 private static final class Sector implements Serializable { | |
18 | |
19 int sector; | |
20 double value; | |
21 | |
22 Sector(int sector, double value) { | |
23 this.sector = sector; | |
24 this.value = value; | |
25 } | |
26 } // class Sector | |
27 | |
28 protected int gaugeId; | |
29 | |
30 protected Map<String, Double> mainValues; | |
31 protected List<Sector> sectors; | |
32 | |
33 public GaugeRange() { | |
34 } | |
35 | |
36 public GaugeRange(double start, double end, int gaugeId) { | |
37 super(start, end); | |
38 this.gaugeId = gaugeId; | |
39 mainValues = new HashMap<String, Double>(); | |
40 sectors = new ArrayList<Sector>(3); | |
41 } | |
42 | |
43 public void addMainValue(String label, Double value) { | |
44 int idx = label.indexOf('('); | |
45 if (idx >= 0) { | |
46 label = label.substring(0, idx); | |
47 } | |
48 mainValues.put(label, value); | |
49 } | |
50 | |
51 protected Double getMainValue(String label) { | |
52 Double v = mainValues.get(label); | |
53 if (v == null) { | |
54 log.warn("Missing main value '" | |
55 + label + "' for gauge " + gaugeId); | |
56 } | |
57 return v; | |
58 } | |
59 | |
60 public void buildClasses() { | |
61 Double mnq = getMainValue("MNQ"); | |
62 Double mq = getMainValue("MQ"); | |
63 Double mhq = getMainValue("MHQ"); | |
64 Double hq5 = getMainValue("HQ5"); | |
65 | |
66 Double [][] pairs = { | |
67 { mnq, mq }, | |
68 { mq, mhq }, | |
69 { hq5, hq5 } }; | |
70 | |
71 for (int c = 0; c < pairs.length; ++c) { | |
72 Double [] pair = pairs[c]; | |
73 if (pair[0] != null && pair[1] != null) { | |
74 double value = 0.5*(pair[0] + pair[1]); | |
75 sectors.add(new Sector(c, value)); | |
76 } | |
77 } | |
78 } | |
79 | |
80 public double getSectorBorder(int sector) { | |
81 for (Sector s: sectors) { | |
82 if (s.sector == sector) { | |
83 return s.value; | |
84 } | |
85 } | |
86 return Double.NaN; | |
87 } | |
88 | |
89 public int classify(double value) { | |
90 for (Sector sector: sectors) { | |
91 if (value < sector.value) { | |
92 return sector.sector; | |
93 } | |
94 } | |
95 return sectors.size(); | |
96 } | |
97 | |
98 public String toString() { | |
99 StringBuilder sb = new StringBuilder("sectors: ["); | |
100 | |
101 for (int i = 0, S = sectors.size(); i < S; ++i) { | |
102 if (i > 0) sb.append(", "); | |
103 Sector s = sectors.get(i); | |
104 sb.append(s.sector).append(": ").append(s.value);; | |
105 } | |
106 | |
107 sb.append("] mainvalues: ").append(mainValues); | |
108 | |
109 return sb.toString(); | |
110 } | |
111 } | |
112 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |