moon-grib

    GRIB2 archive decoder and geographic window query library

    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    31 minutes ago
    Downloads
    12

    No dependencies

    #MoonGRIB

    MoonGRIB 是用 MoonBit 编写的 GRIB2 库和命令行工具。它按变量、层次与时次查看文件内容,并从气象网格中提取指定区域,输出坐标、数值和缺测标记。

    #快速开始

    需要 MoonBit 工具链。JavaScript 命令行入口需要 Node.js。字段编号、行号和列号从 0 开始。

    moon test --deny-warn moon test --target js --deny-warn moon run cmd/main --target js -- range-inventory sample.grib2 moon run cmd/main --target js -- range-window sample.grib2 0 119.5 29.7 120.6 30.7 moon run cmd/main --target js -- estimate sample.grib2 0 30.2 120.1 bilinear moon run cmd/main --target js -- raster sample.grib2 0 119.5 29.7 120.6 30.7 asc

    窗口参数依次为西、南、东、北,包含端点。西界大于东界表示跨日期变更线。经度输出范围为 [-180, 180)。窗口输出 CSV,range-window 另在标准错误流输出读取次数、字节数和解码数。栅格可导出 ASCII Grid 或 PGM,ASCII Grid 需要等间距的方形单元。

    #功能和格式范围

    能力入口或范围
    文件和消息open_archive 从内存打开,open_indexed 按需读取。支持连续消息、消息内多字段、可选本地节和先前位图引用
    网格规则经纬网格 3.0、规则高斯网格 3.40,支持正反方向、行列优先、交替扫描和缺测位图。缩减网格和交错网格超出当前范围
    内存读取简单打包 5.0、通用组拆分 5.2、一阶及二阶空间差分 5.3,以及单精度或双精度 IEEE 5.4
    读取文件范围range-window 按需读取 5.0 和 5.4 的偏移区间,range-inventory 仅读取消息结构和元数据
    查询和比较point、stats、estimate、profile、compare。支持最近邻、双线性插值和缺测策略
    时间轴支持瞬时产品的有效时刻、点位和区域时间序列,interval 展示模板 4.8 的区间与统计描述
    导出和编码窗口 CSV、JSON Lines、ASCII Grid、PGM。MoonBit API 可编码规则经纬网格的简单整数打包或 IEEE64

    库入口 open_indexed 接受 ByteSource 回调,可以从本地文件或其他支持定位偏移的数据源读取。范围查询合并相邻字节段,位图存在时先构建缺测值索引。空间差分采用预处理与检查点恢复,适用于内存读取路径。CLI 支持的具体命令可直接运行 moon run cmd/main --target js 查看。

    #验证与性能

    Python 对照脚本使用 ECMWF ecCodes 和 NumPy。

    moon build --target js python examples/verify_eccodes.py python -B examples/benchmark_window.py

    逐点验证结果覆盖四种数据表示和两种网格。 窗口基准使用一个由 16 条消息组成的 11,522,864 字节文件。每次对照检查三种命令的 CSV 输出完全一致,墙钟时间采用五次独立 CLI 启动的中位数。

    窗口格点数整场解码后裁剪内存内窗口解码按范围读取范围读取字节
    小窗口121191.16 ms113.66 ms112.43 ms1152
    大窗口40401238.30 ms182.57 ms202.60 ms81712

    范围读取字节包含从文件读取的消息索引、字段元数据和负载,计时包含 Node.js 启动和 CSV 序列化。大窗口在本机上的范围读取耗时高于内存内窗口路径。

    格式说明见 ECMWF GRIB2 模板 与 ecCodes 文档。许可证为 Apache-2.0。

    #Reproducible forecast example

    moon build --target js python -B examples/demo.py ./forecast-demo python -B examples/verify_encoded.py

    The demo generates two GRIB2 fields for the same forecast, lists the fields, extracts a 3-by-2 region and writes a grayscale PGM preview. It checks the inventory, the missing cell and the image dimensions. The output directory also includes a CSV file and the input GRIB2. The separate ecCodes interoperability script checks both encoded messages, including the date rollover, grid coordinates, packed values and bitmap.

    GribError

    pub(all) suberror GribError {
    Invalid(Int, String)
    Unsupported(Int, String)
    }

    AggregationInterval

    pub(all) struct AggregationInterval {
    start : ReferenceTime?
    end : ReferenceTime
    missing_inputs : UInt64?
    ranges : Array[StatisticalRange]
    }

    Archive

    pub struct Archive {
    bytes : Bytes
    fields : Array[Field]
    }

    Archive::aggregation_interval

    fn Archive::aggregation_interval(self : Archive, index : Int) -> AggregationInterval? raise GribError

    Archive::decode

    fn Archive::decode(self : Archive, index : Int) -> DecodedField raise GribError

    Archive::field

    fn Archive::field(self : Archive, index : Int) -> Field raise GribError

    Archive::field_count

    fn Archive::field_count(self : Archive) -> Int

    Archive::grid

    fn Archive::grid(self : Archive, index : Int) -> Grid raise GribError

    Archive::metadata

    fn Archive::metadata(self : Archive, index : Int) -> Metadata raise GribError

    Archive::point_series

    fn Archive::point_series(self : Archive, filter : Filter, pos : Position, start : ReferenceTime, end : ReferenceTime, options? : SeriesOptions) -> Array[SeriesPoint] raise GribError

    Archive::regional_series

    fn Archive::regional_series(self : Archive, filter : Filter, bounds : Bounds, start : ReferenceTime, end : ReferenceTime, max_fields? : Int, max_points? : Int) -> Array[RegionalSeriesPoint] raise GribError

    Archive::select

    fn Archive::select(self : Archive, filter : Filter) -> Array[Metadata] raise GribError

    Archive::select_valid_time

    fn Archive::select_valid_time(self : Archive, filter : Filter, start : ReferenceTime, end : ReferenceTime) -> Array[Metadata] raise GribError

    Archive::validate

    fn Archive::validate(self : Archive, decode_values? : Bool) -> Array[FieldDiagnostic]

    AreaStatistics

    pub(all) struct AreaStatistics {
    points : Int
    present : Int
    missing : Int
    footprint_steradians : Double
    covered_steradians : Double
    weighted_mean : Double?
    }

    Bounds

    pub(all) struct Bounds {
    west : Double
    south : Double
    east : Double
    north : Double
    }

    ByteSource

    pub struct ByteSource {
    length : Int
    // private fields
    }

    ByteSource::new

    fn ByteSource::new(length : Int, read : (Int, Int) -> Bytes raise GribError) -> ByteSource raise GribError

    ByteSource::read_exact

    fn ByteSource::read_exact(self : ByteSource, start : Int, length : Int) -> Bytes raise GribError

    ByteSource::read_span

    fn ByteSource::read_span(self : ByteSource, span : Span) -> Bytes raise GribError

    DecodedField

    pub struct DecodedField {
    bytes : Bytes
    grid : Grid
    payload : Int
    // private fields
    }

    DecodedField::area_statistics

    fn DecodedField::area_statistics(self : DecodedField, bounds : Bounds, max_points? : Int) -> AreaStatistics raise GribError

    DecodedField::at

    fn DecodedField::at(self : DecodedField, row : Int, column : Int) -> Sample raise GribError

    DecodedField::compare_window

    fn DecodedField::compare_window(self : DecodedField, other : DecodedField, bounds : Bounds, tolerance? : Double, max_points? : Int) -> WindowDifference raise GribError

    DecodedField::dimensions

    fn DecodedField::dimensions(self : DecodedField) -> (Int, Int)

    DecodedField::estimate

    fn DecodedField::estimate(self : DecodedField, position : Position, interpolation? : InterpolationMethod, missing? : MissingPolicy) -> EstimatedValue raise GribError

    DecodedField::profile

    fn DecodedField::profile(self : DecodedField, from : Position, to : Position, points : Int, interpolation? : InterpolationMethod, missing? : MissingPolicy) -> Array[EstimatedValue] raise GribError

    DecodedField::raster

    fn DecodedField::raster(self : DecodedField, bounds : Bounds, max_points? : Int) -> Raster raise GribError

    DecodedField::resample

    fn DecodedField::resample(self : DecodedField, target : TargetGrid, interpolation? : InterpolationMethod, missing? : MissingPolicy, max_points? : Int) -> ResamplingResult raise GribError

    DecodedField::scan_window

    fn DecodedField::scan_window(self : DecodedField, bounds : Bounds, max_points? : Int, scan_limit? : Int) -> Array[Sample] raise GribError

    Reference implementation for range-query comparisons. It decodes every grid cell in logical row order and retains only selected cells.

    DecodedField::statistics

    fn DecodedField::statistics(self : DecodedField, bounds : Bounds, max_points? : Int) -> Statistics raise GribError

    DecodedField::visit_window

    fn DecodedField::visit_window(self : DecodedField, bounds : Bounds, emit : (Sample) -> Unit, max_points? : Int) -> Unit raise GribError

    DecodedField::window

    fn DecodedField::window(self : DecodedField, bounds : Bounds, max_points? : Int) -> Array[Sample] raise GribError

    EstimatedValue

    pub(all) struct EstimatedValue {
    position : Position
    value : Double?
    contributing_points : Int
    }

    Field

    pub(all) struct Field {
    discipline : Int
    message : Span
    identification : Span
    grid_definition : Span
    product_definition : Span
    representation : Span
    bitmap : Span
    data : Span
    bitmap_source : Span?
    }

    FieldDiagnostic

    pub(all) struct FieldDiagnostic {
    index : Int
    result : FieldStatus
    }

    FieldStatus

    pub(all) enum FieldStatus {
    Ready
    Malformed(Int, String)
    UnsupportedField(Int, String)
    }

    Filter

    pub(all) struct Filter {
    discipline : Int?
    category : Int?
    parameter : Int?
    centre : Int?
    surface_kind : Int?
    forecast_unit : Int?
    forecast_offset : Int?
    reference_time : String?
    }

    Filter::empty

    fn Filter::empty() -> Filter

    Filter::matches

    fn Filter::matches(self : Filter, value : Metadata) -> Bool

    ForecastTime

    pub(all) struct ForecastTime {
    reference : ReferenceTime
    valid : ReferenceTime
    offset_seconds : Int64
    }

    Grid

    pub struct Grid {
    columns : Int
    rows : Int
    points : Int
    first_latitude : Double
    first_longitude : Double
    latitude_step : Double
    longitude_step : Double
    column_major : Bool
    alternating : Bool
    // private fields
    }

    Grid::latitude

    fn Grid::latitude(self : Grid, row : Int) -> Double

    Grid::longitude

    fn Grid::longitude(self : Grid, column : Int) -> Double

    IndexedArchive

    pub struct IndexedArchive {
    // private fields
    }

    IndexedArchive::decode

    fn IndexedArchive::decode(self : IndexedArchive, index : Int) -> RangeField raise GribError

    IndexedArchive::field

    fn IndexedArchive::field(self : IndexedArchive, index : Int) -> Field raise GribError

    IndexedArchive::field_count

    fn IndexedArchive::field_count(self : IndexedArchive) -> Int

    IndexedArchive::metadata

    fn IndexedArchive::metadata(self : IndexedArchive, index : Int) -> Metadata raise GribError

    Read only the header prefix required by the public field inventory.

    IndexedArchive::raw_message

    fn IndexedArchive::raw_message(self : IndexedArchive, index : Int, max_bytes? : Int) -> Bytes raise GribError

    IndexedArchive::select

    fn IndexedArchive::select(self : IndexedArchive, filter : Filter) -> Array[Metadata] raise GribError

    IntegerPackingSpec

    pub(all) struct IntegerPackingSpec {
    binary_scale : Int
    decimal_scale : Int
    }

    InterpolationMethod

    pub(all) enum InterpolationMethod {
    Nearest
    Bilinear
    }

    Metadata

    pub(all) struct Metadata {
    field_index : Int
    origin : ReferenceTime
    centre : Int
    subcentre : Int
    variable : Variable
    product_template : Int
    grid_template : Int
    packing_template : Int
    forecast_unit : Int?
    forecast_offset : Int?
    first_surface : Surface?
    second_surface : Surface?
    message : Span
    }

    Metadata::json

    Metadata::valid_time

    fn Metadata::valid_time(self : Metadata) -> ForecastTime? raise GribError

    MissingPolicy

    pub(all) enum MissingPolicy {
    RequireAll
    Renormalize
    }

    Position

    pub(all) struct Position {
    latitude : Double
    longitude : Double
    }

    ProductSpec

    pub(all) struct ProductSpec {
    variable : Variable
    reference : ReferenceTime
    centre : Int
    subcentre : Int
    forecast_unit : Int
    forecast_offset : Int
    level_kind : Int
    level_value : Int?
    }

    RangeField

    pub struct RangeField {
    grid : Grid
    // private fields
    }

    RangeField::window

    fn RangeField::window(self : RangeField, bounds : Bounds, options? : RangeOptions) -> ReadWindow raise GribError

    RangeOptions

    pub(all) struct RangeOptions {
    max_points : Int
    merge_gap : Int
    max_read : Int
    }

    RangeOptions::standard

    fn RangeOptions::standard() -> RangeOptions

    Raster

    pub struct Raster {
    rows : Int
    columns : Int
    north : Double
    west : Double
    latitude_spacing : Double
    longitude_spacing : Double
    // private fields
    }

    Raster::ascii_grid

    fn Raster::ascii_grid(self : Raster, missing_value? : Double) -> String raise GribError

    Raster::latitude

    fn Raster::latitude(self : Raster, row : Int) -> Double

    Raster::longitude

    fn Raster::longitude(self : Raster, column : Int) -> Double

    Raster::pgm

    fn Raster::pgm(self : Raster) -> String

    Raster::value

    fn Raster::value(self : Raster, row : Int, column : Int) -> Double? raise GribError

    ReadWindow

    pub(all) struct ReadWindow {
    samples : Array[Sample]
    payload_reads : Int
    payload_bytes : Int64
    decoded_values : Int
    }

    ReadWindow::metrics_json

    ReferenceTime

    pub(all) struct ReferenceTime {
    year : Int
    month : Int
    day : Int
    hour : Int
    minute : Int
    second : Int
    }

    ReferenceTime::iso

    fn ReferenceTime::iso(self : ReferenceTime) -> String

    RegionalSeriesPoint

    pub(all) struct RegionalSeriesPoint {
    field_index : Int
    time : ForecastTime
    statistics : Statistics
    }

    RegularGridSpec

    pub(all) struct RegularGridSpec {
    columns : Int
    rows : Int
    latitude_microdegrees : Int
    longitude_microdegrees : Int
    i_increment_microdegrees : Int
    j_increment_microdegrees : Int
    }

    ResamplingResult

    pub(all) struct ResamplingResult {
    raster : Raster
    present : Int
    missing : Int
    outside_or_unsampled : Int
    }

    ResamplingResult::metrics_json

    Sample

    pub(all) struct Sample {
    row : Int
    column : Int
    latitude : Double
    longitude : Double
    value : Double?
    }

    Sample::json

    SeriesOptions

    pub(all) struct SeriesOptions {
    interpolation : InterpolationMethod
    missing : MissingPolicy
    max_fields : Int
    }

    SeriesOptions::standard

    fn SeriesOptions::standard() -> SeriesOptions

    SeriesPoint

    pub(all) struct SeriesPoint {
    field_index : Int
    time : ForecastTime
    location : EstimatedValue
    }

    SeriesPoint::json

    Span

    pub(all) struct Span {
    offset : Int
    length : Int
    }

    StatisticalRange

    pub(all) struct StatisticalRange {
    process : Int
    increment_type : Int
    range_unit : Int
    range_length : UInt64?
    increment_unit : Int
    increment : UInt64?
    }

    Statistics

    pub(all) struct Statistics {
    points : Int
    present : Int
    missing : Int
    minimum : Double?
    maximum : Double?
    mean : Double?
    population_variance : Double?
    }

    Statistics::json

    Surface

    pub(all) struct Surface {
    kind : Int
    value : Double?
    }

    TargetGrid

    pub(all) struct TargetGrid {
    rows : Int
    columns : Int
    north : Double
    west : Double
    latitude_spacing : Double
    longitude_spacing : Double
    }

    Variable

    pub(all) struct Variable {
    discipline : Int
    category : Int
    parameter : Int
    }

    WindowDifference

    pub(all) struct WindowDifference {
    evaluated : Int
    paired : Int
    both_missing : Int
    left_only : Int
    right_only : Int
    above_tolerance : Int
    mean_delta : Double?
    mean_absolute_error : Double?
    maximum_absolute_error : Double?
    }

    encode_ieee

    fn encode_ieee(grid : RegularGridSpec, product : ProductSpec, values : Array[Double?]) -> Bytes raise GribError

    encode_simple

    fn encode_simple(grid : RegularGridSpec, product : ProductSpec, scales : IntegerPackingSpec, values : Array[UInt?]) -> Bytes raise GribError

    open_archive

    fn open_archive(bytes : Bytes) -> Archive raise GribError

    open_indexed

    fn open_indexed(source : ByteSource) -> IndexedArchive raise GribError

    open_range_field

    fn open_range_field(source : ByteSource, field : Field) -> RangeField raise GribError

    samples_csv

    fn samples_csv(samples : Array[Sample]) -> String

    scan_source

    fn scan_source(source : ByteSource) -> Array[Field] raise GribError