Skip to content

Commit e2f8cab

Browse files
committed
Additional fixes
1 parent 0e8e716 commit e2f8cab

7 files changed

Lines changed: 73 additions & 30 deletions

File tree

esmvalcore/cmor/_fixes/cmip6/cesm2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ def fix_file(self, filepath, output_dir, add_unique_suffix=False):
7474
)
7575
with Dataset(new_path, mode="a") as dataset:
7676
dataset.variables["a_bnds"][:] = dataset.variables["a_bnds"][
77-
::-1, :
77+
::-1,
78+
:,
7879
]
7980
dataset.variables["b_bnds"][:] = dataset.variables["b_bnds"][
80-
::-1, :
81+
::-1,
82+
:,
8183
]
8284
return new_path
8385

esmvalcore/cmor/_fixes/cmip6/cesm2_waccm.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ def fix_file(self, filepath, output_dir, add_unique_suffix=False):
5050
)
5151
with Dataset(new_path, mode="a") as dataset:
5252
dataset.variables["a_bnds"][:] = dataset.variables["a_bnds"][
53-
:, ::-1
53+
:,
54+
::-1,
5455
]
5556
dataset.variables["b_bnds"][:] = dataset.variables["b_bnds"][
56-
:, ::-1
57+
:,
58+
::-1,
5759
]
5860
return new_path
5961

