Source code for climpred.tutorial

"""Implement climpred.tutorial.load_dataset to load analysis ready datasets."""

import hashlib
import os as _os
import urllib
from typing import Dict, Optional
from urllib.request import urlretrieve as _urlretrieve

import xarray as xr
from xarray.backends.api import open_dataset as _open_dataset

_default_cache_dir: str = _os.sep.join(("~", ".climpred_data"))

aliases = [
true_file_names = [
file_descriptions = [
    "area averages for the MPI control run of SST/SSS.",
    "lat/lon/time for the MPI control run of SST/SSS.",
    "perfect model decadal prediction ensemble area averages of SST/SSS/AMO.",
    "perfect model decadal prediction ensemble lat/lon/time of SST/SSS/AMO.",
    "hindcast decadal prediction ensemble of global mean SSTs.",
    "hindcast decadal prediction ensemble of global mean SSS.",
    "hindcast decadal prediction ensemble of eastern Pacific SSTs.",
    "uninitialized ensemble of global mean SSTs.",
    "hindcast initialized ensemble of global mean SSTs",
    "uninitialized ensemble of global mean SSTs",
    "assimilation in MPI-ESM of global mean SSTs",
    "observations of global mean SSTs.",
    "reconstruction of global mean SSTs.",
    "reconstruction of global mean SSS.",
    "reconstruction of eastern Pacific SSTs",
    "daily RMM1 from the GMAO-GEOS-V2p1 model for SubX",
    "observed RMM with interannual variablity included",
    "S2S ECMWF on-the-fly hindcasts from the S2S Project for Germany",
    "CPC/ERA5 observations for S2S forecasts over Germany",
    "monthly multi-member hindcasts of sea-surface temperature averaged over the Nino3.4 region from the NMME project from IRIDL",  # noqa: E501
    "monthly Reyn_SmithOIv2 sea-surface temperature observations averaged over the Nino3.4 region",  # noqa: E501

FILE_ALIAS_DICT = dict(zip(aliases, true_file_names))
FILE_DESCRIPTIONS = dict(zip(aliases, file_descriptions))

def _file_md5_checksum(fname):
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
    return hash_md5.hexdigest()

def _get_datasets():
    """Print out available datasets for the user to load if no args are given."""
    for key in FILE_DESCRIPTIONS.keys():
        print(f"'{key}': {FILE_DESCRIPTIONS[key]}")

def _cache_all():
    """Cache all datasets for pytest -n 4 woth pytest-xdist."""
    for d in aliases:

def _initialize_proxy(proxy_dict):
    """Open a proxy for firewalled servers so that the downloads can go through.

        proxy_dict (dictionary): Keys are either 'http' or 'https' and
            values are the proxy server.

    proxy = urllib.request.ProxyHandler(proxy_dict)
    opener = urllib.request.build_opener(proxy)

[docs]def load_dataset( name: Optional[str] = None, cache: bool = True, cache_dir: str = _default_cache_dir, github_url: str = "", branch: str = "master", extension: Optional[str] = None, proxy_dict: Optional[Dict[str, str]] = None, **kws, ) -> xr.Dataset: """Load example data or a mask from an online repository. Args: name: Name of the netcdf file containing the dataset, without the ``.nc`` extension. If ``None``, this function prints out the available datasets to import. cache: If ``True``, cache data locally for use on later calls. cache_dir: The directory in which to search for and cache the data. github_url: Github repository where the data is stored. branch: The git branch to download from. extension: Subfolder within the repository where the data is stored. proxy_dict: Dictionary with keys as either "http" or "https" and values as the proxy server. This is useful if you are on a work computer behind a firewall and need to use a proxy out to download data. kws: Keywords passed to :py:meth:`~xarray.open_dataset`. Returns: The desired :py:class:`xarray.Dataset` Examples: >>> from climpred.tutorial import load_dataset >>> proxy_dict = {"http": ""} >>> ds = load_dataset("FOSI-SST", cache=False, proxy_dict=proxy_dict) """ if name is None: return _get_datasets() if proxy_dict is not None: _initialize_proxy(proxy_dict) # # filename-in-python # Allows for generalized file extensions. name, ext = _os.path.splitext(name) if not ext.endswith(".nc"): ext += ".nc" # use aliases if name in FILE_ALIAS_DICT.keys(): name = FILE_ALIAS_DICT[name] longdir = _os.path.expanduser(cache_dir) fullname = name + ext localfile = _os.sep.join((longdir, fullname)) md5name = name + ".md5" md5file = _os.sep.join((longdir, md5name)) if not _os.path.exists(localfile): # This will always leave this directory on disk. # May want to add an option to remove it. if not _os.path.isdir(longdir): _os.mkdir(longdir) if extension is not None: url = "/".join((github_url, "raw", branch, extension, fullname)) _urlretrieve(url, localfile) url = "/".join((github_url, "raw", branch, extension, md5name)) _urlretrieve(url, md5file) else: url = "/".join((github_url, "raw", branch, fullname)) _urlretrieve(url, localfile) url = "/".join((github_url, "raw", branch, md5name)) _urlretrieve(url, md5file) localmd5 = _file_md5_checksum(localfile) with open(md5file, "r") as f: remotemd5 = if localmd5 != remotemd5: _os.remove(localfile) msg = """ Try downloading the file again. There was a confliction between your local .md5 file compared to the one in the remote repository, so the local copy has been removed to resolve the issue. """ raise IOError(msg) ds = _open_dataset(localfile, **kws) if not cache: ds = ds.load() _os.remove(localfile) return ds