Source code for arlmet._file

"""File class for reading and writing ARL meteorology binary files."""

from __future__ import annotations

import os
import warnings
from collections import OrderedDict
from collections.abc import Iterable, Iterator, Mapping, Sequence
from itertools import pairwise
from pathlib import Path
from types import TracebackType
from typing import TYPE_CHECKING, Any, BinaryIO, Literal, Self, cast

import numpy.typing as npt
import pandas as pd
from typing_extensions import override
from xarray.backends import CachingFileManager
from xarray.backends.locks import SerializableLock

from arlmet._exceptions import ARLFormatError, ARLFormatWarning
from arlmet._grid import Grid, Projection
from arlmet._header import check_level_count, check_year, record_length_from_grid
from arlmet._indexrecord import IndexRecord
from arlmet._recordset import RecordSet
from arlmet._time import ensure_timestamp
from arlmet.collection import VariableAccessor
from arlmet.record import DataRecord, _corrects, _is_missing, _require_mode
from arlmet.vertical import _DEFAULT_MODEL_TOP, VerticalAxis

if TYPE_CHECKING:
    import xarray as xr

__all__ = ["File"]


def _open_binary(path: str | os.PathLike[str], mode: str) -> BinaryIO:
    """
    Open ``path`` in binary ``mode`` ("r", "w", or "a").

    CachingFileManager switches mode "w" to "a" after the first open so that
    reopening a writer appends instead of truncating. It only recognizes the
    bare letter, so the manager gets "r"/"w" and the "b" is added here.
    """
    # open() only narrows to BinaryIO for literal modes.
    return cast(BinaryIO, open(path, mode + "b"))


def _with_diffs(rs: RecordSet) -> list[DataRecord]:
    """Return the records of *rs* in index order, each followed by its DIF records."""
    return [rec for record in rs.records for rec in (record, *record.diffs)]


#: More missing records than this in one time step stop HYSPLIT (``metinp.f``).
_MAX_MISSING = 20


def _at(time: pd.Timestamp) -> str:
    """Format a valid time for a message."""
    return f"{time:%Y-%m-%d %H:%M}"


def _span(delta: pd.Timedelta) -> str:
    """Format a time difference in hours, or minutes when it is not whole hours."""
    minutes = int(delta / pd.Timedelta(minutes=1))
    return f"{minutes // 60} h" if minutes % 60 == 0 else f"{minutes} min"


