Good afternoon, I’m Angel Matos from Peru, Here, this topic processing seismological data using Python and Obspy is not very widespread. I am currently working on my undergraduate thesis and a part of the workflow is to read a SEG2 file format recorded from an OYO seismograph (OYO Corp from Japan). I have two queries and I hope you can help me.
a) With Obspy, I can read files with .dat extension (from Geometrics seismograph), on the other hand, I can’t read files with .sg2 extension (from OYO seismograph). You can see from the picture below a better explanation about the extensions. This screenshot was taken from SeisImagerSW Manual
The case i that I don’t really want to use SeisImager, For my undergraduate thesis, I would like to read seismological data using Obspy, so, Is there a way I can read files with .sg2 extension.
Example of my code in Python (Python 3.8.5, Obspy 1.2.2, and working with JupyterNotebook on an environment created with Anaconda)
from obspy import read
st = read(pathname_or_url = “filename_1.dat”) #this works
st = read(pathname_or_url = “filename_2.sg2”) #this gives me a large error ending with KeyError: ‘SAMPLE_INTERVAL’
b) If that’s not possible, is there a way to convert .sg2 to .dat? Is it possible that exists a Python code for converting .sg2 to .dat? , since I need to automate the process (work with many files)
c) I would like you to be able to help me by providing a bibliography or some standard guide to how to work with these files. In a try to convert the files, I found this on internet:
This is .sg2 extension (used from OYO seismographs) data_OYO.sg2 (204.6 KB)
This is .dat extension (used from GEOMETRICS seismographs) data_OYO_fixed.dat (201.0 KB)
Both are SEG2 file format about the same data but with different .extension, the case is that obspy is able to read .dat extension only, and the other one gave me an error. Thank a lot.
It looks like the free form header written by that instrument does not follow SEG2 conventions (although I don’t know much about SEG2, only know that it’s definition is ugly and flexible to an extent that makes it hard to deal with).
The binary content of the free form header of the valid file looks like this:
Looking at our source code it is a list of items each comprised of one unsigned integer (2 bytes length) followed by “field_name value” of varying size given by the leading number.
The file that can not be read looks different in terms of free form header:
It basically seems to have some garbage in front and also each individual header value is not preceded by its length in bytes, solely relying on a single NULL character to represent the end of one key/value string.
It looks like it might be possible to read that file from the looks of it, but you would have to probably patch the obspy source code to get it done, replacing or extending the part that reads the free form header (function parse_free_form(...) in obspy/io/seg2/seg2.py). You could set a debugger break point in there and do it interactively for a start.
dangerous, as you would get wrong data if you read a file that has a different sampling rate, and…
you will lose all the other free form header info (although it seems like in the “broken” file all the header variables are either 0 or 1 so they might just be bogus values anyway maybe)
Dear Mr. Megies, Than you so much for you reply. I understood the main idea of what you said, but since I’m too inexperienced (I’m not a developer) working with the source code of modules and libraries and also understanding binary code, I’m afraid I could damage my data or even my system.
Just to mention, I found a way to convert .sg2 files in .dat files, by using a software (I’ not mentioning the name of that software, cause maybe it can be illegal or considered promotion for the policy of this community; of course, I’m sure there exist a lot of softwares that convert files). Then, with that conversion, I can succesfully use Obspy and proceed with my workflow.
As I mentioned before, for me, the ideal would be that Obspy could read directly .sg2 files or to build a little Python code that converts .sg2 files into .dat files, in order to automate the procces, instead of using a software and converting the files one by one.
Next year, I expect my university reopen in a normally way so I can work with developer parties. And, I will be happy to share my findings.
Glad you found a solution, and we have no problem at all if you mention what software you converted your files with.
Like I said , it would totally be possible to modify our source code to read those files, but first we would need somebody more familiar and/or working with SEG2 give a statement how much of a format breach we are looking at and how to judge the situation.
Well that hard coded hack is really only if you are sure about the sampling rate a-priori, obviously. It wouldnt be hard to read, just would like some input from people familliar with the SEG2 file format definition how to judge this different header structure.
There must be a variety of programs, I used this one: https://www.geogiga.com/en/frontendexpress.php
The downside is that with this program, from my knowledge, you have to convert one by one, no automatization. But maybe for your task is valuable, greetings.
Thank you for your prompt reply😊
It really helped me. Yeah, frontend doesn’t seem to have an automatic function. I have 1000 shots, I guess I need to start working on it.