-
Notifications
You must be signed in to change notification settings - Fork 552
Description
Avoid duplicates
- I searched existing issues
Bug Summary
There might be a bug when performing thest.rotate()
method.
Description
Problem Overview
When using the st.rotate() method in ObsPy, an issue arises when the tr.id of a trace is invalid. This problem occurs when creating a trace using the Trace class or reading a SAC file from an external source. It appears that ObsPy does not validate the tr.id, leading to unexpected behavior.
from obspy.core.trace import Trace
# Create a trace with an invalid tr.id
meta = {'station': 'MANZ.', 'network': 'BW', 'channel': 'EHZ'}
tr = Trace(header=meta)
print(tr.id) # Outputs 'BW.MANZ...EHZ'
# Performing st.rotate() causes issues when unpacking tr.id
net, sta, loc, cha = tr.id.split(".")
Additional Context
The problem becomes evident when using the st.rotate() method, which internally involves unpacking the components of tr.id. This can lead to errors and unexpected behavior due to the invalid format of tr.id.
The tricky thing is that, if we just set the tr.id by assignment, obspy will raise the error.
>>> tr.id = "invalid.trid"
Traceback (most recent call last):
File "/Users/junliu/anaconda3/envs/obspy/lib/python3.11/site-packages/obspy/core/trace.py", line 905, in id
net, sta, loc, cha = value.split(".")
^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 4, got 2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/junliu/anaconda3/envs/obspy/lib/python3.11/site-packages/obspy/core/trace.py", line 486, in __setattr__
return super(Trace, self).__setattr__(key, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/junliu/anaconda3/envs/obspy/lib/python3.11/site-packages/obspy/core/trace.py", line 912, in id
raise ValueError(msg)
ValueError: Not a valid SEED ID: 'invaild.trid'
And these debugging message is much more clear ( ValueError: Not a valid SEED ID: 'invaild.trid' )
Proposed Solution
Consider adding validation checks for tr.id when creating instances of the Stream or Trace class. This would help ensure that tr.id follows a valid format, preventing unexpected errors during operations like st.rotate().
Code to Reproduce
from obspy.core import read
st = read()
st.write("RJOB", format = "SAC")
# Then modify the header of sacfiles by sac
# All those command execute by sac
# r RJOB*
# ch KSTNM RJOB..
# wh
# read those sac file and perform the st.rotate()
st = read("RJOB*")
st.rotate(method = "NE->RT")
Error Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/junliu/anaconda3/envs/obspy/lib/python3.11/site-packages/obspy/core/stream.py", line 2756, in rotate
net, sta, loc, cha = tr.id.split(".")
^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 4)
ObsPy Version?
1.4.0
Operating System?
MacOS(M1)
Python Version?
3.11
Installation Method?
conda