Skip to content

Commit 8da2667

Browse files
authored
Merge pull request #10 from slimgroup/gen-and-perf
Improve implementation for better performance with remotes
2 parents 4c2eaec + 2153357 commit 8da2667

15 files changed

+91
-277
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020

2121
matrix:
22-
version: ['1.6', '1.7']
22+
version: ['1.6', '1.7', '1.8']
2323
os: [ubuntu-latest, macos-latest]
2424

2525
steps:

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SegyIO"
22
uuid = "157a0f19-4d44-4de5-a0d0-07e2f0ac4dfa"
33
authors = ["Henryk Modzelewski <[email protected]>"]
4-
version = "0.7.7"
4+
version = "0.8.0"
55

66
[deps]
77
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"

src/SegyIO.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module SegyIO
22

33
myRoot=dirname(dirname(pathof(SegyIO)))
4-
CHUNKSIZE=2048
5-
MB2B=1024^2
4+
CHUNKSIZE = 2048
5+
TRACE_CHUNKSIZE = 2048
6+
MB2B = 1024^2
67

78
# what's being used
89
using Distributed

src/read/read_block.jl

+8-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
11
export read_block!
22

33
function read_block!(b::BlockScan, ns::Int, dsf::Int, tmp_data, tmp_headers)
4-
5-
f = open(b.file)
6-
seek(f, b.startbyte)
7-
brange = b.endbyte - b.startbyte
8-
s = IOBuffer(read(f, brange))
9-
ntraces = Int((brange)/(240 + ns*4))
10-
fh = FileHeader()
11-
fh.bfh.ns = ns; fh.bfh.DataSampleFormat = dsf
12-
13-
# Check dsf
14-
datatype = Float32
15-
if fh.bfh.DataSampleFormat == 1
16-
datatype = IBMFloat32
17-
elseif fh.bfh.DataSampleFormat != 5
18-
@error "Data type not supported ($(fh.bfh.DataSampleFormat))"
19-
end
20-
21-
th_b2s = th_byte2sample()
22-
# Read each trace
23-
for trace in 1:ntraces
24-
read_trace!(s, fh.bfh, datatype, tmp_headers, tmp_data, trace, th_b2s)
25-
end
26-
4+
read_block!(b, th_keys(), ns, dsf, tmp_data, tmp_headers)
275
end
286

