Mercurial > gemma
annotate pkg/controllers/gauges.go @ 4484:819eafe60f41
Merged
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Thu, 26 Sep 2019 12:16:23 +0200 |
parents | 4394daeea96a |
children | 3b3cf2083730 |
rev | line source |
---|---|
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
17 "context" |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
18 "database/sql" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "encoding/csv" |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "fmt" |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "log" |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
22 "math" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 "net/http" |
2762
f95ec0bb565c
Added endpoint to deliver average waterlevels for a given gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2741
diff
changeset
|
24 "sort" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "strconv" |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
26 "strings" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 "time" |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 "github.com/gorilla/mux" |
2826
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
30 "github.com/jackc/pgx/pgtype" |
2762
f95ec0bb565c
Added endpoint to deliver average waterlevels for a given gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2741
diff
changeset
|
31 "gonum.org/v1/gonum/stat" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
33 "gemma.intevation.de/gemma/pkg/common" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 "gemma.intevation.de/gemma/pkg/models" |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
35 |
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
36 mw "gemma.intevation.de/gemma/pkg/middleware" |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 ) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 const ( |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
40 selectPredictedObserveredSQL = ` |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
41 SELECT |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
42 measure_date, |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
43 date_issue, |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
44 predicted, |
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
45 water_level |
3277
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
46 FROM ( |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
47 SELECT |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
48 location, |
3277
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
49 measure_date, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
50 date_issue, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
51 false AS predicted, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
52 water_level |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
53 FROM waterway.gauge_measurements |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
54 UNION ALL |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
55 SELECT |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
56 location, |
3277
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
57 measure_date, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
58 date_issue, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
59 true AS predicted, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
60 water_level |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
61 FROM waterway.gauge_predictions |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
62 ) AS gmp |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
63 WHERE |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
64 location = ( |
2763
a06d11d1f0b3
Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2762
diff
changeset
|
65 $1::char(2), |
a06d11d1f0b3
Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2762
diff
changeset
|
66 $2::char(3), |
a06d11d1f0b3
Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2762
diff
changeset
|
67 $3::char(5), |
a06d11d1f0b3
Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2762
diff
changeset
|
68 $4::char(5), |
a06d11d1f0b3
Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2762
diff
changeset
|
69 $5::int |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
70 ) AND |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
71 measure_date BETWEEN |
2884
fe3d5e824ee9
calculate interval boundaries for Nash Sutcliffe in right order.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
72 $6::timestamp - '72hours'::interval AND $6::timestamp |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
73 ORDER BY measure_date, date_issue |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
74 ` |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 selectWaterlevelsSQL = ` |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 SELECT |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 measure_date, |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 water_level, |
2783
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
79 value_min, |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
80 value_max, |
2766
1f173d1a731d
Fixed broken SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2763
diff
changeset
|
81 predicted |
3277
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
82 FROM ( |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
83 SELECT |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
84 location, |
3277
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
85 measure_date, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
86 date_issue, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
87 water_level, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
88 NULL AS value_min, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
89 NULL AS value_max, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
90 false AS predicted |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
91 FROM waterway.gauge_measurements |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
92 UNION ALL |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
93 SELECT |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
94 location, |
3277
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
95 measure_date, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
96 date_issue, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
97 water_level, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
98 lower(conf_interval) AS value_min, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
99 upper(conf_interval) AS value_max, |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
100 true AS predicted |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
101 FROM waterway.gauge_predictions |
232fc90e6ee2
Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents:
3223
diff
changeset
|
102 ) AS gmp |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 WHERE |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 ` |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
105 |
2826
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
106 selectAllWaterlevelsMeasuredRangeSQL = ` |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
107 SELECT |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
108 min(measure_date), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
109 max(measure_date) |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
110 FROM waterway.gauge_measurements |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
111 WHERE |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
112 location = ( |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
113 $1::char(2), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
114 $2::char(3), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
115 $3::char(5), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
116 $4::char(5), |
2826
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
117 $5::int |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
118 )::isrs |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
119 AND staging_done |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
120 ` |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
121 |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
122 selectAllWaterlevelsMeasuredSQL = ` |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
123 SELECT |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
124 extract(day from measure_date)::varchar || ':' || |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
125 extract(month from measure_date)::varchar AS day_month, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
126 percentile_disc(0.25) within group (order by water_level) AS q25, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
127 percentile_disc(0.5) within group (order by water_level) AS median, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
128 percentile_disc(0.75) within group (order by water_level) AS q75, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
129 avg(water_level) AS mean, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
130 min(water_level) AS min, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
131 max(water_level) AS max |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
132 FROM waterway.gauge_measurements |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
133 WHERE |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
134 location = ( |
2826
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
135 $1::char(2), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
136 $2::char(3), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
137 $3::char(5), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
138 $4::char(5), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
139 $5::int |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
140 )::isrs |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
141 AND staging_done |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
142 GROUP BY extract(day from measure_date)::varchar || ':' || |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
143 extract(month from measure_date)::varchar; |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
144 ` |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
145 selectYearWaterlevelsMeasuredSQL = ` |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
146 SELECT |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
147 measure_date, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
148 water_level |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
149 FROM waterway.gauge_measurements |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
150 WHERE |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
151 location = ( |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
152 $1::char(2), |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
153 $2::char(3), |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
154 $3::char(5), |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
155 $4::char(5), |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
156 $5::int |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
157 )::isrs |
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
158 AND staging_done |
2806
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
159 AND measure_date BETWEEN $6 AND $7 |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
160 ORDER BY measure_date |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
161 ` |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
162 ) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
163 |
2783
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
164 func float64format(v float64) string { |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
165 return strconv.FormatFloat(v, 'f', -1, 64) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
166 } |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
167 |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
168 func nullFloat64format(v sql.NullFloat64) string { |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
169 if v.Valid { |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
170 return float64format(v.Float64) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
171 } |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
172 return "" |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
173 } |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
174 |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
175 func boolFormat(b bool) string { |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
176 if b { |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
177 return "t" |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
178 } |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
179 return "f" |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
180 } |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
181 |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
182 func yearWaterlevels(rw http.ResponseWriter, req *http.Request) { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
183 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
184 gauge := mux.Vars(req)["gauge"] |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
185 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
186 isrs, err := models.IsrsFromString(gauge) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
187 if err != nil { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
188 http.Error( |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
189 rw, fmt.Sprintf("error: Invalid ISRS code: %v", err), |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
190 http.StatusBadRequest) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
191 return |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
192 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
193 |
2806
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
194 year, _ := strconv.Atoi(mux.Vars(req)["year"]) |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
195 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
196 conn := mw.GetDBConn(req) |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
197 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
198 ctx := req.Context() |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
199 |
2806
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
200 begin := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC) |
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
201 end := time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.UTC).Add(-time.Microsecond) |
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
202 |
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
203 log.Printf("info: begin %s\n", begin) |
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
204 log.Printf("info: end %s\n", end) |
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
205 |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
206 rows, err := conn.QueryContext( |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
207 ctx, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
208 selectYearWaterlevelsMeasuredSQL, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
209 isrs.CountryCode, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
210 isrs.LoCode, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
211 isrs.FairwaySection, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
212 isrs.Orc, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
213 isrs.Hectometre, |
2806
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
214 begin, |
c6374c520228
Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2803
diff
changeset
|
215 end, |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
216 ) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
217 if err != nil { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
218 http.Error( |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
219 rw, fmt.Sprintf("error: %v", err), |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
220 http.StatusInternalServerError) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
221 return |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
222 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
223 defer rows.Close() |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
224 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
225 var values []float64 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
226 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
227 lastDay, lastMonth := -1, -1 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
228 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
229 write := func() error { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
230 var err error |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
231 if len(values) > 0 { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
232 mean := stat.Mean(values, nil) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
233 _, err = fmt.Fprintf( |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
234 rw, "%02d-%02d,%s\n", lastDay, lastMonth, |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
235 float64format(mean)) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
236 values = values[:0] |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
237 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
238 return err |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
239 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
240 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
241 for rows.Next() { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
242 var when time.Time |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
243 var value float64 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
244 if err := rows.Scan(&when, &value); err != nil { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
245 log.Printf("error: %v", err) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
246 // Too late for an HTTP error code. |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
247 return |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
248 } |
2807
1fa57ad05e1e
Calculate the yearly waterlevel over an UTC time interval.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2806
diff
changeset
|
249 when = when.UTC() |
2803
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
250 day, month := when.Day(), int(when.Month()) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
251 if day != lastDay || month != lastMonth { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
252 if err := write(); err != nil { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
253 log.Printf("error: %v", err) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
254 // Too late for an HTTP error code. |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
255 return |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
256 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
257 lastDay, lastMonth = day, month |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
258 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
259 values = append(values, value) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
260 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
261 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
262 if err := rows.Err(); err != nil { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
263 log.Printf("error: %v", err) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
264 // Too late for an HTTP error code. |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
265 return |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
266 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
267 |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
268 if err := write(); err != nil { |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
269 log.Printf("error: %v", err) |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
270 // Too late for an HTTP error code. |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
271 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
272 } |
46a9a7c1281f
Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2800
diff
changeset
|
273 |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
274 func longtermWaterlevels(rw http.ResponseWriter, req *http.Request) { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
275 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
276 gauge := mux.Vars(req)["gauge"] |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
277 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
278 isrs, err := models.IsrsFromString(gauge) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
279 if err != nil { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
280 http.Error( |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
281 rw, fmt.Sprintf("error: Invalid ISRS code: %v", err), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
282 http.StatusBadRequest) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
283 return |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
284 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
285 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
286 conn := mw.GetDBConn(req) |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
287 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
288 ctx := req.Context() |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
289 |
2826
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
290 var begin, end pgtype.Timestamp |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
291 |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
292 err = conn.QueryRowContext( |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
293 ctx, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
294 selectAllWaterlevelsMeasuredRangeSQL, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
295 isrs.CountryCode, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
296 isrs.LoCode, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
297 isrs.FairwaySection, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
298 isrs.Orc, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
299 isrs.Hectometre, |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
300 ).Scan(&begin, &end) |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
301 |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
302 switch { |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
303 case err == sql.ErrNoRows || begin.Status != pgtype.Present || end.Status != pgtype.Present: |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
304 http.NotFound(rw, req) |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
305 return |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
306 case err != nil: |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
307 http.Error( |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
308 rw, fmt.Sprintf("error: %v", err), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
309 http.StatusInternalServerError) |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
310 return |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
311 } |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
312 |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
313 rows, err := conn.QueryContext( |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
314 ctx, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
315 selectAllWaterlevelsMeasuredSQL, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
316 isrs.CountryCode, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
317 isrs.LoCode, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
318 isrs.FairwaySection, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
319 isrs.Orc, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
320 isrs.Hectometre, |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
321 ) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
322 if err != nil { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
323 http.Error( |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
324 rw, fmt.Sprintf("error: %v", err), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
325 http.StatusInternalServerError) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
326 return |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
327 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
328 defer rows.Close() |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
329 |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
330 type result struct { |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
331 day int |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
332 month int |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
333 q25 float64 |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
334 median float64 |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
335 q75 float64 |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
336 mean float64 |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
337 min float64 |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
338 max float64 |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
339 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
340 |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
341 results := make([]result, 0, 366) |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
342 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
343 start := time.Now() |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
344 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
345 for rows.Next() { |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
346 var r result |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
347 var dayMonth string |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
348 if err := rows.Scan( |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
349 &dayMonth, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
350 &r.q25, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
351 &r.median, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
352 &r.q75, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
353 &r.mean, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
354 &r.min, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
355 &r.max, |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
356 ); err != nil { |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
357 http.Error( |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
358 rw, fmt.Sprintf("error: %v", err), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
359 http.StatusInternalServerError) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
360 } |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
361 parts := strings.SplitN(dayMonth, ":", 2) |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
362 r.day, _ = strconv.Atoi(parts[0]) |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
363 r.month, _ = strconv.Atoi(parts[1]) |
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
364 results = append(results, r) |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
365 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
366 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
367 if err := rows.Err(); err != nil { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
368 http.Error( |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
369 rw, fmt.Sprintf("error: %v", err), |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
370 http.StatusInternalServerError) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
371 return |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
372 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
373 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
374 log.Printf("info: loading entries took %s\n", time.Since(start)) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
375 |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
376 log.Printf("info: days found: %d\n", len(results)) |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
377 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
378 sort.Slice(results, func(i, j int) bool { |
2832
b5555005f51e
cosmetics: removed superfluous casts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2826
diff
changeset
|
379 if d := results[i].month - results[j].month; d != 0 { |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
380 return d < 0 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
381 } |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
382 return results[i].day < results[j].day |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
383 }) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
384 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
385 rw.Header().Add("Content-Type", "text/csv") |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
386 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
387 out := csv.NewWriter(rw) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
388 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
389 record := []string{ |
2826
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
390 fmt.Sprintf("#interval: %d-%d", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
391 begin.Time.UTC().Year(), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
392 end.Time.UTC().Year()), |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
393 "", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
394 "", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
395 "", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
396 "", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
397 "", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
398 "", |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
399 } |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
400 |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
401 if err := out.Write(record); err != nil { |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
402 log.Printf("error: %v\n", err) |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
403 // Too late for an HTTP error code. |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
404 return |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
405 } |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
406 |
f7ae108c2838
longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2817
diff
changeset
|
407 record = []string{ |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
408 "#date", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
409 "#min", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
410 "#max", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
411 "#mean", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
412 "#median", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
413 "#q25", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
414 "#q75", |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
415 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
416 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
417 if err := out.Write(record); err != nil { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
418 log.Printf("error: %v\n", err) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
419 // Too late for an HTTP error code. |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
420 return |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
421 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
422 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
423 for i := range results { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
424 r := &results[i] |
2809
216bc6394911
Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2808
diff
changeset
|
425 record[0] = fmt.Sprintf("%02d-%02d", r.day, r.month) |
2800
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
426 record[1] = float64format(r.min) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
427 record[2] = float64format(r.max) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
428 record[3] = float64format(r.mean) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
429 record[4] = float64format(r.median) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
430 record[5] = float64format(r.q25) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
431 record[6] = float64format(r.q75) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
432 if err := out.Write(record); err != nil { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
433 log.Printf("error: %v\n", err) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
434 // Too late for an HTTP error code. |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
435 return |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
436 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
437 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
438 |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
439 out.Flush() |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
440 if err := out.Error(); err != nil { |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
441 log.Printf("error: %v", err) |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
442 // Too late for an HTTP error code. |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
443 return |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
444 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
445 } |
db1052bc162a
Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2783
diff
changeset
|
446 |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
447 func parseISRS(code string) (*models.Isrs, error) { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
448 isrs, err := models.IsrsFromString(code) |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
449 if err != nil { |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
450 return nil, mw.JSONError{ |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
451 Code: http.StatusBadRequest, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
452 Message: fmt.Sprintf("error: Invalid ISRS code: %v", err), |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
453 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
454 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
455 return isrs, nil |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
456 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
457 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
458 type observedPredictedValues struct { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
459 when time.Time |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
460 observed float64 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
461 predicted common.TimedValues |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
462 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
463 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
464 func loadNashSutcliffeData( |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
465 ctx context.Context, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
466 conn *sql.Conn, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
467 gauge *models.Isrs, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
468 when time.Time, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
469 ) ([]observedPredictedValues, error) { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
470 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
471 var rows *sql.Rows |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
472 var err error |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
473 if rows, err = conn.QueryContext( |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
474 ctx, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
475 selectPredictedObserveredSQL, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
476 gauge.CountryCode, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
477 gauge.LoCode, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
478 gauge.FairwaySection, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
479 gauge.Orc, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
480 gauge.Hectometre, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
481 when, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
482 ); err != nil { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
483 return nil, err |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
484 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
485 defer rows.Close() |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
486 |
3101
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
487 acceptedDeltas := []time.Duration{ |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
488 -time.Hour * 24, |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
489 -time.Hour * 48, |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
490 -time.Hour * 72, |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
491 } |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
492 |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
493 isAccepted := func(observed, predicted time.Time) bool { |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
494 for _, delta := range acceptedDeltas { |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
495 t := observed.Add(delta) |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
496 d := predicted.Sub(t) |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
497 if -10*time.Millisecond < d && d < 10*time.Millisecond { |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
498 return true |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
499 } |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
500 } |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
501 return false |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
502 } |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
503 |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
504 var ( |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
505 hasCurrent bool |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
506 current observedPredictedValues |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
507 values []observedPredictedValues |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
508 ) |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
509 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
510 for rows.Next() { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
511 var ( |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
512 measureDate time.Time |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
513 issueDate time.Time |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
514 predicted bool |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
515 value float64 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
516 ) |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
517 if err := rows.Scan( |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
518 &measureDate, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
519 &issueDate, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
520 &predicted, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
521 &value, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
522 ); err != nil { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
523 return nil, err |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
524 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
525 measureDate = measureDate.UTC() |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
526 issueDate = issueDate.UTC() |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
527 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
528 if hasCurrent { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
529 if !current.when.Equal(measureDate) { |
3097
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
530 if !math.IsNaN(current.observed) && len(current.predicted) > 0 { |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
531 values = append(values, current) |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
532 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
533 current = observedPredictedValues{ |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
534 observed: math.NaN(), |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
535 when: measureDate, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
536 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
537 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
538 } else { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
539 hasCurrent = true |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
540 current = observedPredictedValues{ |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
541 observed: math.NaN(), |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
542 when: measureDate, |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
543 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
544 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
545 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
546 if predicted { |
3101
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
547 if isAccepted(measureDate, issueDate) { |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
548 current.predicted = append( |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
549 current.predicted, |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
550 common.TimedValue{When: issueDate, Value: value}, |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
551 ) |
41ed69dbf9bb
Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3099
diff
changeset
|
552 } |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
553 } else { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
554 current.observed = value |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
555 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
556 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
557 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
558 if err := rows.Err(); err != nil { |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
559 return nil, err |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
560 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
561 |
3097
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
562 if hasCurrent && !math.IsNaN(current.observed) && len(current.predicted) > 0 { |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
563 values = append(values, current) |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
564 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
565 |
3097
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
566 // for i := range values { |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
567 // log.Printf("%v %f %d\n", values[i].when, values[i].observed, len(values[i].predicted)) |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
568 // if len(values[i].predicted) > 0 { |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
569 // for j := range values[i].predicted { |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
570 // log.Printf("\t%v %f\n", values[i].predicted[j].When, values[i].predicted[j].Value) |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
571 // } |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
572 // } |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
573 // } |
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3092
diff
changeset
|
574 |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
575 return values, nil |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
576 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
577 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
578 func nashSutcliffe(req *http.Request) (jr mw.JSONResult, err error) { |
4243
d776110b4db0
Made the de-serialized input of the JSON handler accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4242
diff
changeset
|
579 |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
580 gauge := mux.Vars(req)["gauge"] |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
581 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
582 var isrs *models.Isrs |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
583 if isrs, err = parseISRS(gauge); err != nil { |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
584 return |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
585 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
586 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
587 var when time.Time |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
588 if w := req.FormValue("when"); w != "" { |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
589 if when, err = common.ParseTime(w); err != nil { |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
590 err = mw.JSONError{ |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
591 Code: http.StatusBadRequest, |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
592 Message: fmt.Sprintf("error: wrong time format: %v", err), |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
593 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
594 return |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
595 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
596 } else { |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
597 when = time.Now() |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
598 } |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
599 when = when.UTC() |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
600 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
601 ctx := req.Context() |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
602 |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
603 var values []observedPredictedValues |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
604 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
605 if values, err = loadNashSutcliffeData(ctx, mw.JSONConn(req), isrs, when); err != nil { |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
606 return |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
607 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
608 |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
609 log.Printf("info: found %d value(s) for Nash Sutcliffe.\n", len(values)) |
2878
4f66a3ba424b
Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2861
diff
changeset
|
610 |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
611 type coeff struct { |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
612 Value float64 `json:"value"` |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
613 Samples int `json:"samples"` |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
614 Hours int `json:"hours"` |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
615 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
616 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
617 type coeffs struct { |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
618 When models.ImportTime `json:"when"` |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
619 Coeffs []coeff `json:"coeffs"` |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
620 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
621 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
622 var predicted, observed []float64 |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
623 |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
624 cs := make([]coeff, 3) |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
625 for i := range cs { |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
626 cs[i].Hours = (i + 1) * 24 |
3092
7dc9660df743
Fixed c&p sign error in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3091
diff
changeset
|
627 delta := -time.Duration(cs[i].Hours) * time.Hour |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
628 |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
629 for j := range values { |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
630 when := values[j].when.Add(delta) |
3099
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3097
diff
changeset
|
631 if p, ok := values[j].predicted.Find(when); ok { |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
632 predicted = append(predicted, p) |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
633 observed = append(observed, values[j].observed) |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
634 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
635 } |
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
636 |
4087
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
637 cs[i].Value = sanitizeFloat64(common.NashSutcliffe(predicted, observed)) |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
638 cs[i].Samples = len(predicted) |
3088
09d1ffce3d00
Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2913
diff
changeset
|
639 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
640 predicted = predicted[:0] |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3090
diff
changeset
|
641 observed = observed[:0] |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
642 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
643 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
644 jr = mw.JSONResult{ |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
645 Result: &coeffs{ |
3089
813309225e35
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
646 When: models.ImportTime{Time: when}, |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
647 Coeffs: cs, |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
648 }, |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
649 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
650 return |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
651 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2694
diff
changeset
|
652 |
4087
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
653 func sanitizeFloat64(x float64) float64 { |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
654 switch { |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
655 case math.IsNaN(x): |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
656 return 0 |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
657 case math.IsInf(x, +1): |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
658 return math.MaxFloat64 |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
659 case math.IsInf(x, -1): |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
660 return -math.MaxFloat64 |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
661 } |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
662 return x |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
663 } |
1562a5fb36dc
Handle Nash-Sutcliff more tolerant for NaN/Inf values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
664 |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
665 func waterlevels(rw http.ResponseWriter, req *http.Request) { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
666 gauge := mux.Vars(req)["gauge"] |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
667 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
668 isrs, err := models.IsrsFromString(gauge) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
669 if err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
670 http.Error( |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
671 rw, fmt.Sprintf("error: Invalid ISRS code: %v", err), |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
672 http.StatusBadRequest) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
673 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
674 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
675 |
3725
18777f6df3ef
Partially fix waterlevel selection for gauge_measurements(_predictions)
Raimund Renkert <raimund.renkert@intevation.de>
parents:
3302
diff
changeset
|
676 // TODO: FIXME The filter is not correct for predictions!? |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
677 filters := filterAnd{ |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
678 buildFilterTerm( |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
679 "location = ($%d::char(2), $%d::char(3), $%d::char(5), $%d::char(5), $%d::int)", |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
680 isrs.CountryCode, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
681 isrs.LoCode, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
682 isrs.FairwaySection, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
683 isrs.Orc, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
684 isrs.Hectometre, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
685 ), |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
686 &filterOr{ |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
687 &filterNot{&filterTerm{format: "predicted"}}, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
688 buildFilterTerm( |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
689 `date_issue = ( |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
690 SELECT max(date_issue) |
3725
18777f6df3ef
Partially fix waterlevel selection for gauge_measurements(_predictions)
Raimund Renkert <raimund.renkert@intevation.de>
parents:
3302
diff
changeset
|
691 FROM waterway.gauge_predictions gm |
3302
ec6163c6687d
'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents:
3277
diff
changeset
|
692 WHERE location = ($%d::char(2), $%d::char(3), $%d::char(5), $%d::char(5), $%d::int))`, |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
693 isrs.CountryCode, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
694 isrs.LoCode, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
695 isrs.FairwaySection, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
696 isrs.Orc, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
697 isrs.Hectometre, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
698 ), |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
699 }, |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
700 } |
2858
401bca8eaafb
Filter predicted values for waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2832
diff
changeset
|
701 |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
702 if from := req.FormValue("from"); from != "" { |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
703 fromTime, err := common.ParseTime(from) |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
704 if err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
705 http.Error( |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
706 rw, fmt.Sprintf("error: Invalid from time: %v", err), |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
707 http.StatusBadRequest) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
708 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
709 } |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
710 filters = append(filters, buildFilterTerm("measure_date >= $%d", fromTime)) |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
711 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
712 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
713 if to := req.FormValue("to"); to != "" { |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
714 toTime, err := common.ParseTime(to) |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
715 if err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
716 http.Error( |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
717 rw, fmt.Sprintf("error: Invalid from time: %v", err), |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
718 http.StatusBadRequest) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
719 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
720 } |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
721 filters = append(filters, buildFilterTerm("measure_date <= $%d", toTime)) |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
722 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
723 |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
724 var stmt strings.Builder |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
725 var args []interface{} |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
726 |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
727 stmt.WriteString(selectWaterlevelsSQL) |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
728 filters.serialize(&stmt, &args) |
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
729 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
730 conn := mw.GetDBConn(req) |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
731 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
732 ctx := req.Context() |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
733 |
3194
eeff2cc4ff9d
controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
734 rows, err := conn.QueryContext(ctx, stmt.String(), args...) |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
735 if err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
736 http.Error( |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
737 rw, fmt.Sprintf("error: %v", err), |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
738 http.StatusInternalServerError) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
739 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
740 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
741 defer rows.Close() |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
742 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
743 rw.Header().Add("Content-Type", "text/csv") |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
744 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
745 out := csv.NewWriter(rw) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
746 |
2783
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
747 record := []string{ |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
748 "#date", |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
749 "#water_level", |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
750 "#value_min", |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
751 "#value_max", |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
752 "#predicted", |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
753 } |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
754 |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
755 if err := out.Write(record); err != nil { |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
756 log.Printf("error: %v", err) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
757 // Too late for an HTTP error code. |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
758 return |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
759 } |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
760 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
761 for rows.Next() { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
762 var ( |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
763 measureDate time.Time |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
764 waterlevel float64 |
2783
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
765 valueMin sql.NullFloat64 |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
766 valueMax sql.NullFloat64 |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
767 predicted bool |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
768 ) |
2783
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
769 if err := rows.Scan( |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
770 &measureDate, |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
771 &waterlevel, |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
772 &valueMin, |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
773 &valueMax, |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
774 &predicted, |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
775 ); err != nil { |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
776 log.Printf("error: %v\n", err) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
777 // Too late for an HTTP error code. |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
778 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
779 } |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3725
diff
changeset
|
780 record[0] = measureDate.Format(common.TimeFormat) |
2783
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
781 record[1] = float64format(waterlevel) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
782 record[2] = nullFloat64format(valueMin) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
783 record[3] = nullFloat64format(valueMax) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
784 record[4] = boolFormat(predicted) |
2806821cfd63
Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2766
diff
changeset
|
785 |
2694
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
786 if err := out.Write(record); err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
787 log.Printf("error: %v", err) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
788 // Too late for an HTTP error code. |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
789 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
790 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
791 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
792 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
793 if err := rows.Err(); err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
794 log.Printf("error: %v", err) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
795 // Too late for an HTTP error code. |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
796 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
797 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
798 |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
799 out.Flush() |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
800 if err := out.Error(); err != nil { |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
801 log.Printf("error: %v", err) |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
802 // Too late for an HTTP error code. |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
803 return |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
804 } |
0d7a4fdb9e12
Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
805 } |