Skip to content

Instantly share code, notes, and snippets.

@kwinkunks
Last active March 24, 2021 19:28
Show Gist options
  • Save kwinkunks/8dc1381921e374e38e7bf136339a7da0 to your computer and use it in GitHub Desktop.
Save kwinkunks/8dc1381921e374e38e7bf136339a7da0 to your computer and use it in GitHub Desktop.
Read a SEG2 formatted seismic or GPR file.
"""
Run as a script:
python test_check.py /path/to/myfile.seg2
Adapted from https://github.com/agile-geoscience/jeepr
"""
from struct import unpack
import sys
def check_file(fname):
"""
Check a file to see if it's SEG2 format.
SEG2 format specification:
Pullan, S. E., 1990, Recommended standard for seismic (/radar) files in
the personal computer environment: Geophysics, 55, no. 09, 1260-1271.
"""
with open(fname, 'rb') as f:
valid = True
file_descriptor_block = f.read(32)
# Check file descriptor indicates SEG2.
first_byte, = unpack(b'B', file_descriptor_block[0:1])
if first_byte == 0x55:
endian = b'<'
elif first_byte == 0x3a:
endian = b'>'
else:
valid = False
# Check SEG2 revision number (only rev 1 exists).
rev, = unpack(endian + b'H', file_descriptor_block[2:4])
return valid, rev
if __name__ == '__main__':
valid, rev = check_file(sys.argv[1])
if valid and rev == 1:
print('This appears to be a valid SEG2 file.')
elif rev != 1:
print('This may be valid SEG2, but rev is {} not 1.'.format(rev))
else:
print('This does not appear to be a valid SEG2 file.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment