Mercurial > gemma
annotate pkg/common/time.go @ 4687:f73868037365
morphology: do correct clipping of classbreak heights based on z range of model.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 16 Oct 2019 13:02:08 +0200 |
parents | 8c5df0f3562e |
children | d6710d29516b |
rev | line source |
---|---|
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
7 // Copyright (C) 2018, 2019 by via donau |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
13 // * Bernhard E. Reiter <bernhard.reiter@intevation.de> |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 package common |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
17 import ( |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
18 "math" |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
19 "time" |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
20 ) |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
21 |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 const ( |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
23 // time.RFC3339 equals "simplified ISO format as defined by ECMA-262" |
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
24 // https://tc39.github.io/ecma262/#sec-date-time-string-format |
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
25 // and "SHOULD be used in new protocols on the Internet." (RFC section 5.6) |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
26 |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
27 // TimeFormat is the preferred time format represention in gemma. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
28 TimeFormat = time.RFC3339 |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
29 // TimeFormatMicro is the same as TimeFormat but also |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
30 // contains 3 digits for micro seconds. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
31 // This is useful for e.g. times in imports and the |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
32 // respective logs. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
33 TimeFormatMicro = "2006-01-02T15:04:05.999Z07:00" |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
34 // TimeFormatMicroLocal is the same as TimeFormatMicro but |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
35 // w/o the timezone. Only used for tolerant parsing. |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
36 TimeFormatMicroLocal = "2006-01-02T15:04:05.000" |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
37 // DateFormat represents the preferred date format in Gemma. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
38 DateFormat = "2006-01-02" |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 ) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
40 |
4075
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
41 // TimeParser is a list of time formats. |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
42 type TimeParser []string |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
43 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
44 // ParseTime parses the time formats known to Gemma. |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
45 var ParseTime = TimeParser{ |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
46 TimeFormat, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
47 TimeFormatMicro, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
48 TimeFormatMicroLocal, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
49 DateFormat, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
50 }.Parse |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
51 |
3332
c86a8e70b40f
Made time interpolation more precise and added a unit test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3095
diff
changeset
|
52 var utc0 = time.Unix(0, 0) |
c86a8e70b40f
Made time interpolation more precise and added a unit test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3095
diff
changeset
|
53 |
4075
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
54 // Parse tries to parse a given string by the entries of the layout |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
55 // list one after another. The first matching time is returned. |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
56 // If no layout matches the last error is returned or time zero |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
57 // if the layout list is empty. |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
58 func (tg TimeParser) Parse(s string) (time.Time, error) { |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
59 var err error |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
60 var t time.Time |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
61 for _, layout := range tg { |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
62 if t, err = time.Parse(layout, s); err == nil { |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
63 break |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
64 } |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
65 } |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
66 return t, err |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
67 } |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
68 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
69 // InterpolateTime returns a function that linearly interpolates the |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
70 // time between t1 and t2 for given value m1 to m2. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
71 // If m1 is given to the returned function t1 is returned. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
72 // If m2 is given to the returned function t2 is returned. |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
73 // Values between m1 and m2 will result in the proportional |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4078
diff
changeset
|
74 // time between t1 and t2. |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
75 func InterpolateTime(t1 time.Time, m1 float64, t2 time.Time, m2 float64) func(float64) time.Time { |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
76 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
77 // f(m1) = t1 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
78 // f(m2) = t2 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
79 // t1 = m1*a + b <=> b = t1 - m1*a |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
80 // t2 = m2*a + b |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
81 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
82 // t1 - t2 = a*(m1 - m2) |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
83 // a = (t1-t2)/(m1 - m2) for m1 != m2 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
84 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
85 if m1 == m2 { |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
86 t := t1.Add(t2.Sub(t1) / 2) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
87 return func(float64) time.Time { return t } |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
88 } |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
89 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
90 a := t1.Sub(t2).Seconds() / (m1 - m2) |
3332
c86a8e70b40f
Made time interpolation more precise and added a unit test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3095
diff
changeset
|
91 b := t1.Sub(utc0).Seconds() - m1*a |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
92 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
93 return func(m float64) time.Time { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
94 x := m*a + b |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
95 secs := math.Ceil(x) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
96 nsecs := math.Ceil((x - secs) * (999999999 + 1)) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
97 return time.Unix(int64(secs), int64(nsecs)) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
98 } |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
99 } |