annotate pkg/controllers/bottlenecks.go @ 5203:355195a90298 new-fwa

Start calculting the navigability. TODO: accumulate and do output.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 May 2020 18:59:14 +0200
parents fbc79c8459b4
children 7ca9e6c9a203
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
13 // * Sascha Wilde <wilde@intevation.de>
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
14
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 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
16
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
17 import (
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
18 "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
19 "database/sql"
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
20 "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
21 "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
22 "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
23 "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
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 = `
3689
268348a58c9c Fixed SQL in bottleneck/fairway-depth api.
Sascha Wilde <wilde@intevation.de>
parents: 3666
diff changeset
36 SELECT limiting FROM waterway.bottlenecks bn
3666
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
37 WHERE bn.validity @> current_timestamp AND objnam = $1
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
38 `
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
39
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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 JOIN waterway.bottlenecks bn
4073
21854740f433 Adapted fwa calculations (for diagrams) to new historization model.
Sascha Wilde <wilde@intevation.de>
parents: 4052
diff changeset
51 ON fa.bottleneck_id = bn.bottleneck_id
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
52 WHERE
3666
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
53 bn.validity @> current_timestamp 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
54 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
55 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
56 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
57 (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
58 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
59 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
60 ),
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 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
62 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
63 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
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 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
66 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
67 ),
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 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
69 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
70 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
71 )
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 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
73 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
74 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
75 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
76 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
77 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
78 `
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
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
80 selectGaugeLDCSQL = `
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
81 SELECT
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
82 grwl.value
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3208
diff changeset
83 FROM waterway.gauges_reference_water_levels grwl
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3208
diff changeset
84 JOIN waterway.bottlenecks bns
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3208
diff changeset
85 ON grwl.location = bns.gauge_location
4700
a0c320d89682 FA diagram: take latest available LDC.
Sascha Wilde <wilde@intevation.de>
parents: 4354
diff changeset
86 AND grwl.validity @> COALESCE(upper(bns.validity), current_timestamp)
a0c320d89682 FA diagram: take latest available LDC.
Sascha Wilde <wilde@intevation.de>
parents: 4354
diff changeset
87 WHERE lower(bns.validity) = (SELECT max(lower(validity))
a0c320d89682 FA diagram: take latest available LDC.
Sascha Wilde <wilde@intevation.de>
parents: 4354
diff changeset
88 FROM waterway.bottlenecks WHERE objnam = $1)
3666
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
89 AND bns.objnam = $1
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
90 AND grwl.depth_reference like 'LDC%'
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
91 `
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
92 )
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
93
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
94 // 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
95 var afdRefs = []float64{
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
96 230,
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
97 250,
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
98 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
99
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
100 // According to clarification, it has to be assumed, that at times
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
101 // with no data, the best case (which by convention is the highest
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
102 // class created by classify()) should be assumed. That is due to the
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
103 // fact, that at times where bottlenecks are not a limiting factor on
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
104 // the waterway, services don't provide any data for the bottleneck in
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
105 // question.
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
106 //
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
107 // FIXME: A potential improvement could be to intersect the time
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
108 // ranges with the time ranges where bottlenecks were "active" (this
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
109 // _might_ be derivable from the validity periods in the bottleneck
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
110 // data. So it _might_ be possible do detect actual missing data (BN
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
111 // valid, but no data from FA service). Anyway, this is left out for
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
112 // now, as many clarification regarding the base assumtions would be
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
113 // needed and the results still might be unrelyable.
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
114 func optimisticPadClassification(
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
115 from, to time.Time,
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
116 classified []time.Duration,
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
117 ) []time.Duration {
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
118
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
119 var actualDuration time.Duration
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
120 for _, v := range classified {
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
121 actualDuration += v
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
122 }
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
123
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
124 // If the actual duration is smaller than the length
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
125 // of the classifaction interval extend the
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
126 // time spend in the highest class by the difference.
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
127 if delta := to.Sub(from) - actualDuration; delta > 0 {
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
128 classified[len(classified)-1] += delta
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
129 }
4344
6ac94171a994 Fixed typos, tightened code removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4343
diff changeset
130
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
131 return classified
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
132 }
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
133
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3428
diff changeset
134 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
135 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
136 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
137 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
138 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
139 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
140 return percents
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
141 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
142
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
143 func intervalMode(mode string) int {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
144 switch strings.ToLower(mode) {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
145 case "monthly":
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
146 return 0
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
147 case "quarterly":
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
148 return 1
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
149 case "yearly":
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
150 return 2
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
151 default:
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
152 return 0
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
153 }
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
154 }
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
155
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
156 func loadDepthValues(
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
157 ctx context.Context,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
158 conn *sql.Conn,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
159 bottleneck string,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
160 los int,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
161 from, to time.Time,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
162 ) (availMeasurements, error) {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
163
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
164 rows, err := conn.QueryContext(
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
165 ctx, selectAvailableDepthSQL, bottleneck, los, from, to)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
166 if err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
167 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
168 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
169 defer rows.Close()
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
170
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
171 var ms availMeasurements
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
172
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
173 for rows.Next() {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
174 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
175 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
176 &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
177 &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
178 &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
179 &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
180 ); err != nil {
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
181 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
182 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
183 m.when = m.when.UTC()
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
184 ms = append(ms, m)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
185 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
186
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
187 if err := rows.Err(); err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
188 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
189 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
190
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
191 return ms, nil
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
192 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
193
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
194 func loadLDCReferenceValue(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
195 ctx context.Context,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
196 conn *sql.Conn,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
197 bottleneck string,
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
198 ) ([]float64, error) {
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
199 var value float64
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
200 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
201 switch {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
202 case err == sql.ErrNoRows:
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
203 return nil, nil
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
204 case err != nil:
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
205 return nil, err
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
206 }
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
207 return []float64{value}, nil
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
208 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
209
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
210 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
211 parts := strings.Split(breaks, ",")
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
212 var values []float64
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
213
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
214 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
215 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
216 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
217 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
218 }
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
219 }
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
220
5198
0e91d200299d Removed duplicated code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5196
diff changeset
221 return common.DedupFloat64s(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
222 }
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
223
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
224 func bottleneckAvailabilty(rw http.ResponseWriter, req *http.Request) {
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3428
diff changeset
225
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
226 mode := intervalMode(req.FormValue("mode"))
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
227 bn := mux.Vars(req)["objnam"]
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
228
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
229 if bn == "" {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
230 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
231 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
232 "Missing objnam of bottleneck",
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
233 http.StatusBadRequest,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
234 )
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
235 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
236 }
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
237
5196
5bc8daa986d9 More code movement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5195
diff changeset
238 from, to, ok := parseFromTo(rw, req)
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
239 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
240 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
241 }
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
242
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
243 los, ok := parseFormInt(rw, req, "los", 1)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
244 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
245 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
246 }
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
247
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
248 conn := middleware.GetDBConn(req)
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
249 ctx := req.Context()
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
250
4702
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
251 var limiting string
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
252 err := conn.QueryRowContext(ctx, selectLimitingSQL, bn).Scan(&limiting)
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
253 switch {
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
254 case err == sql.ErrNoRows:
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
255 http.Error(
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
256 rw, fmt.Sprintf("Unknown limitation for %s.", bn),
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
257 http.StatusNotFound)
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
258 return
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
259 case err != nil:
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
260 http.Error(
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
261 rw, fmt.Sprintf("DB error: %v.", err),
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
262 http.StatusInternalServerError)
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
263 return
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
264 }
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
265
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
266 access := limitingFactor(limiting)
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
267
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
268 ldcRefs, err := loadLDCReferenceValue(ctx, conn, bn)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
269 if err != nil {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
270 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
271 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
272 fmt.Sprintf("Internal server error: %v", err),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
273 http.StatusInternalServerError,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
274 )
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
275 return
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
276 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
277
3412
5e284f39a081 fairway availibility: Reduce defaults to 230 and 250 as class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3405
diff changeset
278 if len(ldcRefs) == 0 {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
279 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
280 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
281 "No gauge reference values found for bottleneck",
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
282 http.StatusNotFound,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
283 )
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
284 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
285 }
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
286
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
287 var breaks []float64
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
288 if b := req.FormValue("breaks"); b != "" {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
289 breaks = breaksToReferenceValue(b)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
290 } else {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
291 breaks = afdRefs
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
292 }
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
293
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3428
diff changeset
294 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
295
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
296 var ms availMeasurements
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
297 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
298 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
299 }
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
300
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
301 if len(ms) == 0 {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
302 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
303 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
304 "No available fairway depth values found",
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
305 http.StatusNotFound,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
306 )
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
307 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
308 }
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
309
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
310 rw.Header().Add("Content-Type", "text/csv")
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
311
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
312 out := csv.NewWriter(rw)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
313
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
314 record := make([]string, 1+2+len(breaks)+1)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
315 record[0] = "#time"
4354
e357730c090a FA: Fixed units in csv headers.
Sascha Wilde <wilde@intevation.de>
parents: 4353
diff changeset
316 record[1] = fmt.Sprintf("# < LDC (%.1f) [%%]", ldcRefs[0])
e357730c090a FA: Fixed units in csv headers.
Sascha Wilde <wilde@intevation.de>
parents: 4353
diff changeset
317 record[2] = fmt.Sprintf("# >= LDC (%.1f) [%%]", ldcRefs[0])
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
318 for i, v := range breaks {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
319 if i == 0 {
3455
188b55ef814f Slightly improved generated CSV headers, also used as legend by client.
Sascha Wilde <wilde@intevation.de>
parents: 3445
diff changeset
320 record[3] = fmt.Sprintf("#d < %.1f [%%]", v)
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
321 }
3455
188b55ef814f Slightly improved generated CSV headers, also used as legend by client.
Sascha Wilde <wilde@intevation.de>
parents: 3445
diff changeset
322 record[i+4] = fmt.Sprintf("#d >= %.1f [%%]", v)
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
323 }
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
324
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
325 if err := out.Write(record); err != nil {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
326 // Too late for HTTP status message.
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
327 log.Printf("error: %v\n", err)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
328 return
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
329 }
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
330
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
331 interval := intervals[mode](from, to)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
332
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
333 now := time.Now()
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
334 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() {
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
335 // Don't interpolate for the future
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
336 if now.Sub(pto) < 0 {
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
337 pto = now
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
338 }
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
339
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
340 lnwl := ms.classify(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
341 pfrom, pto,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
342 ldcRefs,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
343 (*availMeasurement).getValue,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
344 )
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
345
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
346 afd := ms.classify(
3468
850e81f13e96 fairway availibility: Fixed time interval for line.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3455
diff changeset
347 pfrom, pto,
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
348 breaks,
4702
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
349 access,
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
350 )
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
351
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
352 duration := pto.Sub(pfrom)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
353 lnwlPercents := durationsToPercentage(duration, lnwl)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
354 afdPercents := durationsToPercentage(duration, afd)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
355
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
356 record[0] = label
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
357 for i, v := range lnwlPercents {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
358 record[1+i] = fmt.Sprintf("%.3f", v)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
359 }
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
360 for i, v := range afdPercents {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
361 record[3+i] = fmt.Sprintf("%.3f", v)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
362 }
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
363
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
364 if err := out.Write(record); err != nil {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
365 // Too late for HTTP status message.
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
366 log.Printf("error: %v\n", err)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
367 return
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
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 }
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
370
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
371 out.Flush()
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
372 if err := out.Error(); err != nil {
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
373 // Too late for HTTP status message.
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
374 log.Printf("error: %v\n", err)
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
375 }
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 }
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
377
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
378 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
379
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3393
diff changeset
380 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
381
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
382 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
383 if bn == "" {
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
384 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
385 rw, "Missing objnam of bottleneck",
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
386 http.StatusBadRequest)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
387 return
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
388 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
389
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
390 from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
391 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
392 return
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
393 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
394
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
395 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
396 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
397 return
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
398 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
399
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
400 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
401 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
402 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
403
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
404 los, ok := parseFormInt(rw, req, "los", 1)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
405 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
406 return
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
407 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
408
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
409 conn := middleware.GetDBConn(req)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
410 ctx := req.Context()
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
411
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
412 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
413 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
414 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
415 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
416 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
417 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
418 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
419 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
420 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
421 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
422 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
423 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
424 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
425 }
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
426
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
427 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
428
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3393
diff changeset
429 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
430
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
431 // load the measurements
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
432 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
433 if err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
434 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
435 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
436 http.StatusInternalServerError)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
437 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
438 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
439
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
440 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
441 if err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
442 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
443 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
444 http.StatusInternalServerError)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
445 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
446 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
447 if len(ldcRefs) == 0 {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
448 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
449 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
450 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
451
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3412
diff changeset
452 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
453 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
454 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
455 } 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
456 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
457 }
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
458
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
459 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
460
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
461 out := csv.NewWriter(rw)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
462
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
463 // label, ldc, classes
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
464 record := make([]string, 1+2+len(breaks)+1)
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
465 record[0] = "#time"
4353
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
466 record[1] = fmt.Sprintf("# < LDC (%.1f) [d]", ldcRefs[0])
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
467 record[2] = fmt.Sprintf("# >= LDC (%.1f) [d]", ldcRefs[0])
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
468 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
469 if i == 0 {
4353
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
470 record[3] = fmt.Sprintf("# < %.1f [d]", v)
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
471 }
4353
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
472 record[i+4] = fmt.Sprintf("# >= %.1f [d]", v)
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
473 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
474
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
475 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
476 // 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
477 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
478 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
479 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
480
3367
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
481 //log.Println(len(ms))
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
482 //for i := range ms {
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
483 // 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
484 //}
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
485
3367
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
486 log.Printf("info: measurements: %d\n", len(ms))
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
487 if len(ms) > 1 {
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
488 log.Printf("info: first: %v\n", ms[0].when)
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
489 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
490 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
491 }
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
492
3367
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
493 interval := intervals[mode](from, to)
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
494
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
495 now := time.Now()
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
496 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() {
4343
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
497 // Don't interpolate for the future
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
498 if now.Sub(pto) < 0 {
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
499 pto = now
63c25eb9c07c FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents: 4149
diff changeset
500 }
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
501
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
502 ldc := ms.classify(
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
503 pfrom, pto,
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
504 ldcRefs,
4702
ef21c1464843 FA: fix inconsistency in access to values of LDCs and limiting value.
Sascha Wilde <wilde@intevation.de>
parents: 4700
diff changeset
505 (*availMeasurement).getValue,
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
506 )
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
507
3367
ecb4baa2be1a Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3337
diff changeset
508 ranges := ms.classify(
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
509 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
510 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
511 access,
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
512 )
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
513
4353
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
514 // Round to full days
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
515 ldcRounded := common.RoundToFullDays(ldc)
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
516 rangesRounded := common.RoundToFullDays(ranges)
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
517
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
518 record[0] = label
4353
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
519 for i, v := range ldcRounded {
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
520 record[i+1] = fmt.Sprintf("%d", v)
3445
e07b18f2482e Write fairway-depth as CSV, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
521 }
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
522
4353
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
523 for i, d := range rangesRounded {
97312d7954ba FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents: 4344
diff changeset
524 record[3+i] = fmt.Sprintf("%d", d)
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
525 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
526
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
527 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
528 // 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
529 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
530 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
531 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
532 }
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
533
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
534 out.Flush()
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
535 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
536 // 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
537 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
538 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
539 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
540
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
541 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
542 monthly,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
543 quarterly,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
544 yearly,
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
545 }
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
546
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
547 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
548 pfrom := from
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
549 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
550 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
551 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
552 }
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
553 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
554 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
555 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
556 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
557 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
558 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
559
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
560 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
561 pfrom := from
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
562 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
563 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
564 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
565 }
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
566 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
567 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
568 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
569 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
570 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
571 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
572
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
573 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
574 pfrom := from
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
575 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
576 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
577 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
578 }
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
579 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
580 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
581 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
582 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
583 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
584 }