-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathascii_data.go
86 lines (72 loc) · 2.03 KB
/
ascii_data.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
package comtrade
import (
"bytes"
"io"
"os"
"strconv"
)
// Parse AsciiData AsciiData is the ascii data of a comtrade data file.
func init() {
Add(TypeAscii, func() Parser {
return &AsciiData{}
})
}
type AsciiData struct {
}
func (a *AsciiData) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*Data, error) {
comtradeData := &Data{}
datFile, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer datFile.Close()
content, err := io.ReadAll(datFile)
if err != nil {
return nil, err
}
lines := bytes.Split(content, []byte("\n"))
if len(lines)-1 != int(endSamp) {
return nil, ErrInvalidFile
}
comtradeData.AnalogData = make([]AnalogData, int(endSamp))
comtradeData.DigitalData = make([]DigitalData, int(endSamp))
for i := 0; i < int(endSamp); i++ {
comtradeData.AnalogData[i].Data = make([]int32, analogNum)
comtradeData.DigitalData[i].Data = make([]uint8, digitalNum)
}
total := 1 + 1 + int(analogNum) + int(digitalNum)
var tempList [][]byte
for i := 0; i < int(endSamp); i++ {
tempList = bytes.Split(lines[i], []byte(","))
if len(tempList) != total {
return nil, ErrReadFirstLine
}
if value, err := strconv.Atoi(ByteToString(tempList[0])); err != nil {
return nil, err
} else {
comtradeData.AnalogData[i].N = uint32(value)
comtradeData.DigitalData[i].N = uint32(value)
}
if value, err := strconv.Atoi(ByteToString(tempList[1])); err != nil {
return nil, err
} else {
comtradeData.AnalogData[i].Timestamp = uint32(value)
comtradeData.DigitalData[i].Timestamp = uint32(value)
}
for j := 0; j < int(analogNum); j++ {
if value, err := strconv.ParseInt(ByteToString(tempList[j+2]), 10, 64); err != nil {
return nil, err
} else {
comtradeData.AnalogData[i].Data[j] = int32(value)
}
}
for j := 0; j < int(digitalNum); j++ {
if value, err := strconv.ParseInt(ByteToString(tempList[j+2+int(analogNum)]), 10, 64); err != nil {
return nil, err
} else {
comtradeData.DigitalData[i].Data[j] = uint8(value)
}
}
}
return comtradeData, nil
}