ewatercycle.util

Module Contents

ewatercycle.util.find_closest_point(grid_longitudes: Iterable[float], grid_latitudes: Iterable[float], point_longitude: float, point_latitude: float) Tuple[int, int]

Find closest grid cell to a point based on Geographical distances.

Parameters:
  • grid_longitudes – 1d array of model grid longitudes in degrees

  • grid_latitudes – 1d array of model grid latitudes in degrees

  • point_longitude – longitude in degrees of target coordinate

  • point_latitude – latitude in degrees of target coordinate

Returns:

index of closest grid point in the original longitude array idx_lat: index of closest grid point in the original latitude array

Return type:

idx_lon

ewatercycle.util.geographical_distances(point_longitude: float, point_latitude: float, lon_vectors: numpy.ndarray, lat_vectors: numpy.ndarray, radius=6373.0) numpy.ndarray

It uses Spherical Earth projected to a plane formula: https://en.wikipedia.org/wiki/Geographical_distance

Parameters:
  • point_longitude – longitude in degrees of target coordinate

  • point_latitude – latitude in degrees of target coordinate

  • lon_vectors – 1d array of longitudes in degrees

  • lat_vectors – 1d array of latitudes in degrees

  • radius – Radius of a sphere in km. Default is Earths approximate radius.

Returns:

array of geographical distance of point to all vector members

Return type:

distances

ewatercycle.util.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.

ewatercycle.util.get_extents(shapefile: Any, pad=0) Dict[str, float]

Get lat/lon extents from shapefile and add padding.

Parameters:
  • shapefile – Path to shapfile

  • pad – Optional padding

Returns:

Dict with start_longitude, start_latitude, end_longitude, end_latitude

ewatercycle.util.fit_extents_to_grid(extents, step=0.1, offset=0.05, ndigits=2) Dict[str, float]

Get lat/lon extents fitted to a grid.

Parameters:
  • extents – Dict with start_longitude, start_latitude, end_longitude, end_latitude

  • step – Distance between to grid cells

  • offset – Offset to pad with after rounding extent to step.

  • ndigits – Number of digits to return

Returns:

Dict with start_longitude, start_latitude, end_longitude, end_latitude

ewatercycle.util.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.

References

[1] Randall Monroe, 2019. xkcd: Coordinate Precision. https://xkcd.com/2170/

ewatercycle.util.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 pathlib.Path object.

Parameters:
  • input_path – Input string path that can be a relative or absolute path.

  • parent – Optional parent path of the input path

  • must_exist – Optional argument to check if the input path exists.

  • 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 pathlib.Path object.

ewatercycle.util.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.

Parameters:
  • source_file – Input string path of the file that needs to be reindexed.

  • var_name – Variable name in the source_file dataset.

  • mask_file – Input string path of the mask file.

  • target_file – Output string path of the

  • reindexed. (file that is) –

class ewatercycle.util.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: configparser.ConfigParser

Case sensitive config parser See https://stackoverflow.com/questions/1611799/preserve-case-in-configparser

optionxform(optionstr)