-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreference_list.go
79 lines (74 loc) · 1.71 KB
/
reference_list.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
package quebec
import (
"encoding/csv"
"fmt"
"io"
"github.com/whitewater-guide/gorge/core"
)
func (s *scriptQuebec) getReferenceList() (map[string]core.Gauge, error) {
resp, err := core.Client.Get(s.referenceListURL, &core.RequestOptions{SkipCookies: true})
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
reader.Comma = ','
reader.LazyQuotes = true
result := make(map[string]core.Gauge)
skippedTitle := false
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if e, ok := err.(*csv.ParseError); ok && e.Err == csv.ErrFieldCount {
continue
} else if err != nil {
return nil, err
}
if len(line) != 18 {
return nil, fmt.Errorf("unexpected csv format with %d rows insteas of 18", len(line))
}
if !skippedTitle {
skippedTitle = true
continue
}
code := line[0]
lat, err := convertDMS(line[2])
if err != nil {
return nil, fmt.Errorf("failed to parse latitude '%s'", line[2])
}
lon, err := convertDMS(line[3])
if err != nil {
return nil, fmt.Errorf("failed to parse longtitude '%s'", line[3])
}
dataType := line[9]
// B = Discharge and Water Level
// H = Water level
// Q = Discharge
var level, flow string
if old, ok := result[code]; ok {
level = old.LevelUnit
flow = old.FlowUnit
}
if level == "" && (dataType == "B" || dataType == "H") {
level = "m"
}
if flow == "" && (dataType == "B" || dataType == "Q") {
flow = "m3/s"
}
station := core.Gauge{
GaugeID: core.GaugeID{
Code: code,
Script: s.name,
},
Location: &core.Location{
Latitude: lat,
Longitude: -lon,
},
LevelUnit: level,
FlowUnit: flow,
}
result[code] = station
}
return result, nil
}