Skip to content

Commit 66d7758

Browse files
authored
Set the calendar to Gregorian but replace the bounds (#125)
* Save using the Gregorian calendar * Try replacing time bounds * Indent correctly
1 parent 7d75d0b commit 66d7758

4 files changed

Lines changed: 39 additions & 11 deletions

File tree

CMIP7/esm1p6/atmosphere/cmip7_ancil_common.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def esm_grid_mask_cube(args):
4545
return cube
4646

4747

48-
def set_gregorian(var):
48+
def set_gregorian(var, replace_bounds=False):
4949
# Change the calendar to Gregorian for the model
5050
time = var.coord("time")
5151
origin = time.units.origin
@@ -59,17 +59,43 @@ def set_gregorian(var):
5959
date.year, date.month, date.day, date.hour, date.minute, date.second
6060
)
6161
tvals[i] = newunits.date2num(newdate)
62-
for j in range(2):
63-
date = time.units.num2date(tbnds[i][j])
64-
newdate = cftime.DatetimeProlepticGregorian(
62+
if replace_bounds:
63+
beg_date = cftime.DatetimeProlepticGregorian(
6564
date.year,
6665
date.month,
67-
date.day,
66+
1,
6867
date.hour,
6968
date.minute,
7069
date.second,
7170
)
72-
tbnds[i][j] = newunits.date2num(newdate)
71+
tbnds[i][0] = newunits.date2num(beg_date)
72+
if date.month == 12:
73+
end_year = date.year + 1
74+
end_month = 1
75+
else:
76+
end_year = date.year
77+
end_month = date.month + 1
78+
end_date = cftime.DatetimeProlepticGregorian(
79+
end_year,
80+
end_month,
81+
1,
82+
date.hour,
83+
date.minute,
84+
date.second,
85+
)
86+
tbnds[i][1] = newunits.date2num(end_date)
87+
else:
88+
for j in range(2):
89+
date = time.units.num2date(tbnds[i][j])
90+
newdate = cftime.DatetimeProlepticGregorian(
91+
date.year,
92+
date.month,
93+
date.day,
94+
date.hour,
95+
date.minute,
96+
date.second,
97+
)
98+
tbnds[i][j] = newunits.date2num(newdate)
7399
time.points = tvals
74100
time.bounds = tbnds
75101
time.units = newunits
@@ -134,7 +160,9 @@ def zero_poles(cube):
134160
cube.data[:, -1] = 0.0
135161

136162

137-
def save_ancil(cubes, save_dirpath, save_filename, gregorian=True):
163+
def save_ancil(
164+
cubes, save_dirpath, save_filename, gregorian=True, replace_bounds=False
165+
):
138166
"""
139167
Handle both a list and a single cube
140168
"""
@@ -148,7 +176,7 @@ def save_ancil(cubes, save_dirpath, save_filename, gregorian=True):
148176
cube.attributes["grid_staggering"] = 3 # New dynamics
149177
if gregorian:
150178
cube.attributes["time_type"] = 1 # Gregorian
151-
set_gregorian(cube)
179+
set_gregorian(cube, replace_bounds=replace_bounds)
152180
"""
153181
ANTS doesn't set the calendar header for monthly fields
154182
See fileformats/ancil/time_headers.py

CMIP7/esm1p6/atmosphere/ozone/cmip7_HI_ozone_generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def esm_hi_ozone_save_dirpath(args):
3939
def save_cmip7_hi_ozone(args, cube):
4040
# Save as an ancillary file
4141
save_dirpath = esm_hi_ozone_save_dirpath(args)
42-
save_ancil(cube, save_dirpath, args.save_filename, gregorian=False)
42+
save_ancil(cube, save_dirpath, args.save_filename, replace_bounds=True)
4343

4444

4545
if __name__ == "__main__":

CMIP7/esm1p6/atmosphere/ozone/cmip7_PI_mean_ozone_generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def save_cmip7_pi_mean_ozone(args, cube):
5353
cube.remove_coord("month_number")
5454
# Save as an ancillary file
5555
save_dirpath = esm_pi_ozone_save_dirpath(args)
56-
save_ancil(cube, save_dirpath, args.save_filename, gregorian=False)
56+
save_ancil(cube, save_dirpath, args.save_filename, replace_bounds=True)
5757

5858

5959
if __name__ == "__main__":

CMIP7/esm1p6/atmosphere/ozone/cmip7_PI_ozone_generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def esm_pi_ozone_save_dirpath(args):
3939
def save_cmip7_pi_ozone(args, cube):
4040
# Save as an ancillary file
4141
save_dirpath = esm_pi_ozone_save_dirpath(args)
42-
save_ancil(cube, save_dirpath, args.save_filename, gregorian=False)
42+
save_ancil(cube, save_dirpath, args.save_filename, replace_bounds=True)
4343

4444

4545
if __name__ == "__main__":

0 commit comments

Comments
 (0)