Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | fc351f12b906 |
children | 1df6984628c3 |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import de.intevation.flys.model.DischargeTable; | |
4 import de.intevation.flys.model.Gauge; | |
5 import de.intevation.flys.model.River; | |
6 | |
7 import de.intevation.flys.utils.DoubleUtil; | |
8 | |
9 import gnu.trove.TDoubleArrayList; | |
10 | |
11 import java.io.Serializable; | |
12 | |
13 import java.util.ArrayList; | |
14 import java.util.Collections; | |
15 import java.util.Comparator; | |
16 import java.util.List; | |
17 | |
18 import org.apache.log4j.Logger; | |
19 | |
20 public class Segment | |
21 implements Serializable | |
22 { | |
23 private static Logger log = Logger.getLogger(Segment.class); | |
24 | |
25 public static final Comparator<Segment> REF_CMP = | |
26 new Comparator<Segment>() { | |
27 @Override | |
28 public int compare(Segment a, Segment b) { | |
29 double d = a.referencePoint - b.referencePoint; | |
30 if (d < 0d) return -1; | |
31 return d > 0d ? +1 : 0; | |
32 } | |
33 }; | |
34 | |
35 protected double from; | |
36 protected double to; | |
37 protected double [] values; | |
38 protected double [] backup; | |
39 protected double referencePoint; | |
40 | |
41 public Segment() { | |
42 } | |
43 | |
44 public Segment(double referencePoint) { | |
45 this.referencePoint = referencePoint; | |
46 } | |
47 | |
48 public Segment(double from, double to, double [] values) { | |
49 this.from = from; | |
50 this.to = to; | |
51 this.values = values; | |
52 } | |
53 | |
54 public boolean isUp() { | |
55 return from < to; | |
56 } | |
57 | |
58 public boolean inside(double km) { | |
59 return from < to | |
60 ? km >= from && km <= to | |
61 : km >= to && km <= from; | |
62 } | |
63 | |
64 @Override | |
65 public String toString() { | |
66 StringBuilder sb = new StringBuilder("Segment: ["); | |
67 sb.append("from: ").append(from).append("; to: ") | |
68 .append(to) | |
69 .append("; ref: ").append(referencePoint) | |
70 .append("; values: ("); | |
71 for (int i = 0; i < values.length; ++i) { | |
72 if (i > 0) sb.append(", "); | |
73 sb.append(values[i]); | |
74 } | |
75 sb.append(")]"); | |
76 return sb.toString(); | |
77 } | |
78 | |
79 public void setFrom(double from) { | |
80 this.from = from; | |
81 } | |
82 | |
83 public void backup() { | |
84 backup = values != null | |
85 ? (double [])values.clone() | |
86 : null; | |
87 } | |
88 | |
89 public double [] getBackup() { | |
90 return backup; | |
91 } | |
92 | |
93 public double getFrom() { | |
94 return from; | |
95 } | |
96 | |
97 public void setTo(double to) { | |
98 this.to = to; | |
99 } | |
100 | |
101 public double getTo() { | |
102 return to; | |
103 } | |
104 | |
105 public void setValues(double [] values) { | |
106 this.values = values; | |
107 } | |
108 | |
109 public double [] getValues() { | |
110 return values; | |
111 } | |
112 | |
113 public int numValues() { | |
114 return values.length; | |
115 } | |
116 | |
117 public void setReferencePoint(double referencePoint) { | |
118 this.referencePoint = referencePoint; | |
119 } | |
120 | |
121 public double getReferencePoint() { | |
122 return referencePoint; | |
123 } | |
124 | |
125 public static List<Segment> parseSegments(String input) { | |
126 | |
127 ArrayList<Segment> segments = new ArrayList<Segment>(); | |
128 | |
129 TDoubleArrayList vs = new TDoubleArrayList(); | |
130 | |
131 for (String segmentStr: input.split(":")) { | |
132 String [] parts = segmentStr.split(";"); | |
133 if (parts.length < 3) { | |
134 log.warn("invalid segment: '" + segmentStr + "'"); | |
135 continue; | |
136 } | |
137 try { | |
138 double from = Double.parseDouble(parts[0].trim()); | |
139 double to = Double.parseDouble(parts[1].trim()); | |
140 | |
141 vs.clear(); | |
142 | |
143 for (String valueStr: parts[2].split(",")) { | |
144 vs.add(DoubleUtil.round( | |
145 Double.parseDouble(valueStr.trim()))); | |
146 } | |
147 | |
148 double [] values = vs.toNativeArray(); | |
149 segments.add(new Segment(from, to, values)); | |
150 } | |
151 catch (NumberFormatException nfe) { | |
152 log.warn("invalid segment: '" + segmentStr + "'"); | |
153 } | |
154 } | |
155 | |
156 return segments; | |
157 } | |
158 | |
159 public static boolean setReferencePointConvertQ( | |
160 List<Segment> segments, | |
161 River river, | |
162 boolean isQ, | |
163 Calculation report | |
164 ) { | |
165 int numResults = -1; | |
166 | |
167 boolean success = true; | |
168 | |
169 // assign reference points | |
170 for (Segment segment: segments) { | |
171 Gauge gauge = river.maxOverlap(segment.getFrom(), segment.getTo()); | |
172 | |
173 if (gauge == null) { | |
174 log.warn("no gauge found. Defaults to mid point."); | |
175 segment.setReferencePoint( | |
176 0.5*(segment.getFrom()+segment.getTo())); | |
177 } | |
178 else { | |
179 double ref = gauge.getStation().doubleValue(); | |
180 log.debug( | |
181 "reference gauge: " + gauge.getName() + | |
182 " (km " + ref + ")"); | |
183 segment.setReferencePoint(ref); | |
184 } | |
185 | |
186 double [] values = segment.values; | |
187 | |
188 if (numResults == -1) { | |
189 numResults = values.length; | |
190 } | |
191 else if (numResults != values.length) { | |
192 log.warn("wrong length of values"); | |
193 return false; | |
194 } | |
195 | |
196 // convert to Q if needed | |
197 if (!isQ && gauge != null) { | |
198 | |
199 DischargeTable dt = gauge.fetchMasterDischargeTable(); | |
200 | |
201 double [][] table = | |
202 DischargeTables.loadDischargeTableValues(dt, 1); | |
203 | |
204 // need the original values for naming | |
205 segment.backup(); | |
206 | |
207 for (int i = 0; i < values.length; ++i) { | |
208 double w = values[i] / 100.0; | |
209 double [] qs = DischargeTables.getQsForW(table, w); | |
210 if (qs.length == 0) { | |
211 log.warn("No Qs found for W = " + values[i]); | |
212 report.addProblem("cannot.find.w.for.q", values[i]); | |
213 values[i] = Double.NaN; | |
214 success = false; | |
215 } | |
216 else { | |
217 values[i] = qs[0]; | |
218 if (qs.length > 1) { | |
219 log.warn( | |
220 "More than one Q found for W = " + values[i]); | |
221 } | |
222 } | |
223 } | |
224 } | |
225 } // for all segments | |
226 | |
227 Collections.sort(segments, Segment.REF_CMP); | |
228 | |
229 return success; | |
230 } | |
231 } | |
232 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |