Convert calendar using iris

653 views Asked by At

How can I convert a calendar with Iris? For example, converting a 'gregorian' calendar to '365_day'?

At the moment, I am doing

original_tcoord = cube_in.coords()[tcoord_idx]

tmp_time = cf_units.num2date(
    original_tcoord.points, 
    original_tcoord.units.name, 
    target_calendar,
)

new_time = cf_units.date2num(
    tmp_time, 
    original_tcoord.units.name, 
    target_calendar,
)
new_unit = cf_units.Unit(
    original_tcoord.units.name,
    calendar=target_calendar
)
new_tcoord = iris.coords.DimCoord(
    new_time,
    standard_name=original_tcoord.standard_name, 
    long_name=original_tcoord.long_name, 
    var_name=original_tcoord.var_name,
    units=new_unit,
)

cube_adjusted = cube_in.copy()
cube_adjusted.remove_coord('time')
cube_adjusted.add_dim_coord(
    new_tcoord, 
    tcoord_idx
)

Surely this isn't the fastest way?

1

There are 1 answers

3
Nick Savage On BEST ANSWER

This can probably be done more simply, just by replacing the units on your existing coordinate:

tcoord = cube.coord('time')
tcoord.units = cf_units.Unit(tcoord.units.origin, calendar='gregorian')

this suggestion comes from Ruth Comer not me, but I am re-posting here as suggested.