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

Remove deprecated callback argument from preprocessor load function #2207

Merged
merged 2 commits into from
Sep 28, 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
3 changes: 0 additions & 3 deletions esmvalcore/_recipe/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ def _get_default_settings(dataset):

settings = {}

# Configure (deprecated, remove for v2.10.0) load callback
settings['load'] = {'callback': 'default'}

if _derive_needed(dataset):
settings['derive'] = {
'short_name': facets['short_name'],
Expand Down
11 changes: 3 additions & 8 deletions esmvalcore/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,19 +677,15 @@ def load(self) -> Cube:
iris.cube.Cube
An :mod:`iris` cube with the data corresponding the the dataset.
"""
return self._load_with_callback(callback='default')

def _load_with_callback(self, callback):
# TODO: Remove the callback argument for v2.10.0.
input_files = list(self.files)
for supplementary_dataset in self.supplementaries:
input_files.extend(supplementary_dataset.files)
esgf.download(input_files, self.session['download_dir'])

cube = self._load(callback)
cube = self._load()
supplementary_cubes = []
for supplementary_dataset in self.supplementaries:
supplementary_cube = supplementary_dataset._load(callback)
supplementary_cube = supplementary_dataset._load()
supplementary_cubes.append(supplementary_cube)

output_file = _get_output_file(self.facets, self.session.preproc_dir)
Expand All @@ -704,7 +700,7 @@ def _load_with_callback(self, callback):

return cubes[0]

def _load(self, callback) -> Cube:
def _load(self) -> Cube:
"""Load self.files into an iris cube and return it."""
if not self.files:
lines = [
Expand All @@ -731,7 +727,6 @@ def _load(self, callback) -> Cube:
**self.facets,
}
settings['load'] = {
'callback': callback,
'ignore_warnings': get_ignored_warnings(
self.facets['project'], 'load'
),
Expand Down
5 changes: 1 addition & 4 deletions esmvalcore/preprocessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,7 @@ def apply(self, step: str, debug: bool = False):
def cubes(self):
"""Cubes."""
if self._cubes is None:
callback = self.settings.get('load', {}).get('callback')
self._cubes = [
ds._load_with_callback(callback) for ds in self.datasets
]
self._cubes = [ds.load() for ds in self.datasets]
return self._cubes

@cubes.setter
Expand Down
19 changes: 2 additions & 17 deletions esmvalcore/preprocessor/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import shutil
import warnings
from collections.abc import Callable
from itertools import groupby
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -96,7 +95,7 @@ def _get_attr_from_field_coord(ncfield, coord_name, attr):
return None


def concatenate_callback(raw_cube, field, _):
def _load_callback(raw_cube, field, _):
"""Use this callback to fix anything Iris tries to break."""
# Remove attributes that cause issues with merging and concatenation
_delete_attributes(
Expand All @@ -122,7 +121,6 @@ def _delete_attributes(iris_object, atts):

def load(
file: str | Path,
callback: Optional[Callable] = None,
ignore_warnings: Optional[list[dict]] = None,
) -> CubeList:
"""Load iris cubes from string or Path objects.
Expand All @@ -131,11 +129,6 @@ def load(
----------
file:
File to be loaded. Could be string or POSIX Path object.
callback:
Callback function passed to :func:`iris.load_raw`.

.. deprecated:: 2.8.0
This argument will be removed in 2.10.0.
ignore_warnings:
Keyword arguments passed to :func:`warnings.filterwarnings` used to
ignore warnings issued by :func:`iris.load_raw`. Each list element
Expand All @@ -152,14 +145,6 @@ def load(
Cubes are empty.

"""
if not (callback is None or callback == 'default'):
msg = ("The argument `callback` has been deprecated in "
"ESMValCore version 2.8.0 and is scheduled for removal in "
"version 2.10.0.")
warnings.warn(msg, ESMValCoreDeprecationWarning)
if callback == 'default':
callback = concatenate_callback

file = Path(file)
logger.debug("Loading:\n%s", file)

Expand Down Expand Up @@ -191,7 +176,7 @@ def load(
# warnings.filterwarnings
# (see https://github.com/SciTools/cf-units/issues/240)
with suppress_errors():
raw_cubes = iris.load_raw(file, callback=callback)
raw_cubes = iris.load_raw(file, callback=_load_callback)
logger.debug("Done with loading %s", file)

if not raw_cubes:
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/preprocessor/_io/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from iris.coords import DimCoord
from iris.cube import Cube, CubeList

from esmvalcore.preprocessor._io import concatenate_callback, load
from esmvalcore.preprocessor._io import load


def _create_sample_cube():
Expand Down Expand Up @@ -60,7 +60,7 @@ def test_callback_remove_attributes(self):
cube.attributes[attr] = attr
self._save_cube(cube)
for temp_file in self.temp_files:
cubes = load(temp_file, callback='default')
cubes = load(temp_file)
cube = cubes[0]
self.assertEqual(1, len(cubes))
self.assertTrue((cube.data == np.array([1, 2])).all())
Expand All @@ -79,7 +79,7 @@ def test_callback_remove_attributes_from_coords(self):
coord.attributes[attr] = attr
self._save_cube(cube)
for temp_file in self.temp_files:
cubes = load(temp_file, callback='default')
cubes = load(temp_file)
cube = cubes[0]
self.assertEqual(1, len(cubes))
self.assertTrue((cube.data == np.array([1, 2])).all())
Expand All @@ -94,7 +94,7 @@ def test_callback_fix_lat_units(self):
cube = _create_sample_cube()
temp_file = self._save_cube(cube)

cubes = load(temp_file, callback=concatenate_callback)
cubes = load(temp_file)
cube = cubes[0]
self.assertEqual(1, len(cubes))
self.assertTrue((cube.data == np.array([1, 2])).all())
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/preprocessor/test_preprocessing_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_load_save_task(tmp_path):
iris.save(cube, in_file)
dataset = Dataset(short_name='tas')
dataset.files = [in_file]
dataset._load_with_callback = lambda _: cube.copy()
dataset.load = lambda: cube.copy()

# Create task
task = PreprocessingTask([
Expand Down Expand Up @@ -57,11 +57,11 @@ def test_load_save_and_other_task(tmp_path, monkeypatch):

dataset1 = Dataset(short_name='tas', dataset='dataset1')
dataset1.files = [file1]
dataset1._load_with_callback = lambda _: in_cube.copy()
dataset1.load = lambda: in_cube.copy()

dataset2 = Dataset(short_name='tas', dataset='dataset1')
dataset2.files = [file2]
dataset2._load_with_callback = lambda _: in_cube.copy()
dataset2.load = lambda: in_cube.copy()

# Create some mock preprocessor functions and patch
# `esmvalcore.preprocessor` so it uses them.
Expand Down
7 changes: 0 additions & 7 deletions tests/integration/recipe/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
)

DEFAULT_PREPROCESSOR_STEPS = (
'load',
'remove_supplementary_variables',
'save',
)
Expand All @@ -106,9 +105,6 @@ def create_test_file(filename, tracking_id=None):
def _get_default_settings_for_chl(save_filename):
"""Get default preprocessor settings for chl."""
defaults = {
'load': {
'callback': 'default'
},
'remove_supplementary_variables': {},
'save': {
'compress': False,
Expand Down Expand Up @@ -557,9 +553,6 @@ def test_default_fx_preprocessor(tmp_path, patched_datafinder, session):
assert preproc_dir.startswith(str(tmp_path))

defaults = {
'load': {
'callback': 'default'
},
'remove_supplementary_variables': {},
'save': {
'compress': False,
Expand Down
1 change: 0 additions & 1 deletion tests/unit/recipe/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ def test_get_default_settings(mocker):

settings = _recipe._get_default_settings(dataset)
assert settings == {
'load': {'callback': 'default'},
'remove_supplementary_variables': {},
'save': {'compress': False, 'alias': 'sic'},
}
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,6 @@ def mock_preprocess(items, step, input_files, output_file, debug,

load_args = {
'load': {
'callback': 'default',
'ignore_warnings': None,
},
'fix_file': {
Expand Down