Skip to content

Instantly share code, notes, and snippets.

@kwinkunks
Last active April 28, 2021 17:18
Show Gist options
  • Save kwinkunks/238594e09711d70c98e5bb7eab0a6035 to your computer and use it in GitHub Desktop.
Save kwinkunks/238594e09711d70c98e5bb7eab0a6035 to your computer and use it in GitHub Desktop.
Convert SEG-Y into WAV files

You'll need to install Python somehow before using this script.

I recommend installing Miniconda

Then do this in the terminal (or Anaconda prompt on Windows):

conda create -n segy2wav python=3.8 scipy
conda activate segy2wav
pip install click segyio
python segy2wav.py --help

Good luck!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Matt Hall, Agile Scientific
Licence: Apache 2.0, please re-use this code!
To use the CLI type this on the command line:
python segy2wav.py --help
"""
import os
from scipy.io import wavfile
import numpy as np
import segyio
import click
@click.command()
@click.option('--infile', required=True, help='Input SEG-Y file.')
@click.option('--outfile', help='Output WAV file.')
def convert(infile, outfile=None):
"""
Convert from SEG-Y to WAV file.
Args:
infile (str): The input SEG-Y filename.
outfile (str): The output filename (optional).
Returns:
None. (Just writes the file to disk.)
"""
# Deal with not getting an output filename.
if outfile is None:
outfile = os.path.splitext(infile)[0] + '.wav'
# Read the SEG-Y data and get the sampling frequency.
with segyio.open(infile, strict=False) as s:
data = np.stack([t.astype(float) for t in s.trace])
fs = int(1e6 / s.bin[segyio.BinField.Interval])
# Normalize the data to ±1.0 and scale to int16.
data /= np.max(np.abs(data))
data *= np.iinfo(np.int16).max
data = np.nan_to_num(data)
# Write out the WAV file.
wavfile.write(outfile, fs, data.astype(np.int16))
print(f'Wrote {outfile}')
return
# What to do if this file gets run as a script.
if __name__ == '__main__':
convert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment