-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_parser.go
More file actions
141 lines (116 loc) · 3.08 KB
/
csv_parser.go
File metadata and controls
141 lines (116 loc) · 3.08 KB
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
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
)
type RawRfRecord struct {
recorded_at time.Time
hz_start string
hz_end string
hz_step string
averaged_samples string
samples []string
}
const (
matchPattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}, [0-9]{2}:[0-9]{2}:[0-9]{2}, *"
timeLayout = "2006-01-02 15:04:05 (MST)"
)
func Parse(data <-chan string, rf_data chan<- RfRecord) {
for line := range data {
matched, err := regexp.MatchString(matchPattern, line)
if err != nil {
log.Printf("Error matching data: %s\n", err)
continue
}
if false == matched {
continue
}
// TODO Validate the line before trying to parse it
records, err := buildRfRecords(line)
go func() {
for _, record := range records {
rf_data <- record
}
}()
}
}
func buildRfRecords(line string) ([]RfRecord, error) {
parsed_csv := parseCsv(line)
if len(parsed_csv) == 0 {
return nil, errors.New("Empty raw record")
}
raw_record := RawRfRecord{
recorded_at: getTimestampFromRecord(parsed_csv),
hz_start: parsed_csv[2],
hz_end: parsed_csv[3],
hz_step: parsed_csv[4],
averaged_samples: parsed_csv[5],
samples: parsed_csv[6 : len(parsed_csv)-1],
}
result, err := processRawRfRecord(raw_record)
return result, err
}
func processRawRfRecord(rawRecord RawRfRecord) ([]RfRecord, error) {
// TODO This should be handled by a validator in Parse
step, err := stringToFloat64(rawRecord.hz_step)
if err != nil {
msg := fmt.Sprintf("Error parsing step for: %+v - %s\n", rawRecord)
log.Printf(msg)
return nil, errors.New(msg)
}
start, err := stringToFloat64(rawRecord.hz_start)
if err != nil {
msg := fmt.Sprintf("Error parsing step for: %+v - %s\n", rawRecord)
log.Printf(msg)
return nil, errors.New(msg)
}
records := make([]RfRecord, len(rawRecord.samples))
for i, raw_power := range rawRecord.samples {
frequency := start + (float64(i) * step)
power, err := stringToFloat64(raw_power)
if err != nil {
log.Printf("Error parsing power value(%s): %s\n", raw_power, err)
continue
}
records[i] = RfRecord{
recorded_at: rawRecord.recorded_at,
frequency: frequency,
power: power,
}
}
return records, nil
}
func stringToFloat64(val string) (float64, error) {
return strconv.ParseFloat(val, 64)
}
// TODO add err return for empty slice
func parseCsv(data string) []string {
reader := csv.NewReader(strings.NewReader(data))
record, err := reader.Read()
if err != nil {
log.Println("Error parsing CSV: %s\n", err)
return make([]string, 0)
}
for i, val := range record {
record[i] = strings.TrimSpace(val)
}
return record
}
func getTimestampFromRecord(record []string) time.Time {
record_date := record[0]
record_time := record[1]
record_tz, _ := time.Now().Local().Zone()
composed_timestamp := fmt.Sprintf("%s %s (%s)", record_date, record_time, record_tz)
recorded_at, err := time.Parse(timeLayout, composed_timestamp)
if err != nil {
// TODO handle error gracefully
log.Fatal(err)
}
return recorded_at
}