Skip to content

Commit 9e5575b

Browse files
authored
Avoid dimensionality mismatch between data array and CF variables. (#4610)
1 parent af97c70 commit 9e5575b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

docs/src/whatsnew/3.2.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ v3.2.1 |build_date| [unreleased]
4444
attribute from a given CRS instead of creating a new `ccrs.Globe()` object.
4545
Iris can now handle non-Earth semi-major axes, as discussed in :issue:`4582` (:pull:`4605`).
4646

47+
#. `@trexfeathers`_ avoided a dimensionality mismatch when streaming the
48+
:attr:`~iris.coords.Coord.bounds` array for a scalar
49+
:class:`~iris.coords.Coord`. (:pull:`4610`).
50+
4751
💼 **Internal**
4852

4953
#. N/A
@@ -198,7 +202,7 @@ v3.2.1 |build_date| [unreleased]
198202
from assuming the globe to be the Earth (:issue:`4408`, :pull:`4497`)
199203

200204
#. `@rcomer`_ corrected the ``long_name`` mapping from UM stash code ``m01s09i215``
201-
to indicate cloud fraction greater than 7.9 oktas, rather than 7.5
205+
to indicate cloud fraction greater than 7.9 oktas, rather than 7.5
202206
(:issue:`3305`, :pull:`4535`)
203207

204208
#. `@lbdreyer`_ fixed a bug in :class:`iris.io.load_http` which was missing an import

lib/iris/fileformats/netcdf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,6 +2865,14 @@ def _increment_name(self, varname):
28652865

28662866
@staticmethod
28672867
def _lazy_stream_data(data, fill_value, fill_warn, cf_var):
2868+
if hasattr(data, "shape") and data.shape == (1,) + cf_var.shape:
2869+
# (Don't do this check for string data).
2870+
# Reduce dimensionality where the data array has an extra dimension
2871+
# versus the cf_var - to avoid a broadcasting ambiguity.
2872+
# Happens when bounds data is for a scalar point - array is 2D but
2873+
# contains just 1 row, so the cf_var is 1D.
2874+
data = data.squeeze(axis=0)
2875+
28682876
if is_lazy_data(data):
28692877

28702878
def store(data, cf_var, fill_value):

lib/iris/tests/unit/fileformats/netcdf/test_Saver.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
TransverseMercator,
3131
VerticalPerspective,
3232
)
33-
from iris.coords import DimCoord
33+
from iris.coords import AuxCoord, DimCoord
3434
from iris.cube import Cube
3535
from iris.fileformats.netcdf import Saver
3636
import iris.tests.stock as stock
@@ -299,6 +299,22 @@ def test_with_climatology(self):
299299
saver.write(cube)
300300
self.assertCDL(nc_path)
301301

302+
def test_dimensional_to_scalar(self):
303+
# Bounds for 1 point are still in a 2D array.
304+
scalar_bounds = self.array_lib.arange(2).reshape(1, 2)
305+
scalar_point = scalar_bounds.mean()
306+
scalar_data = self.array_lib.zeros(1)
307+
scalar_coord = AuxCoord(points=scalar_point, bounds=scalar_bounds)
308+
cube = Cube(scalar_data, aux_coords_and_dims=[(scalar_coord, 0)])[0]
309+
with self.temp_filename(".nc") as nc_path:
310+
with Saver(nc_path, "NETCDF4") as saver:
311+
saver.write(cube)
312+
ds = nc.Dataset(nc_path)
313+
# Confirm that the only dimension is the one denoting the number
314+
# of bounds - have successfully saved the 2D bounds array into 1D.
315+
self.assertEqual(["bnds"], list(ds.dimensions.keys()))
316+
ds.close()
317+
302318

303319
class Test__create_cf_bounds(tests.IrisTest):
304320
# Method is substituted in test_Saver__lazy.

0 commit comments

Comments
 (0)