Writing ARL Files#
arl-met has two supported authoring paths:
arlmet.open_dataset()/arlmet.write_dataset()for the common case where surface variables have noleveldimension and upper-air variables share onelevelcoordinate.arlmet.Filefor exact low-level control, including per-record forecast hours and other irregular layouts.
Note
The documented Dataset writer is intentionally conservative. If you have a
better idea for handling irregular ARL files at a higher level, open an
issue or pull request. The current recommendation is to keep one simple
Dataset contract and route edge cases through arlmet.File.
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()
(Quick Start describes it). Use your own file’s name in its place.
Round-trip with Dataset#
In [1]: import arlmet
In [2]: ds = arlmet.open_dataset("met.arl")
In [3]: ds["TEMP"] = ds["TEMP"] - 273.15
In [4]: arlmet.write_dataset(ds, "edited.arl")
In [5]: arlmet.open_dataset("edited.arl")["TEMP"].max().item() # degrees C now
Out[5]: 15.084589958190918
To request a trailing DIF record when writing a parent variable, set the
parent DataArray’s diff attr to the DIF record name:
In [6]: ds = arlmet.open_dataset("met.arl")
In [7]: ds["WWND"].attrs["diff"] = "DIFW"
In [8]: arlmet.write_dataset(ds, "edited-with-diff.arl")
In [9]: with arlmet.File("edited-with-diff.arl") as met:
...: record = met[met.times[0]][(1, "WWND")]
...: print([dif.variable for dif in record.diffs])
...:
['DIFW']
Dataset write contract#
timeandarl_gridmust be presentsourcemust be inds.attrs(arlmet_versionthere is not written)forecast_hour(time), a coordinate, is the ARL index-record forecast hour (0 when absent)surface variables use dims
(time, lat, lon)or(time, y, x)upper-air variables use dims
(time, level, lat, lon)or(time, level, y, x)the
latandlon(oryandx) coordinates increase along their dimensions, asarlmet.open_dataset()returns them; rows and columns are written in array order, so a Dataset turned north-up withds.isel(lat=slice(None, None, -1))raises until it is sorted (ds.sortby("lat"))all upper-air variables must share the Dataset
levelcoordinate
The forecast_hour coordinate holds one forecast hour per time step, the one
the ARL index record stores, and arlmet.write_dataset() writes it into the
index record and into every data record of that time step. A data record’s own
forecast hour can differ from its index record’s (an accumulated field taken from
a 6-hour forecast may say 6 where the index says 0); that is a fact about the
file’s records, read with arlmet.DataRecord.forecast, and the Dataset
does not represent it. HYSPLIT reads a data record’s forecast hour only as the
missing flag (a negative hour marks the record missing); the forecast hour it
uses is the index record’s. So a round trip through a Dataset changes no value
HYSPLIT reads, and a file that must keep per-record hours is written with
arlmet.File (below).
In [10]: ds = arlmet.open_dataset("met.arl")
In [11]: ds["forecast_hour"]
Out[11]:
<xarray.DataArray 'forecast_hour' (time: 4)> Size: 32B
array([0, 0, 0, 0])
Coordinates:
* 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
Attributes:
long_name: ARL index record forecast hour
description: Forecast hour stored in the INDX record for each time step....
units: hours
Write constraints#
forecast_hourmust be a coordinate with dims("time",)when presentvariable names must be exactly 4 characters, as ARL stores them (a shorter name such as
O3is refused rather than padded, since the padded name would not match the data variable when read back)each 2D slice must be finite, or entirely NaN, while a partly-NaN slice raises. A (variable, level) that holds data at some time step is written at every time step, and where its slice is entirely NaN (as a record flagged missing reads) the record is written flagged missing, as HYSPLIT’s writer leaves a record it never fills (forecast hour -1); a (variable, level) that is NaN at every time step gets no record (this is how
arlmet.open_dataset()represents a variable stored on only some levels). Finding which slices hold data takes one pass over the values before writing, so a lazy Dataset reads each slice twiceupper-air levels are written in
levelcoordinate order and renumbered1..N, so a Dataset holding a subset of a file’s levels (fromopen_dataset(levels=...)ords.sel(level=...)) is written as a compact filethe output must not be the file the Dataset was opened from; write to a new path. The file is written to a temporary name and renamed into place when complete, so an interrupted write never leaves a truncated file
generated DIF names must be declared on the parent variable with
attrs["diff"], must start withDIF, and must be one HYSPLIT’s name table pairs with the parent (see below)Dataset DIF writing is parent-led; do not add
DIF*variables as separate Dataset data variablessurface-only datasets must pass
vertical_axis=explicitly toarlmet.write_dataset()
Differential records#
arl-met supports two DIF authoring paths:
low-level: pass
diff="DIF..."toarlmet.RecordSet.create_datarecord()Dataset: set
DataArray.attrs["diff"] = "DIF..."on the parent variable
In both cases the caller supplies only the intended parent field values.
arl-met writes the parent record first, immediately unpacks the packed parent,
computes the residual target - unpacked_parent, and writes that residual as
the trailing DIF record.
The low-level writer also takes several names, diff=["DIFW", "DIFW"], and
writes that many DIF records after the parent, in order. Each holds what
packing left after the parent and the DIF records before it, so a reader that
adds them all, as HYSPLIT and arl-met do, gets back to the target. This is how
a crop or rewrite keeps a parent’s DIF records when a file has several; one is
the usual case, and the Dataset path writes one.
Validation rules:
DIF names must start with
DIFeach DIF name must be one that HYSPLIT’s name table pairs with the parent at its level:
DIFWwithWWNDorDZDTandDIFTwithTKENorTKEUabove the surface, anyDIF*name withTPP*orPRT*at the surface (level 0); any other pair raisesValueError, since a reader would not pair it (ARL File Format has the table)generated DIF records inherit the parent time, level, and forecast
the same DIF name cannot be rebound to a different parent variable during one write
existing on-disk
DIF*records are preserved on low-level rewrite and subset extraction
Analysis with open_dataset#
arlmet.open_dataset() gives a flat view suited for analysis. Surface
variables (e.g. PRSS, SHGT) have dimensions (time, lat, lon); upper-
air variables (e.g. UWND, VWND, TEMP) have dimensions
(time, level, lat, lon).
In [12]: ds = arlmet.open_dataset("met.arl", bbox=(-114, 39, -110, 42), levels=[0, 1, 2])
In [13]: print(ds["PRSS"].dims)
('time', 'lat', 'lon')
In [14]: print(ds["TEMP"].dims)
('time', 'level', 'lat', 'lon')
In [15]: p = arlmet.pressure(ds)
In [16]: z = arlmet.z_agl(ds)
In [17]: p.values
Out[17]: array([850., 700.])
In [18]: z.sizes
Out[18]: Frozen({'time': 4, 'level': 2, 'lat': 31, 'lon': 41})
Contributing ideas#
If you have a better high-level pattern for irregular ARL authoring, contributions are welcome. The project intentionally documents one primary Dataset workflow plus the exact low-level fallback.