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

PP Proleptic Gregorian #5138

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/iris/fileformats/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ def core_data(self):
def calendar(self):
"""Return the calendar of the field."""
# TODO #577 What calendar to return when ibtim.ic in [0, 3]
calendar = cf_units.CALENDAR_STANDARD
calendar = cf_units.CALENDAR_PROLEPTIC_GREGORIAN
if self.lbtim.ic == 2:
calendar = cf_units.CALENDAR_360_DAY
elif self.lbtim.ic == 4:
Expand Down
11 changes: 9 additions & 2 deletions lib/iris/fileformats/pp_load_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ def _new_coord_and_dims(
a new (coordinate, dims) pair.

"""
if (
units.is_time_reference()
and units.calendar == cf_units.CALENDAR_PROLEPTIC_GREGORIAN
):
units = cf_units.Unit(
"hours since epoch", calendar=cf_units.CALENDAR_STANDARD
)
bounds = lower_and_upper_bounds
if is_vector_operation:
dims, points, bounds = _reduce_points_and_bounds(points, bounds)
Expand Down Expand Up @@ -548,7 +555,7 @@ def _epoch_date_hours_internals(epoch_hours_unit, datetime):
if m == 0:
# Add a 'January', by changing month=0 to 1.
m = 1
if calendar == cf_units.CALENDAR_STANDARD:
if calendar == cf_units.CALENDAR_PROLEPTIC_GREGORIAN:
days_offset += 31
elif calendar == cf_units.CALENDAR_360_DAY:
days_offset += 30
Expand All @@ -561,7 +568,7 @@ def _epoch_date_hours_internals(epoch_hours_unit, datetime):
if y == 0:
# Add a 'Year 0', by changing year=0 to 1.
y = 1
if calendar == cf_units.CALENDAR_STANDARD:
if calendar == cf_units.CALENDAR_PROLEPTIC_GREGORIAN:
days_in_year_0 = 366
elif calendar == cf_units.CALENDAR_360_DAY:
days_in_year_0 = 360
Expand Down
71 changes: 35 additions & 36 deletions lib/iris/fileformats/pp_save_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ def _general_time_rules(cube, pp):
frt_coord = scalar_coord(cube, "forecast_reference_time")
clim_season_coord = scalar_coord(cube, "clim_season")

if time_coord is not None:
time_unit = time_coord.units
if time_unit.calendar == "standard":
time_unit = time_unit.change_calendar("proleptic_gregorian")

cm_time_mean = scalar_cell_method(cube, "mean", "time")
cm_time_min = scalar_cell_method(cube, "minimum", "time")
cm_time_max = scalar_cell_method(cube, "maximum", "time")
Expand All @@ -122,7 +127,7 @@ def _general_time_rules(cube, pp):
if time_coord is not None and fp_coord is None and frt_coord is None:
pp.lbtim.ia = 0
pp.lbtim.ib = 0
pp.t1 = time_coord.units.num2date(time_coord.points[0])
pp.t1 = time_unit.num2date(time_coord.points[0])
pp.t2 = cftime.datetime(0, 0, 0, calendar=None, has_year_zero=True)

# Forecast.
Expand All @@ -133,10 +138,8 @@ def _general_time_rules(cube, pp):
):
pp.lbtim.ia = 0
pp.lbtim.ib = 1
pp.t1 = time_coord.units.num2date(time_coord.points[0])
pp.t2 = time_coord.units.num2date(
time_coord.points[0] - fp_coord.points[0]
)
pp.t1 = time_unit.num2date(time_coord.points[0])
pp.t2 = time_unit.num2date(time_coord.points[0] - fp_coord.points[0])
pp.lbft = fp_coord.points[0]

# Time mean (non-climatological).
Expand All @@ -151,8 +154,8 @@ def _general_time_rules(cube, pp):
# XXX How do we know *which* time to use if there are more than
# one? *Can* there be more than one?
pp.lbtim.ib = 2
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")

if (
Expand All @@ -164,8 +167,8 @@ def _general_time_rules(cube, pp):
):
# Handle missing forecast period using time and forecast ref time.
pp.lbtim.ib = 2
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
stop = time_coord.units.convert(
time_coord.bounds[0, 1], "hours since epoch"
)
Expand All @@ -181,8 +184,8 @@ def _general_time_rules(cube, pp):
and cm_time_mean is not None
):
pp.lbtim.ib = 2
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])

if (
time_coord is not None
Expand Down Expand Up @@ -238,12 +241,8 @@ def _general_time_rules(cube, pp):
pp.lbtim.ia = int(cm_time_max.intervals[0][:-5])

if time_coord is not None and time_coord.has_bounds():
lower_bound_yr = time_coord.units.num2date(
time_coord.bounds[0, 0]
).year
upper_bound_yr = time_coord.units.num2date(
time_coord.bounds[0, 1]
).year
lower_bound_yr = time_unit.num2date(time_coord.bounds[0, 0]).year
upper_bound_yr = time_unit.num2date(time_coord.bounds[0, 1]).year
else:
lower_bound_yr = None
upper_bound_yr = None
Expand All @@ -261,8 +260,8 @@ def _general_time_rules(cube, pp):
# Climatological time mean - single year.
pp.lbtim.ia = 0
pp.lbtim.ib = 2
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")

elif (
Expand All @@ -278,19 +277,19 @@ def _general_time_rules(cube, pp):
# Climatological time mean - spanning years - djf.
pp.lbtim.ia = 0
pp.lbtim.ib = 3
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
if pp.t1.month == 12:
pp.t1 = cftime.datetime(pp.t1.year, 12, 1, 0, 0, 0)
else:
pp.t1 = cftime.datetime(pp.t1.year - 1, 12, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 3, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
time_coord.bounds[0, 0] != time_unit.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
time_coord.bounds[0, 1] != time_unit.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
Expand All @@ -309,16 +308,16 @@ def _general_time_rules(cube, pp):
pp.lbtim.ia = 0
pp.lbtim.ib = 3
# TODO: wut?
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
pp.t1 = cftime.datetime(pp.t1.year, 3, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 6, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
time_coord.bounds[0, 0] != time_unit.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
time_coord.bounds[0, 1] != time_unit.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
Expand All @@ -337,16 +336,16 @@ def _general_time_rules(cube, pp):
pp.lbtim.ia = 0
pp.lbtim.ib = 3
# TODO: wut?
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
pp.t1 = cftime.datetime(pp.t1.year, 6, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 9, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
time_coord.bounds[0, 0] != time_unit.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
time_coord.bounds[0, 1] != time_unit.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
Expand All @@ -365,16 +364,16 @@ def _general_time_rules(cube, pp):
pp.lbtim.ia = 0
pp.lbtim.ib = 3
# TODO: wut?
pp.t1 = time_coord.units.num2date(time_coord.bounds[0, 0])
pp.t2 = time_coord.units.num2date(time_coord.bounds[0, 1])
pp.t1 = time_unit.num2date(time_coord.bounds[0, 0])
pp.t2 = time_unit.num2date(time_coord.bounds[0, 1])
pp.t1 = cftime.datetime(pp.t1.year, 9, 1, 0, 0, 0)
pp.t2 = cftime.datetime(pp.t2.year, 12, 1, 0, 0, 0)
_conditional_warning(
time_coord.bounds[0, 0] != time_coord.units.date2num(pp.t1),
time_coord.bounds[0, 0] != time_unit.date2num(pp.t1),
"modified t1 for climatological seasonal mean",
)
_conditional_warning(
time_coord.bounds[0, 1] != time_coord.units.date2num(pp.t2),
time_coord.bounds[0, 1] != time_unit.date2num(pp.t2),
"modified t2 for climatological seasonal mean",
)
pp.lbft = fp_coord.units.convert(fp_coord.bounds[0, 1], "hours")
Expand All @@ -398,7 +397,7 @@ def _calendar_rules(cube, pp):
if time_coord is not None:
if time_coord.units.calendar == "360_day":
pp.lbtim.ic = 2
elif time_coord.units.calendar == "standard":
elif time_coord.units.calendar in ["standard", "proleptic_gregorian"]:
pp.lbtim.ic = 1
elif time_coord.units.calendar == "365_day":
pp.lbtim.ic = 4
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/tests/unit/fileformats/pp/test_PPField.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Test_calendar(tests.IrisTest):
def test_greg(self):
field = DummyPPField()
field.lbtim = SplittableInt(1, {"ia": 2, "ib": 1, "ic": 0})
self.assertEqual(field.calendar, "standard")
self.assertEqual(field.calendar, "proleptic_gregorian")

def test_360(self):
field = DummyPPField()
Expand Down
Loading