1

The Iris user guide gives lots of info about combining constraints on coordinates with a logical AND. Is it also possible to combine them with a logical OR?

I want to extract parts of a cube where (coord1 == x AND coord2 == y) OR (coord1 == z).

Thanks.

RuthC
  • 1,269
  • 1
  • 10
  • 21

1 Answers1

1

Constraints do support the "&" operator, but not "|".
I think the logic of that is that, when applied to extraction from a cube, the result should always be just a single cube -- which would not be always be the case if an 'OR' were allowed.
So for cube extraction, you can think of the constraints as specifying a 'cutout shape', which is only permitted to be hyper-rectangular : This is just like numpy indexing operations.

In fact the exact case you suggest "(x=1 and y=2) or z=3" is a counter-example : The result of this would not always be "square", so can't generally be a single cube.

The case of loading from a set of datafiles, however, is somewhat different : the result can anyway have multiple cubes of incompatible shapes.
In that case, you can sometimes use a "cube function" type of constraint to select portions of data, potentially using an 'or'-like logic.
However, the results would then depend on the source data format, i.e. what are the "raw cubes" it initially loads + thus selects from.
For example, a cube function like:

def cubefn(cube):
    return (cube.name() == 'air_temperature' or
            cube.coord('model_level').points[0] < 7)

That could result in an air-temperature cube over all levels, and various other cubes all restricted to the first 7 levels.
Again, you can see why that isn't workable in the context of cube extraction.

pp-mo
  • 468
  • 2
  • 8
  • Thanks for the explanation. In my case both coords are on the same dimension, but I see your point that this would not be true in general (and I'm aware Iris throws an exception if you try to to an extract on a multidimensional coord, so there is consistency there). I think doing the restriction on load will work for my case, or my best bet might be to add a new coord whose points are calculated from a function of the points on the existing coords. – RuthC Jul 31 '17 at 12:15