Quick Start#
This page shows the shortest useful end-to-end workflow: open an ARL file, inspect a few variables, crop it to a smaller domain, and write the result back to disk.
The examples run when the docs are built, on met.arl, a small file of made-up
values that the build writes with arlmet.write_dataset(): a July day over
Utah, four time steps six hours apart, on a 61 by 51 grid of 0.1 degree cells
and four pressure levels. Use your own file’s name in its place; if you have no
ARL file yet, Downloading Archived Meteorology shows how to fetch one.
Open a file lazily with xarray#
The main entry point is arlmet.open_dataset(). It returns an
xarray.Dataset backed by lazy ARL reads, so variables are unpacked only
when you actually access them.
Surface variables (e.g. PRSS, SHGT) have dimensions
(time, lat, lon) and upper-air variables (e.g. UWND, VWND,
TEMP) have dimensions (time, level, lat, lon). On a projected grid,
such as HRRR’s or NAM’s, lat and lon are y and x (the
lat and lon coordinates are then 2D). The level
coordinate contains only the upper-air pressure levels. ds.isel(level=0)
selects the first upper-air level for upper-air variables and leaves surface
variables unchanged.
In [1]: import arlmet
In [2]: ds = arlmet.open_dataset("met.arl")
In [3]: print(ds)
<xarray.Dataset> Size: 1MB
Dimensions: (lon: 61, lat: 51, level: 4, time: 4)
Coordinates:
* lon (lon) float64 488B -115.0 -114.9 -114.8 ... -109.1 -109.0
* lat (lat) float64 408B 38.0 38.1 38.2 38.3 ... 42.8 42.9 43.0
* level (level) int64 32B 1 2 3 4
pressure (level) float64 32B 850.0 700.0 500.0 300.0
* time (time) datetime64[us] 32B 2024-07-18 ... 2024-07-18T18:00:00
forecast_hour (time) int64 32B 0 0 0 0
arl_grid int64 8B 0
Data variables:
HGTS (time, level, lat, lon) float32 199kB ...
PRSS (time, lat, lon) float32 50kB ...
SHGT (time, lat, lon) float32 50kB ...
T02M (time, lat, lon) float32 50kB ...
TEMP (time, level, lat, lon) float32 199kB ...
UWND (time, level, lat, lon) float32 199kB ...
VWND (time, level, lat, lon) float32 199kB ...
WWND (time, level, lat, lon) float32 199kB ...
Attributes:
source: DOCS
vertical_flag: 2
arlmet_version: 0.1.0b13
In [4]: print(ds["PRSS"].dims)
('time', 'lat', 'lon')
In [5]: print(ds["UWND"].dims)
('time', 'level', 'lat', 'lon')
In [6]: ds.isel(level=0) # selects first upper level; surface vars unchanged
Out[6]:
<xarray.Dataset> Size: 399kB
Dimensions: (lon: 61, lat: 51, time: 4)
Coordinates:
* lon (lon) float64 488B -115.0 -114.9 -114.8 ... -109.1 -109.0
* lat (lat) float64 408B 38.0 38.1 38.2 38.3 ... 42.8 42.9 43.0
* time (time) datetime64[us] 32B 2024-07-18 ... 2024-07-18T18:00:00
forecast_hour (time) int64 32B 0 0 0 0
level int64 8B 1
arl_grid int64 8B 0
pressure float64 8B 850.0
Data variables:
HGTS (time, lat, lon) float32 50kB ...
PRSS (time, lat, lon) float32 50kB ...
SHGT (time, lat, lon) float32 50kB ...
T02M (time, lat, lon) float32 50kB ...
TEMP (time, lat, lon) float32 50kB ...
UWND (time, lat, lon) float32 50kB ...
VWND (time, lat, lon) float32 50kB ...
WWND (time, lat, lon) float32 50kB ...
Attributes:
source: DOCS
vertical_flag: 2
arlmet_version: 0.1.0b13
arlmet also registers itself as an xarray backend, so the same Dataset is
available through xarray.open_dataset() with engine="arl". That route
adds xarray’s generic options, such as chunks= for dask-backed arrays.
arl-met does not install dask: chunks= raises ImportError until you run
pip install dask.
In [7]: import xarray as xr
In [8]: ds = xr.open_dataset("met.arl", engine="arl")
In [9]: ds = xr.open_dataset("met.arl", engine="arl", chunks={"time": 1})
In [10]: ds["TEMP"]
Out[10]:
<xarray.DataArray 'TEMP' (time: 4, level: 4, lat: 51, lon: 61)> Size: 199kB
dask.array<open_dataset-TEMP, shape=(4, 4, 51, 61), dtype=float32, chunksize=(1, 4, 51, 61), chunktype=numpy.ndarray>
Coordinates:
* time (time) datetime64[us] 32B 2024-07-18 ... 2024-07-18T18:00:00
forecast_hour (time) int64 32B dask.array<chunksize=(1,), meta=np.ndarray>
* level (level) int64 32B 1 2 3 4
pressure (level) float64 32B dask.array<chunksize=(4,), meta=np.ndarray>
* lat (lat) float64 408B 38.0 38.1 38.2 38.3 ... 42.8 42.9 43.0
* lon (lon) float64 488B -115.0 -114.9 -114.8 ... -109.1 -109.0
arl_grid int64 8B ...
ARL files have no standard extension, so xarray recognizes them by their first
record: xr.open_dataset("met.arl") works without engine= too,
as long as no other installed backend claims the file first.
Select a smaller domain while reading#
Pass bbox= and levels= to crop before unpacking.
In [11]: ds = arlmet.open_dataset(
....: "met.arl",
....: bbox=(-114.0, 39.0, -110.0, 42.0),
....: levels=[0, 1, 2],
....: )
....:
In [12]: ds.sizes
Out[12]: Frozen({'lon': 41, 'lat': 31, 'level': 2, 'time': 4})
Write a cropped copy to a new ARL file#
Use arlmet.extract_subset() when you want a new ARL file on disk rather
than an in-memory xarray subset.
In [13]: arlmet.extract_subset(
....: "met.arl",
....: "cropped.arl",
....: bbox=(-114.0, 39.0, -110.0, 42.0),
....: levels=[0, 1, 2],
....: )
....:
Out[13]: PosixPath('cropped.arl')
Modify a file and write it back#
Use arlmet.open_dataset() and arlmet.write_dataset() for the
common case where surface variables have no level dimension and upper-air
variables share one level coordinate.
In [14]: ds = arlmet.open_dataset("met.arl")
In [15]: ds["TEMP"] = ds["TEMP"] - 273.15
In [16]: arlmet.write_dataset(ds, "edited.arl")
If you need a trailing DIF record for a parent variable, set the parent
DataArray’s diff attr before writing:
In [17]: ds["WWND"].attrs["diff"] = "DIFW"
In [18]: arlmet.write_dataset(ds, "edited-with-diff.arl")
The forecast_hour coordinate is each time step’s index-record forecast hour,
and arlmet.write_dataset() writes it into every record. For irregular
files, including per-record forecast hours, see Writing ARL Files for the
low-level arlmet.File workflow.
Sample variables at arbitrary points#
Use arlmet.sample_points() for trajectory or receptor-style sampling.
In [19]: import pandas as pd
In [20]: points = pd.DataFrame(
....: {
....: "lon": [-111.9],
....: "lat": [40.7],
....: "z": [850.0],
....: "time": ["2024-07-18 00:00"],
....: }
....: )
....:
In [21]: arlmet.sample_points("met.arl", points, ["UWND", "VWND"])
Out[21]:
lon lat z time UWND VWND
0 -111.9 40.7 850.0 2024-07-18 00:00 6.191424 -3.834117
Where to go next#
Downloading Archived Meteorology for NOAA archive download helpers
Cropping ARL Data for lazy reads versus writing cropped ARL files
Writing ARL Files for Dataset writer requirements and low-level file creation
API Reference for the full reference