|
10 | 10 | import iris |
11 | 11 | import mule |
12 | 12 | import numpy as np |
13 | | -from cmip7_ancil_constants import UM_VERSION |
| 13 | +from cmip7_ancil_constants import ( |
| 14 | + MONTHS_IN_A_YEAR, |
| 15 | + UM_VERSION, |
| 16 | +) |
14 | 17 |
|
15 | 18 | INTERPOLATION_SCHEME = iris.analysis.AreaWeighted(mdtol=0.5) |
16 | 19 |
|
@@ -72,6 +75,40 @@ def set_gregorian(var): |
72 | 75 | time.units = newunits |
73 | 76 |
|
74 | 77 |
|
| 78 | +def extend_years(cube): |
| 79 | + """ |
| 80 | + Extend a cube representing a monthly time series by duplicating |
| 81 | + and adjusting the first and last years. |
| 82 | + Based on Crown copyright code from ozone_cmip6_ancillary_for_suite.py |
| 83 | + by Steven Hardiman of the UK Met Office. |
| 84 | + """ |
| 85 | + time_coord = cube.coord("time") |
| 86 | + time_points = time_coord.points |
| 87 | + # Do not extend a cube containing less than two years of data. |
| 88 | + if len(time_points) < MONTHS_IN_A_YEAR * 2: |
| 89 | + return cube |
| 90 | + |
| 91 | + # Duplicate the first year. |
| 92 | + length_one_year = time_points[MONTHS_IN_A_YEAR] - time_points[0] |
| 93 | + beg_year = cube[:MONTHS_IN_A_YEAR].copy() |
| 94 | + beg_year_tc = beg_year.coord("time") |
| 95 | + beg_year_tc.points = beg_year_tc.points - length_one_year |
| 96 | + if time_coord.has_bounds(): |
| 97 | + beg_year_tc.bounds = beg_year_tc.bounds - length_one_year |
| 98 | + |
| 99 | + # Duplicate the last year. |
| 100 | + length_one_year = time_points[-1] - time_points[-1 - MONTHS_IN_A_YEAR] |
| 101 | + end_year = cube[-MONTHS_IN_A_YEAR:].copy() |
| 102 | + end_year_tc = end_year.coord("time") |
| 103 | + end_year_tc.points = end_year_tc.points + length_one_year |
| 104 | + if time_coord.has_bounds(): |
| 105 | + end_year_tc.bounds = end_year_tc.bounds + length_one_year |
| 106 | + |
| 107 | + # Return a cube with extended years. |
| 108 | + cubelist = iris.cube.CubeList((beg_year, cube, end_year)) |
| 109 | + return cubelist.concatenate_cube() |
| 110 | + |
| 111 | + |
75 | 112 | def set_coord_system(cube): |
76 | 113 | coord_system = iris.coord_systems.GeogCS(6371229.0) |
77 | 114 | cube.coord("latitude").coord_system = coord_system |
|
0 commit comments