Skip to main content

A Python package for reading and writing miniSEED formatted data

Project description

mseedlib - a Python package to read and write miniSEED formatted data

The mseedlib package allows for reading and writing of miniSEED formatted data, which is commonly used for seismological and other geophysical time series data.

The module leverages the C-language libmseed for most of the heavy data format and manipulation work.

Installation

The releases should be installed directly from PyPI with, for example, pip install mseedlib. The package does not depend on anything other than the Python standard library.

Example usage

Working programs for a variety of use cases ca be found in the examples directory of the repository.

Read a file and print details from each record:

from mseedlib import MS3RecordReader,TimeFormat

input_file = 'testdata-3channel-signal.mseed3'

with MS3RecordReader(input_file) as msreader:
    for msr in msreader:
        # Print values directly
        print(f'   SourceID: {msr.sourceid}, record length {msr.reclen}')
        print(f' Start Time: {msr.starttime_str(timeformat=TimeFormat.ISOMONTHDAY_SPACE_Z)}')
        print(f'    Samples: {msr.samplecnt}')

        # Alternatively, use the library print function
        msr.print()

Read a file into a trace list and print the list:

from mseedlib import MSTraceList

input_file = 'testdata-3channel-signal.mseed3'

mstl = MSTraceList(input_file)

# Print the trace list using the library print function
mstl.print(details=1, gaps=True)

# Alternatively, traverse the data structures and print each trace ID and segment
for traceid in mstl.traceids():
    print(traceid)

    for segment in traceid.segments():
        print('  ', segment)

Writing miniSEED requires specifying a "record handler" function that is a callback to consume, and do whatever you want, with generated records.

Simple example of writing multiple channels of data:

import math
from mseedlib import MSTraceList, timestr2nstime

# Generate synthetic sinusoid data, starting at 0, 45, and 90 degrees
data0 = list(map(lambda x: int(math.sin(math.radians(x)) * 500), range(0, 500)))
data1 = list(map(lambda x: int(math.sin(math.radians(x)) * 500), range(45, 500 + 45)))
data2 = list(map(lambda x: int(math.sin(math.radians(x)) * 500), range(90, 500 + 90)))

mstl = MSTraceList()

sample_rate = 40.0
start_time = timestr2nstime("2024-01-01T15:13:55.123456789Z")
format_version = 2
record_length = 512

# Add synthetic data to the trace list
mstl.add_data(sourceid="FDSN:XX_TEST__B_S_0",
              data_samples=data0, sample_type='i',
              sample_rate=sample_rate, start_time=start_time)

mstl.add_data(sourceid="FDSN:XX_TEST__B_S_0",
              data_samples=data1, sample_type='i',
              sample_rate=sample_rate, start_time=start_time)

mstl.add_data(sourceid="FDSN:XX_TEST__B_S_0",
              data_samples=data2, sample_type='i',
              sample_rate=sample_rate, start_time=start_time)

# Record handler called for each generated record
def record_handler(record, handler_data):
    handler_data['fh'].write(record)

output_file = 'output.mseed'

with open(output_file, 'wb') as file_handle:
  # Generate miniSEED records
  mstl.pack(record_handler,
            {'fh':file_handle},
            flush_data=True)

Package design rationale

The package functionality and exposed API are designed to support the most common use cases of reading and writing miniSEED data using libmseed. Extensions of data handling beyond the functionality of the library are out-of-scope for this package. Furthermore, the naming of functions, classes, arguments, etc. follows the naming used in the library in order to reference their fundamentals at the C level if needed; even though this leaves some names distinctly non-Pythonic.

In a nutshell, the goal of this package is to provide just enough of a Python layer to libmseed to handle the most common cases of miniSEED data without needing to know any of the C-level details.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2024 Chad Trabant, EarthScope Data Services

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mseedlib-0.0.6.tar.gz (1.3 MB view hashes)

Uploaded Source

Built Distributions

mseedlib-0.0.6-py3-none-win_amd64.whl (294.8 kB view hashes)

Uploaded Python 3 Windows x86-64

mseedlib-0.0.6-py3-none-win32.whl (294.8 kB view hashes)

Uploaded Python 3 Windows x86

mseedlib-0.0.6-py3-none-musllinux_1_2_x86_64.whl (246.9 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ x86-64

mseedlib-0.0.6-py3-none-musllinux_1_2_i686.whl (285.2 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ i686

mseedlib-0.0.6-py3-none-musllinux_1_2_aarch64.whl (249.0 kB view hashes)

Uploaded Python 3 musllinux: musl 1.2+ ARM64

mseedlib-0.0.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (243.5 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ x86-64

mseedlib-0.0.6-py3-none-manylinux_2_17_i686.manylinux_2_12_i686.manylinux2010_i686.manylinux2014_i686.whl (276.1 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.12+ i686 manylinux: glibc 2.17+ i686

mseedlib-0.0.6-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (241.2 kB view hashes)

Uploaded Python 3 manylinux: glibc 2.17+ ARM64

mseedlib-0.0.6-py3-none-macosx_14_0_arm64.whl (209.9 kB view hashes)

Uploaded Python 3 macOS 14.0+ ARM64

mseedlib-0.0.6-py3-none-macosx_13_0_x86_64.whl (228.4 kB view hashes)

Uploaded Python 3 macOS 13.0+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page