-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathread_file.jl
63 lines (49 loc) · 1.85 KB
/
read_file.jl
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
export read_file
"""
read_file(s)
Read entire SEGY file from stream 's'.
"""
function read_file(s::IO, warn_user::Bool; start_byte::Int = 3600,
end_byte::Int = position(seekend(s)))
# Read with all keys
return read_file(s, th_keys(), warn_user; start_byte=start_byte, end_byte=end_byte)
end
"""
read_file(s, keys)
Read entire SEGY file from stream 's', only reading the header values in 'keys'.
"""
function read_file(s::IO, keys::Array{String, 1}, warn_user::Bool;
start_byte::Int = 3600, end_byte::Int = position(seekend(s)))
swap_bytes = bswap_needed(s)
# Read File Header
fh = read_fileheader(s; swap_bytes=swap_bytes)
# Move to start of block
seek(s, start_byte)
# Check datatype of file
datatype = Float32
if fh.bfh.DataSampleFormat == 1
datatype = IBMFloat32
elseif fh.bfh.DataSampleFormat != 5
@error "Data type not supported ($(fh.bfh.DataSampleFormat))"
end
# Check fixed length trace flag
(fh.bfh.FixedLengthTraceFlag!=1 & warn_user) && @warn "Fixed length trace flag set in stream: $s"
## Check for extended text header
# Read traces
trace_size = (240 + fh.bfh.ns*4)
ntraces = Int((end_byte - start_byte)/trace_size)
# Preallocate memory
headers = zeros(BinaryTraceHeader, ntraces)
data = Array{datatype, 2}(undef, fh.bfh.ns, ntraces)
th_b2s = th_byte2sample()
# Read each trace
ref = position(s)
for trace in 1:TRACE_CHUNKSIZE:ntraces
seek(s, (trace - 1) * trace_size + ref)
tracee = min(trace + TRACE_CHUNKSIZE - 1, ntraces)
chunk = length(trace:tracee)*trace_size
sloc = IOBuffer(read(s, chunk))
read_traces!(sloc, view(headers, trace:tracee), view(data, :, trace:tracee), keys, th_b2s; swap_bytes=swap_bytes)
end
return SeisBlock(fh, headers, data)
end