/
dataFile.go
87 lines (71 loc) · 2.08 KB
/
dataFile.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
////////////////////////////////////////////////////////////////////////////////
// dataFile.go - Sep-5-2022 - aldebap
//
// Parser for Go-Plot data files
////////////////////////////////////////////////////////////////////////////////
package plot
import (
"bufio"
"errors"
"fmt"
"strconv"
"strings"
)
// LoadDataFile load a data file and return a Plot
func LoadDataFile(x_column uint8, y_column uint8, reader *bufio.Reader) ([]Point_2d, error) {
point := make([]Point_2d, 0, 10)
// read the input line by line
var line string
var firstLine = true
for {
bufLine, isPrefix, err := reader.ReadLine()
if err != nil {
break
}
line += string(bufLine)
if !isPrefix {
// ignore first line since it's expected to be a header
if firstLine {
firstLine = false
line = ""
continue
}
line = strings.TrimLeft(line, " ")
line = strings.TrimRight(line, " ")
// doing the split manually as strings.split() doesn't behave correctly when multiple spaces separate columns
//column := strings.Split(line, " ")
var column []string = make([]string, 0)
var value string
for _, char := range line {
if char == ' ' {
if len(value) > 0 {
column = append(column, value)
value = ""
}
continue
}
value = value + string(char)
}
if len(value) > 0 {
column = append(column, value)
}
// check if the line have the expected columns
if len(column) < int(x_column) || len(column) < int(y_column) {
return nil, errors.New(`line with less columns than expected: "` + line + `"`)
}
// check if the columns are numeric
x, err := strconv.ParseFloat(column[x_column-1], 64)
if err != nil {
return nil, errors.New(`column ` + fmt.Sprintf("%d", x_column) + ` expected to be numeric: "` + line + `"`)
}
y, err := strconv.ParseFloat(column[y_column-1], 64)
if err != nil {
return nil, errors.New(`column ` + fmt.Sprintf("%d", y_column) + ` expected to be numeric: "` + line + `"`)
}
// add the new point
point = append(point, Point_2d{X: x, Y: y})
line = ""
}
}
return point, nil
}