New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
segy: struct.error: 'h' format requires -32768 <= number <= 32767 #1393
Comments
Hi, here's a more pythonic way of writing your code, which does not resolve the issue, but it's a bit clearer for others, I hope. A few notes:
# !/usr/bin/env python
from obspy import read
filename = ['200', '201']
for fname in filename:
st = read(fname + '.sgy')
for x in range(1, 13, 1):
print('Writing Channel %d to .txt File' % (x + 12))
merged_st = st[((x - 1) + 12)::24].copy()
merged_st.merge(method=1, fill_value=None, interpolation_samples=2)
merged_st.write('%s_channel_%d.txt' % (fname, x), format='TSPAIR')
print('Writing Channel %d to .sgy File' % (x + 12))
merged_st.write('%s_channel_%d.sgy' % (fname, x), format="SEGY") As I said before, this code fails with the same error. I guess it's related to some invalid value in your stream... |
Thanks! Joseph Farrugia
|
I suspect it's the sample rate setting; for SEG-Y it is stored as a signed short int in microseconds, so it has to be between 1 us and 32.767 ms. This is commonly a problem in going to/from data that don't fit the assumptions in SEG-Y. You can divide the dt header by 1000, in which anything in Hz will be kHz in your SEG-Y workflow. |
I'll give that a shot! Thanks Brendan! Joseph Farrugia
|
Hey Brendan, I did as you suggested, and set the dt header to dt/1000 (2000/1000). I still get the same error! # !/usr/bin/env python
from obspy import read
filename = ['201']
for fname in filename:
st = read(fname + '.sgy')
for x in range(1, 13, 1):
print('Writing Channel %d to .txt File' % (x + 12))
merged_st = st[((x - 1) + 12)::24].copy()
merged_st.merge(method=1, fill_value=None, interpolation_samples=2)
merged_st[0].stats.segy.trace_header['sample_interval_in_ms_for_this_trace'] = 2
merged_st.write('%s_channel_%d_copy.txt' % (fname, x), format='TSPAIR')
print('Writing Channel %d to .sgy File' % (x + 12))
merged_st.write('%s_channel_%d_copy.sgy' % (fname, x), format="SEGY") Here's all the header information from the traces:
I think I changed the right attribute. |
@josephjamesfarrugia, download links to files are not working for me. Also, my feeling tells me this is the same problem as in #1385. |
@megies I've updated the links so they should work now! |
@megies @bsmithyman
Copy of the current code is below: |
Update: I changed the last line of my code to: merged_st.write('%s_channel_%d_copy.sgy' % (fname, x), format="SEGY", data_encoding=1, byteorder=sys.byteorder) And now I receive the following error (same as #1385):
|
Also, the length of each trace is long...approximately 45 minutes. Might that be producing the error? Update: I was ABLE to write a 30 second trace to SEGY. I'm thinking there must be a file size limit. |
Update: It's the number of samples.
If I set the record length to just a minute (roughly two traces), the number of samples (30500) is less than the max number of samples allowed (original error -- struct.error: 'h' format requires -32768 <= number <= 32767). I gradually increased the record length until the number of samples exceeded 32767, and the write to SEGY failed as expected. So I think it's a matter of resampling the stream object. Therefore, I'll close this issue. However, I get a new error when trying to write the resampled stream object. Thanks everyone for your time and input. |
Reopening as this definitely requires a better error message. Also: We currently write the number of samples as a signed short (thus the range from -32768 to 32767). We could store it as an unsigned short which would double the effective range of allowed number of samples. The SEG-Y manual does not appear to specify whether to use signed or unsigned integers (it only says to use two's complement integers) so we might risk compatibility with other SEG-Y tools. Any SEG-Y experts around here that know the best way to handle this? |
One of the clearer quick references I use is here, which is useful for the old formats. My mostly-compatible SEG-Y rev. 0 / rev. 1 library uses |
Thanks for the hint to look at the SU source code! I did not actually run any tests but from looking at the code I don't think SU can deal with unsigned values for the following reasons:
I guess we should just check if Seismic Unix can deal with unsigned values. Signed and unsigned values are identical if one does not leave the positive range of the signed version. Cheers! |
Everything looks fine in the code, but two things might be missing (that's what I additionally did to the data): trim all traces to get equal start and end times, fill gaps with zeros. |
In a recent discussion on the mailing list (http://lists.swapbytes.de/archives/obspy-users/2017-March/002358.html) it was pointed out that two's complement numbers are by definition signed numbers and that the SEG-Y spec states that all integers are two's complement integers. ObsPy thus does the correct (and maximally compatible) thing. Users who want unsigned values will have to monkey-patch ObsPy to get that behaviour. We still need a better error message though (also for #1396). |
"struct.error: short format requires SHRT_MIN <= number <= SHRT_MAX" "This error might occur because the traces within your stream are longer than SHRT_MAX, try to slice the stream in traces smaller than SHRT_MAX before saving to SEGY" |
This has been worked on and improved in recent PRs. Closing |
obspy version 1.0.1,Python version 2.7.6, OSX.
obspy was installed via pip and python via their website
Essentially, I'd like to use obspy to merge all the traces in a stream object, and then write a .sgy file from the merged stream. I've been able to do the aforementioned by writing the merged stream to a .txt file, but I've been unsuccessful in writing the .sgy file.
Below is a copy of my code:
I receive the following error:
I'm fairly new to Python, but have experience with Matlab. From my research, I don't think this is a Python error. Hoping someone knows how to rectify the problem!
You can download the corresponding files to run with my script here to replicate the error: https://www.dropbox.com/s/7m5wbb1hkmmhok8/200.sgy?dl=0
https://www.dropbox.com/s/sdttwiurxhjwgcg/201.sgy?dl=0
The text was updated successfully, but these errors were encountered: