-
Notifications
You must be signed in to change notification settings - Fork 36
/
station.go
162 lines (137 loc) · 3.14 KB
/
station.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package meta
import (
"sort"
"strconv"
"strings"
"time"
)
const (
stationCode = iota
stationNetwork
stationName
stationLatitude
stationLongitude
stationElevation
stationDepth
stationDatum
stationStart
stationEnd
stationLast
)
var stationHeaders Header = map[string]int{
"Station": stationCode,
"Network": stationNetwork,
"Name": stationName,
"Latitude": stationLatitude,
"Longitude": stationLongitude,
"Elevation": stationElevation,
"Depth": stationDepth,
"Datum": stationDatum,
"Start Date": stationStart,
"End Date": stationEnd,
}
type Station struct {
Reference
Position
Span
}
type StationList []Station
func (s StationList) Len() int { return len(s) }
func (s StationList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s StationList) Less(i, j int) bool {
switch {
case s[i].Code < s[j].Code:
return true
case s[i].Code > s[j].Code:
return false
case s[i].Network < s[j].Network:
return true
default:
return false
}
}
func (s StationList) encode() [][]string {
var data [][]string
data = append(data, stationHeaders.Columns())
for _, row := range s {
data = append(data, []string{
strings.TrimSpace(row.Code),
strings.TrimSpace(row.Network),
strings.TrimSpace(row.Name),
strings.TrimSpace(row.latitude),
strings.TrimSpace(row.longitude),
strings.TrimSpace(row.elevation),
strings.TrimSpace(row.depth),
strings.TrimSpace(row.Datum),
row.Start.Format(DateTimeFormat),
row.End.Format(DateTimeFormat),
})
}
return data
}
func (s *StationList) decode(data [][]string) error {
if !(len(data) > 1) {
return nil
}
var stations []Station
fields := stationHeaders.Fields(data[0])
for _, row := range data[1:] {
d := fields.Remap(row)
lat, err := strconv.ParseFloat(d[stationLatitude], 64)
if err != nil {
return err
}
lon, err := strconv.ParseFloat(d[stationLongitude], 64)
if err != nil {
return err
}
elev, err := ParseFloat64(d[stationElevation])
if err != nil {
return err
}
depth, err := ParseFloat64(d[stationDepth])
if err != nil {
return err
}
start, err := time.Parse(DateTimeFormat, d[stationStart])
if err != nil {
return err
}
end, err := time.Parse(DateTimeFormat, d[stationEnd])
if err != nil {
return err
}
stations = append(stations, Station{
Reference: Reference{
Code: strings.TrimSpace(d[stationCode]),
Network: strings.TrimSpace(d[stationNetwork]),
Name: strings.TrimSpace(d[stationName]),
},
Span: Span{
Start: start,
End: end,
},
Position: Position{
Latitude: lat,
Longitude: lon,
Elevation: elev,
Datum: strings.TrimSpace(d[stationDatum]),
Depth: depth,
latitude: strings.TrimSpace(d[stationLatitude]),
longitude: strings.TrimSpace(d[stationLongitude]),
elevation: strings.TrimSpace(d[stationElevation]),
depth: strings.TrimSpace(d[stationDepth]),
},
})
}
*s = StationList(stations)
return nil
}
func LoadStations(path string) ([]Station, error) {
var s []Station
if err := LoadList(path, (*StationList)(&s)); err != nil {
return nil, err
}
sort.Sort(StationList(s))
return s, nil
}