SciTools / iris

A powerful, format-agnostic, and community-driven Python package for analysing and visualising Earth science data
https://scitools-iris.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
634 stars 283 forks source link

Cube loses ancillary variables and cell_measures after (some) intersections #5413

Closed valeriupredoi closed 1 month ago

valeriupredoi commented 1 year ago

Hey folks,

Here's one sneaky one for you:

Main point

An iris cube that has ancillary variables and cell_measures, upon intersecting it with a region via cube.intersect may lose those ancillary variables and cell_measures depending on what the coordinates of the intersected region are.

Minimal fully reproducible code

import iris

cube = iris.load_cube("cube_to_intersect_smol.nc")

def extract_region(cube, start_longitude, end_longitude, start_latitude,
                   end_latitude):
    print("Cube before intersection", cube)
    print("Ancil variables", cube.ancillary_variables())
    print("Cell measures", cube.cell_measures())
    region_subset = cube.intersection(
        longitude=(start_longitude, end_longitude),
        latitude=(start_latitude, end_latitude),
        ignore_bounds=True,
    )
    print("Cube after intersection", region_subset)

# intersection cube loses cell measures and ancillary variables
extract_region(cube,
               start_longitude=-90,
               end_longitude=40,
               start_latitude=20,
               end_latitude=80
    )

# intersection cube doesn't lose cell measures and ancillary variables
extract_region(cube,
               start_longitude=160,
               end_longitude=280,
               start_latitude=-5,
               end_latitude=5
    )

cube_to_intersect_smol.nc attached here (rename to .nc, size=1.2M, smol like I says) cube_to_intersect_smol.nc.txt

Have I looked at docs?

Yes https://scitools-iris.readthedocs.io/en/latest/generated/api/iris.cube.html#iris.cube.Cube.intersection no mention of any side effects, coordinates are circular

Iris version and installation type

(esmvaltool) valeriu@valeriu-PORTEGE-Z30-C:~$ conda list iris
# packages in environment at /home/valeriu/miniconda3/envs/esmvaltool:
#
# Name                    Version                   Build  Channel
iris                      3.6.1              pyha770c72_0    conda-forge
iris-esmf-regrid          0.7.0              pyhd8ed1ab_0    conda-forge

AOBs

Related to https://github.com/SciTools/iris/issues/3483

Great many thanks in advance! Many thanks to @rebeccaherman1 for alerting me to this via https://github.com/ESMValGroup/ESMValCore/issues/2162

Obligatory pint emoji

:beer:

rebeccaherman1 commented 1 year ago

@valeriupredoi in your post, the second region (reproduced below) retains the ancillary variables, right? I think you forgot to change a word in the code comment.

# intersection cube loses ancillary variables
extract_region(cube,
               start_longitude=160,
               end_longitude=280,
               start_latitude=-5,
               end_latitude=5
    )

Thanks for looking into it and submitting this issue!

valeriupredoi commented 1 year ago

well spotted @rebeccaherman1 - corrected now, cheers :+1:

valeriupredoi commented 1 month ago

cheers folks :beer: