Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cube.aggregated_by produces wrong bounds in some cases #1528

Closed
pp-mo opened this issue Jan 12, 2015 · 2 comments
Closed

cube.aggregated_by produces wrong bounds in some cases #1528

pp-mo opened this issue Jan 12, 2015 · 2 comments
Assignees

Comments

@pp-mo
Copy link
Member

pp-mo commented Jan 12, 2015

Try the following:

import iris
import iris.cube
import numpy as np

cube = iris.cube.Cube(np.zeros((12, 2, 2)), standard_name='wind_speed', units='m s-1')
co_y = iris.coords.AuxCoord([1996, 1997, 1997, 1996, 1997, 1997, 1996, 1997, 1997, 1996, 1997, 1997], long_name='year')
co_m = iris.coords.AuxCoord(['Dec', 'Jan', 'Feb']*4, long_name='month')
co_r = iris.coords.AuxCoord([2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 1, 1], standard_name=u'realization')
cube.add_aux_coord(co_y, 0)
cube.add_aux_coord(co_m, 0)
cube.add_aux_coord(co_r, 0)

print(cube)

means = cube.aggregated_by(['month','year'],iris.analysis.MEAN)
print('means.coord(\'realization\'):\n', means.coord('realization'))

Result is:

>>> print cube
wind_speed / (m s-1)                (-- : 12; -- : 2; -- : 2)
     Auxiliary coordinates:
          month                         x        -       -
          realization                   x        -       -
          year                          x        -       -
>>> 
>>> means = cube.aggregated_by(['month','year'],iris.analysis.MEAN)
>>> print 'means.coord(\'realization\'):\n', means.coord('realization')
means.coord('realization'):
AuxCoord(array([ 1.5,  1.5,  1.5]), bounds=array([[2, 1],
       [2, 1],
       [2, 1]]), standard_name=u'realization', units=Unit('1'))
>>> 

But we expected

  • all points == 2 (instead of 1.5)
  • all bounds == [1, 4] (instead of [2, 1])

Note:
Trying subsets of the data suggests that it is the last 3 realization values being out-of-order which triggers the problem...

>>> means = cube[:9].aggregated_by(['month','year'],iris.analysis.MEAN)
>>> print means.coord('realization')
AuxCoord(array([ 3.,  3.,  3.]), bounds=array([[2, 4],
       [2, 4],
       [2, 4]]), standard_name=u'realization', units=Unit('1'))
>>> means = cube[6:].aggregated_by(['month','year'],iris.analysis.MEAN)
>>> print means.coord('realization')
AuxCoord(array([ 2.5,  2.5,  2.5]), bounds=array([[4, 1],
       [4, 1],
       [4, 1]]), standard_name=u'realization', units=Unit('1'))
@pp-mo pp-mo changed the title aggregate_by produces wrong bounds in some cases cube.aggregated_by produces wrong bounds in some cases Jan 12, 2015
@rcomer
Copy link
Member

rcomer commented Nov 16, 2018

This has tripped me up a few times, and I had thought that the solution would be to make sure the bounds are always of the form [min, max]. So I started working on a branch. However, some of the existing tests for aggregated_by are expecting to find bounds in the "wrong" order:

So, what is the desired behaviour?

@rcomer
Copy link
Member

rcomer commented Dec 8, 2021

#4315 fixes the bounds problem. The OP also notes that the points are wrong, and I have opened #4455 to more specifically cover that problem. So I think this can be closed.

@rcomer rcomer closed this as completed Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants