Skip to content
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

CMTSOLUTION parsing does not interpret dates correctly (issue with formatting/whitespace handling) #3270

Open
1 task done
amkearns-usgs opened this issue Feb 28, 2023 · 5 comments
Labels
bug-unconfirmed reported bug that still needs to be confirmed

Comments

@amkearns-usgs
Copy link

Avoid duplicates

  • I searched existing issues

Bug Summary

Reading in some CMTSOLUTION files in obspy produces errors where the full date is not parsed in correctly. This appears to be an issue with how obspy expects the first CMTSOLUTION file's line to have its whitespace formatted, leading it to miss the first digit of the year in certain cases, and thus leading to, ex., 2022 only having 022 parsed in, thus claiming the correct year is 22.

An example of a CMTSOLUTION first line that produces this issue is this one:
PDE 2022 12 04 19 24 15.20 -15.40 -172.97 36.0 0.0 6.7
The problem appears to originate from the first field, which here is 3 characters, as the code in obspy appears to expect a 4-character value only, per the first few lines of _internal_read_single_cmtsolution:

def _internal_read_single_cmtsolution(buf):
"""
Reads a single CMTSOLUTION file to a :class:`~obspy.core.event.Catalog`
object.
:param buf: File to read.
:type buf: open file or file-like object
"""
# The first line encodes the preliminary epicenter.
line = buf.readline()
hypocenter_catalog = line[:5].strip().decode()
origin_time = line[5:].strip().split()[:6]

I'm willing to fix this issue myself, as it should be a simple task, mainly rewriting that method to perform the string split() method before doing anything else on the line, and then dealing with each of the fields as necessary.

Code to Reproduce

No response

Error Traceback

No response

ObsPy Version?

1.4.0

Operating System?

tested on Ubuntu and MacOS (but should be an issue in any version)

Python Version?

3.9.16

Installation Method?

conda

@amkearns-usgs amkearns-usgs added the bug-unconfirmed reported bug that still needs to be confirmed label Feb 28, 2023
@aringler-usgs
Copy link
Member

Here is an example for @amkearns-usgs bug.

import obspy
cat = obspy.read_events("CMTSOLUTION")[0]
print(cat)

Here is the relevant output:
Event: 0021-01-03T12:38:52.070000Z | +51.120, -179.780 | 6.04 mw

Here is the file: (CMTSOLUTION.TXT)

@QuLogic
Copy link
Member

QuLogic commented Mar 1, 2023

The format is fixed field width, not space separated, as defined in https://www.ldeo.columbia.edu/~gcmt/projects/CMT/catalog/allorder.ndk_explained Unless there is some new standard, those files are invalid, and ObsPy is parsing it "correctly" (or rather, as best it can.)

@aringler-usgs
Copy link
Member

@QuLogic Thanks for your help. The link appears to be for ndk format and not CMTSOLUTION format (CMTSOLUTION format is the one we are having issues with). Or are we missing something?

@QuLogic
Copy link
Member

QuLogic commented Mar 1, 2023

That's true. Looking back at the history, there's not much of a formal definition and that's really the only reference we have. We can't just split on space as there are files with the first field that aligns up to the next field: #1479.

Our only other reference is SPECFEM3D, and it looks like while they previously used a fixed field width, they've had to move to some complicated heuristic which I guess we'll have to replicate.
https://github.com/SPECFEM/specfem3d/blob/7ab2414052ea4f35752821a1e137ac94c9aa7b59/src/specfem3D/get_cmt.f90#L105-L115

@megies
Copy link
Member

megies commented Mar 15, 2023

they've had to move to some complicated heuristic which I guess we'll have to replicate.

🙈

I'm willing to fix this issue myself

Feel free to go ahead and make a PR 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-unconfirmed reported bug that still needs to be confirmed
Projects
None yet
Development

No branches or pull requests

4 participants