• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hydrologie / xdatasets / 19147903377

06 Nov 2025 07:47PM UTC coverage: 20.335% (-0.2%) from 20.507%
19147903377

push

github

web-flow
Bump the actions group in /.github/workflows with 6 updates + cruft update (#258)

0 of 16 new or added lines in 7 files covered. (0.0%)

7 existing lines in 6 files now uncovered.

97 of 477 relevant lines covered (20.34%)

1.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.92
/src/xdatasets/tutorial.py
1
import uuid
6✔
2
from functools import reduce
6✔
3
from html import escape
6✔
4

5
from IPython.core.display import HTML
6✔
6
from xarray.core.formatting_html import _icon, _mapping_section, _obj_repr
6✔
7

8

9
catalog_path = "https://raw.githubusercontent.com/hydrocloudservices/catalogs/main/catalogs/main.yaml"
6✔
10

11

12
def open_dataset(
6✔
13
    name: str,
14
    **kws,  # noqa: F841
15
):
16
    r"""
17
    Open a dataset from the online public repository (requires internet).
18

19
    Available datasets:
20
    * ``"era5_reanalysis_single_levels"``: ERA5 reanalysis subset (t2m and tp)
21
    * ``"cehq"``: CEHQ flow and water levels observations
22

23
    Parameters
24
    ----------
25
    name : str
26
        Name of the file containing the dataset.
27
        e.g. 'era5_reanalysis_single_levels'.
28
    \*\*kws : dict, optional
29
        Currently not used.
30

31
    See Also
32
    --------
33
    xarray.open_dataset
34
    """
35
    try:
×
36
        import intake
×
37
    except ImportError as e:
×
38
        raise ImportError(
×
39
            "tutorial.open_dataset depends on intake and intake-xarray to download and manage datasets."
40
            " To proceed please install intake and intake-xarray.",
41
        ) from e
42

43
    cat = intake.open_catalog(catalog_path)
×
44
    dataset_info = [
×
45
        (category, dataset_name) for category in cat._entries.keys() for dataset_name in cat[category]._entries.keys() if dataset_name == name
46
    ]
47

48
    data = reduce(lambda array, index: array[index], dataset_info, cat)
×
49

50
    if data.describe()["driver"][0] == "geopandasfile":
×
51
        data = data.read()
×
52
    elif data.describe()["driver"][0] == "zarr":
×
53
        data = data.to_dask()
×
54
    else:
55
        raise NotImplementedError(
×
56
            f"Dataset {name} is not available. Please request further datasets to our github issues pages",
57
        )
58
    return data
×
59

60

61
def summarize_coords(variables):
6✔
62
    li_items = []
×
63
    for k in variables:
×
64
        li_content = summarize_variable(k, is_index=False)
×
65
        li_items.append(f"<li class='xr-var-item'>{li_content}</li>")
×
66

67
    vars_li = "".join(li_items)
×
68

69
    return f"<ul class='xr-var-list'>{vars_li}</ul>"
×
70

71

72
def summarize_variable(name, is_index=False, dtype=None):  # noqa: F841
6✔
73
    cssclass_idx = " class='xr-has-index'" if is_index else ""
×
74
    name = escape(str(name))
×
75

76
    # "unique" ids required to expand/collapse subsections
77
    attrs_id = "attrs-" + str(uuid.uuid4())
×
78
    data_id = "data-" + str(uuid.uuid4())
×
79
    attrs_icon = _icon("icon-file-text2")
×
80
    data_icon = _icon("icon-database")
×
81

82
    return (
×
83
        f"<div class='xr-var-preview'><span{cssclass_idx}>{name}</span></div>"
84
        f"<input id='{attrs_id}' class='xr-var-attrs-in' "
85
        f"type='checkbox'>"
86
        f"<label for='{attrs_id}' title='Show/Hide attributes'>"
87
        f"{attrs_icon}</label>"
88
        f"<input id='{data_id}' class='xr-var-data-in' type='checkbox'>"
89
        f"<label for='{data_id}' title='Show/Hide data repr'>"
90
        f"{data_icon}</label>"
91
    )
92

93

94
def list_available_datasets():
6✔
95
    """
96
    Open, load lazily, and close a dataset from the public online repository (requires internet).
97

98
    See Also
99
    --------
100
    open_dataset
101
    """
102
    try:
×
103
        import intake
×
104
    except ImportError as e:
×
105
        raise ImportError(
×
106
            "tutorial.open_dataset depends on intake and intake-xarray to download and manage datasets."
107
            " To proceed please install intake and intake-xarray.",
108
        ) from e
109

110
    cat = intake.open_catalog(catalog_path)
×
111

112
    # This will need refactor if the catalog has more than 2 levels
113
    # list(itertools.chain.from_iterable([list(cat[name].keys()) for name in cat._entries.keys()]))
114

NEW
115
    datasets_catalog = {field: list(sorted(cat[field]._entries.keys())) for field in sorted(cat._entries.keys())}
×
116

117
    def add_section(datasets_catalog):
×
118
        return [
×
119
            _mapping_section(
120
                datasets,
121
                name=field.capitalize(),
122
                details_func=summarize_coords,
123
                max_items_collapse=25,
124
                expand_option_name="display_expand_coords",
125
            )
126
            for field, datasets in datasets_catalog.items()
127
        ]
128

129
    a = _obj_repr(
×
130
        "",
131
        [f"<div class='xr-obj-type'>{escape('xdatasets.Catalog')}</div>"],
132
        add_section(datasets_catalog),
133
    )
134

135
    return HTML(a)
×
136

137

138
def load_dataset(*args, **kwargs):
6✔
139
    r"""
140
    Open, load lazily, and close a dataset from the online repository (requires internet).
141

142
    Parameters
143
    ----------
144
    \*args : sequence
145
        A sequence of positional arguments passed to `open_dataset`.
146
    \*\*kwargs : dict
147
        A dictionary of keyword arguments passed to `open_dataset`.
148

149
    See Also
150
    --------
151
    open_dataset
152
    """
153
    return open_dataset(*args, **kwargs)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc