Skip to content

aggregated_by() - time coords do not match expected output. #4665

@mokhodge

Description

@mokhodge

🐛 Bug Report

This might link to #4455. When aggregate by month_number the time coords of the outputted cube are not always in the order you would expect yet the corresponding cube.data is.

How To Reproduce

filepath included as an example, have tried on other files and same behaviour occurs

import iris.analysis
import matplotlib.pyplot as plt
import iris.quickplot as qplt
import iris.coord_categorisation
import iris.cube

fname = f'/home/h03/hadmi/Python/MedGOLD/Colombia/data_files/pr/pr_CHIRPS' \
        f'_1981-2010_regridded_colombia.nc'
cube = iris.load_cube(fname)
iris.coord_categorisation.add_month_number(cube, 'time',
                                               name='month_number')
ycon1 = iris.Constraint(
        time=lambda t: 1990 <= t.point.year <= 2010)
ycon2 = iris.Constraint(
        time=lambda t: 1991 <= t.point.year <= 2010)

cube1 = cube.extract(ycon1)
cube2 = cube.extract(ycon2)
monthly_clim1 = cube1.aggregated_by('month_number', iris.analysis.MEAN)
monthly_clim2 = cube2.aggregated_by('month_number', iris.analysis.MEAN)

fig = plt.figure()
ax = fig.add_subplot(2,2,1)
qplt.plot(monthly_clim1[:, 16, 22])
ax.set_title('Clim 1990 -2010')
ax.set_ylabel('mm/month') # this shows the monthly_clim1 cube starts in Jan as expected

bx = fig.add_subplot(2,2,2)
qplt.plot(monthly_clim2[:, 16, 22])
bx.set_title('Clim 1991 -2010')
bx.set_ylabel('mm/month') # this shows the monthly_clim2 cube starts in July (not expected). It appears the time coords just comes from the middle of the original time coords

plt.show()

#### Show the cube.data always starts from Jan even if cube.coord('time') says it is July
t_cube = cube2.copy()
t_cube = t_cube[:, 16, 22]
print(t_cube.coord('time')) #starts in Jan

DimCoord([1991-01-01 00:00:00, 1991-02-01 00:00:00, 1991-03-01 00:00:00,
1991-04-01 00:00:00, 1991-05-01 00:00:00, 1991-06-01 00:00:00,
1991-07-01 00:00:00, 1991-08-01 00:00:00, 1991-09-01 00:00:00,
1991-10-01 00:00:00, 1991-11-01 00:00:00, 1991-12-01 00:00:00,

# assign some dummy data to t_cube to see what is going on
blank = []
list_num = list(range(1,13))
for i in list(range(1,21)):
    blank.extend(list_num)

t_cube.data = blank
agg_cube = t_cube.aggregated_by('month_number', iris.analysis.MEAN)
print(agg_cube.coord('time')) # can see the time coord starts in July, can also see you get two time.points for Aug (08) and a missing Feb (02)

DimCoord([2000-07-02 00:00:00, 2000-08-02 00:00:00, 2000-08-30 00:00:00,
2000-09-30 00:00:00, 2000-10-30 00:00:00, 2000-11-30 00:00:00,
2000-12-30 00:00:00, 2001-01-30 00:00:00, 2001-03-02 00:00:00,
2001-04-01 00:00:00, 2001-05-02 00:00:00, 2001-06-01 00:00:00], bounds=[[1991-01-01 00:00:00, 2010-01-01 00:00:00],
[1991-02-01 00:00:00, 2010-02-01 00:00:00],
[1991-03-01 00:00:00, 2010-03-01 00:00:00],
[1991-04-01 00:00:00, 2010-04-01 00:00:00],
[1991-05-01 00:00:00, 2010-05-01 00:00:00],
[1991-06-01 00:00:00, 2010-06-01 00:00:00],
[1991-07-01 00:00:00, 2010-07-01 00:00:00],
[1991-08-01 00:00:00, 2010-08-01 00:00:00],
[1991-09-01 00:00:00, 2010-09-01 00:00:00],
[1991-10-01 00:00:00, 2010-10-01 00:00:00],
[1991-11-01 00:00:00, 2010-11-01 00:00:00],
[1991-12-01 00:00:00, 2010-12-01 00:00:00]], standard_name='time', calendar='gregorian', var_name='time')

print(agg_cube.data) 
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12.]
# from the time coord you would expect something like cube.data = [7 8 9 10 11 12 1 2 3 4 5 6]
# but the output is [1 2 3 4 5 6 7 8 9 10 11 12] which is what you would expect if agg_cube.coord('time') started in Jan

Expected behaviour

if aggregated_by('month_number') and month_number aux coord starts at 1, I would expect the agg_cube.coord('time') to also start in Jan

Screenshots

Environment

  • Iris Version: 3.0.1

Additional context

Click to expand this section...
Please add additional verbose information in this section e.g., code, output, tracebacks, screenshots etc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions