arlmet.converters.from_gfs#

arlmet.converters.from_gfs(datasets, *, bbox=None)[source]#

Convert GFS or GDAS GRIB2 files on pressure levels to a Dataset for arlmet.write_dataset().

This is HYSPLIT’s api2arl for NCEP’s pgrb2 files on a lat/lon grid (GFS forecasts and GDAS analyses at 0.25, 0.5 or 1 degree), taking the Datasets cfgrib.open_datasets() returns for one or several files. Each valid time is one time step. Where two files hold a field at the same valid time, the one with the shorter forecast step is used: an analysis (f000) gives the instantaneous fields and the previous cycle’s 6-hour forecast the precipitation and fluxes the analysis lacks, as NOAA’s GDAS ARL files are made. A field a time step has no file for is written as a missing record there. The index record’s forecast hour is the step of the file that gave the pressure-level fields.

GRIB2 (cfgrib name)

ARL

conversion

gh

HGTS

geopotential height (m)

t, u, v

TEMP, UWND, VWND

K, m s-1

w

WWND

Pa s-1 to hPa s-1

r

RELH

%

q

SPHU

kg kg-1

sp

PRSS

Pa to hPa

prmsl

MSLP

Pa to hPa

orog

SHGT

m

t (surface)

TMPS

K

t2m, r2

T02M, RH2M

K, %

u10, v10

U10M, V10M

m s-1

tcc (instant)

TCLD

%

cape, cin

CAPE, CINH

J kg-1

fricv

USTR

m s-1

HPBL (see below)

PBLH

m

tp

TPP6

kg m-2 to m, since the bucket’s start

avg_ishf

SHTF

W m-2, positive upward

avg_slhtf

LHTF

W m-2, positive upward

sdswrf

DSWF

W m-2

avg_utaua,

UMOF,

N m-2

avg_vtaua

VMOF

Older ecCodes name the averaged fluxes shtfl, lhtfl, dswrf, uflx and vflx; both names are read.

NCEP’s planetary boundary layer height, HPBL, is a local GRIB2 parameter that ecCodes (2.50 and earlier) has no name for, so cfgrib calls it unknown, and cfgrib.open_datasets() drops it from a forecast file, where a second unnamed field shares its level. Open it on its own with GFS_HPBL (Examples): it is found by its numbers and written as PBLH. Without it no PBLH is written. A future ecCodes that names it hpbl needs no second open.

Parameters:
  • datasets (Iterable[Dataset]) – The Datasets of one or more GRIB2 files, as cfgrib.open_datasets() returns them (each with a scalar valid_time), one list after another. Datasets and variables not in the table are left out. Every time step must have the same pressure levels; select them first (ds.sel(isobaricInhPa=...)) to write fewer.

  • bbox (tuple[float, float, float, float] | None) – A bounding box (west, south, east, north) in degrees, as arlmet.open_dataset() takes: only the grid cells inside it are converted. Longitudes may be from -180 to 180 or from 0 to 360, and the box may cross 0. None converts the whole grid.

Returns:

The fields in arlmet.open_dataset()’s layout, with source "GFS" and each variable’s ARL units in its attrs.

Return type:

Dataset

Raises:

ValueError – If no Dataset holds pressure-level fields, a Dataset has no scalar valid time, the time steps have different pressure levels, or the grids differ.

Notes

A field missing at every point (the GDAS files’ instantaneous tcc) is left out when it is written. No DIF* records are written, as api2arl writes none.

Each field is read from its file once, cropped to bbox first, and is then in memory: a global 0.25 degree time step is about 3 GB, the western United States about 0.2 GB.

Examples

>>> import cfgrib
>>> import arlmet
>>> files = ["gdas.t18z.pgrb2.1p00.f006", "gdas.t00z.pgrb2.1p00.f000"]
>>> import xarray as xr
>>> hpbl = {
...     "filter_by_keys": arlmet.converters.GFS_HPBL,
...     "read_keys": list(arlmet.converters.GFS_HPBL),
... }
>>> datasets = [
...     ds for f in files for ds in cfgrib.open_datasets(f)
... ]
>>> datasets += [
...     xr.open_dataset(f, engine="cfgrib", backend_kwargs=hpbl) for f in files
... ]
>>> ds = arlmet.converters.from_gfs(datasets)
>>> arlmet.write_dataset(ds, "gdas.arl")