x lines of Python: read and write SEG-Y
Reading SEG-Y files comes up a lot in the geophysicist's workflow. Writing, less often, but it does come up occasionally. As long as we're mostly concerned with trace data and not location, both of these tasks can be fairly easily accomplished with ObsPy.
Today we'll load some seismic, compute an attribute on it, and save a new SEG-Y, in 10 lines of Python.
ObsPy is a rare thing. It demonstrates what a research group can accomplish with a little planning and a lot of perseverance (cf my whinging earlier this year about certain consortiums in our field). It's an open source Python package from the geophysicists at the University of Munich — Karl Bernhard Zoeppritz studied there for a while, so you know it's legit. The tool serves their research in earthquake and global seismology needs, and also happens to handle SEG-Y files quite nicely.
conda install obspy
. So, since minimalism is kind of the point here, look out for a future x lines of Python using that library. The sentences
As before, we'd like to express the process in just a few sentences of plain English. Assuming we just want to read the data into a NumPy array, look at it, do something to it, and write a new file, here's what we're doing:
- Read (or really index) the file as an ObsPy Stream object.
- Stack (in the NumPy sense) the Trace objects into a single NumPy array. We have data!
- Get the 99th percentile of the amplitudes to make plotting easier.
- Plot the data so we can see it.
- Get the sample interval of the data from a trace header.
- Compute the similarity attribute using our library bruges.
- Make a new Stream object to hold the outbound data.
- Add a Stats object, which holds the header, and recycle some header info.
- Append info about our data to the header.
- Write a new SEG-Y file with our computed data in it!
There's a bit more in the Jupyter Notebook (examining the file and trace headers, for example, and a few more plots) which, remember, you can run right in your browser! You don't need to install a thing. Please give it a look! Quick tip: Just keep hitting Shift+Enter to run the cells.
The seismic data used in this post is from the NPRA seismic repository of the USGS. The data is in the public domain.