arlmet.write_dataset#

arlmet.write_dataset(ds, filename_or_obj, *, vertical_axis=None)[source]#

Write a simple flat Dataset representation to an ARL file.

This writer supports the common-case Dataset contract returned by open_dataset():

  • surface variables use dims (time, y, x) or (time, lat, lon)

  • upper-air variables use dims (time, level, y, x) or (time, level, lat, lon)

  • all upper-air variables share the same level coordinate

Upper-air levels are written in level coordinate order and renumbered 1..N (surface is 0), so a Dataset holding a subset of a file’s levels (e.g. from open_dataset(levels=...) or ds.sel(level=...)) is written as a compact file, like arlmet.extract_subset() does.

Every (variable, level) that holds data at some time step is written at every time step, so each time step has the same records. Where its slice at a time step is entirely NaN (as a record flagged missing in the source file reads), the record is written flagged missing, as HYSPLIT’s writer leaves a record it never fills (pakini.f: forecast hour -1, no values), and HYSPLIT skips it. A (variable, level) that is NaN at every time step gets no record: that is how open_dataset() represents a variable stored on only some levels. Finding which slices hold data takes one pass over the values before anything is written, so a lazy Dataset (as open_dataset() returns) reads each slice twice.

The forecast_hour(time) coordinate gives each time step’s index-record forecast hour, and every record of that time step is written with it (0 when the coordinate is absent). A file whose records carry their own forecast hours is rewritten with the time step’s hour in each; HYSPLIT reads a data record’s forecast hour only as the missing flag, so this does not change what it reads. To write per-record hours, use arlmet.RecordSet.create_datarecord().

A variable whose attrs["diff"] names a DIF* record is written with that DIF record after it on each level (see arlmet.RecordSet.create_datarecord()). The name must be one HYSPLIT’s name table pairs with the variable: DIFW for WWND or DZDT and DIFT for TKEN or TKEU above the surface, any DIF* name for TPP* or PRT* at the surface.

Parameters:
  • ds (Dataset) – Dataset in the layout returned by open_dataset().

  • filename_or_obj (str | PathLike[str]) – Output ARL file path. Overwrites any existing file, but must not be the file ds was opened from.

  • vertical_axis (VerticalAxis | None) – Vertical axis to write. By default it is rebuilt from the Dataset’s level coordinates. Required for surface-only Datasets; otherwise it must have one level per level coordinate value plus the surface, and agree with the Dataset’s pressure, sigma, height or eta coordinate when it has one.

Return type:

None

Raises:

ValueError – If the Dataset does not match the layout above (including a lat, lon, y or x coordinate that does not increase), has no time steps, a time is outside 1940 to 2039 (a label’s two-digit year cannot hold it), a lat, lon, x or y coordinate disagrees with the grid the arl_grid coordinate describes, the vertical axis disagrees with the Dataset’s own vertical coordinate or has more than 99 levels above the surface (a record’s header numbers its level in two characters), a slice is partly NaN or non-finite, an attrs["diff"] name is not one the name table pairs with its variable, or filename_or_obj is the file ds was opened from.

Notes

The file is written to a temporary file next to filename_or_obj and renamed into place once complete, so an interrupted write never leaves a truncated file under the final name.