Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java @ 329:0b2358bc716d
Discharge table: Added static method getWForQ() to interpolate a w value for a given q value based on a given discharge table.
flys-artifacts/trunk@1727 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 19 Apr 2011 11:05:27 +0000 |
parents | 07e642030172 |
children | 67b3f54188aa |
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 |
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
|
30 public static final double EPSILON = 1e-5; |
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
|
31 |
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
|
32 public static Comparator<double []> Q_COMPARATOR = |
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
|
33 new Comparator<double []>() { |
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
|
34 public int compare(double [] a, double [] b) { |
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
|
35 double d = a[0] - b[0]; |
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
|
36 if (d < -EPSILON) return -1; |
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
|
37 if (d > +EPSILON) return +1; |
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
|
38 return 0; |
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
|
39 } |
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
|
40 }; |
146
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 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
|
43 |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 protected String riverName; |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 |
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
|
46 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
|
47 |
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
|
48 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
|
49 |
150
c904d52cdfd0
Don't used interleaved x/y data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
146
diff
changeset
|
50 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
|
51 |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 public DischargeTables() { |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
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
|
55 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
|
56 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
|
57 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 |
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
|
59 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
|
60 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
|
61 } |
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 |
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 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
|
64 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
|
65 } |
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
|
66 |
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
|
67 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
|
68 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
|
69 } |
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 |
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 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
|
72 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
|
73 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
|
74 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
|
75 ) { |
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
|
76 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
|
77 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
|
78 this.riverName = riverName; |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 this.gaugeNames = gaugeNames; |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 |
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
|
82 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
|
83 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
|
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 |
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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 } |
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
|
91 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
|
92 } |
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
|
93 |
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
|
94 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
|
95 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
|
96 } |
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
|
97 |
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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 return values; |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 |
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
|
106 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
|
107 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
|
108 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
109 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
|
110 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
111 Query gaugeQuery = session.createQuery( |
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
|
112 "from Gauge where name=:gauge and river.name=:river and kind=:kind"); |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
113 gaugeQuery.setParameter("river", riverName); |
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
|
114 gaugeQuery.setInteger("kind", kind); |
146
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 if (gauges.isEmpty()) { |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
120 log.warn( |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
121 "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
|
122 continue; |
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 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
|
125 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
126 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
|
127 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
128 if (tables.isEmpty()) { |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
129 log.warn( |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
130 "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
|
131 continue; |
146
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
134 // 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
|
135 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
|
136 |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
137 List<DischargeTableValue> dtvs = |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
138 table.getDischargeTableValues(); |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
139 |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
140 double [][] vs = new double[2][dtvs.size()]; |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
141 |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
142 int idx = 0; |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
143 for (DischargeTableValue dtv: dtvs) { |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
144 vs[0][idx] = dtv.getQ().doubleValue() * scale; |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
145 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
|
146 ++idx; |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
147 } |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
148 |
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
|
149 // TODO: Do this db level. |
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
|
150 Arrays.sort(vs, Q_COMPARATOR); |
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
|
151 |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
152 values.put(gaugeName, vs); |
146
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 } |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
154 |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
154
diff
changeset
|
155 return values; |
146
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 } |
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
|
157 |
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
|
158 public static boolean getWForQ( |
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
|
159 double [][] values, |
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
|
160 double q, |
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
|
161 double [] result |
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
|
162 ) { |
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
|
163 int index = Arrays.binarySearch(values, new double [] { q }, Q_COMPARATOR); |
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
|
164 if (index >= 0) { |
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
|
165 result[0] = values[1][index]; |
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
|
166 return true; |
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
|
167 } |
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
|
168 |
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
|
169 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
|
170 |
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
|
171 if (index == 0 || index+1 >= values.length) { |
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
|
172 // do not extraploate |
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
|
173 return false; |
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
|
174 } |
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
|
175 |
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
|
176 double q1 = values[index ][0]; |
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
|
177 double q2 = values[index+1][0]; |
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
|
178 double w1 = values[index ][1]; |
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
|
179 double w2 = values[index+1][1]; |
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
|
180 |
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 // w1 = m*q1 + b |
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
|
182 // w2 = m*q2 + b |
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
|
183 // w2 - w1 = m*(q2 - q1) |
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
|
184 // m = (w2 - w1)/(q2 - q1) # q2 != q1 |
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
|
185 // b = w1 - m*q1 |
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
|
186 |
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
|
187 if (q1 == q2) { |
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
|
188 result[0] = 0.5*(w1 + w2); |
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
|
189 } |
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
|
190 else { |
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 double m = (w2 - w1)/(q2 - q1); |
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
|
192 double b = w1 - m*q1; |
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 result[0] = q*m + b; |
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 |
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 return true; |
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
|
197 } |
146
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
198 } |
e8dfe6ba2000
Fetch w/q values of discharge tables for given gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |