MS3RecordBatchReader

class pymseed.MS3RecordBatchReader(batches, columns)[source]

Bases: object

Reads miniSEED records as MS3RecordBatch es; also an Arrow stream.

Create one with from_file(), from_buffer() or from_filelike(). Iterate over it for batches, or pass it to an Arrow library to read them all as one stream, e.g. pyarrow.table(reader), polars.DataFrame(reader) or a DuckDB query. Either way, each batch is read once: a stream continues from wherever iteration left off.

Parameters:
COLUMNS = ('sourceid', 'starttime', 'endtime', 'samprate', 'samplecnt', 'encoding', 'pubversion', 'formatversion', 'record')

The names of the columns a reader can read, in batch order.

classmethod from_buffer(buffer, *, columns=None, batch_size=8192, skip_not_data=False, validate_crc=True, verbose=0)[source]

Read batches of the records in a buffer.

Parameters:
  • buffer (Any) – A bytes-like object holding miniSEED records back to back.

  • columns (Iterable[Literal['sourceid', 'starttime', 'endtime', 'samprate', 'samplecnt', 'encoding', 'pubversion', 'formatversion', 'record']] | None) – The columns to read, from COLUMNS; all of them by default. Others are neither filled nor allocated, and without record, batches don’t keep the buffer alive.

  • batch_size (int) – Records per batch, at most, up to 2**30. A batch may hold fewer at the end of a block, or before input skipped with skip_not_data.

  • skip_not_data (bool) – Skip input that isn’t miniSEED, a byte at a time, until a record is found, including an incomplete record at the end and records that fail CRC validation or can’t be decoded.

  • validate_crc (bool) – Validate CRCs when present (miniSEED 3).

  • verbose (int) – libmseed verbosity.

Raises:

MiniSEEDError – While reading, if a record can’t be parsed, or the buffer ends partway through one, after yielding the records before it. Not raised for either with skip_not_data.

Return type:

MS3RecordBatchReader

classmethod from_filelike(fh, *, columns=None, block_size=8388608, batch_size=8192, skip_not_data=False, validate_crc=True, verbose=0)[source]

Read batches of the records read from a file-like object.

Reads block_size bytes at a time with fh.read. A batch never spans blocks: a record straddling two is read with the second, which is read in after a copy of the record with fh.readinto if fh has it, else copied after the record. With the record column, a batch references the block it was read from.

Parameters:
  • fh (Any) – An object with a read(n) method returning bytes. read must block until data is available, as an empty result ends the input.

  • block_size (int) – Bytes read at a time.

  • columns (Iterable[Literal['sourceid', 'starttime', 'endtime', 'samprate', 'samplecnt', 'encoding', 'pubversion', 'formatversion', 'record']] | None) – As for from_buffer().

  • batch_size (int) – As for from_buffer().

  • skip_not_data (bool) – As for from_buffer().

  • validate_crc (bool) – As for from_buffer().

  • verbose (int) – As for from_buffer().

Raises:

MiniSEEDError – As for from_buffer().

Return type:

MS3RecordBatchReader

classmethod from_file(filename, *, columns=None, block_size=8388608, batch_size=8192, skip_not_data=False, validate_crc=True, verbose=0)[source]

Read batches of the records in a file. See from_filelike().

The file is opened when reading starts, and closed when it ends or the reader is closed.

Parameters:
  • filename (str | PathLike[str])

  • columns (Iterable[Literal['sourceid', 'starttime', 'endtime', 'samprate', 'samplecnt', 'encoding', 'pubversion', 'formatversion', 'record']] | None)

  • block_size (int)

  • batch_size (int)

  • skip_not_data (bool)

  • validate_crc (bool)

  • verbose (int)

Return type:

MS3RecordBatchReader

close()[source]

Stop reading, closing a file opened by from_file().

Return type:

None

__arrow_c_stream__(requested_schema=None)[source]

Export the remaining batches as an arrow_array_stream PyCapsule.

The Arrow PyCapsule interface, through to_arrow(). The consumer pulls batches as it reads, each referencing the batch’s columns and records without copying them. A batch that fails to read ends the stream with an error. Requires pyarrow.

Parameters:

requested_schema (Any)

Return type:

Any

to_arrow()[source]

The remaining batches as a pyarrow.RecordBatchReader, read as it’s read.

Requires pyarrow 14 or later (the [pyarrow] optional dependency).

Return type:

pa.RecordBatchReader