Skip to content

Commit 59e4205

Browse files
Add _read_facets to intake configuration: see intake/intake-esm#717
1 parent 2050081 commit 59e4205

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

esmvalcore/config/_intake.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import stat
99
from functools import lru_cache
1010
from pathlib import Path
11+
from typing import Any
1112

1213
import yaml
1314

@@ -122,3 +123,49 @@ def load_intake_config():
122123
def get_intake_config():
123124
"""Get the esgf-pyclient configuration."""
124125
return load_intake_config()
126+
127+
128+
def _read_facets(
129+
cfg: dict,
130+
fhandle: str | None,
131+
project: str | None = None,
132+
) -> tuple[dict[str, Any], str]:
133+
"""
134+
Extract facet mapping from ESMValCore configuration for a given catalog file handle.
135+
136+
Recursively traverses the ESMValCore configuration structure to find the
137+
facet mapping that corresponds to the specified file handle.
138+
139+
Parameters
140+
----------
141+
cfg : dict
142+
The ESMValCore intake configuration dictionary.
143+
fhandle : str
144+
The file handle/path of the intake-esm catalog to match.
145+
project : str, optional
146+
The current project name in the configuration hierarchy.
147+
148+
Returns
149+
-------
150+
tuple
151+
A tuple containing:
152+
- dict: Facet mapping between ESMValCore facets and catalog columns
153+
- str: The project name associated with the catalog file
154+
"""
155+
if fhandle is None:
156+
raise ValueError(
157+
"Unable to ascertain facets without valid file handle."
158+
)
159+
160+
for _project, val in cfg.items():
161+
if not (isinstance(val, list)):
162+
return _read_facets(val, fhandle, project or _project)
163+
for facet_info in val:
164+
file, facets = facet_info.get("file"), facet_info.get("facets")
165+
if file == fhandle:
166+
return facets, project # type: ignore[return-value]
167+
else:
168+
raise ValueError(
169+
f"No facets found for {fhandle} in the config file. "
170+
"Please check the config file and ensure it is valid."
171+
)

0 commit comments

Comments
 (0)