steimpack

    Steim codecs, numeric miniSEED waveform I/O and archive queries

    Download zip
    Version
    0.4.0
    License
    MIT
    Last updated
    3 hours ago
    Downloads
    45

    No dependencies

    #SteimPack

    SteimPack 用 MoonBit 读取 miniSEED 2 和 3 波形档案、写出 miniSEED 3,支持 Steim 压缩、整数和 IEEE 浮点采样。可按台站和 UTC 时段提取采样,导出带时间戳的 CSV,核对档案补传后的覆盖范围,或将旧记录保留原编码迁移为 miniSEED 3。Windows 和 Linux 用户均可直接下载原生校验器检查文件和管道。

    #在 MoonBit 中使用

    moon add kent131452777/steimpack

    在使用该库的包的 moon.pkg 中导入所需的接口。

    import {
    "kent131452777/steimpack" @wave,
    "kent131452777/steimpack/codec",
    "kent131452777/steimpack/mseed",
    "kent131452777/steimpack/timeline",
    }

    @mseed.parse(bytes) 读取一条记录并核对 miniSEED 3 的 CRC-32C。@wave.samples(record) 返回整数采样;对包含整数和浮点数的档案,可用 @wave.samples_f64(record) 统一读取,或用 @wave.samples_f64_range(record, first, end) 只保留指定范围。@wave.verify_samples(record) 检查整个波形,无需保存解码数组。@wave.migrate_record(record) 将一条数值记录转换成一条 miniSEED 3 记录。跨午夜读写示例 · MoonCakes API

    moon run examples/records --target js

    #用命令行处理波形

    从发布页下载 steimpack.cjs,用 Node.js 运行。下列命令使用仓库内的 Albuquerque 台站五分钟样本。可以把同名路径替换成自己的文件。下载 CLI · 台站样本

    检查档案。 每条记录输出一行 JSON,包括文件偏移、长度、源标识、纳秒级 UTC 起点、编码和采样数。适合定位时间间断或无法读取的记录。

    node steimpack.cjs inspect tests/data/station.mseed -o records.jsonl

    分析多台站档案。 下面把同一时段的 Albuquerque 和 College 两个台站放在一份档案里。第一条分析命令列出每个来源的间断、重叠、采样率变化和无法判断的记录。第二条只导出 College 的 6,000 个采样。多个 --sid 可以一起使用,保留记录在档案里的顺序。College 台站样本

    cat tests/data/station.mseed tests/data/cola.mseed > stations.mseed node steimpack.cjs timeline stations.mseed node steimpack.cjs decode stations.mseed --sid FDSN:IU_COLA_00_B_H_Z -o cola.i32

    在 Windows 命令提示符中,可以用下面的二进制复制命令生成同一份输入文件。后续 node 命令也可直接在 PowerShell 运行,-o 会按原始字节写出结果。

    copy /b tests\data\station.mseed+tests\data\cola.mseed stations.mseed

    样本中 College 有三处 1 微秒的时间偏差。--tolerance-ns 1000 可忽略含边界在内的这类偏差。事件给出源标识、当前记录的 UTC 起点与原始字节偏移。间断和重叠的 deltaNs 分别为正数和负数,以十进制字符串保留精度。最后一行汇总本次扫描。

    提取事件窗口。 下列命令从 Albuquerque 通道截取 06:32:00 至 06:32:10 UTC 的 200 个采样。起点包含在内,终点排除。decode 可直接输出供 NumPy 使用的小端 int32。select 复制含有任一命中采样的整条原始记录,保留字节序、校验和及附加信息,也适用于浮点波形。两条命令都支持只给一个时间边界。

    node steimpack.cjs decode tests/data/station.mseed --from 2010-02-27T06:32:00Z --until 2010-02-27T06:32:10Z -o event.i32 node steimpack.cjs select tests/data/station.mseed --from 2010-02-27T06:32:00Z --until 2010-02-27T06:32:10Z -o original-records.mseed

    盘点回补后的覆盖时段。 availability 读完整份档案后按来源和时间输出区间。重复记录只计一次,晚到的记录可以填平缺口。College 样本在 1 微秒容差下合为一个区间,实际覆盖 299.999999 秒,合并时跨过的两个缺口共计 2 微秒。程序分别写入 coveredNs 和 bridgedNs。

    node steimpack.cjs availability tests/data/cola.mseed --tolerance-ns 1000

    导出带时间的采样表。 CSV 的三列是 source_id,time_utc,value。同一张表可包含多个通道,时间精确到纳秒,采样值保留档案自身的单位。下例把 10 秒钟的 200 行采样交给电子表格或绘图程序,还可用 f64le 将整段整数或浮点波形读入 NumPy。

    node steimpack.cjs decode tests/data/station.mseed --from 2010-02-27T06:32:00Z --until 2010-02-27T06:32:10Z --format csv -o event.csv node steimpack.cjs decode tests/data/station.mseed --format f64le -o station.f64 python -c "import numpy as np; print(np.fromfile('station.f64', dtype='<f8').shape)"

    迁移或重新编码。 repack --encoding preserve 每读入一条数值记录就写出一条 miniSEED 3 记录,保留原编码、采样位模式和时间边界,包括浮点 NaN 的原始位和负零。对已有整数档案也可显式选择 Steim-1 或 Steim-2 重新压缩。从连续的原始整数采样开始时,提供台站标识、UTC 起点与采样率。

    node steimpack.cjs repack tests/data/station.mseed --encoding preserve -o station-v3.mseed node steimpack.cjs decode tests/data/station.mseed -o station.i32 node steimpack.cjs encode station.i32 -o continuous.mseed --sid FDSN:IU_ANMO_00_B_H_Z --start 2010-02-27T06:30:00.019538Z --rate 20 --encoding steim1

    重新压缩时,--frames N 限制单条输出记录的 64 字节帧数,默认 63。省略文件名或使用 - 可读写标准流。输出到文件时,整个操作成功后才替换目标文件。写到标准输出时,出错前已输出的记录可能已被下游读取。

    #用原生程序校验档案

    Linux 版提供 x86-64 和 ARM64 原生可执行文件,在 Ubuntu 22.04 上构建,使用系统的 glibc。以下命令按当前 CPU 选择压缩包并核对 SHA-256。下载与校验和

    base=https://github.com/kent131452777/steimpack/releases/download/v0.4.0 archive="steimcheck-linux-$(uname -m).tar.gz" curl -fsSLO "$base/$archive" curl -fsSLO "$base/SHA256SUMS" sha256sum --check --ignore-missing SHA256SUMS && tar -xzf "$archive" ./steimcheck tests/data/station.mseed

    Windows x64 版可在 PowerShell 中下载、校验和运行。校验器使用操作系统自带组件,支持包含中文或空格的路径。

    $base = 'https://github.com/kent131452777/steimpack/releases/download/v0.4.0' $name = 'steimcheck-windows-x86_64.zip' Invoke-WebRequest "$base/$name" -OutFile $name Invoke-WebRequest "$base/SHA256SUMS" -OutFile SHA256SUMS $hex = (Get-FileHash $name -Algorithm SHA256).Hash.ToLowerInvariant() if ((Get-Content SHA256SUMS) -cnotcontains "$hex $name") { throw 'SHA-256 mismatch' } Expand-Archive $name -DestinationPath steimcheck-win .\steimcheck-win\steimcheck.exe tests\data\station.mseed

    这份样本的结果是 {"records":15,"samples":6000,"bytes":7680,"mode":"waveform"}。也可以将二进制记录送入程序的标准输入。默认检查整数与浮点载荷长度,以及 Steim 压缩波形的末样本 Xn。遇到损坏记录时,程序以非零状态退出,并在标准错误报告字节位置。对其他编码可用 --headers-only 检查记录结构和 miniSEED 3 校验和。

    #格式与验证

    能力范围
    miniSEED 2 输入128 字节至 16 MiB 的记录,独立识别头部和数据字节序,应用时间修正。迁移 200/201 事件检测、300/310/320/390/395 校准和 500 时钟异常信息
    miniSEED 3 输入、输出CRC-32C、UTC 纳秒、采样率或周期、标志、发布版本、JSON 扩展头
    波形读取Steim-1、Steim-2、16/32 位有符号 PCM、IEEE binary32 和 binary64
    波形写入保留六种数值编码迁移到 miniSEED 3,或将整数采样压缩为 Steim-1、Steim-2
    连续性检查不同来源分别跟踪,支持重复与乱序记录、采样率或周期变化、纳秒容差
    兼容性验证FDSN 官方样本、传统字节序组合、三个台站的波形或校准记录,以及 Steim-1、Steim-2 各一百万点的独立解码

    WaveformError

    pub(all) suberror WaveformError {
    InvalidWaveform(String)
    }

    SampleTime

    pub(all) struct SampleTime {
    year : Int
    day : Int
    hour : Int
    minute : Int
    second : Int
    nanosecond : Int
    } derive(Compare, Eq)

    UTC start time with a one-based ordinal day and a nanosecond fraction.

    SampleTime::from_header

    Read the record start without changing its civil-time fields.

    SampleTime::validate

    fn SampleTime::validate(self : SampleTime) -> Unit raise WaveformError

    Check the Gregorian ordinal date, UTC clock fields and nanosecond fraction.

    TimeWindow

    type TimeWindow

    A half-open interval of UTC sample labels, with optional bounds.

    TimeWindow::contains_record

    fn TimeWindow::contains_record(self : TimeWindow, record :
    Record
    ) -> Bool raise WaveformError

    Test whether a parsed record has a selected sample or a selected header-only timestamp. With both bounds omitted, accept every parsed record, including opaque payloads with unspecified timing. For a known positive count, a matching start proves the record contains a sample.

    TimeWindow::indices

    fn TimeWindow::indices(self : TimeWindow, record :
    Record
    ) -> (Int, Int) raise WaveformError

    Return the zero-based half-open sample range of a parsed record. Header-only records have no sample indices. A bound after the last sample gives the insertion index at the end. Samples beyond year 9999 follow every valid bound. Sample order alone can establish a full or empty range. Other multi-sample ranges require a known rate and a start in years 1 through 9999.

    TimeWindow::new

    fn TimeWindow::new(from? : SampleTime, until? : SampleTime) -> TimeWindow raise WaveformError

    Validate the dates and order of the bounds. Equal bounds select nothing.

    advance_header

    Compute a new record start from the original stream header, keeping its metadata.

    migrate_record

    fn migrate_record(record :
    Record
    ) -> Bytes raise WaveformError

    Write one parsed miniSEED 2 or 3 record as miniSEED 3, preserving its numeric encoding, sample bits and translated header. Steim data is checked through Xn in its original packing. A legacy header without data is written with zero samples, encoding and rate.

    pack_record

    fn pack_record(header :
    Header
    , encoding :
    Encoding
    , input : ArrayView[Int], max_frames~ : Int) -> (Bytes, Int) raise WaveformError

    Encode the largest prefix that fits a miniSEED 3 record. A positive-length input always consumes at least one sample. Resume at the returned count, using the original header and cumulative sample index to calculate the start.

    sample_duration_ns

    fn sample_duration_ns(rate : Double, offset : Int64) ->
    BigInt
    raise WaveformError

    Exact physical duration of a nonnegative number of sample intervals, rounded to the nearest nanosecond. Positive rates are hertz and negative rates are seconds per sample. The duration is independent of civil dates.

    samples

    Decode one integer waveform record. Both byte orders are accepted for legacy input. Header-only records yield an empty series.

    samples_f64

    fn samples_f64(record :
    Record
    ) -> Array[Double] raise WaveformError

    Read signed PCM, Steim, or IEEE binary32/binary64 samples as doubles. Integers and finite binary32 values widen exactly. Floating-point samples retain signed zero, infinities and NaN.

    samples_f64_range

    fn samples_f64_range(record :
    Record
    , start : Int, end : Int) -> Array[Double] raise WaveformError

    Read indices in [start, end) as doubles. The entire PCM extent or Steim integration through Xn is checked, including for an empty range.

    samples_range

    fn samples_range(record :
    Record
    , start : Int, end : Int) -> Array[Int] raise WaveformError

    Decode only sample indices in [start, end). Check the entire PCM extent or integrate Steim differences through Xn, including for an empty range.

    time_at

    fn time_at(start : SampleTime, rate : Double, offset : Int64) -> SampleTime raise WaveformError

    Advance from an original record start by an absolute sample index. Positive rates are hertz, negative rates are seconds per sample. The result is rounded to the nearest nanosecond. An explicitly encoded leap second is preserved.

    verify_samples

    fn verify_samples(record :
    Record
    ) -> Unit raise WaveformError

    Check a numeric waveform without storing decoded samples. Integer and float PCM are checked against the declared count. Steim is integrated through Xn.