Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/Segment.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/Segment.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts.model; | |
2 | |
3 import org.dive4elements.river.model.DischargeTable; | |
4 import org.dive4elements.river.model.Gauge; | |
5 import org.dive4elements.river.model.River; | |
6 | |
7 import org.dive4elements.river.utils.DoubleUtil; | |
8 | |
9 import java.io.Serializable; | |
10 | |
11 import java.util.ArrayList; | |
12 import java.util.Collections; | |
13 import java.util.Comparator; | |
14 import java.util.List; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 /** A Range with values and a reference point. */ | |
19 public class Segment | |
20 implements Serializable | |
21 { | |
22 private static Logger log = Logger.getLogger(Segment.class); | |
23 | |
24 public static final Comparator<Segment> REF_CMP = | |
25 new Comparator<Segment>() { | |
26 @Override | |
27 public int compare(Segment a, Segment b) { | |
28 double d = a.referencePoint - b.referencePoint; | |
29 if (d < 0d) return -1; | |
30 return d > 0d ? +1 : 0; | |
31 } | |
32 }; | |
33 | |
34 protected double from; | |
35 protected double to; | |
36 protected double [] values; | |
37 protected double [] backup; | |
38 protected double referencePoint; | |
39 | |
40 public Segment() { | |
41 } | |
42 | |
43 public Segment(double referencePoint) { | |
44 this.referencePoint = referencePoint; | |
45 } | |
46 | |
47 public Segment(double from, double to, double [] values) { | |
48 this.from = from; | |
49 this.to = to; | |
50 this.values = values; | |
51 } | |
52 | |
53 public boolean isUp() { | |
54 return from < to; | |
55 } | |
56 | |
57 /** Checks whether given km lies inside the to/from bounds of this segment. */ | |
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 /** Use DoubleUtil to parse Segments. */ | |
126 public static List<Segment> parseSegments(String input) { | |
127 | |
128 final List<Segment> segments = new ArrayList<Segment>(); | |
129 | |
130 DoubleUtil.parseSegments(input, new DoubleUtil.SegmentCallback() { | |
131 @Override | |
132 public void newSegment(double from, double to, double [] values) { | |
133 segments.add(new Segment(from, to, values)); | |
134 } | |
135 }); | |
136 | |
137 return segments; | |
138 } | |
139 | |
140 public static boolean setReferencePointConvertQ( | |
141 List<Segment> segments, | |
142 River river, | |
143 boolean isQ, | |
144 Calculation report | |
145 ) { | |
146 int numResults = -1; | |
147 | |
148 boolean success = true; | |
149 | |
150 // assign reference points | |
151 for (Segment segment: segments) { | |
152 Gauge gauge = river.maxOverlap(segment.getFrom(), segment.getTo()); | |
153 | |
154 if (gauge == null) { | |
155 log.warn("no gauge found. Defaults to mid point."); | |
156 segment.setReferencePoint( | |
157 0.5*(segment.getFrom()+segment.getTo())); | |
158 } | |
159 else { | |
160 double ref = gauge.getStation().doubleValue(); | |
161 log.debug( | |
162 "reference gauge: " + gauge.getName() + | |
163 " (km " + ref + ")"); | |
164 segment.setReferencePoint(ref); | |
165 } | |
166 | |
167 double [] values = segment.values; | |
168 | |
169 if (numResults == -1) { | |
170 numResults = values.length; | |
171 } | |
172 else if (numResults != values.length) { | |
173 log.warn("wrong length of values"); | |
174 return false; | |
175 } | |
176 | |
177 // convert to Q if needed | |
178 if (!isQ && gauge != null) { | |
179 | |
180 DischargeTable dt = gauge.fetchMasterDischargeTable(); | |
181 | |
182 //TODO: Change scale from 100 to 1 immediately after | |
183 // discharge table import changed to cm! | |
184 double [][] table = | |
185 DischargeTables.loadDischargeTableValues(dt, 100); | |
186 | |
187 // need the original values for naming | |
188 segment.backup(); | |
189 | |
190 for (int i = 0; i < values.length; ++i) { | |
191 //TODO: s.o. | |
192 double w = values[i]; /* / 100.0; */ | |
193 double [] qs = DischargeTables.getQsForW(table, w); | |
194 if (qs.length == 0) { | |
195 log.warn("No Qs found for W = " + values[i]); | |
196 report.addProblem("cannot.find.q.for.w", values[i]); | |
197 values[i] = Double.NaN; | |
198 success = false; | |
199 } | |
200 else { | |
201 values[i] = qs[0]; | |
202 if (qs.length > 1) { | |
203 log.warn( | |
204 "More than one Q found for W = " + values[i]); | |
205 } | |
206 } | |
207 } | |
208 } | |
209 } // for all segments | |
210 | |
211 Collections.sort(segments, Segment.REF_CMP); | |
212 | |
213 return success; | |
214 } | |
215 } | |
216 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |