-
Notifications
You must be signed in to change notification settings - Fork 3
/
sites.go
93 lines (83 loc) · 2.21 KB
/
sites.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package nzwgn
import (
"fmt"
"strconv"
"github.com/mattn/go-nulltype"
"github.com/whitewater-guide/gorge/core"
)
type dataItem struct {
core.Measurement
levelUnit string
flowUnit string
name string
}
// Another possible option is "WQ%20/%20Rivers%20and%20Streams" but I think it's useless
const collection = "River%20and%20Stream%20Levels"
func (s *scriptNzwgn) fetchLocations() (map[string]core.Location, error) {
url := fmt.Sprintf("%s?Service=Hilltop&Request=SiteList&Collection=%s&Location=LatLong", s.url, collection)
var list siteList
err := core.Client.GetAsXML(url, &list, nil)
if err != nil {
return nil, err
}
result := make(map[string]core.Location, len(list.Site))
for _, v := range list.Site {
// because lat/long is bad site doesn't stop to exist
lat, _ := strconv.ParseFloat(v.Latitude, 64) //nolint:errcheck
lng, _ := strconv.ParseFloat(v.Longitude, 64) //nolint:errcheck
result[core.CodeFromName(v.Name)] = core.Location{
Latitude: core.TruncCoord(lat),
Longitude: core.TruncCoord(lng),
}
}
return result, nil
}
func (s *scriptNzwgn) fetchValues() (map[string]dataItem, error) {
url := fmt.Sprintf("%s?Service=Hilltop&Request=GetData&Collection=%s&Location=LatLong", s.url, collection)
var data measurements
err := core.Client.GetAsXML(url, &data, nil)
if err != nil {
return nil, err
}
result := map[string]dataItem{}
for _, i := range data.Measurement {
var flow, level nulltype.NullFloat64
switch i.DataSource.ItemInfo.ItemName {
case "Flow":
flow.Scan(i.Data.E.I1) //nolint:errcheck
case "Stage":
level.Scan(i.Data.E.I1) //nolint:errcheck
default:
continue
}
unit := i.DataSource.ItemInfo.Units
if unit == "m³/sec" {
unit = "m3/s"
}
m, ok := result[i.SiteName]
if !ok {
m = dataItem{
Measurement: core.Measurement{
GaugeID: core.GaugeID{
Script: s.name,
Code: core.CodeFromName(i.SiteName),
},
Timestamp: core.HTime{
Time: i.Data.E.T.Time,
},
},
}
}
if !m.Level.Valid() && level.Valid() {
m.levelUnit = unit
m.Level = level
}
if !m.Flow.Valid() && flow.Valid() {
m.flowUnit = unit
m.Flow = flow
}
m.name = i.SiteName
result[i.SiteName] = m
}
return result, nil
}