[docs] class File: """ Read or write an ARL meteorology file. Parameters ---------- path : path-like Location of the ARL file on disk. mode : {"r", "w"}, default "r" File mode. Read mode scans the file immediately; write mode expects the caller to provide ``source``, ``grid``, and ``vertical_axis`` before creating records. source : str, optional Four-character ARL source identifier used when writing. grid : Grid, optional Horizontal grid metadata used when writing. vertical_axis : VerticalAxis, optional Vertical axis metadata used when writing: the surface and at most 99 levels above it, the most a record's header numbers. Attributes ---------- path : pathlib.Path Filesystem path for the ARL file. mode : {"r", "w"} Active file mode. times : list[pandas.Timestamp] Sorted valid times discovered in the file. variables : VariableAccessor Lazy accessor for variable-wise views inherited from RecordCollection. Methods ------- __getitem__(key) Get a RecordSet by valid time (Timestamp or string), or by position in ``times`` (int). __iter__() Iterate over valid times in ``times`` order (sorted). create_grid(...) Build and attach a Grid when writing a new file. create_recordset(time, *, forecast=None) Create a writable RecordSet for one valid time. add_record(time, variable, level, *, forecast, data=None, diff=None) Add one writable DataRecord, creating its RecordSet if needed. flush() Write pending record sets to disk and release their in-memory data. sample_points(points, variables, ...) Interpolate fields at arbitrary lon/lat/z sample points. extract_subset(dest, ...) Write a spatial/vertical subset to a new ARL file and return its path. to_dataset(...) Project the file into the simplified analysis xarray Dataset. close() Flush pending writes and release the file handle. Examples -------- >>> import arlmet >>> with arlmet.File("met.arl") as met: ... met.times[0] Timestamp('2024-07-18 00:00:00') """ def __init__( self, path: str | os.PathLike[str], mode: Literal["r", "w"] = "r", *, source: str | None = None, grid: Grid | None = None, vertical_axis: VerticalAxis | None = None, ): # File attrs self.path = Path(path) self.mode: Literal["r", "w"] = mode if self.mode not in ("r", "w"): raise ValueError("Mode must be 'r' (read) or 'w' (write).") # Open the binary file handle self._manager = CachingFileManager(_open_binary, self.path, mode=self.mode) self._handle: BinaryIO | None = None # Record reads are a seek followed by a read on the one shared handle; # this lock keeps the pair atomic when threads read concurrently (e.g. # dask chunks). SerializableLock pickles, unlike threading.Lock. self._lock = SerializableLock() # Must be consistent throughout the file self._source: str | None = source self._grid: Grid | None = grid if self.mode == "w" and vertical_axis is not None: check_level_count(len(vertical_axis.levels) - 1) self._vaxis: VerticalAxis | None = vertical_axis # Initialize recordsets as an ordered dict to preserve time order # Mapping: time -> RecordSet self._recordsets: OrderedDict[pd.Timestamp, RecordSet] = OrderedDict() # Every index record in file order, repeats included, as (valid time, # byte position, bytes in the time step). HYSPLIT finds a time step by # its position, so check() and concat need this order, not ``times``. self._steps: list[tuple[pd.Timestamp, int, int]] = [] self.variables = VariableAccessor(self) # Scan the file to populate recordsets in read mode if self.mode != "w": self._scan() def __getstate__(self) -> dict[str, Any]: """Copy and pickle without the open handle; the manager reopens the file.""" state = self.__dict__.copy() state["_handle"] = None return state @property def handle(self) -> BinaryIO: """Open binary handle to the file, reopened if the file cache closed it.""" # Hot record read/write paths hit this repeatedly, so keep one # acquired handle per File instead of reentering the manager. xarray's # global file cache closes the least recently used file once it holds # `file_cache_maxsize` (128) files, so reacquire when that happens; the # manager reopens the file (in append mode for writers). if self._handle is None or self._handle.closed: # CachingFileManager.acquire() returns IO[Any]; _open_binary # guarantees BinaryIO at runtime. self._handle = cast(BinaryIO, self._manager.acquire()) return self._handle def _read_at(self, position: int, n_bytes: int) -> bytes: """Read ``n_bytes`` starting at byte ``position``; safe to call from several threads.""" with self._lock: fh = self.handle fh.seek(position) return fh.read(n_bytes) @property def size(self) -> int: """Size of the file on disk, in bytes.""" return self.path.stat().st_size @property def source(self) -> str: """ARL source identifier; set it in write mode before creating records.""" if self._source is None: raise ValueError("Source has not been set for this File.") return self._source @source.setter def source(self, value: str): _require_mode(self, "w") self._source = value @property def grid(self) -> Grid: """Horizontal grid metadata; set it in write mode before creating records.""" if self._grid is None: raise ValueError("Grid has not been set for this File.") return self._grid @grid.setter def grid(self, value: Grid): _require_mode(self, "w") if not isinstance(value, Grid): raise TypeError("grid must be a Grid instance.") self._grid = value @property def vertical_axis(self) -> VerticalAxis: """Vertical coordinate metadata; set it in write mode before creating records.""" if self._vaxis is None: raise ValueError("Vertical axis has not been set for this File.") return self._vaxis @vertical_axis.setter def vertical_axis(self, value: VerticalAxis): _require_mode(self, "w") if not isinstance(value, VerticalAxis): raise TypeError("vertical_axis must be a VerticalAxis instance.") check_level_count(len(value.levels) - 1) self._vaxis = value @property def times(self) -> list[pd.Timestamp]: """Return a sorted list of timestamps in the file.""" return sorted(self._recordsets.keys()) @property def records(self) -> list[DataRecord]: """All DataRecords in the file, in ``times`` order.""" return [ record for time in self.times for record in self._recordsets[time].records ] @property def record_length(self) -> int: """Length in bytes of each record in the file, from the grid.""" return record_length_from_grid(self.grid)
[docs] def create_grid( self, nx: int, ny: int, *, pole_lat: float, pole_lon: float, tangent_lat: float, tangent_lon: float, grid_size: float, orientation: float, cone_angle: float, sync_x: float, sync_y: float, sync_lat: float, sync_lon: float, ) -> Grid: """ Create and attach the horizontal grid metadata for a writable file. Parameters ---------- nx : int Number of grid points in the x direction. ny : int Number of grid points in the y direction. pole_lat, pole_lon : float Projection pole definition from the ARL index record. tangent_lat, tangent_lon : float Reference latitude and longitude that define the projection. grid_size : float Grid spacing in kilometers at the projection reference point. orientation : float Rotation of the grid y-axis relative to true north. cone_angle : float Projection cone angle used for stereographic, Lambert, or Mercator grids. sync_x, sync_y : float One-based grid coordinates of the synchronization point. sync_lat, sync_lon : float Geographic coordinates of the synchronization point. Returns ------- Grid The created grid instance, also stored on the file. """ _require_mode(self, "w") if self._grid is not None: raise ValueError("Grid has already been set for this File.") # Build projection proj = Projection( pole_lat=pole_lat, pole_lon=pole_lon, tangent_lat=tangent_lat, tangent_lon=tangent_lon, grid_size=grid_size, orientation=orientation, cone_angle=cone_angle, sync_x=sync_x, sync_y=sync_y, sync_lat=sync_lat, sync_lon=sync_lon, ) # Create grid grid = Grid(projection=proj, nx=nx, ny=ny) self._grid = grid return grid
def _create_recordset( self, position: int, source: str | None, grid: Grid | None, time: pd.Timestamp, *, forecast: int | None = None, ) -> RecordSet: """Create a new RecordSet (internal factory).""" if time in self._recordsets: raise ValueError(f"A RecordSet for time {time} already exists.") if source is not None and self._source != source: raise ValueError("Source mismatch when creating RecordSet.") if grid is not None and self._grid != grid: raise ValueError("Grid mismatch when creating RecordSet.") rs = RecordSet(file=self, position=position, time=time, forecast=forecast) self._recordsets[time] = rs return rs
[docs] def create_recordset( self, time: pd.Timestamp | str, *, forecast: int | None = None ) -> RecordSet: """ Create a writable RecordSet for one valid time. Parameters ---------- time : pandas.Timestamp or compatible datetime-like Valid time for the new record set. forecast : int, optional Forecast hour for the index record header. HYSPLIT docs are unclear on this, but conversion code appears to use the forecast hour from the first variable specified in the config file. This is brittle in arlmet's case, so we chose to either allow specifying it here or an index's forecast hour will be set to the minimum forecast hour among its variables (defaulting to -1 when all variables are missing data). Returns ------- RecordSet Writable record set associated with ``time``. Raises ------ ValueError If ``time`` is outside 1940 to 2039, the years a label's two-digit year can hold. """ _require_mode(self, "w") if self.source is None or self.grid is None: raise ValueError("Source and Grid must be set to create RecordSets.") position = -1 # New recordsets have no on-disk position yet source = grid = None # skip checks in _create_recordset ts = ensure_timestamp(time) check_year(ts.year) return self._create_recordset( position=position, source=source, grid=grid, time=ts, forecast=forecast )
[docs] def add_record( self, time: pd.Timestamp | str, variable: str, level: int, *, forecast: int, data: npt.ArrayLike | None = None, diff: str | Sequence[str] | None = None, ) -> DataRecord: """ Add one writable DataRecord, creating its RecordSet if needed. Shorthand for ``create_recordset(time)`` (when ``time`` has no record set yet) followed by :meth:`RecordSet.create_datarecord`. A record set created here derives its index-record forecast from its records; call :meth:`create_recordset` first to set it explicitly. Parameters ---------- time : pandas.Timestamp or str Valid time of the record. variable : str Four-character ARL variable name. level : int ARL level index for the record. forecast : int Forecast hour to write into the record header. data : array-like, optional ``(ny, nx)`` field values. When omitted, assign the whole field later with ``record[:] = values``. diff : str or sequence of str, optional Name of a trailing DIF record to derive from the parent field, or the names of several, written in order. Each must be one HYSPLIT's name table pairs with ``variable`` at ``level`` (see :meth:`RecordSet.create_datarecord`). Returns ------- DataRecord Writable data record for ``variable`` at ``level`` and ``time``. """ _require_mode(self, "w") ts = ensure_timestamp(time) recordset = self._recordsets.get(ts) if recordset is None: recordset = self.create_recordset(ts) return recordset.create_datarecord( variable, level, forecast=forecast, data=data, diff=diff )
def _scan(self) -> None: """ Populate RecordSet objects by walking the on-disk index records. Raises ------ ARLFormatError If the file is not valid ARL: no index record (an empty file), an unparseable index record, a size that is not a whole number of records, an index record that declares more data records than remain, inconsistent metadata between index records, a variable listed twice on one level, or a time step repeated with different content. Warns ----- ARLFormatWarning If a time step is repeated byte for byte (the copy is skipped), or a ``DIF*`` record has no record before it on its level that HYSPLIT's name table pairs it with (it is applied to nothing). """ # Scan the file to populate recordsets in read mode fh = self.handle size = self.size # Extent (position, n_bytes) of each time step's first occurrence, # used to compare against any repeated copy of the same time. extents: dict[pd.Timestamp, tuple[int, int]] = {} # The first byte of each (name, level) of DIF record that pairs with # nothing, and how many there are, for one warning after the scan. unpaired: dict[tuple[str, int], int] = {} n_unpaired = 0 while fh.tell() < size: # Get starting position of each recordset position = fh.tell() # Parse index record try: index = IndexRecord.from_position(fh, position=position) except EOFError: break # End of file except ARLFormatError as exc: raise ARLFormatError(f"{self.path}: {exc}") from exc # Set metadata when reading the first index record if self._source is None: self._source = index.source if self._grid is None: self._grid = index.grid # The first index record fixes the record length. Every record # has the same length, so the file must hold a whole number. if size % self.record_length != 0: raise ARLFormatError( f"{self.path}: file size {size} bytes is not a whole " f"number of {self.record_length}-byte records " f"({self.grid.nx}x{self.grid.ny} grid); the file is " "truncated or corrupt." ) if self._vaxis is None: self._vaxis = index.vertical_axis elif self._vaxis != index.vertical_axis: raise ARLFormatError( f"{self.path}: vertical axis mismatch between index records " f"(index record for {index.time} at byte {position})." ) # The time step is the index record plus one record per variable # per level; it must fit in what remains of the file. record_length = self.record_length n_data = sum(len(lvl.variables) for lvl in index.levels) n_bytes = (1 + n_data) * record_length if position + n_bytes > size: remaining = (size - position) // record_length - 1 raise ARLFormatError( f"{self.path}: index record for {index.time} at byte " f"{position} declares {n_data} data records, but only " f"{remaining} remain in the file; the file is truncated." ) self._steps.append((index.time, position, n_bytes)) # Some NOAA archive files repeat a whole time step. Skip a # byte-identical copy; anything else is ambiguous, so raise. if index.time in extents: first_position, first_n_bytes = extents[index.time] if first_n_bytes != n_bytes or not self._same_bytes( first_position, position, n_bytes ): raise ARLFormatError( f"{self.path}: time step {index.time} is repeated with " f"different content (first at byte {first_position}, " f"again at byte {position})." ) warnings.warn( f"{self.path}: time step {index.time} is repeated " f"(byte-identical copy at byte {position}); the repeated " "copy was ignored.", ARLFormatWarning, stacklevel=3, ) fh.seek(position + n_bytes) continue extents[index.time] = (position, n_bytes) # Create a RecordSet for this index record (time) try: rs = self._create_recordset( position=position, source=index.source, grid=index.grid, time=index.time, forecast=index.forecast, ) except ValueError as exc: # Source/grid mismatch with the first index record. raise ARLFormatError( f"{self.path}: index record for {index.time} at byte " f"{position}: {exc}" ) from exc # Read data records for this index record position += record_length # start of data records for lvl in index.levels: # A DIF record corrects the nearest record before it on its # level that HYSPLIT's name table pairs it with (_corrects), # whatever lies between; several DIF records can correct one # record, as HYSPLIT adds each one it reads. level_records: list[DataRecord] = [] for var, info in lvl.variables: checksum = info.checksum reserved = info.reserved if var.startswith("DIF"): parent = next( ( dr for dr in reversed(level_records) if _corrects(var, dr.variable, lvl.level) ), None, ) if parent is None: unpaired.setdefault((var, lvl.level), position) n_unpaired += 1 else: parent._create_diff( position=position, variable=var, checksum=checksum, reserved=reserved, ) else: if (lvl.level, var) in rs: raise ARLFormatError( f"{self.path}: index record for {index.time} " f"lists '{var}' twice at level {lvl.level} " f"(again at byte {position})." ) # Create data record dr = rs._create_datarecord( position=position, variable=var, level=lvl.level, checksum=checksum, reserved=reserved, ) level_records.append(dr) position += record_length # go to next record # Move file pointer to the start of the next index record fh.seek(position) if not self._steps: raise ARLFormatError( f"{self.path}: holds no index record at byte 0; it may be " "empty, truncated, or not ARL." ) if unpaired: found = [ f"{name} at level {level} (byte {first})" for (name, level), first in list(unpaired.items())[:5] ] if len(unpaired) > 5: found.append(f"and {len(unpaired) - 5} more") warnings.warn( f"{self.path}: {n_unpaired} DIF record(s) have no record before " "them on their level that HYSPLIT's name table pairs them with, " "so they are not applied and a rewrite or subset leaves them out: " f"{', '.join(found)}. DIFW corrects WWND or DZDT, DIFT corrects " "TKEN or TKEU, and at the surface any DIF* corrects TPP* or PRT*.", ARLFormatWarning, stacklevel=3, ) def _same_bytes(self, first: int, second: int, n_bytes: int) -> bool: """ Compare two ``n_bytes`` spans of the file, one record at a time. Reading record by record keeps memory at two records even for large grids (an HRRR time step is ~200 MB). """ fh = self.handle chunk = self.record_length for offset in range(0, n_bytes, chunk): length = min(chunk, n_bytes - offset) fh.seek(first + offset) a = fh.read(length) fh.seek(second + offset) b = fh.read(length) if a != b: return False return True
[docs] def check(self) -> list[str]: """ Return what is wrong with this file that opening it does not catch. Opening a file raises :class:`ARLFormatError` when it is cut short. A file can also be damaged in ways that open cleanly: a time step written partway, a missing time step, or a record overwritten with other bytes. HYSPLIT then stops partway through a run or reads bad values. ``check`` looks for these the way HYSPLIT reads a file, time step by time step in file order (``metset.f``): - every time step has as many data records as the first, since HYSPLIT steps from one index record to the next by that count; - each time step is as far after the one before it on disk as the second is after the first. HYSPLIT stops when the spacing changes, and it finds a time step by its position in the file, so a time step repeated (some NOAA HRRR archive files repeat one byte for byte; opening skips the copy) or out of order makes it stop or read the wrong hours. A missing time step changes the spacing too; - every data record's header can be read, and names the variable, level, and time its index record lists. It reads each record's 50-byte header, not its values, so it does not compare checksums. :meth:`arlmet.DataRecord.verify_checksum` compares one record's packed bytes with its stored checksum. It also lists each record flagged missing (a negative forecast hour, or the name ``NULL``, in its header), which arlmet reads as NaN: HYSPLIT skips such a record, and stops when a time step has more than 20 (``metinp.f``). Returns ------- list of str One message per problem. Empty when none is found. Raises ------ ValueError If the file is open for writing. Examples -------- A time step written partway: >>> with arlmet.File("20210226_18-23_hrrr") as met: # doctest: +SKIP ... met.check() ['2021-02-26 19:00 has 43 of the 298 data records the first time step has.'] """ _require_mode(self, "r") problems = [] # HYSPLIT reads the time steps in file order, so check them so too. recordsets = sorted(self._recordsets.values(), key=lambda rs: rs.position) if recordsets: n_first = len(_with_diffs(recordsets[0])) for rs in recordsets[1:]: n = len(_with_diffs(rs)) if n != n_first: problems.append( f"{_at(rs.time)} has {n} of the {n_first} data records " "the first time step has." ) # Walk every index record on disk, repeats included: HYSPLIT takes the # spacing from the first two and finds a time step by its position. steps = self._steps if len(steps) > 1: step = steps[1][0] - steps[0][0] for (before, _, _), (after, position, _) in pairwise(steps): if after - before == step and step > pd.Timedelta(0): continue if after == before: problems.append( f"{_at(after)} is repeated at byte {position}. arlmet " "skips the copy, but HYSPLIT finds a time step by its " "position, so it stops or reads each later time step as " "the one before it." ) elif after < before: problems.append( f"{_at(after)} (byte {position}) comes after " f"{_at(before)} in the file. HYSPLIT reads the time " "steps in file order, so it stops or reads the wrong hours." ) else: problems.append( f"{_at(before)} to {_at(after)} is " f"{_span(after - before)}; the first two time steps are " f"{_span(step)} apart." ) if step <= pd.Timedelta(0): break # the first two set no spacing to compare with for rs in recordsets: n_missing = 0 for record in _with_diffs(rs): where = ( f"{_at(rs.time)}: the {record.variable} record at level " f"{record.level} (byte {record.position})" ) try: header = record.header except ARLFormatError as exc: problems.append(f"{where} cannot be read. {exc}") continue # A record header holds the hour; the minutes are only in the # index record. if header.time != rs.time.floor("h"): problems.append(f"{where} is for {_at(header.time)}.") if _is_missing(header): n_missing += 1 flag = ( f"forecast hour {header.forecast}" if header.forecast < 0 else f"named {header.variable}" ) problems.append( f"{where} is flagged missing ({flag}). HYSPLIT skips " "it, and arlmet reads it as NaN." ) if n_missing > _MAX_MISSING: problems.append( f"{_at(rs.time)} has {n_missing} records flagged missing; " f"HYSPLIT stops at more than {_MAX_MISSING} in one time step." ) return problems
[docs] def flush(self) -> None: """ Write pending record sets to disk and release their in-memory data. Record sets are held in memory until the file is flushed or closed. When writing many time steps, call ``flush()`` after filling each one so memory stays bounded by one time step instead of the whole file. Flushed record sets cannot be modified. Examples -------- >>> with arlmet.File( # doctest: +SKIP ... "out.arl", mode="w", source="TEST", grid=grid, vertical_axis=vaxis ... ) as arl: ... for time, fields in steps: # fields: {(name, level): array} ... rs = arl.create_recordset(time, forecast=0) ... for (name, level), data in fields.items(): ... rs.create_datarecord(name, level=level, forecast=0, data=data) ... arl.flush() """ _require_mode(self, "w") for rs in self._recordsets.values(): if rs.position == -1 and len(rs) > 0: rs._flush() self.handle.flush()
[docs] def close(self) -> None: """Flush pending writes and close the managed binary file handle.""" try: if self.mode == "w": self.flush() finally: # Close the file manager — this releases the underlying file handle. # Any mmap objects created from it become invalid and are GC'd automatically. self._manager.close() self._handle = None
[docs] def sample_points( self, points: pd.DataFrame | Mapping[str, Any], variables: str | Iterable[str], *, time: pd.Timestamp | str | None = None, z_kind: Literal["native", "pressure", "agl", "msl"] = "pressure", method: Literal["linear", "nearest"] = "linear", earth_relative: bool = False, model_top: float | None = _DEFAULT_MODEL_TOP, ) -> pd.DataFrame: """ Sample fields from this file at arbitrary lon/lat/z points. Equivalent to :func:`arlmet.sample_points` with this file as the only input. Parameters ---------- points : pandas.DataFrame or mapping 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 or iterable of 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 : pandas.Timestamp or str, optional One valid time for every point, used when ``points`` has no ``time`` column. Passing both raises ``ValueError``. When neither is given, the file must contain exactly one time, which is used. A time must be one of the file's valid times (no interpolation in time). z_kind : {"pressure", "native", "agl", "msl"}, default "pressure" Vertical coordinate system of the ``z`` values: - ``"native"``: fractional ARL level index. - ``"pressure"``: hPa, interpolated as ``ln p`` from the ground (``PRSS``, required) up; a point below the ground is NaN, and levels at or below the ground are left out. Sigma files use the ``PRES`` field when they have it, else the levels' pressure from ``PRSS``, as hybrid files (flag 4) do; pressure files (flag 2) use the stored levels; terrain-following files (flag 3) raise ``ValueError``; WRF files (flag 5) use the ``PRES`` field. - ``"agl"``: meters above ground level. Sigma/hybrid/WRF files integrate hypsometrically from the ground (``PRSS``, ``T02M``) through each level's pressure (``PRES`` on WRF files) with the virtual temperature (``TEMP`` with ``SPHU`` or ``RELH``), as HYSPLIT's ``prfsig`` and ``prfecm`` do; pressure files use ``HGTS - SHGT`` from the ground up; terrain-following files scale the stored levels as HYSPLIT's ``prfter`` does (with ``model_top=None``, the stored levels as they are). - ``"msl"``: meters above mean sea level, taken as the height above the ground at the point (less ``SHGT`` there), then as ``"agl"``. Every file needs ``SHGT``. Each vertical coordinate system has exactly one method, as in HYSPLIT; there is no fallback between them. At heights and pressures level 0 is the ground (at native levels too, on sigma, hybrid and WRF files): a point between it and the first level is interpolated from the ground values (``PRSS`` for ``PRES``, ``T02M`` for ``TEMP``, ``U10M``/``V10M`` for winds), and pressure is interpolated as ``ln p``, as HYSPLIT does. Where the first level is below the ground (its ``PRES`` above ``PRSS``) the ground values are passed over at heights, as in HYSPLIT, and that level is left out at pressures. The *Vertical Coordinates* guide has the details. method : {"linear", "nearest"}, default "linear" 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, default False Rotate sampled wind pairs (``UWND``/``VWND``, ``U10M``/``V10M``) from the grid axes to east/north. Winds in ARL files on projected grids are stored grid-relative, as HYSPLIT expects. Both components of a pair must be requested. No effect on lat/lon grids. model_top : float or None, default 25000 The HYSPLIT run's top of model domain [m] (the ``CONTROL`` file's, PYSTILT's ``z_top``). At heights the values are the ones a HYSPLIT run with that top uses, on its internal levels; the default is PYSTILT's default top. ``None`` reads each grid point's column at the point's own height instead, which is not HYSPLIT's value. See :func:`arlmet.sample_points`. Returns ------- pandas.DataFrame 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. Raises ------ ValueError If a required column is missing, ``time`` is given alongside a ``time`` column, no time is given for a multi-time file, a point time is not in the 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 >>> pts = pd.DataFrame({"lon": [-111.9], "lat": [40.7], "z": [850.0]}) >>> with arlmet.File("met.arl") as met: ... out = met.sample_points(pts, ["UWND", "VWND"], time="2024-07-18 00:00") >>> out.round(1) lon lat z UWND VWND 0 -111.9 40.7 850.0 6.2 -3.8 """ # Delayed import: ops sit on top of file, so file's use of the sampling # op is lazy to avoid a file <-> ops import cycle (see File.extract_subset). from arlmet._ops.sample import sample_points return sample_points( self, points, variables, time=time, z_kind=z_kind, method=method, earth_relative=earth_relative, model_top=model_top, )
[docs] def to_dataset( self, *, drop_variables: Sequence[str] | None = None, bbox: tuple[float, float, float, float] | None = None, bbox_crs: Any = None, levels: Iterable[int] | None = None, ) -> xr.Dataset: """ Project this file into the simplified analysis Dataset representation. Parameters ---------- drop_variables : sequence of str, optional Variable names to omit. bbox : tuple[float, float, float, float], optional 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 (:meth:`arlmet.Grid.window_from_bbox`). bbox_crs : pyproj.CRS or str, optional The coordinate reference system ``bbox`` is in (``"EPSG:32612"``, anything :meth:`pyproj.CRS.from_user_input` takes), making the box ``(xmin, ymin, xmax, ymax)`` in its units; the default is degrees (:meth:`arlmet.Grid.window_from_bbox`). levels : iterable of int, optional ARL level indices to keep. Returns ------- xarray.Dataset See :func:`arlmet.open_dataset` for the layout. """ from arlmet._xarray.dataset import _build_dataset_from_file return _build_dataset_from_file( self, drop_variables=drop_variables, bbox=bbox, bbox_crs=bbox_crs, levels=levels, )
[docs] def extract_subset( self, dest: str | os.PathLike[str], *, bbox: tuple[float, float, float, float] | None = None, bbox_crs: Any = None, levels: Iterable[int] | None = None, variables: str | Iterable[str] | None = None, overwrite: bool = True, ) -> Path: """ Write a spatial/vertical subset of this file to a new ARL file. Equivalent to :func:`arlmet.extract_subset` with this file's path as the input. Parameters ---------- dest : path-like Output ARL file. Must not be this file. bbox : tuple[float, float, float, float], optional 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 (:meth:`arlmet.Grid.window_from_bbox`). bbox_crs : pyproj.CRS or str, optional The coordinate reference system ``bbox`` is in (``"EPSG:32612"``, anything :meth:`pyproj.CRS.from_user_input` takes), making the box ``(xmin, ymin, xmax, ymax)`` in its units; the default is degrees (:meth:`arlmet.Grid.window_from_bbox`). levels : iterable of int, optional ARL level indices to keep. Output levels are compacted and renumbered from zero while preserving the selected level heights. variables : str or iterable of str, optional Variable names to keep, as the file spells them; a single string is one name. All variables are included by default. overwrite : bool, default True Replace an existing ``dest``; with False an existing ``dest`` is returned as it is, without reading this file. Returns ------- pathlib.Path The output path, ``Path(dest)``. Examples -------- >>> import arlmet >>> with arlmet.File("met.arl") as met: ... out = met.extract_subset("subset.arl", bbox=(-114, 39, -110, 42)) >>> ds = arlmet.open_dataset(out) """ from arlmet._ops.subset import extract_subset return extract_subset( self.path, dest, bbox=bbox, bbox_crs=bbox_crs, levels=levels, variables=variables, overwrite=overwrite, )
def __getitem__(self, key: str | int | pd.Timestamp) -> RecordSet: if isinstance(key, str): # Allow lookup by string time representation key = ensure_timestamp(key) elif isinstance(key, int): # Positional lookup follows the sorted `times`, not file order key = self.times[key] return self._recordsets[key] def __iter__(self) -> Iterator[pd.Timestamp]: return iter(self.times) def __len__(self) -> int: return len(self._recordsets) def __contains__(self, key: object) -> bool: try: ts = ensure_timestamp(key) except (TypeError, ValueError, OverflowError): # Not interpretable as a timestamp return False return ts in self._recordsets @override def __repr__(self) -> str: grid_str = ( f"{self._grid.nx}\u00d7{self._grid.ny}" if self._grid is not None else "None" ) levels_str = str(len(self._vaxis.levels)) if self._vaxis is not None else "None" return ( f"File({self.path.name!r}, mode={self.mode!r}, " f"times={len(self)}, grid={grid_str}, levels={levels_str})" ) def __enter__(self) -> Self: return self def __exit__( self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> None: self.close()