-
Notifications
You must be signed in to change notification settings - Fork 36
/
mount.go
147 lines (122 loc) · 2.86 KB
/
mount.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
package meta
import (
"sort"
"strconv"
"strings"
"time"
)
const (
mountCode = iota
mountNetwork
mountName
mountLatitude
mountLongitude
mountElevation
mountDatum
mountDescription
mountStart
mountEnd
mountLast
)
var mountHeaders Header = map[string]int{
"Mount": mountCode,
"Network": mountNetwork,
"Name": mountName,
"Latitude": mountLatitude,
"Longitude": mountLongitude,
"Elevation": mountElevation,
"Datum": mountDatum,
"Description": mountDescription,
"Start Date": mountStart,
"End Date": mountEnd,
}
type Mount struct {
Reference
Position
Span
Description string
}
type MountList []Mount
func (m MountList) Len() int { return len(m) }
func (m MountList) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m MountList) Less(i, j int) bool { return m[i].Code < m[j].Code }
func (m MountList) encode() [][]string {
var data [][]string
data = append(data, mountHeaders.Columns())
for _, row := range m {
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.Datum),
strings.TrimSpace(row.Description),
row.Start.Format(DateTimeFormat),
row.End.Format(DateTimeFormat),
})
}
return data
}
func (m *MountList) decode(data [][]string) error {
if !(len(data) > 1) {
return nil
}
var mounts []Mount
fields := mountHeaders.Fields(data[0])
for _, row := range data[1:] {
d := fields.Remap(row)
lat, err := strconv.ParseFloat(d[mountLatitude], 64)
if err != nil {
return err
}
lon, err := strconv.ParseFloat(d[mountLongitude], 64)
if err != nil {
return err
}
elev, err := strconv.ParseFloat(d[mountElevation], 64)
if err != nil {
return err
}
start, err := time.Parse(DateTimeFormat, d[mountStart])
if err != nil {
return err
}
end, err := time.Parse(DateTimeFormat, d[mountEnd])
if err != nil {
return err
}
mounts = append(mounts, Mount{
Reference: Reference{
Code: strings.TrimSpace(d[mountCode]),
Network: strings.TrimSpace(d[mountNetwork]),
Name: strings.TrimSpace(d[mountName]),
},
Position: Position{
Latitude: lat,
Longitude: lon,
Elevation: elev,
Datum: strings.TrimSpace(d[mountDatum]),
latitude: strings.TrimSpace(d[mountLatitude]),
longitude: strings.TrimSpace(d[mountLongitude]),
elevation: strings.TrimSpace(d[mountElevation]),
},
Span: Span{
Start: start,
End: end,
},
Description: strings.TrimSpace(d[mountDescription]),
})
}
*m = MountList(mounts)
return nil
}
func LoadMounts(path string) ([]Mount, error) {
var m []Mount
if err := LoadList(path, (*MountList)(&m)); err != nil {
return nil, err
}
sort.Sort(MountList(m))
return m, nil
}