xclim.testing package

Helpers for testing xclim.

Submodules

xclim.testing.utils module

Testing and tutorial utilities’ module.

xclim.testing.utils.get_file(name: Union[str, PathLike, Sequence[str | os.PathLike]], github_url: str = 'https://github.com/Ouranosinc/xclim-testdata', branch: str = 'master', cache_dir: Path = PosixPath('/home/docs/.xclim_testing_data')) pathlib.Path | list[pathlib.Path][source]

Return a file from an online GitHub-like repository.

If a local copy is found then always use that to avoid network traffic.

Parameters
  • name (str | os.PathLike | Sequence[str | os.PathLike]) – Name of the file or list/tuple of names of files containing the dataset(s) including suffixes.

  • github_url (str) – URL to GitHub repository where the data is stored.

  • branch (str, optional) – For GitHub-hosted files, the branch to download from.

  • cache_dir (Path) – The directory in which to search for and write cached data.

Returns

Path | list[Path]

xclim.testing.utils.get_local_testdata(patterns: Union[str, Sequence[str]], temp_folder: str | os.PathLike, branch: str = 'master', _local_cache: str | os.PathLike = PosixPath('/home/docs/.xclim_testing_data')) pathlib.Path | list[pathlib.Path][source]

Copy specific testdata from a default cache to a temporary folder.

Return files matching pattern in the default cache dir and move to a local temp folder.

Parameters
  • patterns (str | Sequence[str]) – Glob patterns, which must include the folder.

  • temp_folder (str | os.PathLike) – Target folder to copy files and filetree to.

  • branch (str) – For GitHub-hosted files, the branch to download from.

  • _local_cache (str | os.PathLike) – Local cache of testing data.

Returns

Path | list[Path]

xclim.testing.utils.list_datasets(github_repo='Ouranosinc/xclim-testdata', branch='main')[source]

Return a DataFrame listing all xclim test datasets available on the GitHub repo for the given branch.

The result includes the filepath, as passed to open_dataset, the file size (in KB) and the html url to the file. This uses an unauthenticated call to GitHub’s REST API, so it is limited to 60 requests per hour (per IP). A single call of this function triggers one request per subdirectory, so use with parsimony.

xclim.testing.utils.list_input_variables(submodules: Optional[Sequence[str]] = None, realms: Optional[Sequence[str]] = None) dict[source]

List all possible variables names used in xclim’s indicators.

Made for development purposes. Parses all indicator parameters with the xclim.core.utils.InputKind.VARIABLE or OPTIONAL_VARIABLE kinds.

Parameters
  • realms (Sequence of str, optional) – Restrict the output to indicators of a list of realms only. Default None, which parses all indicators.

  • submodules (str, optional) – Restrict the output to indicators of a list of submodules only. Default None, which parses all indicators.

Returns

dict – A mapping from variable name to indicator class.

xclim.testing.utils.open_dataset(name: str | os.PathLike, suffix: Optional[str] = None, dap_url: Optional[str] = None, github_url: str = 'https://github.com/Ouranosinc/xclim-testdata', branch: str = 'main', cache: bool = True, cache_dir: Path = PosixPath('/home/docs/.xclim_testing_data'), **kwargs) Dataset[source]

Open a dataset from the online GitHub-like repository.

If a local copy is found then always use that to avoid network traffic.

Parameters
  • name (str or os.PathLike) – Name of the file containing the dataset.

  • suffix (str, optional) – If no suffix is given, assumed to be netCDF (‘.nc’ is appended). For no suffix, set “”.

  • dap_url (str, optional) – URL to OPeNDAP folder where the data is stored. If supplied, supersedes github_url.

  • github_url (str) – URL to GitHub repository where the data is stored.

  • branch (str, optional) – For GitHub-hosted files, the branch to download from.

  • cache_dir (Path) – The directory in which to search for and write cached data.

  • cache (bool) – If True, then cache data locally for use on subsequent calls.

  • kwargs – For NetCDF files, keywords passed to xarray.open_dataset().

Returns

Union[Dataset, Path]

See also

xarray.open_dataset

xclim.testing.utils.publish_release_notes(style: str = 'md', file: Optional[Union[PathLike, StringIO, TextIO]] = None) str | None[source]

Format release history in Markdown or ReStructuredText.

Parameters
  • style ({“rst”, “md”}) – Use ReStructuredText formatting or Markdown. Default: Markdown.

  • file ({os.PathLike, StringIO, TextIO}, optional) – If provided, prints to the given file-like object. Otherwise, returns a string.

Returns

str, optional

Notes

This function is solely for development purposes.

xclim.testing.utils.show_versions(file: Optional[Union[PathLike, StringIO, TextIO]] = None, deps: Optional[list] = None) str | None[source]

Print the versions of xclim and its dependencies.

Parameters
  • file ({os.PathLike, StringIO, TextIO}, optional) – If provided, prints to the given file-like object. Otherwise, returns a string.

  • deps (list, optional) – A list of dependencies to gather and print version information from. Otherwise, prints xclim dependencies.

Returns

str or None