Mercurial > gemma
annotate pkg/controllers/bottlenecks.go @ 3430:6994602d2935
fairway availibilty: Implemented for streches and sections.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 23 May 2019 17:28:14 +0200 |
parents | 075757ca8fce |
children | d7ddb21f7017 |
rev | line source |
---|---|
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
17 "context" |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
19 "encoding/csv" |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "fmt" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "log" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "net/http" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 "sort" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 "strconv" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "strings" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 "time" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
3117
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
28 "github.com/gorilla/mux" |
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
29 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 "gemma.intevation.de/gemma/pkg/common" |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
31 "gemma.intevation.de/gemma/pkg/middleware" |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 const ( |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
35 selectLimitingSQL = ` |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
36 SELECT limiting from waterway.bottlenecks WHERE objnam = $1` |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
37 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 selectAvailableDepthSQL = ` |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 WITH data AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 SELECT |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 efa.measure_date, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 efa.available_depth_value, |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
43 efa.available_width_value, |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 efa.water_level_value |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 FROM waterway.effective_fairway_availability efa |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 JOIN waterway.fairway_availability fa |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 ON efa.fairway_availability_id = fa.id |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 JOIN waterway.bottlenecks bn |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 ON fa.bottleneck_id = bn.id |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 WHERE |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 bn.objnam = $1 AND |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 efa.level_of_service = $2 AND |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 efa.measure_type = 'Measured' AND |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
54 (efa.available_depth_value IS NOT NULL OR |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
55 efa.available_width_value IS NOT NULL) AND |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 efa.water_level_value IS NOT NULL |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 ), |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 before AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 SELECT * FROM data WHERE measure_date < $3 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 ORDER BY measure_date DESC LIMIT 1 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 ), |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 inside AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 SELECT * FROM data WHERE measure_date BETWEEN $3 AND $4 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 ), |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 after AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 SELECT * FROM data WHERE measure_date > $4 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 ORDER BY measure_date LIMIT 1 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 SELECT * FROM before |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 UNION ALL |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 SELECT * FROM inside |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 UNION ALL |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 SELECT * FROM after |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 ORDER BY measure_date |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 ` |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 selectGaugeLevelsSQL = ` |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 SELECT |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 grwl.depth_reference, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 grwl.value |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
81 FROM waterway.gauges_reference_water_levels grwl |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
82 JOIN waterway.bottlenecks bns |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
83 ON grwl.location = bns.gauge_location |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
84 AND grwl.validity = bns.gauge_validity |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 WHERE bns.objnam = $1 AND ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 grwl.depth_reference like 'HDC%' OR |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 grwl.depth_reference like 'LDC%' OR |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 grwl.depth_reference like 'MW%' |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 ` |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
91 selectGaugeLDCSQL = ` |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
92 SELECT |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
93 grwl.value |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
94 FROM waterway.gauges_reference_water_levels grwl |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
95 JOIN waterway.bottlenecks bns |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
96 ON grwl.location = bns.gauge_location |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3208
diff
changeset
|
97 AND grwl.validity = bns.gauge_validity |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
98 WHERE bns.objnam = $1 AND grwl.depth_reference like 'LDC%' |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
99 ` |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
102 type ( |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
103 availMeasurement struct { |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
104 when time.Time |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
105 depth int16 |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
106 width int16 |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
107 value int16 |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
108 } |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
109 |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
110 availMeasurements []availMeasurement |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
111 ) |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
112 |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
113 // afdRefs are the typical available fairway depth reference values. |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
114 var afdRefs = []float64{ |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
115 230, |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
116 250, |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
117 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
118 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
119 func (measurement *availMeasurement) getDepth() float64 { |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
120 return float64(measurement.depth) |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
121 } |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
122 |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
123 func (measurement *availMeasurement) getValue() float64 { |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
124 return float64(measurement.value) |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
125 } |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
126 |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
127 func (measurement *availMeasurement) getWidth() float64 { |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
128 return float64(measurement.width) |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
129 } |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
130 |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
131 func limitingFactor(limiting string) func(*availMeasurement) float64 { |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
132 switch limiting { |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
133 case "depth": |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
134 return (*availMeasurement).getDepth |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
135 case "width": |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
136 return (*availMeasurement).getWidth |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
137 default: |
3393
cf0b5274698a
Cosmetics: Forgot a newline in logging.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3392
diff
changeset
|
138 log.Printf("warn: unknown limitation '%s'. default to 'depth'\n", limiting) |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
139 return (*availMeasurement).getDepth |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
140 } |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
141 } |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
142 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
143 func (measurements availMeasurements) classify( |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
144 from, to time.Time, |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
145 breaks []float64, |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
146 access func(*availMeasurement) float64, |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
147 ) []time.Duration { |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
148 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
149 if len(breaks) == 0 { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
150 return []time.Duration{} |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
151 } |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
152 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
153 result := make([]time.Duration, len(breaks)+1) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
154 classes := make([]float64, len(breaks)+2) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
155 values := make([]time.Time, len(classes)) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
156 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
157 // Add sentinels |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
158 classes[0] = breaks[0] - 9999 |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
159 classes[len(classes)-1] = breaks[len(breaks)-1] + 9999 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
160 for i := range breaks { |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
161 classes[i+1] = breaks[i] |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
162 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
163 |
3378
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
164 idx := sort.Search(len(measurements), func(i int) bool { |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
165 // All values before from can be ignored. |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
166 return !measurements[i].when.Before(from) |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
167 }) |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
168 |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
169 if idx >= len(measurements) { |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
170 return result |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
171 } |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
172 |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
173 // Be safe for interpolation. |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
174 if idx > 0 { |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
175 idx-- |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
176 } |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
177 |
ff79a9b6707f
waterlevel classification: Re-enable finding a good starting points for classification intervals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3374
diff
changeset
|
178 measurements = measurements[idx:] |
3374
7d8c41cc50f5
Added TODO in waterlevel classifcation to find a good starting point.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3373
diff
changeset
|
179 |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
180 for i := 0; i < len(measurements)-1; i++ { |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
181 p1 := &measurements[i] |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
182 p2 := &measurements[i+1] |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
183 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
184 if p1.when.After(to) { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
185 return result |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
186 } |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
187 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
188 if p2.when.Before(from) { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
189 continue |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
190 } |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
191 |
3370
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
192 lo, hi := maxTime(p1.when, from), minTime(p2.when, to) |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
193 |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
194 m1, m2 := access(p1), access(p2) |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
195 if m1 == m2 { // The whole interval is in only one class. |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
196 for j := 0; j < len(classes)-1; j++ { |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
197 if classes[j] <= m1 && m1 <= classes[j+1] { |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
198 result[j] += hi.Sub(lo) |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
199 break |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
200 } |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
201 } |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
202 continue |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
203 } |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
204 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
205 f := common.InterpolateTime( |
3370
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
206 p1.when, m1, |
ac630f0f5dbf
Waterlevel classification: Handle case of same values in adjoint meassure dates correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
207 p2.when, m2, |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
208 ) |
3318
753e7c8e8c95
Fixed time interval handling in waterlevel classification
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3317
diff
changeset
|
209 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
210 for j, c := range classes { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
211 values[j] = f(c) |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
212 } |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
213 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
214 for j := 0; j < len(values)-1; j++ { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
215 start, end := orderTime(values[j], values[j+1]) |
3208
3bba82bcb6a6
Second fix to waterlevel classification. Set the bounds of the intervals correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3201
diff
changeset
|
216 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
217 if start.After(hi) || end.Before(lo) { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
218 continue |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
219 } |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
220 |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
221 start, end = maxTime(start, lo), minTime(end, hi) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
222 result[j] += end.Sub(start) |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
223 } |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
224 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
225 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
226 return result |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
227 } |
3208
3bba82bcb6a6
Second fix to waterlevel classification. Set the bounds of the intervals correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3201
diff
changeset
|
228 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
229 func orderTime(a, b time.Time) (time.Time, time.Time) { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
230 if a.Before(b) { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
231 return a, b |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
232 } |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
233 return b, a |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
234 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
235 |
3337
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
236 func minTime(a, b time.Time) time.Time { |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
237 if a.Before(b) { |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
238 return a |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
239 } |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
240 return b |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
241 } |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
242 |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
243 func maxTime(a, b time.Time) time.Time { |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
244 if a.After(b) { |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
245 return a |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
246 } |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
247 return b |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
248 } |
146bf3a1752c
Fixed waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3329
diff
changeset
|
249 |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
250 func durationsToPercentage(duration time.Duration, classes []time.Duration) []float64 { |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
251 percents := make([]float64, len(classes)) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
252 total := 100 / duration.Seconds() |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
253 for i, v := range classes { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
254 percents[i] = v.Seconds() * total |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
255 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
256 return percents |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
257 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
258 |
3115
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
259 func parseTime(s, what string) (time.Time, error) { |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
260 var t time.Time |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
261 var err error |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
262 if t, err = time.Parse(common.TimeFormat, s); err != nil { |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
263 return time.Time{}, JSONError{ |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
264 Code: http.StatusBadRequest, |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
265 Message: fmt.Sprintf( |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
266 "Invalid time format for '%s' field: %v", what, err), |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
267 } |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
268 } |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
269 return t.UTC(), nil |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
270 } |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
271 |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
272 func parseInt(s, what string) (int, error) { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
273 i, err := strconv.Atoi(s) |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
274 if err != nil { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
275 return 0, JSONError{ |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
276 Code: http.StatusBadRequest, |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
277 Message: fmt.Sprintf( |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
278 "Invalid value for field '%s': %v", what, err), |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
279 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
280 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
281 return i, nil |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
282 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
283 |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
284 func loadDepthValues( |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
285 ctx context.Context, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
286 conn *sql.Conn, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
287 bottleneck string, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
288 los int, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
289 from, to time.Time, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
290 ) (availMeasurements, error) { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
291 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
292 rows, err := conn.QueryContext( |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
293 ctx, selectAvailableDepthSQL, bottleneck, los, from, to) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
294 if err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
295 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
296 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
297 defer rows.Close() |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
298 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
299 var ms availMeasurements |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
300 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
301 for rows.Next() { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
302 var m availMeasurement |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
303 if err := rows.Scan( |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
304 &m.when, |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
305 &m.depth, |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
306 &m.width, |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
307 &m.value, |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
308 ); err != nil { |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
309 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
310 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
311 m.when = m.when.UTC() |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
312 ms = append(ms, m) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
313 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
314 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
315 if err := rows.Err(); err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
316 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
317 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
318 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
319 return ms, nil |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
320 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
321 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
322 func loadLDCReferenceValue( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
323 ctx context.Context, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
324 conn *sql.Conn, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
325 bottleneck string, |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
326 ) ([]float64, error) { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
327 var value float64 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
328 err := conn.QueryRowContext(ctx, selectGaugeLDCSQL, bottleneck).Scan(&value) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
329 switch { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
330 case err == sql.ErrNoRows: |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
331 return nil, nil |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
332 case err != nil: |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
333 return nil, err |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
334 } |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
335 return []float64{value}, nil |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
336 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
337 |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
338 func breaksToReferenceValue(breaks string) []float64 { |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
339 parts := strings.Split(breaks, ",") |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
340 var values []float64 |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
341 |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
342 for _, part := range parts { |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
343 part = strings.TrimSpace(part) |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
344 if v, err := strconv.ParseFloat(part, 64); err == nil { |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
345 values = append(values, v) |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
346 } |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
347 } |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
348 |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
349 sort.Float64s(values) |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
350 |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
351 // dedup |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
352 for i := 1; i < len(values); { |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
353 if values[i-1] == values[i] { |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
354 copy(values[i:], values[i+1:]) |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
355 values = values[:len(values)-1] |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
356 } else { |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
357 i++ |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
358 } |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
359 } |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
360 return values |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
361 } |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
362 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
363 func bottleneckAvailabilty( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
364 _ interface{}, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
365 req *http.Request, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
366 conn *sql.Conn, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
367 ) (jr JSONResult, err error) { |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
368 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
369 bn := mux.Vars(req)["objnam"] |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
370 var from, to time.Time |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
371 var los int |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
372 var breaks []float64 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
373 var ldcRefs []float64 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
374 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
375 if bn == "" { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
376 err = JSONError{ |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
377 Code: http.StatusBadRequest, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
378 Message: "Missing objnam of bottleneck", |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
379 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
380 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
381 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
382 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
383 if f := req.FormValue("from"); f != "" { |
3115
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
384 if from, err = parseTime(f, "from"); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
385 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
386 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
387 } else { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
388 from = time.Now().AddDate(-1, 0, 0).UTC() |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
389 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
390 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
391 if t := req.FormValue("to"); t != "" { |
3115
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
392 if to, err = parseTime(t, "to"); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
393 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
394 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
395 } else { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
396 to = from.AddDate(1, 0, 0).UTC() |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
397 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
398 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
399 if to.Before(from) { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
400 to, from = from, to |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
401 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
402 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
403 if l := req.FormValue("los"); l != "" { |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
404 if los, err = parseInt(l, "los"); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
405 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
406 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
407 } else { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
408 los = 1 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
409 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
410 |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
411 if b := req.FormValue("breaks"); b != "" { |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
412 breaks = breaksToReferenceValue(b) |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
413 } else { |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
414 breaks = afdRefs |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
415 } |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
416 |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
417 ctx := req.Context() |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
418 |
3412
5e284f39a081
fairway availibility: Reduce defaults to 230 and 250 as class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3405
diff
changeset
|
419 if ldcRefs, err = loadLDCReferenceValue(ctx, conn, bn); err != nil { |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
420 return |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
421 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
422 |
3412
5e284f39a081
fairway availibility: Reduce defaults to 230 and 250 as class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3405
diff
changeset
|
423 if len(ldcRefs) == 0 { |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
424 err = JSONError{ |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
425 Code: http.StatusNotFound, |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
426 Message: "No gauge reference values found for bottleneck", |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
427 } |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
428 return |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
429 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
430 |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
431 log.Printf("info: time interval: (%v - %v)\n", from, to) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
432 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
433 var ms availMeasurements |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
434 if ms, err = loadDepthValues(ctx, conn, bn, los, from, to); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
435 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
436 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
437 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
438 if len(ms) == 0 { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
439 err = JSONError{ |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
440 Code: http.StatusNotFound, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
441 Message: "No available fairway depth values found", |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
442 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
443 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
444 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
445 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
446 lnwl := ms.classify( |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
447 from, to, |
3412
5e284f39a081
fairway availibility: Reduce defaults to 230 and 250 as class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3405
diff
changeset
|
448 ldcRefs, |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
449 (*availMeasurement).getValue, |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
450 ) |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
451 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
452 afd := ms.classify( |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
453 from, to, |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
454 breaks, |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
455 (*availMeasurement).getDepth, |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
456 ) |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
457 |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
458 duration := to.Sub(from) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
459 lnwlPercents := durationsToPercentage(duration, lnwl) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3428
diff
changeset
|
460 afdPercents := durationsToPercentage(duration, afd) |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
461 |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
462 type ldcOutput struct { |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
463 Value float64 `json:"value"` |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
464 Below float64 `json:"below"` |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
465 Above float64 `json:"above"` |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
466 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
467 |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
468 type intervalOutput struct { |
3428
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
469 From *float64 `json:"from,omitempty"` |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
470 To *float64 `json:"to,omitempty"` |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
471 Percent float64 `json:"percent"` |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
472 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
473 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
474 type output struct { |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
475 LDC []intervalOutput `json:"ldc"` |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
476 AFD []intervalOutput `json:"afd"` |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
477 } |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
478 |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
479 out := output{ |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
480 LDC: []intervalOutput{ |
3428
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
481 {To: &ldcRefs[0], Percent: lnwlPercents[0]}, |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
482 {From: &ldcRefs[0], Percent: lnwlPercents[1]}, |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
483 }, |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
484 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
485 |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
486 for i, percent := range afdPercents { |
3428
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
487 var from, to *float64 |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
488 switch { |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
489 case i == 0: |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
490 to = &breaks[i] |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
491 case i == len(afdPercents)-1: |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
492 from = &breaks[len(breaks)-1] |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
493 default: |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
494 from = &breaks[i-1] |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
495 to = &breaks[i] |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
496 } |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
497 out.AFD = append(out.AFD, intervalOutput{ |
3428
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
498 From: from, |
075757ca8fce
fairway availabilty: A more constistent result format.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3426
diff
changeset
|
499 To: to, |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
500 Percent: percent, |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
501 }) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
502 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
503 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
504 jr = JSONResult{Result: &out} |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
505 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
506 } |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
507 |
3426
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
508 /* |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
509 |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
510 { "type": "below", "value": 230, "percent": 0 }, |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
511 { "below": 250, "value": 9 } |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
512 { "above": 250, "value": 8.1 |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
513 |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
514 */ |
4b303c149db4
fairway availibilty: Changed result document to be more precise.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
515 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
516 func intervalMode(mode string) int { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
517 switch strings.ToLower(mode) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
518 case "monthly": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
519 return 0 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
520 case "quarterly": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
521 return 1 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
522 case "yearly": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
523 return 2 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
524 default: |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
525 return 0 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
526 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
527 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
528 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
529 func bottleneckAvailableFairwayDepth(rw http.ResponseWriter, req *http.Request) { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
530 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
531 mode := intervalMode(req.FormValue("mode")) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
532 var from, to time.Time |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
533 var los int |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
534 |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
535 bn := mux.Vars(req)["objnam"] |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
536 if bn == "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
537 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
538 rw, "Missing objnam of bottleneck", |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
539 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
540 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
541 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
542 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
543 if f := req.FormValue("from"); f != "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
544 var err error |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
545 if from, err = time.Parse(common.TimeFormat, f); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
546 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
547 rw, fmt.Sprintf("Invalid format for 'from': %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
548 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
549 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
550 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
551 } else { |
3201
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
552 from = time.Now().AddDate(-1, 0, 0) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
553 } |
3201
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
554 from = from.UTC() |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
555 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
556 if t := req.FormValue("to"); t != "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
557 var err error |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
558 if to, err = time.Parse(common.TimeFormat, t); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
559 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
560 rw, fmt.Sprintf("Invalid format for 'to': %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
561 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
562 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
563 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
564 } else { |
3201
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
565 to = from.AddDate(1, 0, 0) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
566 } |
3201
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
567 to = to.UTC() |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
568 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
569 if to.Before(from) { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
570 to, from = from, to |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
571 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
572 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
573 if l := req.FormValue("los"); l != "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
574 var err error |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
575 if los, err = strconv.Atoi(l); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
576 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
577 rw, fmt.Sprintf("Invalid format for 'los': %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
578 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
579 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
580 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
581 } else { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
582 los = 1 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
583 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
584 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
585 conn := middleware.GetDBConn(req) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
586 ctx := req.Context() |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
587 |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
588 var limiting string |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
589 err := conn.QueryRowContext(ctx, selectLimitingSQL, bn).Scan(&limiting) |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
590 switch { |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
591 case err == sql.ErrNoRows: |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
592 http.Error( |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
593 rw, fmt.Sprintf("Unknown limitation for %s.", bn), |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
594 http.StatusNotFound) |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
595 return |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
596 case err != nil: |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
597 http.Error( |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
598 rw, fmt.Sprintf("DB error: %v.", err), |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
599 http.StatusInternalServerError) |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
600 return |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
601 } |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
602 |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
603 access := limitingFactor(limiting) |
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
604 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
605 log.Printf("info: time interval: (%v - %v)\n", from, to) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3393
diff
changeset
|
606 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
607 // load the measurements |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
608 ms, err := loadDepthValues(ctx, conn, bn, los, from, to) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
609 if err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
610 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
611 rw, fmt.Sprintf("Loading measurements failed: %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
612 http.StatusInternalServerError) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
613 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
614 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
615 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
616 ldcRefs, err := loadLDCReferenceValue(ctx, conn, bn) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
617 if err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
618 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
619 rw, fmt.Sprintf("Loading LDC failed: %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
620 http.StatusInternalServerError) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
621 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
622 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
623 if len(ldcRefs) == 0 { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
624 http.Error(rw, "No LDC found", http.StatusNotFound) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
625 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
626 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
627 |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
628 var breaks []float64 |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
629 if b := req.FormValue("breaks"); b != "" { |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
630 breaks = breaksToReferenceValue(b) |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
631 } else { |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
632 breaks = afdRefs |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
633 } |
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
634 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
635 rw.Header().Add("Content-Type", "text/csv") |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
636 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
637 out := csv.NewWriter(rw) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
638 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
639 // label, classes, lnwl |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
640 record := make([]string, 1+1+len(breaks)+1) |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
641 record[0] = "#label" |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
642 record[1] = "# >= LDC [h]" |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
643 for i, v := range breaks { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
644 if i == 0 { |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
645 record[2] = fmt.Sprintf("# < %.2f [h]", v) |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
646 } |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3412
diff
changeset
|
647 record[i+3] = fmt.Sprintf("# >= %.2f [h]", v) |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
648 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
649 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
650 if err := out.Write(record); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
651 // Too late for HTTP status message. |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
652 log.Printf("error: %v\n", err) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
653 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
654 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
655 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
656 //log.Println(len(ms)) |
3201
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
657 //for i := range ms { |
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
658 // log.Println(ms[i].when, ms[i].depth) |
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
659 //} |
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
660 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
661 log.Printf("info: measurements: %d\n", len(ms)) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
662 if len(ms) > 1 { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
663 log.Printf("info: first: %v\n", ms[0].when) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
664 log.Printf("info: last: %v\n", ms[len(ms)-1].when) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
665 log.Printf("info: interval: %.2f [h]\n", ms[len(ms)-1].when.Sub(ms[0].when).Hours()) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
666 } |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
667 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
668 interval := intervals[mode](from, to) |
3201
d5294f1a4ad4
First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3133
diff
changeset
|
669 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
670 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
671 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
672 ranges := ms.classify( |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
673 pfrom, pto, |
3385
e994565a59a1
waterlevel classification: Added 'breaks=<value 1>,<value 2>,...,<value n>' query parameter to set the class breaks for classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3378
diff
changeset
|
674 breaks, |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
675 access, |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
676 ) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
677 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3337
diff
changeset
|
678 ldc := ms.classify( |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
679 pfrom, pto, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
680 ldcRefs, |
3392
96d3af15fc28
available fairway depth: Decide if width or depth is critial by looking at the bottleneck limiting factor.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3385
diff
changeset
|
681 access, |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
682 ) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
683 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
684 record[0] = label |
3208
3bba82bcb6a6
Second fix to waterlevel classification. Set the bounds of the intervals correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3201
diff
changeset
|
685 record[1] = fmt.Sprintf("%.3f", ldc[1].Hours()) |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
686 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
687 for i, d := range ranges { |
3208
3bba82bcb6a6
Second fix to waterlevel classification. Set the bounds of the intervals correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3201
diff
changeset
|
688 record[2+i] = fmt.Sprintf("%.3f", d.Hours()) |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
689 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
690 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
691 if err := out.Write(record); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
692 // Too late for HTTP status message. |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
693 log.Printf("error: %v\n", err) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
694 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
695 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
696 } |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
697 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
698 out.Flush() |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
699 if err := out.Error(); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
700 // Too late for HTTP status message. |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
701 log.Printf("error: %v\n", err) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
702 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
703 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
704 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
705 var intervals = []func(time.Time, time.Time) func() (time.Time, time.Time, string){ |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
706 monthly, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
707 quarterly, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
708 yearly, |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
709 } |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
710 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
711 func monthly(from, to time.Time) func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
712 pfrom := from |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
713 return func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
714 if pfrom.After(to) { |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
715 return time.Time{}, time.Time{}, "" |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
716 } |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
717 f := pfrom |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
718 pfrom = pfrom.AddDate(0, 1, 0) |
3133
7b4092b6b51a
Fairway availability depth: Fixed SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3122
diff
changeset
|
719 label := fmt.Sprintf("%02d-%d", f.Month(), f.Year()) |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
720 return f, f.AddDate(0, 1, 0).Add(-time.Nanosecond), label |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
721 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
722 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
723 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
724 func quarterly(from, to time.Time) func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
725 pfrom := from |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
726 return func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
727 if pfrom.After(to) { |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
728 return time.Time{}, time.Time{}, "" |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
729 } |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
730 f := pfrom |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
731 pfrom = pfrom.AddDate(0, 3, 0) |
3373
27aca46771b5
Waterlevel calculation: fixed labels of quarters (again).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3372
diff
changeset
|
732 label := fmt.Sprintf("Q%d-%d", (int(f.Month())-1)/3+1, f.Year()) |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
733 return f, f.AddDate(0, 3, 0).Add(-time.Nanosecond), label |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
734 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
735 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
736 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
737 func yearly(from, to time.Time) func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
738 pfrom := from |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
739 return func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
740 if pfrom.After(to) { |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
741 return time.Time{}, time.Time{}, "" |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
742 } |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
743 f := pfrom |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
744 pfrom = pfrom.AddDate(1, 0, 0) |
3133
7b4092b6b51a
Fairway availability depth: Fixed SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3122
diff
changeset
|
745 label := fmt.Sprintf("%d", f.Year()) |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
746 return f, f.AddDate(1, 0, 0).Add(-time.Nanosecond), label |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
747 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
748 } |