-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
98 lines (85 loc) · 1.97 KB
/
read.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
package sacio
import (
"bufio"
"os"
"reflect"
)
// Read SAC file to structured SACData
func (s *SACData) Read(filePath string) error {
type SACRawData struct {
BitOrder int
DataBytes []byte
}
// Open SAC file
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
// Read file to bytes
var bytes []byte
reader := bufio.NewReader(file)
buffer := make([]byte, 1024)
for {
n, err := reader.Read(buffer)
if err != nil {
break
}
bytes = append(bytes, buffer[:n]...)
}
// Check file bit order
sacRawData := &SACRawData{
BitOrder: MSBFIRST,
DataBytes: bytes,
}
if assembleInt32(sacRawData.DataBytes[75*4+4:75*4+8], MSBFIRST) != 6 {
sacRawData.BitOrder = LSBFIRST
}
// Read data to struct
var (
dataOrder = sacRawData.BitOrder
dataBytes = sacRawData.DataBytes
)
for i, j := 0, 0; i < HEADER_LENGTH; j++ {
header, err := getVariableByIndex(j)
if err != nil {
return err
}
var (
dataVariable = header.Variable
dataSlice = dataBytes[i : i+header.Width]
)
i += header.Width
if dataVariable == "UNUSED" || dataVariable == "INTERNAL" {
continue
}
t := reflect.ValueOf(s).Elem()
switch header.DataType {
case "F":
result := assembleFloat32(dataSlice, dataOrder)
err = setStructField(t, dataVariable, F(result))
case "N":
result := assembleInt32(dataSlice, dataOrder)
err = setStructField(t, dataVariable, N(result))
case "L":
result := assembleBool(dataSlice, dataOrder)
err = setStructField(t, dataVariable, L(result))
case "K":
result := assembleString(dataSlice)
err = setStructField(t, dataVariable, K(result))
case "I":
result := assembleEnum(dataSlice, dataOrder)
err = setStructField(t, dataVariable, I(result))
}
if err != nil {
return err
}
}
// Read body section
var bodyData []F
for i := HEADER_LENGTH; i < len(dataBytes); i += 4 {
bodyData = append(bodyData, assembleFloat32(dataBytes[i:i+4], dataOrder))
}
s.Body = bodyData
return nil
}