Developer notes#
Reference material for people changing arl-met itself. The rules a change must keep (the ARL format rules and the design invariants) and the commands are in AGENTS.md; how to send a change is in Contributing.
HYSPLIT routines arl-met mirrors#
arl-met follows HYSPLIT’s own reader and writer wherever the format leaves room
for choice. Every agreement figure below comes from HYSPLIT v5.3.2’s own
library, run by the Fortran drivers in validation/ (the files, the points
and HYSPLIT’s values are there, with the steps to run them again);
extract_subset is checked against xtrct_grid built from the same
HYSPLIT source. The Fortran routines it matches:
HYSPLIT |
What it does |
In arl-met |
|---|---|---|
|
pack and unpack one field as byte differences; compute the checksum |
|
|
write records and the index record; the checksum table is reset to 0 at each time step and filled only for the records packed |
|
|
read a met file; never compares checksums |
|
|
read the 12 projection values raw ( |
read and written raw too; only |
|
place a longitude on a lat/lon grid: in 0-360, measured from
|
|
|
the map projection of a projected grid: |
|
|
choose the vertical conversion from the vertical flag |
|
|
build the profile at every grid point, then read a value at a position bilinearly from the four grid points around it |
|
|
the run’s internal vertical grid: |
|
|
the sigma (flag 1, and flag 5 without WRF’s hybrid coefficients) and
hybrid (flag 4) profiles: heights from the ground
( |
the column in |
|
the pressure-level (flag 2) profile: heights from |
the column in |
|
the terrain-following (flag 3) profile: a stored level |
|
|
the WRF (flag 5) profile: pressure from the |
not used: the coefficients are not in the ARL file, so
|
|
crop a met file to a grid window |
|
Vertical-axis conversions#
Each subclass of arlmet.VerticalAxis converts its levels with
keyword-only inputs (to_pressure(*, surface_pressure=None) and
to_height_agl(*, surface_pressure=None, temperature=None, pressure=None,
hgts=None, terrain=None)), uses only the ones its coordinate system needs, and
raises ValueError naming a missing one.
Flag |
Class |
|
|
|---|---|---|---|
1 |
|
hypsometric, from |
|
2 |
the stored levels |
|
|
3 |
raises |
the stored levels (already above ground) |
|
4 |
|
hypsometric, as for flag 1 |
|
5 |
raises |
hypsometric, as for flag 1 with |
Any other flag raises ValueError in from_flag.
Flag 5 is what HYSPLIT’s arw2arl writes for a WRF run with
hybrid_opt=2: the levels are WRF’s mass-level eta (ZNU) with 1.0 for
the surface, and the offset is WRF’s model top P_TOP in hPa. HYSPLIT’s
prfcom sends a flag-5 file to prfwrf only when the file has MUBA and
MUPE and hybrid_wrfvcoords.txt sits beside it; otherwise metini
treats it as flag 1 and prfsig integrates heights between the PRES
levels. WrfAxis files follow that prfsig path exactly as sigma files
with PRES do (vertical._hysplit_z_agl and the column in
_ops/sample/column.py); on two HRRR columns written as flag-5 files they agree with
the compiled prfsig within 0.007 hPa.
Module layout#
src/arlmet/
__init__.py public exports
_file.py File: file handle, scan of index records, check(), factories
_recordset.py RecordSet: the records of one valid time
record.py DataRecord (one packed 2D field); public home of Header,
IndexRecord, LvlInfo, VarInfo
collection.py RecordCollection protocol, VariableView, VariableAccessor
_header.py Header: the 50-byte record header codec, grid letter helpers
_indexrecord.py IndexRecord, VarInfo, LvlInfo: the index record codec
_grid.py Projection, Grid, GridWindow: horizontal geometry only
vertical.py VerticalAxis (abstract) and SigmaAxis, PressureAxis,
TerrainAxis, HybridAxis, WrfAxis
packing.py pack(), unpack(), calculate_checksum()
_pack.c C extension arlmet._pack: pack_core and unpack_core,
HYSPLIT's pakout and pakinp
_exceptions.py ARLFormatError, ARLFormatWarning
_io.py atomic_output(), reject_same_file(): safe output paths
_time.py ensure_timestamp()
archives.py Archive, HRRRArchive, NAMArchive, GDASArchive, ...;
ARCHIVES registry, get_archive(): NOAA archive downloads
_inventory.py inventory(), Inventory: a folder's files by their headers
_probe.py probe(), Probe: one file from two index records
_ops/
subset.py extract_subset(), resolve_window(), normalize_levels()
sample/ sample_points(), one step per module:
points.py sample_points(); one time step (_sample_time)
horizontal.py the points on the grid, bilinear weights, blending
fields.py a field as HYSPLIT's metinp reads it
column.py the column per vertical type (prfsig, prfprs, prfter, prfecm)
profile.py a variable's profile, read at a height, pressure or level
internal.py HYSPLIT's internal levels (metlvl, zindex, prfend)
concat.py concat(), concat_by_time()
_xarray/
dataset.py open_dataset(), write_dataset(): the flat Dataset path
_entrypoint.py ARLBackendEntrypoint: xr.open_dataset(path, engine="arl")
_backend.py ArlVariableArray (xarray BackendArray)
_coords.py the arl_grid coordinate codec, vertical-axis coordinates
_accessor.py ds.arl.grid, .vertical_axis, .source
_vertical.py pressure(), z_agl(), z_msl()
Module imports#
Runtime imports between modules (TYPE_CHECKING imports left out; lazy
means inside a function body):
vertical, _exceptions, _io, _time no arlmet imports
_grid (pyproj)
packing → _pack (C extension), _grid
_header → _time, _exceptions, _grid
_indexrecord → _time, _exceptions, _grid, _header, vertical
record → _exceptions, _grid, _header, _indexrecord, packing, vertical
collection → _grid, record, vertical
_recordset → collection, _grid, _header, _indexrecord, record, vertical
_file → _time, collection, _exceptions, _grid, _header, _indexrecord,
record, _recordset, vertical;
lazy: _ops.sample, _ops.subset, _xarray.dataset
_probe → _exceptions, _grid, _header, _indexrecord
_inventory → _exceptions, _file, _io, _time
_ops.subset → _io, _exceptions, _file, _grid, _header, _indexrecord, packing, record, vertical
_ops.sample → _time, _grid, vertical; lazy: _file
_ops.concat → _exceptions, _file, _io
_xarray.* → _io, _time, _grid, _header, record, _ops.sample, _ops.subset, vertical; lazy: _file
converters.* → _grid, _header, _indexrecord, _xarray._coords, vertical
archives → _time; lazy: fsspec, _ops.subset
_file imports the operations and the Dataset builder lazily because they
import _file. archives is not imported by import arlmet, and imports
fsspec only when it downloads.
Memory tests#
tests/test_memory.py measures the writers with tracemalloc: the peak
during extract_subset() (both the byte-copy path, without a bbox,
and the repack path, with one) and write_dataset() (including from
a lazy Dataset), and what is still held after they return with the garbage
collector disabled. Its grids are about 200x200, so array buffers dominate Python
object overhead.
Python sizes each open file’s buffer from the filesystem’s st_blksize. On a
filesystem with a large block size (some network filesystems report 128 KB,
where a local disk reports 4 KB) each open file adds that much, and three of
these tests fail. Run them with the temporary directory on a local disk.
Examples in the docs#
The user guides run their examples when the docs are built (just build-docs,
as CI does), so they show real output and the build fails when one breaks. There
is no sample ARL file in the repository, and an example never downloads one, so
docs/_ext/docs_examples.py writes small files of made-up values with
arlmet.write_dataset(). A page that reads a file starts with a block that
is run but not shown:
.. ipython:: python
:suppress:
import os
import docs_examples
docs_cwd = docs_examples.temp_workdir()
docs_examples.sample_file("met.arl")
temp_workdir() changes to a new temporary folder, so the examples can write
files with short names, and the page ends with another such block,
os.chdir(docs_cwd). sample_file() takes the valid times, and
upper=False for surface variables only or projected=True for a Lambert
grid. A few things to know:
An expression is echoed only at the top level of a block:
printone inside awithorfor.A block that raises or warns fails the build, unless it has
:okexcept:or:okwarning:.All pages share one session, so each page imports what it uses.
An example that downloads stays a plain
code-block, and its page says so.
Benchmarks#
Use real files from the archives for performance work, for example the July 2024 NCEP/NCAR Reanalysis month:
from arlmet.archives import get_archive
get_archive("reanalysis").fetch("2024-07-01", "2024-07-01", dest_dir="met/")
This gives RP202407.gbl (73x144, 124 times, 18 levels). Measured on one
development machine, extract_subset() took about 2.1 s for
bbox=(-170, 10, -40, 70) (repack path) and about 0.3 s for
levels=[0, 1, 2, 3, 4] without a bbox (byte-copy path). A 614x428 NAM12 day
(get_archive("nam12")) took about 0.27 s for the same levels.
Format references#
HYSPLIT meteorology files: the ARL format overview.
HYSPLIT CONTROL file, the “number of input data grids” line: one number
Nmakes each file its own grid; two numbers1 Nare one grid withNfiles in time.HYSPLIT compilation limits: at most 12 met files with the one-number form and 128 files per grid with
1 N, the reasonconcat()andconcat_by_time()exist.GDAS1 packing notes and the READY archive catalog.