Skip to content

Commit

Permalink
Use iris (SciTools/iris#5031) for saving
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela committed Nov 21, 2022
1 parent 05d69f7 commit be991f8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion esmvalcore/_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def _run_dask(self, cfg) -> None:
logger.info(f"Skipping task {task.name}")
for future in dask.distributed.as_completed(futures_to_files):
filename = futures_to_files[future]
logger.info(f"Wrote {filename}")
logger.info(f"Wrote (delayed) {filename}")

def _run_sequential(self) -> None:
"""Run tasks sequentially."""
Expand Down
6 changes: 4 additions & 2 deletions esmvalcore/preprocessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from pprint import pformat

from dask.delayed import Delayed
from iris.cube import Cube

from .._provenance import TrackedFile
Expand Down Expand Up @@ -458,7 +459,7 @@ def save(self):
self._cubes,
**self.settings['save'],
)
if not self.settings['save'].get('compute', True):
if isinstance(result, Delayed):
self.delayed = result
self.files = [self.settings['save']['filename']]
self.files = preprocess(self.files,
Expand Down Expand Up @@ -621,7 +622,8 @@ def _run(self, _):

for product in self.products:
product.close()
self.delayeds[product.filename] = product.delayed
if product.delayed is not None:
self.delayeds[product.filename] = product.delayed
metadata_files = write_metadata(self.products,
self.write_ncl_interface)
return metadata_files
Expand Down
28 changes: 12 additions & 16 deletions esmvalcore/preprocessor/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import iris.aux_factory
import iris.exceptions
import numpy as np
import xarray
import yaml
from cf_units import suppress_errors

Expand Down Expand Up @@ -285,8 +284,8 @@ def save(cubes,
Returns
-------
str
filename
str or dask.delayed.Delayed
filename or delayed
Raises
------
Expand Down Expand Up @@ -341,20 +340,17 @@ def save(cubes,
cube.var_name = alias

cube = cubes[0]
if compute is True:
iris.save(cube, **kwargs)
if not compute and not cube.has_lazy_data():
# What should happen if the data is not lazy and we're asked for a
# lazy save?
# https://github.com/SciTools/iris/pull/5031#issuecomment-1322166230
compute = True

result = iris.save(cube, compute=compute, **kwargs)
if compute:
logger.info("Wrote (immediate) %s", filename)
return filename

data_array = xarray.DataArray.from_iris(cube)
kwargs.pop('target')
kwargs['_FillValue'] = kwargs.pop('fill_value')
encoding = {cube.var_name: kwargs}
delayed = data_array.to_netcdf(
filename,
encoding=encoding,
compute=False,
)
return delayed
return result


def _get_debug_filename(filename, step):
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
'scitools-iris>=3.2.1',
'shapely[vectorized]',
'stratify',
'xarray',
'yamale',
],
# Test dependencies
Expand Down

0 comments on commit be991f8

Please sign in to comment.