/
sfl.go
183 lines (166 loc) · 4.15 KB
/
sfl.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package feeds
import (
"bufio"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
func FindSFLFiles(dir string) (files []string, err error) {
pattern := "????-??-??T??-??-??[\\-\\+]??-??.sfl"
err = filepath.WalkDir(dir, func(walkPath string, d fs.DirEntry, walkErr error) error {
if walkErr != nil {
if d == nil {
// Failed fs.Stat on root dir
return walkErr
}
// ReadDir failed on this directory
fmt.Fprintf(os.Stderr, "error: %v\n", walkErr)
return nil
}
if !d.IsDir() {
// Look for uncompressed EVT files
found, matchErr := filepath.Match(pattern, d.Name())
if matchErr != nil {
panic(matchErr)
}
if found {
files = append(files, walkPath)
}
}
return nil
})
return files, err
}
// *****************************************************************************
type Sfl struct {
i int // index of next item to emit
data []sflRecord
headers []string
paths []string
outDir string
file *os.File // current output file
warnings []Warning
}
func NewSfl(files []string, outDir string) (s *Sfl, err error) {
s = &Sfl{i: -1}
s.data = []sflRecord{}
s.outDir = outDir
for idx, f := range files {
fileText, err := ioutil.ReadFile(f)
if err != nil {
return s, err
}
sc := bufio.NewScanner(strings.NewReader(string(fileText)))
lineNum := 0
for sc.Scan() {
lineNum++
lineText := sc.Text()
if lineNum == 1 {
s.paths = append(s.paths, f)
s.headers = append(s.headers, sc.Text())
continue
}
cols := strings.Split(lineText, "\t")
if len(cols) > 1 && len(cols[0]) == 25 {
tstamp := cols[0][:19] + "+00:00" // TZ untrustworthy, force UTC
tstamp = tstamp[:13] + ":" + tstamp[14:16] + ":" + tstamp[17:]
lineTime, err := time.Parse(time.RFC3339, tstamp)
if err != nil {
// Skip this line
newErr := fmt.Errorf("sfl: could not parse timestamp %s:%d %v", f, lineNum, err)
s.warnings = append(s.warnings, Warning{err: newErr})
continue
}
s.data = append(s.data, sflRecord{time: lineTime, data: lineText, idx: idx})
} else {
newErr := fmt.Errorf("sfl: unparsable line %s:%d", f, lineNum)
s.warnings = append(s.warnings, Warning{err: newErr})
}
}
}
// Sort by time, ascending
sort.SliceStable(s.data, func(i, j int) bool {
return s.data[i].time.Before(s.data[j].time)
})
return s, nil
}
func (s *Sfl) Close() (err error) {
if s.file != nil {
err = s.file.Close()
s = nil
}
return
}
func (s *Sfl) Earliest() (t time.Time) {
if len(s.data) > 0 {
t = s.data[0].time
}
return
}
func (s *Sfl) Emit() (err error) {
if s.i < 0 {
return
}
rec := s.data[s.i]
outFileTime, err := timeFromFilename(s.paths[rec.idx])
if err != nil {
return fmt.Errorf("sfl: %v", err)
}
doyDir := fmt.Sprintf("%d_%03d", outFileTime.Year(), outFileTime.YearDay())
outDir := filepath.Join(s.outDir, "datafiles", "evt", doyDir)
if err = os.MkdirAll(outDir, os.ModePerm); err != nil {
return fmt.Errorf("sfl: %v", err)
}
outPath := filepath.Join(outDir, filepath.Base(s.paths[rec.idx]))
if s.file == nil || s.file.Name() != outPath {
// No output file has been opened yet or it's time to change to a new output file
if err = s.Close(); err != nil {
return fmt.Errorf("sfl: %v", err)
}
if s.file, err = os.OpenFile(outPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm); err != nil {
return fmt.Errorf("sfl: %v", err)
}
// Write the header first
s.file.WriteString(fmt.Sprintf("%s\r\n", s.headers[rec.idx]))
}
// Write data line
s.file.WriteString(fmt.Sprintf("%s\r\n", rec.data))
return
}
func (s *Sfl) Time() (t time.Time) {
if s.i >= 0 && len(s.data) > 0 {
t = s.data[s.i].time
}
return
}
func (s *Sfl) Next() bool {
if s.i+1 < len(s.data) {
s.i++
return true
}
return false
}
func (s *Sfl) Warnings() []Warning {
return s.warnings
}
func (s *Sfl) Name() string {
return "sfl"
}
func (s *Sfl) Len() int {
return len(s.data)
}
// sfl is one data line of an SFL file with a header line prepended if this is
// the first line in a file.
type sflRecord struct {
time time.Time
idx int
data string
}
func (sr sflRecord) String() string {
return fmt.Sprintf("%v %s", sr.time, sr.data)
}