Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/MainValuesArtifact.java @ 8331:27d42c9ee367
Main values: Reduce code duplication and correct logic to specify whether we are at gauge or not.
author | "Tom Gottfried <tom@intevation.de>" |
---|---|
date | Fri, 26 Sep 2014 09:44:48 +0200 |
parents | c086b06b81e5 |
children | be7864b4fabb |
comparison
equal
deleted
inserted
replaced
8330:e87a993c6611 | 8331:27d42c9ee367 |
---|---|
30 | 30 |
31 import org.dive4elements.river.model.Gauge; | 31 import org.dive4elements.river.model.Gauge; |
32 import org.dive4elements.river.model.MainValue; | 32 import org.dive4elements.river.model.MainValue; |
33 import org.dive4elements.river.model.River; | 33 import org.dive4elements.river.model.River; |
34 | 34 |
35 import org.dive4elements.river.artifacts.access.RiverAccess; | |
35 import org.dive4elements.river.artifacts.access.RangeAccess; | 36 import org.dive4elements.river.artifacts.access.RangeAccess; |
36 import org.dive4elements.river.artifacts.model.Calculation; | 37 import org.dive4elements.river.artifacts.model.Calculation; |
37 import org.dive4elements.river.artifacts.model.FacetTypes; | 38 import org.dive4elements.river.artifacts.model.FacetTypes; |
38 import org.dive4elements.river.artifacts.model.MainValuesQFacet; | 39 import org.dive4elements.river.artifacts.model.MainValuesQFacet; |
39 import org.dive4elements.river.artifacts.model.MainValuesWFacet; | 40 import org.dive4elements.river.artifacts.model.MainValuesWFacet; |
41 import org.dive4elements.river.artifacts.model.WstValueTable; | 42 import org.dive4elements.river.artifacts.model.WstValueTable; |
42 import org.dive4elements.river.artifacts.model.WstValueTableFactory; | 43 import org.dive4elements.river.artifacts.model.WstValueTableFactory; |
43 | 44 |
44 import org.dive4elements.river.artifacts.states.StaticState; | 45 import org.dive4elements.river.artifacts.states.StaticState; |
45 import org.dive4elements.river.artifacts.resources.Resources; | 46 import org.dive4elements.river.artifacts.resources.Resources; |
46 | |
47 import org.dive4elements.river.utils.RiverUtils; | |
48 | 47 |
49 | 48 |
50 /** | 49 /** |
51 * Artifact to access main and extreme values of a river. | 50 * Artifact to access main and extreme values of a river. |
52 * This artifact neglects (Static)D4EArtifacts capabilities of interaction | 51 * This artifact neglects (Static)D4EArtifacts capabilities of interaction |
121 super.setup(identifier, factory, context, callMeta, data, loadFacets); | 120 super.setup(identifier, factory, context, callMeta, data, loadFacets); |
122 String restriction = getDatacageIDValue(data); | 121 String restriction = getDatacageIDValue(data); |
123 log.debug("mainvalue restriction " + restriction); | 122 log.debug("mainvalue restriction " + restriction); |
124 boolean restricted = restriction.endsWith("q") || restriction.endsWith("w"); | 123 boolean restricted = restriction.endsWith("q") || restriction.endsWith("w"); |
125 if (!restricted || restriction.endsWith("q")) { | 124 if (!restricted || restriction.endsWith("q")) { |
126 Facet qfacet0 = new MainValuesQFacet( | 125 fs.add(new MainValuesQFacet( |
127 DURATION_MAINVALUES_Q, | 126 MAINVALUES_Q, |
128 Resources.getMsg( | 127 Resources.getMsg( |
129 callMeta, | 128 callMeta, |
130 "facet.discharge_curves.mainvalues.q"), | 129 "facet.discharge_curves.mainvalues.q"))); |
131 false); | |
132 Facet qfacet1 = new MainValuesQFacet( | |
133 COMPUTED_DISCHARGE_MAINVALUES_Q, | |
134 Resources.getMsg( | |
135 callMeta, | |
136 "facet.discharge_curves.mainvalues.q"), | |
137 false); | |
138 Facet qfacet2 = new MainValuesQFacet( | |
139 MAINVALUES_Q, | |
140 Resources.getMsg( | |
141 callMeta, | |
142 "facet.discharge_curves.mainvalues.q"), | |
143 true); | |
144 Facet qfacet3 = new MainValuesQFacet( | |
145 HISTORICAL_DISCHARGE_MAINVALUES_Q, | |
146 Resources.getMsg( | |
147 callMeta, | |
148 "historical_discharge.mainvalues.q"), | |
149 false); | |
150 | |
151 fs.add(qfacet0); | |
152 fs.add(qfacet1); | |
153 fs.add(qfacet2); | |
154 fs.add(qfacet3); | |
155 } | 130 } |
156 if (!restricted || restriction.endsWith("w")) { | 131 if (!restricted || restriction.endsWith("w")) { |
157 Facet wfacet1 = new MainValuesWFacet( | 132 fs.add(new MainValuesWFacet( |
158 COMPUTED_DISCHARGE_MAINVALUES_W, | 133 MAINVALUES_W, |
159 Resources.getMsg( | 134 Resources.getMsg( |
160 callMeta, | 135 callMeta, |
161 "facet.discharge_curves.mainvalues.w"), | 136 "facet.discharge_curves.mainvalues.w"))); |
162 false); | |
163 Facet wfacet2 = new MainValuesWFacet( | |
164 MAINVALUES_W, | |
165 Resources.getMsg( | |
166 callMeta, | |
167 "facet.discharge_curves.mainvalues.w"), | |
168 true); | |
169 Facet wfacet3 = new MainValuesWFacet( | |
170 HISTORICAL_DISCHARGE_MAINVALUES_W, | |
171 Resources.getMsg( | |
172 callMeta, | |
173 "historical_discharge.mainvalues.w"), | |
174 true); | |
175 fs.add(wfacet1); | |
176 fs.add(wfacet2); | |
177 fs.add(wfacet3); | |
178 } | 137 } |
179 } | 138 } |
180 | 139 |
181 | 140 |
182 /** | 141 /** |
236 /** Get important data from the 'calling' artifact. */ | 195 /** Get important data from the 'calling' artifact. */ |
237 @Override | 196 @Override |
238 protected void initialize(Artifact artifact, Object context, CallMeta meta) { | 197 protected void initialize(Artifact artifact, Object context, CallMeta meta) { |
239 log.debug("MainValuesArtifact.initialize"); | 198 log.debug("MainValuesArtifact.initialize"); |
240 D4EArtifact winfo = (D4EArtifact) artifact; | 199 D4EArtifact winfo = (D4EArtifact) artifact; |
241 RangeAccess rangeAccess = new RangeAccess(winfo); | 200 River river = new RiverAccess(winfo).getRiver(); |
242 double [] locations = rangeAccess.getKmRange(); | 201 double [] locations = new RangeAccess(winfo).getKmRange(); |
243 | 202 |
244 if (locations != null) { | 203 if (locations != null) { |
245 double location = locations[0]; | 204 double location = locations[0]; |
246 addData("ld_locations", new DefaultStateData("ld_locations", null, null, | 205 addData("ld_locations", new DefaultStateData("ld_locations", null, null, |
247 String.valueOf(location))); | 206 String.valueOf(location))); |
318 * Access the Gauge that the mainvalues are taken from. | 277 * Access the Gauge that the mainvalues are taken from. |
319 * @return Gauge that main values are taken from or null in case of | 278 * @return Gauge that main values are taken from or null in case of |
320 * invalid parameterization. | 279 * invalid parameterization. |
321 */ | 280 */ |
322 protected Gauge getGauge(double km) { | 281 protected Gauge getGauge(double km) { |
323 River river = RiverUtils.getRiver(this); | 282 River river = new RiverAccess((D4EArtifact)this).getRiver(); |
324 | 283 |
325 if (river == null) { | 284 if (river == null) { |
326 log.error("River is null"); | 285 log.error("River is null"); |
327 return null; | 286 return null; |
328 } | 287 } |
329 | 288 |
330 return river.determineGaugeByPosition(km); | 289 return river.determineGaugeByPosition(km); |
331 } | 290 } |
332 | 291 |
333 /** | 292 /** |
334 * Access the Gauge that the mainvalues are taken from. | |
335 * @return Gauge that main values are taken from or null in case of | |
336 * invalid parameterization. | |
337 */ | |
338 protected Gauge getGauge() { | |
339 River river = RiverUtils.getRiver(this); | |
340 | |
341 // TODO use helper to get location as double | |
342 String locationStr = getDataAsString("ld_locations"); | |
343 | |
344 if (river == null) { | |
345 log.error("River is null"); | |
346 return null; | |
347 } | |
348 | |
349 if (locationStr == null) { | |
350 log.error("Locationstr is null"); | |
351 return null; | |
352 } | |
353 | |
354 double location = Double.parseDouble(locationStr); | |
355 | |
356 return river.determineGaugeByPosition(location); | |
357 } | |
358 | |
359 | |
360 /** | |
361 * Get current location. | 293 * Get current location. |
362 * @return the location. | 294 * @return the location. |
363 */ | 295 */ |
364 public double getLocation() { | 296 public double getLocation() { |
365 double location = Double.parseDouble(getDataAsString("ld_locations")); | 297 double location = Double.parseDouble(getDataAsString("ld_locations")); |
367 } | 299 } |
368 | 300 |
369 | 301 |
370 /** | 302 /** |
371 * Get a list of "Q" main values. | 303 * Get a list of "Q" main values. |
304 * @param Array of length 1 (isn't it lovely?) giving the station for | |
305 * which the main values should be returned | |
372 * @return list of Q main values. | 306 * @return list of Q main values. |
373 */ | 307 */ |
374 public List<NamedDouble> getMainValuesQ(double[] kms) { | 308 public List<NamedDouble> getMainValuesQ(double[] kms, Object pnpObject) { |
375 log.debug("MainValuesArtifact.getMainValuesQ(d)"); | 309 if (kms.length > 1) { |
310 log.error("How did you dare to give an array of lenght >1! " + | |
311 "DAS GEHT GARNICHT!!!! (we'll just take the first value)"); | |
312 } | |
376 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | 313 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); |
377 boolean atGauge = false; | |
378 Gauge gauge = getGauge(kms[0]); | 314 Gauge gauge = getGauge(kms[0]); |
379 WstValueTable interpolator = WstValueTableFactory.getTable(RiverUtils.getRiver(this)); | 315 River river = new RiverAccess((D4EArtifact)this).getRiver(); |
316 WstValueTable interpolator = WstValueTableFactory.getTable(river); | |
380 Calculation c = new Calculation(); | 317 Calculation c = new Calculation(); |
381 double w_out[] = {0.0f}; | 318 double w_out[] = {0.0f}; |
382 double q_out[] = {0.0f}; | 319 double q_out[] = {0.0f}; |
320 double pnp = Double.NaN; | |
321 | |
383 if (gauge != null) { | 322 if (gauge != null) { |
384 double gaugeStation = gauge.getStation().doubleValue(); | 323 double gaugeStation = gauge.getStation().doubleValue(); |
385 atGauge = Math.abs(kms[0] - gaugeStation) < 1e-4; | |
386 List<MainValue> orig = gauge.getMainValues(); | 324 List<MainValue> orig = gauge.getMainValues(); |
325 if (pnpObject instanceof Number) { | |
326 pnp = Double.valueOf(pnpObject.toString()); | |
327 } | |
328 | |
387 for (MainValue mv : orig) { | 329 for (MainValue mv : orig) { |
388 if (mv.getMainValue().getType().getName().equals("Q")) { | 330 if (mv.getMainValue().getType().getName().equals("Q")) { |
389 if (atGauge) { | 331 if (pnpObject instanceof Number) { |
390 q_out[0] = mv.getValue().doubleValue(); | 332 q_out[0] = mv.getValue().doubleValue(); |
391 } | 333 } |
392 else { | 334 else { |
393 interpolator.interpolate(mv.getValue().doubleValue(), | 335 interpolator.interpolate(mv.getValue().doubleValue(), |
394 gaugeStation, kms, w_out, q_out, c); | 336 gaugeStation, kms, w_out, q_out, c); |
403 return filteredList; | 345 return filteredList; |
404 } | 346 } |
405 | 347 |
406 /** | 348 /** |
407 * Get a list of "Q" main values. | 349 * Get a list of "Q" main values. |
408 * @param atGauge whether is at gauge or needs interpolation. | |
409 * @return list of Q main values. | 350 * @return list of Q main values. |
410 */ | 351 */ |
411 public List<NamedDouble> getMainValuesQ(boolean atGauge) { | 352 public List<NamedDouble> getMainValuesQ(Object pnpObject) { |
412 log.debug("MainValuesArtifact.getMainValuesQ(b)"); | 353 double kms[] = {getLocation()}; |
354 return getMainValuesQ(kms, pnpObject); | |
355 } | |
356 | |
357 | |
358 public List<NamedDouble> getMainValuesW(double[] kms, Object pnpObject) { | |
413 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | 359 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); |
414 Gauge gauge = getGauge(); | 360 Gauge gauge = getGauge(kms[0]); |
415 WstValueTable interpolator = WstValueTableFactory.getTable(RiverUtils.getRiver(this)); | 361 River river = new RiverAccess((D4EArtifact)this).getRiver(); |
362 WstValueTable interpolator = WstValueTableFactory.getTable(river); | |
416 Calculation c = new Calculation(); | 363 Calculation c = new Calculation(); |
417 double w_out[] = {0.0f}; | 364 double w_out[] = {0.0f}; |
418 double q_out[] = {0.0f}; | 365 double q_out[] = {0.0f}; |
419 double kms[] = {getLocation()}; | 366 double pnp = Double.NaN; |
367 | |
420 if (gauge != null) { | 368 if (gauge != null) { |
421 double gaugeStation = gauge.getStation().doubleValue(); | 369 double gaugeStation = gauge.getStation().doubleValue(); |
422 List<MainValue> orig = gauge.getMainValues(); | 370 List<MainValue> orig = gauge.getMainValues(); |
371 if (pnpObject instanceof Number) { | |
372 pnp = Double.valueOf(pnpObject.toString()); | |
373 } | |
374 | |
423 for (MainValue mv : orig) { | 375 for (MainValue mv : orig) { |
424 if (mv.getMainValue().getType().getName().equals("Q")) { | 376 if (pnpObject instanceof Number) { |
425 if (atGauge) { | 377 if (mv.getMainValue().getType().getName().equals("W")) { |
426 q_out[0] = mv.getValue().doubleValue(); | 378 filteredList.add(new NamedDouble( |
427 } | |
428 else { | |
429 interpolator.interpolate(mv.getValue().doubleValue(), | |
430 gaugeStation, kms, w_out, q_out, c); | |
431 } | |
432 filteredList.add(new NamedDouble( | |
433 mv.getMainValue().getName(), | 379 mv.getMainValue().getName(), |
434 q_out[0] | 380 mv.getValue().doubleValue()/100 + pnp |
435 )); | 381 )); |
436 } | |
437 } | |
438 } | |
439 return filteredList; | |
440 } | |
441 | |
442 | |
443 /** Get main values of km. */ | |
444 public List<NamedDouble> getMainValuesW(double[] kms) { | |
445 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
446 boolean atGauge = false; | |
447 double gaugeDatum = 0d; | |
448 Gauge gauge = getGauge(kms[0]); | |
449 if (gauge == null) { | |
450 return filteredList; | |
451 } | |
452 else if (Math.abs(kms[0] - gauge.getStation().doubleValue()) < 1e-4) { | |
453 atGauge = true; | |
454 gaugeDatum = gauge.getDatum().doubleValue(); | |
455 } | |
456 | |
457 WstValueTable interpolator = WstValueTableFactory.getTable(RiverUtils.getRiver(this)); | |
458 Calculation c = new Calculation(); | |
459 | |
460 double gaugeStation = gauge.getStation().doubleValue(); | |
461 double w_out[] = {0.0f}; | |
462 double q_out[] = {0.0f}; | |
463 if (gauge != null) { | |
464 List<MainValue> orig = gauge.getMainValues(); | |
465 for (MainValue mv : orig) { | |
466 if (atGauge) { | |
467 if (mv.getMainValue().getType().getName().equals("W")) { | |
468 filteredList.add(new NamedDouble(mv.getMainValue().getName(), | |
469 mv.getValue().doubleValue())); | |
470 } | 382 } |
471 } else | 383 } else |
472 // We cannot interpolate the W values, so derive them | 384 // We cannot interpolate the W values, so derive them |
473 // from given Q values. | 385 // from given Q values. |
474 if (mv.getMainValue().getType().getName().equals("Q")) { | 386 if (mv.getMainValue().getType().getName().equals("Q")) { |
484 } | 396 } |
485 return filteredList; | 397 return filteredList; |
486 } | 398 } |
487 | 399 |
488 | 400 |
489 public List<NamedDouble> getMainValuesW(boolean atGauge, double[] kms) { | |
490 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
491 Gauge gauge = getGauge(); | |
492 WstValueTable interpolator = WstValueTableFactory.getTable(RiverUtils.getRiver(this)); | |
493 Calculation c = new Calculation(); | |
494 | |
495 double w_out[] = {0.0f}; | |
496 double q_out[] = {0.0f}; | |
497 if (gauge != null) { | |
498 double gaugeStation = gauge.getStation().doubleValue(); | |
499 List<MainValue> orig = gauge.getMainValues(); | |
500 for (MainValue mv : orig) { | |
501 if (atGauge) { | |
502 if (mv.getMainValue().getType().getName().equals("W")) { | |
503 filteredList.add(new NamedDouble(mv.getMainValue().getName(), | |
504 mv.getValue().doubleValue())); | |
505 | |
506 } | |
507 } else | |
508 // We cannot interpolate the W values, so derive them | |
509 // from given Q values. | |
510 if (mv.getMainValue().getType().getName().equals("Q")) { | |
511 interpolator.interpolate(mv.getValue().doubleValue(), | |
512 gaugeStation, kms, w_out, q_out, c); | |
513 filteredList.add(new NamedDouble( | |
514 "W(" + mv.getMainValue().getName() +")", | |
515 w_out[0] | |
516 )); | |
517 } | |
518 } | |
519 } | |
520 return filteredList; | |
521 } | |
522 | |
523 | |
524 /** | 401 /** |
525 * Get a list of "W" main values. | 402 * Get a list of "W" main values. |
526 * @param atGauge if true, do not interpolate | |
527 * @return list of W main values. | 403 * @return list of W main values. |
528 */ | 404 */ |
529 public List<NamedDouble> getMainValuesW(boolean atGauge) { | 405 public List<NamedDouble> getMainValuesW(Object pnpObject) { |
530 return getMainValuesW(atGauge, new double[] {getLocation()}); | 406 return getMainValuesW(new double[] {getLocation()}, pnpObject); |
531 } | 407 } |
532 | 408 |
533 | 409 |
534 /** | 410 /** |
535 * Returns the name of this artifact ('mainvalue'). | 411 * Returns the name of this artifact ('mainvalue'). |