Skip to content

Commit

Permalink
Improve preprocessor output sorting code (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela authored Jun 23, 2023
1 parent 26b50fa commit 1101d36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
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

0 comments on commit 1101d36

Please sign in to comment.