Skip to content

Commit 555eef3

Browse files
authored
FIX: Improve test coverage and runtime (#24)
1 parent 5ea9997 commit 555eef3

3 files changed

Lines changed: 65 additions & 96 deletions

File tree

intake_esgf/base.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -182,66 +182,9 @@ def parallel_download(
182182
return None, None
183183

184184

185-
def get_relative_esgf_path(entry: dict[str, Any]) -> Path:
186-
"""Return the relative ESGF path from the Globus entry."""
187-
if "content" not in entry:
188-
raise ValueError("'content' not part of the entry.")
189-
content = entry["content"]
190-
if set(["version", "dataset_id", "directory_format_template_"]).difference(
191-
content.keys()
192-
):
193-
raise ValueError("Entry content does not contain expected keys.")
194-
# For some reason, the `version` in the globus response is just an integer and not
195-
# what is used in the file path so I have to parse it out of the `dataset_id`
196-
content["version"] = [content["dataset_id"].split("|")[0].split(".")[-1]]
197-
# Format the file path using the template in the response
198-
file_path = content["directory_format_template_"][0]
199-
file_path = Path(
200-
file_path.replace("%(root)s/", "")
201-
.replace("%(", "{")
202-
.replace(")s", "[0]}")
203-
.format(**content)
204-
)
205-
return file_path
206-
207-
208-
def combine_file_info(indices, dataset_ids: list[str]) -> dict[str, Any]:
209-
"""Combine file information for the given datasets from all indices.
210-
211-
Parameters
212-
----------
213-
indices
214-
A list of index classes, see `intake_esgf.core`.
215-
dataset_ids
216-
The dataset_ids for which we are seeking file information.
217-
"""
218-
merged_info = {}
219-
for ind in indices:
220-
try:
221-
infos = ind.get_file_info(dataset_ids)
222-
except requests.exceptions.RequestException:
223-
continue
224-
# loop thru all the infos and uniquely add by path
225-
for info in infos:
226-
path = info["path"]
227-
if path not in merged_info:
228-
merged_info[path] = {}
229-
for key, val in info.items():
230-
if isinstance(val, list):
231-
if key not in merged_info[path]:
232-
merged_info[path][key] = val
233-
else:
234-
merged_info[path][key] += val
235-
else:
236-
if key not in merged_info[path]:
237-
merged_info[path][key] = val
238-
return [info for key, info in merged_info.items()]
239-
240-
241185
def check_for_esgf_dataroot() -> Union[Path, None]:
242186
"""Return a direct path to the ESGF data is it exists."""
243187
to_check = [
244-
"/gpfs/alpine/cli137/proj-shared/ESGF/esg_dataroot/css03_data/", # OLCF
245188
"/p/css03/esgf_publish", # Nimbus
246189
"/eagle/projects/ESGF2/esg_dataroot", # ALCF
247190
"/global/cfs/projectdirs/m3522/cmip6/", # NERSC data lake

intake_esgf/tests/test_basic.py

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
11
from intake_esgf import ESGFCatalog
2+
from intake_esgf.exceptions import NoSearchResults
23

34

45
def test_search():
5-
cat = ESGFCatalog().search(
6-
activity_id="CMIP",
6+
cat = ESGFCatalog(esgf1_indices="esgf-node.llnl.gov")
7+
print(cat)
8+
cat = ESGFCatalog(esgf1_indices=["esgf-node.llnl.gov"]).search(
79
experiment_id="historical",
8-
source_id="CESM2",
9-
variable_id=["gpp", "areacella", "sftlf"],
10+
source_id="CanESM5",
11+
variable_id=["gpp"],
1012
variant_label=["r1i1p1f1"],
1113
)
12-
assert len(cat.df) == 3
14+
print(cat)
15+
ds = cat.to_dataset_dict()
16+
assert "gpp" in ds
17+
assert "sftlf" in ds["gpp"]
1318

1419

15-
def test_global_search():
16-
cat = ESGFCatalog(esgf1_indices=True).search(
17-
activity_id="CMIP",
20+
def test_esgroot():
21+
cat = ESGFCatalog()
22+
cat.set_esgf_data_root(cat.local_cache)
23+
cat.search(
1824
experiment_id="historical",
19-
source_id="CESM2",
20-
variable_id=["gpp", "areacella", "sftlf"],
25+
source_id="CanESM5",
26+
variable_id=["gpp"],
2127
variant_label=["r1i1p1f1"],
2228
)
23-
assert len(cat.df) == 3
29+
ds = cat.to_dataset_dict(add_measures=False)
30+
assert "gpp" in ds
31+
log = cat.session_log()
32+
assert "download" not in log
33+
assert f"accessed {cat.esgf_data_root}" in cat.session_log()
34+
35+
36+
def test_noresults():
37+
cat = ESGFCatalog()
38+
try:
39+
cat.search(variable_id="does_not_exist")
40+
except NoSearchResults:
41+
pass
2442

2543

2644
def test_tracking_ids():
@@ -32,24 +50,49 @@ def test_tracking_ids():
3250
[
3351
"hdl:21.14100/0577d84f-9954-494f-8cc8-465aa4fd910e",
3452
"hdl:21.14100/0972f78b-158e-4c6b-bcdf-7d0d75d7a8cd",
35-
"hdl:21.14100/0e4dfb8f-b677-456e-abc7-71e1ebc16deb",
36-
"hdl:21.14100/17b6c62f-455b-49bc-8674-564f7ca5ed6a",
37-
"hdl:21.14100/1bd030c9-1761-4fca-911e-6ea2b6407bc7",
38-
"hdl:21.14100/2844ea5a-4589-4ed4-bbb7-c13e9964a4b7",
3953
]
4054
)
41-
assert len(cat.df) == 7
55+
assert len(cat.df) == 2
4256

4357

4458
def test_add_cell_measures():
4559
# these measures are in r1i1p1f2 / piControl
4660
cat = ESGFCatalog().search(
47-
variable_id="gpp",
61+
variable_id="mrros",
4862
source_id="UKESM1-0-LL",
4963
variant_label="r2i1p1f2",
5064
frequency="mon",
5165
experiment_id="historical",
5266
)
53-
ds = cat.to_dataset_dict()["gpp"]
67+
ds = cat.to_dataset_dict()["mrros"]
5468
assert "sftlf" in ds
5569
assert "areacella" in ds
70+
71+
72+
def test_modelgroups():
73+
cat = ESGFCatalog().search(
74+
experiment_id="historical",
75+
source_id=["CanESM5", "GFDL-CM4"],
76+
variable_id=["tas", "pr"],
77+
variant_label=["r1i1p1f1", "r2i1p1f1"],
78+
table_id="day",
79+
)
80+
assert len(cat.model_groups()) == 4
81+
82+
83+
def test_remove_ensemble():
84+
cat = ESGFCatalog().search(
85+
experiment_id="historical",
86+
source_id=["UKESM1-0-LL"],
87+
variable_id=["tas"],
88+
table_id="Amon",
89+
)
90+
assert len(cat.model_groups()) > 1
91+
cat.remove_ensembles()
92+
assert len(cat.df) == 1
93+
assert cat.df.iloc[0]["member_id"] == "r1i1p1f2"
94+
95+
96+
def test_download_dbase():
97+
cat = ESGFCatalog()
98+
assert len(cat.download_summary().columns)

intake_esgf/tests/test_operators.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,17 @@ def test_global_mean():
1515
cat = ESGFCatalog().search(
1616
experiment_id=["historical"],
1717
source_id="CanESM5",
18-
activity_id="CMIP",
1918
variant_label="r1i1p1f1",
20-
variable_id=["nbp", "tas", "fgco2"],
19+
variable_id=["gpp", "fgco2"],
2120
frequency="mon",
2221
)
2322
dsd = cat.to_dataset_dict(ignore_facets=["table_id"])
2423
dsd = trim_time(dsd)
2524
dsd = ops.global_mean(dsd)
26-
assert set(["fgco2", "tas", "nbp"]) == set(dsd.keys())
25+
assert set(["fgco2", "gpp"]) == set(dsd.keys())
2726

2827

2928
def test_ensemble_mean():
30-
cat = ESGFCatalog().search(
31-
experiment_id="historical",
32-
source_id=["CESM2", "CanESM5"],
33-
variant_label=["r1i1p1f1", "r2i1p1f1", "r3i1p1f1"],
34-
variable_id=["tas", "pr"],
35-
frequency="mon",
36-
)
37-
dsd = cat.to_dataset_dict(ignore_facets=["institution_id", "table_id"])
38-
dsd = trim_time(dsd)
39-
dsd = ops.ensemble_mean(dsd)
40-
assert set(
41-
["CESM2.mean.pr", "CESM2.mean.tas", "CanESM5.mean.pr", "CanESM5.mean.tas"]
42-
) == set(dsd.keys())
43-
44-
45-
def test_composition():
4629
"""Run a test on composition of operators.
4730
4831
Operators may be locally defined, but we expect that the only argument taken is a
@@ -54,8 +37,8 @@ def test_composition():
5437
cat = ESGFCatalog().search(
5538
experiment_id="historical",
5639
source_id=["CanESM5"],
57-
variant_label=["r1i1p1f1", "r2i1p1f1", "r3i1p1f1"],
58-
variable_id=["tas"],
40+
variant_label=["r1i1p1f1", "r2i1p1f1"],
41+
variable_id=["gpp"],
5942
frequency="mon",
6043
)
6144
ensemble_mean = partial(ops.ensemble_mean, include_std=True)

0 commit comments

Comments
 (0)