-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread.go
162 lines (142 loc) · 3.64 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package mseedio
import (
"bufio"
"fmt"
"os"
)
// m.Read() reads miniSEED file to structured MiniSeedData
func (m *MiniSeedData) Read(filePath string) error {
// Open miniSEED 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]...)
}
// Return error if length is less than 48 bytes
if len(bytes) < 48 {
err := fmt.Errorf("file length is less than 48 bytes")
return err
}
// Guess data bit order
bitOrder, err := getBitOrder(bytes[46:48])
if err != nil {
return err
}
// Parse fixed and blockette sections
var (
fixedSections = []fixedSection{}
blocketteSections = []blocketteSection{}
samplesNumber = 0 // Total number of samples
)
for i := 0; i < len(bytes); i += 64 {
var (
fs = fixedSection{}
bs = blocketteSection{}
)
// Parse fixed section
fsOffset := i + FIXED_SECTION_LENGTH
err := fs.Parse(bytes[i:fsOffset], bitOrder)
if err != nil ||
fs.SectionEndOffset != FIXED_SECTION_LENGTH ||
(fs.DataQuality != "D" && fs.DataQuality != "R" &&
fs.DataQuality != "Q" && fs.DataQuality != "M") {
continue
}
// Parse blockette
bsOffset := i + int(fs.DataStartOffset)
err = bs.Parse(bytes[fsOffset:bsOffset], bitOrder)
if err != nil {
continue
}
// Determine encoding for non 100-blockettes
if bs.BlocketteCode == 1001 {
// Encoding is usually in bytes[fsOffset:bsOffset][12]
bs.EncodingFormat = int32(bytes[fsOffset:bsOffset][12])
}
// Set slice position [start:end]
fs.ReaderOffset = sectionOffset{
i, fsOffset,
}
bs.ReaderOffset = sectionOffset{
fsOffset, bsOffset,
}
// Set global start time
if i == 0 {
m.StartTime = fs.StartTime
}
// Add samples and append
samplesNumber += int(fs.SamplesNumber)
fixedSections = append(fixedSections, fs)
blocketteSections = append(blocketteSections, bs)
}
// Detect initial frame length automatically
var initLength int
for i := 64; i < len(bytes); i += 64 {
// Parse fixed section again
var fs = fixedSection{}
var fsOffset = i + FIXED_SECTION_LENGTH
err := fs.Parse(bytes[i:fsOffset], bitOrder)
if err == nil && fs.SectionEndOffset == FIXED_SECTION_LENGTH {
initLength = i
break
}
}
if initLength == 0 {
initLength = len(bytes)
}
// Detect rest each frame length automatically
var (
frameLength = []int{initLength}
lastOffset sectionOffset
)
for i := 1; i < len(fixedSections); i++ {
readerOffset := fixedSections[i].ReaderOffset
frameLength = append(frameLength, readerOffset.Start-lastOffset.Start)
lastOffset = readerOffset
}
// Parse data series section
for i := 0; i < len(fixedSections); i++ {
var (
endIndex = len(bytes)
startIndex = blocketteSections[i].ReaderOffset.End
)
if i != len(fixedSections)-1 {
endIndex = fixedSections[i].ReaderOffset.Start + frameLength[i+1]
}
var ds dataSection
err = ds.Parse(
bytes[startIndex:endIndex],
int(fixedSections[i].SamplesNumber),
int(blocketteSections[i].BlocketteCode),
int(blocketteSections[i].EncodingFormat),
bitOrder,
)
if err != nil {
return err
}
// Append data series
m.Series = append(m.Series, dataSeries{
DataSection: ds,
FixedSection: fixedSections[i],
BlocketteSection: blocketteSections[i],
})
}
// Set file info
m.Order = bitOrder
m.Samples = samplesNumber
m.Records = len(fixedSections)
m.Type = int(blocketteSections[0].BlocketteCode)
m.EndTime = fixedSections[len(fixedSections)-1].StartTime
return nil
}