esmvalcore/preprocessor/_supplementary_vars.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def add_cell_measure(
104104

105105

106106
def add_ancillary_variable(
107-
cube: Cube, ancillary_cube: Cube | iris.coords.AncillaryVariable
107+
cube: Cube,
108+
ancillary_cube: Cube | iris.coords.AncillaryVariable,
108109
) -> None:
109110
"""Add ancillary variable to cube (in-place).
110111
@@ -154,7 +155,8 @@ def add_ancillary_variable(
154155
data_dims[ancillary_dim] = cube_dim
155156
except iris.exceptions.CoordinateNotFoundError:
156157
logger.debug(
157-
"%s from ancillary cube not found in cube coords.", coord
158+
"%s from ancillary cube not found in cube coords.",
159+
coord,
158160
)
159161
if None in data_dims:
160162
none_dims = ", ".join(

esmvalcore/preprocessor/_volume.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from iris.util import broadcast_to_shape
1919

2020
from esmvalcore.iris_helpers import ignore_iris_vague_metadata_warnings
21+
from esmvalcore.preprocessor._regrid import extract_levels
2122
from esmvalcore.preprocessor._shared import (
2223
get_array_module,
2324
get_coord_weights,
@@ -30,7 +31,6 @@
3031
from esmvalcore.preprocessor._supplementary_vars import (
3132
register_supplementaries,
3233
)
33-
from esmvalcore.preprocessor._regrid import extract_levels
3434

3535
if TYPE_CHECKING:
3636
from collections.abc import Iterable, Sequence
@@ -696,15 +696,17 @@ def extract_surface_from_atm(
696696
try:
697697
ps_cube = cube.ancillary_variable("surface_air_pressure")
698698
except iris.exceptions.AncillaryVariableNotFoundError as exc:
699-
raise ValueError("Surface air pressure could not be found") from exc
699+
msg = "Surface air pressure could not be found"
700+
raise ValueError(msg) from exc
700701

701702
# Fill masked data if necessary (interpolation fails with masked data)
702703
(z_axis,) = cube.coord_dims(cube.coord(axis="Z", dim_coords=True))
703704
npx = get_array_module(cube.core_data())
704705
if iris.util.is_masked(cube.core_data()):
705706
mask = npx.ma.getmaskarray(cube.core_data())
706707
first_unmasked_data = _get_first_unmasked_data(
707-
cube.core_data(), axis=z_axis
708+
cube.core_data(),
709+
axis=z_axis,
708710
)
709711
dim_map = [dim for dim in range(cube.ndim) if dim != z_axis]
710712
first_unmasked_data = iris.util.broadcast_to_shape(

tests/integration/cmor/_fixes/test_data/create_test_data.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ def save_cnrm_cm6_1_cl_file(save_path):
203203
dataset.createVariable("time", np.float64, dimensions=("time",))
204204
dataset.createVariable("lev", np.float64, dimensions=("lev",))
205205
dataset.createVariable(
206-
"lev_bnds", np.float64, dimensions=("lev", "bnds")
206+
"lev_bnds",
207+
np.float64,
208+
dimensions=("lev", "bnds"),
207209
)
208210
dataset.createVariable("lat", np.float64, dimensions=("lat",))
209211
dataset.createVariable("lon", np.float64, dimensions=("lon",))
@@ -238,14 +240,20 @@ def save_cnrm_cm6_1_cl_file(save_path):
238240
# Wrong shape of bounds is intended
239241
dataset.createVariable("ap", np.float64, dimensions=("lev",))
240242
dataset.createVariable(
241-
"ap_bnds", np.float64, dimensions=("bnds", "lev")
243+
"ap_bnds",
244+
np.float64,
245+
dimensions=("bnds", "lev"),
242246
)
243247
dataset.createVariable("b", np.float64, dimensions=("lev",))
244248
dataset.createVariable(
245-
"b_bnds", np.float64, dimensions=("bnds", "lev")
249+
"b_bnds",
250+
np.float64,
251+
dimensions=("bnds", "lev"),
246252
)
247253
dataset.createVariable(
248-
"ps", np.float64, dimensions=("time", "lat", "lon")
254+
"ps",
255+
np.float64,
256+
dimensions=("time", "lat", "lon"),
249257
)
250258
dataset.variables["ap"][:] = [1.0, 2.0, 5.0]
251259
dataset.variables["ap_bnds"][:] = [[0.0, 1.5, 1.5], [3.0, 3.0, 6.0]]
@@ -257,10 +265,14 @@ def save_cnrm_cm6_1_cl_file(save_path):
257265

258266
# Cl variable
259267
dataset.createVariable(
260-
"cl", np.float32, dimensions=("time", "lev", "lat", "lon")
268+
"cl",
269+
np.float32,
270+
dimensions=("time", "lev", "lat", "lon"),
261271
)
262272
dataset.variables["cl"][:] = np.full(
263-
(1, 3, 2, 2), 0.0, dtype=np.float32
273+
(1, 3, 2, 2),
274+
0.0,
275+
dtype=np.float32,
264276
)
265277
dataset.variables[
266278
"cl"
@@ -371,7 +383,9 @@ def save_cesm2_waccm_cl_file(save_path):
371383
# Dimensional variables
372384
dataset.createVariable("lev", np.float64, dimensions=("lev",))
373385
dataset.createVariable(
374-
"lev_bnds", np.float64, dimensions=("lev", "bnds")
386+
"lev_bnds",
387+
np.float64,
388+
dimensions=("lev", "bnds"),
375389
)
376390
dataset.variables["lev"][:] = [1.0, 2.0]
377391
dataset.variables["lev"].bounds = "lev_bnds"
@@ -388,11 +402,15 @@ def save_cesm2_waccm_cl_file(save_path):
388402
# Coordinates for derivation of pressure coordinate
389403
dataset.createVariable("a", np.float64, dimensions=("lev",))
390404
dataset.createVariable(
391-
"a_bnds", np.float64, dimensions=("lev", "bnds")
405+
"a_bnds",
406+
np.float64,
407+
dimensions=("lev", "bnds"),
392408
)
393409
dataset.createVariable("b", np.float64, dimensions=("lev",))
394410
dataset.createVariable(
395-
"b_bnds", np.float64, dimensions=("lev", "bnds")
411+
"b_bnds",
412+
np.float64,
413+
dimensions=("lev", "bnds"),
396414
)
397415
dataset.variables["a"][:] = [1.0, 2.0]
398416
dataset.variables["a"].bounds = "a_bnds"
@@ -404,7 +422,9 @@ def save_cesm2_waccm_cl_file(save_path):
404422
# Cl variable
405423
dataset.createVariable("cl", np.float32, dimensions=("lev",))
406424
dataset.variables["cl"][:] = np.full(
407-
(2,), [0.0, 1.0], dtype=np.float32
425+
(2,),
426+
[0.0, 1.0],
427+
dtype=np.float32,
408428
)
409429
dataset.variables[
410430
"cl"
@@ -428,7 +448,9 @@ def save_gfdl_cm4_cl_file(save_path):
428448
dataset.createVariable("time", np.float64, dimensions=("time",))
429449
dataset.createVariable("lev", np.float64, dimensions=("lev",))
430450
dataset.createVariable(
431-
"lev_bnds", np.float64, dimensions=("lev", "bnds")
451+
"lev_bnds",
452+
np.float64,
453+
dimensions=("lev", "bnds"),
432454
)
433455
dataset.createVariable("lat", np.float64, dimensions=("lat",))
434456
dataset.createVariable("lon", np.float64, dimensions=("lon",))
@@ -464,14 +486,20 @@ def save_gfdl_cm4_cl_file(save_path):
464486
# Coordinates for derivation of pressure coordinate
465487
dataset.createVariable("ap", np.float64, dimensions=("lev",))
466488
dataset.createVariable(
467-
"ap_bnds", np.float64, dimensions=("lev", "bnds")
489+
"ap_bnds",
490+
np.float64,
491+
dimensions=("lev", "bnds"),
468492
)
469493
dataset.createVariable("b", np.float64, dimensions=("lev",))
470494
dataset.createVariable(
471-
"b_bnds", np.float64, dimensions=("lev", "bnds")
495+
"b_bnds",
496+
np.float64,
497+
dimensions=("lev", "bnds"),
472498
)
473499
dataset.createVariable(
474-
"ps", np.float64, dimensions=("time", "lat", "lon")
500+
"ps",
501+
np.float64,
502+
dimensions=("time", "lat", "lon"),
475503
)
476504
dataset.variables["ap"][:] = [1.0, 2.0, 5.0]
477505
dataset.variables["ap"].units = "Pa"
@@ -484,10 +512,14 @@ def save_gfdl_cm4_cl_file(save_path):
484512

485513
# Cl variable
486514
dataset.createVariable(
487-
"cl", np.float32, dimensions=("time", "lev", "lat", "lon")
515+
"cl",
516+
np.float32,
517+
dimensions=("time", "lev", "lat", "lon"),
488518
)
489519
dataset.variables["cl"][:] = np.full(
490-
(1, 3, 2, 2), 0.0, dtype=np.float32
520+
(1, 3, 2, 2),
521+
0.0,
522+
dtype=np.float32,
491523
)
492524
dataset.variables[
493525
"cl"

tests/integration/preprocessor/_supplementary_vars/test_add_supplementary_variables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ def test_add_ancillary_vars_errors(self):
278278
units="Pa",
279279
)
280280
ancillary_cube = iris.cube.Cube(
281-
np.ones((3)), dim_coords_and_dims=[(plev_dim, 0)]
281+
np.ones(3),
282+
dim_coords_and_dims=[(plev_dim, 0)],
282283
)
283284
msg = "No coordinate associated with ancillary"
284285
with pytest.raises(ValueError, match=msg):

tests/unit/preprocessor/_volume/test_volume.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,11 @@ def test_extract_trajectory(self):
822822
def test__get_first_unmasked_data(self):
823823
"""Test to get first unmasked value of an array along an axis."""
824824
(z_axis,) = self.grid_4d_2.coord_dims(
825-
self.grid_4d_2.coord(axis="Z", dim_coords=True)
825+
self.grid_4d_2.coord(axis="Z", dim_coords=True),
826826
)
827827
result = _get_first_unmasked_data(
828-
self.grid_4d_2.core_data(), axis=z_axis
828+
self.grid_4d_2.core_data(),
829+
axis=z_axis,
829830
)
830831
expected = np.ma.ones((4, 2, 2))
831832
self.assert_array_equal(result, expected)
@@ -843,13 +844,14 @@ def test_extract_surface_from_atm(self):
843844
with self.assertRaises(ValueError) as err:
844845
extract_surface_from_atm(self.grid_4d_ps)
845846
self.assertEqual(
846-
"Surface air pressure could not be found", str(err.exception)
847+
"Surface air pressure could not be found",
848+
str(err.exception),
847849
)
848850
# Test with ancillary variable
849851
self.grid_4d_ps.add_ancillary_variable(ps_ancillary, [0, 2, 3])
850852
result = extract_surface_from_atm(self.grid_4d_ps)
851853
expected = np.ma.array(
852-
np.broadcast_to([[[0.0]], [[0.0]], [[2.0]], [[3.0]]], (4, 2, 2))
854+
np.broadcast_to([[[0.0]], [[0.0]], [[2.0]], [[3.0]]], (4, 2, 2)),
853855
)
854856
self.assert_array_equal(result.data, expected)
855857
assert result.var_name == "vars"

0 commit comments

Comments
 (0)