Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeRange.java @ 3651:06a65baae494
merged flys-artifacts/2.9
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:43 +0200 |
parents | c44ff50f4970 |
children | 05eeedc5b156 |
comparison
equal
deleted
inserted
replaced
3549:6a8f83c538e3 | 3651:06a65baae494 |
---|---|
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 String name; | |
29 | |
30 protected int gaugeId; | |
31 | |
32 protected Map<String, Double> mainValues; | |
33 protected List<Sector> sectors; | |
34 | |
35 public GaugeRange() { | |
36 } | |
37 | |
38 public GaugeRange(double start, double end, int gaugeId) { | |
39 this(start, end, null, gaugeId); | |
40 } | |
41 | |
42 public GaugeRange( | |
43 double start, | |
44 double end, | |
45 String name, | |
46 int gaugeId | |
47 ) { | |
48 super(start, end); | |
49 this.name = name; | |
50 this.gaugeId = gaugeId; | |
51 mainValues = new HashMap<String, Double>(); | |
52 sectors = new ArrayList<Sector>(3); | |
53 } | |
54 | |
55 public void addMainValue(String label, Double value) { | |
56 int idx = label.indexOf('('); | |
57 if (idx >= 0) { | |
58 label = label.substring(0, idx); | |
59 } | |
60 mainValues.put(label, value); | |
61 } | |
62 | |
63 protected Double getMainValue(String label) { | |
64 Double v = mainValues.get(label); | |
65 if (v == null) { | |
66 log.warn("Missing main value '" | |
67 + label + "' for gauge " + gaugeId); | |
68 } | |
69 return v; | |
70 } | |
71 | |
72 public void buildClasses() { | |
73 Double mnq = getMainValue("MNQ"); | |
74 Double mq = getMainValue("MQ"); | |
75 Double mhq = getMainValue("MHQ"); | |
76 Double hq5 = getMainValue("HQ5"); | |
77 | |
78 Double [][] pairs = { | |
79 { mnq, mq }, | |
80 { mq, mhq }, | |
81 { hq5, hq5 } }; | |
82 | |
83 for (int c = 0; c < pairs.length; ++c) { | |
84 Double [] pair = pairs[c]; | |
85 if (pair[0] != null && pair[1] != null) { | |
86 double value = 0.5*(pair[0] + pair[1]); | |
87 sectors.add(new Sector(c, value)); | |
88 } | |
89 } | |
90 } | |
91 | |
92 public double getSectorBorder(int sector) { | |
93 for (Sector s: sectors) { | |
94 if (s.sector == sector) { | |
95 return s.value; | |
96 } | |
97 } | |
98 return Double.NaN; | |
99 } | |
100 | |
101 public int classify(double value) { | |
102 for (Sector sector: sectors) { | |
103 if (value < sector.value) { | |
104 return sector.sector; | |
105 } | |
106 } | |
107 return sectors.size(); | |
108 } | |
109 | |
110 public String getName() { | |
111 return name; | |
112 } | |
113 | |
114 public void setName(String name) { | |
115 this.name = name; | |
116 } | |
117 | |
118 public String toString() { | |
119 StringBuilder sb = new StringBuilder("sectors: ["); | |
120 | |
121 for (int i = 0, S = sectors.size(); i < S; ++i) { | |
122 if (i > 0) sb.append(", "); | |
123 Sector s = sectors.get(i); | |
124 sb.append(s.sector).append(": ").append(s.value);; | |
125 } | |
126 | |
127 sb.append("] mainvalues: ").append(mainValues); | |
128 | |
129 return sb.toString(); | |
130 } | |
131 } | |
132 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |