Skip to content

Can't get area weights of cube with additional auxillary coordinates of lat and lon  #3916

@nhsavage

Description

@nhsavage

🐛 Bug Report

Iris is unable to calculate area weights for rotated pole grids if auxiliary coordinates with true latitude and longitude have been added to the cube.

How To Reproduce

  1. load data on a rotated pole grid
# load rotated pole grids
fname = iris.sample_data_path("rotated_pole.nc")
cube = iris.load_cube(fname)
  1. attach the aux coords thus:
# get cube's coordinate system
cs = cube.coord_system()
# get coord names
# Longitude
xcoord = cube.coord(axis="X", dim_coords=True)
# Latitude
ycoord = cube.coord(axis="Y", dim_coords=True)

# read in the grid lat/lon points from the cube
glat = cube.coord(ycoord).points
glon = cube.coord(xcoord).points

# create a rectangular grid out of an array of
# glon and glat values, shape will be len(glat)xlen(glon)
x, y = np.meshgrid(glon, glat)

# get the cube dimensions which corresponds to glon and glat
x_dim = cube.coord_dims(xcoord)[0]
y_dim = cube.coord_dims(ycoord)[0]

# define two new variables to hold the unrotated coordinates
rlongitude, rlatitude = iris.analysis.cartography.unrotate_pole(
    x, y, cs.grid_north_pole_longitude, cs.grid_north_pole_latitude
)

# create two new auxillary coordinates to hold
# the values of the unrotated coordinates
reg_long = iris.coords.AuxCoord(rlongitude, long_name="longitude", units="degrees")
reg_lat = iris.coords.AuxCoord(rlatitude, long_name="latitude", units="degrees")

# add two auxilary coordinates to the cube holding
# regular(unrotated) lat/lon values
cube.add_aux_coord(reg_long, [y_dim, x_dim])
cube.add_aux_coord(reg_lat, [y_dim, x_dim])

  1. print the cube:
air_temperature / (K)               (grid_latitude: 550; grid_longitude: 700)
     Dimension coordinates:
          grid_latitude                           x                    -
          grid_longitude                          -                    x
     Auxiliary coordinates:
          latitude                                x                    x
          longitude                               x                    x

4. try and get area weights:
weights =iris.analysis.cartography.area_weights(cube)

Traceback (most recent call last):
File "area_bug.py", line 49, in
weights =iris.analysis.cartography.area_weights(cube)
File ".../lib/python3.6/site-packages/iris/analysis/cartography.py", line 399, in area_weights
lon, lat = _get_lon_lat_coords(cube)
File ".../python3.6/site-packages/iris/analysis/cartography.py", line 189, in _get_lon_lat_coords
"Calling _get_lon_lat_coords with multiple lat or lon coords"
ValueError: Calling _get_lon_lat_coords with multiple lat or lon coords is currently disallowed

5. remove the extra coords - works fine:

cube.remove_coord('longitude')
cube.remove_coord('latitude')
weights =iris.analysis.cartography.area_weights(cube)

Expected behaviour

I expect that a cube like this should return the correct area weights

Environment

  • RHEL 7.9
  • Iris Version: 2.2.0

Additional context

Click to expand this section...
https://github.com/SciTools/iris/blob/master/lib/iris/analys... 

I would suggest that this: 

coord for coord in cube.coords() if "latitude" in coord.name() 

 

should actually be: 

coord for coord in cube.coords(dim_coords=True) if "latitude" in coord.name()

See here for further details.

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions