ewatercycle.util ================ .. py:module:: ewatercycle.util .. autoapi-nested-parse:: Utility functions for the eWaterCycle package. Module Contents --------------- .. py:function:: find_closest_point(grid_longitudes: collections.abc.Iterable[float], grid_latitudes: collections.abc.Iterable[float], point_longitude: float, point_latitude: float) -> tuple[int, int] Find the closest grid cell to a point based on Geographical distances. :param grid_longitudes: 1d array of model grid longitudes in degrees :param grid_latitudes: 1d array of model grid latitudes in degrees :param point_longitude: longitude in degrees of target coordinate :param point_latitude: latitude in degrees of target coordinate :returns: Tuple with first index of closest grid point in the original longitude array and second the index of closest grid point in the original latitude array. .. py:function:: geographical_distances(point_longitude: float, point_latitude: float, lon_vectors: numpy.ndarray, lat_vectors: numpy.ndarray, radius=6373.0) -> numpy.ndarray Calculate geographical distances. It uses Spherical Earth projected to a plane formula: https://en.wikipedia.org/wiki/Geographical_distance :param point_longitude: longitude in degrees of target coordinate :param point_latitude: latitude in degrees of target coordinate :param lon_vectors: 1d array of longitudes in degrees :param lat_vectors: 1d array of latitudes in degrees :param radius: Radius of a sphere in km. Default is Earths approximate radius. :returns: Array of geographical distance of point to all vector members. .. py:function:: get_time(time_iso: str) -> datetime.datetime Return a datetime in UTC. Convert a date string in ISO format to a datetime and check if it is in UTC. .. py:function:: get_extents(shapefile: Any, pad=0) -> dict[str, float] Get lat/lon extents from shapefile and add padding. :param shapefile: Path to shapfile :param pad: Optional padding :returns: Dict with `start_longitude`, `start_latitude`, `end_longitude`, `end_latitude` .. py:function:: fit_extents_to_grid(extents, step=0.1, offset=0.05, ndigits=2) -> dict[str, float] Get lat/lon extents fitted to a grid. :param extents: Dict with `start_longitude`, `start_latitude`, `end_longitude`, `end_latitude` :param step: Distance between to grid cells :param offset: Offset to pad with after rounding extent to step. :param ndigits: Number of digits to return :returns: Dict with `start_longitude`, `start_latitude`, `end_longitude`, `end_latitude` .. py:function:: merge_esvmaltool_datasets(datasets: list[xarray.Dataset]) -> xarray.Dataset Merge the separate output datasets from an ESMValTool recipe into one dataset. ESMValTool has bad management of floating point precision in coordinates. Every CMORized file can have different rounding errors in the values of its coordinates. This will prevent easy merging with xarray's open_mfdataset, or combine_by_coords. By rounding to the 7th decimal place, more than sufficient precision is preserved ('waldo-on-a-page' precision)[1], while solving the floating point inprecision issue. .. rubric:: References [1] Randall Monroe, 2019. xkcd: Coordinate Precision. https://xkcd.com/2170/ .. py:function:: to_absolute_path(input_path: str | pathlib.Path, parent: pathlib.Path | None = None, must_exist: bool = False, must_be_in_parent=True) -> pathlib.Path Parse input string as :py:class:`pathlib.Path` object. :param input_path: Input string path that can be a relative or absolute path. :param parent: Optional parent path of the input path :param must_exist: Optional argument to check if the input path exists. :param must_be_in_parent: Optional argument to check if the input path is subpath of parent path :returns: The input path that is an absolute path and a :py:class:`pathlib.Path` object. .. py:function:: reindex(source_file: str, var_name: str, mask_file: str, target_file: str) Conform the input file onto the indexes of a mask file. Writing the results to the target file. :param source_file: Input string path of the file that needs to be reindexed. :param var_name: Variable name in the source_file dataset. :param mask_file: Input string path of the mask file. :param target_file: Output string path of the file that is reindexed. .. py:class:: CaseConfigParser(defaults=None, dict_type=_default_dict, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=DEFAULTSECT, interpolation=_UNSET, converters=_UNSET) Bases: :py:obj:`configparser.ConfigParser` Case sensitive config parser. See https://stackoverflow.com/questions/1611799/preserve-case-in-configparser .. py:method:: optionxform(optionstr) Do not convert option names to lowercase. .. py:function:: extract_package_name(value: str) -> str Extract package name from entry point value. E.g. "ewatercycle_HBV.model:HBV" will return "ewatercycle_HBV". .. py:function:: get_package_versions() -> dict[str, str] Get the version numbers of the ewatercycle package and its plugins. .. py:function:: plot_catchment(shapefile: str | pathlib.Path, axis: None = None, lat_bounds: tuple[float, float] | None = None, lon_bounds: tuple[float, float] | None = None, figsize: tuple[float, float] | None = None, color: str | None = None) -> tuple[matplotlib.figure.Figure, cartopy.mpl.geoaxes.GeoAxes] plot_catchment(shapefile: str | pathlib.Path, axis: cartopy.mpl.geoaxes.GeoAxes, lat_bounds: tuple[float, float] | None = None, lon_bounds: tuple[float, float] | None = None, figsize: tuple[float, float] | None = None, color: str | None = None) -> None Plot a catchment shapefile. :param shapefile: Path to the shapefile. First geometry will be displayed. :param lat_bounds: The latitude bounds of the plot. Defaults to None, which will make the bounds be infered automatically. :param lon_bounds: The longitude bounds of the plot. Defaults to None, which will make the bounds be infered automatically. :param axis: Existing GeoAxes object to plot the shapefile's geometry in. If not provided, a new plot with the Plate Carree projection, coastlines, rivers and oceans will be generated. :param figsize: Desired size of the figure (if axis is not provided). Defaults to None. :param color: Face color of the catchment. Defaults to None, which will fall back to the Cartopy default color. Can be a name or hexadecimal color code (starting with #). :returns: The generated Figure and GeoAxes objects, or None in case the axis argument was provided.