annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java @ 674:d5f9ba1d055f

Added calculation base class to collect the problems during the calculations. Adjusted calculation 4 to use the mechanism. flys-artifacts/trunk@2098 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 10 Jun 2011 09:19:27 +0000
parents 20c3a5b36434
children db68806e6563
rev   line source
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.artifacts.model;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.util.List;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.util.Map;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.util.HashMap;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.util.Arrays;
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
7 import java.util.Comparator;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import java.io.Serializable;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import org.hibernate.Session;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import org.hibernate.Query;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import org.apache.log4j.Logger;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
315
94c176fdcb89 Removed the SessionHolder and adapted imports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 311
diff changeset
16 import de.intevation.flys.backend.SessionHolder;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import de.intevation.flys.model.Gauge;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 import de.intevation.flys.model.DischargeTable;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import de.intevation.flys.model.DischargeTableValue;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 public class DischargeTables
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 implements Serializable
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 {
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
24 private static Logger log = Logger.getLogger(DischargeTables.class);
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
25
328
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
26 public static final double DEFAULT_SCALE = 100.0;
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
27
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
28 public static final int MASTER = 0;
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
29
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 protected List<String> gaugeNames;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 protected String riverName;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33
154
03362976fa0f DischargeTables: Cache the scale, too. Otherwise two calls to getValues() with different arguments will result in the same output.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 153
diff changeset
34 protected double scale;
03362976fa0f DischargeTables: Cache the scale, too. Otherwise two calls to getValues() with different arguments will result in the same output.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 153
diff changeset
35
328
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
36 protected int kind;
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
37
150
c904d52cdfd0 Don't used interleaved x/y data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 146
diff changeset
38 protected Map<String, double [][]> values;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 public DischargeTables() {
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
328
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
43 public DischargeTables(String riverName, String gaugeName) {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
44 this(riverName, gaugeName, MASTER);
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46
328
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
47 public DischargeTables(String riverName, String gaugeName, int kind) {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
48 this(riverName, new String [] { gaugeName }, kind);
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
49 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
50
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
51 public DischargeTables(String riverName, String [] gaugeNames) {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
52 this(riverName, gaugeNames, MASTER);
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
53 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
54
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
55 public DischargeTables(String riverName, String [] gaugeNames, int kind) {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
56 this(riverName, Arrays.asList(gaugeNames), kind);
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
57 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
58
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
59 public DischargeTables(
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
60 String riverName,
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
61 List<String> gaugeNames,
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
62 int kind
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
63 ) {
154
03362976fa0f DischargeTables: Cache the scale, too. Otherwise two calls to getValues() with different arguments will result in the same output.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 153
diff changeset
64 scale = Double.NaN;
328
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
65 this.kind = kind;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 this.riverName = riverName;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 this.gaugeNames = gaugeNames;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
328
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
70 public double [][] getFirstTable() {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
71 return getFirstTable(DEFAULT_SCALE);
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
72 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
73
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
74 public double [][] getFirstTable(double scale) {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
75 Map<String, double [][]> values = getValues(scale);
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
76 for (double [][] table: values.values()) {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
77 return table;
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
78 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
79 return null;
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
80 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
81
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
82 public Map<String, double [][]> getValues() {
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
83 return getValues(DEFAULT_SCALE);
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
84 }
07e642030172 Discharge tables: Added convenience constructors/methods to ease the access to the master discharge table of a gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 315
diff changeset
85
153
c97f9bc9b642 The out() operation of the WINFO artifact draws discharge tables for gauges specified by the user.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 150
diff changeset
86 public Map<String, double [][]> getValues(double scale) {
154
03362976fa0f DischargeTables: Cache the scale, too. Otherwise two calls to getValues() with different arguments will result in the same output.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 153
diff changeset
87 if (values == null || scale != this.scale) {
153
c97f9bc9b642 The out() operation of the WINFO artifact draws discharge tables for gauges specified by the user.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 150
diff changeset
88 values = loadValues(scale);
154
03362976fa0f DischargeTables: Cache the scale, too. Otherwise two calls to getValues() with different arguments will result in the same output.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 153
diff changeset
89 this.scale = scale;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 return values;
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93
153
c97f9bc9b642 The out() operation of the WINFO artifact draws discharge tables for gauges specified by the user.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 150
diff changeset
94 protected Map<String, double [][]> loadValues(double scale) {
150
c904d52cdfd0 Don't used interleaved x/y data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 146
diff changeset
95 Map<String, double [][]> values = new HashMap<String, double [][]>();
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
97 Session session = SessionHolder.HOLDER.get();
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
99 Query gaugeQuery = session.createQuery(
336
7f13ed751277 Fixed broken HQL statement.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 334
diff changeset
100 "from Gauge where name=:gauge and river.name=:river");
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
101 gaugeQuery.setParameter("river", riverName);
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
103 for (String gaugeName: gaugeNames) {
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
104 gaugeQuery.setParameter("gauge", gaugeName);
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
105 List<Gauge> gauges = gaugeQuery.list();
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
106 if (gauges.isEmpty()) {
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
107 log.warn(
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
108 "no gauge '"+gaugeName+"' at river '"+riverName+"'");
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
109 continue;
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
110 }
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
111 Gauge gauge = gauges.get(0);
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
113 List<DischargeTable> tables = gauge.getDischargeTables();
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
115 if (tables.isEmpty()) {
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
116 log.warn(
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
117 "no discharge table for gauge '" + gaugeName + "'");
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
118 continue;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
121 // TODO: Filter by time interval
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
122 DischargeTable table = tables.get(0);
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
123
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
124 List<DischargeTableValue> dtvs =
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
125 table.getDischargeTableValues();
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
126
333
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
127 final double [][] vs = new double[2][dtvs.size()];
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
128
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
129 boolean qSorted = true;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
130
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
131 double lastQ = -Double.MAX_VALUE;
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
132
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
133 int idx = 0;
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
134 for (DischargeTableValue dtv: dtvs) {
333
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
135 double q = dtv.getQ().doubleValue();
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
136 vs[0][idx] = q * scale;
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
137 vs[1][idx] = dtv.getW().doubleValue() * scale;
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
138 ++idx;
333
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
139
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
140 if (qSorted && lastQ > q) {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
141 qSorted = false;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
142 }
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
143 lastQ = q;
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
144 }
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
145
333
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
146 if (!qSorted) {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
147 log.debug("need to sort by q values");
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
148 // TODO: Do this db level.
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
149 // XXX: This is so ugly :-(
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
150 Integer [] indices = new Integer[vs[0].length];
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
151 for (int i = 0; i < indices.length; ++i) {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
152 indices[i] = i;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
153 }
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
154
333
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
155 Arrays.sort(indices, new Comparator<Integer>() {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
156 public int compare(Integer a, Integer b) {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
157 double va = vs[1][a];
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
158 double vb = vs[1][b];
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
159 double d = va - vb;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
160 if (d < 0.0) return -1;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
161 if (d > 0.0) return +1;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
162 return 0;
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
163 }
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
164 });
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
165
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
166 double [][] vs2 = new double[2][vs[0].length];
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
167 for (int i = 0; i < indices.length; ++i) {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
168 vs2[0][i] = vs[0][indices[i]];
337
ec17fad34061 Discharge table: Fix problem when sorting by q (copied w instead of q).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 336
diff changeset
169 vs2[1][i] = vs[1][indices[i]];
333
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
170 }
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
171 values.put(gaugeName, vs2);
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
172 }
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
173 else {
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
174 values.put(gaugeName, vs);
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
175 }
67b3f54188aa Discharge tables: Sorting of q values was done wrong.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 329
diff changeset
176
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 }
311
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
178
ea3a438a7021 Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 154
diff changeset
179 return values;
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 }
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
181
376
aa0889141b15 Removed an unused parameter from DischargeTables.getQForW().
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 337
diff changeset
182 public static double getQForW(double [][] values, double w) {
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
183
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
184 boolean debug = log.isDebugEnabled();
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
185
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
186 if (debug) {
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
187 log.debug("calculating getQForW(" + w + ")");
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
188 }
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
189
334
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
190 int index = Arrays.binarySearch(values[0], w);
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
191 if (index >= 0) {
334
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
192 return values[0][index];
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
193 }
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
194
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
195 index = -index - 1; // insert position
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
196
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
197 if (index < 1 || index >= values[0].length) {
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
198 // do not extraploate
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
199 if (debug) {
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
200 log.debug("we do not extrapolate: NaN");
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
201 }
334
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
202 return Double.NaN;
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
203 }
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
204
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
205 double w1 = values[0][index-1];
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
206 double w2 = values[0][index ];
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
207 double q1 = values[1][index-1];
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
208 double q2 = values[1][index ];
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
209
334
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
210 // q1 = m*w1 + b
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
211 // q2 = m*w2 + b
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
212 // q2 - q1 = m*(w2 - w1)
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
213 // m = (q2 - q1)/(w2 - w1) # w2 != w1
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
214 // b = q1 - m*w1
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
215
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
216 double q;
334
b7c8df643dc4 Discharge table: we need a getQForW() and not a getWForQ().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 333
diff changeset
217 if (w1 == w2) {
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
218 q = 0.5*(q1 + q2);
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
219 if (debug) {
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
220 log.debug("same w1 and w1: " + w1);
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
221 }
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
222 }
378
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
223 else {
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
224 double m = (q2 - q1)/(w2 - w1);
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
225 double b = q1 - m*w1;
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
226 q = w*m + b;
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
227 }
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
228 if (debug) {
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
229 log.debug("Q(" + w + ") = " + q);
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
230 }
20c3a5b36434 Repaired DischargeTables.getQForW()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 376
diff changeset
231 return q;
329
0b2358bc716d Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 328
diff changeset
232 }
146
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233 }
e8dfe6ba2000 Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org