Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve preprocessor output sorting code #2111

Merged
merged 1 commit into from
Jun 23, 2023
Merged
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
7 changes: 2 additions & 5 deletions esmvalcore/preprocessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from ._detrend import detrend
from ._io import (
_get_debug_filename,
_sort_products,
cleanup,
concatenate,
load,
Expand Down Expand Up @@ -663,11 +664,7 @@ def _run(self, _):
self.products = _apply_multimodel(self.products, step,
self.debug)
else:
for product in sorted(
self.products,
# Sort datasets according to their order in the recipe.
key=lambda p: p.attributes.get('recipe_dataset_index', ''),
):
for product in _sort_products(self.products):
logger.debug("Applying single-model steps to %s", product)
for step in block:
if step in product.settings:
Expand Down
19 changes: 12 additions & 7 deletions esmvalcore/preprocessor/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,23 @@ def cleanup(files, remove=None):
return files


def _sort_products(products):
"""Sort preprocessor output files by their order in the recipe."""
return sorted(
products,
key=lambda p: (
p.attributes.get('recipe_dataset_index', 1e6),
p.attributes.get('dataset', ''),
),
)


def write_metadata(products, write_ncl=False):
"""Write product metadata to file."""
output_files = []
for output_dir, prods in groupby(products,
lambda p: os.path.dirname(p.filename)):
sorted_products = sorted(
prods,
key=lambda p: (
p.attributes.get('recipe_dataset_index', 1e6),
p.attributes.get('dataset', ''),
),
)
sorted_products = _sort_products(prods)
metadata = {}
for product in sorted_products:
if isinstance(product.attributes.get('exp'), (list, tuple)):
Expand Down