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
levelcoordinate
Upper-air levels are written in
levelcoordinate order and renumbered1..N(surface is 0), so a Dataset holding a subset of a file’s levels (e.g. fromopen_dataset(levels=...)ords.sel(level=...)) is written as a compact file, likearlmet.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 howopen_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 (asopen_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, usearlmet.RecordSet.create_datarecord().A variable whose
attrs["diff"]names aDIF*record is written with that DIF record after it on each level (seearlmet.RecordSet.create_datarecord()). The name must be one HYSPLIT’s name table pairs with the variable:DIFWforWWNDorDZDTandDIFTforTKENorTKEUabove the surface, anyDIF*name forTPP*orPRT*at the surface.- Parameters:
ds (
Dataset) – Dataset in the layout returned byopen_dataset().filename_or_obj (
str|PathLike[str]) – Output ARL file path. Overwrites any existing file, but must not be the filedswas opened from.vertical_axis (
VerticalAxis|None) – Vertical axis to write. By default it is rebuilt from the Dataset’slevelcoordinates. Required for surface-only Datasets; otherwise it must have one level perlevelcoordinate value plus the surface, and agree with the Dataset’spressure,sigma,heightoretacoordinate when it has one.
- Return type:
- Raises:
ValueError – If the Dataset does not match the layout above (including a
lat,lon,yorxcoordinate that does not increase), has no time steps, a time is outside 1940 to 2039 (a label’s two-digit year cannot hold it), alat,lon,xorycoordinate disagrees with the grid thearl_gridcoordinate 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, anattrs["diff"]name is not one the name table pairs with its variable, orfilename_or_objis the filedswas opened from.
Notes
The file is written to a temporary file next to
filename_or_objand renamed into place once complete, so an interrupted write never leaves a truncated file under the final name.