-
Notifications
You must be signed in to change notification settings - Fork 36
/
constituent.go
152 lines (124 loc) · 2.91 KB
/
constituent.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
package meta
import (
"sort"
"strconv"
"strings"
"time"
)
const (
constituentGauge = iota
constituentNumber
constituentName
constituentAmplitude
constituentLag
constituentStart
constituentEnd
constituentLast
)
var constituentHeaders Header = map[string]int{
"Gauge": constituentGauge,
"Number": constituentNumber,
"Constituent": constituentName,
"Amplitude": constituentAmplitude,
"Lag": constituentLag,
"Start Date": constituentStart,
"End Date": constituentEnd,
}
type Constituent struct {
Span
Gauge string
Number int
Name string
Amplitude float64
Lag float64
amplitude string // shadow variable to maintain formatting
lag string // shadow variable to maintain formatting
}
type ConstituentList []Constituent
func (c ConstituentList) Len() int { return len(c) }
func (c ConstituentList) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ConstituentList) Less(i, j int) bool {
switch {
case c[i].Gauge < c[j].Gauge:
return true
case c[i].Gauge > c[j].Gauge:
return false
case c[i].Start.Before(c[j].Start):
return true
case c[i].Start.After(c[j].Start):
return false
case c[i].Number < c[j].Number:
return true
default:
return false
}
}
func (c ConstituentList) encode() [][]string {
var data [][]string
data = append(data, constituentHeaders.Columns())
for _, row := range c {
data = append(data, []string{
strings.TrimSpace(row.Gauge),
strconv.Itoa(row.Number),
strings.TrimSpace(row.Name),
strings.TrimSpace(row.amplitude),
strings.TrimSpace(row.lag),
row.Start.Format(DateTimeFormat),
row.End.Format(DateTimeFormat),
})
}
return data
}
func (c *ConstituentList) decode(data [][]string) error {
if !(len(data) > 1) {
return nil
}
var constituents []Constituent
fields := constituentHeaders.Fields(data[0])
for _, row := range data[1:] {
d := fields.Remap(row)
num, err := ParseInt(d[constituentNumber])
if err != nil {
return err
}
amp, err := strconv.ParseFloat(d[constituentAmplitude], 64)
if err != nil {
return err
}
lag, err := strconv.ParseFloat(d[constituentLag], 64)
if err != nil {
return err
}
start, err := time.Parse(DateTimeFormat, d[constituentStart])
if err != nil {
return err
}
end, err := time.Parse(DateTimeFormat, d[constituentEnd])
if err != nil {
return err
}
constituents = append(constituents, Constituent{
Span: Span{
Start: start,
End: end,
},
Gauge: d[constituentGauge],
Number: num,
Name: d[constituentName],
Amplitude: amp,
Lag: lag,
amplitude: d[constituentAmplitude],
lag: d[constituentLag],
})
}
*c = ConstituentList(constituents)
return nil
}
func LoadConstituents(path string) ([]Constituent, error) {
var c []Constituent
if err := LoadList(path, (*ConstituentList)(&c)); err != nil {
return nil, err
}
sort.Sort(ConstituentList(c))
return c, nil
}