297
function read_block!(b::BlockScan, keys::Array{String, 1}, ns::Int, dsf::Int, tmp_data, tmp_headers)
@@ -32,7 +10,8 @@ function read_block!(b::BlockScan, keys::Array{String, 1}, ns::Int, dsf::Int, tm
3210
seek(f, b.startbyte)
3311
brange = b.endbyte - b.startbyte
3412
s = IOBuffer(read(f, brange))
35-
ntraces = Int((brange)/(240 + ns*4))
13+
trace_size = (240 + ns*4)
14+
ntraces = Int((brange)/trace_size)
3615
fh = FileHeader()
3716
fh.bfh.ns = ns; fh.bfh.DataSampleFormat = dsf
3817

@@ -46,8 +25,10 @@ function read_block!(b::BlockScan, keys::Array{String, 1}, ns::Int, dsf::Int, tm
4625

4726
th_b2s = th_byte2sample()
4827
# Read each trace
49-
for trace in 1:ntraces
50-
read_trace!(s, fh.bfh, datatype, tmp_headers, tmp_data, trace, keys, th_b2s)
28+
for trace in 1:TRACE_CHUNKSIZE:ntraces
29+
tracee = min(trace + TRACE_CHUNKSIZE - 1, ntraces)
30+
chunk = length(trace:tracee)*trace_size
31+
sloc = IOBuffer(read(s, chunk))
32+
read_traces!(sloc, fh.bfh, datatype, tmp_headers, view(tmp_data, :, trace:tracee), trace, keys, th_b2s)
5133
end
52-
5334
end

src/read/read_file.jl

+12-45
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,9 @@ read_file(s)
66
Read entire SEGY file from stream 's'.
77
"""
88
function read_file(s::IO, warn_user::Bool; start_byte::Int = 3600,
9-
end_byte::Int = position(seekend(s)))
10-
11-
# Read File Header
12-
fh = read_fileheader(s)
13-
14-
# Move to start of block
15-
seek(s, start_byte)
16-
17-
# Check datatype of file
18-
datatype = Float32
19-
if fh.bfh.DataSampleFormat == 1
20-
datatype = IBMFloat32
21-
elseif fh.bfh.DataSampleFormat != 5
22-
@error "Data type not supported ($(fh.bfh.DataSampleFormat))"
23-
end
24-
25-
# Check fixed length trace flag
26-
(fh.bfh.FixedLengthTraceFlag!=1 & warn_user) && @warn "Fixed length trace flag set in stream: $s"
27-
28-
## Check for extended text header
29-
30-
# Read traces
31-
ntraces = Int((end_byte - start_byte)/(240 + fh.bfh.ns*4))
32-
33-
# Preallocate memory
34-
headers = Array{BinaryTraceHeader, 1}(undef, ntraces)
35-
data = Array{datatype, 2}(undef, fh.bfh.ns, ntraces)
36-
th_b2s = th_byte2sample()
37-
38-
# Read each trace
39-
for trace in 1:ntraces
40-
41-
read_trace!(s, fh.bfh, datatype, headers, data, trace, th_b2s)
42-
43-
end
44-
45-
return SeisBlock(fh, headers, data)
9+
end_byte::Int = position(seekend(s)))
10+
# Read with all keys
11+
return read_file(s, th_keys(), warn_user; start_byte=start_byte, end_byte=end_byte)
4612
end
4713

4814
"""
@@ -51,8 +17,7 @@ read_file(s, keys)
5117
Read entire SEGY file from stream 's', only reading the header values in 'keys'.
5218
"""
5319
function read_file(s::IO, keys::Array{String, 1}, warn_user::Bool;
54-
start_byte::Int = 3600,
55-
end_byte::Int = position(seekend(s)))
20+
start_byte::Int = 3600, end_byte::Int = position(seekend(s)))
5621

5722
# Read File Header
5823
fh = read_fileheader(s)
@@ -64,7 +29,7 @@ function read_file(s::IO, keys::Array{String, 1}, warn_user::Bool;
6429
datatype = Float32
6530
if fh.bfh.DataSampleFormat == 1
6631
datatype = IBMFloat32
67-
else
32+
elseif fh.bfh.DataSampleFormat != 5
6833
@error "Data type not supported ($(fh.bfh.DataSampleFormat))"
6934
end
7035

@@ -74,18 +39,20 @@ function read_file(s::IO, keys::Array{String, 1}, warn_user::Bool;
7439
## Check for extended text header
7540

7641
# Read traces
77-
ntraces = Int((end_byte - start_byte)/(240 + fh.bfh.ns*4))
42+
trace_size = (240 + fh.bfh.ns*4)
43+
ntraces = Int((end_byte - start_byte)/trace_size)
7844

7945
# Preallocate memory
8046
headers = Array{BinaryTraceHeader, 1}(undef, ntraces)
8147
data = Array{datatype, 2}(undef, fh.bfh.ns, ntraces)
8248
th_b2s = th_byte2sample()
8349

8450
# Read each trace
85-
for trace in 1:ntraces
86-
87-
read_trace!(s, fh.bfh, datatype, headers, data, trace, keys, th_b2s)
88-
51+
for trace in 1:TRACE_CHUNKSIZE:ntraces
52+
tracee = min(trace + TRACE_CHUNKSIZE - 1, ntraces)
53+
chunk = length(trace:tracee)*trace_size
54+
sloc = IOBuffer(read(s, chunk))
55+
read_traces!(sloc, fh.bfh, datatype, headers, view(data, :, trace:tracee), trace, keys, th_b2s)
8956
end
9057

9158
return SeisBlock(fh, headers, data)

src/read/read_fileheader.jl

+11-65
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,7 @@ Use: fileheader = read_fileheader(s::IO; bigendian::Bool = true)
99
Returns a binary file header formed using bytes 3200-3600 from the stream 's'.
1010
"""
1111
function read_fileheader(s::IO; bigendian::Bool = true)
12-
13-
# Return to start of stream
14-
seekstart(s)
15-
16-
# Read text header
17-
th = String(read(s, 3200))
18-
19-
# Initialize binary file header
20-
bfh = BinaryFileHeader()
21-
fh_b2s = fh_byte2sample()
22-
23-
24-
if bigendian
25-
# Read all header values and put in fileheader
26-
for k in keys(fh_b2s)
27-
28-
# Seek to this header value location
29-
seek(s, fh_b2s[k])
30-
31-
# Read into file header
32-
sym = Symbol(k)
33-
setfield!(bfh, sym, bswap(read(s, typeof(getfield(bfh, sym)))))
34-
end
35-
else
36-
# Read all header values and put in fileheader
37-
for k in keys(fh_b2s)
38-
39-
# Seek to this header value location
40-
seek(s, fh_b2s[k])
41-
42-
# Read into file header
43-
sym = Symbol(k)
44-
setfield!(bfh, sym, read(s, typeof(getfield(bfh, sym))))
45-
end
46-
end
47-
48-
# Move to end of file header
49-
seek(s, 3600)
50-
return FileHeader(th, bfh)
12+
return read_fileheader(s, fh_keys(); bigendian=bigendian)
5113
end
5214

5315

@@ -85,39 +47,23 @@ function read_fileheader(s::IO, keys::Array{String,1}; bigendian::Bool = true)
8547
seekstart(s)
8648

8749
# Read text header
88-
th = String(read(s, 3200))
50+
th = read(s, 3600)
8951

9052
# Initialize binary file header
9153
bfh = BinaryFileHeader()
9254
fh_b2s = fh_byte2sample()
55+
swp(x) = bigendian ? bswap(x) : x
9356

57+
for k in keys
9458

95-
if bigendian
96-
# Read all header values and put in fileheader
97-
for k in keys
98-
99-
# Seek to this header value location
100-
seek(s, fh_b2s[k])
101-
102-
# Read into file header
103-
sym = Symbol(k)
104-
setfield!(bfh, sym, bswap(read(s, typeof(getfield(bfh, sym)))))
105-
end
106-
else
107-
# Read all header values and put in fileheader
108-
for k in keys
109-
110-
# Seek to this header value location
111-
seek(s, fh_b2s[k])
112-
113-
# Read into file header
114-
sym = Symbol(k)
115-
setfield!(bfh, sym, read(s, typeof(getfield(bfh, sym))))
116-
end
59+
# Read into file header
60+
sym = Symbol(k)
61+
nb = sizeof(getfield(bfh, sym))-1
62+
bst = fh_b2s[k]+1
63+
val = swp(reinterpret(typeof(getfield(bfh, sym)), th[bst:bst+nb])[1])
64+
setfield!(bfh, sym, val)
11765
end
11866

119-
# Move to end of file header
12067
seek(s, 3600)
121-
122-
return FileHeader(th, bfh)
68+
return FileHeader(String(th[1:3200]), bfh)
12369
end

src/read/read_trace.jl

+19-24
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@ Use: read_trace!(s::IO,
1212
Reads 'trace' from the current position of stream 's' into 'headers' and
1313
'data'.
1414
"""
15-
function read_trace!(s::IO, fh::BinaryFileHeader,
16-
datatype::Type,
17-
headers::AbstractArray{BinaryTraceHeader,1},
18-
data::AbstractArray{<:Union{IBMFloat32, Float32}, 2},
19-
trace::Int,
20-
th_byte2sample::Dict{String,Int32})
21-
22-
# Read trace header
23-
setindex!(headers, read_traceheader(s, th_byte2sample), trace)
24-
25-
# Read trace
26-
setindex!(data, read_tracedata(s, fh, datatype), :, trace)
27-
28-
nothing
15+
function read_traces!(s::IO, fh::BinaryFileHeader, datatype::Type,
16+
headers::AbstractVector{BinaryTraceHeader},
17+
data::AbstractMatrix{<:Union{IBMFloat32, Float32}},
18+
trace::Int, th_byte2sample::Dict{String,Int32})
19+
20+
return read_traces!(s, fh, datatype, headers, data, trace, collect(keys(th_byte2sample)), th_byte2sample)
2921
end
3022

3123
"""
@@ -41,20 +33,23 @@ Use: read_trace!(s::IO,
4133
Reads 'trace' from the current position of stream 's' into 'headers' and
4234
'data'. Only the header values in 'keys' and read.
4335
"""
44-
function read_trace!(s::IO, fh::BinaryFileHeader,
36+
function read_traces!(s::IO, fh::BinaryFileHeader,
4537
datatype::Type,
46-
headers::AbstractArray{BinaryTraceHeader,1},
47-
data::AbstractArray{<:Union{IBMFloat32, Float32}, 2},
38+
headers::AbstractVector{BinaryTraceHeader},
39+
data::AbstractMatrix{<:Union{IBMFloat32, Float32}},
4840
trace::Int,
4941
keys::Array{String,1},
5042
th_byte2sample::Dict{String,Int32})
51-
52-
# Read trace header
53-
setindex!(headers, read_traceheader(s, keys, th_byte2sample), trace)
54-
55-
# Read trace
56-
setindex!(data, read_tracedata(s, fh, datatype), :, trace)
57-
43+
44+
ntrace = size(data, 2)
45+
ntrace == 0 && return
46+
for trace_loc=0:ntrace-1
47+
# Read trace header
48+
setindex!(headers, read_traceheader(s, keys, th_byte2sample), trace+trace_loc)
49+
50+
# Read trace
51+
setindex!(data, read_tracedata(s, fh, datatype), :, trace_loc+1)
52+
end
5853
nothing
5954
end
6055

0 commit comments

Comments
 (0)