-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kerim371/add-segy-endian-check
Add segy endian check
- Loading branch information
Showing
12 changed files
with
134 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
is_machine_littleendian() = Base.ENDIAN_BOM == 0x04030201 | ||
is_machine_bigendian() = Base.ENDIAN_BOM == 0x01020304 | ||
|
||
function is_segy_littleendian(s::IO) | ||
bfh = BinaryFileHeader() | ||
fh_b2s = fh_byte2sample() | ||
|
||
old_pos = position(s) | ||
seek(s, fh_b2s["DataSampleFormat"]) | ||
dsf = read(s, typeof(bfh.DataSampleFormat)) | ||
dsf_bs = bswap(dsf) | ||
seek(s, old_pos) | ||
|
||
if (dsf >= 1 && dsf <= 16 && is_machine_littleendian()) || | ||
(dsf_bs >= 1 && dsf_bs <= 16 && is_machine_bigendian()) | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
function is_segy_bigendian(s::IO) | ||
bfh = BinaryFileHeader() | ||
fh_b2s = fh_byte2sample() | ||
|
||
old_pos = position(s) | ||
seek(s, fh_b2s["DataSampleFormat"]) | ||
dsf = read(s, typeof(bfh.DataSampleFormat)) | ||
dsf = bswap(read(s, typeof(bfh.DataSampleFormat))) | ||
seek(s, old_pos) | ||
|
||
if (dsf >= 1 && dsf <= 16 && is_machine_bigendian()) || | ||
(dsf_bs >= 1 && dsf_bs <= 16 && is_machine_littleendian()) | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
|
||
""" | ||
bswap_needed(s::IO) | ||
Checks whether SEGY and host machine's byte order are same or not. | ||
NOTE: comparison is done based on binary file header value (Data Sample Format) | ||
thus stream (s::IO) must be opened from the begining of the file. | ||
# Examples | ||
s = open("/my/segy.segy", "r") | ||
swap_bytes = bswap_needed(s) | ||
""" | ||
function bswap_needed(s::IO) | ||
bfh = BinaryFileHeader() | ||
fh_b2s = fh_byte2sample() | ||
|
||
old_pos = position(s) | ||
seek(s, fh_b2s["DataSampleFormat"]) | ||
dsf = read(s, typeof(bfh.DataSampleFormat)) | ||
seek(s, old_pos) | ||
|
||
if dsf >= 1 && dsf <= 16 | ||
return false | ||
else | ||
return true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,53 @@ | ||
export read_trace | ||
export read_traces | ||
|
||
""" | ||
Use: read_trace!(s::IO, | ||
Use: read_traces!(s::IO, | ||
fh::BinaryFileHeader, | ||
datatype::Type, | ||
headers::AbstractArray{BinaryTraceHeader,1}, | ||
data::AbstractArray{<:Union{IBMFloat32, Float32}, 2}, | ||
trace::Int, | ||
th_byte2sample::Dict{String,Int32}) | ||
th_byte2sample::Dict{String,Int32}; | ||
swap_bytes::Bool) | ||
Reads 'trace' from the current position of stream 's' into 'headers' and | ||
'data'. | ||
""" | ||
function read_traces!(s::IO, headers::AbstractVector{BinaryTraceHeader}, | ||
data::AbstractMatrix{<:Union{IBMFloat32, Float32}}, | ||
th_byte2sample::Dict{String,Int32}) | ||
th_byte2sample::Dict{String,Int32}; | ||
swap_bytes::Bool) | ||
|
||
return read_traces!(s, headers, data, collect(keys(th_byte2sample)), th_byte2sample) | ||
return read_traces!(s, headers, data, collect(keys(th_byte2sample)), th_byte2sample; swap_bytes=swap_bytes) | ||
end | ||
|
||
""" | ||
Use: read_trace!(s::IO, | ||
Use: read_traces!(s::IO, | ||
fh::BinaryFileHeader, | ||
datatype::Type, | ||
headers::AbstractArray{BinaryTraceHeader,1}, | ||
data::AbstractArray{<:Union{IBMFloat32, Float32}, 2}, | ||
trace::Int, | ||
keys::Array{String,1}, | ||
th_byte2sample::Dict{String,Int32}) | ||
th_byte2sample::Dict{String,Int32}; | ||
swap_bytes::Bool) | ||
Reads 'trace' from the current position of stream 's' into 'headers' and | ||
'data'. Only the header values in 'keys' and read. | ||
""" | ||
function read_traces!(s::IO, headers::AbstractVector{BinaryTraceHeader}, | ||
data::AbstractMatrix{DT}, keys::Array{String,1}, | ||
th_byte2sample::Dict{String,Int32}) where {DT<:Union{IBMFloat32, Float32}} | ||
th_byte2sample::Dict{String,Int32}; swap_bytes::Bool) where {DT<:Union{IBMFloat32, Float32}} | ||
|
||
ntrace = size(data, 2) | ||
ntrace == 0 && return | ||
swp = swp_func(DT) | ||
tmph = zeros(UInt8, 240) | ||
for trace_loc=0:ntrace-1 | ||
# Read trace header | ||
read_traceheader!(s, keys, th_byte2sample, headers[trace_loc+1]; th=tmph) | ||
read_traceheader!(s, keys, th_byte2sample, headers[trace_loc+1]; swap_bytes=swap_bytes, th=tmph) | ||
# Read trace | ||
read!(s, view(data, :, trace_loc+1)) | ||
end | ||
map!(swp, data, data) | ||
(swap_bytes) && (data = bswap.(data)) | ||
nothing | ||
end | ||
|
||
swp_func(::Type{Float32}) = bswap | ||
swp_func(::Any) = x -> x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.