annotate pkg/controllers/bottlenecks.go @ 3201:d5294f1a4ad4

First fix for waterlevel classifications.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 08 May 2019 16:00:24 +0200
parents 7b4092b6b51a
children 3bba82bcb6a6
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>
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
17 "context"
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
19 "encoding/csv"
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "fmt"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "log"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "net/http"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "sort"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 "strconv"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 "strings"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 "time"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
3117
6b5132fd385e Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3116
diff changeset
28 "github.com/gorilla/mux"
6b5132fd385e Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3116
diff changeset
29
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 "gemma.intevation.de/gemma/pkg/common"
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
31 "gemma.intevation.de/gemma/pkg/middleware"
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 )
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 const (
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
35 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
36 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
37 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
38 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
39 efa.available_depth_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
40 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
41 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
42 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
43 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
44 JOIN waterway.bottlenecks bn
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 ON fa.bottleneck_id = bn.id
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 WHERE
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 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
48 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
49 efa.measure_type = 'Measured' 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
50 efa.available_depth_value IS NOT NULL 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
51 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
52 ),
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 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
54 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
55 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
56 ),
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 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
58 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
59 ),
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 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
61 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
62 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
63 )
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 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
65 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
66 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
67 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
68 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
69 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
70 `
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 selectGaugeLevelsSQL = `
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 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
74 grwl.depth_reference,
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 grwl.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
76 FROM waterway.gauges_reference_water_levels grwl JOIN
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 waterway.bottlenecks bns
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 ON bns.fk_g_fid = grwl.gauge_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
79 WHERE bns.objnam = $1 AND (
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 grwl.depth_reference like 'HDC%' OR
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 grwl.depth_reference like 'LDC%' OR
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 grwl.depth_reference like 'MW%'
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 )
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
84 `
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
85 selectGaugeLDCSQL = `
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
86 SELECT
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
87 grwl.value
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
88 FROM waterway.gauges_reference_water_levels grwl JOIN
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
89 waterway.bottlenecks bns
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
90 ON bns.fk_g_fid = grwl.gauge_id
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
91 WHERE bns.objnam = $1 AND grwl.depth_reference like 'LDC%'
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
92 `
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
93 )
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
94
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
95 type (
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
96 referenceValue struct {
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
97 level int
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
98 value float64
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
99 }
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
100
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
101 availMeasurement struct {
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
102 when time.Time
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
103 depth int
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
104 value int
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
105 }
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
106
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
107 availMeasurements []availMeasurement
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
108 )
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
109
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
110 // afdRefs are the typical available fairway depth reference values.
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
111 var afdRefs = []referenceValue{
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
112 {0, 200},
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
113 {1, 230},
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
114 {2, 250},
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
115 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
116
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
117 func (measurement *availMeasurement) getDepth() float64 {
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
118 return float64(measurement.depth)
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
119 }
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
120
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
121 func (measurement *availMeasurement) getValue() float64 {
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
122 return float64(measurement.value)
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
123 }
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
124
c68cef0346b7 Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3076
diff changeset
125 func (measurements availMeasurements) classifyAvailMeasurements(
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
126 from, to time.Time,
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
127 classes []referenceValue,
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
128 access func(*availMeasurement) float64,
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
129 ) []time.Duration {
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
130
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
131 type classValues struct {
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
132 when time.Time
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
133 kind common.ValueRangeKind
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
134 }
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
135
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
136 //var invalid time.Duration
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
137 result := make([]time.Duration, len(classes)+1)
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
138
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
139 if len(measurements) == 0 ||
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
140 to.Before(measurements[0].when) ||
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
141 from.After(measurements[len(measurements)-1].when) {
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
142 log.Println("empty")
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
143 return result
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
144 }
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
145
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
146 cvs := make([]classValues, len(classes))
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
147
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
148 classify := func(v func(float64) (time.Time, common.ValueRangeKind)) {
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
149 for i := range classes {
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
150 cvs[i].when, cvs[i].kind = v(classes[i].value)
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
151 }
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
152 }
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
153
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
154 vbt := func(p1, p2 *availMeasurement) func(time.Time) (float64, common.ValueRangeKind) {
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
155 return common.InterpolateValueByTime(p1.when, access(p1), p2.when, access(p2))
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
156 }
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
157
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
158 pairs:
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
159 for i := 0; i < len(measurements)-1; i++ {
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
160 p1 := &measurements[i]
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
161 p2 := &measurements[i+1]
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
162
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
163 var start, end time.Time
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
164
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
165 switch {
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
166 case !p2.when.After(p1.when):
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
167 // Segment invalid
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
168 // log.Println("invalid")
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
169 continue pairs
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
170
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
171 case p1.when.After(to) || p2.when.Before(from):
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
172 // Segment complete outside.
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
173 // log.Println("complete outside")
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
174 continue pairs
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
175
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
176 case p1.when.After(from) && p2.when.Before(to):
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
177 // (from-to) is complete inside segment.
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
178 // invalid += p1.when.Sub(from)
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
179 // invalid += to.Sub(p2.when)
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
180 // log.Println("complete inside")
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
181 v := vbt(p1, p2)
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
182 f, _ := v(from)
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
183 t, _ := v(to)
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
184 classify(common.InterpolateTimeByValue(from, f, to, t))
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
185 start, end = from, to
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
186
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
187 case p1.when.After(from):
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
188 // from is inside segment
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
189 // invalid += p1.when.Sub(from)
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
190 // log.Println("from is inside")
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
191 f, _ := vbt(p1, p2)(from)
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
192 classify(common.InterpolateTimeByValue(
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
193 from, f,
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
194 p2.when, access(p2),
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
195 ))
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
196 start, end = from, p2.when
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
197
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
198 case p2.when.Before(to):
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
199 // to is inside segment
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
200 // invalid += to.Sub(p2.when)
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
201 // log.Println("to is inside")
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
202 t, _ := vbt(p1, p2)(to)
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
203 classify(common.InterpolateTimeByValue(
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
204 p1.when, access(p1),
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
205 to, t,
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
206 ))
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
207 start, end = p1.when, to
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
208
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
209 case !p1.when.Before(from) && !to.After(p2.when):
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
210 // Segment complete inside.
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
211 // log.Println("is complete inside")
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
212 classify(common.InterpolateTimeByValue(
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
213 p1.when, access(p1),
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
214 p2.when, access(p2),
3027
84e6577a474b Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2967
diff changeset
215 ))
3036
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
216 start, end = p1.when, p2.when
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
217 default:
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
218 log.Println("warn: unexpected case. That should not happen.")
7f12a87c56ff Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3027
diff changeset
219 continue pairs
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
220 }
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
221
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
222 for i := len(cvs) - 1; i >= 0; i-- {
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
223 switch cvs[i].kind {
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
224 case common.ValueAbove:
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
225 result[i+1] += end.Sub(start)
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
226 continue pairs
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
227
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
228 case common.ValueInside:
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
229 // -> split
3040
a661e9b8f3b6 Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3038
diff changeset
230 if access(p1) < classes[i].value {
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
231 // started below -> second part above
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
232 result[i+1] = end.Sub(cvs[i].when)
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
233 end = cvs[i].when
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
234 } else {
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
235 // started above -> first part above
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
236 result[i+1] = cvs[i].when.Sub(start)
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
237 start = cvs[i].when
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
238 }
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
239 }
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
240 }
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
241 result[0] += end.Sub(start)
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
242 }
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
243
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
244 return result
2967
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
245 }
7c301ff449bc Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2962
diff changeset
246
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
247 func durationsToPercentage(from, to time.Time, classes []time.Duration) []float64 {
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
248 percents := make([]float64, len(classes))
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
249 total := 100 / to.Sub(from).Seconds()
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
250 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
251 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
252 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
253 return percents
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
254 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
255
3115
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
256 func parseTime(s, what string) (time.Time, error) {
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
257 var t time.Time
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
258 var err error
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
259 if t, err = time.Parse(common.TimeFormat, s); err != nil {
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
260 return time.Time{}, JSONError{
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
261 Code: http.StatusBadRequest,
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
262 Message: fmt.Sprintf(
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
263 "Invalid time format for '%s' field: %v", what, err),
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
264 }
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
265 }
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
266 return t.UTC(), nil
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
267 }
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
268
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
269 func parseInt(s, what string) (int, error) {
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
270 i, err := strconv.Atoi(s)
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
271 if err != nil {
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
272 return 0, JSONError{
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
273 Code: http.StatusBadRequest,
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
274 Message: fmt.Sprintf(
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
275 "Invalid value for field '%s': %v", what, err),
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
276 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
277 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
278 return i, nil
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
279 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
280
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
281 func loadDepthValues(
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
282 ctx context.Context,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
283 conn *sql.Conn,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
284 bottleneck string,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
285 los int,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
286 from, to time.Time,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
287 ) (availMeasurements, error) {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
288
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
289 rows, err := conn.QueryContext(
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
290 ctx, selectAvailableDepthSQL, bottleneck, los, from, to)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
291 if err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
292 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
293 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
294 defer rows.Close()
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
295
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
296 var ms availMeasurements
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
297
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
298 for rows.Next() {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
299 var m availMeasurement
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
300 if err := rows.Scan(&m.when, &m.depth, &m.value); err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
301 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
302 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
303 m.when = m.when.UTC()
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
304 ms = append(ms, m)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
305 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
306
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
307 if err := rows.Err(); err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
308 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
309 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
310
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
311 return ms, nil
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
312 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
313
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
314 func loadLDCReferenceValue(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
315 ctx context.Context,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
316 conn *sql.Conn,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
317 bottleneck string,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
318 ) ([]referenceValue, error) {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
319
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
320 var value float64
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
321 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
322 switch {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
323 case err == sql.ErrNoRows:
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
324 return nil, nil
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
325 case err != nil:
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
326 return nil, err
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
327 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
328 return []referenceValue{{0, value}}, nil
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
329 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
330
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
331 func loadLNWLReferenceValues(
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
332 ctx context.Context,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
333 conn *sql.Conn,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
334 bottleneck string,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
335 ) ([]referenceValue, error) {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
336 rows, err := conn.QueryContext(ctx, selectGaugeLevelsSQL, bottleneck)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
337 if err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
338 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
339 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
340 defer rows.Close()
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
341
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
342 var levels []referenceValue
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
343
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
344 loop:
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
345 for rows.Next() {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
346 var what string
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
347 var value int
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
348 if err := rows.Scan(&what, &value); err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
349 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
350 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
351 var level int
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
352 switch {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
353 case strings.HasPrefix(what, "LDC"):
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
354 level = 0
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
355 case strings.HasPrefix(what, "MW"):
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
356 level = 1
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
357 case strings.HasPrefix(what, "HDC"):
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
358 level = 2
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
359 default:
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
360 return nil, fmt.Errorf("Unexpected reference level type '%s'", what)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
361 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
362 for i := range levels {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
363 if levels[i].level == level {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
364 levels[i].value = float64(value)
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
365 continue loop
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
366 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
367 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
368 levels = append(levels, referenceValue{
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
369 level: level,
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
370 value: float64(value),
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
371 })
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
372 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
373
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
374 if err := rows.Err(); err != nil {
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
375 return nil, err
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
376 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
377
3117
6b5132fd385e Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3116
diff changeset
378 sort.Slice(levels, func(i, j int) bool {
6b5132fd385e Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3116
diff changeset
379 return levels[i].level < levels[j].level
6b5132fd385e Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3116
diff changeset
380 })
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
381
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
382 return levels, nil
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
383 }
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
384
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
385 func bottleneckAvailabilty(
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
386 _ interface{},
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
387 req *http.Request,
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
388 conn *sql.Conn,
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
389 ) (jr JSONResult, err error) {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
390 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
391
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
392 if bn == "" {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
393 err = JSONError{
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
394 Code: http.StatusBadRequest,
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
395 Message: "Missing objnam of bottleneck",
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
396 }
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
397 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
398 }
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
399
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
400 var from, to time.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
401
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
402 if f := req.FormValue("from"); f != "" {
3115
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
403 if from, err = parseTime(f, "from"); err != nil {
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
404 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
405 }
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
406 } else {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
407 from = time.Now().AddDate(-1, 0, 0).UTC()
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
408 }
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
409
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
410 if t := req.FormValue("to"); t != "" {
3115
524bc6545b20 Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3114
diff changeset
411 if to, err = parseTime(t, "to"); err != nil {
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
412 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
413 }
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
414 } else {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
415 to = from.AddDate(1, 0, 0).UTC()
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
416 }
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
417
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
418 if to.Before(from) {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
419 to, from = from, to
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
420 }
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
421
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
422 log.Printf("info: time interval: (%v - %v)\n", from, to)
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
423
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
424 var los int
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
425 if l := req.FormValue("los"); l != "" {
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
426 if los, err = parseInt(l, "los"); err != nil {
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
427 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
428 }
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
429 } else {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
430 los = 1
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
431 }
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
432
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
433 ctx := req.Context()
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
434
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
435 var lnwlRefs []referenceValue
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
436 if lnwlRefs, err = loadLNWLReferenceValues(ctx, conn, bn); err != nil {
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
437 return
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
438 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
439
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
440 if len(lnwlRefs) == 0 {
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
441 err = JSONError{
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
442 Code: http.StatusNotFound,
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
443 Message: "No gauge reference values found for bottleneck",
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
444 }
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
445 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
446 }
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
447
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
448 var ms availMeasurements
3116
ff0b9a94e0e4 Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3115
diff changeset
449 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
450 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
451 }
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
452
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
453 if len(ms) == 0 {
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
454 err = JSONError{
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
455 Code: http.StatusNotFound,
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
456 Message: "No available fairway depth values found",
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
457 }
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
458 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
459 }
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
460
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
461 lnwl := ms.classifyAvailMeasurements(
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
462 from, to,
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
463 lnwlRefs,
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
464 (*availMeasurement).getValue,
3038
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
465 )
d47d289d6e68 Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3036
diff changeset
466
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
467 afd := ms.classifyAvailMeasurements(
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
468 from, to,
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
469 afdRefs,
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
470 (*availMeasurement).getDepth,
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
471 )
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
472
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
473 lnwlPercents := durationsToPercentage(from, to, lnwl)
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
474 afdPercents := durationsToPercentage(from, to, afd)
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
475
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
476 type lnwlOutput struct {
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
477 Level string `json:"level"`
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
478 Value float64 `json:"value"`
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
479 Percent float64 `json:"percent"`
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
480 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
481
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
482 type afdOutput struct {
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
483 Value float64 `json:"value"`
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
484 Percent float64 `json:"percent"`
2962
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
485 }
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
486
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
487 type output struct {
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
488 LNWL []lnwlOutput `json:"lnwl"`
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
489 AFD []afdOutput `json:"afd"`
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
490 }
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
491
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
492 out := output{}
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
493
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
494 for i := range lnwlRefs {
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
495 var level string
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
496 switch lnwlRefs[i].level {
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
497 case 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
498 level = "LDC"
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
499 case 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
500 level = "MW"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
501 case 2:
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
502 level = "HDC"
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
503 }
3076
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
504 out.LNWL = append(out.LNWL, lnwlOutput{
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
505 Level: level,
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
506 Value: lnwlRefs[i].value,
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
507 Percent: lnwlPercents[i],
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
508 })
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
509 }
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
510
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
511 for i := range afdRefs {
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
512 out.AFD = append(out.AFD, afdOutput{
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
513 Value: afdRefs[i].value,
e0daeb05bf50 Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3040
diff changeset
514 Percent: afdPercents[i],
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
515 })
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
516 }
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
517
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
518 jr = JSONResult{Result: &out}
8d825551bc72 Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
519 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
520 }
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
521
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
522 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
523
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
524 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
525
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
526 if bn == "" {
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
527 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
528 rw, "Missing objnam of bottleneck",
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
529 http.StatusBadRequest)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
530 return
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
531 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
532
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
533 var mode int
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
534 if m := req.FormValue("mode"); m != "" {
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
535 switch strings.ToLower(m) {
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
536 case "monthly":
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
537 mode = 0
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
538 case "quarterly":
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
539 mode = 1
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
540 case "yearly":
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
541 mode = 2
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
542 default:
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
543 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
544 rw, fmt.Sprintf("Unknown 'mode' value %s.", m),
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
545 http.StatusBadRequest)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
546 return
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
547 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
548 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
549
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
550 var from, to time.Time
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
551
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
552 if f := req.FormValue("from"); f != "" {
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
553 var err error
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
554 if from, err = time.Parse(common.TimeFormat, f); err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
555 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
556 rw, fmt.Sprintf("Invalid format for 'from': %v.", err),
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
557 http.StatusBadRequest)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
558 return
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
559 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
560 } else {
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
561 from = time.Now().AddDate(-1, 0, 0)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
562 }
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
563 from = from.UTC()
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
564
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
565 if t := req.FormValue("to"); t != "" {
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
566 var err error
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
567 if to, err = time.Parse(common.TimeFormat, t); err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
568 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
569 rw, fmt.Sprintf("Invalid format for 'to': %v.", err),
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
570 http.StatusBadRequest)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
571 return
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
572 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
573 } else {
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
574 to = from.AddDate(1, 0, 0)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
575 }
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
576 to = to.UTC()
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
577
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
578 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
579 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
580 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
581
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
582 log.Printf("info: time interval: (%v - %v)\n", from, to)
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
583
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
584 var los int
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
585 if l := req.FormValue("los"); l != "" {
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
586 var err error
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
587 if los, err = strconv.Atoi(l); err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
588 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
589 rw, fmt.Sprintf("Invalid format for 'los': %v.", err),
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
590 http.StatusBadRequest)
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
591 return
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
592 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
593 } else {
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
594 los = 1
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
595 }
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
596
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
597 conn := middleware.GetDBConn(req)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
598 ctx := req.Context()
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
599
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
600 // load the measurements
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
601 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
602 if err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
603 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
604 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
605 http.StatusInternalServerError)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
606 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
607 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
608
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
609 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
610 if err != nil {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
611 http.Error(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
612 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
613 http.StatusInternalServerError)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
614 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
615 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
616 if len(ldcRefs) == 0 {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
617 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
618 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
619 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
620
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
621 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
622
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
623 out := csv.NewWriter(rw)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
624
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
625 // label, classes, lnwl
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
626 record := make([]string, 1+1+len(afdRefs)+1)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
627 record[0] = "#label"
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
628 record[1] = "# >= LDC [h]"
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
629 for i, v := range afdRefs {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
630 if i == 0 {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
631 record[2] = fmt.Sprintf("# < %.2f [h]", v.value)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
632 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
633 record[i+3] = fmt.Sprintf("# >= %.2f [h]", v.value)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
634 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
635
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
636 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
637 // 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
638 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
639 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
640 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
641
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
642 // log.Println(len(ms))
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
643 //for i := range ms {
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
644 // 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
645 //}
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
646
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
647 interval := intervals[mode](from, to)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
648
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
649 //log.Printf("first: %v\n", ms[0].when)
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
650 //log.Printf("last: %v\n", ms[len(ms)-1].when)
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
651
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
652 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
653
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
654 //log.Printf("pfrom: %v\n", pfrom)
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
655
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
656 // Find good starting point
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
657 idx := sort.Search(len(ms), func(i int) bool {
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
658 return !ms[i].when.Before(pfrom)
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
659 })
3201
d5294f1a4ad4 First fix for waterlevel classifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3133
diff changeset
660 //log.Printf("%d\n", idx)
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
661 if idx > 0 {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
662 idx--
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
663 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
664 samples := ms[idx:]
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
665
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
666 ranges := samples.classifyAvailMeasurements(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
667 pfrom, pto,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
668 afdRefs,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
669 (*availMeasurement).getDepth,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
670 )
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
671
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
672 ldc := samples.classifyAvailMeasurements(
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
673 pfrom, pto,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
674 ldcRefs,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
675 (*availMeasurement).getDepth,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
676 )
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
677
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
678 record[0] = label
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
679 record[1] = fmt.Sprintf("%.3f", ldc[1].Hours()/24)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
680
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
681 for i, d := range ranges {
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
682 record[2+i] = fmt.Sprintf("%.3f", d.Hours()/24)
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
683 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
684
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
685 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
686 // 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
687 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
688 return
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
689 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
690 }
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
691
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
692 out.Flush()
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
693 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
694 // 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
695 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
696 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
697 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
698
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
699 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
700 monthly,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
701 quarterly,
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
702 yearly,
3118
4dcbf9e9013c Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3117
diff changeset
703 }
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
704
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
705 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
706 pfrom := from
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
707 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
708 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
709 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
710 }
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
711 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
712 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
713 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
714 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
715 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
716 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
717
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
718 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
719 pfrom := from
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
720 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
721 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
722 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
723 }
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
724 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
725 pfrom = pfrom.AddDate(0, 3, 0)
3133
7b4092b6b51a Fairway availability depth: Fixed SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3122
diff changeset
726 label := fmt.Sprintf("Q%d-%d", int(f.Month())/4+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
727 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
728 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
729 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
730
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
731 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
732 pfrom := from
3119
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
733 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
734 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
735 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
736 }
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
737 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
738 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
739 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
740 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
741 }
ad5a00ccd276 Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3118
diff changeset
742 }