Reading mseed file with bad timestamp

Hi

Unfortunately there seems to be a bug in the software of one of our digitizer that resulted in a timestamp 2025-366 (year-julday) for the the first day of this year. Trying to read this file using obspy fails with the error

...
 File "/usr/local/lib/python3.9/dist-packages/obspy/core/utcdatetime.py", line 432, in __init__
    raise ValueError(msg)
ValueError: 'julday' out of bounds for year 2025: 366

Which of course is correct behaviour. Question though is if it is possible somehow then, using obspy, to read in the file and correct the date (we have no reasons to believe that the timing is bad, only the date seems erroneous) so that the corrected file can be written.

If, not, info on what other software to use will be appreciated.

you could probably edit the io.mseed code to intercept that julian day and modifiy it, e.g. somewhere like:

That’s an alternative but I was sort of hopping that there was a quick way out of this.

But looking at the peace of code you cite, isn’t that a bug we are looking at (the if condition at line 672) since for leap years 366 is a valid day number every leap year?

It looks pretty quick to me, just add a condition there and do -1 for your wrong data.

Regarding your question, I’m not getting what you mean?

In line 672:

if not (1 <= values[1] <= 366):

Would trigger e.g. for december 31 2024 which has day number 366, wouldn’t it?

euh ? no ? if value[1] = 366 will make the check be True and it’ll pass ?

Hmm, yes, just forget about this walk astray, think the ski-trip earlier today consumed most oxygen in my grey sludge…

@cpaitor it is relatively simple to fix fixed headers in MSEED files, so you could “repair” your file manually.

e.g.

from struct import unpack
data = open("my_file.mseed", "rb"), read()
print(unpack('>2H', data[20:24]))

Now what you can do is go through your file and put back together the byte data as is and just replace those 2x2 bytes that encode year and day-of-year with the fixed ones and write back to a repaired file.

Usually MSEED files will be quite well behaved, so just hopping around with record length (512, 2048, …) should be fine and for every record you could add a quick check by just doing a obspy.read(io.BytesIO(data[x:x+record_length]), 'MSEED') and see if it makes sense.