Convert byte order to native on reading data or merging traces?
megies opened this issue · comments
Problem
First posted on the forum: https://discourse.obspy.org/t/the-big-endian-and-little-endian-confilict-for-merging-seismogram/1890/3
Currently, we raise an exception when attempting to merge traces that would otherwise be mergeable but only differ on the byteorder of the underlying data array, e.g. coming from different files with data in differing byteorder.
Probably we should fix the byte order and enable merging instead.
import numpy as np
from obspy import read
# make different endian sacfile
tr = read()[0]
starttime = tr.stats.starttime
first_part = tr.copy()
first_part = first_part.trim(starttime, starttime + 5)
first_part.write("/tmp/first.sac", format="SAC", byteorder="<")
second_part = tr.copy()
second_part = second_part.trim(starttime + 5, starttime + 10)
second_part.write("/tmp/second.sac", format="SAC", byteorder=">")
# merge them
st = read("/tmp/first.sac")
st += read("/tmp/second.sac")
st.plot(automerge=False)
try:
st.merge()
except Exception as e:
print(str(e))
for tr in st:
dtype = tr.data.dtype.newbyteorder('native')
tr.data = np.require(tr.data, dtype=dtype)
st.merge()
st.plot()
Proposed solution
Ideally we should probably simply convert byteorder to native on reading any data, but then we might also have to avoid keeping the original byteorder around in metadata (like we do when reading MiniSEED) or we might run into weird issues further down the road (e.g. when writing data again) and there is a mismatch between the data byteorder in the current object and the target byteorder when writing data out to file.
A more conservative approach might be to only convert byte order to native when actually performing operations that work on combining multiple traces, so probably only when merging (aka calling Trace.__(i)add__
)
Anybody have any thoughts on this? Might be easy to overlook some implications when changing things in this regard..
This issue has been mentioned on ObsPy Forum. There might be relevant details there: