annotate pkg/controllers/gauges.go @ 3277:232fc90e6ee2

Disentangle gauge measurements and predictions Representing both in one table has led to the necessity to make the distinction at many places such as statements, definitions of partial indexes and application code. At least in one place in the AGM import the distinction in application code was too late and measurements matching an approved measurement could have been missed.
author Tom Gottfried <tom@intevation.de>
date Wed, 15 May 2019 19:08:49 +0200
parents 9e087a495f41
children ec6163c6687d
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
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
47 fk_gauge_id,
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
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
55 fk_gauge_id,
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
2913
cf6f595af2cb Fixed SQL statement for nash sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2884
diff changeset
63 fk_gauge_id = (
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
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
83 fk_gauge_id,
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
232fc90e6ee2 Disentangle gauge measurements and predictions
Tom Gottfried <tom@intevation.de>
parents: 3223
diff changeset
93 fk_gauge_id,
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)
f7ae108c2838 longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2817
diff changeset
109 FROM waterway.gauge_measurements WHERE
f7ae108c2838 longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2817
diff changeset
110 fk_gauge_id = (
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
111 $1::char(2),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
112 $2::char(3),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
113 $3::char(5),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
114 $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
115 $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
116 )::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
117 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
118 `
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 selectAllWaterlevelsMeasuredSQL = `
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
121 SELECT
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
122 extract(day from measure_date)::varchar || ':' ||
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
123 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
124 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
125 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
126 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
127 avg(water_level) AS mean,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
128 min(water_level) AS min,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
129 max(water_level) AS max
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
130 FROM waterway.gauge_measurements WHERE
f7ae108c2838 longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2817
diff changeset
131 fk_gauge_id = (
f7ae108c2838 longterm waterlevels statistics: Generate a comment line with the interval of years.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2817
diff changeset
132 $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
133 $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
134 $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
135 $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
136 $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
137 )::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
138 AND staging_done
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
139 GROUP BY extract(day from measure_date)::varchar || ':' ||
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
140 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
141 `
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
142 selectYearWaterlevelsMeasuredSQL = `
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
143 SELECT
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
144 measure_date,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
145 water_level
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
146 FROM waterway.gauge_measurements
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
147 WHERE
2817
ff58440ce809 Added staging_done to the longterm waterlevels selections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2814
diff changeset
148 staging_done AND
ff58440ce809 Added staging_done to the longterm waterlevels selections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2814
diff changeset
149 fk_gauge_id = (
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
150 $1::char(2),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
151 $2::char(3),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
152 $3::char(5),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
153 $4::char(5),
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
154 $5::int
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
155 )
2806
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
156 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
157 ORDER BY measure_date
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
158 `
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
159 )
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
160
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
161 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
162 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
163 }
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
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 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
166 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
167 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
168 }
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 ""
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
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 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
173 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
174 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
175 }
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 "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
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
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
179 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
180
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
181 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
182
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
183 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
184 if err != nil {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
185 http.Error(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
186 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
187 http.StatusBadRequest)
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
188 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
189 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
190
2806
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
191 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
192
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
193 conn := middleware.GetDBConn(req)
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 ctx := req.Context()
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
196
2806
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
197 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
198 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
199
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
200 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
201 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
202
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
203 rows, err := conn.QueryContext(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
204 ctx,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
205 selectYearWaterlevelsMeasuredSQL,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
206 isrs.CountryCode,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
207 isrs.LoCode,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
208 isrs.FairwaySection,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
209 isrs.Orc,
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
210 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
211 begin,
c6374c520228 Use between filter to speed up SQL for fetching yearly waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2803
diff changeset
212 end,
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
213 )
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
214 if err != nil {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
215 http.Error(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
216 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
217 http.StatusInternalServerError)
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
218 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
219 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
220 defer rows.Close()
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 var values []float64
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 lastDay, lastMonth := -1, -1
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 write := func() error {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
227 var err error
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
228 if len(values) > 0 {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
229 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
230 _, err = fmt.Fprintf(
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
231 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
232 float64format(mean))
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
233 values = values[:0]
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
234 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
235 return err
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
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
238 for rows.Next() {
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
239 var when time.Time
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
240 var value float64
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
241 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
242 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
243 // 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
244 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
245 }
2807
1fa57ad05e1e Calculate the yearly waterlevel over an UTC time interval.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2806
diff changeset
246 when = when.UTC()
2803
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
247 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
248 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
249 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
250 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
251 // 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
252 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
253 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
254 lastDay, lastMonth = day, month
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 values = append(values, value)
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
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
259 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
260 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
261 // 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
262 return
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
263 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
264
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
265 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
266 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
267 // 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
268 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
269 }
46a9a7c1281f Added GET /api/data/year-waterlevels/{gauge}/{year}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2800
diff changeset
270
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
271 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
272
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
273 gauge := mux.Vars(req)["gauge"]
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 isrs, err := models.IsrsFromString(gauge)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
276 if err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
277 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
278 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
279 http.StatusBadRequest)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
280 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
281 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
282
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
283 conn := middleware.GetDBConn(req)
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 ctx := req.Context()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
286
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
287 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
288
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 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
290 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
291 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
292 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
293 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
294 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
295 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
296 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
297 ).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
298
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 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
300 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
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308 }
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
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
310 rows, err := conn.QueryContext(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
311 ctx,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
312 selectAllWaterlevelsMeasuredSQL,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
313 isrs.CountryCode,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
314 isrs.LoCode,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
315 isrs.FairwaySection,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
316 isrs.Orc,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
317 isrs.Hectometre,
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
318 )
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
319 if err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
320 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
321 rw, fmt.Sprintf("error: %v", err),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
322 http.StatusInternalServerError)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
323 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
324 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
325 defer rows.Close()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
326
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
327 type result struct {
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
328 day int
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
329 month int
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
330 q25 float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
331 median float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
332 q75 float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
333 mean float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
334 min float64
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
335 max float64
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
336 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
337
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
338 results := make([]result, 0, 366)
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
339
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
340 start := time.Now()
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 for rows.Next() {
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
343 var r result
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
344 var dayMonth string
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
345 if err := rows.Scan(
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
346 &dayMonth,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
347 &r.q25,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
348 &r.median,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
349 &r.q75,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
350 &r.mean,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
351 &r.min,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
352 &r.max,
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
353 ); err != nil {
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
354 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
355 rw, fmt.Sprintf("error: %v", err),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
356 http.StatusInternalServerError)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
357 }
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
358 parts := strings.SplitN(dayMonth, ":", 2)
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
359 r.day, _ = strconv.Atoi(parts[0])
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
360 r.month, _ = strconv.Atoi(parts[1])
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
361 results = append(results, r)
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
362 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
363
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
364 if err := rows.Err(); err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
365 http.Error(
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
366 rw, fmt.Sprintf("error: %v", err),
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
367 http.StatusInternalServerError)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
368 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
369 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
370
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
371 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
372
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
373 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
374
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
375 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
376 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
377 return d < 0
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
378 }
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
379 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
380 })
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
381
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
382 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
383
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
384 out := csv.NewWriter(rw)
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 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
387 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
388 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
389 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
390 "",
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 "",
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 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
399 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
400 // 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
401 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
402 }
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
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 record = []string{
2800
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
405 "#date",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
406 "#min",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
407 "#max",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
408 "#mean",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
409 "#median",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
410 "#q25",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
411 "#q75",
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
412 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
413
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
414 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
415 log.Printf("error: %v\n", err)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
416 // 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
417 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
418 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
419
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
420 for i := range results {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
421 r := &results[i]
2809
216bc6394911 Optimized longterm waterlevel statistics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2808
diff changeset
422 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
423 record[1] = float64format(r.min)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
424 record[2] = float64format(r.max)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
425 record[3] = float64format(r.mean)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
426 record[4] = float64format(r.median)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
427 record[5] = float64format(r.q25)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
428 record[6] = float64format(r.q75)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
429 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
430 log.Printf("error: %v\n", err)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
431 // 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
432 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
433 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
434 }
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 out.Flush()
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
437 if err := out.Error(); err != nil {
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
438 log.Printf("error: %v", err)
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
439 // 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
440 return
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
441 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
442 }
db1052bc162a Added GET /data/longterm-waterlevels/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2783
diff changeset
443
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
444 func parseISRS(code string) (*models.Isrs, error) {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
445 isrs, err := models.IsrsFromString(code)
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
446 if err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
447 return nil, JSONError{
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
448 Code: http.StatusBadRequest,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
449 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
450 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
451 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
452 return isrs, nil
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
453 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
454
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
455 type observedPredictedValues struct {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
456 when time.Time
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
457 observed float64
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
458 predicted common.TimedValues
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
459 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
460
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
461 func loadNashSutcliffeData(
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
462 ctx context.Context,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
463 conn *sql.Conn,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
464 gauge *models.Isrs,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
465 when time.Time,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
466 ) ([]observedPredictedValues, error) {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
467
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
468 var rows *sql.Rows
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
469 var err error
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
470 if rows, err = conn.QueryContext(
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
471 ctx,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
472 selectPredictedObserveredSQL,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
473 gauge.CountryCode,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
474 gauge.LoCode,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
475 gauge.FairwaySection,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
476 gauge.Orc,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
477 gauge.Hectometre,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
478 when,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
479 ); err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
480 return nil, err
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
481 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
482 defer rows.Close()
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
483
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
484 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
485 -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
486 -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
487 -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
488 }
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
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 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
491 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
492 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
493 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
494 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
495 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
496 }
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 }
41ed69dbf9bb Nash Sutcliffe: Optimization: When append predicted values only accept predictions where the issue date is 24/48/72h in the past of the measure date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3099
diff changeset
498 return 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
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
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
501 var (
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
502 hasCurrent bool
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
503 current observedPredictedValues
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
504 values []observedPredictedValues
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
505 )
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
506
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
507 for rows.Next() {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
508 var (
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
509 measureDate time.Time
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
510 issueDate time.Time
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
511 predicted bool
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
512 value float64
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
513 )
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
514 if err := rows.Scan(
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
515 &measureDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
516 &issueDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
517 &predicted,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
518 &value,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
519 ); err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
520 return nil, err
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
521 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
522 measureDate = measureDate.UTC()
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
523 issueDate = issueDate.UTC()
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
524
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
525 if hasCurrent {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
526 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
527 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
528 values = append(values, current)
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
529 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
530 current = observedPredictedValues{
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
531 observed: math.NaN(),
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
532 when: measureDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
533 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
534 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
535 } else {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
536 hasCurrent = true
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
537 current = observedPredictedValues{
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
538 observed: math.NaN(),
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
539 when: measureDate,
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
540 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
541 }
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 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
544 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
545 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
546 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
547 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
548 )
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 }
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
550 } else {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
551 current.observed = value
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
552 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
553 }
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 if err := rows.Err(); err != nil {
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
556 return nil, err
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
557 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
558
3097
e6ba32b060df Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3092
diff changeset
559 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
560 values = append(values, current)
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
561 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
562
3097
e6ba32b060df Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3092
diff changeset
563 // 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
564 // 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
565 // 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
566 // 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
567 // 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
568 // }
e6ba32b060df Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3092
diff changeset
569 // }
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
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
572 return values, nil
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
573 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
574
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
575 func nashSutcliffe(
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
576 _ interface{},
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
577 req *http.Request,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
578 conn *sql.Conn,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
579 ) (jr JSONResult, err error) {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
580 gauge := mux.Vars(req)["gauge"]
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
581
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
582 var isrs *models.Isrs
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
583 if isrs, err = parseISRS(gauge); err != nil {
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
584 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
585 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
586
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
587 var when time.Time
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
588 if w := req.FormValue("when"); w != "" {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
589 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
590 err = JSONError{
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
591 Code: http.StatusBadRequest,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
592 Message: fmt.Sprintf("error: wrong time format: %v", err),
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
593 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
594 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
595 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
596 } else {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
597 when = time.Now()
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
598 }
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
599 when = when.UTC()
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
600
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
601 ctx := req.Context()
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
602
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
603 var values []observedPredictedValues
2878
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
604
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
605 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
606 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
607 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
608
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
609 log.Printf("info: found %d value(s) for Nash Sutcliffe.\n", len(values))
2878
4f66a3ba424b Fixed selection for Nash-Sutcliffe coeff calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2861
diff changeset
610
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
611 type coeff struct {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
612 Value float64 `json:"value"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
613 Samples int `json:"samples"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
614 Hours int `json:"hours"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
615 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
616
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
617 type coeffs struct {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
618 When models.ImportTime `json:"when"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
619 Coeffs []coeff `json:"coeffs"`
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
620 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
621
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
622 var predicted, observed []float64
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
623
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
624 cs := make([]coeff, 3)
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
625 for i := range cs {
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
626 cs[i].Hours = (i + 1) * 24
3092
7dc9660df743 Fixed c&p sign error in Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3091
diff changeset
627 delta := -time.Duration(cs[i].Hours) * time.Hour
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
628
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
629 for j := range values {
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
630 when := values[j].when.Add(delta)
3099
f516ac26f4db "Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3097
diff changeset
631 if p, ok := values[j].predicted.Find(when); ok {
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
632 predicted = append(predicted, p)
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
633 observed = append(observed, values[j].observed)
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
634 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
635 }
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
636
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
637 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
638 cs[i].Samples = len(predicted)
3088
09d1ffce3d00 Reworked Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2913
diff changeset
639
3091
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
640 predicted = predicted[:0]
cec9d4af5f03 Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3090
diff changeset
641 observed = observed[:0]
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
642 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
643
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
644 jr = JSONResult{
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
645 Result: &coeffs{
3089
813309225e35 Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3088
diff changeset
646 When: models.ImportTime{Time: when},
2741
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
647 Coeffs: cs,
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
648 },
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
649 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
650 return
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
651 }
87aed4f9b1b8 Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2694
diff changeset
652
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
653 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
654 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
655
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 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
657 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
658 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
659 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
660 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
661 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
662 }
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
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
664 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
665 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
666 "fk_gauge_id = ($%d::char(2), $%d::char(3), $%d::char(5), $%d::char(5), $%d::int)",
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 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
668 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
669 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
670 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
671 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
672 ),
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 &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
674 &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
675 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
676 `date_issue = (
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 SELECT max(date_issue) FROM waterway.gauge_measurements
3223
9e087a495f41 Fixed SQL statements. Got broken when migrated to new filter builder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3194
diff changeset
678 WHERE fk_gauge_id = ($%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
679 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
680 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
681 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
682 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
683 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
684 ),
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
685 },
eeff2cc4ff9d controllers: re-factored the SQL filter to a tree like structure to be of more general use.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
686 }
2858
401bca8eaafb Filter predicted values for waterlevels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2832
diff changeset
687
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
688 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
689 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
690 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
691 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
692 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
693 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
694 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
695 }
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
696 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
697 }
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
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
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706 }
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
707 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
708 }
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 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
711 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
712
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 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
714 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
715
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
716 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
717
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
718 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
719
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
720 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
721 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
722 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
723 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
724 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
725 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
726 }
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 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
728
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 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
730
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 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
732
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
733 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
734 "#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
735 "#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
736 "#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
737 "#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
738 "#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
739 }
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
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 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
742 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
743 // 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
744 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
745 }
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
746
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
747 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
748 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
749 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
750 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
751 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
752 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
753 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
754 )
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
755 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
756 &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
757 &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
758 &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
759 &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
760 &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
761 ); 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
762 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
763 // 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
764 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
765 }
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 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
767 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
768 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
769 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
770 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
771
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
772 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
773 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
774 // 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
775 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
776 }
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 }
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
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 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
780 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
781 // 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
782 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
783 }
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
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 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
786 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
787 log.Printf("error: %v", err)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
788 // Too late for an HTTP error code.
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
789 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
790 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
791 }