Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/471.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ESMValTool recipes from the conda environment if it is available.
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ def write_recipe(self, definition: ExecutionDefinition) -> Path:
:
The path to the written recipe.
"""
recipe_dir = None
if (env_path := definition.diagnostic.provider.env_path).exists(): # type: ignore[attr-defined]
# Use the recipe from the conda environment if available.
subdir = Path("lib").joinpath("python*", "site-packages", "esmvaltool", "recipes")
if recipe_dirs := sorted(env_path.glob(str(subdir))):
recipe_dir = recipe_dirs[-1]
recipe = load_recipe(self.base_recipe, recipe_dir)
input_files = {
project: dataset_collection.datasets
for project, dataset_collection in definition.datasets.items()
}
recipe = load_recipe(self.base_recipe)
self.update_recipe(recipe, input_files)
recipe_txt = yaml.safe_dump(recipe, sort_keys=False)
logger.info(f"Using ESMValTool recipe:\n{recipe_txt}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pooch
import yaml
from loguru import logger

from climate_ref_esmvaltool.types import Recipe

Expand Down Expand Up @@ -160,19 +161,30 @@ def dataframe_to_recipe(
_RECIPES.load_registry(_buffer)


def load_recipe(recipe: str) -> Recipe:
def load_recipe(recipe: str, path: Path | None) -> Recipe:
"""Load a recipe.

Parameters
----------
recipe
The name of an ESMValTool recipe.
path:
The path where the ESMValTool recipes are located. If None, use the
default path for the recipe registry.

Returns
-------
The loaded recipe.
"""
if path is not None:
# Override default registry path.
previous_path = _RECIPES.path # type: ignore[attr-defined]
_RECIPES.path = path # type: ignore[attr-defined]
filename = _RECIPES.fetch(recipe)
if path is not None:
# Restore previous registry path.
_RECIPES.path = previous_path # type: ignore[attr-defined]
logger.info(f"Updating recipe from {filename}")

def normalize(obj: Any) -> Any:
# Ensure objects in the recipe are not shared.
Expand Down
Loading