ValueError: operands could not be broadcast together with shapes (96,193) (96,192)

93 views
Skip to first unread message

Geouser

unread,
Jun 20, 2019, 12:08:34 PM6/20/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Dear all,

I have recently started using Iris and no matter which datasets I work with, I cannot get Iris to plot vectors with iplt.quiver. You can find the datasets here (2 nc files):

from __future__ import (absolute_import, division, print_function)
import matplotlib.pyplot as plt
import matplotlib.cm as mpl_cm
import cartopy.crs as ccrs
import iris
import iris.plot as iplt
import iris.quickplot as qplt
import iris.analysis

#%% Importing cubes and preparing datasets
filenames
= ['/Users/sebastianbubmann/Dropbox/AOD_Uebungen/Uebung_8/FOCI1.1-SW010_echam6_ATM_mm_235002_gpheight.nc',
             
'/Users/sebastianbubmann/Dropbox/AOD_Uebungen/Uebung_8/FOCI1.1-SW010_echam6_ATM_mm_235002_wind.nc']

cubes
= iris.load(filenames)

gpheight
= cubes[0]
vwind
= cubes[1]
uwind
= cubes[2]

gpheigt_500
= gpheight.extract(iris.Constraint(air_pressure=50000))
gpheight_1000
= gpheight.extract(iris.Constraint(air_pressure=100000))

vwind_500
= vwind.extract(iris.Constraint(air_pressure=50000))
vwind_1000
= vwind.extract(iris.Constraint(air_pressure=100000))

uwind_500
= uwind.extract(iris.Constraint(air_pressure=50000))
uwind_1000
= uwind.extract(iris.Constraint(air_pressure=100000))

gpheigt_500
= gpheigt_500.collapsed('time', iris.analysis.MEAN)
gpheight_1000
= gpheight_1000.collapsed('time', iris.analysis.MEAN)
vwind_500
= vwind_500.collapsed('time', iris.analysis.MEAN)
vwind_1000
= vwind_1000.collapsed('time', iris.analysis.MEAN)
uwind_500
= uwind_500.collapsed('time', iris.analysis.MEAN)
uwind_1000
= uwind_1000.collapsed('time', iris.analysis.MEAN)

gpheigt_500
.rename('Gpheight 500 hPa')

#%% Plotting
fig1
= plt.figure()
ax
= plt.subplot(projection=ccrs.NorthPolarStereo())
#ax.set_global()
brewer_cmap
= mpl_cm.get_cmap('brewer_YlOrRd_09')
contour
= qplt.contour(gpheigt_500, brewer_cmap.N, cmap=brewer_cmap)
#vector = iplt.quiver(uwind_500,vwind_500)
plt
.clabel(contour, inline=False)
ax
.coastlines()
iplt
.show()
#plt.savefig('FNAME.png',bbox_inches="tight", DPI=600)

I do also with other vector cubes get the issue:
Traceback (most recent call last):

 
File "<ipython-input-133-0e613291776a>", line 6, in <module>
    vector
= iplt.quiver(uwind_500,vwind_500)

 
File "/anaconda3/lib/python3.7/site-packages/iris/plot.py", line 1305, in quiver
   
*args, **kwargs)

 
File "/anaconda3/lib/python3.7/site-packages/iris/plot.py", line 460, in _draw_2d_from_points
   
*args, **kwargs)

 
File "/anaconda3/lib/python3.7/site-packages/iris/plot.py", line 883, in _map_common
    new_args
, kwargs = arg_func(x, y, data, *args, **kwargs)

 
File "/anaconda3/lib/python3.7/site-packages/iris/plot.py", line 1252, in _vector_component_args
    mags
= np.sqrt(u_data * u_data + v_data * v_data)

 
File "/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py", line 4067, in __add__
   
return add(self, other)

 
File "/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py", line 1018, in __call__
    result
= self.f(da, db, *args, **kwargs)

ValueError: operands could not be broadcast together with shapes (96,193) (96,192)

But when I check the cubes, they seem to be of identical dimension:
vwind_500.shape
Out[131]: (96, 192)

uwind_500
.shape
Out[132]: (96, 192)

I have no clue if this is a bug but I trust that it is more likely I got something wrong with my code. Thanks a lot for any help in advance and best regards

Sebastian


Simon Peatman

unread,
Jun 21, 2019, 6:39:09 AM6/21/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Sebastian,

I've had a look at this and I'm sure it's a bug in Iris.  If you do:
uwind_500.coord('longitude').circular
vwind_500
.coord('longitude').circular
both come out as True.  When this is the case, iris.plot copies the column of values at 0deg longitude and appends it to the end as 360deg longitude.  However, it does this for u only, not for v!  This is why, when you gave it two Cubes of shape (96, 192), it told you that one of them was (96, 193).

As a quick workaround, you should be able to do
uwind_500.coord('longitude').circular = False
before plotting.

I've also posted an issue on Github so hopefully this will be fixed: https://github.com/SciTools/iris/issues/3340

Simon

Geouser

unread,
Jun 26, 2019, 3:48:03 PM6/26/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hi Simon,

thanks a lot for your quick response and workaround. Worked well for me.

Best

Sebastian

Simon Peatman

unread,
Aug 30, 2019, 9:14:05 AM8/30/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
FYI, there is now a bug fix for this which will be in v2.3: https://github.com/SciTools/iris/pull/3371

Simon
Reply all
Reply to author
Forward
0 new messages