annotate pkg/controllers/gauges.go @ 3302:ec6163c6687d

'Historicise' gauges on import Gauge data sets will be updated or a new version will be inserted depending on temporal validity and a timestamp marking the last update in the RIS-Index of a data set. The trigger on date_info is removed because the value is actually an attribut coming from the RIS-Index. Gauge measurements and predictions are associated to the version with matching temporal validity. Bottlenecks are always associated to the actual version of the gauge, although this might change as soon as bottlenecks are 'historicised', too.
author Tom Gottfried <tom@intevation.de>
date Thu, 16 May 2019 18:41:43 +0200
parents 232fc90e6ee2
children 18777f6df3ef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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/middleware"
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
35 "gemma.intevation.de/gemma/pkg/models"
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
36 )
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 const (
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
39 selectPredictedObserveredSQL = `
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
40 SELECT
2878
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
41 measure_date,
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
42 date_issue,
2878
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
43 predicted,
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
44 water_level
3277
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
45 FROM (
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
46 SELECT
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
47 location,
3277
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
48 measure_date,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
49 date_issue,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
50 false AS predicted,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
51 water_level
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
52 FROM waterway.gauge_measurements
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
53 UNION ALL
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
54 SELECT
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
55 location,
3277
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
56 measure_date,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
57 date_issue,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
58 true AS predicted,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
59 water_level
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
60 FROM waterway.gauge_predictions
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
61 ) AS gmp
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
62 WHERE
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
63 location = (
2763
a06d11d1f0b3 Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2762
diff changeset
64 $1::char(2),
a06d11d1f0b3 Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2762
diff changeset
65 $2::char(3),
a06d11d1f0b3 Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2762
diff changeset
66 $3::char(5),
a06d11d1f0b3 Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2762
diff changeset
67 $4::char(5),
a06d11d1f0b3 Fixed gauge selection in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2762
diff changeset
68 $5::int
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
69 ) AND
2878
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
70 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
71 $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
72 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
73 `
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
74 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
75 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
76 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
77 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
78 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
79 value_max,
2766
1f173d1a731d Fixed broken SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2763
diff changeset
80 predicted
3277
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
81 FROM (
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
82 SELECT
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
83 location,
3277
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
84 measure_date,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
85 date_issue,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
86 water_level,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
87 NULL AS value_min,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
88 NULL AS value_max,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
89 false AS predicted
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
90 FROM waterway.gauge_measurements
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
91 UNION ALL
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
92 SELECT
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
93 location,
3277
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
94 measure_date,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
95 date_issue,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
96 water_level,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
97 lower(conf_interval) AS value_min,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
98 upper(conf_interval) AS value_max,
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
99 true AS predicted
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
100 FROM waterway.gauge_predictions
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
101 ) 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
102 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
103 `
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
104
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
105 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
106 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
107 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
108 max(measure_date)
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
109 FROM waterway.gauge_measurements
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
110 WHERE
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
111 location = (
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
112 $1::char(2),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
113 $2::char(3),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
114 $3::char(5),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
115 $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
116 $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
117 )::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
118 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
119 `
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 selectAllWaterlevelsMeasuredSQL = `
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
122 SELECT
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
123 extract(day from measure_date)::varchar || ':' ||
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
124 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
125 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
126 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
127 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
128 avg(water_level) AS mean,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
129 min(water_level) AS min,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
130 max(water_level) AS max
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
131 FROM waterway.gauge_measurements
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
132 WHERE
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
133 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
134 $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
135 $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
136 $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
137 $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
138 $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
139 )::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
140 AND staging_done
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
141 GROUP BY extract(day from measure_date)::varchar || ':' ||
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
142 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
143 `
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
144 selectYearWaterlevelsMeasuredSQL = `
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
145 SELECT
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
146 measure_date,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
147 water_level
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
148 FROM waterway.gauge_measurements
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
149 WHERE
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
150 location = (
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
151 $1::char(2),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
152 $2::char(3),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
153 $3::char(5),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
154 $4::char(5),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
155 $5::int
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
156 )::isrs
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
157 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
158 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
159 ORDER BY measure_date
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
160 `
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
161 )
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
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
163 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
164 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
165 }
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 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
168 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
169 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
170 }
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 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
172 }
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 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
175 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
176 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
177 }
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 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
179 }
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
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
181 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
182
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
183 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
184
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
185 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
186 if err != nil {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
187 http.Error(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
188 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
189 http.StatusBadRequest)
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
190 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
191 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
192
2806
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
193 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
194
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
195 conn := middleware.GetDBConn(req)
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
196
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
197 ctx := req.Context()
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
198
2806
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
199 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
200 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
201
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
202 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
203 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
204
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
205 rows, err := conn.QueryContext(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
206 ctx,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
207 selectYearWaterlevelsMeasuredSQL,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
208 isrs.CountryCode,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
209 isrs.LoCode,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
210 isrs.FairwaySection,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
211 isrs.Orc,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
212 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
213 begin,
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
214 end,
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
215 )
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
216 if err != nil {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
217 http.Error(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
218 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
219 http.StatusInternalServerError)
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
220 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
221 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
222 defer rows.Close()
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
223
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
224 var values []float64
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
225
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
226 lastDay, lastMonth := -1, -1
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
227
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
228 write := func() error {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
229 var err error
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
230 if len(values) > 0 {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
231 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
232 _, err = fmt.Fprintf(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
233 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
234 float64format(mean))
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
235 values = values[:0]
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
236 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
237 return err
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
238 }
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 for rows.Next() {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
241 var when time.Time
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
242 var value float64
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
243 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
244 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
245 // 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
246 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
247 }
2807
1fa57ad05e1e Calculate the yearly waterlevel over an UTC time interval.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2806
diff changeset
248 when = when.UTC()
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
249 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
250 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
251 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
252 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
253 // 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
254 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
255 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
256 lastDay, lastMonth = day, month
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
257 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
258 values = append(values, value)
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
259 }
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 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
262 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
263 // 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
264 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
265 }
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 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
268 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
269 // 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
270 }
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
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
273 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
274
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
275 gauge := mux.Vars(req)["gauge"]
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
276
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
277 isrs, err := models.IsrsFromString(gauge)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
278 if err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
279 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
280 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
281 http.StatusBadRequest)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
282 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
283 }
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 conn := middleware.GetDBConn(req)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
286
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
287 ctx := req.Context()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
288
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
289 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
290
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 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
292 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
293 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
294 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
295 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
296 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
297 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
298 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
299 ).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
300
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 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
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
310 }
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
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
312 rows, err := conn.QueryContext(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
313 ctx,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
314 selectAllWaterlevelsMeasuredSQL,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
315 isrs.CountryCode,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
316 isrs.LoCode,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
317 isrs.FairwaySection,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
318 isrs.Orc,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
319 isrs.Hectometre,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
320 )
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
321 if err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
322 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
323 rw, fmt.Sprintf("error: %v", err),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
324 http.StatusInternalServerError)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
325 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
326 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
327 defer rows.Close()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
328
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
329 type result struct {
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
330 day int
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
331 month int
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
332 q25 float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
333 median float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
334 q75 float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
335 mean float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
336 min float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
337 max float64
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
338 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
339
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
340 results := make([]result, 0, 366)
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
341
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
342 start := time.Now()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
343
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
344 for rows.Next() {
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
345 var r result
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
346 var dayMonth string
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
347 if err := rows.Scan(
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
348 &dayMonth,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
349 &r.q25,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
350 &r.median,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
351 &r.q75,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
352 &r.mean,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
353 &r.min,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
354 &r.max,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
355 ); err != nil {
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
356 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
357 rw, fmt.Sprintf("error: %v", err),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
358 http.StatusInternalServerError)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
359 }
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
360 parts := strings.SplitN(dayMonth, ":", 2)
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
361 r.day, _ = strconv.Atoi(parts[0])
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
362 r.month, _ = strconv.Atoi(parts[1])
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
363 results = append(results, r)
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
364 }
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 if err := rows.Err(); err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
367 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
368 rw, fmt.Sprintf("error: %v", err),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
369 http.StatusInternalServerError)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
370 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
371 }
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 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
374
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
375 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
376
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
377 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
378 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
379 return d < 0
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
380 }
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
381 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
382 })
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 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
385
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
386 out := csv.NewWriter(rw)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
387
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
388 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
389 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
390 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
391 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
392 "",
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 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
401 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
402 // 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
403 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
404 }
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 record = []string{
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
407 "#date",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
408 "#min",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
409 "#max",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
410 "#mean",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
411 "#median",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
412 "#q25",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
413 "#q75",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
414 }
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 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
417 log.Printf("error: %v\n", err)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
418 // 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
419 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
420 }
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 for i := range results {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
423 r := &results[i]
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
424 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
425 record[1] = float64format(r.min)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
426 record[2] = float64format(r.max)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
427 record[3] = float64format(r.mean)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
428 record[4] = float64format(r.median)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
429 record[5] = float64format(r.q25)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
430 record[6] = float64format(r.q75)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
431 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
432 log.Printf("error: %v\n", err)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
433 // 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
434 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
435 }
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 out.Flush()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
439 if err := out.Error(); err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
440 log.Printf("error: %v", err)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
441 // 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
442 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
443 }
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
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
446 func parseISRS(code string) (*models.Isrs, error) {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
447 isrs, err := models.IsrsFromString(code)
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
448 if err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
449 return nil, JSONError{
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
450 Code: http.StatusBadRequest,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
451 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
452 }
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 return isrs, nil
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
455 }
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 type observedPredictedValues struct {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
458 when time.Time
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
459 observed float64
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
460 predicted common.TimedValues
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
461 }
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 func loadNashSutcliffeData(
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
464 ctx context.Context,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
465 conn *sql.Conn,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
466 gauge *models.Isrs,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
467 when time.Time,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
468 ) ([]observedPredictedValues, error) {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
469
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
470 var rows *sql.Rows
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
471 var err error
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
472 if rows, err = conn.QueryContext(
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
473 ctx,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
474 selectPredictedObserveredSQL,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
475 gauge.CountryCode,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
476 gauge.LoCode,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
477 gauge.FairwaySection,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
478 gauge.Orc,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
479 gauge.Hectometre,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
480 when,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
481 ); err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
482 return nil, err
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
483 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
484 defer rows.Close()
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
485
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
486 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
487 -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
488 -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
489 -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
490 }
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 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
493 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
494 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
495 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
496 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
497 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
498 }
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 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
501 }
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
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
503 var (
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
504 hasCurrent bool
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
505 current observedPredictedValues
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
506 values []observedPredictedValues
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
507 )
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 for rows.Next() {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
510 var (
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
511 measureDate time.Time
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
512 issueDate time.Time
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
513 predicted bool
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
514 value float64
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
515 )
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
516 if err := rows.Scan(
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
517 &measureDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
518 &issueDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
519 &predicted,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
520 &value,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
521 ); err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
522 return nil, err
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
523 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
524 measureDate = measureDate.UTC()
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
525 issueDate = issueDate.UTC()
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
526
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
527 if hasCurrent {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
528 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
529 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
530 values = append(values, current)
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
531 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
532 current = observedPredictedValues{
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
533 observed: math.NaN(),
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
534 when: measureDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
535 }
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 } else {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
538 hasCurrent = true
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
539 current = observedPredictedValues{
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
540 observed: math.NaN(),
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
541 when: measureDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
542 }
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 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
546 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
547 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
548 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
549 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
550 )
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 }
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
552 } else {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
553 current.observed = value
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
554 }
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 if err := rows.Err(); err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
558 return nil, err
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
559 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
560
3097
e6ba32b060df Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3092
diff changeset
561 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
562 values = append(values, current)
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
563 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
564
3097
e6ba32b060df Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3092
diff changeset
565 // 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
566 // 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
567 // 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
568 // 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
569 // 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
570 // }
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
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
574 return values, nil
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
575 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
576
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
577 func nashSutcliffe(
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
578 _ interface{},
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
579 req *http.Request,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
580 conn *sql.Conn,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
581 ) (jr JSONResult, err error) {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
582 gauge := mux.Vars(req)["gauge"]
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
583
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
584 var isrs *models.Isrs
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
585 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
586 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
587 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
588
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
589 var when time.Time
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
590 if w := req.FormValue("when"); w != "" {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
591 if when, err = time.Parse(models.ImportTimeFormat, w); err != nil {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
592 err = JSONError{
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
593 Code: http.StatusBadRequest,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
594 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
595 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
596 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
597 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
598 } else {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
599 when = time.Now()
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
600 }
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
601 when = when.UTC()
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
602
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
603 ctx := req.Context()
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
604
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
605 var values []observedPredictedValues
2878
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
606
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
607 if values, err = loadNashSutcliffeData(ctx, conn, isrs, when); err != nil {
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
608 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
609 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
610
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
611 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
612
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
613 type coeff struct {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
614 Value float64 `json:"value"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
615 Samples int `json:"samples"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
616 Hours int `json:"hours"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
617 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
618
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
619 type coeffs struct {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
620 When models.ImportTime `json:"when"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
621 Coeffs []coeff `json:"coeffs"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
622 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
623
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
624 var predicted, observed []float64
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
625
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
626 cs := make([]coeff, 3)
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
627 for i := range cs {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
628 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
629 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
630
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
631 for j := range values {
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
632 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
633 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
634 predicted = append(predicted, p)
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
635 observed = append(observed, values[j].observed)
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
636 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
637 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
638
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
639 cs[i].Value = common.NashSutcliffe(predicted, observed)
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
640 cs[i].Samples = len(predicted)
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
641
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
642 predicted = predicted[:0]
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
643 observed = observed[:0]
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
644 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
645
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
646 jr = JSONResult{
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
647 Result: &coeffs{
3089
813309225e35 Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3088
diff changeset
648 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
649 Coeffs: cs,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
650 },
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 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
653 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
654
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
655 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
656 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
657
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
658 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
659 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
660 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
661 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
662 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
663 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
664 }
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
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
666 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
667 buildFilterTerm(
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
668 "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
669 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
670 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
671 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
672 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
673 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
674 ),
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
675 &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
676 &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
677 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
678 `date_issue = (
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
679 SELECT max(date_issue)
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
680 FROM waterway.gauge_measurements gm
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3277
diff changeset
681 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
682 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
683 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
684 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
685 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
686 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
687 ),
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 },
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 }
2858
401bca8eaafb Filter predicted values for waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2832
diff changeset
690
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
691 if from := req.FormValue("from"); from != "" {
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
692 fromTime, err := time.Parse(models.ImportTimeFormat, from)
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
693 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
694 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
695 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
696 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
697 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
698 }
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
699 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
700 }
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
701
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 to := req.FormValue("to"); to != "" {
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
703 toTime, err := time.Parse(models.ImportTimeFormat, to)
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", 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
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
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
713 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
714 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
715
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
716 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
717 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
718
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
719 conn := middleware.GetDBConn(req)
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
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
721 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
722
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
723 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
724 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
725 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
726 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
727 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
728 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
729 }
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
730 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
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 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
733
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
734 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
735
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
736 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
737 "#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
738 "#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
739 "#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
740 "#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
741 "#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
742 }
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
743
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
744 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
745 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
746 // 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
747 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
748 }
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
749
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
750 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
751 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
752 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
753 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
754 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
755 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
756 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
757 )
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
758 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
759 &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
760 &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
761 &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
762 &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
763 &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
764 ); 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
765 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
766 // 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
767 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
768 }
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
769 record[0] = measureDate.Format(models.ImportTimeFormat)
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
770 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
771 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
772 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
773 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
774
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
775 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
776 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
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 }
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
780 }
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
781
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
782 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
783 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
784 // 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
785 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
786 }
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
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 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
789 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
790 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
791 // 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
792 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
793 }
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 }