pythonpython-iris

Combine Iris constraints with OR?


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.


Solution

  • 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.