Iris: Cube broadcasting for addition/subtraction.

Created on 23 Dec 2015  路  7Comments  路  Source: SciTools/iris

As @shoyer noted at PR #888, the cube broadcasting rules for addition and subtraction are different (more strict) than for multiplication and division. Lines 260-279 in iris.analysis.maths have the extra add/subtract checks. This has caught me out a few times when trying to take differences between very similar cubes.

For example, today the subtract threw an error with This operation cannot be performed as there are differing coordinates (latitude, longitude) remaining which cannot be ignored. It took some digging to realise that the only difference was that var_name was set for both coords on only one of the cubes.

I'm wondering if these extra checks for addition and subtraction could simply be removed. There is a specific (old) test, so the behaviour is obviously deliberate. I don't understand why though.

Most helpful comment

@rcomer Finally, we got there in the end. Thanks for hangin' in there.... hope you weren't holding your breath :wink:

All 7 comments

Thanks @rcomer. I agree - this is peculiar behaviour, and there is no obvious comment justifying it. If you're keen, it might be worth putting the straw man PR up which just uses _binary_op_common for add & subtract...

Thanks @pelson. I'll do that when I get a chance.

Hi,
Any work around for this subtraction problem? I have two cubes from which I have removed most of the Scalar Coordinates, leaving just:

>>> print model_ann_mean
unknown / (K)                       (latitude: 144; longitude: 192)
     Dimension coordinates:
          latitude                           x               -
          longitude                          -               x
     Scalar coordinates:
          time: 1999-01-01 12:00:00, bound=(1989-01-01 00:00:00, 2009-01-01 00:00:00)
     Attributes:
          Conventions: CF-1.5
>>> print era_ann_mean
unknown / (K)                       (latitude: 144; longitude: 192)
     Dimension coordinates:
          latitude                           x               -
          longitude                          -               x
     Scalar coordinates:
          time: 1998-12-17 00:00:00, bound=(1988-12-16 12:00:00, 2008-12-16 12:00:00)
     Attributes:
          Conventions: CF-1.5

But:

>>> bias_ann_mean = model_ann_mean  - era_ann_mean
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/projects/access/apps/pythonlib/iris/1.9.1/lib/python2.7/site-packages/Iris-1.9.1-py2.7-linux-x86_64.egg/iris/cube.py", line 2826, in __sub__
    return iris.analysis.maths.subtract(self, other, ignore=True)
  File "/projects/access/apps/pythonlib/iris/1.9.1/lib/python2.7/site-packages/Iris-1.9.1-py2.7-linux-x86_64.egg/iris/analysis/maths.py", line 233, in subtract
    dim=dim, ignore=ignore, in_place=in_place)
  File "/projects/access/apps/pythonlib/iris/1.9.1/lib/python2.7/site-packages/Iris-1.9.1-py2.7-linux-x86_64.egg/iris/analysis/maths.py", line 279, in _add_subtract_common
    in bad_coord_grps}))
ValueError: This operation cannot be performed as there are differing coordinates (latitude, longitude) remaining which cannot be ignored.
>>> 

Yet the lat and long points are identical. Not sure what happened to the names through averaging etc to this stage. So, totally stuck, any help/advice much appreciated.

Thanks,

Roger

Hi @rwbodman,

have you checked the var_name on the latitude and longitude coordinates? They can all be safely set to None if they differ e.g.

model_ann_mean.coord('longitude').var_name = None

If all else fails and you don't mind loading the data, you can just do

bias_ann_mean = model_ann_mean - era_ann_mean.data

The good news is that these issues are being actively looked at ready for Iris v2 (see #2599).

Ok, that helps. Thanks for your reply.

Regards,

Roger

On 7 Sep 2017, at 7:55 pm, Ruth Comer notifications@github.com wrote:

have you checked the var_name on the latitude and longitude coordinates? They can all be safely set to None if they differ e.g.

model_ann_mean.coord('longitude').var_name = None
If all else fails and you don't mind loading the data, you can just do

bias_ann_mean = model_ann_mean - era_ann_mean.data

Now fixed by #3774 #3785 :+1: :tada: :raised_hands: :sunglasses: :partying_face:

@rcomer Finally, we got there in the end. Thanks for hangin' in there.... hope you weren't holding your breath :wink:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rcomer picture rcomer  路  5Comments

pelson picture pelson  路  4Comments

cpelley picture cpelley  路  3Comments

DPeterK picture DPeterK  路  5Comments

bjlittle picture bjlittle  路  5Comments