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