grib2io.xarray_backend

grib2io Backend Engine for Xarray

grib2io provides a Xarray backend entrypoint for decoding many GRIB2 messages from a single file or many files and represented as Xarray DataArray objects and collected along common coordinates as Datasets and DataTrees.

The grib2io.xarray_backend engine API is experimental. Its interface and behavior may change in future releases, which could affect backward compatibility.

Users are encouraged to treat this backend as subject to change and to pin their grib2io version if depending on its current implementation details.

   1"""
   2grib2io Backend Engine for Xarray
   3=================================
   4grib2io provides a Xarray backend entrypoint for decoding many GRIB2 messages
   5from a single file or many files and represented as Xarray DataArray objects and
   6collected along common coordinates as Datasets and DataTrees.
   7
   8.. warning::
   9
  10   The ``grib2io.xarray_backend`` engine API is **experimental**.
  11   Its interface and behavior may change in future releases,
  12   which could affect backward compatibility.
  13
  14   Users are encouraged to treat this backend as subject to change
  15   and to pin their ``grib2io`` version if depending on its current
  16   implementation details.
  17"""
  18
  19from grib2io._grib2io import _data
  20from grib2io import Grib2Message, Grib2GridDef, msgs_from_index, tables, templates
  21from grib2io.utils.spatial import snap_to_nearest_cell_center
  22import grib2io
  23from xarray.backends.locks import SerializableLock
  24from xarray.core import indexing
  25from xarray.backends import (
  26    BackendArray,
  27    BackendEntrypoint,
  28)
  29from copy import copy
  30from dataclasses import dataclass, field, astuple
  31import datetime
  32import glob
  33import itertools
  34import json
  35import logging
  36import os
  37import re
  38import textwrap
  39import typing
  40from numpy.typing import NDArray
  41import warnings
  42
  43from . import tables
  44
  45import numpy as np
  46import pandas as pd
  47import xarray as xr
  48from pathlib import Path
  49from pyproj import CRS
  50
  51# Check if xarray version supports DataTree
  52_HAS_DATATREE = hasattr(xr, "DataTree")
  53
  54# Check for NumPy 2.0+ StringDType
  55_HAS_STRINGDTYPE = hasattr(np, "dtypes") and hasattr(np.dtypes, "StringDType")
  56
  57_logger = logging.getLogger(__name__)
  58
  59_DEFAULT_REMOTE_STORAGE_OPTIONS = {
  60    "config_kwargs": {
  61        "connect_timeout": 30,
  62        "read_timeout": 120,
  63        "retries": {"max_attempts": 10, "mode": "adaptive"},
  64    }
  65}
  66
  67
  68def _merge_default_storage_options(storage_options):
  69    """Merge user storage options over conservative remote defaults."""
  70    merged = {
  71        "config_kwargs": {
  72            "connect_timeout": _DEFAULT_REMOTE_STORAGE_OPTIONS["config_kwargs"]["connect_timeout"],
  73            "read_timeout": _DEFAULT_REMOTE_STORAGE_OPTIONS["config_kwargs"]["read_timeout"],
  74            "retries": dict(_DEFAULT_REMOTE_STORAGE_OPTIONS["config_kwargs"]["retries"]),
  75        }
  76    }
  77    user = storage_options or {}
  78    if not isinstance(user, dict):
  79        return merged
  80
  81    for key, value in user.items():
  82        if key == "config_kwargs" and isinstance(value, dict):
  83            merged_cfg = merged.setdefault("config_kwargs", {})
  84            for cfg_key, cfg_val in value.items():
  85                if cfg_key == "retries" and isinstance(cfg_val, dict):
  86                    retries = merged_cfg.setdefault("retries", {})
  87                    retries.update(cfg_val)
  88                else:
  89                    merged_cfg[cfg_key] = cfg_val
  90        else:
  91            merged[key] = value
  92    return merged
  93
  94
  95_LOCK = SerializableLock()
  96
  97_LEVEL_NAME_MAPPING = grib2io.tables.get_table("4.5.grib2io.level.name")
  98
  99_TREE_HIERARCHY_LEVELS = [
 100    "typeOfFirstFixedSurface",
 101    "valueOfFirstFixedSurface",
 102    "productDefinitionTemplateNumber",
 103    "perturbationNumber",
 104    "leadTime",
 105    "duration",
 106    "percentileValue",
 107    "typeOfProbability",
 108    "thresholdLowerLimit",
 109    "thresholdUpperLimit",
 110]
 111
 112
 113def _decode_ptype(values: np.ndarray) -> np.ndarray:
 114    """
 115    Decode numeric precipitation type codes into human-readable strings.
 116
 117    Parameters
 118    ----------
 119    values : np.ndarray
 120        Array of numeric precipitation type codes.
 121
 122    Returns
 123    -------
 124    np.ndarray
 125        Array of decoded precipitation type strings.
 126    """
 127    return _decode_code(values, "4.201")
 128
 129
 130def _decode_code(values: np.ndarray, table: str) -> np.ndarray:
 131    """
 132    Decode numeric codes into human-readable strings using a GRIB2 table.
 133
 134    Parameters
 135    ----------
 136    values : np.ndarray
 137    Array of numeric codes.
 138    table : str
 139    The GRIB2 table to use for decoding (e.g., "4.201").
 140
 141    Returns
 142    -------
 143    np.ndarray
 144    Array of decoded string definitions.
 145    """
 146
 147    def _lookup(val):
 148        res = tables.get_value_from_table(str(int(val)), table)
 149        if isinstance(res, list):
 150            return str(res[0])
 151        return str(res)
 152
 153    # Pass otypes to avoid string truncation based on the first element
 154    # Use StringDType if available (NumPy 2.0+), otherwise fallback to object
 155    if _HAS_STRINGDTYPE:
 156        vlookup = np.vectorize(_lookup, otypes=[np.dtypes.StringDType])
 157    else:
 158        vlookup = np.vectorize(_lookup, otypes=[object])
 159    return vlookup(values)
 160
 161
 162AVAILABLE_NON_GEO_COORDS = [
 163    "duration",
 164    "leadTime",
 165    "percentileValue",
 166    "perturbationNumber",
 167    "refDate",
 168    "thresholdLowerLimit",
 169    "thresholdUpperLimit",
 170    "valueOfFirstFixedSurface",
 171    "valueOfSecondFixedSurface",
 172    "typeOfAerosol",
 173    "constituentType",
 174    "sourceSinkIndicator",
 175    "firstWavelength",
 176    "secondWavelength",
 177    "firstSizeOfAerosol",
 178    "secondSizeOfAerosol",
 179    "scaledValueOfFirstWavelength",
 180    "scaledValueOfSecondWavelength",
 181    "scaledValueOfCentralWaveNumber",
 182    "scaledValueOfFirstSize",
 183    "scaledValueOfSecondSize",
 184]
 185"""Available non-geographic coordinate names."""
 186
 187AVAILABLE_NON_GEO_DIMS = [
 188    "duration",
 189    "leadTime",
 190    "percentileValue",
 191    "perturbationNumber",
 192    "refDate",
 193    "threshold",
 194    "level",
 195    "typeOfAerosol",
 196    "constituentType",
 197    "sourceSinkIndicator",
 198    "firstWavelength",
 199    "secondWavelength",
 200    "firstSizeOfAerosol",
 201    "secondSizeOfAerosol",
 202]
 203"""Available non-geographic dimension names."""
 204
 205# Lookup table to define surface types that should be parsed as vertical coordinates
 206VERTICAL_COORDINATE_SURFACES = [
 207    "Ground or Water Surface",
 208    "Isothermal Level",
 209    "Specified radius from the centre of the Sun",
 210    "Isobaric Surface",
 211    "Mean Sea Level",
 212    "Specific Altitude Above Mean Sea Level",
 213    "Specified Height Level Above Ground",
 214    "Sigma Level",
 215    "Hybrid Level",
 216    "Depth Below Land Surface",
 217    "Isentropic (theta) Level",
 218    "Level at Specified Pressure Difference from Ground to Level",
 219    "Potential Vorticity Surface",
 220    "Eta Level",
 221    "Logarithmic Hybrid Level",
 222    "Sigma height level",
 223    "Hybrid Height Level",
 224    "Hybrid Pressure Level",
 225    "Soil level",
 226    "Sea-ice level",
 227    "Depth Below Sea Level",
 228    "Depth Below Water Surface",
 229    "Ocean Model Level",
 230    "Ocean level defined by water density (sigma-theta) difference from near-surface to level",
 231    "Ocean level defined by water potential temperature difference from near-surface to level",
 232    "Ocean level defined by vertical eddy diffusivity difference from near-surface to level",
 233    "Ocean level defined by water density (rho) difference from near-surface to level",
 234]
 235"""
 236Lookup table to define surface types that should be parsed as vertical coordinates
 237when `data_model="nws-viz"`.
 238"""
 239
 240
 241def parse_data_model(ds: xr.Dataset, data_model: str) -> xr.Dataset:
 242    """
 243    Normalize a GRIB2-derived Dataset to a target data model (currently ``"nws-viz"``).
 244
 245    When ``data_model == "nws-viz"``, this function converts coordinate and
 246    variable names to snake_case, derives CF-like metadata, promotes select
 247    GRIB-derived quantities to coordinates, optionally swaps dimensions, and
 248    standardizes units/attributes. If ``data_model`` is anything else, the
 249    input dataset is returned unchanged.
 250
 251    Parameters
 252    ----------
 253    ds : xarray.Dataset
 254        GRIB2-derived dataset whose variables and attributes follow the
 255        conventions emitted by ``grib2io``. Expected to contain GRIB-related
 256        attributes such as ``typeOfFirstFixedSurface``,
 257        ``typeOfSecondFixedSurface``, and (for probabilistic variables)
 258        ``typeOfProbability``.
 259    data_model : str
 260        Target data model name. Only the value ``"nws-viz"`` triggers
 261        transformations.
 262
 263    Returns
 264    -------
 265    xarray.Dataset
 266        A new dataset with:
 267        * Selected coordinates renamed:
 268          ``refDate -> forecast_reference_time``,
 269          ``leadTime -> lead_time``,
 270          ``validDate -> time``,
 271          ``percentileValue -> percentile``,
 272          ``thresholdLowerLimit -> threshold_lower_limit``,
 273          ``thresholdUpperLimit -> threshold_upper_limit``.
 274        * Vertical coordinates derived from
 275          ``valueOfFirstFixedSurface`` / ``valueOfSecondFixedSurface`` and their
 276          corresponding ``typeOf*FixedSurface`` definitions. New coordinate
 277          names are generated from the surface definition (lowercased, spaces
 278          to underscores, punctuation removed). If the name already exists, a
 279          ``"_2"`` suffix is appended.
 280        * Possible dimension swaps:
 281          ``level -> <derived_vertical_coord>`` when present; and for
 282          probabilistic variables, ``threshold -> threshold_lower_limit`` or
 283          ``threshold -> threshold_upper_limit`` when
 284          ``typeOfProbability`` indicates the appropriate semantics.
 285        * Variable names lowercased; dataset- and variable-level attributes
 286          converted to snake_case (except GRIB section attributes which are
 287          normalized to ``grib...``).
 288        * CF-adjacent metadata populated: ``standard_name`` and
 289          ``cell_methods`` are set via the shortname→CF lookup table.
 290        * Percent units normalized from ``"%"`` to ``"percent"`` on coordinates.
 291        * For precipitation type (``PTYPE``) thresholds, numeric codes are
 292          decoded to strings (GRIB2 Table 4.201) in relevant attrs/coords.
 293
 294    Notes
 295    -----
 296    - Precipitation type decoding uses GRIB2 Table 4.201 via
 297      ``tables.get_value_from_table(code, "4.201")`` and returns a NumPy
 298      array with ``np.dtypes.StringDType``.
 299    - CF-related lookups are performed using
 300      ``tables.get_table("shortname_to_cf")``.
 301    - Vertical coordinate surface names are validated against
 302      ``VERTICAL_COORDINATE_SURFACES`` before promotion to coordinates.
 303
 304    Warnings
 305    --------
 306    This function assumes the presence of certain GRIB-derived attributes on the
 307    first data variable (e.g., ``typeOfFirstFixedSurface``,
 308    ``typeOfSecondFixedSurface``, and possibly ``typeOfProbability``).
 309    If these are absent or malformed, errors (e.g., ``KeyError``) may occur.
 310
 311    Examples
 312    --------
 313    >>> ds2 = parse_data_model(ds, "nws-viz")
 314    >>> list(ds2.coords)
 315    ['forecast_reference_time', 'lead_time', 'time', 'percentile', ...]
 316    """
 317    # convert coordinates and attributes to CF if requested
 318    if data_model == "nws-viz":
 319        # define regex to convert to snake case
 320        pattern = re.compile(r"(?<!^)(?=[A-Z])")
 321
 322        # check for coordinates and rename
 323        for coord in ds.coords:
 324            if coord == "refDate":
 325                ds = ds.rename({"refDate": "forecast_reference_time"})
 326
 327            elif coord == "leadTime":
 328                ds = ds.rename({"leadTime": "lead_time"})
 329
 330            elif coord == "validDate":
 331                ds = ds.rename({"validDate": "time"})
 332
 333            elif coord == "percentileValue":
 334                ds = ds.rename({"percentileValue": "percentile"})
 335
 336            elif coord == "perturbationNumber":
 337                ds = ds.rename({"perturbationNumber": "perturbation"})
 338                ds["perturbation"].attrs["long_name"] = "Ensemble Perturbation Number"
 339
 340            elif coord == "thresholdLowerLimit":
 341                ds = ds.rename({"thresholdLowerLimit": "threshold_lower_limit"})
 342                ds["threshold_lower_limit"].attrs["long_name"] = "Threshold Lower Limit"
 343                ds["threshold_lower_limit"].attrs["units"] = ds[list(ds.data_vars.keys())[0]].attrs["units"]
 344
 345                if "PTYPE" in ds.data_vars:
 346                    ds["threshold_lower_limit"] = xr.apply_ufunc(
 347                        _decode_ptype,
 348                        ds["threshold_lower_limit"],
 349                        dask="parallelized",
 350                        output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
 351                    )
 352
 353                # check if thresholdLowerLimit should be a dimension coordinate
 354                if "threshold" in ds.dims:
 355                    var_key = list(ds.data_vars.keys())[0]
 356                    prob_types = [
 357                        "Probability of event below lower limit",
 358                        "Probability of event above lower limit",
 359                        "Probability of event equal to lower limit",
 360                        "Probability of event between upper and lower limits (the range includes lower limit but not the upper limit)",
 361                    ]
 362                    if ds[var_key].attrs["typeOfProbability"] in prob_types:
 363                        ds = ds.swap_dims({"threshold": "threshold_lower_limit"})
 364
 365            elif coord == "thresholdUpperLimit":
 366                ds = ds.rename({"thresholdUpperLimit": "threshold_upper_limit"})
 367                ds["threshold_upper_limit"].attrs["long_name"] = "Threshold Upper Limit"
 368                ds["threshold_upper_limit"].attrs["units"] = ds[list(ds.data_vars.keys())[0]].attrs["units"]
 369
 370                if "PTYPE" in ds.data_vars:
 371                    ds["threshold_upper_limit"] = xr.apply_ufunc(
 372                        _decode_ptype,
 373                        ds["threshold_upper_limit"],
 374                        dask="parallelized",
 375                        output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
 376                    )
 377
 378                if "threshold" in ds.dims:
 379                    var_key = list(ds.data_vars.keys())[0]
 380                    prob_types = [
 381                        "Probability of event below upper limit",
 382                        "Probability of event above upper limit",
 383                    ]
 384                    if ds[var_key].attrs["typeOfProbability"] in prob_types:
 385                        ds = ds.swap_dims({"threshold": "threshold_upper_limit"})
 386
 387            elif coord == "typeOfAerosol":
 388                ds = ds.rename({"typeOfAerosol": "aerosol_type"})
 389                ds["aerosol_type"].attrs["long_name"] = "Aerosol Type"
 390                ds["aerosol_type"] = xr.apply_ufunc(
 391                    _decode_code,
 392                    ds["aerosol_type"],
 393                    "4.233",
 394                    dask="parallelized",
 395                    output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
 396                )
 397
 398            elif coord == "constituentType":
 399                ds = ds.rename({"constituentType": "constituent_type"})
 400                ds["constituent_type"].attrs["long_name"] = "Chemical Constituent Type"
 401                ds["constituent_type"] = xr.apply_ufunc(
 402                    _decode_code,
 403                    ds["constituent_type"],
 404                    "4.230",
 405                    dask="parallelized",
 406                    output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
 407                )
 408
 409            elif coord == "sourceSinkIndicator":
 410                ds = ds.rename({"sourceSinkIndicator": "source_sink_indicator"})
 411                ds["source_sink_indicator"].attrs["long_name"] = "Source/Sink Indicator"
 412                ds["source_sink_indicator"] = xr.apply_ufunc(
 413                    _decode_code,
 414                    ds["source_sink_indicator"],
 415                    "4.238",
 416                    dask="parallelized",
 417                    output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
 418                )
 419
 420            elif coord == "firstWavelength":
 421                ds = ds.rename({"firstWavelength": "first_wavelength"})
 422                ds["first_wavelength"].attrs["long_name"] = "First Wavelength"
 423                ds["first_wavelength"].attrs["units"] = "m"
 424
 425            elif coord == "secondWavelength":
 426                ds = ds.rename({"secondWavelength": "second_wavelength"})
 427                ds["second_wavelength"].attrs["long_name"] = "Second Wavelength"
 428                ds["second_wavelength"].attrs["units"] = "m"
 429
 430            elif coord == "firstSizeOfAerosol":
 431                ds = ds.rename({"firstSizeOfAerosol": "first_size_of_aerosol"})
 432                ds["first_size_of_aerosol"].attrs["long_name"] = "First Size of Aerosol"
 433                ds["first_size_of_aerosol"].attrs["units"] = "m"
 434
 435            elif coord == "secondSizeOfAerosol":
 436                ds = ds.rename({"secondSizeOfAerosol": "second_size_of_aerosol"})
 437                ds["second_size_of_aerosol"].attrs["long_name"] = "Second Size of Aerosol"
 438                ds["second_size_of_aerosol"].attrs["units"] = "m"
 439
 440            elif coord == "scaledValueOfFirstWavelength":
 441                ds = ds.rename({"scaledValueOfFirstWavelength": "scaled_first_wavelength"})
 442                ds["scaled_first_wavelength"].attrs["long_name"] = "Scaled Value of First Wavelength"
 443
 444            elif coord == "scaledValueOfSecondWavelength":
 445                ds = ds.rename({"scaledValueOfSecondWavelength": "scaled_second_wavelength"})
 446                ds["scaled_second_wavelength"].attrs["long_name"] = "Scaled Value of Second Wavelength"
 447
 448            elif coord == "scaledValueOfCentralWaveNumber":
 449                ds = ds.rename({"scaledValueOfCentralWaveNumber": "scaled_central_wave_number"})
 450                ds["scaled_central_wave_number"].attrs["long_name"] = "Scaled Value of Central Wave Number"
 451
 452            elif coord == "scaledValueOfFirstSize":
 453                ds = ds.rename({"scaledValueOfFirstSize": "scaled_first_size"})
 454                ds["scaled_first_size"].attrs["long_name"] = "Scaled Value of First Size"
 455
 456            elif coord == "scaledValueOfSecondSize":
 457                ds = ds.rename({"scaledValueOfSecondSize": "scaled_second_size"})
 458                ds["scaled_second_size"].attrs["long_name"] = "Scaled Value of Second Size"
 459
 460            # If the dataset has valueOfFirstFixedSurface as a coordinate
 461            elif coord == "valueOfFirstFixedSurface":
 462                # Get the valueOfFirstFixedSurface coordinate
 463                da = ds.valueOfFirstFixedSurface
 464
 465                # Get the definition and units from typeOfFirstFixedSurface
 466                var_key = list(ds.data_vars.keys())[0]
 467                definition, units = ds[var_key].attrs["typeOfFirstFixedSurface"]
 468
 469                if definition in VERTICAL_COORDINATE_SURFACES:
 470                    # Convert definition to lowercase and replace spaces with underscores
 471                    key = definition.lower().replace(" ", "_")
 472
 473                    # remove special characters
 474                    key = re.sub(r"[^a-z0-9_]", "", key)
 475
 476                    # Add units and grib_name attributes
 477                    da.attrs["units"] = units
 478                    da.attrs["grib_name"] = [
 479                        "valueOfFirstFixedSurface",
 480                        "typeOfFirstFixedSurface",
 481                    ]
 482
 483                    # Assign the coordinate with the new key name
 484                    ds = ds.assign_coords({key: da})
 485
 486                    # If valueOfFirstFixedSurface is a dimension, swap it with the new key
 487                    if "level" in ds.dims:
 488                        ds = ds.swap_dims({"level": key})
 489
 490                # Remove the original coordinates
 491                del ds["valueOfFirstFixedSurface"]
 492
 493            # If the dataset has valueOfSecondFixedSurface as a coordinate
 494            elif coord == "valueOfSecondFixedSurface":
 495                # Get the valueOfSecondFixedSurface coordinate
 496                da = ds.valueOfSecondFixedSurface
 497
 498                # Get the definition and units from typeOfSecondFixedSurface
 499                var_key = list(ds.data_vars.keys())[0]
 500                definition, units = ds[var_key].attrs["typeOfSecondFixedSurface"]
 501
 502                if definition in VERTICAL_COORDINATE_SURFACES:
 503                    # Convert definition to lowercase and replace spaces with underscores
 504                    key = definition.lower().replace(" ", "_")
 505
 506                    # remove special characters
 507                    key = re.sub(r"[^a-z0-9_]", "", key)
 508
 509                    # check if key is already in coords
 510                    if key in ds.coords:
 511                        key = key + "_2"
 512
 513                    # Add units and grib_name attributes
 514                    da.attrs["units"] = units
 515                    da.attrs["grib_name"] = [
 516                        "valueOfSecondFixedSurface",
 517                        "typeOfSecondFixedSurface",
 518                    ]
 519
 520                    # Assign the coordinate with the new key name
 521                    ds = ds.assign_coords({key: da})
 522
 523                # Remove the original coordinates
 524                del ds["valueOfSecondFixedSurface"]
 525            else:
 526                # change coord name to snake case
 527                new_coord_name = pattern.sub("_", coord).lower()
 528                ds = ds.rename({coord: new_coord_name})
 529
 530        # convert all attributes and variable names to snake case
 531        for var in ds.data_vars:
 532            da = ds[var]
 533            record = tables.get_table("shortname_to_cf").get(da.name)
 534            da.attrs["standard_name"] = "unknown" if record is None else record["cf_standard_name"]
 535            da.attrs["cell_methods"] = "unknown" if record is None else record["cf_cell_methods"]
 536
 537            ds[var] = da
 538
 539            # rename variable
 540            new_var_name = var.lower()
 541            ds = ds.rename({var: new_var_name})
 542
 543            # remove attr for typeOfFirstFixedSurface (applied as coordinate above)
 544            if "typeOfFirstFixedSurface" in ds[new_var_name].attrs:
 545                definition, units = ds[new_var_name].attrs["typeOfFirstFixedSurface"]
 546                ds[new_var_name].attrs["typeOfFirstFixedSurface"] = f"{definition} ({units})"
 547
 548            if "typeOfSecondFixedSurface" in ds[new_var_name].attrs:
 549                definition, units = ds[new_var_name].attrs["typeOfSecondFixedSurface"]
 550                ds[new_var_name].attrs["typeOfSecondFixedSurface"] = f"{definition} ({units})"
 551
 552            ds[new_var_name].attrs.pop("percentileValue", None)
 553
 554            if "threshold_lower_limit" in ds.coords:
 555                ds[new_var_name].attrs.pop("thresholdLowerLimit", None)
 556
 557            if "threshold_upper_limit" in ds.coords:
 558                ds[new_var_name].attrs.pop("thresholdUpperLimit", None)
 559
 560            for attr in list(ds[new_var_name].attrs.keys()):
 561                # skip grib section attrs
 562                if "GRIB2IO_section" in attr:
 563                    # replace GRIB2IO with grib in attr
 564                    new_attr_name = attr.replace("GRIB2IO", "grib")
 565                else:
 566                    # change attr name to snake case
 567                    new_attr_name = pattern.sub("_", attr).lower()
 568
 569                # update new attr name for specific CF names
 570                if new_attr_name == "full_name":
 571                    new_attr_name = "long_name"
 572
 573                # change % to percent
 574                if attr == "units" and ds[new_var_name].attrs[attr] == "%":
 575                    ds[new_var_name].attrs[attr] = "percent"
 576
 577                if new_var_name == "ptype" and "threshold" in new_attr_name:
 578                    value = ds[new_var_name].attrs.pop(attr)
 579                    ds[new_var_name].attrs[attr] = _decode_ptype(value)
 580                else:
 581                    # change attr name in attrs
 582                    ds[new_var_name].attrs[new_attr_name] = ds[new_var_name].attrs.pop(attr)
 583
 584            try:
 585                new_cell_methods = section4_to_cell_methods(ds[new_var_name].attrs["grib_section4"])
 586            except KeyError:
 587                pass
 588            else:
 589                if new_cell_methods:
 590                    existing = ds[new_var_name].attrs.get("cell_methods")
 591                    ds[new_var_name].attrs["cell_methods"] = " ".join(filter(None, [existing, new_cell_methods]))
 592
 593        # change dataset attrs to snake case
 594        for attr in list(ds.attrs.keys()):
 595            # change attr name to snake case
 596            new_attr_name = pattern.sub("_", attr).lower()
 597
 598            # change attr name in attrs
 599            ds.attrs[new_attr_name] = ds.attrs.pop(attr)
 600
 601        # change % to percent
 602        for coord in ds.coords:
 603            if "units" in ds[coord].attrs and ds[coord].attrs["units"] == "%":
 604                ds[coord].attrs["units"] = "percent"
 605
 606        # Update history for provenance
 607        history = ds.attrs.get("history", "")
 608        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
 609        ds.attrs["history"] = f"{now}: Parsed to data model {data_model}\n{history}"
 610
 611    # Update history for provenance
 612    history = ds.attrs.get("history", "")
 613    now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
 614    ds.attrs["history"] = f"{now}: Normalized to {data_model} data model\n{history}"
 615
 616    return ds
 617
 618
 619def section4_to_cell_methods(section4_array: np.ndarray) -> typing.Optional[str]:
 620    cell_methods = None
 621    if section4_array[1] == 0:
 622        cell_methods = "lead_time: point"
 623    elif section4_array[1] == 8:
 624        to_join = []
 625        # interval_end = datetime.datetime(*section4_array[17:23]
 626        time_unit_table = tables.get_table("4.4")
 627        for i in reversed(range(section4_array[23])):
 628            offset = 6 * i
 629            method = tables.get_table("4.10")[str(section4_array[25 + offset])]
 630            method = method.replace("Average", "mean").lower()
 631            if section4_array[26 + offset] == 1:
 632                dim = "forecast_reference_time"
 633            elif section4_array[26 + offset] == 2:
 634                dim = "lead_time"
 635            # duration_unit = time_unit_table[str(section4_array[27 + offset])].lower()
 636            # duration_value = section4_array[28 + offset]
 637            input_interval_units = time_unit_table[str(section4_array[29 + offset])].lower()
 638            input_interval_value = section4_array[28 + offset]
 639            to_join.append(f"{dim:s}: {method:s} (interval: {input_interval_value:d} {input_interval_units:s})")
 640            # comment: duration {duration_value:d} {duration_unit:s} ending {interval_end:%Y-%m-%dT%H:%M:%s}
 641        cell_methods = " ".join(to_join)
 642    return cell_methods
 643
 644
 645# ---------------------------------------------------------------------------
 646# Lazy import guards for optional dependencies
 647# ---------------------------------------------------------------------------
 648
 649
 650class GribBackendEntrypoint(BackendEntrypoint):
 651    """
 652    xarray backend engine entrypoint for opening and decoding grib2 files.
 653
 654    .. warning::
 655
 656       This backend is experimental and the API/behavior may change without
 657       backward compatibility.
 658    """
 659
 660    def open_dataset(
 661        self,
 662        filename_or_obj,
 663        drop_variables=None,
 664        save_index=True,
 665        filters=None,
 666        data_model=None,
 667        chunks=None,
 668        storage_options=None,
 669    ) -> xr.Dataset:
 670        """
 671        Read and parse metadata from a GRIB2 file.
 672
 673        Parameters
 674        ----------
 675        filename_or_obj : str or file-like
 676            GRIB2 file to be opened. Can be a local path or a remote URI.
 677        drop_variables : list of str, optional
 678            List of variables to exclude from the dataset.
 679        save_index : bool, optional
 680            Whether to save the GRIB2 index to a file (default is True).
 681        filters : dict, optional
 682            Filter GRIB2 messages to a single hypercube. Dictionary keys can
 683            be any GRIB2 metadata attribute name.
 684        data_model : str, optional
 685            Parse GRIB metadata following a defined data model convention
 686            (e.g., "nws-viz").
 687        chunks : int, dict or 'auto', optional
 688            If chunks is provided, it is used to load the dataset into a
 689            dask-backed dataset.
 690        storage_options : dict, optional
 691            Extra options passed to the storage backend.
 692
 693        Returns
 694        -------
 695        xarray.Dataset
 696            Xarray dataset of GRIB2 messages.
 697        """
 698        if filters is None:
 699            filters = {}
 700
 701        with grib2io.open(
 702            filename_or_obj,
 703            save_index=save_index,
 704            _xarray_backend=True,
 705            **(storage_options or {}),
 706        ) as f:
 707            file_index = pd.DataFrame(f._index)
 708            file_index = file_index.assign(msg=list(f))
 709
 710        ds = _open_dataset_from_index(
 711            file_index,
 712            filename_or_obj,
 713            filters,
 714            data_model,
 715            drop_variables=drop_variables,
 716            chunks=chunks,
 717        )
 718
 719        # Update history for provenance
 720        history = ds.attrs.get("history", "")
 721        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
 722        ds.attrs["history"] = f"{now}: Initialized via grib2io.open_dataset from {filename_or_obj}\n{history}"
 723        ds.attrs["data_model"] = "" if data_model is None else data_model
 724
 725        return ds
 726
 727    def open_datatree(
 728        self,
 729        filename_or_obj,
 730        drop_variables=None,
 731        save_index=True,
 732        filters=None,
 733        stack_vertical=False,
 734        chunks=None,
 735    ) -> typing.Any:
 736        """
 737        Open a GRIB2 file as an xarray DataTree.
 738
 739        Parameters
 740        ----------
 741        filename : str
 742            Path to the GRIB2 file.
 743        drop_variables : list, optional
 744            List of variables to exclude.
 745        filters : dict, optional
 746            Filter criteria for GRIB2 messages.
 747        stack_vertical : bool, optional
 748            If True, organize the tree with vertical layers stacked in a single dataset.
 749        chunks : int, dict or 'auto', optional
 750            If chunks is provided, it is used to load the dataset into a
 751            dask-backed dataset.
 752
 753        Returns
 754        -------
 755        xarray.DataTree
 756            A hierarchical DataTree representation of the GRIB2 data.
 757        """
 758        if not _HAS_DATATREE:
 759            raise ImportError("xarray version does not support DataTree functionality.")
 760
 761        if filters is None:
 762            filters = {}
 763
 764        # Open the file without any filters first to get all messages
 765        with grib2io.open(filename_or_obj, save_index=save_index, _xarray_backend=True) as f:
 766            file_index = pd.DataFrame(f._index)
 767            file_index = file_index.assign(msg=list(f))
 768
 769        # Build tree structure from GRIB messages with specified options
 770        tree = build_datatree_from_grib(
 771            filename_or_obj,
 772            file_index,
 773            filters,
 774            stack_vertical=stack_vertical,
 775            drop_variables=drop_variables,
 776            chunks=chunks,
 777        )
 778
 779        # Update history for provenance
 780        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
 781        history = f"{now}: Initialized via grib2io.open_datatree\n"
 782
 783        def _add_history(node):
 784            if node.ds is not None:
 785                node.ds.attrs["history"] = history + node.ds.attrs.get("history", "")
 786            for child in node.children.values():
 787                _add_history(child)
 788
 789        _add_history(tree)
 790
 791        # Put warning here so it is the last message from likely other Xarray warnings.
 792        warnings.warn(
 793            "grib2io’s xarray backend DataTree support is experimental. The DataTree structure or attributes may change in future releases.",
 794            UserWarning,
 795            stacklevel=2,
 796        )
 797
 798        return tree
 799
 800
 801class GribBackendArray(BackendArray):
 802    """
 803    BackendArray implementation for GRIB2 data.
 804    """
 805
 806    def __init__(self, array: "OnDiskArray", lock: SerializableLock):
 807        """
 808        Initialize the GribBackendArray.
 809
 810        Parameters
 811        ----------
 812        array : OnDiskArray
 813            The on-disk array object.
 814        lock : SerializableLock
 815            The lock to use for thread-safe access.
 816        """
 817        self.array = array
 818        self.shape = array.shape
 819        self.dtype = np.dtype(array.dtype)
 820        self.lock = lock
 821
 822    def __getitem__(self, key: xr.core.indexing.ExplicitIndexer) -> np.typing.ArrayLike:
 823        return xr.core.indexing.explicit_indexing_adapter(
 824            key,
 825            self.shape,
 826            indexing.IndexingSupport.BASIC,
 827            self._raw_getitem,
 828        )
 829
 830    def _raw_getitem(self, key: tuple) -> np.ndarray:
 831        """
 832        Implement thread-safe access to data on disk.
 833
 834        Parameters
 835        ----------
 836        key : tuple
 837            The indexing key.
 838
 839        Returns
 840        -------
 841        np.ndarray
 842            The indexed array.
 843        """
 844        with self.lock:
 845            return self.array[key]
 846
 847
 848class Grid:
 849    def __new__(cls, section3):
 850        gdtn = section3[4]
 851        Gdt = templates.gdt_class_by_gdtn(gdtn)
 852
 853        @dataclass
 854        class _Grid(Gdt):
 855            section3: NDArray = field(init=True, repr=True)
 856            # Section 3 looked up common attributes.  Other looked up attributes are available according
 857            # to the Grid Definition Template.
 858            gridDefinitionSection: NDArray = field(init=False, repr=False, default=templates.GridDefinitionSection())
 859            sourceOfGridDefinition: int = field(init=False, repr=False, default=templates.SourceOfGridDefinition())
 860            numberOfDataPoints: int = field(init=False, repr=False, default=templates.NumberOfDataPoints())
 861            interpretationOfListOfNumbers: templates.Grib2Metadata = field(
 862                init=False,
 863                repr=False,
 864                default=templates.InterpretationOfListOfNumbers(),
 865            )
 866            gridDefinitionTemplateNumber: templates.Grib2Metadata = field(init=False, repr=False, default=templates.GridDefinitionTemplateNumber())
 867            gridDefinitionTemplate: list = field(init=False, repr=False, default=templates.GridDefinitionTemplate())
 868            _earthparams: dict = field(init=False, repr=False, default=templates.EarthParams())
 869            _dxsign: float = field(init=False, repr=False, default=templates.DxSign())
 870            _dysign: float = field(init=False, repr=False, default=templates.DySign())
 871            _llscalefactor: float = field(init=False, repr=False, default=templates.LLScaleFactor())
 872            _lldivisor: float = field(init=False, repr=False, default=templates.LLDivisor())
 873            _xydivisor: float = field(init=False, repr=False, default=templates.XYDivisor())
 874            shapeOfEarth: templates.Grib2Metadata = field(init=False, repr=False, default=templates.ShapeOfEarth())
 875            earthShape: str = field(init=False, repr=False, default=templates.EarthShape())
 876            earthRadius: float = field(init=False, repr=False, default=templates.EarthRadius())
 877            earthMajorAxis: float = field(init=False, repr=False, default=templates.EarthMajorAxis())
 878            earthMinorAxis: float = field(init=False, repr=False, default=templates.EarthMinorAxis())
 879            resolutionAndComponentFlags: list = field(init=False, repr=False, default=templates.ResolutionAndComponentFlags())
 880            ny: int = field(init=False, repr=False, default=templates.Ny())
 881            nx: int = field(init=False, repr=False, default=templates.Nx())
 882            scanModeFlags: list = field(init=False, repr=False, default=templates.ScanModeFlags())
 883            projParameters: dict = field(init=False, repr=False, default=templates.ProjParameters())
 884
 885            def __post_init__(self):
 886                self.gdtn = self.section3[4]
 887
 888        grid = _Grid(section3)
 889        return grid
 890
 891
 892def exclusive_slice_to_inclusive(item: slice):
 893    """
 894    Convert a slice with exclusive stop to an inclusive slice.
 895
 896    If the slice has a step, the stop is reduced by the step, so that both
 897    interpretations would yield the same result.
 898
 899    The means that [start, stop) is converted to [start, stop - step].
 900
 901    Parameters
 902    ----------
 903    item
 904        The slice to convert.
 905
 906    Returns
 907    -------
 908    slice
 909        The converted slice.
 910    """
 911    # return the None slice
 912    if item.start is None and item.stop is None and item.step is None:
 913        return item
 914    if not isinstance(item, slice):
 915        raise ValueError(f"item must be a slice; it was of type {type(item)}")
 916    # if step is None, it's one
 917    step = 1 if item.step is None else item.step
 918    if item.stop < item.start or step < 1:
 919        raise ValueError(f"slice {item} not accounted for")
 920    # handle case where slice has one item
 921    if abs(item.stop - item.start) == step:
 922        return [item.start]
 923    # other cases require reducing the stop by the step
 924    s = slice(item.start, item.stop - step, step)
 925    return s
 926
 927
 928class Validator:
 929    def __set_name__(self, owner, name):
 930        self.private_name = f"_{name}"
 931        self.name = name
 932
 933    def __get__(self, obj, objtype=None):
 934        try:
 935            value = getattr(obj, self.private_name)
 936        except AttributeError:
 937            value = None
 938        return value
 939
 940
 941class PdIndex(Validator):
 942    def __set__(self, obj, value):
 943        try:
 944            value = pd.Index(value)
 945        except TypeError:
 946            value = pd.Index([value])
 947        setattr(obj, self.private_name, value)
 948
 949
 950def _asarray_tuplesafe(values: typing.Any) -> np.ndarray:
 951    """
 952    Convert values to a numpy array of at most 1-dimension and preserve tuples.
 953
 954    Adapted from ``pandas.core.common._asarray_tuplesafe``.
 955
 956    Parameters
 957    ----------
 958    values : any
 959        The values to convert.
 960
 961    Returns
 962    -------
 963    np.ndarray
 964        The converted numpy array.
 965    """
 966    if isinstance(values, tuple):
 967        result = np.empty(1, dtype=object)
 968        result[0] = values
 969    else:
 970        result = np.asarray(values)
 971        if result.ndim == 2:
 972            result = np.empty(len(values), dtype=object)
 973            result[:] = values
 974
 975    return result
 976
 977
 978def array_safe_eq(a: typing.Any, b: typing.Any) -> bool:
 979    """
 980    Check if a and b are equal, even if they are numpy arrays.
 981
 982    Parameters
 983    ----------
 984    a : any
 985        First object to compare.
 986    b : any
 987        Second object to compare.
 988
 989    Returns
 990    -------
 991    bool
 992        True if equal, False otherwise.
 993    """
 994    if a is b:
 995        return True
 996    if hasattr(a, "equals"):
 997        return a.equals(b)
 998    if hasattr(a, "all") and hasattr(b, "all"):
 999        return a.shape == b.shape and (a == b).all()
1000    if hasattr(a, "all") or hasattr(b, "all"):
1001        return False
1002    try:
1003        return a == b
1004    except TypeError:
1005        return NotImplementedError
1006
1007
1008def dc_eq(dc1: typing.Any, dc2: typing.Any) -> bool:
1009    """
1010    Check if two dataclasses which hold numpy arrays are equal.
1011
1012    Parameters
1013    ----------
1014    dc1 : any
1015        First dataclass to compare.
1016    dc2 : any
1017        Second dataclass to compare.
1018
1019    Returns
1020    -------
1021    bool
1022        True if equal, False otherwise.
1023    """
1024    if dc1 is dc2:
1025        return True
1026    if dc1.__class__ is not dc2.__class__:
1027        return NotImplementedError
1028    t1 = astuple(dc1)
1029    t2 = astuple(dc2)
1030    return all(array_safe_eq(a1, a2) for a1, a2 in zip(t1, t2))
1031
1032
1033def coords_from_cube(cube: dict) -> typing.Dict[str, xr.Variable]:
1034    """
1035    Create a dictionary of xarray Variables from a cube definition.
1036
1037    Parameters
1038    ----------
1039    cube : dict
1040        Dimension cube definition.
1041
1042    Returns
1043    -------
1044    dict of str to xarray.Variable
1045        Coordinates for the Dataset/DataArray.
1046    """
1047    keys = list(cube.keys())
1048    keys.remove("x")
1049    keys.remove("y")
1050    coords = dict()
1051    for k in keys:
1052        if k is not None:
1053            if len(cube[k]) > 1:
1054                coords[k] = xr.Variable(dims=k, data=cube[k], attrs=dict(grib_name=k))
1055            elif len(cube[k]) == 1:
1056                coords[k] = xr.Variable(dims=tuple(), data=cube[k][0], attrs=dict(grib_name=k))
1057    return coords
1058
1059
1060@dataclass
1061class OnDiskArray:
1062    """
1063    On-disk array representation for GRIB2 messages.
1064    """
1065
1066    file_name: typing.Union[str, typing.List[str]]
1067    index: pd.DataFrame = field(repr=False)
1068    cube: dict = field(repr=False)
1069    shape: typing.Tuple[int, ...] = field(init=False)
1070    ndim: int = field(init=False)
1071    geo_ndim: int = field(init=False)
1072    dtype: str = "float32"
1073
1074    def __post_init__(self):
1075        # multiple grids not allowed so can just use first
1076        geo_shape = (self.index.iloc[0].ny, self.index.iloc[0].nx)
1077
1078        self.geo_shape = geo_shape
1079        self.geo_ndim = len(geo_shape)
1080
1081        if len(self.index) == 1:
1082            self.shape = geo_shape
1083        else:
1084            if self.index.index.nlevels == 1:
1085                self.shape = tuple([len(self.index.index)]) + geo_shape
1086            else:
1087                self.shape = tuple([len(i) for i in self.index.index.levels]) + geo_shape
1088        self.ndim = len(self.shape)
1089
1090        cols = ["msg", "sectionOffset"]
1091        if "file_index" in self.index.columns:
1092            cols.append("file_index")
1093        self.index = self.index[cols]
1094
1095    def __getitem__(self, item: tuple) -> np.ndarray:
1096        """
1097        Retrieve data from disk for the specified slices.
1098
1099        Parameters
1100        ----------
1101        item : tuple
1102            The slicing tuple.
1103
1104        Returns
1105        -------
1106        np.ndarray
1107            The retrieved data array.
1108        """
1109        # dimensions not in index are internal to tdlpack records; 2 dims for
1110        # grids; 1 dim for stations
1111
1112        index_slicer = item[: -self.geo_ndim]
1113        # maintain all multindex levels
1114        index_slicer = tuple([[i] if isinstance(i, int) else i for i in index_slicer])
1115
1116        # pandas loc slicing is inclusive, therefore convert slices into
1117        # explicit lists
1118        index_slicer_inclusive = tuple([exclusive_slice_to_inclusive(i) if isinstance(i, slice) else i for i in index_slicer])
1119
1120        # get records selected by item in new index dataframe
1121        if len(index_slicer_inclusive) == 1:
1122            index = self.index.loc[index_slicer_inclusive]
1123        elif len(index_slicer_inclusive) > 1:
1124            index = self.index.loc[index_slicer_inclusive, :]
1125        else:
1126            index = self.index
1127        index = index.set_index(index.index)
1128
1129        # set miloc to new relative locations in sub array
1130        index["miloc"] = list(zip(*[index.index.unique(level=dim).get_indexer(index.index.get_level_values(dim)) for dim in index.index.names]))
1131
1132        if len(index_slicer_inclusive) == 1:
1133            array_field_shape = tuple([len(index.index)]) + self.geo_shape
1134        elif len(index_slicer_inclusive) > 1:
1135            array_field_shape = index.index.levshape + self.geo_shape
1136        else:
1137            array_field_shape = self.geo_shape
1138
1139        array_field = np.full(array_field_shape, fill_value=np.nan, dtype="float32")
1140
1141        if "file_index" in index.columns:
1142            for file_idx, group in index.groupby("file_index"):
1143                filename = self.file_name[file_idx] if isinstance(self.file_name, list) else self.file_name
1144                with open(filename, mode="rb") as filehandle:
1145                    for key, row in group.iterrows():
1146                        bitmap_offset = None if pd.isna(row["sectionOffset"][6]) else int(row["sectionOffset"][6])
1147                        values = _data(filehandle, row.msg, bitmap_offset, row["sectionOffset"][7])
1148
1149                        if len(index_slicer_inclusive) >= 1:
1150                            array_field[row.miloc] = values
1151                        else:
1152                            array_field = values
1153        else:
1154            with open(self.file_name, mode="rb") as filehandle:
1155                for key, row in index.iterrows():
1156                    bitmap_offset = None if pd.isna(row["sectionOffset"][6]) else int(row["sectionOffset"][6])
1157                    values = _data(filehandle, row.msg, bitmap_offset, row["sectionOffset"][7])
1158
1159                    if len(index_slicer_inclusive) >= 1:
1160                        array_field[row.miloc] = values
1161                    else:
1162                        array_field = values
1163
1164        # handle geo dim slicing
1165        array_field = array_field[(Ellipsis,) + item[-self.geo_ndim :]]
1166
1167        # squeeze array dimensions expressed as integer
1168        for i, it in reversed(list(enumerate(item[: -self.geo_ndim]))):
1169            if isinstance(it, int):
1170                array_field = array_field[(slice(None, None, None),) * i + (0,)]
1171
1172        return array_field
1173
1174
1175def dims_to_shape(d: dict) -> tuple:
1176    """
1177    Convert dimension metadata to a shape tuple.
1178
1179    Parameters
1180    ----------
1181    d : dict
1182        Dimension metadata dictionary.
1183
1184    Returns
1185    -------
1186    tuple
1187        Shape tuple.
1188    """
1189    if "nx" in d:
1190        t = (d["ny"], d["nx"])
1191    else:
1192        t = (d["nsta"],)
1193    return t
1194
1195
1196def filter_index(index: pd.DataFrame, k: str, v: typing.Any) -> pd.DataFrame:
1197    """
1198    Filter a GRIB2 index DataFrame by a key-value pair.
1199
1200    Supports slice and vectorized-indexing similar to xarray's ``sel``.
1201
1202    Parameters
1203    ----------
1204    index : pandas.DataFrame
1205        The GRIB2 index DataFrame to filter.
1206    k : str
1207        Column name to filter by.
1208    v : any
1209        Value(s) or slice to filter for.
1210
1211    Returns
1212    -------
1213    pandas.DataFrame
1214        Filtered index.
1215    """
1216    if isinstance(v, slice):
1217        index = index.set_index(k)
1218        index = index.loc[v]
1219        index = index.reset_index()
1220    else:
1221        label = (
1222            v
1223            if getattr(v, "ndim", 1) > 1  # vectorized-indexing
1224            else _asarray_tuplesafe(v)
1225        )
1226        if label.ndim == 0:
1227            # see https://github.com/pydata/xarray/pull/4292 for details
1228            label_value = label[()] if label.dtype.kind in "mM" else label.item()
1229            try:
1230                indexer = pd.Index(index[k]).get_loc(label_value)
1231                if isinstance(indexer, int):
1232                    index = index.iloc[[indexer]]
1233                else:
1234                    index = index.iloc[indexer]
1235            except KeyError:
1236                index = index.iloc[[]]
1237        else:
1238            indexer = pd.Index(index[k]).get_indexer_for(np.ravel(v))
1239            index = index.iloc[indexer[indexer >= 0]]
1240
1241    return index
1242
1243
1244def parse_grib_index(
1245    index: pd.DataFrame,
1246    filters: typing.Mapping[str, typing.Any] = dict(),
1247) -> typing.Tuple[pd.DataFrame, typing.Dict[str, typing.List[str]], dict, typing.Dict[str, dict]]:
1248    """
1249    Apply filters.
1250
1251    Evaluate remaining dimensions based on pdtn and parse each out.
1252
1253    Parameters
1254    ----------
1255    index
1256        Pandas DataFrame containing the GRIB2 message index.
1257    filters
1258        Filter GRIB2 messages to single hypercube. Dict keys can be any
1259        GRIB2 metadata attribute name.
1260
1261    Returns
1262    -------
1263    index
1264        Modified Pandas DataFrame with added GRIB2 metadata columns.
1265    dim_coords
1266        List of GRIB2 attributes that will be used for coordinates and/or dimensions.
1267    attrs
1268        Dict of metadata attributes (non-coordinates, non-geo)
1269    """
1270
1271    # make a copy of filters, remove filters as they are applied
1272    filters = copy(filters)
1273
1274    for k, v in filters.items():
1275        if k not in index.columns:
1276            kwarg = {k: index.msg.apply(lambda msg: getattr(msg, k))}
1277            index = index.assign(**kwarg)
1278        # adopt parts of xarray's sel logic  so that filters behave similarly
1279        # allowed to filter to nothing to make empty dataset
1280        index = filter_index(index, k, v)
1281
1282    if len(index) == 0:
1283        return index, list(), dict(), dict()
1284
1285    dim_coords = dict()  # key=name of dim, value=list of coord names
1286    attrs = dict()
1287    coord_attrs = dict()
1288
1289    # expand index
1290    index = index.assign(shortName=index.msg.apply(lambda msg: msg.shortName))
1291    index = index.assign(nx=index.msg.apply(lambda msg: msg.nx))
1292    index = index.assign(ny=index.msg.apply(lambda msg: msg.ny))
1293    index = index.astype({"ny": "int", "nx": "int"})
1294
1295    # apply common filters(to all definition templates) to reduce dataset to
1296    # single cube
1297    # ensure only one of each of the below exists after filters applied
1298    required_uniques = [
1299        "productDefinitionTemplateNumber",
1300        "typeOfGeneratingProcess",
1301        "typeOfFirstFixedSurface",
1302        "typeOfSecondFixedSurface",
1303    ]
1304
1305    def meta_check(index, attrs, meta):
1306        """
1307        add meta to the datframe index
1308        check that there is a single type
1309        add the type to attrs
1310
1311        returns index, attrs
1312        """
1313        index = index.assign(**{meta: index.msg.apply(lambda msg: getattr(msg, meta))})
1314
1315        unique = index[meta].unique()
1316        if len(index[meta].unique()) > 1:
1317            raise ValueError(f"filter to a single {meta}; found: {[str(i) for i in unique]}")
1318        value = unique.item()
1319        if isinstance(value, grib2io.templates.Grib2Metadata):
1320            value = value.definition
1321
1322        # None is returned if no value found,
1323        # check and change to string None
1324        if value is None:
1325            value = "None"
1326
1327        attrs[meta] = value
1328        return index, attrs
1329
1330    for meta in required_uniques:
1331        index, attrs = meta_check(index, attrs, meta)
1332
1333    pdtn = index.productDefinitionTemplateNumber.iloc[0].value
1334
1335    # determine which non geo dimensions can be created from data by this point
1336    # the index is filtered down to a single type for all required_uniques
1337
1338    # Dim Name     # matching dim_name for using this data as index coordinate
1339    dim_coords["refDate"] = ["refDate"]
1340    coord_attrs["refDate"] = dict(standard_name="forecast_reference_time")
1341    #   dim_coords["refDate"] = ["refDate", "hour"] # non dim name matching items in list are used as non-index coordinates
1342
1343    dim_coords["leadTime"] = ["leadTime"]
1344    coord_attrs["leadTime"] = dict(standard_name="forecast_period")
1345
1346    if "valueOfFirstFixedSurface" not in index.columns:
1347        index = index.assign(valueOfFirstFixedSurface=index.msg.apply(lambda msg: msg.valueOfFirstFixedSurface))
1348    if "valueOfsecondFixedSurface" not in index.columns:
1349        index = index.assign(valueOfSecondFixedSurface=index.msg.apply(lambda msg: msg.valueOfSecondFixedSurface))
1350
1351    # dim name api change, user could run ds = ds.swap_dims(fixedSurface="valueOfFirstFixedSurface")
1352    index = index.assign(level=list(zip(index["valueOfFirstFixedSurface"], index["valueOfSecondFixedSurface"])))
1353    #   index = index.assign(level=index.msg.apply(lambda msg: msg.level))
1354    # lack of "level" indeicates don't create extra index coordinate "level"
1355    dim_coords["level"] = ["valueOfFirstFixedSurface", "valueOfSecondFixedSurface"]
1356
1357    # logic for parsing possible dims from specific product definition section
1358
1359    if pdtn in {5, 9}:
1360        # Probability forecasts at a horizontal level or in a horizontal layer
1361        # in a continuous or non-continuous time interval.  (see Template
1362        # 4.9)
1363        #       AVAILABLE_THRESHOLD = {
1364        #           0: {'has_lower': True, 'has_upper': False},
1365        #           1: {'has_lower': False, 'has_upper': True},
1366        #           2: {'has_lower': True, 'has_upper': True},
1367        #           3: {'has_lower': True, 'has_upper': False},
1368        #           4: {'has_lower': False, 'has_upper': True},
1369        #           5: {'has_lower': True, 'has_upper': False},
1370        #       }
1371
1372        index, attrs = meta_check(index, attrs, "typeOfProbability")
1373        if "thresholdLowerLimit" not in index.columns:
1374            index = index.assign(thresholdLowerLimit=index.msg.apply(lambda msg: msg.thresholdLowerLimit))
1375        if "thresholdUpperLimit" not in index.columns:
1376            index = index.assign(thresholdUpperLimit=index.msg.apply(lambda msg: msg.thresholdUpperLimit))
1377        if "threshold" not in index.columns:
1378            # using composite of lower and upper, but could use threshold string from grib2io as long as that is unique and based on lower and upper
1379            index = index.assign(threshold=list(zip(index["thresholdLowerLimit"], index["thresholdUpperLimit"])))
1380        #           index = index.assign(threshold = index.msg.apply(lambda msg: msg.threshold))
1381
1382        # ommiting threshold results in no index being assigned for this possible dim
1383        dim_coords["threshold"] = ["thresholdLowerLimit", "thresholdUpperLimit"]
1384
1385    if pdtn in {6, 10}:
1386        # Percentile forecasts at a horizontal level or in a horizontal layer
1387        # in a continuous or non-continuous time interval.  (see Template
1388        # 4.10)
1389        dim_coords["percentileValue"] = ["percentileValue"]
1390        coord_attrs["percentileValue"] = dict(long_name="percentile", units="percent")
1391
1392    if pdtn in {
1393        8,
1394        9,
1395        10,
1396        11,
1397        12,
1398        13,
1399        14,
1400        42,
1401        43,
1402        45,
1403        46,
1404        47,
1405        61,
1406        62,
1407        63,
1408        67,
1409        68,
1410        72,
1411        73,
1412        78,
1413        79,
1414        82,
1415        83,
1416        84,
1417        85,
1418        87,
1419        91,
1420    }:
1421        dim_coords["duration"] = ["duration"]
1422
1423    if pdtn in {
1424        1,
1425        11,
1426        33,
1427        34,
1428        41,
1429        43,
1430        45,
1431        47,
1432        49,
1433        54,
1434        56,
1435        58,
1436        59,
1437        63,
1438        68,
1439        77,
1440        79,
1441        81,
1442        83,
1443        84,
1444        85,
1445        92,
1446    }:
1447        dim_coords["perturbationNumber"] = ["perturbationNumber"]
1448
1449    if pdtn in {2, 3, 4, 12, 13, 14}:
1450        index, attrs = meta_check(index, attrs, "typeOfDerivedForecast")
1451
1452    if pdtn in {5, 9}:
1453        dim_coords["typeOfProbability"] = ["typeOfProbability"]
1454
1455    if pdtn in {6, 10}:
1456        dim_coords["percentileValue"] = ["percentileValue"]
1457
1458    if pdtn in {8, 15, 42, 46, 62, 67, 72, 78, 82, 1001, 1002, 1100, 1101}:
1459        index, attrs = meta_check(index, attrs, "statisticalProcess")
1460
1461    # Logic for Trace Gas and Aerosol dimensions
1462    if pdtn in {40, 41, 42, 43, 76, 77, 78, 79}:
1463        dim_coords["constituentType"] = ["constituentType"]
1464
1465    if pdtn in {76, 77, 78, 79}:
1466        dim_coords["sourceSinkIndicator"] = ["sourceSinkIndicator"]
1467
1468    if pdtn in {44, 45, 46, 47, 48, 49, 50, 80, 81, 82, 83, 84, 85}:
1469        dim_coords["typeOfAerosol"] = ["typeOfAerosol"]
1470
1471    if pdtn in {80, 81, 82, 83, 84}:
1472        dim_coords["sourceSinkIndicator"] = ["sourceSinkIndicator"]
1473
1474    if pdtn in {48, 49, 80, 81}:
1475        dim_coords["firstWavelength"] = ["firstWavelength"]
1476        dim_coords["secondWavelength"] = ["secondWavelength"]
1477        dim_coords["firstSizeOfAerosol"] = ["firstSizeOfAerosol"]
1478        dim_coords["secondSizeOfAerosol"] = ["secondSizeOfAerosol"]
1479
1480    # Finish logic by pdtn
1481
1482    for k, v in dim_coords.items():
1483        for meta in v:
1484            if meta not in index.columns:
1485                index = index.assign(**{meta: index.msg.apply(lambda msg: getattr(msg, meta))})
1486
1487    return index, dim_coords, attrs, coord_attrs
1488
1489
1490# Custom open_datatree function to open grib files as DataTree
1491def open_datatree(
1492    filename: str,
1493    *,
1494    drop_variables: typing.Optional[typing.List[str]] = None,
1495    filters: typing.Optional[typing.Mapping[str, typing.Any]] = None,
1496    engine: str = "grib2io",
1497    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
1498    **kwargs,
1499) -> typing.Any:
1500    """
1501    Open a GRIB2 file as an xarray DataTree.
1502
1503    Parameters
1504    ----------
1505    filename : str
1506        Path to the GRIB2 file.
1507    drop_variables : list, optional
1508        List of variables to exclude.
1509    filters : dict, optional
1510        Filter criteria for GRIB2 messages.
1511    engine : str, optional
1512        Engine to use for opening the file, defaults to "grib2io".
1513    chunks : int, dict or 'auto', optional
1514        If chunks is provided, it is used to load the dataset into a
1515        dask-backed dataset.
1516    **kwargs : optional
1517        Additional keyword arguments passed to the xarray backend.
1518
1519    Returns
1520    -------
1521    xarray.DataTree
1522        A hierarchical DataTree representation of the GRIB2 data.
1523    """
1524    if not _HAS_DATATREE:
1525        raise ImportError("xarray version does not support DataTree functionality.")
1526
1527    if filters is None:
1528        filters = {}
1529
1530    # Open the file without any filters first to get all messages
1531    with grib2io.open(filename, _xarray_backend=True) as f:
1532        file_index = pd.DataFrame(f._index)
1533        file_index = file_index.assign(msg=msgs_from_index(f._index))
1534
1535    # Build tree structure from GRIB messages
1536    root = build_datatree_from_grib(
1537        filename,
1538        file_index,
1539        filters,
1540        drop_variables=drop_variables,
1541        chunks=chunks,
1542    )
1543
1544    # Update history for provenance
1545    now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
1546    existing_history = root.attrs.get("history", "") if hasattr(root, "attrs") else ""
1547    history = f"{now}: Initialized via grib2io.open_datatree from {filename}\n{existing_history}"
1548    if hasattr(root, "attrs"):
1549        root.attrs["history"] = history
1550    # Also add to all datasets in the tree
1551    for node in root.subtree:
1552        if node.ds is not None:
1553            node.ds.attrs["history"] = history + node.ds.attrs.get("history", "")
1554
1555    return root
1556
1557    return root
1558
1559
1560def build_da_without_coords(index: pd.DataFrame, cube: dict, filename: str, attrs: dict) -> xr.DataArray:
1561    """
1562    Build a DataArray without coordinates from a cube of grib2 messages.
1563
1564    Parameters
1565    ----------
1566    index : pd.DataFrame
1567        Index of cube.
1568    cube : dict
1569        Cube of grib2 messages.
1570    filename : str
1571        Filename of grib2 file.
1572    attrs : dict
1573        Attributes for the DataArray.
1574
1575    Returns
1576    -------
1577    xr.DataArray
1578        DataArray without coordinates.
1579    """
1580
1581    dim_names = [k for k in cube.keys() if cube[k] is not None and len(cube[k]) > 1]
1582    constant_meta_names = [k for k in cube.keys() if cube[k] is None]
1583    dims = {k: len(cube[k]) for k in dim_names}
1584
1585    # guard against bad datarrays being formed
1586    dims_total = 1
1587    dims_to_filter = []
1588    for (
1589        dim_name,
1590        dim_len,
1591    ) in dims.items():
1592        if dim_name not in {"x", "y", "station"}:
1593            dims_total *= dim_len
1594            dims_to_filter.append(dim_name)
1595
1596    # Check number of GRIB2 message indexed compared to non-X/Y
1597    # dimensions.
1598    if dims_total != len(index):
1599        raise ValueError(
1600            f"DataArray dimensions are not compatible with number of GRIB2 messages; DataArray has {dims_total} "
1601            f"and GRIB2 index has {len(index)}. Consider applying a filter for dimensions: {dims_to_filter}"
1602        )
1603
1604    data = OnDiskArray(filename, index, cube)
1605    lock = _LOCK
1606    data = GribBackendArray(data, lock)
1607    data = indexing.LazilyIndexedArray(data)
1608    if len(dim_names) != len(data.shape):
1609        raise ValueError(
1610            "different number of dimensions on data "
1611            f"and dims: {len(data.shape)} vs {len(dim_names)}\n"
1612            "Grib2 messages could not be formed into a data cube; "
1613            "It's possible extra messages exist along a non-accounted for dimension based on PDTN\n"
1614            "It might be possible to get around this by applying a filter on the non-accounted for dimension"
1615        )
1616    da = xr.DataArray(data, dims=dim_names)
1617
1618    da.encoding["original_shape"] = data.shape
1619
1620    da.encoding["preferred_chunks"] = {"y": -1, "x": -1}
1621    msg1 = index.msg.iloc[0]
1622
1623    # plain language metadata is minimized
1624    # add grib section metadata
1625    da.attrs["GRIB2IO_section0"] = msg1.section0
1626    da.attrs["GRIB2IO_section1"] = msg1.section1
1627    da.attrs["GRIB2IO_section2"] = msg1.section2 if msg1.section2 else []
1628    da.attrs["GRIB2IO_section3"] = msg1.section3
1629    da.attrs["GRIB2IO_section4"] = msg1.section4
1630    da.attrs["GRIB2IO_section5"] = msg1.section5
1631    da.attrs["fullName"] = str(msg1.fullName)
1632    da.attrs["shortName"] = str(msg1.shortName)
1633    da.attrs["units"] = str(msg1.units)
1634    da.attrs["originatingCenter"] = str(msg1.originatingCenter.definition)
1635    da.attrs["originatingSubCenter"] = str(msg1.originatingSubCenter.definition)
1636
1637    # add master table
1638    da.attrs["masterTableInfo"] = str(msg1.masterTableInfo.definition)
1639
1640    da.name = index.shortName.iloc[0]
1641    for meta_name in constant_meta_names:
1642        if meta_name in index.columns:
1643            da.attrs[meta_name] = index[meta_name].iloc[0]
1644
1645    da.attrs.update(attrs)
1646
1647    return da
1648
1649
1650def assign_xr_meta(
1651    ds: xr.Dataset,
1652    frames: typing.List[pd.DataFrame],
1653    cube: dict,
1654    non_geo_dims: typing.Dict[str, typing.List[str]],
1655    extra_geo: dict,
1656    coord_attrs: typing.Dict[str, dict],
1657) -> xr.Dataset:
1658    """
1659    Assign coordinates and attributes to the dataset.
1660
1661    Parameters
1662    ----------
1663    ds : xr.Dataset
1664        The dataset to update.
1665    frames : list of pd.DataFrame
1666        The dataframes for each variable.
1667    cube : dict
1668        The dimensions cube.
1669    non_geo_dims : dict
1670        The non-geographic dimensions.
1671    extra_geo : dict
1672        Extra geographic coordinates.
1673    coord_attrs : dict
1674        Attributes for coordinates.
1675
1676    Returns
1677    -------
1678    xr.Dataset
1679        The updated dataset.
1680    """
1681    df = frames[0]
1682
1683    # assign extra geo coords
1684    ds = ds.assign_coords(extra_geo)
1685    # add crs data from first grib message to each data variable and the dataset
1686    geo_attrs = {
1687        "crs_wkt": CRS.from_dict(df.msg.iloc[0].projParameters).to_wkt(),
1688        "gridlengthXDirection": df.msg.iloc[0].gridlengthXDirection,
1689        "gridlengthYDirection": df.msg.iloc[0].gridlengthYDirection,
1690        "latitudeFirstGridpoint": df.msg.iloc[0].latitudeFirstGridpoint,
1691        "longitudeFirstGridpoint": df.msg.iloc[0].longitudeFirstGridpoint,
1692    }
1693    for data_var in ds.data_vars:
1694        ds[data_var].attrs.update(geo_attrs)
1695    ds.attrs.update(geo_attrs)
1696
1697    # add coordinate specific attributes
1698    for coord, attrs in coord_attrs.items():
1699        ds[coord].attrs.update(attrs)
1700
1701    # assign valid date coords
1702    try:
1703        ds = ds.assign_coords(dict(validDate=ds.coords["refDate"] + ds.coords["leadTime"]))
1704        ds.validDate.attrs["standard_name"] = "time"
1705        ds.validDate.attrs["long_name"] = "time"
1706    except Exception as e:
1707        warnings.warn(f"could not parse validTime: {e}")
1708
1709    # assign attributes
1710    ds.attrs["engine"] = "grib2io"
1711
1712    return ds
1713
1714
1715def make_variables(
1716    index: pd.DataFrame,
1717    f: str,
1718    non_geo_dims: typing.Dict[str, typing.List[str]],
1719    allow_uneven_dims: bool = False,
1720) -> typing.Tuple[
1721    typing.Optional[typing.List[pd.DataFrame]],
1722    typing.Optional[typing.List[dict]],
1723    typing.Optional[dict],
1724]:
1725    """
1726    Create an individual dataframe index and cube for each variable.
1727
1728    Parameters
1729    ----------
1730    index : pd.DataFrame
1731        Index of messages.
1732    f : str
1733        Filename.
1734    non_geo_dims : dict
1735        Dimensions not associated with the x,y grid.
1736    allow_uneven_dims : bool, optional
1737        If True, allows uneven dimensions (used for DataTree creation).
1738
1739    Returns
1740    -------
1741    ordered_frames : list of pd.DataFrame, optional
1742        List of dataframes, one for each variable.
1743    cubes : list of dict, optional
1744        List of cubes, one for each variable.
1745    extra_geo : dict, optional
1746        Extra geographic coordinates.
1747    """
1748    # let shortName determine the variables
1749
1750    # set the index to the name
1751    index = index.set_index("shortName").sort_index()
1752    # return nothing if no data
1753    if index.empty:
1754        return None, None, None
1755
1756    # define the DimCube
1757    dims = copy(non_geo_dims)
1758
1759    ordered_meta = list(non_geo_dims.keys())
1760    cubes = list()
1761    ordered_frames = list()
1762    for key in index.index.unique():
1763        frame = index.loc[[key]]
1764        frame = frame.reset_index()
1765        # frame is a dataframe with all records for one variable
1766        c = dict()
1767        # for colname in frame.columns:
1768        for colname in ordered_meta:
1769            uniques = pd.Index(frame[colname]).unique()
1770            if len(uniques) > 1:
1771                c[colname] = uniques.sort_values()
1772            else:
1773                c[colname] = [uniques[0]]
1774
1775        dims = [k for k in ordered_meta if len(c[k]) > 1]
1776
1777        for dim in dims:
1778            if frame[dim].value_counts().nunique() > 1 and not allow_uneven_dims:
1779                raise ValueError(f"uneven number of grib msgs associated with dimension: {dim}\n unique values for {dim}: {frame[dim].unique()} ")
1780
1781        if len(dims) >= 1:  # dims may be empty if no extra dims on top of x,y
1782            frame = frame.sort_values(dims)
1783            frame = frame.set_index(dims)
1784
1785        cubes.append(c)
1786
1787        # miloc is multi-index integer location of msg in nd DataArray
1788        miloc = list(zip(*[frame.index.unique(level=dim).get_indexer(frame.index.get_level_values(dim)) for dim in dims]))
1789
1790        # set frame multi index
1791        if len(miloc) >= 1:  # miloc will be empty when no extra dims, thus no multiindex
1792            dim_ix = tuple([n + "_ix" for n in dims])
1793            frame = frame.set_index(pd.MultiIndex.from_tuples(miloc, names=dim_ix))
1794
1795        ordered_frames.append(frame)
1796
1797    # no variables
1798    if not cubes:
1799        cubes = [dict()]
1800
1801    # check geography of data and assign to cube
1802    if len(index.ny.unique()) > 1 or len(index.nx.unique()) > 1:
1803        raise ValueError("multiple grids not accommodated")
1804    for cube in cubes:
1805        cube["y"] = range(int(index.ny.iloc[0]))
1806        cube["x"] = range(int(index.nx.iloc[0]))
1807
1808    extra_geo = None
1809    msg = index.msg.iloc[0]
1810
1811    # we want the lat lons; make them via accessing a record; we are assuming
1812    # all records are the same grid because they have the same shape;
1813    # may want a unique grid identifier from grib2io to avoid assuming this
1814    latitude, longitude = msg.latlons()
1815    latitude = xr.DataArray(latitude, dims=["y", "x"])
1816    latitude.attrs["standard_name"] = "latitude"
1817    latitude.attrs["units"] = "degrees_north"
1818    longitude = xr.DataArray(longitude, dims=["y", "x"])
1819    longitude.attrs["standard_name"] = "longitude"
1820    longitude.attrs["units"] = "degrees_east"
1821    extra_geo = dict(latitude=latitude, longitude=longitude)
1822
1823    return ordered_frames, cubes, extra_geo
1824
1825
1826def interp_nd(
1827    a: np.ndarray,
1828    *,
1829    method: typing.Union[str, int],
1830    grid_def_in: grib2io.Grib2GridDef,
1831    grid_def_out: grib2io.Grib2GridDef,
1832    method_options: typing.Optional[typing.List[int]] = None,
1833    num_threads: int = 1,
1834) -> np.ndarray:
1835    """
1836    Perform multi-dimensional interpolation on a horizontal grid.
1837
1838    This function reshapes the input array to (N, ny, nx) before performing
1839    interpolation and then reshapes it back to its original dimensions plus
1840    the new grid dimensions.
1841
1842    Parameters
1843    ----------
1844    a : np.ndarray
1845        Input array with horizontal dimensions (..., ny, nx).
1846    method : str or int
1847        Interpolation method.
1848    grid_def_in : grib2io.Grib2GridDef
1849        Input grid definition.
1850    grid_def_out : grib2io.Grib2GridDef
1851        Output grid definition.
1852    method_options : list of int, optional
1853        Interpolation options.
1854    num_threads : int, optional
1855        Number of threads for parallel interpolation.
1856
1857    Returns
1858    -------
1859    np.ndarray
1860        Interpolated array with horizontal dimensions of the output grid.
1861    """
1862    front_shape = a.shape[:-2]
1863    a = a.reshape(-1, a.shape[-2], a.shape[-1])
1864    a = grib2io.interpolate(
1865        a,
1866        method,
1867        grid_def_in,
1868        grid_def_out,
1869        method_options=method_options,
1870        num_threads=num_threads,
1871    )
1872    a = a.reshape(front_shape + (a.shape[-2], a.shape[-1]))
1873    return a
1874
1875
1876def interp_nd_stations(
1877    a: np.ndarray,
1878    *,
1879    method: typing.Union[str, int],
1880    grid_def_in: grib2io.Grib2GridDef,
1881    lats: typing.Sequence[float],
1882    lons: typing.Sequence[float],
1883    method_options: typing.Optional[typing.List[int]] = None,
1884    num_threads: int = 1,
1885) -> np.ndarray:
1886    """
1887    Perform multi-dimensional interpolation to station points.
1888
1889    This function reshapes the input array to (N, ny, nx) before performing
1890    interpolation and then reshapes it back to its original dimensions plus
1891    the station dimension.
1892
1893    Parameters
1894    ----------
1895    a : np.ndarray
1896        Input array with horizontal dimensions (..., ny, nx).
1897    method : str or int
1898        Interpolation method.
1899    grid_def_in : grib2io.Grib2GridDef
1900        Input grid definition.
1901    lats : sequence of float
1902        Station latitudes.
1903    lons : sequence of float
1904        Station longitudes.
1905    method_options : list of int, optional
1906        Interpolation options.
1907    num_threads : int, optional
1908        Number of threads for parallel interpolation.
1909
1910    Returns
1911    -------
1912    np.ndarray
1913        Interpolated array with the last dimension representing stations.
1914    """
1915    front_shape = a.shape[:-2]
1916    a = a.reshape(-1, a.shape[-2], a.shape[-1])
1917    a = grib2io.interpolate_to_stations(
1918        a,
1919        method,
1920        grid_def_in,
1921        lats,
1922        lons,
1923        method_options=method_options,
1924        num_threads=num_threads,
1925    )
1926    a = a.reshape(front_shape + (len(lats),))
1927    return a
1928
1929
1930@xr.register_dataset_accessor("grib2io")
1931class Grib2ioDataSet:
1932    def __init__(self, xarray_obj):
1933        self._obj = xarray_obj
1934
1935    def griddef(self):
1936        return Grib2GridDef.from_section3(self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"])
1937
1938    def interp(
1939        self,
1940        method: typing.Union[str, int],
1941        grid_def_out: grib2io.Grib2GridDef,
1942        method_options: typing.Optional[typing.List[int]] = None,
1943        num_threads: int = 1,
1944    ) -> xr.Dataset:
1945        """
1946        Perform grid spatial interpolation on all variables in the Dataset.
1947
1948        Parameters
1949        ----------
1950        method : str or int
1951            Interpolation method.
1952        grid_def_out : grib2io.Grib2GridDef
1953            Output grid definition.
1954        method_options : list of int, optional
1955            Interpolation options.
1956        num_threads : int, optional
1957            Number of threads.
1958
1959        Returns
1960        -------
1961        xarray.Dataset
1962            Interpolated dataset.
1963        """
1964        da = self._obj.to_array()
1965        da.attrs["GRIB2IO_section3"] = self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"]
1966        da = da.grib2io.interp(method, grid_def_out, method_options=method_options, num_threads=num_threads)
1967        ds = da.to_dataset(dim="variable")
1968
1969        # Update history for provenance
1970        history = ds.attrs.get("history", "")
1971        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
1972        ds.attrs["history"] = f"{now}: Interpolated via {method} to {grid_def_out}\n{history}"
1973
1974        return ds
1975
1976    def interp_to_stations(
1977        self,
1978        method: typing.Union[str, int],
1979        calls: typing.Sequence[str],
1980        lats: typing.Sequence[float],
1981        lons: typing.Sequence[float],
1982        method_options: typing.Optional[typing.List[int]] = None,
1983        num_threads: int = 1,
1984    ) -> xr.Dataset:
1985        """
1986        Perform spatial interpolation to station points on all variables.
1987
1988        Parameters
1989        ----------
1990        method : str or int
1991            Interpolation method.
1992        calls : sequence of str
1993            Station call signs.
1994        lats : sequence of float
1995            Station latitudes.
1996        lons : sequence of float
1997            Station longitudes.
1998        method_options : list of int, optional
1999            Interpolation options.
2000        num_threads : int, optional
2001            Number of threads.
2002
2003        Returns
2004        -------
2005        xarray.Dataset
2006            Dataset interpolated to stations.
2007        """
2008        da = self._obj.to_array()
2009        da.attrs["GRIB2IO_section3"] = self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"]
2010        da = da.grib2io.interp_to_stations(
2011            method,
2012            calls,
2013            lats,
2014            lons,
2015            method_options=method_options,
2016            num_threads=num_threads,
2017        )
2018        ds = da.to_dataset(dim="variable")
2019
2020        # Update history for provenance
2021        history = ds.attrs.get("history", "")
2022        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2023        ds.attrs["history"] = f"{now}: Interpolated to {len(calls)} stations via {method}\n{history}"
2024
2025        return ds
2026
2027    def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
2028        """
2029        Write a DataSet to a grib2 file.
2030
2031        Parameters
2032        ----------
2033        filename
2034            Name of the grib2 file to write to.
2035        mode: {"x", "w", "a"}, optional, default="x"
2036            Persistence mode
2037
2038            | mode | Description                       |
2039            | :---:| :---:                             |
2040            | 'x'  | create (fail if exists)           |
2041            | 'w'  | create (overwrite if exists)      |
2042            | 'a'  | append (create if does not exist) |
2043
2044        """
2045        ds = self._obj
2046
2047        for shortName in sorted(ds):
2048            # make a DataArray from the "Data Variables" in the DataSet
2049            da = ds[shortName]
2050
2051            da.grib2io.to_grib2(filename, mode=mode)
2052            mode = "a"
2053
2054    def update_attrs(self, **kwargs):
2055        """
2056        Raises an error because Datasets don't have a .attrs attribute.
2057
2058        Parameters
2059        ----------
2060        attrs
2061            Attributes to update.
2062        """
2063        raise ValueError(f"Datasets do not have a .attrs attribute; use .grib2io.update_attrs({kwargs}) on a DataArray instead.")
2064
2065    def subset(self, *, lats=None, lons=None) -> xr.Dataset:
2066        """
2067        Subset the Dataset to a box defined by latitudes and/or longitudes.
2068
2069        Parameters
2070        ----------
2071        lats
2072            Two item list or tuple of latitudes.  Default is None which will
2073            return a subset unbounded by latitude.  The first term defines the
2074            southern boundary and the second term defines the northern
2075            boundary.
2076        lons
2077            Two item list or tuple of longitudes.  Default is None which will
2078            return a subset unbounded by longitude.  The first term defines the
2079            western boundary and the second term defines the eastern
2080            boundary.  Can follow either: 0 to 360 postive eastward, or 0 to
2081            -180 westward / 0 to 180 eastward conventions.  The longitude
2082            boundaries cannot cross 0.
2083
2084        Returns
2085        -------
2086        subset
2087            Dataset subset to the bounding box created by input 'lats'/'lons'.
2088            All gridpoints with lat/lon matching contraints are included within
2089            subset.
2090        """
2091        ds = self._obj
2092
2093        newds = xr.Dataset()
2094        for shortName in ds:
2095            newds[shortName] = ds[shortName].grib2io.subset(lats=lats, lons=lons).copy()
2096
2097        # Update history for provenance
2098        history = newds.attrs.get("history", "")
2099        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2100        newds.attrs["history"] = f"{now}: Subsetted to lats={lats}, lons={lons}\n{history}"
2101
2102        return newds
2103
2104    def compute(self, **kwargs):
2105        """
2106        Compute the Dask-backed Dataset with retries for transient errors.
2107
2108        Wraps :func:`grib2io.utils.compute_with_retries`.
2109
2110        Parameters
2111        ----------
2112        **kwargs
2113            Arguments passed to :func:`grib2io.utils.compute_with_retries`,
2114            e.g., `max_attempts` or `base_sleep`.
2115
2116        Returns
2117        -------
2118        xarray.Dataset
2119            The computed Dataset with NumPy-backed data.
2120        """
2121        from .utils import compute_with_retries
2122
2123        return compute_with_retries(self._obj, **kwargs)
2124
2125
2126@xr.register_dataarray_accessor("grib2io")
2127class Grib2ioDataArray:
2128    def __init__(self, xarray_obj):
2129        self._obj = xarray_obj
2130
2131    def griddef(self):
2132        return Grib2GridDef.from_section3(self._obj.attrs["GRIB2IO_section3"])
2133
2134    def interp(self, method, grid_def_out, method_options=None, num_threads=1) -> xr.DataArray:
2135        """
2136        Perform grid spatial interpolation.
2137
2138        Uses the [NCEPLIBS-ip library](https://github.com/NOAA-EMC/NCEPLIBS-ip).
2139
2140        Parameters
2141        ----------
2142        method
2143            Interpolate method to use. This can either be an integer or string
2144            using the following mapping:
2145
2146            | Interpolate Scheme | Integer Value |
2147            | :---:              | :---:         |
2148            | 'bilinear'         | 0             |
2149            | 'bicubic'          | 1             |
2150            | 'neighbor'         | 2             |
2151            | 'budget'           | 3             |
2152            | 'spectral'         | 4             |
2153            | 'neighbor-budget'  | 6             |
2154        grid_def_out
2155            Grib2GridDef object of the output grid.
2156        method_options : list of ints, optional
2157            Interpolation options. See the NCEPLIBS-ip documentation for
2158            more information on how these are used.
2159        num_threads : int, optional
2160            Number of OpenMP threads to use for interpolation. The default
2161            value is 1. If grib2io_interp was not built with OpenMP, then
2162            this keyword argument and value will have no impact.
2163
2164        Returns
2165        -------
2166        interp
2167            DataSet interpolated to new grid definition.  The attribute
2168            GRIB2IO_section3 is replaced with the section3 array from the new
2169            grid definition.
2170        """
2171        da = self._obj
2172        # ensure that y, x are rightmost dims; they should be if opening with
2173        # grib2io engine
2174
2175        # gdtn and gdt is not the entirety of the new s3
2176        npoints = grid_def_out.npoints
2177        s3_new = np.array([0, npoints, 0, 0, grid_def_out.gdtn] + list(grid_def_out.gdt))
2178
2179        # make new lat lons
2180        lats, lons = Grib2Message(section3=s3_new, pdtn=0, drtn=0).grid()
2181        latitude = xr.DataArray(lats, dims=["y", "x"])
2182        longitude = xr.DataArray(lons, dims=["y", "x"])
2183
2184        # create new coords
2185        new_coords = dict(da.coords)
2186        del new_coords["latitude"]
2187        del new_coords["longitude"]
2188        new_coords["longitude"] = longitude
2189        new_coords["latitude"] = latitude
2190
2191        # make grid def in from section3 on da.attrs
2192        grid_def_in = self.griddef()
2193
2194        if da.chunks is None:
2195            data = interp_nd(
2196                da.data,
2197                method=method,
2198                grid_def_in=grid_def_in,
2199                grid_def_out=grid_def_out,
2200                method_options=method_options,
2201                num_threads=num_threads,
2202            )
2203        else:
2204            data = da.data.map_blocks(
2205                interp_nd,
2206                method=method,
2207                grid_def_in=grid_def_in,
2208                grid_def_out=grid_def_out,
2209                method_options=method_options,
2210                chunks=da.chunks[:-2] + latitude.shape,
2211                dtype=da.dtype,
2212            )
2213
2214        new_da = xr.DataArray(data, dims=da.dims, coords=new_coords, attrs=da.attrs)
2215
2216        new_da.attrs["GRIB2IO_section3"] = s3_new
2217        new_da.name = da.name
2218
2219        # Update history for provenance
2220        history = new_da.attrs.get("history", "")
2221        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2222        new_da.attrs["history"] = f"{now}: Interpolated via {method} to {grid_def_out}\n{history}"
2223
2224        return new_da
2225
2226    def interp_to_stations(
2227        self,
2228        method: typing.Union[str, int],
2229        calls: typing.Sequence[str],
2230        lats: typing.Sequence[float],
2231        lons: typing.Sequence[float],
2232        method_options: typing.Optional[typing.List[int]] = None,
2233        num_threads: int = 1,
2234    ) -> xr.DataArray:
2235        """
2236        Perform spatial interpolation to station points.
2237
2238        Parameters
2239        ----------
2240        method : str or int
2241            Interpolate method to use. This can either be an integer or string
2242            using the following mapping:
2243
2244            | Interpolate Scheme | Integer Value |
2245            | :---:              | :---:         |
2246            | 'bilinear'         | 0             |
2247            | 'bicubic'          | 1             |
2248            | 'neighbor'         | 2             |
2249            | 'budget'           | 3             |
2250            | 'spectral'         | 4             |
2251            | 'neighbor-budget'  | 6             |
2252
2253        calls : sequence of str
2254            Station calls used for labeling new station index coordinate
2255        lats : sequence of float
2256            Latitudes of the station points.
2257        lons : sequence of float
2258            Longitudes of the station points.
2259        method_options : list of int, optional
2260            Interpolation options.
2261        num_threads : int, optional
2262            Number of threads.
2263
2264        Returns
2265        -------
2266        xarray.DataArray
2267            DataArray interpolated to lat and lon locations and labeled with
2268            dimension and coordinate 'station'. (..., y, x) -> (..., station)
2269        """
2270        da = self._obj
2271        # TODO ensure that y, x are rightmost dims; they should be if opening
2272        # with grib2io engine
2273
2274        calls = np.asarray(calls)
2275        lats = np.asarray(lats)
2276        lons = np.asarray(lons)
2277        latitude = xr.DataArray(lats, dims=["station"])
2278        longitude = xr.DataArray(lons, dims=["station"])
2279
2280        # create new coords
2281        new_coords = dict(da.coords)
2282        del new_coords["latitude"]
2283        del new_coords["longitude"]
2284        new_coords["longitude"] = longitude
2285        new_coords["latitude"] = latitude
2286        new_coords["station"] = calls
2287
2288        new_dims = da.dims[:-2] + ("station",)
2289
2290        # make grid def in from section3 on da attrs
2291        grid_def_in = self.griddef()
2292
2293        if da.chunks is None:
2294            data = interp_nd_stations(
2295                da.data,
2296                method=method,
2297                grid_def_in=grid_def_in,
2298                lats=lats,
2299                lons=lons,
2300                method_options=method_options,
2301                num_threads=num_threads,
2302            )
2303        else:
2304            data = da.data.map_blocks(
2305                interp_nd_stations,
2306                method=method,
2307                grid_def_in=grid_def_in,
2308                lats=lats,
2309                lons=lons,
2310                method_options=method_options,
2311                drop_axis=-1,
2312                chunks=da.chunks[:-2] + latitude.shape,
2313                dtype=da.dtype,
2314            )
2315
2316        new_da = xr.DataArray(data, dims=new_dims, coords=new_coords, attrs=da.attrs)
2317
2318        new_da.name = da.name
2319
2320        # Update history for provenance
2321        history = new_da.attrs.get("history", "")
2322        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2323        new_da.attrs["history"] = f"{now}: Interpolated to {len(calls)} stations via {method}\n{history}"
2324
2325        return new_da
2326
2327    def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
2328        """
2329        Write a DataArray to a grib2 file.
2330
2331        Parameters
2332        ----------
2333        filename
2334            Name of the grib2 file to write to.
2335        mode: {"x", "w", "a"}, optional, default="x"
2336            Persistence mode
2337
2338            +------+-----------------------------------+
2339            | mode | Description                       |
2340            +======+===================================+
2341            | x    | create (fail if exists)           |
2342            +------+-----------------------------------+
2343            | w    | create (overwrite if exists)      |
2344            +------+-----------------------------------+
2345            | a    | append (create if does not exist) |
2346            +------+-----------------------------------+
2347
2348        """
2349        da = self._obj.copy(deep=True)
2350
2351        coords_keys = sorted(da.coords.keys())
2352        coords_keys = [k for k in coords_keys if k in AVAILABLE_NON_GEO_COORDS]
2353
2354        # If there are dimension coordinates, the DataArray is a hypercube of
2355        # grib2 messages.
2356
2357        # Create `indexes` which is a list of lists of dictionaries for all
2358        # dimension coordinates. Each dictionary key is the dimension
2359        # coordinate name and the value is a list of the dimension coordinate
2360        # values.  This allows for easy iteration over all possible grib2
2361        # messages in the DataArray by using itertools.product.
2362        #
2363        # For example:
2364        # indexes = [
2365        #     [
2366        #         {"leadTime": 9},
2367        #         {"leadTime": 12},
2368        #     ],
2369        #     [
2370        #         {"valueOfFirstFixedSurface": 900},
2371        #         {"valueOfFirstFixedSurface": 925},
2372        #         {"valueOfFirstFixedSurface": 950},
2373        #     ],
2374        # ]
2375
2376        # assign loc indexes to dimensions without indexes for uniform selection by name
2377        loc_indexes = list()
2378        for dim in da.dims:
2379            if dim not in da.indexes:
2380                da = da.assign_coords({dim: range(da[dim].size)})
2381                loc_indexes.append(dim)
2382
2383        indexes = []
2384        for index in [i for i in AVAILABLE_NON_GEO_DIMS if i in da.dims]:
2385            values = da.coords[index].values
2386            if len(values) != len(set(values)):
2387                raise ValueError(
2388                    f"Dimension coordinate '{index}' has duplicate values, but to_grib2 requires unique values to find each GRIB2 message in the DataArray."
2389                )
2390            listeach = [{index: value} for value in sorted(values)]
2391            indexes.append(listeach)
2392
2393        # If `dim_coords` is [], then the DataArray is a single grib2 message and
2394        # itertools.product(*dim_coords) will run once with `selectors = ()`.
2395        for selectors in itertools.product(*indexes):
2396            # Need to find the correct data in the DataArray based on the
2397            # dimension coordinates.
2398            filters = {k: v for d in selectors for k, v in d.items()}
2399
2400            # If `filters` is {}, then the DataArray is a single grib2 message
2401            # and da.sel(indexers={}) returns the DataArray.
2402            selected = da.sel(indexers=filters)
2403
2404            newmsg = Grib2Message(
2405                selected.attrs["GRIB2IO_section0"],
2406                selected.attrs["GRIB2IO_section1"],
2407                selected.attrs["GRIB2IO_section2"],
2408                selected.attrs["GRIB2IO_section3"],
2409                selected.attrs["GRIB2IO_section4"],
2410                selected.attrs["GRIB2IO_section5"],
2411            )
2412            newmsg.data = np.array(selected.data)
2413
2414            # For dimension coordinates, set the grib2 message metadata to the
2415            # dimension coordinate value.
2416            for index, value in filters.items():
2417                if index not in loc_indexes:
2418                    setattr(newmsg, index, value)
2419
2420            # For non-dimension coordinates, set the grib2 message metadata to
2421            # the DataArray coordinate value.
2422            for index in [i for i in coords_keys if i not in da.dims]:
2423                setattr(newmsg, index, selected.coords[index].values)
2424
2425            # Set section 5 attributes to the da.encoding dictionary.
2426            for key, value in selected.encoding.items():
2427                if key in ["dtype", "chunks", "original_shape"]:
2428                    continue
2429                setattr(newmsg, key, value)
2430
2431            # write the message to file
2432            with grib2io.open(filename, mode=mode) as f:
2433                f.write(newmsg)
2434            mode = "a"
2435
2436    def update_attrs(self, **kwargs):
2437        """
2438        Update many of the attributes of the DataArray.
2439
2440        Parameters
2441        ----------
2442        **kwargs
2443            Attributes to update.  This can include many of the GRIB2IO message
2444            attributes that you can find when you print a GRIB2IO message. For
2445            conflicting updates, the last keyword will be used.
2446
2447            +-----------------------+------------------------------------------+
2448            | kwargs                | Description                              |
2449            +=======================+==========================================+
2450            | shortName="VTMP"      | Set shortName to "VTMP", along with      |
2451            |                       | appropriate discipline,                  |
2452            |                       | parameterCategory, parameterNumber,      |
2453            |                       | fullName and units.                      |
2454            +-----------------------+------------------------------------------+
2455            | discipline=0,         | Set shortName, discipline,               |
2456            | parameterCategory=0,  | parameterCategory, parameterNumber,      |
2457            | parameterNumber=1     | fullName and units appropriate for       |
2458            |                       | "Virtual Temperature".                   |
2459            +-----------------------+------------------------------------------+
2460            | discipline=0,         | Conflicting keywords but                 |
2461            | parameterCategory=0,  | 'shortName="TMP"' wins.  Set shortName,  |
2462            | parameterNumber=1,    | discipline, parameterCategory,           |
2463            | shortName="TMP"       | parameterNumber, fullName and units      |
2464            |                       | appropriate for "Temperature".           |
2465            +-----------------------+------------------------------------------+
2466
2467        Returns
2468        -------
2469        DataArray
2470            DataArray with updated attributes.
2471        """
2472        da = self._obj.copy(deep=True)
2473
2474        newmsg = Grib2Message(
2475            da.attrs["GRIB2IO_section0"],
2476            da.attrs["GRIB2IO_section1"],
2477            da.attrs["GRIB2IO_section2"],
2478            da.attrs["GRIB2IO_section3"],
2479            da.attrs["GRIB2IO_section4"],
2480            da.attrs["GRIB2IO_section5"],
2481        )
2482
2483        coords_keys = [k for k in da.coords.keys() if k in AVAILABLE_NON_GEO_COORDS]
2484
2485        for grib2_name, value in kwargs.items():
2486            if grib2_name == "gridDefinitionTemplateNumber":
2487                raise ValueError(
2488                    "The gridDefinitionTemplateNumber attribute cannot be updated.  The best way to change to a different grid is to interpolate the data to a new grid using the grib2io interpolate functions."
2489                )
2490            if grib2_name == "productDefinitionTemplateNumber":
2491                raise ValueError("The productDefinitionTemplateNumber attribute cannot be updated.")
2492            if grib2_name == "dataRepresentationTemplateNumber":
2493                raise ValueError("The dataRepresentationTemplateNumber attribute cannot be updated.")
2494            if grib2_name in coords_keys:
2495                warnings.warn(f"Skipping attribute '{grib2_name}' because it is a coordinate. Use da.assign_coords() to change coordinate values.")
2496                continue
2497            if hasattr(newmsg, grib2_name):
2498                setattr(newmsg, grib2_name, value)
2499            else:
2500                warnings.warn(f"Skipping attribute '{grib2_name}' because it is not a valid GRIB2 attribute for this message and cannot be updated.")
2501                continue
2502
2503        da.attrs["GRIB2IO_section0"] = newmsg.section0
2504        da.attrs["GRIB2IO_section1"] = newmsg.section1
2505        da.attrs["GRIB2IO_section2"] = newmsg.section2 or []
2506        da.attrs["GRIB2IO_section3"] = newmsg.section3
2507        da.attrs["GRIB2IO_section4"] = newmsg.section4
2508        da.attrs["GRIB2IO_section5"] = newmsg.section5
2509        da.attrs["fullName"] = newmsg.fullName
2510        da.attrs["shortName"] = newmsg.shortName
2511        da.attrs["units"] = newmsg.units
2512
2513        return da
2514
2515    def update_section3(self) -> xr.DataArray:
2516        """
2517        Update section3 attributes based on the latitude and longitude corners.
2518
2519        This makes the GRIB2IO_section3 attribute consistent with the grid's
2520        new corners after a change in the spatial extent.
2521        """
2522        da = self._obj
2523        if "GRIB2IO_section3" not in da.attrs:
2524            raise ValueError(
2525                "DataArray has no attr 'GRIB2IO_section3'.  This function only works with Datasets/DataArrrays opened with the 'grib2io' backend."
2526            )
2527        if "latitude" not in da.coords:
2528            raise ValueError("DataArray has no coord 'latitude'")
2529        if "longitude" not in da.coords:
2530            raise ValueError("DataArray has no coord 'longitude'")
2531
2532        grid = Grid(da.attrs["GRIB2IO_section3"])
2533
2534        if grid.gdtn not in [0, 1, 10, 20, 30, 31, 40, 110]:
2535            raise ValueError(
2536                textwrap.dedent("""\
2537                    update_section3 only works for:
2538
2539                    Latitude/Longitude, Equidistant Cylindrical, or Plate Carree (gdtn=0)
2540                    Rotated Latitude/Longitude (gdtn=1)
2541                    Mercator (gdtn=10)
2542                    Polar Stereographic (gdtn=20)
2543                    Lambert Conformal (gdtn=30)
2544                    Albers Equal-Area (gdtn=31)
2545                    Gaussian Latitude/Longitude (gdtn=40)
2546                    Equatorial Azimuthal Equidistant Projection (gdtn=110)
2547                    """)
2548            )
2549
2550        grid.latitudeFirstGridpoint = da.latitude.isel(y=0, x=0)
2551        grid.longitudeFirstGridpoint = da.longitude.isel(y=0, x=0)
2552        grid.nx = len(da.x)
2553        grid.ny = len(da.y)
2554
2555        # last gridpoint does not affect section3 for some gdt but set anyway
2556        grid.latitudeLastGridpoint = da.latitude.isel(y=-1, x=-1)
2557        grid.longitudeLastGridpoint = da.longitude.isel(y=-1, x=-1)
2558
2559        da.attrs["GRIB2IO_section3"] = grid.section3
2560
2561        return da
2562
2563    def subset(self, *, lats=None, lons=None) -> xr.DataArray:
2564        """
2565        Subset the DataArray to a box defined by latitudes and/or longitudes.
2566
2567        Parameters
2568        ----------
2569        lats
2570            Two item list or tuple of latitudes.  Default is None which will
2571            return a subset unbounded by latitude.  The first term defines the
2572            southern boundary and the second term defines the northern
2573            boundary.
2574        lons
2575            Two item list or tuple of longitudes.  Default is None which will
2576            return a subset unbounded by longitude.  The first term defines the
2577            western boundary and the second term defines the eastern
2578            boundary.  Can follow either: 0 to 360 postive eastward, or 0 to
2579            -180 westward / 0 to 180 eastward conventions.  The longitude
2580            boundaries cannot cross 0.
2581
2582        Returns
2583        -------
2584        subset
2585            DataArray subset to the bounding box created by input 'lats'/'lons'.
2586            All gridpoints with lat/lon matching contraints are included within
2587            subset.
2588        """
2589
2590        def slice_from_contiguous_mask(mask):
2591            indices = np.where(mask)[0]
2592
2593            if indices.size > 0:
2594                # slice(start, stop) - stop is exclusive, so we add 1
2595                my_slice = slice(indices[0], indices[-1] + 1)
2596            else:
2597                my_slice = slice(0, 0)
2598            return my_slice
2599
2600        da = self._obj.copy()
2601
2602        if lats is None:
2603            lats = (np.min(da.latitude), np.max(da.latitude))
2604        else:
2605            lats = (min(lats), max(lats))
2606
2607        if lons is None:
2608            lons = (np.min(da.longitude), np.max(da.longitude))
2609        else:
2610            lons = (min(lons), max(lons))
2611
2612        # Internally work in common lon data representation (0->360 positive eastward from 0)
2613        lons = np.mod(np.array(lons) + 360, 360)
2614        lon_da = np.mod(da.longitude + 360, 360)
2615
2616        snap_first_point = snap_to_nearest_cell_center(da.latitude, lon_da, lats[0], lons[0])
2617        snap_last_point = snap_to_nearest_cell_center(da.latitude, lon_da, lats[1], lons[1])
2618        lats = (snap_first_point[0], snap_last_point[0])
2619        lons = (snap_first_point[1], snap_last_point[1])
2620
2621        x = ((lon_da >= lons[0]) & (lon_da <= lons[1])).any("y")
2622        if x.chunks:
2623            x = x.compute()
2624
2625        y = ((da.latitude >= lats[0]) & (da.latitude <= lats[1])).any("x")
2626        if y.chunks:
2627            y = y.compute()
2628
2629        y_slice = slice_from_contiguous_mask(y)
2630        x_slice = slice_from_contiguous_mask(x)
2631
2632        da = da.isel(y=y_slice, x=x_slice)
2633        if da.size < 1:
2634            raise ValueError("None of grid data is within given lat/lon bounds.")
2635
2636        da = da.grib2io.update_section3()
2637
2638        return da
2639
2640    def compute(self, **kwargs):
2641        """
2642        Compute the Dask-backed DataArray with retries for transient errors.
2643
2644        Wraps :func:`grib2io.utils.compute_with_retries`.
2645
2646        Parameters
2647        ----------
2648        **kwargs
2649            Arguments passed to :func:`grib2io.utils.compute_with_retries`,
2650            e.g., `max_attempts` or `base_sleep`.
2651
2652        Returns
2653        -------
2654        xarray.DataArray
2655            The computed DataArray with NumPy-backed data.
2656        """
2657        from .utils import compute_with_retries
2658
2659        return compute_with_retries(self._obj, **kwargs)
2660
2661
2662def open_mfdataset(
2663    filenames: typing.Union[str, typing.Sequence[str]],
2664    *,
2665    drop_variables: typing.Optional[typing.List[str]] = None,
2666    save_index: bool = True,
2667    filters: typing.Mapping[str, typing.Any] = dict(),
2668    data_model: typing.Optional[str] = None,
2669    parallel: bool = False,
2670    preprocess: typing.Optional[typing.Callable] = None,
2671    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
2672    **kwargs,
2673) -> xr.Dataset:
2674    """
2675    Open multiple GRIB2 files as a single xarray Dataset.
2676
2677    This function is optimized for GRIB2 files by combining their indices
2678    and creating a single Dataset, which is often much faster than
2679    using ``xarray.open_mfdataset``. It supports parallel index reading
2680    and dataset opening when ``parallel=True`` and ``dask`` is installed.
2681
2682    Parameters
2683    ----------
2684    filenames : str or sequence of str
2685        GRIB2 files to be opened. Can be a glob pattern.
2686    drop_variables : list of str, optional
2687        List of variables to exclude from the dataset.
2688    save_index : bool, optional
2689        Whether to save the GRIB2 index to a file (default is True).
2690    filters : dict, optional
2691        Filter GRIB2 messages to a single hypercube. Dictionary keys can be
2692        any GRIB2 metadata attribute name.
2693    data_model : str, optional
2694        Parse GRIB metadata following a defined data model convention
2695        (e.g., "nws-viz").
2696    parallel : bool, optional
2697        If True, use ``dask`` to read indices and open datasets in parallel.
2698        Requires the ``dask`` package.
2699    preprocess : callable, optional
2700        A function to apply to each file's dataset before combining.
2701    chunks : int, dict or 'auto', optional
2702        If chunks is provided, it is used to load the dataset into a
2703        dask-backed dataset.
2704    **kwargs : optional
2705        Additional arguments passed to the combination logic.
2706        If ``combine='nested'``, passed to ``xarray.combine_nested``.
2707        If ``combine='by_coords'``, passed to ``xarray.combine_by_coords``.
2708        If ``combine='merge'``, passed to ``xarray.merge``.
2709        If no ``combine`` argument is provided, the function attempts
2710        ``xarray.combine_by_coords`` followed by ``xarray.merge``.
2711
2712    Returns
2713    -------
2714    xarray.Dataset
2715        Xarray dataset of grib2 messages.
2716
2717    Notes
2718    -----
2719    - This function uses a "fast path" when ``preprocess=None`` and no
2720      combination ``**kwargs`` are provided, which concatenates all indices
2721      into a single global index before building the Dataset.
2722    - All files must share the same horizontal grid (ny, nx).
2723    """
2724    if isinstance(filenames, str):
2725        import glob
2726
2727        filenames = sorted(glob.glob(filenames))
2728
2729    storage_options = kwargs.pop("storage_options", None)
2730
2731    def _get_index(fname_and_index: typing.Tuple[str, int]) -> pd.DataFrame:
2732        """
2733        Internal utility to read GRIB2 index from a file.
2734
2735        Parameters
2736        ----------
2737        fname_and_index : tuple of (str, int)
2738            Tuple containing the filename and its position in the file list.
2739
2740        Returns
2741        -------
2742        pandas.DataFrame
2743            The GRIB2 index for the specified file.
2744        """
2745        fname, i = fname_and_index
2746        with grib2io.open(
2747            fname,
2748            save_index=save_index,
2749            _xarray_backend=True,
2750            **(storage_options or {}),
2751        ) as f:
2752            idx = pd.DataFrame(f._index)
2753            idx = idx.assign(msg=list(f))
2754            idx["file_index"] = i
2755            return idx
2756
2757    if parallel:
2758        try:
2759            import dask
2760            from dask.bag import from_sequence
2761
2762            indices = from_sequence(zip(filenames, range(len(filenames)))).map(_get_index).compute()
2763        except ImportError:
2764            warnings.warn("dask not installed, falling back to sequential index reading.")
2765            parallel = False
2766            indices = [_get_index((fname, i)) for i, fname in enumerate(filenames)]
2767    else:
2768        indices = [_get_index((fname, i)) for i, fname in enumerate(filenames)]
2769
2770    if not indices:
2771        return xr.Dataset()
2772
2773    # Validate grid consistency across files using only the first message of each file
2774    grid_cols = ["ny", "nx"]
2775    first_msgs = pd.concat([idx.head(1) for idx in indices], ignore_index=True)
2776    unique_grids = first_msgs[grid_cols].drop_duplicates()
2777    if len(unique_grids) > 1:
2778        grid_list = unique_grids.to_dict("records")
2779        raise ValueError(f"Multiple grids detected in open_mfdataset. All files must have the same grid. Found grids: {grid_list}")
2780
2781    # Determine if we can use the fast path (single index concatenation)
2782    # The fast path is only available if no preprocess is provided and no combination kwargs are used
2783    # that would require individual datasets (like concat_dim for nested combination)
2784    use_fast_path = preprocess is None and not kwargs
2785
2786    if not use_fast_path:
2787        if parallel:
2788            import dask
2789
2790            @dask.delayed
2791            def _open_delayed(idx, fname):
2792                return _open_dataset_from_index(
2793                    idx,
2794                    fname,
2795                    filters,
2796                    data_model,
2797                    drop_variables=drop_variables,
2798                    chunks=chunks,
2799                )
2800
2801            datasets = dask.compute(*[_open_delayed(idx, fname) for idx, fname in zip(indices, filenames)])
2802        else:
2803            datasets = [
2804                _open_dataset_from_index(
2805                    idx,
2806                    fname,
2807                    filters,
2808                    data_model,
2809                    drop_variables=drop_variables,
2810                    chunks=chunks,
2811                )
2812                for idx, fname in zip(indices, filenames)
2813            ]
2814
2815        if preprocess is not None:
2816            datasets = [preprocess(ds) for ds in datasets]
2817
2818        combine_opt = kwargs.pop("combine", None)
2819        if combine_opt == "nested":
2820            ds = xr.combine_nested(datasets, **kwargs)
2821        elif combine_opt == "by_coords":
2822            ds = xr.combine_by_coords(datasets, **kwargs)
2823        elif combine_opt == "merge":
2824            ds = xr.merge(datasets, **kwargs)
2825        else:
2826            # Default behavior: try by_coords, then merge
2827            try:
2828                ds = xr.combine_by_coords(datasets, **kwargs)
2829            except Exception:
2830                ds = xr.merge(datasets, **kwargs)
2831    else:
2832        file_index = pd.concat(indices, ignore_index=True)
2833        ds = _open_dataset_from_index(
2834            file_index,
2835            list(filenames),
2836            filters,
2837            data_model,
2838            drop_variables=drop_variables,
2839            chunks=chunks,
2840        )
2841
2842    # Update history for provenance
2843    history = ds.attrs.get("history", "")
2844    now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2845    ds.attrs["history"] = f"{now}: Initialized via grib2io.open_mfdataset from {len(filenames)} files\n{history}"
2846
2847    return ds
2848
2849
2850def _open_dataset_from_index(
2851    file_index: pd.DataFrame,
2852    filenames: typing.Union[str, typing.List[str]],
2853    filters: typing.Mapping[str, typing.Any] = dict(),
2854    data_model: typing.Optional[str] = None,
2855    drop_variables: typing.Optional[typing.List[str]] = None,
2856    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
2857) -> xr.Dataset:
2858    """
2859    Create an xarray Dataset from a GRIB2 index DataFrame.
2860
2861    This is an internal utility used by ``open_dataset`` and ``open_mfdataset``
2862    to build a Dataset structure from a pre-computed index of GRIB2 messages.
2863
2864    Parameters
2865    ----------
2866    file_index : pandas.DataFrame
2867        GRIB2 index DataFrame, expected to contain GRIB2 metadata and
2868        message pointers.
2869    filenames : str or list of str
2870        Path(s) to the GRIB2 file(s) referenced by the index.
2871    filters : dict, optional
2872        Filter GRIB2 messages to a single hypercube.
2873    data_model : str, optional
2874        Target data model for metadata normalization (e.g., "nws-viz").
2875    drop_variables : list of str, optional
2876        List of shortnames to exclude from the resulting Dataset.
2877    chunks : int, dict or 'auto', optional
2878        If chunks is provided, it is used to load the dataset into a
2879        dask-backed dataset.
2880
2881    Returns
2882    -------
2883    xarray.Dataset
2884        Dataset representing the GRIB2 messages.
2885    """
2886    # parse grib2io _index to dataframe and acquire non-geo possible dims
2887    # (scalar coord when not dim due to squeeze) parse_grib_index applies
2888    # filters to index and expands metadata based on product definition
2889    # template number
2890    file_index, dim_coords, attrs, coord_attrs = parse_grib_index(file_index, filters)
2891
2892    if drop_variables:
2893        file_index = file_index[~file_index["shortName"].isin(drop_variables)]
2894
2895    # Divide up records by variable
2896    frames, cubes, extra_geo = make_variables(file_index, filenames, dim_coords)  # have this return var_attrs
2897
2898    # return empty dataset if no data
2899    if frames is None:
2900        return xr.Dataset()
2901
2902    # create dataframe and add datarrays without any coords
2903    ds = xr.Dataset()
2904    for var_df, var_cube in zip(frames, cubes):
2905        da = build_da_without_coords(var_df, var_cube, filenames, attrs)
2906
2907        # Assign variable-specific coords from its cube
2908        coords = coords_from_cube(var_cube)
2909        da = da.assign_coords(coords)
2910
2911        # Assign extra index associated coords for this variable
2912        for dim_name, coord_names in dim_coords.items():
2913            retain_index_coord = False
2914            for name in coord_names:
2915                if name == dim_name:
2916                    retain_index_coord = True
2917                else:
2918                    if dim_name not in da.dims:
2919                        # for assigning scalar coords
2920                        coord_data = var_df[name].unique().item()
2921                        da = da.assign_coords({name: coord_data})
2922                    else:
2923                        # "ValueError: can only convert an array of size 1 to a Python scalar" indicates the coord is not compatible with the index
2924                        coord_data = [
2925                            var_df[var_df.index.get_level_values(f"{dim_name}_ix") == val][name].unique().item() for val in range(da[dim_name].size)
2926                        ]
2927                        coord = pd.Index(coord_data, name=dim_name)
2928                        da = da.assign_coords({name: (dim_name, coord)})
2929            if not retain_index_coord and dim_name in da.coords:
2930                da = da.drop_vars(dim_name)
2931
2932        ds[da.name] = da
2933
2934    # add coords and dataset meta
2935    # Pass first cube for common geo coords assignment
2936    ds = assign_xr_meta(ds, frames, cubes[0], dim_coords, extra_geo, coord_attrs)
2937
2938    if data_model is not None:
2939        ds = parse_data_model(ds, data_model)
2940
2941    # assign attributes
2942    ds.attrs["engine"] = "grib2io"
2943
2944    if chunks is not None:
2945        ds = ds.chunk(chunks)
2946
2947    return ds
2948
2949
2950def build_datatree_from_grib(
2951    filename: str,
2952    file_index: pd.DataFrame,
2953    filters: typing.Optional[typing.Mapping[str, typing.Any]] = None,
2954    stack_vertical: bool = False,
2955    drop_variables: typing.Optional[typing.List[str]] = None,
2956    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
2957) -> typing.Any:
2958    """
2959    Build a DataTree from GRIB2 messages.
2960
2961    This internal function organizes GRIB2 messages into a hierarchical
2962    tree structure based on level types, PDTNs, and other metadata.
2963
2964    Parameters
2965    ----------
2966    filename : str
2967        Path to the source GRIB2 file.
2968    file_index : pandas.DataFrame
2969        Index of GRIB2 messages.
2970    filters : dict, optional
2971        Filter criteria for selecting messages.
2972    stack_vertical : bool, optional
2973        If True, vertical levels will be stacked in a single dataset
2974        within each node, rather than creating separate nodes per level value.
2975    drop_variables : list of str, optional
2976        List of variable shortnames to exclude.
2977    chunks : int, dict or 'auto', optional
2978        If chunks is provided, it is used to load the dataset into a
2979        dask-backed dataset.
2980
2981    Returns
2982    -------
2983    xarray.DataTree
2984        A hierarchical tree representation of the GRIB2 data.
2985    """
2986    if filters is None:
2987        filters = {}
2988
2989    # Apply any filters from user
2990    for k, v in filters.items():
2991        if k not in file_index.columns:
2992            file_index = file_index.copy()
2993            file_index[k] = file_index.msg.apply(lambda msg: getattr(msg, k, None))
2994        file_index = filter_index(file_index, k, v)
2995
2996    # Make a copy to avoid the SettingWithCopyWarning
2997    file_index = file_index.copy()
2998
2999    # Extract metadata needed for tree organization
3000    # Use a safer approach to handle missing attributes
3001    def safe_getattr(obj, name):
3002        try:
3003            attr = getattr(obj, name)
3004            # Need to test if the attribute is Grib2Metadata. If so,
3005            # then get the value attribute.
3006            if isinstance(attr, grib2io.templates.Grib2Metadata):
3007                attr = attr.value
3008            return attr
3009        except (AttributeError, KeyError):
3010            return None
3011
3012    for attr in _TREE_HIERARCHY_LEVELS:
3013        if (attr not in file_index.columns) and (attr != "valueOfFirstFixedSurface"):
3014            file_index[attr] = file_index.msg.apply(lambda msg: safe_getattr(msg, attr))
3015
3016    # Also extract shortName for variable naming
3017    if "shortName" not in file_index.columns:
3018        file_index = file_index.assign(shortName=file_index.msg.apply(lambda msg: getattr(msg, "shortName", None)))
3019
3020    if drop_variables:
3021        file_index = file_index[~file_index["shortName"].isin(drop_variables)]
3022
3023    file_index = file_index.assign(nx=file_index.msg.apply(lambda msg: getattr(msg, "nx", None)))
3024    file_index = file_index.assign(ny=file_index.msg.apply(lambda msg: getattr(msg, "ny", None)))
3025
3026    # Create root DataTree
3027    root = xr.DataTree()
3028
3029    # Adjust hierarchy levels if we're stacking vertical levels
3030    hierarchy_levels = list(_TREE_HIERARCHY_LEVELS)  # This makes a copy
3031    if stack_vertical and "valueOfFirstFixedSurface" in hierarchy_levels:
3032        hierarchy_levels.remove("valueOfFirstFixedSurface")
3033
3034    # First group by level type
3035    level_groups = {}
3036
3037    # Create a dictionary to group data by level type
3038    for level_type in file_index["typeOfFirstFixedSurface"].unique():
3039        if pd.notna(level_type):  # Skip None/NaN values
3040            level_info = _LEVEL_NAME_MAPPING.get(level_type, f"level_{level_type}")
3041            level_name = level_info[0]
3042            # Get all rows for this level type
3043            level_data = file_index[file_index["typeOfFirstFixedSurface"] == level_type]
3044            level_groups[level_type] = {"name": level_name, "data": level_data}
3045
3046    # Process each level group
3047    for level_type, group_info in level_groups.items():
3048        level_name = group_info["name"]
3049        level_df = group_info["data"]
3050
3051        # Create a branch for this level type
3052        level_tree = xr.DataTree()
3053
3054        # Process this branch based on PDTN, perturbation number, etc.
3055        process_level_branch(level_tree, level_df, filename, chunks=chunks)
3056
3057        # Add this branch to the main tree
3058        root[level_name] = level_tree
3059
3060    return root
3061
3062
3063def process_level_branch(
3064    level_tree: typing.Any,
3065    df: pd.DataFrame,
3066    filename: str,
3067    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3068):
3069    """
3070    Process a level type branch of the data tree.
3071
3072    Organizes the tree by PDTN and other attributes.
3073
3074    Parameters
3075    ----------
3076    level_tree : xarray.DataTree
3077        The DataTree node for this level type.
3078    df : pandas.DataFrame
3079        DataFrame of messages for this level type.
3080    filename : str
3081        Path to the GRIB2 file.
3082    chunks : int, dict or 'auto', optional
3083        If chunks is provided, it is used to load the dataset into a
3084        dask-backed dataset.
3085    """
3086    # Group by PDTN
3087    pdtn_groups = {}
3088
3089    # Group data by PDTN first
3090    for pdtn_value in df["productDefinitionTemplateNumber"].unique():
3091        if pd.notna(pdtn_value):
3092            pdtn_df = df[df["productDefinitionTemplateNumber"] == pdtn_value]
3093            pdtn_groups[pdtn_value] = pdtn_df
3094
3095    # If there's only one PDTN value, skip creating PDTN branch level
3096    if len(pdtn_groups) == 1:
3097        pdtn, pdtn_df = next(iter(pdtn_groups.items()))
3098
3099        pdtn_name = f"pdtn_{int(pdtn)}"
3100
3101        # Check if we need to further subdivide by perturbation number
3102        has_perturbations = "perturbationNumber" in pdtn_df.columns and len(pdtn_df["perturbationNumber"].dropna().unique()) > 1
3103
3104        # Check if we need to further subdivide by probabilities unique for each variable.
3105        has_probabilities = "typeOfProbability" in pdtn_df.columns and len(pdtn_df["typeOfProbability"].dropna().unique()) > 1
3106
3107        if has_perturbations:
3108            # Process perturbations directly on the level tree
3109            process_perturbation_groups(level_tree, pdtn_df, filename, chunks=chunks)
3110        elif has_probabilities:
3111            # Process probability groups
3112            process_probability_groups(level_tree, pdtn_df, filename, chunks=chunks)
3113        else:
3114            # Try to create dataset directly on level
3115            try:
3116                dss = create_datasets_from_df(pdtn_df, filename, chunks=chunks)
3117                if dss is not None:
3118                    dt = xr.DataTree()
3119                    if len(dss) == 1:
3120                        dt.ds = dss[0]
3121                    else:
3122                        for ds in dss:
3123                            varname = list(ds.data_vars)[0]
3124                            dt[f"var_{varname}"] = ds
3125                    level_tree[pdtn_name] = dt
3126                else:
3127                    # Try to separate by variable name as a fallback
3128                    try_process_by_variables(level_tree, pdtn_df, filename, chunks=chunks)
3129            except Exception as e:
3130                print(f"Error creating dataset for level with pdtn {int(pdtn)}: {e}")
3131
3132                # Try to separate by variable name as a fallback
3133                try_process_by_variables(level_tree, pdtn_df, filename, chunks=chunks)
3134    else:
3135        # Multiple PDTN values, process each group with PDTN branch nodes
3136        for pdtn, pdtn_df in pdtn_groups.items():
3137            # Use a simple node name that's easy to use in code
3138            pdtn_name = f"pdtn_{int(pdtn)}"
3139
3140            # Check if we need to further subdivide by perturbation number
3141            has_perturbations = "perturbationNumber" in pdtn_df.columns and len(pdtn_df["perturbationNumber"].dropna().unique()) > 1
3142
3143            # Check if we need to further subdivide by probabilities unique for each variable.
3144            has_probabilities = "typeOfProbability" in pdtn_df.columns and len(pdtn_df["typeOfProbability"].dropna().unique()) > 1
3145
3146            if has_perturbations:
3147                # Create a branch for this PDTN
3148                pdtn_tree = xr.DataTree()
3149
3150                # Process perturbation groups
3151                process_perturbation_groups(pdtn_tree, pdtn_df, filename, chunks=chunks)
3152
3153                # Only add the PDTN branch if it has children
3154                if len(pdtn_tree.children) > 0 or pdtn_tree.ds is not None:
3155                    level_tree[pdtn_name] = pdtn_tree
3156            elif has_probabilities:
3157                # Create a branch for this PDTN
3158                pdtn_tree = xr.DataTree()
3159
3160                # Process probability groups
3161                process_probability_groups(pdtn_tree, pdtn_df, filename, chunks=chunks)
3162
3163                # Only add the PDTN branch if it has children
3164                if len(pdtn_tree.children) > 0 or pdtn_tree.ds is not None:
3165                    level_tree[pdtn_name] = pdtn_tree
3166            else:
3167                # Create a subtree for this PDTN
3168                pdtn_tree = xr.DataTree()
3169
3170                # Try to create dataset directly on level
3171                try:
3172                    dss = create_datasets_from_df(pdtn_df, filename, chunks=chunks)
3173                    if dss is not None:
3174                        if len(dss) == 1:
3175                            pdtn_tree.ds = dss[0]
3176                        else:
3177                            for ds in dss:
3178                                varname = list(ds.data_vars)[0]
3179                                pdtn_tree[f"var_{varname}"] = ds
3180                        level_tree[pdtn_name] = pdtn_tree
3181                    else:
3182                        # Try to separate by variable name as a fallback
3183                        try_process_by_variables(pdtn_tree, pdtn_df, filename, chunks=chunks)
3184                        level_tree[pdtn_name] = pdtn_tree
3185                except Exception as e:
3186                    print(f"Error creating dataset for level with pdtn {int(pdtn)}: {e}")
3187
3188                    # Try to separate by variable name as a fallback
3189                    try_process_by_variables(pdtn_tree, pdtn_df, filename, chunks=chunks)
3190                    level_tree[pdtn_name] = pdtn_tree
3191
3192
3193def process_probability_groups(
3194    target_tree: typing.Any,
3195    pdtn_df: pd.DataFrame,
3196    filename: str,
3197    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3198) -> bool:
3199    """
3200    Process probability groups and add them to the target tree.
3201
3202    Parameters
3203    ----------
3204    target_tree : xarray.DataTree
3205        The tree node to add probability groups to.
3206    pdtn_df : pandas.DataFrame
3207        DataFrame of messages for a specific PDTN.
3208    filename : str
3209        Path to the GRIB2 file.
3210    chunks : int, dict or 'auto', optional
3211        If chunks is provided, it is used to load the dataset into a
3212        dask-backed dataset.
3213
3214    Returns
3215    -------
3216    bool
3217        True if successful.
3218    """
3219    success = False
3220    # Group by type of probability
3221    prob_groups = {}
3222    for prob_value in pdtn_df["typeOfProbability"].unique():
3223        if pd.notna(prob_value):
3224            prob_df = pdtn_df[pdtn_df["typeOfProbability"] == prob_value]
3225            prob_groups[prob_value] = prob_df
3226
3227    # Process each probability group
3228    for prob_num, prob_df in prob_groups.items():
3229        prob_name = f"prob_{int(prob_num)}"
3230
3231        # Try to create dataset for this probability group
3232        try:
3233            dss = create_datasets_from_df(prob_df, filename, chunks=chunks)
3234            dt = xr.DataTree()
3235            if len(dss) == 1:
3236                dt.ds = dss[0]
3237                target_tree[prob_name] = dt
3238            elif len(dss) > 1:
3239                for ds in dss:
3240                    dt[f"var_{ds.data_vars[0]}"] = ds
3241            target_tree[prob_name] = dt
3242        except Exception as e:
3243            # Log error but continue processing other groups
3244            print(f"Error creating dataset for type of probability {prob_name}: {e}")
3245
3246    return success
3247
3248
3249def process_perturbation_groups(
3250    target_tree: typing.Any,
3251    pdtn_df: pd.DataFrame,
3252    filename: str,
3253    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3254) -> bool:
3255    """
3256    Process perturbation groups and add them to the target tree.
3257
3258    Parameters
3259    ----------
3260    target_tree : xarray.DataTree
3261        The tree node to add perturbation groups to.
3262    pdtn_df : pandas.DataFrame
3263        DataFrame of messages for a specific PDTN.
3264    filename : str
3265        Path to the GRIB2 file.
3266    chunks : int, dict or 'auto', optional
3267        If chunks is provided, it is used to load the dataset into a
3268        dask-backed dataset.
3269
3270    Returns
3271    -------
3272    bool
3273        True if at least one perturbation was successfully processed.
3274    """
3275    success = False
3276    # Group by perturbation number
3277    pert_groups = {}
3278    for pert_value in pdtn_df["perturbationNumber"].unique():
3279        if pd.notna(pert_value):
3280            pert_df = pdtn_df[pdtn_df["perturbationNumber"] == pert_value]
3281            pert_groups[pert_value] = pert_df
3282
3283    # Process each perturbation group
3284    for pert_num, pert_df in pert_groups.items():
3285        pert_name = f"pert_{int(pert_num)}"
3286
3287        ## Try to create dataset for this perturbation group
3288        # try:
3289        #    dss = create_datasets_from_df(pert_df, filename)
3290        #    if dss is not None:
3291        #        if len(dss) == 1:
3292        #            target_tree.ds = dss[0]
3293        #        else:
3294        #            dss_dict = {f"ds_{i}": ds for i, ds in enumerate(dss)}
3295        #            atree = xr.DataTree(dss_dict)
3296        #            target_tree[prob_name] = atree
3297        #        success = True
3298        # except Exception as e:
3299        #    # Log error but continue processing other groups
3300        #    print(f"Error creating dataset for perturbation {pert_name}: {e}")
3301
3302        # Try to create dataset for this perturbation group
3303        try:
3304            dss = create_datasets_from_df(pert_df, filename, chunks=chunks)
3305            dt = xr.DataTree()
3306            if len(dss) == 1:
3307                dt.ds = dss[0]
3308                target_tree[pert_name] = dt
3309            elif len(dss) > 1:
3310                for ds in dss:
3311                    dt[f"pert{ds.data_vars[0]}"] = ds
3312            target_tree[pert_name] = dt
3313        except Exception as e:
3314            # Log error but continue processing other groups
3315            print(f"Error creating dataset for perturbation {pert_name}: {e}")
3316
3317    return success
3318
3319
3320def try_process_by_variables(
3321    target_tree: typing.Any,
3322    df: pd.DataFrame,
3323    filename: str,
3324    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3325) -> bool:
3326    """
3327    Try to separate data by variable names and create datasets.
3328
3329    Parameters
3330    ----------
3331    target_tree : xarray.DataTree
3332        The tree node to add variable datasets to.
3333    df : pandas.DataFrame
3334        DataFrame of messages.
3335    filename : str
3336        Path to the GRIB2 file.
3337    chunks : int, dict or 'auto', optional
3338        If chunks is provided, it is used to load the dataset into a
3339        dask-backed dataset.
3340
3341    Returns
3342    -------
3343    bool
3344        True if at least one variable was successfully processed.
3345    """
3346    success = False
3347
3348    try:
3349        for var_name in df["shortName"].unique():
3350            if pd.notna(var_name):
3351                var_df = df[df["shortName"] == var_name]
3352                try:
3353                    var_ds = create_datasets_from_df(var_df, filename, chunks=chunks)
3354                    if var_ds is not None:
3355                        target_tree[f"var_{var_name}"] = var_ds[0]
3356                        success = True
3357                except Exception as var_e:
3358                    print(f"Error creating dataset for variable {var_name}: {var_e}")
3359    except Exception as nested_e:
3360        print(f"Failed to process variables: {nested_e}")
3361
3362    return success
3363
3364
3365def create_datasets_from_df(
3366    df: pd.DataFrame,
3367    filename: str,
3368    verbose: bool = False,
3369    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3370) -> typing.Optional[typing.List[xr.Dataset]]:
3371    """
3372    Create a list of xarray Datasets from a DataFrame of messages.
3373
3374    Parameters
3375    ----------
3376    df : pandas.DataFrame
3377        DataFrame of GRIB messages.
3378    filename : str
3379        Path to the GRIB2 file.
3380    verbose : bool, optional
3381        If True, prints detailed debugging information.
3382    chunks : int, dict or 'auto', optional
3383        If chunks is provided, it is used to load the dataset into a
3384        dask-backed dataset.
3385
3386    Returns
3387    -------
3388    list of xarray.Dataset, optional
3389        List of Datasets, or None if creation failed.
3390    """
3391    try:
3392        # Use parse_grib_index to get dimensions and attributes
3393        file_index, dim_coords, attrs, coord_attrs = parse_grib_index(df, {})
3394
3395        # Divide up records by variable
3396        frames, cubes, extra_geo = make_variables(file_index, filename, dim_coords, allow_uneven_dims=True)
3397
3398        if frames is None:
3399            return None
3400
3401        ds_list = []
3402        for var_df, var_cube in zip(frames, cubes):
3403            da = build_da_without_coords(var_df, var_cube, filename, attrs)
3404
3405            # Assign variable-specific coords from its cube
3406            coords = coords_from_cube(var_cube)
3407            da = da.assign_coords(coords)
3408
3409            # Assign extra index associated coords for this variable
3410            for dim_name, coord_names in dim_coords.items():
3411                retain_index_coord = False
3412                for name in coord_names:
3413                    if name == dim_name:
3414                        retain_index_coord = True
3415                    else:
3416                        if dim_name not in da.dims:
3417                            # for assigning scalar coords
3418                            coord_data = var_df[name].unique().item()
3419                            da = da.assign_coords({name: coord_data})
3420                        else:
3421                            # Handle non-scalar coords
3422                            coord_data = [
3423                                var_df[var_df.index.get_level_values(f"{dim_name}_ix") == val][name].unique().item()
3424                                for val in range(da[dim_name].size)
3425                            ]
3426                            coord = pd.Index(coord_data, name=dim_name)
3427                            da = da.assign_coords({name: (dim_name, coord)})
3428                if not retain_index_coord and dim_name in da.coords:
3429                    da = da.drop_vars(dim_name)
3430
3431            # Create a dataset for this variable
3432            var_ds = xr.Dataset({da.name: da})
3433
3434            # Assign metadata and common coords
3435            var_ds = assign_xr_meta(var_ds, [var_df], var_cube, dim_coords, extra_geo, coord_attrs)
3436
3437            if chunks is not None:
3438                var_ds = var_ds.chunk(chunks)
3439
3440            ds_list.append(var_ds)
3441
3442        return ds_list
3443    except Exception as e:
3444        if verbose:
3445            print(f"Error in create_datasets_from_df: {e}")
3446        return None
3447
3448
3449if _HAS_DATATREE:
3450
3451    @xr.register_datatree_accessor("grib2io")
3452    class Grib2ioDataTree:
3453        """
3454        DataTree accessor for GRIB2 files.
3455
3456        This accessor provides methods for working with GRIB2 data organized
3457        in a hierarchical tree structure.
3458        """
3459
3460        def __init__(self, datatree_obj):
3461            self._obj = datatree_obj
3462
3463        def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
3464            """
3465            Write all datasets in the DataTree to a GRIB2 file.
3466
3467            Parameters
3468            ----------
3469            filename : str
3470                Name of the GRIB2 file to write to.
3471            mode : {"x", "w", "a"}, optional
3472                Persistence mode, default is "x" (create, fail if exists)
3473            """
3474            # Start with the specified mode
3475            current_mode = mode
3476
3477            # Function to recursively process the tree
3478            def process_tree(node):
3479                nonlocal current_mode
3480
3481                # If this is a Dataset node with data variables
3482                if node.ds is not None and node.ds.data_vars:
3483                    # Write dataset to GRIB2 file
3484                    node.ds.grib2io.to_grib2(filename, mode=current_mode)
3485                    # Switch to append mode after first write
3486                    current_mode = "a"
3487
3488                # Process children
3489                for child_name, child_node in node.children.items():
3490                    process_tree(child_node)
3491
3492            # Start processing from the root
3493            process_tree(self._obj)
3494
3495        def griddef(self):
3496            """
3497            Get the grid definition from the first dataset in the tree that has one.
3498
3499            Returns
3500            -------
3501            grib2io.Grib2GridDef
3502                Grid definition object
3503            """
3504
3505            # Function to find first dataset with GRIB2IO_section3
3506            def find_griddef(node):
3507                if node.ds is not None and node.ds.data_vars:
3508                    for var_name in node.ds.data_vars:
3509                        if "GRIB2IO_section3" in node.ds[var_name].attrs:
3510                            return Grib2GridDef.from_section3(node.ds[var_name].attrs["GRIB2IO_section3"])
3511
3512                # Check children
3513                for child_name, child_node in node.children.items():
3514                    griddef = find_griddef(child_node)
3515                    if griddef is not None:
3516                        return griddef
3517
3518                return None
3519
3520            return find_griddef(self._obj)
3521
3522        def interp(
3523            self,
3524            method: typing.Union[str, int],
3525            grid_def_out: grib2io.Grib2GridDef,
3526            method_options: typing.Optional[typing.List[int]] = None,
3527            num_threads: int = 1,
3528        ) -> typing.Any:
3529            """
3530            Interpolate all datasets in the tree to a new grid.
3531
3532            Parameters
3533            ----------
3534            method : str or int
3535                Interpolation method to use.
3536            grid_def_out : grib2io.Grib2GridDef
3537                Target grid definition.
3538            method_options : list of int, optional
3539                Options for interpolation method.
3540            num_threads : int, optional
3541                Number of threads to use for interpolation.
3542
3543            Returns
3544            -------
3545            xarray.DataTree
3546                New DataTree with interpolated data.
3547            """
3548            new_tree = xr.DataTree()
3549
3550            # Function to recursively process the tree
3551            def process_tree(node, new_parent):
3552                # If this is a Dataset node with data variables
3553                if node.ds is not None and node.ds.data_vars:
3554                    # Interpolate dataset
3555                    interp_ds = node.ds.grib2io.interp(
3556                        method,
3557                        grid_def_out,
3558                        method_options=method_options,
3559                        num_threads=num_threads,
3560                    )
3561
3562                    # Add to new tree at the same path
3563                    if node == self._obj:  # Root node
3564                        new_parent.ds = interp_ds
3565                    else:
3566                        new_parent.ds = interp_ds
3567
3568                # Process children
3569                for child_name, child_node in node.children.items():
3570                    # Create same child in new tree
3571                    new_child = xr.DataTree()
3572                    new_parent[child_name] = new_child
3573                    process_tree(child_node, new_child)
3574
3575            # Start processing from the root
3576            process_tree(self._obj, new_tree)
3577
3578            return new_tree
3579
3580        def subset(self, lats: typing.Sequence[float], lons: typing.Sequence[float]) -> typing.Any:
3581            """
3582            Subset all datasets in the tree to a region.
3583
3584            Parameters
3585            ----------
3586            lats : sequence of float
3587                Latitude bounds [min_lat, max_lat].
3588            lons : sequence of float
3589                Longitude bounds [min_lon, max_lon].
3590
3591            Returns
3592            -------
3593            xarray.DataTree
3594                New DataTree with subset data.
3595            """
3596            new_tree = xr.DataTree()
3597
3598            # Function to recursively process the tree
3599            def process_tree(node, new_parent):
3600                # If this is a Dataset node with data variables
3601                if node.ds is not None and node.ds.data_vars:
3602                    # Subset dataset
3603                    subset_ds = node.ds.grib2io.subset(lats, lons)
3604
3605                    # Add to new tree at the same path
3606                    if node == self._obj:  # Root node
3607                        new_parent.ds = subset_ds
3608                    else:
3609                        new_parent.ds = subset_ds
3610
3611                # Process children
3612                for child_name, child_node in node.children.items():
3613                    # Create same child in new tree
3614                    new_child = xr.DataTree()
3615                    new_parent[child_name] = new_child
3616                    process_tree(child_node, new_child)
3617
3618            # Start processing from the root
3619            process_tree(self._obj, new_tree)
3620
3621            return new_tree
AVAILABLE_NON_GEO_COORDS = ['duration', 'leadTime', 'percentileValue', 'perturbationNumber', 'refDate', 'thresholdLowerLimit', 'thresholdUpperLimit', 'valueOfFirstFixedSurface', 'valueOfSecondFixedSurface', 'typeOfAerosol', 'constituentType', 'sourceSinkIndicator', 'firstWavelength', 'secondWavelength', 'firstSizeOfAerosol', 'secondSizeOfAerosol', 'scaledValueOfFirstWavelength', 'scaledValueOfSecondWavelength', 'scaledValueOfCentralWaveNumber', 'scaledValueOfFirstSize', 'scaledValueOfSecondSize']

Available non-geographic coordinate names.

AVAILABLE_NON_GEO_DIMS = ['duration', 'leadTime', 'percentileValue', 'perturbationNumber', 'refDate', 'threshold', 'level', 'typeOfAerosol', 'constituentType', 'sourceSinkIndicator', 'firstWavelength', 'secondWavelength', 'firstSizeOfAerosol', 'secondSizeOfAerosol']

Available non-geographic dimension names.

VERTICAL_COORDINATE_SURFACES = ['Ground or Water Surface', 'Isothermal Level', 'Specified radius from the centre of the Sun', 'Isobaric Surface', 'Mean Sea Level', 'Specific Altitude Above Mean Sea Level', 'Specified Height Level Above Ground', 'Sigma Level', 'Hybrid Level', 'Depth Below Land Surface', 'Isentropic (theta) Level', 'Level at Specified Pressure Difference from Ground to Level', 'Potential Vorticity Surface', 'Eta Level', 'Logarithmic Hybrid Level', 'Sigma height level', 'Hybrid Height Level', 'Hybrid Pressure Level', 'Soil level', 'Sea-ice level', 'Depth Below Sea Level', 'Depth Below Water Surface', 'Ocean Model Level', 'Ocean level defined by water density (sigma-theta) difference from near-surface to level', 'Ocean level defined by water potential temperature difference from near-surface to level', 'Ocean level defined by vertical eddy diffusivity difference from near-surface to level', 'Ocean level defined by water density (rho) difference from near-surface to level']

Lookup table to define surface types that should be parsed as vertical coordinates when data_model="nws-viz".

def parse_data_model( ds: xarray.core.dataset.Dataset, data_model: str) -> xarray.core.dataset.Dataset:
242def parse_data_model(ds: xr.Dataset, data_model: str) -> xr.Dataset:
243    """
244    Normalize a GRIB2-derived Dataset to a target data model (currently ``"nws-viz"``).
245
246    When ``data_model == "nws-viz"``, this function converts coordinate and
247    variable names to snake_case, derives CF-like metadata, promotes select
248    GRIB-derived quantities to coordinates, optionally swaps dimensions, and
249    standardizes units/attributes. If ``data_model`` is anything else, the
250    input dataset is returned unchanged.
251
252    Parameters
253    ----------
254    ds : xarray.Dataset
255        GRIB2-derived dataset whose variables and attributes follow the
256        conventions emitted by ``grib2io``. Expected to contain GRIB-related
257        attributes such as ``typeOfFirstFixedSurface``,
258        ``typeOfSecondFixedSurface``, and (for probabilistic variables)
259        ``typeOfProbability``.
260    data_model : str
261        Target data model name. Only the value ``"nws-viz"`` triggers
262        transformations.
263
264    Returns
265    -------
266    xarray.Dataset
267        A new dataset with:
268        * Selected coordinates renamed:
269          ``refDate -> forecast_reference_time``,
270          ``leadTime -> lead_time``,
271          ``validDate -> time``,
272          ``percentileValue -> percentile``,
273          ``thresholdLowerLimit -> threshold_lower_limit``,
274          ``thresholdUpperLimit -> threshold_upper_limit``.
275        * Vertical coordinates derived from
276          ``valueOfFirstFixedSurface`` / ``valueOfSecondFixedSurface`` and their
277          corresponding ``typeOf*FixedSurface`` definitions. New coordinate
278          names are generated from the surface definition (lowercased, spaces
279          to underscores, punctuation removed). If the name already exists, a
280          ``"_2"`` suffix is appended.
281        * Possible dimension swaps:
282          ``level -> <derived_vertical_coord>`` when present; and for
283          probabilistic variables, ``threshold -> threshold_lower_limit`` or
284          ``threshold -> threshold_upper_limit`` when
285          ``typeOfProbability`` indicates the appropriate semantics.
286        * Variable names lowercased; dataset- and variable-level attributes
287          converted to snake_case (except GRIB section attributes which are
288          normalized to ``grib...``).
289        * CF-adjacent metadata populated: ``standard_name`` and
290          ``cell_methods`` are set via the shortname→CF lookup table.
291        * Percent units normalized from ``"%"`` to ``"percent"`` on coordinates.
292        * For precipitation type (``PTYPE``) thresholds, numeric codes are
293          decoded to strings (GRIB2 Table 4.201) in relevant attrs/coords.
294
295    Notes
296    -----
297    - Precipitation type decoding uses GRIB2 Table 4.201 via
298      ``tables.get_value_from_table(code, "4.201")`` and returns a NumPy
299      array with ``np.dtypes.StringDType``.
300    - CF-related lookups are performed using
301      ``tables.get_table("shortname_to_cf")``.
302    - Vertical coordinate surface names are validated against
303      ``VERTICAL_COORDINATE_SURFACES`` before promotion to coordinates.
304
305    Warnings
306    --------
307    This function assumes the presence of certain GRIB-derived attributes on the
308    first data variable (e.g., ``typeOfFirstFixedSurface``,
309    ``typeOfSecondFixedSurface``, and possibly ``typeOfProbability``).
310    If these are absent or malformed, errors (e.g., ``KeyError``) may occur.
311
312    Examples
313    --------
314    >>> ds2 = parse_data_model(ds, "nws-viz")
315    >>> list(ds2.coords)
316    ['forecast_reference_time', 'lead_time', 'time', 'percentile', ...]
317    """
318    # convert coordinates and attributes to CF if requested
319    if data_model == "nws-viz":
320        # define regex to convert to snake case
321        pattern = re.compile(r"(?<!^)(?=[A-Z])")
322
323        # check for coordinates and rename
324        for coord in ds.coords:
325            if coord == "refDate":
326                ds = ds.rename({"refDate": "forecast_reference_time"})
327
328            elif coord == "leadTime":
329                ds = ds.rename({"leadTime": "lead_time"})
330
331            elif coord == "validDate":
332                ds = ds.rename({"validDate": "time"})
333
334            elif coord == "percentileValue":
335                ds = ds.rename({"percentileValue": "percentile"})
336
337            elif coord == "perturbationNumber":
338                ds = ds.rename({"perturbationNumber": "perturbation"})
339                ds["perturbation"].attrs["long_name"] = "Ensemble Perturbation Number"
340
341            elif coord == "thresholdLowerLimit":
342                ds = ds.rename({"thresholdLowerLimit": "threshold_lower_limit"})
343                ds["threshold_lower_limit"].attrs["long_name"] = "Threshold Lower Limit"
344                ds["threshold_lower_limit"].attrs["units"] = ds[list(ds.data_vars.keys())[0]].attrs["units"]
345
346                if "PTYPE" in ds.data_vars:
347                    ds["threshold_lower_limit"] = xr.apply_ufunc(
348                        _decode_ptype,
349                        ds["threshold_lower_limit"],
350                        dask="parallelized",
351                        output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
352                    )
353
354                # check if thresholdLowerLimit should be a dimension coordinate
355                if "threshold" in ds.dims:
356                    var_key = list(ds.data_vars.keys())[0]
357                    prob_types = [
358                        "Probability of event below lower limit",
359                        "Probability of event above lower limit",
360                        "Probability of event equal to lower limit",
361                        "Probability of event between upper and lower limits (the range includes lower limit but not the upper limit)",
362                    ]
363                    if ds[var_key].attrs["typeOfProbability"] in prob_types:
364                        ds = ds.swap_dims({"threshold": "threshold_lower_limit"})
365
366            elif coord == "thresholdUpperLimit":
367                ds = ds.rename({"thresholdUpperLimit": "threshold_upper_limit"})
368                ds["threshold_upper_limit"].attrs["long_name"] = "Threshold Upper Limit"
369                ds["threshold_upper_limit"].attrs["units"] = ds[list(ds.data_vars.keys())[0]].attrs["units"]
370
371                if "PTYPE" in ds.data_vars:
372                    ds["threshold_upper_limit"] = xr.apply_ufunc(
373                        _decode_ptype,
374                        ds["threshold_upper_limit"],
375                        dask="parallelized",
376                        output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
377                    )
378
379                if "threshold" in ds.dims:
380                    var_key = list(ds.data_vars.keys())[0]
381                    prob_types = [
382                        "Probability of event below upper limit",
383                        "Probability of event above upper limit",
384                    ]
385                    if ds[var_key].attrs["typeOfProbability"] in prob_types:
386                        ds = ds.swap_dims({"threshold": "threshold_upper_limit"})
387
388            elif coord == "typeOfAerosol":
389                ds = ds.rename({"typeOfAerosol": "aerosol_type"})
390                ds["aerosol_type"].attrs["long_name"] = "Aerosol Type"
391                ds["aerosol_type"] = xr.apply_ufunc(
392                    _decode_code,
393                    ds["aerosol_type"],
394                    "4.233",
395                    dask="parallelized",
396                    output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
397                )
398
399            elif coord == "constituentType":
400                ds = ds.rename({"constituentType": "constituent_type"})
401                ds["constituent_type"].attrs["long_name"] = "Chemical Constituent Type"
402                ds["constituent_type"] = xr.apply_ufunc(
403                    _decode_code,
404                    ds["constituent_type"],
405                    "4.230",
406                    dask="parallelized",
407                    output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
408                )
409
410            elif coord == "sourceSinkIndicator":
411                ds = ds.rename({"sourceSinkIndicator": "source_sink_indicator"})
412                ds["source_sink_indicator"].attrs["long_name"] = "Source/Sink Indicator"
413                ds["source_sink_indicator"] = xr.apply_ufunc(
414                    _decode_code,
415                    ds["source_sink_indicator"],
416                    "4.238",
417                    dask="parallelized",
418                    output_dtypes=[np.dtypes.StringDType] if _HAS_STRINGDTYPE else [object],
419                )
420
421            elif coord == "firstWavelength":
422                ds = ds.rename({"firstWavelength": "first_wavelength"})
423                ds["first_wavelength"].attrs["long_name"] = "First Wavelength"
424                ds["first_wavelength"].attrs["units"] = "m"
425
426            elif coord == "secondWavelength":
427                ds = ds.rename({"secondWavelength": "second_wavelength"})
428                ds["second_wavelength"].attrs["long_name"] = "Second Wavelength"
429                ds["second_wavelength"].attrs["units"] = "m"
430
431            elif coord == "firstSizeOfAerosol":
432                ds = ds.rename({"firstSizeOfAerosol": "first_size_of_aerosol"})
433                ds["first_size_of_aerosol"].attrs["long_name"] = "First Size of Aerosol"
434                ds["first_size_of_aerosol"].attrs["units"] = "m"
435
436            elif coord == "secondSizeOfAerosol":
437                ds = ds.rename({"secondSizeOfAerosol": "second_size_of_aerosol"})
438                ds["second_size_of_aerosol"].attrs["long_name"] = "Second Size of Aerosol"
439                ds["second_size_of_aerosol"].attrs["units"] = "m"
440
441            elif coord == "scaledValueOfFirstWavelength":
442                ds = ds.rename({"scaledValueOfFirstWavelength": "scaled_first_wavelength"})
443                ds["scaled_first_wavelength"].attrs["long_name"] = "Scaled Value of First Wavelength"
444
445            elif coord == "scaledValueOfSecondWavelength":
446                ds = ds.rename({"scaledValueOfSecondWavelength": "scaled_second_wavelength"})
447                ds["scaled_second_wavelength"].attrs["long_name"] = "Scaled Value of Second Wavelength"
448
449            elif coord == "scaledValueOfCentralWaveNumber":
450                ds = ds.rename({"scaledValueOfCentralWaveNumber": "scaled_central_wave_number"})
451                ds["scaled_central_wave_number"].attrs["long_name"] = "Scaled Value of Central Wave Number"
452
453            elif coord == "scaledValueOfFirstSize":
454                ds = ds.rename({"scaledValueOfFirstSize": "scaled_first_size"})
455                ds["scaled_first_size"].attrs["long_name"] = "Scaled Value of First Size"
456
457            elif coord == "scaledValueOfSecondSize":
458                ds = ds.rename({"scaledValueOfSecondSize": "scaled_second_size"})
459                ds["scaled_second_size"].attrs["long_name"] = "Scaled Value of Second Size"
460
461            # If the dataset has valueOfFirstFixedSurface as a coordinate
462            elif coord == "valueOfFirstFixedSurface":
463                # Get the valueOfFirstFixedSurface coordinate
464                da = ds.valueOfFirstFixedSurface
465
466                # Get the definition and units from typeOfFirstFixedSurface
467                var_key = list(ds.data_vars.keys())[0]
468                definition, units = ds[var_key].attrs["typeOfFirstFixedSurface"]
469
470                if definition in VERTICAL_COORDINATE_SURFACES:
471                    # Convert definition to lowercase and replace spaces with underscores
472                    key = definition.lower().replace(" ", "_")
473
474                    # remove special characters
475                    key = re.sub(r"[^a-z0-9_]", "", key)
476
477                    # Add units and grib_name attributes
478                    da.attrs["units"] = units
479                    da.attrs["grib_name"] = [
480                        "valueOfFirstFixedSurface",
481                        "typeOfFirstFixedSurface",
482                    ]
483
484                    # Assign the coordinate with the new key name
485                    ds = ds.assign_coords({key: da})
486
487                    # If valueOfFirstFixedSurface is a dimension, swap it with the new key
488                    if "level" in ds.dims:
489                        ds = ds.swap_dims({"level": key})
490
491                # Remove the original coordinates
492                del ds["valueOfFirstFixedSurface"]
493
494            # If the dataset has valueOfSecondFixedSurface as a coordinate
495            elif coord == "valueOfSecondFixedSurface":
496                # Get the valueOfSecondFixedSurface coordinate
497                da = ds.valueOfSecondFixedSurface
498
499                # Get the definition and units from typeOfSecondFixedSurface
500                var_key = list(ds.data_vars.keys())[0]
501                definition, units = ds[var_key].attrs["typeOfSecondFixedSurface"]
502
503                if definition in VERTICAL_COORDINATE_SURFACES:
504                    # Convert definition to lowercase and replace spaces with underscores
505                    key = definition.lower().replace(" ", "_")
506
507                    # remove special characters
508                    key = re.sub(r"[^a-z0-9_]", "", key)
509
510                    # check if key is already in coords
511                    if key in ds.coords:
512                        key = key + "_2"
513
514                    # Add units and grib_name attributes
515                    da.attrs["units"] = units
516                    da.attrs["grib_name"] = [
517                        "valueOfSecondFixedSurface",
518                        "typeOfSecondFixedSurface",
519                    ]
520
521                    # Assign the coordinate with the new key name
522                    ds = ds.assign_coords({key: da})
523
524                # Remove the original coordinates
525                del ds["valueOfSecondFixedSurface"]
526            else:
527                # change coord name to snake case
528                new_coord_name = pattern.sub("_", coord).lower()
529                ds = ds.rename({coord: new_coord_name})
530
531        # convert all attributes and variable names to snake case
532        for var in ds.data_vars:
533            da = ds[var]
534            record = tables.get_table("shortname_to_cf").get(da.name)
535            da.attrs["standard_name"] = "unknown" if record is None else record["cf_standard_name"]
536            da.attrs["cell_methods"] = "unknown" if record is None else record["cf_cell_methods"]
537
538            ds[var] = da
539
540            # rename variable
541            new_var_name = var.lower()
542            ds = ds.rename({var: new_var_name})
543
544            # remove attr for typeOfFirstFixedSurface (applied as coordinate above)
545            if "typeOfFirstFixedSurface" in ds[new_var_name].attrs:
546                definition, units = ds[new_var_name].attrs["typeOfFirstFixedSurface"]
547                ds[new_var_name].attrs["typeOfFirstFixedSurface"] = f"{definition} ({units})"
548
549            if "typeOfSecondFixedSurface" in ds[new_var_name].attrs:
550                definition, units = ds[new_var_name].attrs["typeOfSecondFixedSurface"]
551                ds[new_var_name].attrs["typeOfSecondFixedSurface"] = f"{definition} ({units})"
552
553            ds[new_var_name].attrs.pop("percentileValue", None)
554
555            if "threshold_lower_limit" in ds.coords:
556                ds[new_var_name].attrs.pop("thresholdLowerLimit", None)
557
558            if "threshold_upper_limit" in ds.coords:
559                ds[new_var_name].attrs.pop("thresholdUpperLimit", None)
560
561            for attr in list(ds[new_var_name].attrs.keys()):
562                # skip grib section attrs
563                if "GRIB2IO_section" in attr:
564                    # replace GRIB2IO with grib in attr
565                    new_attr_name = attr.replace("GRIB2IO", "grib")
566                else:
567                    # change attr name to snake case
568                    new_attr_name = pattern.sub("_", attr).lower()
569
570                # update new attr name for specific CF names
571                if new_attr_name == "full_name":
572                    new_attr_name = "long_name"
573
574                # change % to percent
575                if attr == "units" and ds[new_var_name].attrs[attr] == "%":
576                    ds[new_var_name].attrs[attr] = "percent"
577
578                if new_var_name == "ptype" and "threshold" in new_attr_name:
579                    value = ds[new_var_name].attrs.pop(attr)
580                    ds[new_var_name].attrs[attr] = _decode_ptype(value)
581                else:
582                    # change attr name in attrs
583                    ds[new_var_name].attrs[new_attr_name] = ds[new_var_name].attrs.pop(attr)
584
585            try:
586                new_cell_methods = section4_to_cell_methods(ds[new_var_name].attrs["grib_section4"])
587            except KeyError:
588                pass
589            else:
590                if new_cell_methods:
591                    existing = ds[new_var_name].attrs.get("cell_methods")
592                    ds[new_var_name].attrs["cell_methods"] = " ".join(filter(None, [existing, new_cell_methods]))
593
594        # change dataset attrs to snake case
595        for attr in list(ds.attrs.keys()):
596            # change attr name to snake case
597            new_attr_name = pattern.sub("_", attr).lower()
598
599            # change attr name in attrs
600            ds.attrs[new_attr_name] = ds.attrs.pop(attr)
601
602        # change % to percent
603        for coord in ds.coords:
604            if "units" in ds[coord].attrs and ds[coord].attrs["units"] == "%":
605                ds[coord].attrs["units"] = "percent"
606
607        # Update history for provenance
608        history = ds.attrs.get("history", "")
609        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
610        ds.attrs["history"] = f"{now}: Parsed to data model {data_model}\n{history}"
611
612    # Update history for provenance
613    history = ds.attrs.get("history", "")
614    now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
615    ds.attrs["history"] = f"{now}: Normalized to {data_model} data model\n{history}"
616
617    return ds

Normalize a GRIB2-derived Dataset to a target data model (currently "nws-viz").

When data_model == "nws-viz", this function converts coordinate and variable names to snake_case, derives CF-like metadata, promotes select GRIB-derived quantities to coordinates, optionally swaps dimensions, and standardizes units/attributes. If data_model is anything else, the input dataset is returned unchanged.

Parameters
  • ds (xarray.Dataset): GRIB2-derived dataset whose variables and attributes follow the conventions emitted by grib2io. Expected to contain GRIB-related attributes such as typeOfFirstFixedSurface, typeOfSecondFixedSurface, and (for probabilistic variables) typeOfProbability.
  • data_model (str): Target data model name. Only the value "nws-viz" triggers transformations.
Returns
  • xarray.Dataset: A new dataset with:
    • Selected coordinates renamed: refDate -> forecast_reference_time, leadTime -> lead_time, validDate -> time, percentileValue -> percentile, thresholdLowerLimit -> threshold_lower_limit, thresholdUpperLimit -> threshold_upper_limit.
    • Vertical coordinates derived from valueOfFirstFixedSurface / valueOfSecondFixedSurface and their corresponding typeOf*FixedSurface definitions. New coordinate names are generated from the surface definition (lowercased, spaces to underscores, punctuation removed). If the name already exists, a "_2" suffix is appended.
    • Possible dimension swaps: level -> <derived_vertical_coord> when present; and for probabilistic variables, threshold -> threshold_lower_limit or threshold -> threshold_upper_limit when typeOfProbability indicates the appropriate semantics.
    • Variable names lowercased; dataset- and variable-level attributes converted to snake_case (except GRIB section attributes which are normalized to grib...).
    • CF-adjacent metadata populated: standard_name and cell_methods are set via the shortname→CF lookup table.
    • Percent units normalized from "%" to "percent" on coordinates.
    • For precipitation type (PTYPE) thresholds, numeric codes are decoded to strings (GRIB2 Table 4.201) in relevant attrs/coords.
Notes
  • Precipitation type decoding uses GRIB2 Table 4.201 via tables.get_value_from_table(code, "4.201") and returns a NumPy array with np.dtypes.StringDType.
  • CF-related lookups are performed using tables.get_table("shortname_to_cf").
  • Vertical coordinate surface names are validated against VERTICAL_COORDINATE_SURFACES before promotion to coordinates.
Warnings

This function assumes the presence of certain GRIB-derived attributes on the first data variable (e.g., typeOfFirstFixedSurface, typeOfSecondFixedSurface, and possibly typeOfProbability). If these are absent or malformed, errors (e.g., KeyError) may occur.

Examples
>>> ds2 = parse_data_model(ds, "nws-viz")
>>> list(ds2.coords)
['forecast_reference_time', 'lead_time', 'time', 'percentile', ...]
def section4_to_cell_methods(section4_array: numpy.ndarray) -> Optional[str]:
620def section4_to_cell_methods(section4_array: np.ndarray) -> typing.Optional[str]:
621    cell_methods = None
622    if section4_array[1] == 0:
623        cell_methods = "lead_time: point"
624    elif section4_array[1] == 8:
625        to_join = []
626        # interval_end = datetime.datetime(*section4_array[17:23]
627        time_unit_table = tables.get_table("4.4")
628        for i in reversed(range(section4_array[23])):
629            offset = 6 * i
630            method = tables.get_table("4.10")[str(section4_array[25 + offset])]
631            method = method.replace("Average", "mean").lower()
632            if section4_array[26 + offset] == 1:
633                dim = "forecast_reference_time"
634            elif section4_array[26 + offset] == 2:
635                dim = "lead_time"
636            # duration_unit = time_unit_table[str(section4_array[27 + offset])].lower()
637            # duration_value = section4_array[28 + offset]
638            input_interval_units = time_unit_table[str(section4_array[29 + offset])].lower()
639            input_interval_value = section4_array[28 + offset]
640            to_join.append(f"{dim:s}: {method:s} (interval: {input_interval_value:d} {input_interval_units:s})")
641            # comment: duration {duration_value:d} {duration_unit:s} ending {interval_end:%Y-%m-%dT%H:%M:%s}
642        cell_methods = " ".join(to_join)
643    return cell_methods
class GribBackendEntrypoint(xarray.backends.common.BackendEntrypoint):
651class GribBackendEntrypoint(BackendEntrypoint):
652    """
653    xarray backend engine entrypoint for opening and decoding grib2 files.
654
655    .. warning::
656
657       This backend is experimental and the API/behavior may change without
658       backward compatibility.
659    """
660
661    def open_dataset(
662        self,
663        filename_or_obj,
664        drop_variables=None,
665        save_index=True,
666        filters=None,
667        data_model=None,
668        chunks=None,
669        storage_options=None,
670    ) -> xr.Dataset:
671        """
672        Read and parse metadata from a GRIB2 file.
673
674        Parameters
675        ----------
676        filename_or_obj : str or file-like
677            GRIB2 file to be opened. Can be a local path or a remote URI.
678        drop_variables : list of str, optional
679            List of variables to exclude from the dataset.
680        save_index : bool, optional
681            Whether to save the GRIB2 index to a file (default is True).
682        filters : dict, optional
683            Filter GRIB2 messages to a single hypercube. Dictionary keys can
684            be any GRIB2 metadata attribute name.
685        data_model : str, optional
686            Parse GRIB metadata following a defined data model convention
687            (e.g., "nws-viz").
688        chunks : int, dict or 'auto', optional
689            If chunks is provided, it is used to load the dataset into a
690            dask-backed dataset.
691        storage_options : dict, optional
692            Extra options passed to the storage backend.
693
694        Returns
695        -------
696        xarray.Dataset
697            Xarray dataset of GRIB2 messages.
698        """
699        if filters is None:
700            filters = {}
701
702        with grib2io.open(
703            filename_or_obj,
704            save_index=save_index,
705            _xarray_backend=True,
706            **(storage_options or {}),
707        ) as f:
708            file_index = pd.DataFrame(f._index)
709            file_index = file_index.assign(msg=list(f))
710
711        ds = _open_dataset_from_index(
712            file_index,
713            filename_or_obj,
714            filters,
715            data_model,
716            drop_variables=drop_variables,
717            chunks=chunks,
718        )
719
720        # Update history for provenance
721        history = ds.attrs.get("history", "")
722        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
723        ds.attrs["history"] = f"{now}: Initialized via grib2io.open_dataset from {filename_or_obj}\n{history}"
724        ds.attrs["data_model"] = "" if data_model is None else data_model
725
726        return ds
727
728    def open_datatree(
729        self,
730        filename_or_obj,
731        drop_variables=None,
732        save_index=True,
733        filters=None,
734        stack_vertical=False,
735        chunks=None,
736    ) -> typing.Any:
737        """
738        Open a GRIB2 file as an xarray DataTree.
739
740        Parameters
741        ----------
742        filename : str
743            Path to the GRIB2 file.
744        drop_variables : list, optional
745            List of variables to exclude.
746        filters : dict, optional
747            Filter criteria for GRIB2 messages.
748        stack_vertical : bool, optional
749            If True, organize the tree with vertical layers stacked in a single dataset.
750        chunks : int, dict or 'auto', optional
751            If chunks is provided, it is used to load the dataset into a
752            dask-backed dataset.
753
754        Returns
755        -------
756        xarray.DataTree
757            A hierarchical DataTree representation of the GRIB2 data.
758        """
759        if not _HAS_DATATREE:
760            raise ImportError("xarray version does not support DataTree functionality.")
761
762        if filters is None:
763            filters = {}
764
765        # Open the file without any filters first to get all messages
766        with grib2io.open(filename_or_obj, save_index=save_index, _xarray_backend=True) as f:
767            file_index = pd.DataFrame(f._index)
768            file_index = file_index.assign(msg=list(f))
769
770        # Build tree structure from GRIB messages with specified options
771        tree = build_datatree_from_grib(
772            filename_or_obj,
773            file_index,
774            filters,
775            stack_vertical=stack_vertical,
776            drop_variables=drop_variables,
777            chunks=chunks,
778        )
779
780        # Update history for provenance
781        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
782        history = f"{now}: Initialized via grib2io.open_datatree\n"
783
784        def _add_history(node):
785            if node.ds is not None:
786                node.ds.attrs["history"] = history + node.ds.attrs.get("history", "")
787            for child in node.children.values():
788                _add_history(child)
789
790        _add_history(tree)
791
792        # Put warning here so it is the last message from likely other Xarray warnings.
793        warnings.warn(
794            "grib2io’s xarray backend DataTree support is experimental. The DataTree structure or attributes may change in future releases.",
795            UserWarning,
796            stacklevel=2,
797        )
798
799        return tree

xarray backend engine entrypoint for opening and decoding grib2 files.

This backend is experimental and the API/behavior may change without backward compatibility.

def open_dataset( self, filename_or_obj, drop_variables=None, save_index=True, filters=None, data_model=None, chunks=None, storage_options=None) -> xarray.core.dataset.Dataset:
661    def open_dataset(
662        self,
663        filename_or_obj,
664        drop_variables=None,
665        save_index=True,
666        filters=None,
667        data_model=None,
668        chunks=None,
669        storage_options=None,
670    ) -> xr.Dataset:
671        """
672        Read and parse metadata from a GRIB2 file.
673
674        Parameters
675        ----------
676        filename_or_obj : str or file-like
677            GRIB2 file to be opened. Can be a local path or a remote URI.
678        drop_variables : list of str, optional
679            List of variables to exclude from the dataset.
680        save_index : bool, optional
681            Whether to save the GRIB2 index to a file (default is True).
682        filters : dict, optional
683            Filter GRIB2 messages to a single hypercube. Dictionary keys can
684            be any GRIB2 metadata attribute name.
685        data_model : str, optional
686            Parse GRIB metadata following a defined data model convention
687            (e.g., "nws-viz").
688        chunks : int, dict or 'auto', optional
689            If chunks is provided, it is used to load the dataset into a
690            dask-backed dataset.
691        storage_options : dict, optional
692            Extra options passed to the storage backend.
693
694        Returns
695        -------
696        xarray.Dataset
697            Xarray dataset of GRIB2 messages.
698        """
699        if filters is None:
700            filters = {}
701
702        with grib2io.open(
703            filename_or_obj,
704            save_index=save_index,
705            _xarray_backend=True,
706            **(storage_options or {}),
707        ) as f:
708            file_index = pd.DataFrame(f._index)
709            file_index = file_index.assign(msg=list(f))
710
711        ds = _open_dataset_from_index(
712            file_index,
713            filename_or_obj,
714            filters,
715            data_model,
716            drop_variables=drop_variables,
717            chunks=chunks,
718        )
719
720        # Update history for provenance
721        history = ds.attrs.get("history", "")
722        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
723        ds.attrs["history"] = f"{now}: Initialized via grib2io.open_dataset from {filename_or_obj}\n{history}"
724        ds.attrs["data_model"] = "" if data_model is None else data_model
725
726        return ds

Read and parse metadata from a GRIB2 file.

Parameters
  • filename_or_obj (str or file-like): GRIB2 file to be opened. Can be a local path or a remote URI.
  • drop_variables (list of str, optional): List of variables to exclude from the dataset.
  • save_index (bool, optional): Whether to save the GRIB2 index to a file (default is True).
  • filters (dict, optional): Filter GRIB2 messages to a single hypercube. Dictionary keys can be any GRIB2 metadata attribute name.
  • data_model (str, optional): Parse GRIB metadata following a defined data model convention (e.g., "nws-viz").
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
  • storage_options (dict, optional): Extra options passed to the storage backend.
Returns
  • xarray.Dataset: Xarray dataset of GRIB2 messages.
def open_datatree( self, filename_or_obj, drop_variables=None, save_index=True, filters=None, stack_vertical=False, chunks=None) -> Any:
728    def open_datatree(
729        self,
730        filename_or_obj,
731        drop_variables=None,
732        save_index=True,
733        filters=None,
734        stack_vertical=False,
735        chunks=None,
736    ) -> typing.Any:
737        """
738        Open a GRIB2 file as an xarray DataTree.
739
740        Parameters
741        ----------
742        filename : str
743            Path to the GRIB2 file.
744        drop_variables : list, optional
745            List of variables to exclude.
746        filters : dict, optional
747            Filter criteria for GRIB2 messages.
748        stack_vertical : bool, optional
749            If True, organize the tree with vertical layers stacked in a single dataset.
750        chunks : int, dict or 'auto', optional
751            If chunks is provided, it is used to load the dataset into a
752            dask-backed dataset.
753
754        Returns
755        -------
756        xarray.DataTree
757            A hierarchical DataTree representation of the GRIB2 data.
758        """
759        if not _HAS_DATATREE:
760            raise ImportError("xarray version does not support DataTree functionality.")
761
762        if filters is None:
763            filters = {}
764
765        # Open the file without any filters first to get all messages
766        with grib2io.open(filename_or_obj, save_index=save_index, _xarray_backend=True) as f:
767            file_index = pd.DataFrame(f._index)
768            file_index = file_index.assign(msg=list(f))
769
770        # Build tree structure from GRIB messages with specified options
771        tree = build_datatree_from_grib(
772            filename_or_obj,
773            file_index,
774            filters,
775            stack_vertical=stack_vertical,
776            drop_variables=drop_variables,
777            chunks=chunks,
778        )
779
780        # Update history for provenance
781        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
782        history = f"{now}: Initialized via grib2io.open_datatree\n"
783
784        def _add_history(node):
785            if node.ds is not None:
786                node.ds.attrs["history"] = history + node.ds.attrs.get("history", "")
787            for child in node.children.values():
788                _add_history(child)
789
790        _add_history(tree)
791
792        # Put warning here so it is the last message from likely other Xarray warnings.
793        warnings.warn(
794            "grib2io’s xarray backend DataTree support is experimental. The DataTree structure or attributes may change in future releases.",
795            UserWarning,
796            stacklevel=2,
797        )
798
799        return tree

Open a GRIB2 file as an xarray DataTree.

Parameters
  • filename (str): Path to the GRIB2 file.
  • drop_variables (list, optional): List of variables to exclude.
  • filters (dict, optional): Filter criteria for GRIB2 messages.
  • stack_vertical (bool, optional): If True, organize the tree with vertical layers stacked in a single dataset.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
Returns
  • xarray.DataTree: A hierarchical DataTree representation of the GRIB2 data.
class GribBackendArray(xarray.backends.common.BackendArray):
802class GribBackendArray(BackendArray):
803    """
804    BackendArray implementation for GRIB2 data.
805    """
806
807    def __init__(self, array: "OnDiskArray", lock: SerializableLock):
808        """
809        Initialize the GribBackendArray.
810
811        Parameters
812        ----------
813        array : OnDiskArray
814            The on-disk array object.
815        lock : SerializableLock
816            The lock to use for thread-safe access.
817        """
818        self.array = array
819        self.shape = array.shape
820        self.dtype = np.dtype(array.dtype)
821        self.lock = lock
822
823    def __getitem__(self, key: xr.core.indexing.ExplicitIndexer) -> np.typing.ArrayLike:
824        return xr.core.indexing.explicit_indexing_adapter(
825            key,
826            self.shape,
827            indexing.IndexingSupport.BASIC,
828            self._raw_getitem,
829        )
830
831    def _raw_getitem(self, key: tuple) -> np.ndarray:
832        """
833        Implement thread-safe access to data on disk.
834
835        Parameters
836        ----------
837        key : tuple
838            The indexing key.
839
840        Returns
841        -------
842        np.ndarray
843            The indexed array.
844        """
845        with self.lock:
846            return self.array[key]

BackendArray implementation for GRIB2 data.

GribBackendArray( array: OnDiskArray, lock: xarray.backends.locks.SerializableLock)
807    def __init__(self, array: "OnDiskArray", lock: SerializableLock):
808        """
809        Initialize the GribBackendArray.
810
811        Parameters
812        ----------
813        array : OnDiskArray
814            The on-disk array object.
815        lock : SerializableLock
816            The lock to use for thread-safe access.
817        """
818        self.array = array
819        self.shape = array.shape
820        self.dtype = np.dtype(array.dtype)
821        self.lock = lock

Initialize the GribBackendArray.

Parameters
  • array (OnDiskArray): The on-disk array object.
  • lock (SerializableLock): The lock to use for thread-safe access.
array
shape
dtype
lock
class Grid:
849class Grid:
850    def __new__(cls, section3):
851        gdtn = section3[4]
852        Gdt = templates.gdt_class_by_gdtn(gdtn)
853
854        @dataclass
855        class _Grid(Gdt):
856            section3: NDArray = field(init=True, repr=True)
857            # Section 3 looked up common attributes.  Other looked up attributes are available according
858            # to the Grid Definition Template.
859            gridDefinitionSection: NDArray = field(init=False, repr=False, default=templates.GridDefinitionSection())
860            sourceOfGridDefinition: int = field(init=False, repr=False, default=templates.SourceOfGridDefinition())
861            numberOfDataPoints: int = field(init=False, repr=False, default=templates.NumberOfDataPoints())
862            interpretationOfListOfNumbers: templates.Grib2Metadata = field(
863                init=False,
864                repr=False,
865                default=templates.InterpretationOfListOfNumbers(),
866            )
867            gridDefinitionTemplateNumber: templates.Grib2Metadata = field(init=False, repr=False, default=templates.GridDefinitionTemplateNumber())
868            gridDefinitionTemplate: list = field(init=False, repr=False, default=templates.GridDefinitionTemplate())
869            _earthparams: dict = field(init=False, repr=False, default=templates.EarthParams())
870            _dxsign: float = field(init=False, repr=False, default=templates.DxSign())
871            _dysign: float = field(init=False, repr=False, default=templates.DySign())
872            _llscalefactor: float = field(init=False, repr=False, default=templates.LLScaleFactor())
873            _lldivisor: float = field(init=False, repr=False, default=templates.LLDivisor())
874            _xydivisor: float = field(init=False, repr=False, default=templates.XYDivisor())
875            shapeOfEarth: templates.Grib2Metadata = field(init=False, repr=False, default=templates.ShapeOfEarth())
876            earthShape: str = field(init=False, repr=False, default=templates.EarthShape())
877            earthRadius: float = field(init=False, repr=False, default=templates.EarthRadius())
878            earthMajorAxis: float = field(init=False, repr=False, default=templates.EarthMajorAxis())
879            earthMinorAxis: float = field(init=False, repr=False, default=templates.EarthMinorAxis())
880            resolutionAndComponentFlags: list = field(init=False, repr=False, default=templates.ResolutionAndComponentFlags())
881            ny: int = field(init=False, repr=False, default=templates.Ny())
882            nx: int = field(init=False, repr=False, default=templates.Nx())
883            scanModeFlags: list = field(init=False, repr=False, default=templates.ScanModeFlags())
884            projParameters: dict = field(init=False, repr=False, default=templates.ProjParameters())
885
886            def __post_init__(self):
887                self.gdtn = self.section3[4]
888
889        grid = _Grid(section3)
890        return grid
def exclusive_slice_to_inclusive(item: slice):
893def exclusive_slice_to_inclusive(item: slice):
894    """
895    Convert a slice with exclusive stop to an inclusive slice.
896
897    If the slice has a step, the stop is reduced by the step, so that both
898    interpretations would yield the same result.
899
900    The means that [start, stop) is converted to [start, stop - step].
901
902    Parameters
903    ----------
904    item
905        The slice to convert.
906
907    Returns
908    -------
909    slice
910        The converted slice.
911    """
912    # return the None slice
913    if item.start is None and item.stop is None and item.step is None:
914        return item
915    if not isinstance(item, slice):
916        raise ValueError(f"item must be a slice; it was of type {type(item)}")
917    # if step is None, it's one
918    step = 1 if item.step is None else item.step
919    if item.stop < item.start or step < 1:
920        raise ValueError(f"slice {item} not accounted for")
921    # handle case where slice has one item
922    if abs(item.stop - item.start) == step:
923        return [item.start]
924    # other cases require reducing the stop by the step
925    s = slice(item.start, item.stop - step, step)
926    return s

Convert a slice with exclusive stop to an inclusive slice.

If the slice has a step, the stop is reduced by the step, so that both interpretations would yield the same result.

The means that [start, stop) is converted to [start, stop - step].

Parameters
  • item: The slice to convert.
Returns
  • slice: The converted slice.
class Validator:
929class Validator:
930    def __set_name__(self, owner, name):
931        self.private_name = f"_{name}"
932        self.name = name
933
934    def __get__(self, obj, objtype=None):
935        try:
936            value = getattr(obj, self.private_name)
937        except AttributeError:
938            value = None
939        return value
class PdIndex(Validator):
942class PdIndex(Validator):
943    def __set__(self, obj, value):
944        try:
945            value = pd.Index(value)
946        except TypeError:
947            value = pd.Index([value])
948        setattr(obj, self.private_name, value)
def array_safe_eq(a: Any, b: Any) -> bool:
 979def array_safe_eq(a: typing.Any, b: typing.Any) -> bool:
 980    """
 981    Check if a and b are equal, even if they are numpy arrays.
 982
 983    Parameters
 984    ----------
 985    a : any
 986        First object to compare.
 987    b : any
 988        Second object to compare.
 989
 990    Returns
 991    -------
 992    bool
 993        True if equal, False otherwise.
 994    """
 995    if a is b:
 996        return True
 997    if hasattr(a, "equals"):
 998        return a.equals(b)
 999    if hasattr(a, "all") and hasattr(b, "all"):
1000        return a.shape == b.shape and (a == b).all()
1001    if hasattr(a, "all") or hasattr(b, "all"):
1002        return False
1003    try:
1004        return a == b
1005    except TypeError:
1006        return NotImplementedError

Check if a and b are equal, even if they are numpy arrays.

Parameters
  • a (any): First object to compare.
  • b (any): Second object to compare.
Returns
  • bool: True if equal, False otherwise.
def dc_eq(dc1: Any, dc2: Any) -> bool:
1009def dc_eq(dc1: typing.Any, dc2: typing.Any) -> bool:
1010    """
1011    Check if two dataclasses which hold numpy arrays are equal.
1012
1013    Parameters
1014    ----------
1015    dc1 : any
1016        First dataclass to compare.
1017    dc2 : any
1018        Second dataclass to compare.
1019
1020    Returns
1021    -------
1022    bool
1023        True if equal, False otherwise.
1024    """
1025    if dc1 is dc2:
1026        return True
1027    if dc1.__class__ is not dc2.__class__:
1028        return NotImplementedError
1029    t1 = astuple(dc1)
1030    t2 = astuple(dc2)
1031    return all(array_safe_eq(a1, a2) for a1, a2 in zip(t1, t2))

Check if two dataclasses which hold numpy arrays are equal.

Parameters
  • dc1 (any): First dataclass to compare.
  • dc2 (any): Second dataclass to compare.
Returns
  • bool: True if equal, False otherwise.
def coords_from_cube(cube: dict) -> Dict[str, xarray.core.variable.Variable]:
1034def coords_from_cube(cube: dict) -> typing.Dict[str, xr.Variable]:
1035    """
1036    Create a dictionary of xarray Variables from a cube definition.
1037
1038    Parameters
1039    ----------
1040    cube : dict
1041        Dimension cube definition.
1042
1043    Returns
1044    -------
1045    dict of str to xarray.Variable
1046        Coordinates for the Dataset/DataArray.
1047    """
1048    keys = list(cube.keys())
1049    keys.remove("x")
1050    keys.remove("y")
1051    coords = dict()
1052    for k in keys:
1053        if k is not None:
1054            if len(cube[k]) > 1:
1055                coords[k] = xr.Variable(dims=k, data=cube[k], attrs=dict(grib_name=k))
1056            elif len(cube[k]) == 1:
1057                coords[k] = xr.Variable(dims=tuple(), data=cube[k][0], attrs=dict(grib_name=k))
1058    return coords

Create a dictionary of xarray Variables from a cube definition.

Parameters
  • cube (dict): Dimension cube definition.
Returns
  • dict of str to xarray.Variable: Coordinates for the Dataset/DataArray.
@dataclass
class OnDiskArray:
1061@dataclass
1062class OnDiskArray:
1063    """
1064    On-disk array representation for GRIB2 messages.
1065    """
1066
1067    file_name: typing.Union[str, typing.List[str]]
1068    index: pd.DataFrame = field(repr=False)
1069    cube: dict = field(repr=False)
1070    shape: typing.Tuple[int, ...] = field(init=False)
1071    ndim: int = field(init=False)
1072    geo_ndim: int = field(init=False)
1073    dtype: str = "float32"
1074
1075    def __post_init__(self):
1076        # multiple grids not allowed so can just use first
1077        geo_shape = (self.index.iloc[0].ny, self.index.iloc[0].nx)
1078
1079        self.geo_shape = geo_shape
1080        self.geo_ndim = len(geo_shape)
1081
1082        if len(self.index) == 1:
1083            self.shape = geo_shape
1084        else:
1085            if self.index.index.nlevels == 1:
1086                self.shape = tuple([len(self.index.index)]) + geo_shape
1087            else:
1088                self.shape = tuple([len(i) for i in self.index.index.levels]) + geo_shape
1089        self.ndim = len(self.shape)
1090
1091        cols = ["msg", "sectionOffset"]
1092        if "file_index" in self.index.columns:
1093            cols.append("file_index")
1094        self.index = self.index[cols]
1095
1096    def __getitem__(self, item: tuple) -> np.ndarray:
1097        """
1098        Retrieve data from disk for the specified slices.
1099
1100        Parameters
1101        ----------
1102        item : tuple
1103            The slicing tuple.
1104
1105        Returns
1106        -------
1107        np.ndarray
1108            The retrieved data array.
1109        """
1110        # dimensions not in index are internal to tdlpack records; 2 dims for
1111        # grids; 1 dim for stations
1112
1113        index_slicer = item[: -self.geo_ndim]
1114        # maintain all multindex levels
1115        index_slicer = tuple([[i] if isinstance(i, int) else i for i in index_slicer])
1116
1117        # pandas loc slicing is inclusive, therefore convert slices into
1118        # explicit lists
1119        index_slicer_inclusive = tuple([exclusive_slice_to_inclusive(i) if isinstance(i, slice) else i for i in index_slicer])
1120
1121        # get records selected by item in new index dataframe
1122        if len(index_slicer_inclusive) == 1:
1123            index = self.index.loc[index_slicer_inclusive]
1124        elif len(index_slicer_inclusive) > 1:
1125            index = self.index.loc[index_slicer_inclusive, :]
1126        else:
1127            index = self.index
1128        index = index.set_index(index.index)
1129
1130        # set miloc to new relative locations in sub array
1131        index["miloc"] = list(zip(*[index.index.unique(level=dim).get_indexer(index.index.get_level_values(dim)) for dim in index.index.names]))
1132
1133        if len(index_slicer_inclusive) == 1:
1134            array_field_shape = tuple([len(index.index)]) + self.geo_shape
1135        elif len(index_slicer_inclusive) > 1:
1136            array_field_shape = index.index.levshape + self.geo_shape
1137        else:
1138            array_field_shape = self.geo_shape
1139
1140        array_field = np.full(array_field_shape, fill_value=np.nan, dtype="float32")
1141
1142        if "file_index" in index.columns:
1143            for file_idx, group in index.groupby("file_index"):
1144                filename = self.file_name[file_idx] if isinstance(self.file_name, list) else self.file_name
1145                with open(filename, mode="rb") as filehandle:
1146                    for key, row in group.iterrows():
1147                        bitmap_offset = None if pd.isna(row["sectionOffset"][6]) else int(row["sectionOffset"][6])
1148                        values = _data(filehandle, row.msg, bitmap_offset, row["sectionOffset"][7])
1149
1150                        if len(index_slicer_inclusive) >= 1:
1151                            array_field[row.miloc] = values
1152                        else:
1153                            array_field = values
1154        else:
1155            with open(self.file_name, mode="rb") as filehandle:
1156                for key, row in index.iterrows():
1157                    bitmap_offset = None if pd.isna(row["sectionOffset"][6]) else int(row["sectionOffset"][6])
1158                    values = _data(filehandle, row.msg, bitmap_offset, row["sectionOffset"][7])
1159
1160                    if len(index_slicer_inclusive) >= 1:
1161                        array_field[row.miloc] = values
1162                    else:
1163                        array_field = values
1164
1165        # handle geo dim slicing
1166        array_field = array_field[(Ellipsis,) + item[-self.geo_ndim :]]
1167
1168        # squeeze array dimensions expressed as integer
1169        for i, it in reversed(list(enumerate(item[: -self.geo_ndim]))):
1170            if isinstance(it, int):
1171                array_field = array_field[(slice(None, None, None),) * i + (0,)]
1172
1173        return array_field

On-disk array representation for GRIB2 messages.

OnDiskArray( file_name: Union[str, List[str]], index: pandas.DataFrame, cube: dict, dtype: str = 'float32')
file_name: Union[str, List[str]]
index: pandas.DataFrame
cube: dict
shape: Tuple[int, ...]
ndim: int
geo_ndim: int
dtype: str = 'float32'
def dims_to_shape(d: dict) -> tuple:
1176def dims_to_shape(d: dict) -> tuple:
1177    """
1178    Convert dimension metadata to a shape tuple.
1179
1180    Parameters
1181    ----------
1182    d : dict
1183        Dimension metadata dictionary.
1184
1185    Returns
1186    -------
1187    tuple
1188        Shape tuple.
1189    """
1190    if "nx" in d:
1191        t = (d["ny"], d["nx"])
1192    else:
1193        t = (d["nsta"],)
1194    return t

Convert dimension metadata to a shape tuple.

Parameters
  • d (dict): Dimension metadata dictionary.
Returns
  • tuple: Shape tuple.
def filter_index(index: pandas.DataFrame, k: str, v: Any) -> pandas.DataFrame:
1197def filter_index(index: pd.DataFrame, k: str, v: typing.Any) -> pd.DataFrame:
1198    """
1199    Filter a GRIB2 index DataFrame by a key-value pair.
1200
1201    Supports slice and vectorized-indexing similar to xarray's ``sel``.
1202
1203    Parameters
1204    ----------
1205    index : pandas.DataFrame
1206        The GRIB2 index DataFrame to filter.
1207    k : str
1208        Column name to filter by.
1209    v : any
1210        Value(s) or slice to filter for.
1211
1212    Returns
1213    -------
1214    pandas.DataFrame
1215        Filtered index.
1216    """
1217    if isinstance(v, slice):
1218        index = index.set_index(k)
1219        index = index.loc[v]
1220        index = index.reset_index()
1221    else:
1222        label = (
1223            v
1224            if getattr(v, "ndim", 1) > 1  # vectorized-indexing
1225            else _asarray_tuplesafe(v)
1226        )
1227        if label.ndim == 0:
1228            # see https://github.com/pydata/xarray/pull/4292 for details
1229            label_value = label[()] if label.dtype.kind in "mM" else label.item()
1230            try:
1231                indexer = pd.Index(index[k]).get_loc(label_value)
1232                if isinstance(indexer, int):
1233                    index = index.iloc[[indexer]]
1234                else:
1235                    index = index.iloc[indexer]
1236            except KeyError:
1237                index = index.iloc[[]]
1238        else:
1239            indexer = pd.Index(index[k]).get_indexer_for(np.ravel(v))
1240            index = index.iloc[indexer[indexer >= 0]]
1241
1242    return index

Filter a GRIB2 index DataFrame by a key-value pair.

Supports slice and vectorized-indexing similar to xarray's sel.

Parameters
  • index (pandas.DataFrame): The GRIB2 index DataFrame to filter.
  • k (str): Column name to filter by.
  • v (any): Value(s) or slice to filter for.
Returns
  • pandas.DataFrame: Filtered index.
def parse_grib_index( index: pandas.DataFrame, filters: Mapping[str, Any] = {}) -> Tuple[pandas.DataFrame, Dict[str, List[str]], dict, Dict[str, dict]]:
1245def parse_grib_index(
1246    index: pd.DataFrame,
1247    filters: typing.Mapping[str, typing.Any] = dict(),
1248) -> typing.Tuple[pd.DataFrame, typing.Dict[str, typing.List[str]], dict, typing.Dict[str, dict]]:
1249    """
1250    Apply filters.
1251
1252    Evaluate remaining dimensions based on pdtn and parse each out.
1253
1254    Parameters
1255    ----------
1256    index
1257        Pandas DataFrame containing the GRIB2 message index.
1258    filters
1259        Filter GRIB2 messages to single hypercube. Dict keys can be any
1260        GRIB2 metadata attribute name.
1261
1262    Returns
1263    -------
1264    index
1265        Modified Pandas DataFrame with added GRIB2 metadata columns.
1266    dim_coords
1267        List of GRIB2 attributes that will be used for coordinates and/or dimensions.
1268    attrs
1269        Dict of metadata attributes (non-coordinates, non-geo)
1270    """
1271
1272    # make a copy of filters, remove filters as they are applied
1273    filters = copy(filters)
1274
1275    for k, v in filters.items():
1276        if k not in index.columns:
1277            kwarg = {k: index.msg.apply(lambda msg: getattr(msg, k))}
1278            index = index.assign(**kwarg)
1279        # adopt parts of xarray's sel logic  so that filters behave similarly
1280        # allowed to filter to nothing to make empty dataset
1281        index = filter_index(index, k, v)
1282
1283    if len(index) == 0:
1284        return index, list(), dict(), dict()
1285
1286    dim_coords = dict()  # key=name of dim, value=list of coord names
1287    attrs = dict()
1288    coord_attrs = dict()
1289
1290    # expand index
1291    index = index.assign(shortName=index.msg.apply(lambda msg: msg.shortName))
1292    index = index.assign(nx=index.msg.apply(lambda msg: msg.nx))
1293    index = index.assign(ny=index.msg.apply(lambda msg: msg.ny))
1294    index = index.astype({"ny": "int", "nx": "int"})
1295
1296    # apply common filters(to all definition templates) to reduce dataset to
1297    # single cube
1298    # ensure only one of each of the below exists after filters applied
1299    required_uniques = [
1300        "productDefinitionTemplateNumber",
1301        "typeOfGeneratingProcess",
1302        "typeOfFirstFixedSurface",
1303        "typeOfSecondFixedSurface",
1304    ]
1305
1306    def meta_check(index, attrs, meta):
1307        """
1308        add meta to the datframe index
1309        check that there is a single type
1310        add the type to attrs
1311
1312        returns index, attrs
1313        """
1314        index = index.assign(**{meta: index.msg.apply(lambda msg: getattr(msg, meta))})
1315
1316        unique = index[meta].unique()
1317        if len(index[meta].unique()) > 1:
1318            raise ValueError(f"filter to a single {meta}; found: {[str(i) for i in unique]}")
1319        value = unique.item()
1320        if isinstance(value, grib2io.templates.Grib2Metadata):
1321            value = value.definition
1322
1323        # None is returned if no value found,
1324        # check and change to string None
1325        if value is None:
1326            value = "None"
1327
1328        attrs[meta] = value
1329        return index, attrs
1330
1331    for meta in required_uniques:
1332        index, attrs = meta_check(index, attrs, meta)
1333
1334    pdtn = index.productDefinitionTemplateNumber.iloc[0].value
1335
1336    # determine which non geo dimensions can be created from data by this point
1337    # the index is filtered down to a single type for all required_uniques
1338
1339    # Dim Name     # matching dim_name for using this data as index coordinate
1340    dim_coords["refDate"] = ["refDate"]
1341    coord_attrs["refDate"] = dict(standard_name="forecast_reference_time")
1342    #   dim_coords["refDate"] = ["refDate", "hour"] # non dim name matching items in list are used as non-index coordinates
1343
1344    dim_coords["leadTime"] = ["leadTime"]
1345    coord_attrs["leadTime"] = dict(standard_name="forecast_period")
1346
1347    if "valueOfFirstFixedSurface" not in index.columns:
1348        index = index.assign(valueOfFirstFixedSurface=index.msg.apply(lambda msg: msg.valueOfFirstFixedSurface))
1349    if "valueOfsecondFixedSurface" not in index.columns:
1350        index = index.assign(valueOfSecondFixedSurface=index.msg.apply(lambda msg: msg.valueOfSecondFixedSurface))
1351
1352    # dim name api change, user could run ds = ds.swap_dims(fixedSurface="valueOfFirstFixedSurface")
1353    index = index.assign(level=list(zip(index["valueOfFirstFixedSurface"], index["valueOfSecondFixedSurface"])))
1354    #   index = index.assign(level=index.msg.apply(lambda msg: msg.level))
1355    # lack of "level" indeicates don't create extra index coordinate "level"
1356    dim_coords["level"] = ["valueOfFirstFixedSurface", "valueOfSecondFixedSurface"]
1357
1358    # logic for parsing possible dims from specific product definition section
1359
1360    if pdtn in {5, 9}:
1361        # Probability forecasts at a horizontal level or in a horizontal layer
1362        # in a continuous or non-continuous time interval.  (see Template
1363        # 4.9)
1364        #       AVAILABLE_THRESHOLD = {
1365        #           0: {'has_lower': True, 'has_upper': False},
1366        #           1: {'has_lower': False, 'has_upper': True},
1367        #           2: {'has_lower': True, 'has_upper': True},
1368        #           3: {'has_lower': True, 'has_upper': False},
1369        #           4: {'has_lower': False, 'has_upper': True},
1370        #           5: {'has_lower': True, 'has_upper': False},
1371        #       }
1372
1373        index, attrs = meta_check(index, attrs, "typeOfProbability")
1374        if "thresholdLowerLimit" not in index.columns:
1375            index = index.assign(thresholdLowerLimit=index.msg.apply(lambda msg: msg.thresholdLowerLimit))
1376        if "thresholdUpperLimit" not in index.columns:
1377            index = index.assign(thresholdUpperLimit=index.msg.apply(lambda msg: msg.thresholdUpperLimit))
1378        if "threshold" not in index.columns:
1379            # using composite of lower and upper, but could use threshold string from grib2io as long as that is unique and based on lower and upper
1380            index = index.assign(threshold=list(zip(index["thresholdLowerLimit"], index["thresholdUpperLimit"])))
1381        #           index = index.assign(threshold = index.msg.apply(lambda msg: msg.threshold))
1382
1383        # ommiting threshold results in no index being assigned for this possible dim
1384        dim_coords["threshold"] = ["thresholdLowerLimit", "thresholdUpperLimit"]
1385
1386    if pdtn in {6, 10}:
1387        # Percentile forecasts at a horizontal level or in a horizontal layer
1388        # in a continuous or non-continuous time interval.  (see Template
1389        # 4.10)
1390        dim_coords["percentileValue"] = ["percentileValue"]
1391        coord_attrs["percentileValue"] = dict(long_name="percentile", units="percent")
1392
1393    if pdtn in {
1394        8,
1395        9,
1396        10,
1397        11,
1398        12,
1399        13,
1400        14,
1401        42,
1402        43,
1403        45,
1404        46,
1405        47,
1406        61,
1407        62,
1408        63,
1409        67,
1410        68,
1411        72,
1412        73,
1413        78,
1414        79,
1415        82,
1416        83,
1417        84,
1418        85,
1419        87,
1420        91,
1421    }:
1422        dim_coords["duration"] = ["duration"]
1423
1424    if pdtn in {
1425        1,
1426        11,
1427        33,
1428        34,
1429        41,
1430        43,
1431        45,
1432        47,
1433        49,
1434        54,
1435        56,
1436        58,
1437        59,
1438        63,
1439        68,
1440        77,
1441        79,
1442        81,
1443        83,
1444        84,
1445        85,
1446        92,
1447    }:
1448        dim_coords["perturbationNumber"] = ["perturbationNumber"]
1449
1450    if pdtn in {2, 3, 4, 12, 13, 14}:
1451        index, attrs = meta_check(index, attrs, "typeOfDerivedForecast")
1452
1453    if pdtn in {5, 9}:
1454        dim_coords["typeOfProbability"] = ["typeOfProbability"]
1455
1456    if pdtn in {6, 10}:
1457        dim_coords["percentileValue"] = ["percentileValue"]
1458
1459    if pdtn in {8, 15, 42, 46, 62, 67, 72, 78, 82, 1001, 1002, 1100, 1101}:
1460        index, attrs = meta_check(index, attrs, "statisticalProcess")
1461
1462    # Logic for Trace Gas and Aerosol dimensions
1463    if pdtn in {40, 41, 42, 43, 76, 77, 78, 79}:
1464        dim_coords["constituentType"] = ["constituentType"]
1465
1466    if pdtn in {76, 77, 78, 79}:
1467        dim_coords["sourceSinkIndicator"] = ["sourceSinkIndicator"]
1468
1469    if pdtn in {44, 45, 46, 47, 48, 49, 50, 80, 81, 82, 83, 84, 85}:
1470        dim_coords["typeOfAerosol"] = ["typeOfAerosol"]
1471
1472    if pdtn in {80, 81, 82, 83, 84}:
1473        dim_coords["sourceSinkIndicator"] = ["sourceSinkIndicator"]
1474
1475    if pdtn in {48, 49, 80, 81}:
1476        dim_coords["firstWavelength"] = ["firstWavelength"]
1477        dim_coords["secondWavelength"] = ["secondWavelength"]
1478        dim_coords["firstSizeOfAerosol"] = ["firstSizeOfAerosol"]
1479        dim_coords["secondSizeOfAerosol"] = ["secondSizeOfAerosol"]
1480
1481    # Finish logic by pdtn
1482
1483    for k, v in dim_coords.items():
1484        for meta in v:
1485            if meta not in index.columns:
1486                index = index.assign(**{meta: index.msg.apply(lambda msg: getattr(msg, meta))})
1487
1488    return index, dim_coords, attrs, coord_attrs

Apply filters.

Evaluate remaining dimensions based on pdtn and parse each out.

Parameters
  • index: Pandas DataFrame containing the GRIB2 message index.
  • filters: Filter GRIB2 messages to single hypercube. Dict keys can be any GRIB2 metadata attribute name.
Returns
  • index: Modified Pandas DataFrame with added GRIB2 metadata columns.
  • dim_coords: List of GRIB2 attributes that will be used for coordinates and/or dimensions.
  • attrs: Dict of metadata attributes (non-coordinates, non-geo)
def open_datatree( filename: str, *, drop_variables: Optional[List[str]] = None, filters: Optional[Mapping[str, Any]] = None, engine: str = 'grib2io', chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None, **kwargs) -> Any:
1492def open_datatree(
1493    filename: str,
1494    *,
1495    drop_variables: typing.Optional[typing.List[str]] = None,
1496    filters: typing.Optional[typing.Mapping[str, typing.Any]] = None,
1497    engine: str = "grib2io",
1498    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
1499    **kwargs,
1500) -> typing.Any:
1501    """
1502    Open a GRIB2 file as an xarray DataTree.
1503
1504    Parameters
1505    ----------
1506    filename : str
1507        Path to the GRIB2 file.
1508    drop_variables : list, optional
1509        List of variables to exclude.
1510    filters : dict, optional
1511        Filter criteria for GRIB2 messages.
1512    engine : str, optional
1513        Engine to use for opening the file, defaults to "grib2io".
1514    chunks : int, dict or 'auto', optional
1515        If chunks is provided, it is used to load the dataset into a
1516        dask-backed dataset.
1517    **kwargs : optional
1518        Additional keyword arguments passed to the xarray backend.
1519
1520    Returns
1521    -------
1522    xarray.DataTree
1523        A hierarchical DataTree representation of the GRIB2 data.
1524    """
1525    if not _HAS_DATATREE:
1526        raise ImportError("xarray version does not support DataTree functionality.")
1527
1528    if filters is None:
1529        filters = {}
1530
1531    # Open the file without any filters first to get all messages
1532    with grib2io.open(filename, _xarray_backend=True) as f:
1533        file_index = pd.DataFrame(f._index)
1534        file_index = file_index.assign(msg=msgs_from_index(f._index))
1535
1536    # Build tree structure from GRIB messages
1537    root = build_datatree_from_grib(
1538        filename,
1539        file_index,
1540        filters,
1541        drop_variables=drop_variables,
1542        chunks=chunks,
1543    )
1544
1545    # Update history for provenance
1546    now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
1547    existing_history = root.attrs.get("history", "") if hasattr(root, "attrs") else ""
1548    history = f"{now}: Initialized via grib2io.open_datatree from {filename}\n{existing_history}"
1549    if hasattr(root, "attrs"):
1550        root.attrs["history"] = history
1551    # Also add to all datasets in the tree
1552    for node in root.subtree:
1553        if node.ds is not None:
1554            node.ds.attrs["history"] = history + node.ds.attrs.get("history", "")
1555
1556    return root
1557
1558    return root

Open a GRIB2 file as an xarray DataTree.

Parameters
  • filename (str): Path to the GRIB2 file.
  • drop_variables (list, optional): List of variables to exclude.
  • filters (dict, optional): Filter criteria for GRIB2 messages.
  • engine (str, optional): Engine to use for opening the file, defaults to "grib2io".
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
  • **kwargs (optional): Additional keyword arguments passed to the xarray backend.
Returns
  • xarray.DataTree: A hierarchical DataTree representation of the GRIB2 data.
def build_da_without_coords( index: pandas.DataFrame, cube: dict, filename: str, attrs: dict) -> xarray.core.dataarray.DataArray:
1561def build_da_without_coords(index: pd.DataFrame, cube: dict, filename: str, attrs: dict) -> xr.DataArray:
1562    """
1563    Build a DataArray without coordinates from a cube of grib2 messages.
1564
1565    Parameters
1566    ----------
1567    index : pd.DataFrame
1568        Index of cube.
1569    cube : dict
1570        Cube of grib2 messages.
1571    filename : str
1572        Filename of grib2 file.
1573    attrs : dict
1574        Attributes for the DataArray.
1575
1576    Returns
1577    -------
1578    xr.DataArray
1579        DataArray without coordinates.
1580    """
1581
1582    dim_names = [k for k in cube.keys() if cube[k] is not None and len(cube[k]) > 1]
1583    constant_meta_names = [k for k in cube.keys() if cube[k] is None]
1584    dims = {k: len(cube[k]) for k in dim_names}
1585
1586    # guard against bad datarrays being formed
1587    dims_total = 1
1588    dims_to_filter = []
1589    for (
1590        dim_name,
1591        dim_len,
1592    ) in dims.items():
1593        if dim_name not in {"x", "y", "station"}:
1594            dims_total *= dim_len
1595            dims_to_filter.append(dim_name)
1596
1597    # Check number of GRIB2 message indexed compared to non-X/Y
1598    # dimensions.
1599    if dims_total != len(index):
1600        raise ValueError(
1601            f"DataArray dimensions are not compatible with number of GRIB2 messages; DataArray has {dims_total} "
1602            f"and GRIB2 index has {len(index)}. Consider applying a filter for dimensions: {dims_to_filter}"
1603        )
1604
1605    data = OnDiskArray(filename, index, cube)
1606    lock = _LOCK
1607    data = GribBackendArray(data, lock)
1608    data = indexing.LazilyIndexedArray(data)
1609    if len(dim_names) != len(data.shape):
1610        raise ValueError(
1611            "different number of dimensions on data "
1612            f"and dims: {len(data.shape)} vs {len(dim_names)}\n"
1613            "Grib2 messages could not be formed into a data cube; "
1614            "It's possible extra messages exist along a non-accounted for dimension based on PDTN\n"
1615            "It might be possible to get around this by applying a filter on the non-accounted for dimension"
1616        )
1617    da = xr.DataArray(data, dims=dim_names)
1618
1619    da.encoding["original_shape"] = data.shape
1620
1621    da.encoding["preferred_chunks"] = {"y": -1, "x": -1}
1622    msg1 = index.msg.iloc[0]
1623
1624    # plain language metadata is minimized
1625    # add grib section metadata
1626    da.attrs["GRIB2IO_section0"] = msg1.section0
1627    da.attrs["GRIB2IO_section1"] = msg1.section1
1628    da.attrs["GRIB2IO_section2"] = msg1.section2 if msg1.section2 else []
1629    da.attrs["GRIB2IO_section3"] = msg1.section3
1630    da.attrs["GRIB2IO_section4"] = msg1.section4
1631    da.attrs["GRIB2IO_section5"] = msg1.section5
1632    da.attrs["fullName"] = str(msg1.fullName)
1633    da.attrs["shortName"] = str(msg1.shortName)
1634    da.attrs["units"] = str(msg1.units)
1635    da.attrs["originatingCenter"] = str(msg1.originatingCenter.definition)
1636    da.attrs["originatingSubCenter"] = str(msg1.originatingSubCenter.definition)
1637
1638    # add master table
1639    da.attrs["masterTableInfo"] = str(msg1.masterTableInfo.definition)
1640
1641    da.name = index.shortName.iloc[0]
1642    for meta_name in constant_meta_names:
1643        if meta_name in index.columns:
1644            da.attrs[meta_name] = index[meta_name].iloc[0]
1645
1646    da.attrs.update(attrs)
1647
1648    return da

Build a DataArray without coordinates from a cube of grib2 messages.

Parameters
  • index (pd.DataFrame): Index of cube.
  • cube (dict): Cube of grib2 messages.
  • filename (str): Filename of grib2 file.
  • attrs (dict): Attributes for the DataArray.
Returns
  • xr.DataArray: DataArray without coordinates.
def assign_xr_meta( ds: xarray.core.dataset.Dataset, frames: List[pandas.DataFrame], cube: dict, non_geo_dims: Dict[str, List[str]], extra_geo: dict, coord_attrs: Dict[str, dict]) -> xarray.core.dataset.Dataset:
1651def assign_xr_meta(
1652    ds: xr.Dataset,
1653    frames: typing.List[pd.DataFrame],
1654    cube: dict,
1655    non_geo_dims: typing.Dict[str, typing.List[str]],
1656    extra_geo: dict,
1657    coord_attrs: typing.Dict[str, dict],
1658) -> xr.Dataset:
1659    """
1660    Assign coordinates and attributes to the dataset.
1661
1662    Parameters
1663    ----------
1664    ds : xr.Dataset
1665        The dataset to update.
1666    frames : list of pd.DataFrame
1667        The dataframes for each variable.
1668    cube : dict
1669        The dimensions cube.
1670    non_geo_dims : dict
1671        The non-geographic dimensions.
1672    extra_geo : dict
1673        Extra geographic coordinates.
1674    coord_attrs : dict
1675        Attributes for coordinates.
1676
1677    Returns
1678    -------
1679    xr.Dataset
1680        The updated dataset.
1681    """
1682    df = frames[0]
1683
1684    # assign extra geo coords
1685    ds = ds.assign_coords(extra_geo)
1686    # add crs data from first grib message to each data variable and the dataset
1687    geo_attrs = {
1688        "crs_wkt": CRS.from_dict(df.msg.iloc[0].projParameters).to_wkt(),
1689        "gridlengthXDirection": df.msg.iloc[0].gridlengthXDirection,
1690        "gridlengthYDirection": df.msg.iloc[0].gridlengthYDirection,
1691        "latitudeFirstGridpoint": df.msg.iloc[0].latitudeFirstGridpoint,
1692        "longitudeFirstGridpoint": df.msg.iloc[0].longitudeFirstGridpoint,
1693    }
1694    for data_var in ds.data_vars:
1695        ds[data_var].attrs.update(geo_attrs)
1696    ds.attrs.update(geo_attrs)
1697
1698    # add coordinate specific attributes
1699    for coord, attrs in coord_attrs.items():
1700        ds[coord].attrs.update(attrs)
1701
1702    # assign valid date coords
1703    try:
1704        ds = ds.assign_coords(dict(validDate=ds.coords["refDate"] + ds.coords["leadTime"]))
1705        ds.validDate.attrs["standard_name"] = "time"
1706        ds.validDate.attrs["long_name"] = "time"
1707    except Exception as e:
1708        warnings.warn(f"could not parse validTime: {e}")
1709
1710    # assign attributes
1711    ds.attrs["engine"] = "grib2io"
1712
1713    return ds

Assign coordinates and attributes to the dataset.

Parameters
  • ds (xr.Dataset): The dataset to update.
  • frames (list of pd.DataFrame): The dataframes for each variable.
  • cube (dict): The dimensions cube.
  • non_geo_dims (dict): The non-geographic dimensions.
  • extra_geo (dict): Extra geographic coordinates.
  • coord_attrs (dict): Attributes for coordinates.
Returns
  • xr.Dataset: The updated dataset.
def make_variables( index: pandas.DataFrame, f: str, non_geo_dims: Dict[str, List[str]], allow_uneven_dims: bool = False) -> Tuple[Optional[List[pandas.DataFrame]], Optional[List[dict]], Optional[dict]]:
1716def make_variables(
1717    index: pd.DataFrame,
1718    f: str,
1719    non_geo_dims: typing.Dict[str, typing.List[str]],
1720    allow_uneven_dims: bool = False,
1721) -> typing.Tuple[
1722    typing.Optional[typing.List[pd.DataFrame]],
1723    typing.Optional[typing.List[dict]],
1724    typing.Optional[dict],
1725]:
1726    """
1727    Create an individual dataframe index and cube for each variable.
1728
1729    Parameters
1730    ----------
1731    index : pd.DataFrame
1732        Index of messages.
1733    f : str
1734        Filename.
1735    non_geo_dims : dict
1736        Dimensions not associated with the x,y grid.
1737    allow_uneven_dims : bool, optional
1738        If True, allows uneven dimensions (used for DataTree creation).
1739
1740    Returns
1741    -------
1742    ordered_frames : list of pd.DataFrame, optional
1743        List of dataframes, one for each variable.
1744    cubes : list of dict, optional
1745        List of cubes, one for each variable.
1746    extra_geo : dict, optional
1747        Extra geographic coordinates.
1748    """
1749    # let shortName determine the variables
1750
1751    # set the index to the name
1752    index = index.set_index("shortName").sort_index()
1753    # return nothing if no data
1754    if index.empty:
1755        return None, None, None
1756
1757    # define the DimCube
1758    dims = copy(non_geo_dims)
1759
1760    ordered_meta = list(non_geo_dims.keys())
1761    cubes = list()
1762    ordered_frames = list()
1763    for key in index.index.unique():
1764        frame = index.loc[[key]]
1765        frame = frame.reset_index()
1766        # frame is a dataframe with all records for one variable
1767        c = dict()
1768        # for colname in frame.columns:
1769        for colname in ordered_meta:
1770            uniques = pd.Index(frame[colname]).unique()
1771            if len(uniques) > 1:
1772                c[colname] = uniques.sort_values()
1773            else:
1774                c[colname] = [uniques[0]]
1775
1776        dims = [k for k in ordered_meta if len(c[k]) > 1]
1777
1778        for dim in dims:
1779            if frame[dim].value_counts().nunique() > 1 and not allow_uneven_dims:
1780                raise ValueError(f"uneven number of grib msgs associated with dimension: {dim}\n unique values for {dim}: {frame[dim].unique()} ")
1781
1782        if len(dims) >= 1:  # dims may be empty if no extra dims on top of x,y
1783            frame = frame.sort_values(dims)
1784            frame = frame.set_index(dims)
1785
1786        cubes.append(c)
1787
1788        # miloc is multi-index integer location of msg in nd DataArray
1789        miloc = list(zip(*[frame.index.unique(level=dim).get_indexer(frame.index.get_level_values(dim)) for dim in dims]))
1790
1791        # set frame multi index
1792        if len(miloc) >= 1:  # miloc will be empty when no extra dims, thus no multiindex
1793            dim_ix = tuple([n + "_ix" for n in dims])
1794            frame = frame.set_index(pd.MultiIndex.from_tuples(miloc, names=dim_ix))
1795
1796        ordered_frames.append(frame)
1797
1798    # no variables
1799    if not cubes:
1800        cubes = [dict()]
1801
1802    # check geography of data and assign to cube
1803    if len(index.ny.unique()) > 1 or len(index.nx.unique()) > 1:
1804        raise ValueError("multiple grids not accommodated")
1805    for cube in cubes:
1806        cube["y"] = range(int(index.ny.iloc[0]))
1807        cube["x"] = range(int(index.nx.iloc[0]))
1808
1809    extra_geo = None
1810    msg = index.msg.iloc[0]
1811
1812    # we want the lat lons; make them via accessing a record; we are assuming
1813    # all records are the same grid because they have the same shape;
1814    # may want a unique grid identifier from grib2io to avoid assuming this
1815    latitude, longitude = msg.latlons()
1816    latitude = xr.DataArray(latitude, dims=["y", "x"])
1817    latitude.attrs["standard_name"] = "latitude"
1818    latitude.attrs["units"] = "degrees_north"
1819    longitude = xr.DataArray(longitude, dims=["y", "x"])
1820    longitude.attrs["standard_name"] = "longitude"
1821    longitude.attrs["units"] = "degrees_east"
1822    extra_geo = dict(latitude=latitude, longitude=longitude)
1823
1824    return ordered_frames, cubes, extra_geo

Create an individual dataframe index and cube for each variable.

Parameters
  • index (pd.DataFrame): Index of messages.
  • f (str): Filename.
  • non_geo_dims (dict): Dimensions not associated with the x,y grid.
  • allow_uneven_dims (bool, optional): If True, allows uneven dimensions (used for DataTree creation).
Returns
  • ordered_frames (list of pd.DataFrame, optional): List of dataframes, one for each variable.
  • cubes (list of dict, optional): List of cubes, one for each variable.
  • extra_geo (dict, optional): Extra geographic coordinates.
def interp_nd( a: numpy.ndarray, *, method: Union[str, int], grid_def_in: grib2io.Grib2GridDef, grid_def_out: grib2io.Grib2GridDef, method_options: Optional[List[int]] = None, num_threads: int = 1) -> numpy.ndarray:
1827def interp_nd(
1828    a: np.ndarray,
1829    *,
1830    method: typing.Union[str, int],
1831    grid_def_in: grib2io.Grib2GridDef,
1832    grid_def_out: grib2io.Grib2GridDef,
1833    method_options: typing.Optional[typing.List[int]] = None,
1834    num_threads: int = 1,
1835) -> np.ndarray:
1836    """
1837    Perform multi-dimensional interpolation on a horizontal grid.
1838
1839    This function reshapes the input array to (N, ny, nx) before performing
1840    interpolation and then reshapes it back to its original dimensions plus
1841    the new grid dimensions.
1842
1843    Parameters
1844    ----------
1845    a : np.ndarray
1846        Input array with horizontal dimensions (..., ny, nx).
1847    method : str or int
1848        Interpolation method.
1849    grid_def_in : grib2io.Grib2GridDef
1850        Input grid definition.
1851    grid_def_out : grib2io.Grib2GridDef
1852        Output grid definition.
1853    method_options : list of int, optional
1854        Interpolation options.
1855    num_threads : int, optional
1856        Number of threads for parallel interpolation.
1857
1858    Returns
1859    -------
1860    np.ndarray
1861        Interpolated array with horizontal dimensions of the output grid.
1862    """
1863    front_shape = a.shape[:-2]
1864    a = a.reshape(-1, a.shape[-2], a.shape[-1])
1865    a = grib2io.interpolate(
1866        a,
1867        method,
1868        grid_def_in,
1869        grid_def_out,
1870        method_options=method_options,
1871        num_threads=num_threads,
1872    )
1873    a = a.reshape(front_shape + (a.shape[-2], a.shape[-1]))
1874    return a

Perform multi-dimensional interpolation on a horizontal grid.

This function reshapes the input array to (N, ny, nx) before performing interpolation and then reshapes it back to its original dimensions plus the new grid dimensions.

Parameters
  • a (np.ndarray): Input array with horizontal dimensions (..., ny, nx).
  • method (str or int): Interpolation method.
  • grid_def_in (grib2io.Grib2GridDef): Input grid definition.
  • grid_def_out (grib2io.Grib2GridDef): Output grid definition.
  • method_options (list of int, optional): Interpolation options.
  • num_threads (int, optional): Number of threads for parallel interpolation.
Returns
  • np.ndarray: Interpolated array with horizontal dimensions of the output grid.
def interp_nd_stations( a: numpy.ndarray, *, method: Union[str, int], grid_def_in: grib2io.Grib2GridDef, lats: Sequence[float], lons: Sequence[float], method_options: Optional[List[int]] = None, num_threads: int = 1) -> numpy.ndarray:
1877def interp_nd_stations(
1878    a: np.ndarray,
1879    *,
1880    method: typing.Union[str, int],
1881    grid_def_in: grib2io.Grib2GridDef,
1882    lats: typing.Sequence[float],
1883    lons: typing.Sequence[float],
1884    method_options: typing.Optional[typing.List[int]] = None,
1885    num_threads: int = 1,
1886) -> np.ndarray:
1887    """
1888    Perform multi-dimensional interpolation to station points.
1889
1890    This function reshapes the input array to (N, ny, nx) before performing
1891    interpolation and then reshapes it back to its original dimensions plus
1892    the station dimension.
1893
1894    Parameters
1895    ----------
1896    a : np.ndarray
1897        Input array with horizontal dimensions (..., ny, nx).
1898    method : str or int
1899        Interpolation method.
1900    grid_def_in : grib2io.Grib2GridDef
1901        Input grid definition.
1902    lats : sequence of float
1903        Station latitudes.
1904    lons : sequence of float
1905        Station longitudes.
1906    method_options : list of int, optional
1907        Interpolation options.
1908    num_threads : int, optional
1909        Number of threads for parallel interpolation.
1910
1911    Returns
1912    -------
1913    np.ndarray
1914        Interpolated array with the last dimension representing stations.
1915    """
1916    front_shape = a.shape[:-2]
1917    a = a.reshape(-1, a.shape[-2], a.shape[-1])
1918    a = grib2io.interpolate_to_stations(
1919        a,
1920        method,
1921        grid_def_in,
1922        lats,
1923        lons,
1924        method_options=method_options,
1925        num_threads=num_threads,
1926    )
1927    a = a.reshape(front_shape + (len(lats),))
1928    return a

Perform multi-dimensional interpolation to station points.

This function reshapes the input array to (N, ny, nx) before performing interpolation and then reshapes it back to its original dimensions plus the station dimension.

Parameters
  • a (np.ndarray): Input array with horizontal dimensions (..., ny, nx).
  • method (str or int): Interpolation method.
  • grid_def_in (grib2io.Grib2GridDef): Input grid definition.
  • lats (sequence of float): Station latitudes.
  • lons (sequence of float): Station longitudes.
  • method_options (list of int, optional): Interpolation options.
  • num_threads (int, optional): Number of threads for parallel interpolation.
Returns
  • np.ndarray: Interpolated array with the last dimension representing stations.
@xr.register_dataset_accessor('grib2io')
class Grib2ioDataSet:
1931@xr.register_dataset_accessor("grib2io")
1932class Grib2ioDataSet:
1933    def __init__(self, xarray_obj):
1934        self._obj = xarray_obj
1935
1936    def griddef(self):
1937        return Grib2GridDef.from_section3(self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"])
1938
1939    def interp(
1940        self,
1941        method: typing.Union[str, int],
1942        grid_def_out: grib2io.Grib2GridDef,
1943        method_options: typing.Optional[typing.List[int]] = None,
1944        num_threads: int = 1,
1945    ) -> xr.Dataset:
1946        """
1947        Perform grid spatial interpolation on all variables in the Dataset.
1948
1949        Parameters
1950        ----------
1951        method : str or int
1952            Interpolation method.
1953        grid_def_out : grib2io.Grib2GridDef
1954            Output grid definition.
1955        method_options : list of int, optional
1956            Interpolation options.
1957        num_threads : int, optional
1958            Number of threads.
1959
1960        Returns
1961        -------
1962        xarray.Dataset
1963            Interpolated dataset.
1964        """
1965        da = self._obj.to_array()
1966        da.attrs["GRIB2IO_section3"] = self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"]
1967        da = da.grib2io.interp(method, grid_def_out, method_options=method_options, num_threads=num_threads)
1968        ds = da.to_dataset(dim="variable")
1969
1970        # Update history for provenance
1971        history = ds.attrs.get("history", "")
1972        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
1973        ds.attrs["history"] = f"{now}: Interpolated via {method} to {grid_def_out}\n{history}"
1974
1975        return ds
1976
1977    def interp_to_stations(
1978        self,
1979        method: typing.Union[str, int],
1980        calls: typing.Sequence[str],
1981        lats: typing.Sequence[float],
1982        lons: typing.Sequence[float],
1983        method_options: typing.Optional[typing.List[int]] = None,
1984        num_threads: int = 1,
1985    ) -> xr.Dataset:
1986        """
1987        Perform spatial interpolation to station points on all variables.
1988
1989        Parameters
1990        ----------
1991        method : str or int
1992            Interpolation method.
1993        calls : sequence of str
1994            Station call signs.
1995        lats : sequence of float
1996            Station latitudes.
1997        lons : sequence of float
1998            Station longitudes.
1999        method_options : list of int, optional
2000            Interpolation options.
2001        num_threads : int, optional
2002            Number of threads.
2003
2004        Returns
2005        -------
2006        xarray.Dataset
2007            Dataset interpolated to stations.
2008        """
2009        da = self._obj.to_array()
2010        da.attrs["GRIB2IO_section3"] = self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"]
2011        da = da.grib2io.interp_to_stations(
2012            method,
2013            calls,
2014            lats,
2015            lons,
2016            method_options=method_options,
2017            num_threads=num_threads,
2018        )
2019        ds = da.to_dataset(dim="variable")
2020
2021        # Update history for provenance
2022        history = ds.attrs.get("history", "")
2023        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2024        ds.attrs["history"] = f"{now}: Interpolated to {len(calls)} stations via {method}\n{history}"
2025
2026        return ds
2027
2028    def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
2029        """
2030        Write a DataSet to a grib2 file.
2031
2032        Parameters
2033        ----------
2034        filename
2035            Name of the grib2 file to write to.
2036        mode: {"x", "w", "a"}, optional, default="x"
2037            Persistence mode
2038
2039            | mode | Description                       |
2040            | :---:| :---:                             |
2041            | 'x'  | create (fail if exists)           |
2042            | 'w'  | create (overwrite if exists)      |
2043            | 'a'  | append (create if does not exist) |
2044
2045        """
2046        ds = self._obj
2047
2048        for shortName in sorted(ds):
2049            # make a DataArray from the "Data Variables" in the DataSet
2050            da = ds[shortName]
2051
2052            da.grib2io.to_grib2(filename, mode=mode)
2053            mode = "a"
2054
2055    def update_attrs(self, **kwargs):
2056        """
2057        Raises an error because Datasets don't have a .attrs attribute.
2058
2059        Parameters
2060        ----------
2061        attrs
2062            Attributes to update.
2063        """
2064        raise ValueError(f"Datasets do not have a .attrs attribute; use .grib2io.update_attrs({kwargs}) on a DataArray instead.")
2065
2066    def subset(self, *, lats=None, lons=None) -> xr.Dataset:
2067        """
2068        Subset the Dataset to a box defined by latitudes and/or longitudes.
2069
2070        Parameters
2071        ----------
2072        lats
2073            Two item list or tuple of latitudes.  Default is None which will
2074            return a subset unbounded by latitude.  The first term defines the
2075            southern boundary and the second term defines the northern
2076            boundary.
2077        lons
2078            Two item list or tuple of longitudes.  Default is None which will
2079            return a subset unbounded by longitude.  The first term defines the
2080            western boundary and the second term defines the eastern
2081            boundary.  Can follow either: 0 to 360 postive eastward, or 0 to
2082            -180 westward / 0 to 180 eastward conventions.  The longitude
2083            boundaries cannot cross 0.
2084
2085        Returns
2086        -------
2087        subset
2088            Dataset subset to the bounding box created by input 'lats'/'lons'.
2089            All gridpoints with lat/lon matching contraints are included within
2090            subset.
2091        """
2092        ds = self._obj
2093
2094        newds = xr.Dataset()
2095        for shortName in ds:
2096            newds[shortName] = ds[shortName].grib2io.subset(lats=lats, lons=lons).copy()
2097
2098        # Update history for provenance
2099        history = newds.attrs.get("history", "")
2100        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2101        newds.attrs["history"] = f"{now}: Subsetted to lats={lats}, lons={lons}\n{history}"
2102
2103        return newds
2104
2105    def compute(self, **kwargs):
2106        """
2107        Compute the Dask-backed Dataset with retries for transient errors.
2108
2109        Wraps :func:`grib2io.utils.compute_with_retries`.
2110
2111        Parameters
2112        ----------
2113        **kwargs
2114            Arguments passed to :func:`grib2io.utils.compute_with_retries`,
2115            e.g., `max_attempts` or `base_sleep`.
2116
2117        Returns
2118        -------
2119        xarray.Dataset
2120            The computed Dataset with NumPy-backed data.
2121        """
2122        from .utils import compute_with_retries
2123
2124        return compute_with_retries(self._obj, **kwargs)
Grib2ioDataSet(xarray_obj)
1933    def __init__(self, xarray_obj):
1934        self._obj = xarray_obj
def griddef(self):
1936    def griddef(self):
1937        return Grib2GridDef.from_section3(self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"])
def interp( self, method: Union[str, int], grid_def_out: grib2io.Grib2GridDef, method_options: Optional[List[int]] = None, num_threads: int = 1) -> xarray.core.dataset.Dataset:
1939    def interp(
1940        self,
1941        method: typing.Union[str, int],
1942        grid_def_out: grib2io.Grib2GridDef,
1943        method_options: typing.Optional[typing.List[int]] = None,
1944        num_threads: int = 1,
1945    ) -> xr.Dataset:
1946        """
1947        Perform grid spatial interpolation on all variables in the Dataset.
1948
1949        Parameters
1950        ----------
1951        method : str or int
1952            Interpolation method.
1953        grid_def_out : grib2io.Grib2GridDef
1954            Output grid definition.
1955        method_options : list of int, optional
1956            Interpolation options.
1957        num_threads : int, optional
1958            Number of threads.
1959
1960        Returns
1961        -------
1962        xarray.Dataset
1963            Interpolated dataset.
1964        """
1965        da = self._obj.to_array()
1966        da.attrs["GRIB2IO_section3"] = self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"]
1967        da = da.grib2io.interp(method, grid_def_out, method_options=method_options, num_threads=num_threads)
1968        ds = da.to_dataset(dim="variable")
1969
1970        # Update history for provenance
1971        history = ds.attrs.get("history", "")
1972        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
1973        ds.attrs["history"] = f"{now}: Interpolated via {method} to {grid_def_out}\n{history}"
1974
1975        return ds

Perform grid spatial interpolation on all variables in the Dataset.

Parameters
  • method (str or int): Interpolation method.
  • grid_def_out (grib2io.Grib2GridDef): Output grid definition.
  • method_options (list of int, optional): Interpolation options.
  • num_threads (int, optional): Number of threads.
Returns
  • xarray.Dataset: Interpolated dataset.
def interp_to_stations( self, method: Union[str, int], calls: Sequence[str], lats: Sequence[float], lons: Sequence[float], method_options: Optional[List[int]] = None, num_threads: int = 1) -> xarray.core.dataset.Dataset:
1977    def interp_to_stations(
1978        self,
1979        method: typing.Union[str, int],
1980        calls: typing.Sequence[str],
1981        lats: typing.Sequence[float],
1982        lons: typing.Sequence[float],
1983        method_options: typing.Optional[typing.List[int]] = None,
1984        num_threads: int = 1,
1985    ) -> xr.Dataset:
1986        """
1987        Perform spatial interpolation to station points on all variables.
1988
1989        Parameters
1990        ----------
1991        method : str or int
1992            Interpolation method.
1993        calls : sequence of str
1994            Station call signs.
1995        lats : sequence of float
1996            Station latitudes.
1997        lons : sequence of float
1998            Station longitudes.
1999        method_options : list of int, optional
2000            Interpolation options.
2001        num_threads : int, optional
2002            Number of threads.
2003
2004        Returns
2005        -------
2006        xarray.Dataset
2007            Dataset interpolated to stations.
2008        """
2009        da = self._obj.to_array()
2010        da.attrs["GRIB2IO_section3"] = self._obj[list(self._obj.data_vars)[0]].attrs["GRIB2IO_section3"]
2011        da = da.grib2io.interp_to_stations(
2012            method,
2013            calls,
2014            lats,
2015            lons,
2016            method_options=method_options,
2017            num_threads=num_threads,
2018        )
2019        ds = da.to_dataset(dim="variable")
2020
2021        # Update history for provenance
2022        history = ds.attrs.get("history", "")
2023        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2024        ds.attrs["history"] = f"{now}: Interpolated to {len(calls)} stations via {method}\n{history}"
2025
2026        return ds

Perform spatial interpolation to station points on all variables.

Parameters
  • method (str or int): Interpolation method.
  • calls (sequence of str): Station call signs.
  • lats (sequence of float): Station latitudes.
  • lons (sequence of float): Station longitudes.
  • method_options (list of int, optional): Interpolation options.
  • num_threads (int, optional): Number of threads.
Returns
  • xarray.Dataset: Dataset interpolated to stations.
def to_grib2(self, filename, mode: Literal['x', 'w', 'a'] = 'x'):
2028    def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
2029        """
2030        Write a DataSet to a grib2 file.
2031
2032        Parameters
2033        ----------
2034        filename
2035            Name of the grib2 file to write to.
2036        mode: {"x", "w", "a"}, optional, default="x"
2037            Persistence mode
2038
2039            | mode | Description                       |
2040            | :---:| :---:                             |
2041            | 'x'  | create (fail if exists)           |
2042            | 'w'  | create (overwrite if exists)      |
2043            | 'a'  | append (create if does not exist) |
2044
2045        """
2046        ds = self._obj
2047
2048        for shortName in sorted(ds):
2049            # make a DataArray from the "Data Variables" in the DataSet
2050            da = ds[shortName]
2051
2052            da.grib2io.to_grib2(filename, mode=mode)
2053            mode = "a"

Write a DataSet to a grib2 file.

Parameters
  • filename: Name of the grib2 file to write to.
  • mode ({"x", "w", "a"}, optional, default="x"): Persistence mode

    mode Description
    'x' create (fail if exists)
    'w' create (overwrite if exists)
    'a' append (create if does not exist)
def update_attrs(self, **kwargs):
2055    def update_attrs(self, **kwargs):
2056        """
2057        Raises an error because Datasets don't have a .attrs attribute.
2058
2059        Parameters
2060        ----------
2061        attrs
2062            Attributes to update.
2063        """
2064        raise ValueError(f"Datasets do not have a .attrs attribute; use .grib2io.update_attrs({kwargs}) on a DataArray instead.")

Raises an error because Datasets don't have a .attrs attribute.

Parameters
  • attrs: Attributes to update.
def subset(self, *, lats=None, lons=None) -> xarray.core.dataset.Dataset:
2066    def subset(self, *, lats=None, lons=None) -> xr.Dataset:
2067        """
2068        Subset the Dataset to a box defined by latitudes and/or longitudes.
2069
2070        Parameters
2071        ----------
2072        lats
2073            Two item list or tuple of latitudes.  Default is None which will
2074            return a subset unbounded by latitude.  The first term defines the
2075            southern boundary and the second term defines the northern
2076            boundary.
2077        lons
2078            Two item list or tuple of longitudes.  Default is None which will
2079            return a subset unbounded by longitude.  The first term defines the
2080            western boundary and the second term defines the eastern
2081            boundary.  Can follow either: 0 to 360 postive eastward, or 0 to
2082            -180 westward / 0 to 180 eastward conventions.  The longitude
2083            boundaries cannot cross 0.
2084
2085        Returns
2086        -------
2087        subset
2088            Dataset subset to the bounding box created by input 'lats'/'lons'.
2089            All gridpoints with lat/lon matching contraints are included within
2090            subset.
2091        """
2092        ds = self._obj
2093
2094        newds = xr.Dataset()
2095        for shortName in ds:
2096            newds[shortName] = ds[shortName].grib2io.subset(lats=lats, lons=lons).copy()
2097
2098        # Update history for provenance
2099        history = newds.attrs.get("history", "")
2100        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2101        newds.attrs["history"] = f"{now}: Subsetted to lats={lats}, lons={lons}\n{history}"
2102
2103        return newds

Subset the Dataset to a box defined by latitudes and/or longitudes.

Parameters
  • lats: Two item list or tuple of latitudes. Default is None which will return a subset unbounded by latitude. The first term defines the southern boundary and the second term defines the northern boundary.
  • lons: Two item list or tuple of longitudes. Default is None which will return a subset unbounded by longitude. The first term defines the western boundary and the second term defines the eastern boundary. Can follow either: 0 to 360 postive eastward, or 0 to -180 westward / 0 to 180 eastward conventions. The longitude boundaries cannot cross 0.
Returns
  • subset: Dataset subset to the bounding box created by input 'lats'/'lons'. All gridpoints with lat/lon matching contraints are included within subset.
def compute(self, **kwargs):
2105    def compute(self, **kwargs):
2106        """
2107        Compute the Dask-backed Dataset with retries for transient errors.
2108
2109        Wraps :func:`grib2io.utils.compute_with_retries`.
2110
2111        Parameters
2112        ----------
2113        **kwargs
2114            Arguments passed to :func:`grib2io.utils.compute_with_retries`,
2115            e.g., `max_attempts` or `base_sleep`.
2116
2117        Returns
2118        -------
2119        xarray.Dataset
2120            The computed Dataset with NumPy-backed data.
2121        """
2122        from .utils import compute_with_retries
2123
2124        return compute_with_retries(self._obj, **kwargs)

Compute the Dask-backed Dataset with retries for transient errors.

Wraps grib2io.utils.compute_with_retries().

Parameters
Returns
  • xarray.Dataset: The computed Dataset with NumPy-backed data.
@xr.register_dataarray_accessor('grib2io')
class Grib2ioDataArray:
2127@xr.register_dataarray_accessor("grib2io")
2128class Grib2ioDataArray:
2129    def __init__(self, xarray_obj):
2130        self._obj = xarray_obj
2131
2132    def griddef(self):
2133        return Grib2GridDef.from_section3(self._obj.attrs["GRIB2IO_section3"])
2134
2135    def interp(self, method, grid_def_out, method_options=None, num_threads=1) -> xr.DataArray:
2136        """
2137        Perform grid spatial interpolation.
2138
2139        Uses the [NCEPLIBS-ip library](https://github.com/NOAA-EMC/NCEPLIBS-ip).
2140
2141        Parameters
2142        ----------
2143        method
2144            Interpolate method to use. This can either be an integer or string
2145            using the following mapping:
2146
2147            | Interpolate Scheme | Integer Value |
2148            | :---:              | :---:         |
2149            | 'bilinear'         | 0             |
2150            | 'bicubic'          | 1             |
2151            | 'neighbor'         | 2             |
2152            | 'budget'           | 3             |
2153            | 'spectral'         | 4             |
2154            | 'neighbor-budget'  | 6             |
2155        grid_def_out
2156            Grib2GridDef object of the output grid.
2157        method_options : list of ints, optional
2158            Interpolation options. See the NCEPLIBS-ip documentation for
2159            more information on how these are used.
2160        num_threads : int, optional
2161            Number of OpenMP threads to use for interpolation. The default
2162            value is 1. If grib2io_interp was not built with OpenMP, then
2163            this keyword argument and value will have no impact.
2164
2165        Returns
2166        -------
2167        interp
2168            DataSet interpolated to new grid definition.  The attribute
2169            GRIB2IO_section3 is replaced with the section3 array from the new
2170            grid definition.
2171        """
2172        da = self._obj
2173        # ensure that y, x are rightmost dims; they should be if opening with
2174        # grib2io engine
2175
2176        # gdtn and gdt is not the entirety of the new s3
2177        npoints = grid_def_out.npoints
2178        s3_new = np.array([0, npoints, 0, 0, grid_def_out.gdtn] + list(grid_def_out.gdt))
2179
2180        # make new lat lons
2181        lats, lons = Grib2Message(section3=s3_new, pdtn=0, drtn=0).grid()
2182        latitude = xr.DataArray(lats, dims=["y", "x"])
2183        longitude = xr.DataArray(lons, dims=["y", "x"])
2184
2185        # create new coords
2186        new_coords = dict(da.coords)
2187        del new_coords["latitude"]
2188        del new_coords["longitude"]
2189        new_coords["longitude"] = longitude
2190        new_coords["latitude"] = latitude
2191
2192        # make grid def in from section3 on da.attrs
2193        grid_def_in = self.griddef()
2194
2195        if da.chunks is None:
2196            data = interp_nd(
2197                da.data,
2198                method=method,
2199                grid_def_in=grid_def_in,
2200                grid_def_out=grid_def_out,
2201                method_options=method_options,
2202                num_threads=num_threads,
2203            )
2204        else:
2205            data = da.data.map_blocks(
2206                interp_nd,
2207                method=method,
2208                grid_def_in=grid_def_in,
2209                grid_def_out=grid_def_out,
2210                method_options=method_options,
2211                chunks=da.chunks[:-2] + latitude.shape,
2212                dtype=da.dtype,
2213            )
2214
2215        new_da = xr.DataArray(data, dims=da.dims, coords=new_coords, attrs=da.attrs)
2216
2217        new_da.attrs["GRIB2IO_section3"] = s3_new
2218        new_da.name = da.name
2219
2220        # Update history for provenance
2221        history = new_da.attrs.get("history", "")
2222        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2223        new_da.attrs["history"] = f"{now}: Interpolated via {method} to {grid_def_out}\n{history}"
2224
2225        return new_da
2226
2227    def interp_to_stations(
2228        self,
2229        method: typing.Union[str, int],
2230        calls: typing.Sequence[str],
2231        lats: typing.Sequence[float],
2232        lons: typing.Sequence[float],
2233        method_options: typing.Optional[typing.List[int]] = None,
2234        num_threads: int = 1,
2235    ) -> xr.DataArray:
2236        """
2237        Perform spatial interpolation to station points.
2238
2239        Parameters
2240        ----------
2241        method : str or int
2242            Interpolate method to use. This can either be an integer or string
2243            using the following mapping:
2244
2245            | Interpolate Scheme | Integer Value |
2246            | :---:              | :---:         |
2247            | 'bilinear'         | 0             |
2248            | 'bicubic'          | 1             |
2249            | 'neighbor'         | 2             |
2250            | 'budget'           | 3             |
2251            | 'spectral'         | 4             |
2252            | 'neighbor-budget'  | 6             |
2253
2254        calls : sequence of str
2255            Station calls used for labeling new station index coordinate
2256        lats : sequence of float
2257            Latitudes of the station points.
2258        lons : sequence of float
2259            Longitudes of the station points.
2260        method_options : list of int, optional
2261            Interpolation options.
2262        num_threads : int, optional
2263            Number of threads.
2264
2265        Returns
2266        -------
2267        xarray.DataArray
2268            DataArray interpolated to lat and lon locations and labeled with
2269            dimension and coordinate 'station'. (..., y, x) -> (..., station)
2270        """
2271        da = self._obj
2272        # TODO ensure that y, x are rightmost dims; they should be if opening
2273        # with grib2io engine
2274
2275        calls = np.asarray(calls)
2276        lats = np.asarray(lats)
2277        lons = np.asarray(lons)
2278        latitude = xr.DataArray(lats, dims=["station"])
2279        longitude = xr.DataArray(lons, dims=["station"])
2280
2281        # create new coords
2282        new_coords = dict(da.coords)
2283        del new_coords["latitude"]
2284        del new_coords["longitude"]
2285        new_coords["longitude"] = longitude
2286        new_coords["latitude"] = latitude
2287        new_coords["station"] = calls
2288
2289        new_dims = da.dims[:-2] + ("station",)
2290
2291        # make grid def in from section3 on da attrs
2292        grid_def_in = self.griddef()
2293
2294        if da.chunks is None:
2295            data = interp_nd_stations(
2296                da.data,
2297                method=method,
2298                grid_def_in=grid_def_in,
2299                lats=lats,
2300                lons=lons,
2301                method_options=method_options,
2302                num_threads=num_threads,
2303            )
2304        else:
2305            data = da.data.map_blocks(
2306                interp_nd_stations,
2307                method=method,
2308                grid_def_in=grid_def_in,
2309                lats=lats,
2310                lons=lons,
2311                method_options=method_options,
2312                drop_axis=-1,
2313                chunks=da.chunks[:-2] + latitude.shape,
2314                dtype=da.dtype,
2315            )
2316
2317        new_da = xr.DataArray(data, dims=new_dims, coords=new_coords, attrs=da.attrs)
2318
2319        new_da.name = da.name
2320
2321        # Update history for provenance
2322        history = new_da.attrs.get("history", "")
2323        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2324        new_da.attrs["history"] = f"{now}: Interpolated to {len(calls)} stations via {method}\n{history}"
2325
2326        return new_da
2327
2328    def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
2329        """
2330        Write a DataArray to a grib2 file.
2331
2332        Parameters
2333        ----------
2334        filename
2335            Name of the grib2 file to write to.
2336        mode: {"x", "w", "a"}, optional, default="x"
2337            Persistence mode
2338
2339            +------+-----------------------------------+
2340            | mode | Description                       |
2341            +======+===================================+
2342            | x    | create (fail if exists)           |
2343            +------+-----------------------------------+
2344            | w    | create (overwrite if exists)      |
2345            +------+-----------------------------------+
2346            | a    | append (create if does not exist) |
2347            +------+-----------------------------------+
2348
2349        """
2350        da = self._obj.copy(deep=True)
2351
2352        coords_keys = sorted(da.coords.keys())
2353        coords_keys = [k for k in coords_keys if k in AVAILABLE_NON_GEO_COORDS]
2354
2355        # If there are dimension coordinates, the DataArray is a hypercube of
2356        # grib2 messages.
2357
2358        # Create `indexes` which is a list of lists of dictionaries for all
2359        # dimension coordinates. Each dictionary key is the dimension
2360        # coordinate name and the value is a list of the dimension coordinate
2361        # values.  This allows for easy iteration over all possible grib2
2362        # messages in the DataArray by using itertools.product.
2363        #
2364        # For example:
2365        # indexes = [
2366        #     [
2367        #         {"leadTime": 9},
2368        #         {"leadTime": 12},
2369        #     ],
2370        #     [
2371        #         {"valueOfFirstFixedSurface": 900},
2372        #         {"valueOfFirstFixedSurface": 925},
2373        #         {"valueOfFirstFixedSurface": 950},
2374        #     ],
2375        # ]
2376
2377        # assign loc indexes to dimensions without indexes for uniform selection by name
2378        loc_indexes = list()
2379        for dim in da.dims:
2380            if dim not in da.indexes:
2381                da = da.assign_coords({dim: range(da[dim].size)})
2382                loc_indexes.append(dim)
2383
2384        indexes = []
2385        for index in [i for i in AVAILABLE_NON_GEO_DIMS if i in da.dims]:
2386            values = da.coords[index].values
2387            if len(values) != len(set(values)):
2388                raise ValueError(
2389                    f"Dimension coordinate '{index}' has duplicate values, but to_grib2 requires unique values to find each GRIB2 message in the DataArray."
2390                )
2391            listeach = [{index: value} for value in sorted(values)]
2392            indexes.append(listeach)
2393
2394        # If `dim_coords` is [], then the DataArray is a single grib2 message and
2395        # itertools.product(*dim_coords) will run once with `selectors = ()`.
2396        for selectors in itertools.product(*indexes):
2397            # Need to find the correct data in the DataArray based on the
2398            # dimension coordinates.
2399            filters = {k: v for d in selectors for k, v in d.items()}
2400
2401            # If `filters` is {}, then the DataArray is a single grib2 message
2402            # and da.sel(indexers={}) returns the DataArray.
2403            selected = da.sel(indexers=filters)
2404
2405            newmsg = Grib2Message(
2406                selected.attrs["GRIB2IO_section0"],
2407                selected.attrs["GRIB2IO_section1"],
2408                selected.attrs["GRIB2IO_section2"],
2409                selected.attrs["GRIB2IO_section3"],
2410                selected.attrs["GRIB2IO_section4"],
2411                selected.attrs["GRIB2IO_section5"],
2412            )
2413            newmsg.data = np.array(selected.data)
2414
2415            # For dimension coordinates, set the grib2 message metadata to the
2416            # dimension coordinate value.
2417            for index, value in filters.items():
2418                if index not in loc_indexes:
2419                    setattr(newmsg, index, value)
2420
2421            # For non-dimension coordinates, set the grib2 message metadata to
2422            # the DataArray coordinate value.
2423            for index in [i for i in coords_keys if i not in da.dims]:
2424                setattr(newmsg, index, selected.coords[index].values)
2425
2426            # Set section 5 attributes to the da.encoding dictionary.
2427            for key, value in selected.encoding.items():
2428                if key in ["dtype", "chunks", "original_shape"]:
2429                    continue
2430                setattr(newmsg, key, value)
2431
2432            # write the message to file
2433            with grib2io.open(filename, mode=mode) as f:
2434                f.write(newmsg)
2435            mode = "a"
2436
2437    def update_attrs(self, **kwargs):
2438        """
2439        Update many of the attributes of the DataArray.
2440
2441        Parameters
2442        ----------
2443        **kwargs
2444            Attributes to update.  This can include many of the GRIB2IO message
2445            attributes that you can find when you print a GRIB2IO message. For
2446            conflicting updates, the last keyword will be used.
2447
2448            +-----------------------+------------------------------------------+
2449            | kwargs                | Description                              |
2450            +=======================+==========================================+
2451            | shortName="VTMP"      | Set shortName to "VTMP", along with      |
2452            |                       | appropriate discipline,                  |
2453            |                       | parameterCategory, parameterNumber,      |
2454            |                       | fullName and units.                      |
2455            +-----------------------+------------------------------------------+
2456            | discipline=0,         | Set shortName, discipline,               |
2457            | parameterCategory=0,  | parameterCategory, parameterNumber,      |
2458            | parameterNumber=1     | fullName and units appropriate for       |
2459            |                       | "Virtual Temperature".                   |
2460            +-----------------------+------------------------------------------+
2461            | discipline=0,         | Conflicting keywords but                 |
2462            | parameterCategory=0,  | 'shortName="TMP"' wins.  Set shortName,  |
2463            | parameterNumber=1,    | discipline, parameterCategory,           |
2464            | shortName="TMP"       | parameterNumber, fullName and units      |
2465            |                       | appropriate for "Temperature".           |
2466            +-----------------------+------------------------------------------+
2467
2468        Returns
2469        -------
2470        DataArray
2471            DataArray with updated attributes.
2472        """
2473        da = self._obj.copy(deep=True)
2474
2475        newmsg = Grib2Message(
2476            da.attrs["GRIB2IO_section0"],
2477            da.attrs["GRIB2IO_section1"],
2478            da.attrs["GRIB2IO_section2"],
2479            da.attrs["GRIB2IO_section3"],
2480            da.attrs["GRIB2IO_section4"],
2481            da.attrs["GRIB2IO_section5"],
2482        )
2483
2484        coords_keys = [k for k in da.coords.keys() if k in AVAILABLE_NON_GEO_COORDS]
2485
2486        for grib2_name, value in kwargs.items():
2487            if grib2_name == "gridDefinitionTemplateNumber":
2488                raise ValueError(
2489                    "The gridDefinitionTemplateNumber attribute cannot be updated.  The best way to change to a different grid is to interpolate the data to a new grid using the grib2io interpolate functions."
2490                )
2491            if grib2_name == "productDefinitionTemplateNumber":
2492                raise ValueError("The productDefinitionTemplateNumber attribute cannot be updated.")
2493            if grib2_name == "dataRepresentationTemplateNumber":
2494                raise ValueError("The dataRepresentationTemplateNumber attribute cannot be updated.")
2495            if grib2_name in coords_keys:
2496                warnings.warn(f"Skipping attribute '{grib2_name}' because it is a coordinate. Use da.assign_coords() to change coordinate values.")
2497                continue
2498            if hasattr(newmsg, grib2_name):
2499                setattr(newmsg, grib2_name, value)
2500            else:
2501                warnings.warn(f"Skipping attribute '{grib2_name}' because it is not a valid GRIB2 attribute for this message and cannot be updated.")
2502                continue
2503
2504        da.attrs["GRIB2IO_section0"] = newmsg.section0
2505        da.attrs["GRIB2IO_section1"] = newmsg.section1
2506        da.attrs["GRIB2IO_section2"] = newmsg.section2 or []
2507        da.attrs["GRIB2IO_section3"] = newmsg.section3
2508        da.attrs["GRIB2IO_section4"] = newmsg.section4
2509        da.attrs["GRIB2IO_section5"] = newmsg.section5
2510        da.attrs["fullName"] = newmsg.fullName
2511        da.attrs["shortName"] = newmsg.shortName
2512        da.attrs["units"] = newmsg.units
2513
2514        return da
2515
2516    def update_section3(self) -> xr.DataArray:
2517        """
2518        Update section3 attributes based on the latitude and longitude corners.
2519
2520        This makes the GRIB2IO_section3 attribute consistent with the grid's
2521        new corners after a change in the spatial extent.
2522        """
2523        da = self._obj
2524        if "GRIB2IO_section3" not in da.attrs:
2525            raise ValueError(
2526                "DataArray has no attr 'GRIB2IO_section3'.  This function only works with Datasets/DataArrrays opened with the 'grib2io' backend."
2527            )
2528        if "latitude" not in da.coords:
2529            raise ValueError("DataArray has no coord 'latitude'")
2530        if "longitude" not in da.coords:
2531            raise ValueError("DataArray has no coord 'longitude'")
2532
2533        grid = Grid(da.attrs["GRIB2IO_section3"])
2534
2535        if grid.gdtn not in [0, 1, 10, 20, 30, 31, 40, 110]:
2536            raise ValueError(
2537                textwrap.dedent("""\
2538                    update_section3 only works for:
2539
2540                    Latitude/Longitude, Equidistant Cylindrical, or Plate Carree (gdtn=0)
2541                    Rotated Latitude/Longitude (gdtn=1)
2542                    Mercator (gdtn=10)
2543                    Polar Stereographic (gdtn=20)
2544                    Lambert Conformal (gdtn=30)
2545                    Albers Equal-Area (gdtn=31)
2546                    Gaussian Latitude/Longitude (gdtn=40)
2547                    Equatorial Azimuthal Equidistant Projection (gdtn=110)
2548                    """)
2549            )
2550
2551        grid.latitudeFirstGridpoint = da.latitude.isel(y=0, x=0)
2552        grid.longitudeFirstGridpoint = da.longitude.isel(y=0, x=0)
2553        grid.nx = len(da.x)
2554        grid.ny = len(da.y)
2555
2556        # last gridpoint does not affect section3 for some gdt but set anyway
2557        grid.latitudeLastGridpoint = da.latitude.isel(y=-1, x=-1)
2558        grid.longitudeLastGridpoint = da.longitude.isel(y=-1, x=-1)
2559
2560        da.attrs["GRIB2IO_section3"] = grid.section3
2561
2562        return da
2563
2564    def subset(self, *, lats=None, lons=None) -> xr.DataArray:
2565        """
2566        Subset the DataArray to a box defined by latitudes and/or longitudes.
2567
2568        Parameters
2569        ----------
2570        lats
2571            Two item list or tuple of latitudes.  Default is None which will
2572            return a subset unbounded by latitude.  The first term defines the
2573            southern boundary and the second term defines the northern
2574            boundary.
2575        lons
2576            Two item list or tuple of longitudes.  Default is None which will
2577            return a subset unbounded by longitude.  The first term defines the
2578            western boundary and the second term defines the eastern
2579            boundary.  Can follow either: 0 to 360 postive eastward, or 0 to
2580            -180 westward / 0 to 180 eastward conventions.  The longitude
2581            boundaries cannot cross 0.
2582
2583        Returns
2584        -------
2585        subset
2586            DataArray subset to the bounding box created by input 'lats'/'lons'.
2587            All gridpoints with lat/lon matching contraints are included within
2588            subset.
2589        """
2590
2591        def slice_from_contiguous_mask(mask):
2592            indices = np.where(mask)[0]
2593
2594            if indices.size > 0:
2595                # slice(start, stop) - stop is exclusive, so we add 1
2596                my_slice = slice(indices[0], indices[-1] + 1)
2597            else:
2598                my_slice = slice(0, 0)
2599            return my_slice
2600
2601        da = self._obj.copy()
2602
2603        if lats is None:
2604            lats = (np.min(da.latitude), np.max(da.latitude))
2605        else:
2606            lats = (min(lats), max(lats))
2607
2608        if lons is None:
2609            lons = (np.min(da.longitude), np.max(da.longitude))
2610        else:
2611            lons = (min(lons), max(lons))
2612
2613        # Internally work in common lon data representation (0->360 positive eastward from 0)
2614        lons = np.mod(np.array(lons) + 360, 360)
2615        lon_da = np.mod(da.longitude + 360, 360)
2616
2617        snap_first_point = snap_to_nearest_cell_center(da.latitude, lon_da, lats[0], lons[0])
2618        snap_last_point = snap_to_nearest_cell_center(da.latitude, lon_da, lats[1], lons[1])
2619        lats = (snap_first_point[0], snap_last_point[0])
2620        lons = (snap_first_point[1], snap_last_point[1])
2621
2622        x = ((lon_da >= lons[0]) & (lon_da <= lons[1])).any("y")
2623        if x.chunks:
2624            x = x.compute()
2625
2626        y = ((da.latitude >= lats[0]) & (da.latitude <= lats[1])).any("x")
2627        if y.chunks:
2628            y = y.compute()
2629
2630        y_slice = slice_from_contiguous_mask(y)
2631        x_slice = slice_from_contiguous_mask(x)
2632
2633        da = da.isel(y=y_slice, x=x_slice)
2634        if da.size < 1:
2635            raise ValueError("None of grid data is within given lat/lon bounds.")
2636
2637        da = da.grib2io.update_section3()
2638
2639        return da
2640
2641    def compute(self, **kwargs):
2642        """
2643        Compute the Dask-backed DataArray with retries for transient errors.
2644
2645        Wraps :func:`grib2io.utils.compute_with_retries`.
2646
2647        Parameters
2648        ----------
2649        **kwargs
2650            Arguments passed to :func:`grib2io.utils.compute_with_retries`,
2651            e.g., `max_attempts` or `base_sleep`.
2652
2653        Returns
2654        -------
2655        xarray.DataArray
2656            The computed DataArray with NumPy-backed data.
2657        """
2658        from .utils import compute_with_retries
2659
2660        return compute_with_retries(self._obj, **kwargs)
Grib2ioDataArray(xarray_obj)
2129    def __init__(self, xarray_obj):
2130        self._obj = xarray_obj
def griddef(self):
2132    def griddef(self):
2133        return Grib2GridDef.from_section3(self._obj.attrs["GRIB2IO_section3"])
def interp( self, method, grid_def_out, method_options=None, num_threads=1) -> xarray.core.dataarray.DataArray:
2135    def interp(self, method, grid_def_out, method_options=None, num_threads=1) -> xr.DataArray:
2136        """
2137        Perform grid spatial interpolation.
2138
2139        Uses the [NCEPLIBS-ip library](https://github.com/NOAA-EMC/NCEPLIBS-ip).
2140
2141        Parameters
2142        ----------
2143        method
2144            Interpolate method to use. This can either be an integer or string
2145            using the following mapping:
2146
2147            | Interpolate Scheme | Integer Value |
2148            | :---:              | :---:         |
2149            | 'bilinear'         | 0             |
2150            | 'bicubic'          | 1             |
2151            | 'neighbor'         | 2             |
2152            | 'budget'           | 3             |
2153            | 'spectral'         | 4             |
2154            | 'neighbor-budget'  | 6             |
2155        grid_def_out
2156            Grib2GridDef object of the output grid.
2157        method_options : list of ints, optional
2158            Interpolation options. See the NCEPLIBS-ip documentation for
2159            more information on how these are used.
2160        num_threads : int, optional
2161            Number of OpenMP threads to use for interpolation. The default
2162            value is 1. If grib2io_interp was not built with OpenMP, then
2163            this keyword argument and value will have no impact.
2164
2165        Returns
2166        -------
2167        interp
2168            DataSet interpolated to new grid definition.  The attribute
2169            GRIB2IO_section3 is replaced with the section3 array from the new
2170            grid definition.
2171        """
2172        da = self._obj
2173        # ensure that y, x are rightmost dims; they should be if opening with
2174        # grib2io engine
2175
2176        # gdtn and gdt is not the entirety of the new s3
2177        npoints = grid_def_out.npoints
2178        s3_new = np.array([0, npoints, 0, 0, grid_def_out.gdtn] + list(grid_def_out.gdt))
2179
2180        # make new lat lons
2181        lats, lons = Grib2Message(section3=s3_new, pdtn=0, drtn=0).grid()
2182        latitude = xr.DataArray(lats, dims=["y", "x"])
2183        longitude = xr.DataArray(lons, dims=["y", "x"])
2184
2185        # create new coords
2186        new_coords = dict(da.coords)
2187        del new_coords["latitude"]
2188        del new_coords["longitude"]
2189        new_coords["longitude"] = longitude
2190        new_coords["latitude"] = latitude
2191
2192        # make grid def in from section3 on da.attrs
2193        grid_def_in = self.griddef()
2194
2195        if da.chunks is None:
2196            data = interp_nd(
2197                da.data,
2198                method=method,
2199                grid_def_in=grid_def_in,
2200                grid_def_out=grid_def_out,
2201                method_options=method_options,
2202                num_threads=num_threads,
2203            )
2204        else:
2205            data = da.data.map_blocks(
2206                interp_nd,
2207                method=method,
2208                grid_def_in=grid_def_in,
2209                grid_def_out=grid_def_out,
2210                method_options=method_options,
2211                chunks=da.chunks[:-2] + latitude.shape,
2212                dtype=da.dtype,
2213            )
2214
2215        new_da = xr.DataArray(data, dims=da.dims, coords=new_coords, attrs=da.attrs)
2216
2217        new_da.attrs["GRIB2IO_section3"] = s3_new
2218        new_da.name = da.name
2219
2220        # Update history for provenance
2221        history = new_da.attrs.get("history", "")
2222        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2223        new_da.attrs["history"] = f"{now}: Interpolated via {method} to {grid_def_out}\n{history}"
2224
2225        return new_da

Perform grid spatial interpolation.

Uses the NCEPLIBS-ip library.

Parameters
  • method: Interpolate method to use. This can either be an integer or string using the following mapping:
Interpolate Scheme Integer Value
'bilinear' 0
'bicubic' 1
'neighbor' 2
'budget' 3
'spectral' 4
'neighbor-budget' 6

  • grid_def_out: Grib2GridDef object of the output grid.
  • method_options (list of ints, optional): Interpolation options. See the NCEPLIBS-ip documentation for more information on how these are used.
  • num_threads (int, optional): Number of OpenMP threads to use for interpolation. The default value is 1. If grib2io_interp was not built with OpenMP, then this keyword argument and value will have no impact.
Returns
  • interp: DataSet interpolated to new grid definition. The attribute GRIB2IO_section3 is replaced with the section3 array from the new grid definition.
def interp_to_stations( self, method: Union[str, int], calls: Sequence[str], lats: Sequence[float], lons: Sequence[float], method_options: Optional[List[int]] = None, num_threads: int = 1) -> xarray.core.dataarray.DataArray:
2227    def interp_to_stations(
2228        self,
2229        method: typing.Union[str, int],
2230        calls: typing.Sequence[str],
2231        lats: typing.Sequence[float],
2232        lons: typing.Sequence[float],
2233        method_options: typing.Optional[typing.List[int]] = None,
2234        num_threads: int = 1,
2235    ) -> xr.DataArray:
2236        """
2237        Perform spatial interpolation to station points.
2238
2239        Parameters
2240        ----------
2241        method : str or int
2242            Interpolate method to use. This can either be an integer or string
2243            using the following mapping:
2244
2245            | Interpolate Scheme | Integer Value |
2246            | :---:              | :---:         |
2247            | 'bilinear'         | 0             |
2248            | 'bicubic'          | 1             |
2249            | 'neighbor'         | 2             |
2250            | 'budget'           | 3             |
2251            | 'spectral'         | 4             |
2252            | 'neighbor-budget'  | 6             |
2253
2254        calls : sequence of str
2255            Station calls used for labeling new station index coordinate
2256        lats : sequence of float
2257            Latitudes of the station points.
2258        lons : sequence of float
2259            Longitudes of the station points.
2260        method_options : list of int, optional
2261            Interpolation options.
2262        num_threads : int, optional
2263            Number of threads.
2264
2265        Returns
2266        -------
2267        xarray.DataArray
2268            DataArray interpolated to lat and lon locations and labeled with
2269            dimension and coordinate 'station'. (..., y, x) -> (..., station)
2270        """
2271        da = self._obj
2272        # TODO ensure that y, x are rightmost dims; they should be if opening
2273        # with grib2io engine
2274
2275        calls = np.asarray(calls)
2276        lats = np.asarray(lats)
2277        lons = np.asarray(lons)
2278        latitude = xr.DataArray(lats, dims=["station"])
2279        longitude = xr.DataArray(lons, dims=["station"])
2280
2281        # create new coords
2282        new_coords = dict(da.coords)
2283        del new_coords["latitude"]
2284        del new_coords["longitude"]
2285        new_coords["longitude"] = longitude
2286        new_coords["latitude"] = latitude
2287        new_coords["station"] = calls
2288
2289        new_dims = da.dims[:-2] + ("station",)
2290
2291        # make grid def in from section3 on da attrs
2292        grid_def_in = self.griddef()
2293
2294        if da.chunks is None:
2295            data = interp_nd_stations(
2296                da.data,
2297                method=method,
2298                grid_def_in=grid_def_in,
2299                lats=lats,
2300                lons=lons,
2301                method_options=method_options,
2302                num_threads=num_threads,
2303            )
2304        else:
2305            data = da.data.map_blocks(
2306                interp_nd_stations,
2307                method=method,
2308                grid_def_in=grid_def_in,
2309                lats=lats,
2310                lons=lons,
2311                method_options=method_options,
2312                drop_axis=-1,
2313                chunks=da.chunks[:-2] + latitude.shape,
2314                dtype=da.dtype,
2315            )
2316
2317        new_da = xr.DataArray(data, dims=new_dims, coords=new_coords, attrs=da.attrs)
2318
2319        new_da.name = da.name
2320
2321        # Update history for provenance
2322        history = new_da.attrs.get("history", "")
2323        now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2324        new_da.attrs["history"] = f"{now}: Interpolated to {len(calls)} stations via {method}\n{history}"
2325
2326        return new_da

Perform spatial interpolation to station points.

Parameters
  • method (str or int): Interpolate method to use. This can either be an integer or string using the following mapping:

    Interpolate Scheme Integer Value
    'bilinear' 0
    'bicubic' 1
    'neighbor' 2
    'budget' 3
    'spectral' 4
    'neighbor-budget' 6
  • calls (sequence of str): Station calls used for labeling new station index coordinate
  • lats (sequence of float): Latitudes of the station points.
  • lons (sequence of float): Longitudes of the station points.
  • method_options (list of int, optional): Interpolation options.
  • num_threads (int, optional): Number of threads.
Returns
  • xarray.DataArray: DataArray interpolated to lat and lon locations and labeled with dimension and coordinate 'station'. (..., y, x) -> (..., station)
def to_grib2(self, filename, mode: Literal['x', 'w', 'a'] = 'x'):
2328    def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
2329        """
2330        Write a DataArray to a grib2 file.
2331
2332        Parameters
2333        ----------
2334        filename
2335            Name of the grib2 file to write to.
2336        mode: {"x", "w", "a"}, optional, default="x"
2337            Persistence mode
2338
2339            +------+-----------------------------------+
2340            | mode | Description                       |
2341            +======+===================================+
2342            | x    | create (fail if exists)           |
2343            +------+-----------------------------------+
2344            | w    | create (overwrite if exists)      |
2345            +------+-----------------------------------+
2346            | a    | append (create if does not exist) |
2347            +------+-----------------------------------+
2348
2349        """
2350        da = self._obj.copy(deep=True)
2351
2352        coords_keys = sorted(da.coords.keys())
2353        coords_keys = [k for k in coords_keys if k in AVAILABLE_NON_GEO_COORDS]
2354
2355        # If there are dimension coordinates, the DataArray is a hypercube of
2356        # grib2 messages.
2357
2358        # Create `indexes` which is a list of lists of dictionaries for all
2359        # dimension coordinates. Each dictionary key is the dimension
2360        # coordinate name and the value is a list of the dimension coordinate
2361        # values.  This allows for easy iteration over all possible grib2
2362        # messages in the DataArray by using itertools.product.
2363        #
2364        # For example:
2365        # indexes = [
2366        #     [
2367        #         {"leadTime": 9},
2368        #         {"leadTime": 12},
2369        #     ],
2370        #     [
2371        #         {"valueOfFirstFixedSurface": 900},
2372        #         {"valueOfFirstFixedSurface": 925},
2373        #         {"valueOfFirstFixedSurface": 950},
2374        #     ],
2375        # ]
2376
2377        # assign loc indexes to dimensions without indexes for uniform selection by name
2378        loc_indexes = list()
2379        for dim in da.dims:
2380            if dim not in da.indexes:
2381                da = da.assign_coords({dim: range(da[dim].size)})
2382                loc_indexes.append(dim)
2383
2384        indexes = []
2385        for index in [i for i in AVAILABLE_NON_GEO_DIMS if i in da.dims]:
2386            values = da.coords[index].values
2387            if len(values) != len(set(values)):
2388                raise ValueError(
2389                    f"Dimension coordinate '{index}' has duplicate values, but to_grib2 requires unique values to find each GRIB2 message in the DataArray."
2390                )
2391            listeach = [{index: value} for value in sorted(values)]
2392            indexes.append(listeach)
2393
2394        # If `dim_coords` is [], then the DataArray is a single grib2 message and
2395        # itertools.product(*dim_coords) will run once with `selectors = ()`.
2396        for selectors in itertools.product(*indexes):
2397            # Need to find the correct data in the DataArray based on the
2398            # dimension coordinates.
2399            filters = {k: v for d in selectors for k, v in d.items()}
2400
2401            # If `filters` is {}, then the DataArray is a single grib2 message
2402            # and da.sel(indexers={}) returns the DataArray.
2403            selected = da.sel(indexers=filters)
2404
2405            newmsg = Grib2Message(
2406                selected.attrs["GRIB2IO_section0"],
2407                selected.attrs["GRIB2IO_section1"],
2408                selected.attrs["GRIB2IO_section2"],
2409                selected.attrs["GRIB2IO_section3"],
2410                selected.attrs["GRIB2IO_section4"],
2411                selected.attrs["GRIB2IO_section5"],
2412            )
2413            newmsg.data = np.array(selected.data)
2414
2415            # For dimension coordinates, set the grib2 message metadata to the
2416            # dimension coordinate value.
2417            for index, value in filters.items():
2418                if index not in loc_indexes:
2419                    setattr(newmsg, index, value)
2420
2421            # For non-dimension coordinates, set the grib2 message metadata to
2422            # the DataArray coordinate value.
2423            for index in [i for i in coords_keys if i not in da.dims]:
2424                setattr(newmsg, index, selected.coords[index].values)
2425
2426            # Set section 5 attributes to the da.encoding dictionary.
2427            for key, value in selected.encoding.items():
2428                if key in ["dtype", "chunks", "original_shape"]:
2429                    continue
2430                setattr(newmsg, key, value)
2431
2432            # write the message to file
2433            with grib2io.open(filename, mode=mode) as f:
2434                f.write(newmsg)
2435            mode = "a"

Write a DataArray to a grib2 file.

Parameters
  • filename: Name of the grib2 file to write to.
  • mode ({"x", "w", "a"}, optional, default="x"): Persistence mode

    +------+-----------------------------------+ | mode | Description | +======+===================================+ | x | create (fail if exists) | +------+-----------------------------------+ | w | create (overwrite if exists) | +------+-----------------------------------+ | a | append (create if does not exist) | +------+-----------------------------------+

def update_attrs(self, **kwargs):
2437    def update_attrs(self, **kwargs):
2438        """
2439        Update many of the attributes of the DataArray.
2440
2441        Parameters
2442        ----------
2443        **kwargs
2444            Attributes to update.  This can include many of the GRIB2IO message
2445            attributes that you can find when you print a GRIB2IO message. For
2446            conflicting updates, the last keyword will be used.
2447
2448            +-----------------------+------------------------------------------+
2449            | kwargs                | Description                              |
2450            +=======================+==========================================+
2451            | shortName="VTMP"      | Set shortName to "VTMP", along with      |
2452            |                       | appropriate discipline,                  |
2453            |                       | parameterCategory, parameterNumber,      |
2454            |                       | fullName and units.                      |
2455            +-----------------------+------------------------------------------+
2456            | discipline=0,         | Set shortName, discipline,               |
2457            | parameterCategory=0,  | parameterCategory, parameterNumber,      |
2458            | parameterNumber=1     | fullName and units appropriate for       |
2459            |                       | "Virtual Temperature".                   |
2460            +-----------------------+------------------------------------------+
2461            | discipline=0,         | Conflicting keywords but                 |
2462            | parameterCategory=0,  | 'shortName="TMP"' wins.  Set shortName,  |
2463            | parameterNumber=1,    | discipline, parameterCategory,           |
2464            | shortName="TMP"       | parameterNumber, fullName and units      |
2465            |                       | appropriate for "Temperature".           |
2466            +-----------------------+------------------------------------------+
2467
2468        Returns
2469        -------
2470        DataArray
2471            DataArray with updated attributes.
2472        """
2473        da = self._obj.copy(deep=True)
2474
2475        newmsg = Grib2Message(
2476            da.attrs["GRIB2IO_section0"],
2477            da.attrs["GRIB2IO_section1"],
2478            da.attrs["GRIB2IO_section2"],
2479            da.attrs["GRIB2IO_section3"],
2480            da.attrs["GRIB2IO_section4"],
2481            da.attrs["GRIB2IO_section5"],
2482        )
2483
2484        coords_keys = [k for k in da.coords.keys() if k in AVAILABLE_NON_GEO_COORDS]
2485
2486        for grib2_name, value in kwargs.items():
2487            if grib2_name == "gridDefinitionTemplateNumber":
2488                raise ValueError(
2489                    "The gridDefinitionTemplateNumber attribute cannot be updated.  The best way to change to a different grid is to interpolate the data to a new grid using the grib2io interpolate functions."
2490                )
2491            if grib2_name == "productDefinitionTemplateNumber":
2492                raise ValueError("The productDefinitionTemplateNumber attribute cannot be updated.")
2493            if grib2_name == "dataRepresentationTemplateNumber":
2494                raise ValueError("The dataRepresentationTemplateNumber attribute cannot be updated.")
2495            if grib2_name in coords_keys:
2496                warnings.warn(f"Skipping attribute '{grib2_name}' because it is a coordinate. Use da.assign_coords() to change coordinate values.")
2497                continue
2498            if hasattr(newmsg, grib2_name):
2499                setattr(newmsg, grib2_name, value)
2500            else:
2501                warnings.warn(f"Skipping attribute '{grib2_name}' because it is not a valid GRIB2 attribute for this message and cannot be updated.")
2502                continue
2503
2504        da.attrs["GRIB2IO_section0"] = newmsg.section0
2505        da.attrs["GRIB2IO_section1"] = newmsg.section1
2506        da.attrs["GRIB2IO_section2"] = newmsg.section2 or []
2507        da.attrs["GRIB2IO_section3"] = newmsg.section3
2508        da.attrs["GRIB2IO_section4"] = newmsg.section4
2509        da.attrs["GRIB2IO_section5"] = newmsg.section5
2510        da.attrs["fullName"] = newmsg.fullName
2511        da.attrs["shortName"] = newmsg.shortName
2512        da.attrs["units"] = newmsg.units
2513
2514        return da

Update many of the attributes of the DataArray.

Parameters
  • **kwargs: Attributes to update. This can include many of the GRIB2IO message attributes that you can find when you print a GRIB2IO message. For conflicting updates, the last keyword will be used.

+-----------------------+------------------------------------------+ | kwargs | Description | +=======================+==========================================+ | shortName="VTMP" | Set shortName to "VTMP", along with | | | appropriate discipline, | | | parameterCategory, parameterNumber, | | | fullName and units. | +-----------------------+------------------------------------------+ | discipline=0, | Set shortName, discipline, | | parameterCategory=0, | parameterCategory, parameterNumber, | | parameterNumber=1 | fullName and units appropriate for | | | "Virtual Temperature". | +-----------------------+------------------------------------------+ | discipline=0, | Conflicting keywords but | | parameterCategory=0, | 'shortName="TMP"' wins. Set shortName, | | parameterNumber=1, | discipline, parameterCategory, | | shortName="TMP" | parameterNumber, fullName and units | | | appropriate for "Temperature". | +-----------------------+------------------------------------------+

Returns
  • DataArray: DataArray with updated attributes.
def update_section3(self) -> xarray.core.dataarray.DataArray:
2516    def update_section3(self) -> xr.DataArray:
2517        """
2518        Update section3 attributes based on the latitude and longitude corners.
2519
2520        This makes the GRIB2IO_section3 attribute consistent with the grid's
2521        new corners after a change in the spatial extent.
2522        """
2523        da = self._obj
2524        if "GRIB2IO_section3" not in da.attrs:
2525            raise ValueError(
2526                "DataArray has no attr 'GRIB2IO_section3'.  This function only works with Datasets/DataArrrays opened with the 'grib2io' backend."
2527            )
2528        if "latitude" not in da.coords:
2529            raise ValueError("DataArray has no coord 'latitude'")
2530        if "longitude" not in da.coords:
2531            raise ValueError("DataArray has no coord 'longitude'")
2532
2533        grid = Grid(da.attrs["GRIB2IO_section3"])
2534
2535        if grid.gdtn not in [0, 1, 10, 20, 30, 31, 40, 110]:
2536            raise ValueError(
2537                textwrap.dedent("""\
2538                    update_section3 only works for:
2539
2540                    Latitude/Longitude, Equidistant Cylindrical, or Plate Carree (gdtn=0)
2541                    Rotated Latitude/Longitude (gdtn=1)
2542                    Mercator (gdtn=10)
2543                    Polar Stereographic (gdtn=20)
2544                    Lambert Conformal (gdtn=30)
2545                    Albers Equal-Area (gdtn=31)
2546                    Gaussian Latitude/Longitude (gdtn=40)
2547                    Equatorial Azimuthal Equidistant Projection (gdtn=110)
2548                    """)
2549            )
2550
2551        grid.latitudeFirstGridpoint = da.latitude.isel(y=0, x=0)
2552        grid.longitudeFirstGridpoint = da.longitude.isel(y=0, x=0)
2553        grid.nx = len(da.x)
2554        grid.ny = len(da.y)
2555
2556        # last gridpoint does not affect section3 for some gdt but set anyway
2557        grid.latitudeLastGridpoint = da.latitude.isel(y=-1, x=-1)
2558        grid.longitudeLastGridpoint = da.longitude.isel(y=-1, x=-1)
2559
2560        da.attrs["GRIB2IO_section3"] = grid.section3
2561
2562        return da

Update section3 attributes based on the latitude and longitude corners.

This makes the GRIB2IO_section3 attribute consistent with the grid's new corners after a change in the spatial extent.

def subset(self, *, lats=None, lons=None) -> xarray.core.dataarray.DataArray:
2564    def subset(self, *, lats=None, lons=None) -> xr.DataArray:
2565        """
2566        Subset the DataArray to a box defined by latitudes and/or longitudes.
2567
2568        Parameters
2569        ----------
2570        lats
2571            Two item list or tuple of latitudes.  Default is None which will
2572            return a subset unbounded by latitude.  The first term defines the
2573            southern boundary and the second term defines the northern
2574            boundary.
2575        lons
2576            Two item list or tuple of longitudes.  Default is None which will
2577            return a subset unbounded by longitude.  The first term defines the
2578            western boundary and the second term defines the eastern
2579            boundary.  Can follow either: 0 to 360 postive eastward, or 0 to
2580            -180 westward / 0 to 180 eastward conventions.  The longitude
2581            boundaries cannot cross 0.
2582
2583        Returns
2584        -------
2585        subset
2586            DataArray subset to the bounding box created by input 'lats'/'lons'.
2587            All gridpoints with lat/lon matching contraints are included within
2588            subset.
2589        """
2590
2591        def slice_from_contiguous_mask(mask):
2592            indices = np.where(mask)[0]
2593
2594            if indices.size > 0:
2595                # slice(start, stop) - stop is exclusive, so we add 1
2596                my_slice = slice(indices[0], indices[-1] + 1)
2597            else:
2598                my_slice = slice(0, 0)
2599            return my_slice
2600
2601        da = self._obj.copy()
2602
2603        if lats is None:
2604            lats = (np.min(da.latitude), np.max(da.latitude))
2605        else:
2606            lats = (min(lats), max(lats))
2607
2608        if lons is None:
2609            lons = (np.min(da.longitude), np.max(da.longitude))
2610        else:
2611            lons = (min(lons), max(lons))
2612
2613        # Internally work in common lon data representation (0->360 positive eastward from 0)
2614        lons = np.mod(np.array(lons) + 360, 360)
2615        lon_da = np.mod(da.longitude + 360, 360)
2616
2617        snap_first_point = snap_to_nearest_cell_center(da.latitude, lon_da, lats[0], lons[0])
2618        snap_last_point = snap_to_nearest_cell_center(da.latitude, lon_da, lats[1], lons[1])
2619        lats = (snap_first_point[0], snap_last_point[0])
2620        lons = (snap_first_point[1], snap_last_point[1])
2621
2622        x = ((lon_da >= lons[0]) & (lon_da <= lons[1])).any("y")
2623        if x.chunks:
2624            x = x.compute()
2625
2626        y = ((da.latitude >= lats[0]) & (da.latitude <= lats[1])).any("x")
2627        if y.chunks:
2628            y = y.compute()
2629
2630        y_slice = slice_from_contiguous_mask(y)
2631        x_slice = slice_from_contiguous_mask(x)
2632
2633        da = da.isel(y=y_slice, x=x_slice)
2634        if da.size < 1:
2635            raise ValueError("None of grid data is within given lat/lon bounds.")
2636
2637        da = da.grib2io.update_section3()
2638
2639        return da

Subset the DataArray to a box defined by latitudes and/or longitudes.

Parameters
  • lats: Two item list or tuple of latitudes. Default is None which will return a subset unbounded by latitude. The first term defines the southern boundary and the second term defines the northern boundary.
  • lons: Two item list or tuple of longitudes. Default is None which will return a subset unbounded by longitude. The first term defines the western boundary and the second term defines the eastern boundary. Can follow either: 0 to 360 postive eastward, or 0 to -180 westward / 0 to 180 eastward conventions. The longitude boundaries cannot cross 0.
Returns
  • subset: DataArray subset to the bounding box created by input 'lats'/'lons'. All gridpoints with lat/lon matching contraints are included within subset.
def compute(self, **kwargs):
2641    def compute(self, **kwargs):
2642        """
2643        Compute the Dask-backed DataArray with retries for transient errors.
2644
2645        Wraps :func:`grib2io.utils.compute_with_retries`.
2646
2647        Parameters
2648        ----------
2649        **kwargs
2650            Arguments passed to :func:`grib2io.utils.compute_with_retries`,
2651            e.g., `max_attempts` or `base_sleep`.
2652
2653        Returns
2654        -------
2655        xarray.DataArray
2656            The computed DataArray with NumPy-backed data.
2657        """
2658        from .utils import compute_with_retries
2659
2660        return compute_with_retries(self._obj, **kwargs)

Compute the Dask-backed DataArray with retries for transient errors.

Wraps grib2io.utils.compute_with_retries().

Parameters
Returns
  • xarray.DataArray: The computed DataArray with NumPy-backed data.
def open_mfdataset( filenames: Union[str, Sequence[str]], *, drop_variables: Optional[List[str]] = None, save_index: bool = True, filters: Mapping[str, Any] = {}, data_model: Optional[str] = None, parallel: bool = False, preprocess: Optional[Callable] = None, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None, **kwargs) -> xarray.core.dataset.Dataset:
2663def open_mfdataset(
2664    filenames: typing.Union[str, typing.Sequence[str]],
2665    *,
2666    drop_variables: typing.Optional[typing.List[str]] = None,
2667    save_index: bool = True,
2668    filters: typing.Mapping[str, typing.Any] = dict(),
2669    data_model: typing.Optional[str] = None,
2670    parallel: bool = False,
2671    preprocess: typing.Optional[typing.Callable] = None,
2672    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
2673    **kwargs,
2674) -> xr.Dataset:
2675    """
2676    Open multiple GRIB2 files as a single xarray Dataset.
2677
2678    This function is optimized for GRIB2 files by combining their indices
2679    and creating a single Dataset, which is often much faster than
2680    using ``xarray.open_mfdataset``. It supports parallel index reading
2681    and dataset opening when ``parallel=True`` and ``dask`` is installed.
2682
2683    Parameters
2684    ----------
2685    filenames : str or sequence of str
2686        GRIB2 files to be opened. Can be a glob pattern.
2687    drop_variables : list of str, optional
2688        List of variables to exclude from the dataset.
2689    save_index : bool, optional
2690        Whether to save the GRIB2 index to a file (default is True).
2691    filters : dict, optional
2692        Filter GRIB2 messages to a single hypercube. Dictionary keys can be
2693        any GRIB2 metadata attribute name.
2694    data_model : str, optional
2695        Parse GRIB metadata following a defined data model convention
2696        (e.g., "nws-viz").
2697    parallel : bool, optional
2698        If True, use ``dask`` to read indices and open datasets in parallel.
2699        Requires the ``dask`` package.
2700    preprocess : callable, optional
2701        A function to apply to each file's dataset before combining.
2702    chunks : int, dict or 'auto', optional
2703        If chunks is provided, it is used to load the dataset into a
2704        dask-backed dataset.
2705    **kwargs : optional
2706        Additional arguments passed to the combination logic.
2707        If ``combine='nested'``, passed to ``xarray.combine_nested``.
2708        If ``combine='by_coords'``, passed to ``xarray.combine_by_coords``.
2709        If ``combine='merge'``, passed to ``xarray.merge``.
2710        If no ``combine`` argument is provided, the function attempts
2711        ``xarray.combine_by_coords`` followed by ``xarray.merge``.
2712
2713    Returns
2714    -------
2715    xarray.Dataset
2716        Xarray dataset of grib2 messages.
2717
2718    Notes
2719    -----
2720    - This function uses a "fast path" when ``preprocess=None`` and no
2721      combination ``**kwargs`` are provided, which concatenates all indices
2722      into a single global index before building the Dataset.
2723    - All files must share the same horizontal grid (ny, nx).
2724    """
2725    if isinstance(filenames, str):
2726        import glob
2727
2728        filenames = sorted(glob.glob(filenames))
2729
2730    storage_options = kwargs.pop("storage_options", None)
2731
2732    def _get_index(fname_and_index: typing.Tuple[str, int]) -> pd.DataFrame:
2733        """
2734        Internal utility to read GRIB2 index from a file.
2735
2736        Parameters
2737        ----------
2738        fname_and_index : tuple of (str, int)
2739            Tuple containing the filename and its position in the file list.
2740
2741        Returns
2742        -------
2743        pandas.DataFrame
2744            The GRIB2 index for the specified file.
2745        """
2746        fname, i = fname_and_index
2747        with grib2io.open(
2748            fname,
2749            save_index=save_index,
2750            _xarray_backend=True,
2751            **(storage_options or {}),
2752        ) as f:
2753            idx = pd.DataFrame(f._index)
2754            idx = idx.assign(msg=list(f))
2755            idx["file_index"] = i
2756            return idx
2757
2758    if parallel:
2759        try:
2760            import dask
2761            from dask.bag import from_sequence
2762
2763            indices = from_sequence(zip(filenames, range(len(filenames)))).map(_get_index).compute()
2764        except ImportError:
2765            warnings.warn("dask not installed, falling back to sequential index reading.")
2766            parallel = False
2767            indices = [_get_index((fname, i)) for i, fname in enumerate(filenames)]
2768    else:
2769        indices = [_get_index((fname, i)) for i, fname in enumerate(filenames)]
2770
2771    if not indices:
2772        return xr.Dataset()
2773
2774    # Validate grid consistency across files using only the first message of each file
2775    grid_cols = ["ny", "nx"]
2776    first_msgs = pd.concat([idx.head(1) for idx in indices], ignore_index=True)
2777    unique_grids = first_msgs[grid_cols].drop_duplicates()
2778    if len(unique_grids) > 1:
2779        grid_list = unique_grids.to_dict("records")
2780        raise ValueError(f"Multiple grids detected in open_mfdataset. All files must have the same grid. Found grids: {grid_list}")
2781
2782    # Determine if we can use the fast path (single index concatenation)
2783    # The fast path is only available if no preprocess is provided and no combination kwargs are used
2784    # that would require individual datasets (like concat_dim for nested combination)
2785    use_fast_path = preprocess is None and not kwargs
2786
2787    if not use_fast_path:
2788        if parallel:
2789            import dask
2790
2791            @dask.delayed
2792            def _open_delayed(idx, fname):
2793                return _open_dataset_from_index(
2794                    idx,
2795                    fname,
2796                    filters,
2797                    data_model,
2798                    drop_variables=drop_variables,
2799                    chunks=chunks,
2800                )
2801
2802            datasets = dask.compute(*[_open_delayed(idx, fname) for idx, fname in zip(indices, filenames)])
2803        else:
2804            datasets = [
2805                _open_dataset_from_index(
2806                    idx,
2807                    fname,
2808                    filters,
2809                    data_model,
2810                    drop_variables=drop_variables,
2811                    chunks=chunks,
2812                )
2813                for idx, fname in zip(indices, filenames)
2814            ]
2815
2816        if preprocess is not None:
2817            datasets = [preprocess(ds) for ds in datasets]
2818
2819        combine_opt = kwargs.pop("combine", None)
2820        if combine_opt == "nested":
2821            ds = xr.combine_nested(datasets, **kwargs)
2822        elif combine_opt == "by_coords":
2823            ds = xr.combine_by_coords(datasets, **kwargs)
2824        elif combine_opt == "merge":
2825            ds = xr.merge(datasets, **kwargs)
2826        else:
2827            # Default behavior: try by_coords, then merge
2828            try:
2829                ds = xr.combine_by_coords(datasets, **kwargs)
2830            except Exception:
2831                ds = xr.merge(datasets, **kwargs)
2832    else:
2833        file_index = pd.concat(indices, ignore_index=True)
2834        ds = _open_dataset_from_index(
2835            file_index,
2836            list(filenames),
2837            filters,
2838            data_model,
2839            drop_variables=drop_variables,
2840            chunks=chunks,
2841        )
2842
2843    # Update history for provenance
2844    history = ds.attrs.get("history", "")
2845    now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
2846    ds.attrs["history"] = f"{now}: Initialized via grib2io.open_mfdataset from {len(filenames)} files\n{history}"
2847
2848    return ds

Open multiple GRIB2 files as a single xarray Dataset.

This function is optimized for GRIB2 files by combining their indices and creating a single Dataset, which is often much faster than using xarray.open_mfdataset. It supports parallel index reading and dataset opening when parallel=True and dask is installed.

Parameters
  • filenames (str or sequence of str): GRIB2 files to be opened. Can be a glob pattern.
  • drop_variables (list of str, optional): List of variables to exclude from the dataset.
  • save_index (bool, optional): Whether to save the GRIB2 index to a file (default is True).
  • filters (dict, optional): Filter GRIB2 messages to a single hypercube. Dictionary keys can be any GRIB2 metadata attribute name.
  • data_model (str, optional): Parse GRIB metadata following a defined data model convention (e.g., "nws-viz").
  • parallel (bool, optional): If True, use dask to read indices and open datasets in parallel. Requires the dask package.
  • preprocess (callable, optional): A function to apply to each file's dataset before combining.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
  • **kwargs (optional): Additional arguments passed to the combination logic. If combine='nested', passed to xarray.combine_nested. If combine='by_coords', passed to xarray.combine_by_coords. If combine='merge', passed to xarray.merge. If no combine argument is provided, the function attempts xarray.combine_by_coords followed by xarray.merge.
Returns
  • xarray.Dataset: Xarray dataset of grib2 messages.
Notes
  • This function uses a "fast path" when preprocess=None and no combination **kwargs are provided, which concatenates all indices into a single global index before building the Dataset.
  • All files must share the same horizontal grid (ny, nx).
def build_datatree_from_grib( filename: str, file_index: pandas.DataFrame, filters: Optional[Mapping[str, Any]] = None, stack_vertical: bool = False, drop_variables: Optional[List[str]] = None, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None) -> Any:
2951def build_datatree_from_grib(
2952    filename: str,
2953    file_index: pd.DataFrame,
2954    filters: typing.Optional[typing.Mapping[str, typing.Any]] = None,
2955    stack_vertical: bool = False,
2956    drop_variables: typing.Optional[typing.List[str]] = None,
2957    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
2958) -> typing.Any:
2959    """
2960    Build a DataTree from GRIB2 messages.
2961
2962    This internal function organizes GRIB2 messages into a hierarchical
2963    tree structure based on level types, PDTNs, and other metadata.
2964
2965    Parameters
2966    ----------
2967    filename : str
2968        Path to the source GRIB2 file.
2969    file_index : pandas.DataFrame
2970        Index of GRIB2 messages.
2971    filters : dict, optional
2972        Filter criteria for selecting messages.
2973    stack_vertical : bool, optional
2974        If True, vertical levels will be stacked in a single dataset
2975        within each node, rather than creating separate nodes per level value.
2976    drop_variables : list of str, optional
2977        List of variable shortnames to exclude.
2978    chunks : int, dict or 'auto', optional
2979        If chunks is provided, it is used to load the dataset into a
2980        dask-backed dataset.
2981
2982    Returns
2983    -------
2984    xarray.DataTree
2985        A hierarchical tree representation of the GRIB2 data.
2986    """
2987    if filters is None:
2988        filters = {}
2989
2990    # Apply any filters from user
2991    for k, v in filters.items():
2992        if k not in file_index.columns:
2993            file_index = file_index.copy()
2994            file_index[k] = file_index.msg.apply(lambda msg: getattr(msg, k, None))
2995        file_index = filter_index(file_index, k, v)
2996
2997    # Make a copy to avoid the SettingWithCopyWarning
2998    file_index = file_index.copy()
2999
3000    # Extract metadata needed for tree organization
3001    # Use a safer approach to handle missing attributes
3002    def safe_getattr(obj, name):
3003        try:
3004            attr = getattr(obj, name)
3005            # Need to test if the attribute is Grib2Metadata. If so,
3006            # then get the value attribute.
3007            if isinstance(attr, grib2io.templates.Grib2Metadata):
3008                attr = attr.value
3009            return attr
3010        except (AttributeError, KeyError):
3011            return None
3012
3013    for attr in _TREE_HIERARCHY_LEVELS:
3014        if (attr not in file_index.columns) and (attr != "valueOfFirstFixedSurface"):
3015            file_index[attr] = file_index.msg.apply(lambda msg: safe_getattr(msg, attr))
3016
3017    # Also extract shortName for variable naming
3018    if "shortName" not in file_index.columns:
3019        file_index = file_index.assign(shortName=file_index.msg.apply(lambda msg: getattr(msg, "shortName", None)))
3020
3021    if drop_variables:
3022        file_index = file_index[~file_index["shortName"].isin(drop_variables)]
3023
3024    file_index = file_index.assign(nx=file_index.msg.apply(lambda msg: getattr(msg, "nx", None)))
3025    file_index = file_index.assign(ny=file_index.msg.apply(lambda msg: getattr(msg, "ny", None)))
3026
3027    # Create root DataTree
3028    root = xr.DataTree()
3029
3030    # Adjust hierarchy levels if we're stacking vertical levels
3031    hierarchy_levels = list(_TREE_HIERARCHY_LEVELS)  # This makes a copy
3032    if stack_vertical and "valueOfFirstFixedSurface" in hierarchy_levels:
3033        hierarchy_levels.remove("valueOfFirstFixedSurface")
3034
3035    # First group by level type
3036    level_groups = {}
3037
3038    # Create a dictionary to group data by level type
3039    for level_type in file_index["typeOfFirstFixedSurface"].unique():
3040        if pd.notna(level_type):  # Skip None/NaN values
3041            level_info = _LEVEL_NAME_MAPPING.get(level_type, f"level_{level_type}")
3042            level_name = level_info[0]
3043            # Get all rows for this level type
3044            level_data = file_index[file_index["typeOfFirstFixedSurface"] == level_type]
3045            level_groups[level_type] = {"name": level_name, "data": level_data}
3046
3047    # Process each level group
3048    for level_type, group_info in level_groups.items():
3049        level_name = group_info["name"]
3050        level_df = group_info["data"]
3051
3052        # Create a branch for this level type
3053        level_tree = xr.DataTree()
3054
3055        # Process this branch based on PDTN, perturbation number, etc.
3056        process_level_branch(level_tree, level_df, filename, chunks=chunks)
3057
3058        # Add this branch to the main tree
3059        root[level_name] = level_tree
3060
3061    return root

Build a DataTree from GRIB2 messages.

This internal function organizes GRIB2 messages into a hierarchical tree structure based on level types, PDTNs, and other metadata.

Parameters
  • filename (str): Path to the source GRIB2 file.
  • file_index (pandas.DataFrame): Index of GRIB2 messages.
  • filters (dict, optional): Filter criteria for selecting messages.
  • stack_vertical (bool, optional): If True, vertical levels will be stacked in a single dataset within each node, rather than creating separate nodes per level value.
  • drop_variables (list of str, optional): List of variable shortnames to exclude.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
Returns
  • xarray.DataTree: A hierarchical tree representation of the GRIB2 data.
def process_level_branch( level_tree: Any, df: pandas.DataFrame, filename: str, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None):
3064def process_level_branch(
3065    level_tree: typing.Any,
3066    df: pd.DataFrame,
3067    filename: str,
3068    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3069):
3070    """
3071    Process a level type branch of the data tree.
3072
3073    Organizes the tree by PDTN and other attributes.
3074
3075    Parameters
3076    ----------
3077    level_tree : xarray.DataTree
3078        The DataTree node for this level type.
3079    df : pandas.DataFrame
3080        DataFrame of messages for this level type.
3081    filename : str
3082        Path to the GRIB2 file.
3083    chunks : int, dict or 'auto', optional
3084        If chunks is provided, it is used to load the dataset into a
3085        dask-backed dataset.
3086    """
3087    # Group by PDTN
3088    pdtn_groups = {}
3089
3090    # Group data by PDTN first
3091    for pdtn_value in df["productDefinitionTemplateNumber"].unique():
3092        if pd.notna(pdtn_value):
3093            pdtn_df = df[df["productDefinitionTemplateNumber"] == pdtn_value]
3094            pdtn_groups[pdtn_value] = pdtn_df
3095
3096    # If there's only one PDTN value, skip creating PDTN branch level
3097    if len(pdtn_groups) == 1:
3098        pdtn, pdtn_df = next(iter(pdtn_groups.items()))
3099
3100        pdtn_name = f"pdtn_{int(pdtn)}"
3101
3102        # Check if we need to further subdivide by perturbation number
3103        has_perturbations = "perturbationNumber" in pdtn_df.columns and len(pdtn_df["perturbationNumber"].dropna().unique()) > 1
3104
3105        # Check if we need to further subdivide by probabilities unique for each variable.
3106        has_probabilities = "typeOfProbability" in pdtn_df.columns and len(pdtn_df["typeOfProbability"].dropna().unique()) > 1
3107
3108        if has_perturbations:
3109            # Process perturbations directly on the level tree
3110            process_perturbation_groups(level_tree, pdtn_df, filename, chunks=chunks)
3111        elif has_probabilities:
3112            # Process probability groups
3113            process_probability_groups(level_tree, pdtn_df, filename, chunks=chunks)
3114        else:
3115            # Try to create dataset directly on level
3116            try:
3117                dss = create_datasets_from_df(pdtn_df, filename, chunks=chunks)
3118                if dss is not None:
3119                    dt = xr.DataTree()
3120                    if len(dss) == 1:
3121                        dt.ds = dss[0]
3122                    else:
3123                        for ds in dss:
3124                            varname = list(ds.data_vars)[0]
3125                            dt[f"var_{varname}"] = ds
3126                    level_tree[pdtn_name] = dt
3127                else:
3128                    # Try to separate by variable name as a fallback
3129                    try_process_by_variables(level_tree, pdtn_df, filename, chunks=chunks)
3130            except Exception as e:
3131                print(f"Error creating dataset for level with pdtn {int(pdtn)}: {e}")
3132
3133                # Try to separate by variable name as a fallback
3134                try_process_by_variables(level_tree, pdtn_df, filename, chunks=chunks)
3135    else:
3136        # Multiple PDTN values, process each group with PDTN branch nodes
3137        for pdtn, pdtn_df in pdtn_groups.items():
3138            # Use a simple node name that's easy to use in code
3139            pdtn_name = f"pdtn_{int(pdtn)}"
3140
3141            # Check if we need to further subdivide by perturbation number
3142            has_perturbations = "perturbationNumber" in pdtn_df.columns and len(pdtn_df["perturbationNumber"].dropna().unique()) > 1
3143
3144            # Check if we need to further subdivide by probabilities unique for each variable.
3145            has_probabilities = "typeOfProbability" in pdtn_df.columns and len(pdtn_df["typeOfProbability"].dropna().unique()) > 1
3146
3147            if has_perturbations:
3148                # Create a branch for this PDTN
3149                pdtn_tree = xr.DataTree()
3150
3151                # Process perturbation groups
3152                process_perturbation_groups(pdtn_tree, pdtn_df, filename, chunks=chunks)
3153
3154                # Only add the PDTN branch if it has children
3155                if len(pdtn_tree.children) > 0 or pdtn_tree.ds is not None:
3156                    level_tree[pdtn_name] = pdtn_tree
3157            elif has_probabilities:
3158                # Create a branch for this PDTN
3159                pdtn_tree = xr.DataTree()
3160
3161                # Process probability groups
3162                process_probability_groups(pdtn_tree, pdtn_df, filename, chunks=chunks)
3163
3164                # Only add the PDTN branch if it has children
3165                if len(pdtn_tree.children) > 0 or pdtn_tree.ds is not None:
3166                    level_tree[pdtn_name] = pdtn_tree
3167            else:
3168                # Create a subtree for this PDTN
3169                pdtn_tree = xr.DataTree()
3170
3171                # Try to create dataset directly on level
3172                try:
3173                    dss = create_datasets_from_df(pdtn_df, filename, chunks=chunks)
3174                    if dss is not None:
3175                        if len(dss) == 1:
3176                            pdtn_tree.ds = dss[0]
3177                        else:
3178                            for ds in dss:
3179                                varname = list(ds.data_vars)[0]
3180                                pdtn_tree[f"var_{varname}"] = ds
3181                        level_tree[pdtn_name] = pdtn_tree
3182                    else:
3183                        # Try to separate by variable name as a fallback
3184                        try_process_by_variables(pdtn_tree, pdtn_df, filename, chunks=chunks)
3185                        level_tree[pdtn_name] = pdtn_tree
3186                except Exception as e:
3187                    print(f"Error creating dataset for level with pdtn {int(pdtn)}: {e}")
3188
3189                    # Try to separate by variable name as a fallback
3190                    try_process_by_variables(pdtn_tree, pdtn_df, filename, chunks=chunks)
3191                    level_tree[pdtn_name] = pdtn_tree

Process a level type branch of the data tree.

Organizes the tree by PDTN and other attributes.

Parameters
  • level_tree (xarray.DataTree): The DataTree node for this level type.
  • df (pandas.DataFrame): DataFrame of messages for this level type.
  • filename (str): Path to the GRIB2 file.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
def process_probability_groups( target_tree: Any, pdtn_df: pandas.DataFrame, filename: str, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None) -> bool:
3194def process_probability_groups(
3195    target_tree: typing.Any,
3196    pdtn_df: pd.DataFrame,
3197    filename: str,
3198    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3199) -> bool:
3200    """
3201    Process probability groups and add them to the target tree.
3202
3203    Parameters
3204    ----------
3205    target_tree : xarray.DataTree
3206        The tree node to add probability groups to.
3207    pdtn_df : pandas.DataFrame
3208        DataFrame of messages for a specific PDTN.
3209    filename : str
3210        Path to the GRIB2 file.
3211    chunks : int, dict or 'auto', optional
3212        If chunks is provided, it is used to load the dataset into a
3213        dask-backed dataset.
3214
3215    Returns
3216    -------
3217    bool
3218        True if successful.
3219    """
3220    success = False
3221    # Group by type of probability
3222    prob_groups = {}
3223    for prob_value in pdtn_df["typeOfProbability"].unique():
3224        if pd.notna(prob_value):
3225            prob_df = pdtn_df[pdtn_df["typeOfProbability"] == prob_value]
3226            prob_groups[prob_value] = prob_df
3227
3228    # Process each probability group
3229    for prob_num, prob_df in prob_groups.items():
3230        prob_name = f"prob_{int(prob_num)}"
3231
3232        # Try to create dataset for this probability group
3233        try:
3234            dss = create_datasets_from_df(prob_df, filename, chunks=chunks)
3235            dt = xr.DataTree()
3236            if len(dss) == 1:
3237                dt.ds = dss[0]
3238                target_tree[prob_name] = dt
3239            elif len(dss) > 1:
3240                for ds in dss:
3241                    dt[f"var_{ds.data_vars[0]}"] = ds
3242            target_tree[prob_name] = dt
3243        except Exception as e:
3244            # Log error but continue processing other groups
3245            print(f"Error creating dataset for type of probability {prob_name}: {e}")
3246
3247    return success

Process probability groups and add them to the target tree.

Parameters
  • target_tree (xarray.DataTree): The tree node to add probability groups to.
  • pdtn_df (pandas.DataFrame): DataFrame of messages for a specific PDTN.
  • filename (str): Path to the GRIB2 file.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
Returns
  • bool: True if successful.
def process_perturbation_groups( target_tree: Any, pdtn_df: pandas.DataFrame, filename: str, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None) -> bool:
3250def process_perturbation_groups(
3251    target_tree: typing.Any,
3252    pdtn_df: pd.DataFrame,
3253    filename: str,
3254    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3255) -> bool:
3256    """
3257    Process perturbation groups and add them to the target tree.
3258
3259    Parameters
3260    ----------
3261    target_tree : xarray.DataTree
3262        The tree node to add perturbation groups to.
3263    pdtn_df : pandas.DataFrame
3264        DataFrame of messages for a specific PDTN.
3265    filename : str
3266        Path to the GRIB2 file.
3267    chunks : int, dict or 'auto', optional
3268        If chunks is provided, it is used to load the dataset into a
3269        dask-backed dataset.
3270
3271    Returns
3272    -------
3273    bool
3274        True if at least one perturbation was successfully processed.
3275    """
3276    success = False
3277    # Group by perturbation number
3278    pert_groups = {}
3279    for pert_value in pdtn_df["perturbationNumber"].unique():
3280        if pd.notna(pert_value):
3281            pert_df = pdtn_df[pdtn_df["perturbationNumber"] == pert_value]
3282            pert_groups[pert_value] = pert_df
3283
3284    # Process each perturbation group
3285    for pert_num, pert_df in pert_groups.items():
3286        pert_name = f"pert_{int(pert_num)}"
3287
3288        ## Try to create dataset for this perturbation group
3289        # try:
3290        #    dss = create_datasets_from_df(pert_df, filename)
3291        #    if dss is not None:
3292        #        if len(dss) == 1:
3293        #            target_tree.ds = dss[0]
3294        #        else:
3295        #            dss_dict = {f"ds_{i}": ds for i, ds in enumerate(dss)}
3296        #            atree = xr.DataTree(dss_dict)
3297        #            target_tree[prob_name] = atree
3298        #        success = True
3299        # except Exception as e:
3300        #    # Log error but continue processing other groups
3301        #    print(f"Error creating dataset for perturbation {pert_name}: {e}")
3302
3303        # Try to create dataset for this perturbation group
3304        try:
3305            dss = create_datasets_from_df(pert_df, filename, chunks=chunks)
3306            dt = xr.DataTree()
3307            if len(dss) == 1:
3308                dt.ds = dss[0]
3309                target_tree[pert_name] = dt
3310            elif len(dss) > 1:
3311                for ds in dss:
3312                    dt[f"pert{ds.data_vars[0]}"] = ds
3313            target_tree[pert_name] = dt
3314        except Exception as e:
3315            # Log error but continue processing other groups
3316            print(f"Error creating dataset for perturbation {pert_name}: {e}")
3317
3318    return success

Process perturbation groups and add them to the target tree.

Parameters
  • target_tree (xarray.DataTree): The tree node to add perturbation groups to.
  • pdtn_df (pandas.DataFrame): DataFrame of messages for a specific PDTN.
  • filename (str): Path to the GRIB2 file.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
Returns
  • bool: True if at least one perturbation was successfully processed.
def try_process_by_variables( target_tree: Any, df: pandas.DataFrame, filename: str, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None) -> bool:
3321def try_process_by_variables(
3322    target_tree: typing.Any,
3323    df: pd.DataFrame,
3324    filename: str,
3325    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3326) -> bool:
3327    """
3328    Try to separate data by variable names and create datasets.
3329
3330    Parameters
3331    ----------
3332    target_tree : xarray.DataTree
3333        The tree node to add variable datasets to.
3334    df : pandas.DataFrame
3335        DataFrame of messages.
3336    filename : str
3337        Path to the GRIB2 file.
3338    chunks : int, dict or 'auto', optional
3339        If chunks is provided, it is used to load the dataset into a
3340        dask-backed dataset.
3341
3342    Returns
3343    -------
3344    bool
3345        True if at least one variable was successfully processed.
3346    """
3347    success = False
3348
3349    try:
3350        for var_name in df["shortName"].unique():
3351            if pd.notna(var_name):
3352                var_df = df[df["shortName"] == var_name]
3353                try:
3354                    var_ds = create_datasets_from_df(var_df, filename, chunks=chunks)
3355                    if var_ds is not None:
3356                        target_tree[f"var_{var_name}"] = var_ds[0]
3357                        success = True
3358                except Exception as var_e:
3359                    print(f"Error creating dataset for variable {var_name}: {var_e}")
3360    except Exception as nested_e:
3361        print(f"Failed to process variables: {nested_e}")
3362
3363    return success

Try to separate data by variable names and create datasets.

Parameters
  • target_tree (xarray.DataTree): The tree node to add variable datasets to.
  • df (pandas.DataFrame): DataFrame of messages.
  • filename (str): Path to the GRIB2 file.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
Returns
  • bool: True if at least one variable was successfully processed.
def create_datasets_from_df( df: pandas.DataFrame, filename: str, verbose: bool = False, chunks: Union[int, Dict[Any, Any], Literal['auto'], NoneType] = None) -> Optional[List[xarray.core.dataset.Dataset]]:
3366def create_datasets_from_df(
3367    df: pd.DataFrame,
3368    filename: str,
3369    verbose: bool = False,
3370    chunks: typing.Optional[typing.Union[int, typing.Dict[typing.Any, typing.Any], typing.Literal["auto"]]] = None,
3371) -> typing.Optional[typing.List[xr.Dataset]]:
3372    """
3373    Create a list of xarray Datasets from a DataFrame of messages.
3374
3375    Parameters
3376    ----------
3377    df : pandas.DataFrame
3378        DataFrame of GRIB messages.
3379    filename : str
3380        Path to the GRIB2 file.
3381    verbose : bool, optional
3382        If True, prints detailed debugging information.
3383    chunks : int, dict or 'auto', optional
3384        If chunks is provided, it is used to load the dataset into a
3385        dask-backed dataset.
3386
3387    Returns
3388    -------
3389    list of xarray.Dataset, optional
3390        List of Datasets, or None if creation failed.
3391    """
3392    try:
3393        # Use parse_grib_index to get dimensions and attributes
3394        file_index, dim_coords, attrs, coord_attrs = parse_grib_index(df, {})
3395
3396        # Divide up records by variable
3397        frames, cubes, extra_geo = make_variables(file_index, filename, dim_coords, allow_uneven_dims=True)
3398
3399        if frames is None:
3400            return None
3401
3402        ds_list = []
3403        for var_df, var_cube in zip(frames, cubes):
3404            da = build_da_without_coords(var_df, var_cube, filename, attrs)
3405
3406            # Assign variable-specific coords from its cube
3407            coords = coords_from_cube(var_cube)
3408            da = da.assign_coords(coords)
3409
3410            # Assign extra index associated coords for this variable
3411            for dim_name, coord_names in dim_coords.items():
3412                retain_index_coord = False
3413                for name in coord_names:
3414                    if name == dim_name:
3415                        retain_index_coord = True
3416                    else:
3417                        if dim_name not in da.dims:
3418                            # for assigning scalar coords
3419                            coord_data = var_df[name].unique().item()
3420                            da = da.assign_coords({name: coord_data})
3421                        else:
3422                            # Handle non-scalar coords
3423                            coord_data = [
3424                                var_df[var_df.index.get_level_values(f"{dim_name}_ix") == val][name].unique().item()
3425                                for val in range(da[dim_name].size)
3426                            ]
3427                            coord = pd.Index(coord_data, name=dim_name)
3428                            da = da.assign_coords({name: (dim_name, coord)})
3429                if not retain_index_coord and dim_name in da.coords:
3430                    da = da.drop_vars(dim_name)
3431
3432            # Create a dataset for this variable
3433            var_ds = xr.Dataset({da.name: da})
3434
3435            # Assign metadata and common coords
3436            var_ds = assign_xr_meta(var_ds, [var_df], var_cube, dim_coords, extra_geo, coord_attrs)
3437
3438            if chunks is not None:
3439                var_ds = var_ds.chunk(chunks)
3440
3441            ds_list.append(var_ds)
3442
3443        return ds_list
3444    except Exception as e:
3445        if verbose:
3446            print(f"Error in create_datasets_from_df: {e}")
3447        return None

Create a list of xarray Datasets from a DataFrame of messages.

Parameters
  • df (pandas.DataFrame): DataFrame of GRIB messages.
  • filename (str): Path to the GRIB2 file.
  • verbose (bool, optional): If True, prints detailed debugging information.
  • chunks (int, dict or 'auto', optional): If chunks is provided, it is used to load the dataset into a dask-backed dataset.
Returns
  • list of xarray.Dataset, optional: List of Datasets, or None if creation failed.
@xr.register_datatree_accessor('grib2io')
class Grib2ioDataTree:
3452    @xr.register_datatree_accessor("grib2io")
3453    class Grib2ioDataTree:
3454        """
3455        DataTree accessor for GRIB2 files.
3456
3457        This accessor provides methods for working with GRIB2 data organized
3458        in a hierarchical tree structure.
3459        """
3460
3461        def __init__(self, datatree_obj):
3462            self._obj = datatree_obj
3463
3464        def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
3465            """
3466            Write all datasets in the DataTree to a GRIB2 file.
3467
3468            Parameters
3469            ----------
3470            filename : str
3471                Name of the GRIB2 file to write to.
3472            mode : {"x", "w", "a"}, optional
3473                Persistence mode, default is "x" (create, fail if exists)
3474            """
3475            # Start with the specified mode
3476            current_mode = mode
3477
3478            # Function to recursively process the tree
3479            def process_tree(node):
3480                nonlocal current_mode
3481
3482                # If this is a Dataset node with data variables
3483                if node.ds is not None and node.ds.data_vars:
3484                    # Write dataset to GRIB2 file
3485                    node.ds.grib2io.to_grib2(filename, mode=current_mode)
3486                    # Switch to append mode after first write
3487                    current_mode = "a"
3488
3489                # Process children
3490                for child_name, child_node in node.children.items():
3491                    process_tree(child_node)
3492
3493            # Start processing from the root
3494            process_tree(self._obj)
3495
3496        def griddef(self):
3497            """
3498            Get the grid definition from the first dataset in the tree that has one.
3499
3500            Returns
3501            -------
3502            grib2io.Grib2GridDef
3503                Grid definition object
3504            """
3505
3506            # Function to find first dataset with GRIB2IO_section3
3507            def find_griddef(node):
3508                if node.ds is not None and node.ds.data_vars:
3509                    for var_name in node.ds.data_vars:
3510                        if "GRIB2IO_section3" in node.ds[var_name].attrs:
3511                            return Grib2GridDef.from_section3(node.ds[var_name].attrs["GRIB2IO_section3"])
3512
3513                # Check children
3514                for child_name, child_node in node.children.items():
3515                    griddef = find_griddef(child_node)
3516                    if griddef is not None:
3517                        return griddef
3518
3519                return None
3520
3521            return find_griddef(self._obj)
3522
3523        def interp(
3524            self,
3525            method: typing.Union[str, int],
3526            grid_def_out: grib2io.Grib2GridDef,
3527            method_options: typing.Optional[typing.List[int]] = None,
3528            num_threads: int = 1,
3529        ) -> typing.Any:
3530            """
3531            Interpolate all datasets in the tree to a new grid.
3532
3533            Parameters
3534            ----------
3535            method : str or int
3536                Interpolation method to use.
3537            grid_def_out : grib2io.Grib2GridDef
3538                Target grid definition.
3539            method_options : list of int, optional
3540                Options for interpolation method.
3541            num_threads : int, optional
3542                Number of threads to use for interpolation.
3543
3544            Returns
3545            -------
3546            xarray.DataTree
3547                New DataTree with interpolated data.
3548            """
3549            new_tree = xr.DataTree()
3550
3551            # Function to recursively process the tree
3552            def process_tree(node, new_parent):
3553                # If this is a Dataset node with data variables
3554                if node.ds is not None and node.ds.data_vars:
3555                    # Interpolate dataset
3556                    interp_ds = node.ds.grib2io.interp(
3557                        method,
3558                        grid_def_out,
3559                        method_options=method_options,
3560                        num_threads=num_threads,
3561                    )
3562
3563                    # Add to new tree at the same path
3564                    if node == self._obj:  # Root node
3565                        new_parent.ds = interp_ds
3566                    else:
3567                        new_parent.ds = interp_ds
3568
3569                # Process children
3570                for child_name, child_node in node.children.items():
3571                    # Create same child in new tree
3572                    new_child = xr.DataTree()
3573                    new_parent[child_name] = new_child
3574                    process_tree(child_node, new_child)
3575
3576            # Start processing from the root
3577            process_tree(self._obj, new_tree)
3578
3579            return new_tree
3580
3581        def subset(self, lats: typing.Sequence[float], lons: typing.Sequence[float]) -> typing.Any:
3582            """
3583            Subset all datasets in the tree to a region.
3584
3585            Parameters
3586            ----------
3587            lats : sequence of float
3588                Latitude bounds [min_lat, max_lat].
3589            lons : sequence of float
3590                Longitude bounds [min_lon, max_lon].
3591
3592            Returns
3593            -------
3594            xarray.DataTree
3595                New DataTree with subset data.
3596            """
3597            new_tree = xr.DataTree()
3598
3599            # Function to recursively process the tree
3600            def process_tree(node, new_parent):
3601                # If this is a Dataset node with data variables
3602                if node.ds is not None and node.ds.data_vars:
3603                    # Subset dataset
3604                    subset_ds = node.ds.grib2io.subset(lats, lons)
3605
3606                    # Add to new tree at the same path
3607                    if node == self._obj:  # Root node
3608                        new_parent.ds = subset_ds
3609                    else:
3610                        new_parent.ds = subset_ds
3611
3612                # Process children
3613                for child_name, child_node in node.children.items():
3614                    # Create same child in new tree
3615                    new_child = xr.DataTree()
3616                    new_parent[child_name] = new_child
3617                    process_tree(child_node, new_child)
3618
3619            # Start processing from the root
3620            process_tree(self._obj, new_tree)
3621
3622            return new_tree

DataTree accessor for GRIB2 files.

This accessor provides methods for working with GRIB2 data organized in a hierarchical tree structure.

Grib2ioDataTree(datatree_obj)
3461        def __init__(self, datatree_obj):
3462            self._obj = datatree_obj
def to_grib2(self, filename, mode: Literal['x', 'w', 'a'] = 'x'):
3464        def to_grib2(self, filename, mode: typing.Literal["x", "w", "a"] = "x"):
3465            """
3466            Write all datasets in the DataTree to a GRIB2 file.
3467
3468            Parameters
3469            ----------
3470            filename : str
3471                Name of the GRIB2 file to write to.
3472            mode : {"x", "w", "a"}, optional
3473                Persistence mode, default is "x" (create, fail if exists)
3474            """
3475            # Start with the specified mode
3476            current_mode = mode
3477
3478            # Function to recursively process the tree
3479            def process_tree(node):
3480                nonlocal current_mode
3481
3482                # If this is a Dataset node with data variables
3483                if node.ds is not None and node.ds.data_vars:
3484                    # Write dataset to GRIB2 file
3485                    node.ds.grib2io.to_grib2(filename, mode=current_mode)
3486                    # Switch to append mode after first write
3487                    current_mode = "a"
3488
3489                # Process children
3490                for child_name, child_node in node.children.items():
3491                    process_tree(child_node)
3492
3493            # Start processing from the root
3494            process_tree(self._obj)

Write all datasets in the DataTree to a GRIB2 file.

Parameters
  • filename (str): Name of the GRIB2 file to write to.
  • mode ({"x", "w", "a"}, optional): Persistence mode, default is "x" (create, fail if exists)
def griddef(self):
3496        def griddef(self):
3497            """
3498            Get the grid definition from the first dataset in the tree that has one.
3499
3500            Returns
3501            -------
3502            grib2io.Grib2GridDef
3503                Grid definition object
3504            """
3505
3506            # Function to find first dataset with GRIB2IO_section3
3507            def find_griddef(node):
3508                if node.ds is not None and node.ds.data_vars:
3509                    for var_name in node.ds.data_vars:
3510                        if "GRIB2IO_section3" in node.ds[var_name].attrs:
3511                            return Grib2GridDef.from_section3(node.ds[var_name].attrs["GRIB2IO_section3"])
3512
3513                # Check children
3514                for child_name, child_node in node.children.items():
3515                    griddef = find_griddef(child_node)
3516                    if griddef is not None:
3517                        return griddef
3518
3519                return None
3520
3521            return find_griddef(self._obj)

Get the grid definition from the first dataset in the tree that has one.

Returns
def interp( self, method: Union[str, int], grid_def_out: grib2io.Grib2GridDef, method_options: Optional[List[int]] = None, num_threads: int = 1) -> Any:
3523        def interp(
3524            self,
3525            method: typing.Union[str, int],
3526            grid_def_out: grib2io.Grib2GridDef,
3527            method_options: typing.Optional[typing.List[int]] = None,
3528            num_threads: int = 1,
3529        ) -> typing.Any:
3530            """
3531            Interpolate all datasets in the tree to a new grid.
3532
3533            Parameters
3534            ----------
3535            method : str or int
3536                Interpolation method to use.
3537            grid_def_out : grib2io.Grib2GridDef
3538                Target grid definition.
3539            method_options : list of int, optional
3540                Options for interpolation method.
3541            num_threads : int, optional
3542                Number of threads to use for interpolation.
3543
3544            Returns
3545            -------
3546            xarray.DataTree
3547                New DataTree with interpolated data.
3548            """
3549            new_tree = xr.DataTree()
3550
3551            # Function to recursively process the tree
3552            def process_tree(node, new_parent):
3553                # If this is a Dataset node with data variables
3554                if node.ds is not None and node.ds.data_vars:
3555                    # Interpolate dataset
3556                    interp_ds = node.ds.grib2io.interp(
3557                        method,
3558                        grid_def_out,
3559                        method_options=method_options,
3560                        num_threads=num_threads,
3561                    )
3562
3563                    # Add to new tree at the same path
3564                    if node == self._obj:  # Root node
3565                        new_parent.ds = interp_ds
3566                    else:
3567                        new_parent.ds = interp_ds
3568
3569                # Process children
3570                for child_name, child_node in node.children.items():
3571                    # Create same child in new tree
3572                    new_child = xr.DataTree()
3573                    new_parent[child_name] = new_child
3574                    process_tree(child_node, new_child)
3575
3576            # Start processing from the root
3577            process_tree(self._obj, new_tree)
3578
3579            return new_tree

Interpolate all datasets in the tree to a new grid.

Parameters
  • method (str or int): Interpolation method to use.
  • grid_def_out (grib2io.Grib2GridDef): Target grid definition.
  • method_options (list of int, optional): Options for interpolation method.
  • num_threads (int, optional): Number of threads to use for interpolation.
Returns
  • xarray.DataTree: New DataTree with interpolated data.
def subset(self, lats: Sequence[float], lons: Sequence[float]) -> Any:
3581        def subset(self, lats: typing.Sequence[float], lons: typing.Sequence[float]) -> typing.Any:
3582            """
3583            Subset all datasets in the tree to a region.
3584
3585            Parameters
3586            ----------
3587            lats : sequence of float
3588                Latitude bounds [min_lat, max_lat].
3589            lons : sequence of float
3590                Longitude bounds [min_lon, max_lon].
3591
3592            Returns
3593            -------
3594            xarray.DataTree
3595                New DataTree with subset data.
3596            """
3597            new_tree = xr.DataTree()
3598
3599            # Function to recursively process the tree
3600            def process_tree(node, new_parent):
3601                # If this is a Dataset node with data variables
3602                if node.ds is not None and node.ds.data_vars:
3603                    # Subset dataset
3604                    subset_ds = node.ds.grib2io.subset(lats, lons)
3605
3606                    # Add to new tree at the same path
3607                    if node == self._obj:  # Root node
3608                        new_parent.ds = subset_ds
3609                    else:
3610                        new_parent.ds = subset_ds
3611
3612                # Process children
3613                for child_name, child_node in node.children.items():
3614                    # Create same child in new tree
3615                    new_child = xr.DataTree()
3616                    new_parent[child_name] = new_child
3617                    process_tree(child_node, new_child)
3618
3619            # Start processing from the root
3620            process_tree(self._obj, new_tree)
3621
3622            return new_tree

Subset all datasets in the tree to a region.

Parameters
  • lats (sequence of float): Latitude bounds [min_lat, max_lat].
  • lons (sequence of float): Longitude bounds [min_lon, max_lon].
Returns
  • xarray.DataTree: New DataTree with subset data.