arlmet.extract_subset#

arlmet.extract_subset(path, dest, *, bbox=None, bbox_crs=None, levels=None, variables=None, overwrite=True)[source]#

Extract a spatial/vertical subset from an ARL file into a new ARL file.

Parameters:
  • path (str | PathLike[str]) – Input ARL file.

  • dest (str | PathLike[str]) – Output ARL file. Must not be path.

  • bbox (tuple[float, float, float, float] | None) – Geographic bounding box (west, south, east, north) in degrees. On a projected grid the crop is the smallest rectangle of cells that holds the box’s part on the grid (arlmet.Grid.window_from_bbox()).

  • bbox_crs (Any) – The coordinate reference system bbox is in ("EPSG:32612", anything pyproj.CRS.from_user_input() takes), making the box (xmin, ymin, xmax, ymax) in its units; the default is degrees (arlmet.Grid.window_from_bbox()).

  • levels (Iterable[int] | None) – Integer ARL level indices to keep, which must include 0, the surface. Output levels are compacted and renumbered from zero while preserving the selected level heights.

  • variables (str | Iterable[str] | None) – Variable names to keep, as the file spells them ("TEMP", not "temp"); a single string is one name. A DIF* name the file holds is accepted, but naming it selects nothing: a DIF* record is written with its parent record, when the parent is selected. All variables are included by default.

  • overwrite (bool) – Replace an existing dest. With False an existing dest is returned as it is, without reading the input, so a rerun skips finished work; nothing checks that it came from the same inputs and arguments.

Returns:

The output path, Path(dest). Open it with File or open_dataset() to read the subset.

Return type:

Path

Raises:
  • TypeError – If levels holds anything but integers (a string, or 1.5).

  • ValueError – If dest is the same file as path, the cropped grid is too small to hold the ARL index record, levels leaves out level 0 (the surface: the output is renumbered from zero, so the lowest kept level would become the surface), variables is empty or names a variable the file does not hold (every unknown name is listed, also when others are known), or the selected levels and variables leave no record in any time step, so there is nothing to write.

Notes

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

Without a horizontal crop (bbox is None or covers the whole grid), the selected records are copied byte for byte, with only their level numbers and the index records rewritten, which is much faster than unpacking and re-packing them. A crop re-packs each record’s window. Either way each DIF* record is written right after the record it corrects, and the index record lists the records in that order.

Examples

>>> import arlmet
>>> out = arlmet.extract_subset(
...     "met.arl",
...     "subset.arl",
...     bbox=(-114.0, 39.0, -110.0, 42.0),
...     levels=[0, 1, 2],
... )
>>> ds = arlmet.open_dataset(out)