arlmet.sample_points#

arlmet.sample_points(files, points, variables, *, time=None, z_kind='pressure', method='linear', earth_relative=False, model_top=25000.0)[source]#

Sample meteorological variables at arbitrary (lon, lat, z, time) points.

Accepts a single ARL file or a sequence of files spanning different time periods. Each may be an open File or a path to an ARL file; paths are opened (read mode) and closed automatically, while already-open Files are left open for the caller to manage. A path is opened and its index records read on every call, so in a loop over many calls on one file pass an open File. Each point is sampled from the file that contains its valid time. Time is not interpolated: each point’s time must be one of the valid times of the files, and a point between two valid times raises ValueError.

An input may also be a Dataset in arlmet.open_dataset()’s layout, such as a converter’s output (arlmet.converters.from_gfs(), …). It is sampled as the ARL file arlmet.write_dataset() would write from it, without writing one: the same vertical axis and values, and a variable is stored at a level when it holds a value there at any time step; its slice that is NaN everywhere at one time step reads as NaN, as the file’s record flagged missing does. The values are the Dataset’s own, so they differ from the written file’s by its packing precision at most.

Parameters:
  • files (File | str | PathLike[str] | Dataset | Sequence[File | str | PathLike[str] | Dataset]) – A single open File, path, or Dataset in arlmet.open_dataset()’s layout, or a sequence of them. Each valid time must appear in at most one input.

  • points (DataFrame | Mapping[str, Any]) – Table-like object with lon, lat (degrees), and z columns, and optionally a time column. Any other columns are carried through to the result unchanged.

  • variables (str | Iterable[str]) – One or more ARL field names (e.g. "TEMP", "UWND"), or "pressure" for the virtual pressure variable. A name must not collide with an existing column of points. On a WRF file with mass-coupled winds (MUU0, MUV0) UWND, VWND and WWND are decoupled as HYSPLIT does (UWND * MSFU / MUU0, …), and on a file with THET but no TEMP, TEMP is THET * (PRES / 1000) ** 0.286.

  • time (Timestamp | str | None) – One valid time for every point, used when points has no time column. Passing both raises ValueError. When neither is given, the input file(s) must contain exactly one valid time, which is used. A time must be a valid time of a file (no interpolation in time).

  • z_kind (Literal['native', 'pressure', 'agl', 'msl']) – Vertical coordinate system of the z values. See arlmet.File.sample_points() for the method each vertical coordinate system uses and the fields it requires.

  • method (Literal['linear', 'nearest']) – Horizontal interpolation: bilinear or nearest grid point. At heights ("agl", "msl"), as in HYSPLIT, the column at each of the four grid points around a point is interpolated to the point’s z first, with that grid point’s own ground and level heights, and the four values are then blended; a grid point whose column ends below z does not count, and the others are reweighted. At pressures and native levels each level is blended first, as before. On a WRF file (source AWRF) UWND and VWND are read where WRF’s staggered grid holds them, half a cell east and north of each point, as HYSPLIT reads them; “nearest” gives the winds at the nearest cell’s center. The Sampling guide has the details.

  • earth_relative (bool) – Rotate sampled wind pairs (UWND/VWND, U10M/V10M) from the grid axes to east/north, using the meridian convergence of each file’s grid. Both components of a pair must be requested. No effect on lat/lon grids.

  • model_top (float | None) – The HYSPLIT run’s top of model domain [m], the CONTROL file’s line after the vertical motion option (PYSTILT’s z_top, 25000 by default). At heights ("agl", "msl") the values are the ones a HYSPLIT run with that top uses: on HYSPLIT’s own internal levels (those its metlvl picks for this file’s vertical grid), which follow the terrain as sigma surfaces that go flat at max(model_top, 25000) m, linear between those levels, the first level’s value below it (10 m, or 5 m on the finest grid), and above a file’s top TEMP, UWND, VWND and pressure extrapolated as HYSPLIT does (other variables NaN there). A sigma, terrain-following, hybrid or WRF file without SHGT takes HYSPLIT’s terrain estimate from PRSS. The default matches a run with PYSTILT’s default top; a run with another top needs its value, because below 25 km the internal levels depend on it (the Sampling guide says how far). None reads each grid point’s column at the point’s own height above that grid point’s ground, between the file’s own levels, which is not what HYSPLIT does (differences of up to 4.6 K, 3.5 m/s and 1.9 hPa below 5 km) and gives NaN above a file’s top. The default applies at heights only: at "pressure" and "native" it is ignored, and a value passed with either raises.

Returns:

Copy of points (all columns and the index preserved) with one added column per requested variable. Points outside the grid or the vertical range are NaN. A point within 1e-6 cells of the grid’s edge is on it, so a projected grid’s edge nodes, given by the longitude and latitude arlmet.Grid.calculate_coords() reports, have values. result.attrs["arlmet_version"] is the arlmet version that sampled. At heights (z_kind "agl" or "msl") with a model_top, result.attrs["model_top"] is the one used [m]; with model_top=None or at pressures it is absent.

Return type:

DataFrame

Raises:

ValueError – If a required column is missing, time is given alongside a time column, no time is given and the inputs hold more than one time, a point time is in none of the files, a time is in more than one file, a variable name collides with a column of points, z_kind or method is invalid, a field that z_kind requires is missing, or model_top is passed with z_kind "pressure" or "native" or is not a positive number.

Examples

>>> import pandas as pd
>>> import arlmet
>>> points = pd.DataFrame(
...     {"lon": [-111.9], "lat": [40.7], "z": [850.0], "time": ["2024-07-18 00:00"]}
... )
>>> arlmet.sample_points("met.arl", points, ["UWND", "VWND"]).round(1)
     lon   lat      z              time  UWND  VWND
0 -111.9  40.7  850.0  2024-07-18 00:00   6.2  -3.8

Sample across files spanning different times by passing their paths:

>>> arlmet.sample_points(
...     ["met_00.arl", "met_06.arl"], points, ["UWND", "VWND"]
... ).round(1)
     lon   lat      z              time  UWND  VWND
0 -111.9  40.7  850.0  2024-07-18 00:00   6.2  -3.8

Sample GFS GRIB files without writing an ARL file, through the converter:

>>> import cfgrib
>>> datasets = [
...     ds for f in ["gfs.t00z.pgrb2.0p25.f000"] for ds in cfgrib.open_datasets(f)
... ]
>>> met = arlmet.converters.from_gfs(datasets)
>>> arlmet.sample_points(met, points, ["TEMP"])