-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsites.go
68 lines (64 loc) · 1.69 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
package usgs
import (
"fmt"
"strconv"
"strings"
"github.com/whitewater-guide/gorge/core"
)
func (s *scriptUSGS) listStations(flow bool, gauges map[string]core.Gauge) error {
// Select parameter https://help.waterdata.usgs.gov/parameter_cd?group_cd=PHY
parameterCd, levelUnit, flowUnit := paramLevel, "ft", "" // Gage height, feet
if flow {
parameterCd, levelUnit, flowUnit = paramFlow, "", "ft3/s" // Discharge, cubic feet per second
}
return core.Client.StreamCSV(
fmt.Sprintf("%s/site/?format=rdb&stateCd=%s&siteType=ST¶meterCd=%s&siteStatus=all&hasDataTypeCd=iv", s.url, s.stateCd, parameterCd),
func(row []string) error {
g, ok := gauges[row[1]]
if ok {
if flow {
g.FlowUnit = flowUnit
} else {
g.LevelUnit = levelUnit
}
} else {
lat, err := strconv.ParseFloat(row[4], 64)
if err != nil {
return nil
}
lng, err := strconv.ParseFloat(row[5], 64)
if err != nil {
return nil
}
zone, err := core.CoordinateToTimezone(lat, lng)
if err != nil {
zone = "UTC"
}
alt, _ := strconv.ParseFloat(strings.TrimSpace(row[8]), 64)
g = core.Gauge{
GaugeID: core.GaugeID{
Script: s.name,
Code: row[1],
},
Name: row[2],
URL: fmt.Sprintf("https://waterdata.usgs.gov/nwis/inventory?agency_code=%s&site_no=%s", row[0], row[1]),
LevelUnit: levelUnit,
FlowUnit: flowUnit,
Location: &core.Location{
Latitude: core.TruncCoord(lat),
Longitude: core.TruncCoord(lng),
Altitude: alt,
},
Timezone: zone,
}
}
gauges[row[1]] = g
return nil
},
core.CSVStreamOptions{
Comma: '\t',
NumColumns: 12,
HeaderHeight: 29,
},
)
}