-
Notifications
You must be signed in to change notification settings - Fork 0
/
r2.go
73 lines (65 loc) · 1.43 KB
/
r2.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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"slices"
"strconv"
)
// Just enabling ReuseRecord makes a pretty big difference (shaves roughly 20%
// or so).
func r2(measurementsPath string) {
file, err := os.Open(measurementsPath)
if err != nil {
log.Fatalf("Could not open %s: %s", measurementsPath, err)
}
defer file.Close()
stationStats := make(map[string]Stats)
csvReader := csv.NewReader(file)
csvReader.Comma = ';'
csvReader.Comment = '#'
csvReader.FieldsPerRecord = 2
csvReader.ReuseRecord = true
for {
row, err := csvReader.Read()
if row == nil && err == io.EOF {
break
}
if err != nil {
log.Fatalf("Could not read row from %s: %s", measurementsPath, err)
}
station := row[0]
reading, err := strconv.ParseFloat(row[1], 64)
if err != nil {
log.Fatalf("Could not convert reading '%s' to float64", row[1])
}
stats, ok := stationStats[station]
if !ok {
stationStats[station] = Stats{
Max: reading,
Min: reading,
Sum: reading,
Count: 1,
}
} else {
stats.Add(reading)
stationStats[station] = stats
}
}
stations := make([]string, 0, len(stationStats))
for station := range stationStats {
stations = append(stations, station)
}
slices.Sort(stations)
fmt.Print("{")
for i, station := range stations {
stats := stationStats[station]
fmt.Printf("%s=%s", station, stats)
if i != len(stations)-1 {
fmt.Print(", ")
}
}
fmt.Println("}")
}