Skip to content

Allow equal duration_subdivision period and fidelity arguments#2322

Open
maxwhitemet wants to merge 7 commits intometoppv:masterfrom
maxwhitemet:duration_subdivision_update
Open

Allow equal duration_subdivision period and fidelity arguments#2322
maxwhitemet wants to merge 7 commits intometoppv:masterfrom
maxwhitemet:duration_subdivision_update

Conversation

@maxwhitemet
Copy link
Contributor

Addresses #1013

Currently, a user of the DurationSubdivision plugin looking to divide an accumulation diagnostic cube (e.g. 3 hourly precip to 1 hourly) needs to specify both a target_period and fidelity argument, with the former being the time period each output will span (e.g. 1hr), and the latter the shortest increment into which the input cube is divided before being aggregated into the output periods.

This fidelity argument was designed primarily with sunshine duration in mind and is only useful when used with a night mask or day mask to help demarcate where day/night is during each fidelity period, zeroing points where the mask indicates night.

The existing implementation fails when fidelity == target_period, forcing users to set fidelity to values smaller than target_period (e.g., 1800s instead of 3600s), increasing processing time. Thus this PR makes a minor change to allow fidelity to be equal to target_period.

Copy link
Contributor

@gavinevans gavinevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @maxwhitemet 👍

I've added a couple of minor comments.

Copy link
Contributor

@gavinevans gavinevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, @maxwhitemet 👍

I've added a few minor comments.

Copy link
Contributor

@gavinevans gavinevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @maxwhitemet 👍

Just one suggestion.

Comment on lines +2193 to +2227

new_period_cubes = iris.cube.CubeList()

# The cycle times are already the same. However, below we use this variable
# to recalculate the forecast periods relative to the cycletime for each of our
# extracted shorter duration cubes.
cycle_time = fidelity_period_cube.coord("forecast_reference_time").cell(0).point

new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()
# If the fidelity is the same as the target period, we can skip the step of
# summing up the fidelity period cubes into target period cubes seen in the else
# statement below and just enforce the time point standard and unify cycletime
# on the fidelity period cubes.
if self.fidelity == self.target_period:
for time_slice in fidelity_period_cube.slices_over("time"):
enforce_time_point_standard(time_slice)
new_period_cubes.append(time_slice)

new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()

else:
while start_time < end_time:
period_constraint = iris.Constraint(
time=lambda cell: start_time
<= cell.bound[0]
< start_time + interval
)
components = fidelity_period_cube.extract(period_constraint)
component_cube = components.collapsed("time", iris.analysis.SUM)
enforce_time_point_standard(component_cube)
new_period_cubes.append(component_cube)
start_time += interval

new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could save a few lines by just calling unify_cycletime once.

Suggested change
new_period_cubes = iris.cube.CubeList()
# The cycle times are already the same. However, below we use this variable
# to recalculate the forecast periods relative to the cycletime for each of our
# extracted shorter duration cubes.
cycle_time = fidelity_period_cube.coord("forecast_reference_time").cell(0).point
new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()
# If the fidelity is the same as the target period, we can skip the step of
# summing up the fidelity period cubes into target period cubes seen in the else
# statement below and just enforce the time point standard and unify cycletime
# on the fidelity period cubes.
if self.fidelity == self.target_period:
for time_slice in fidelity_period_cube.slices_over("time"):
enforce_time_point_standard(time_slice)
new_period_cubes.append(time_slice)
new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()
else:
while start_time < end_time:
period_constraint = iris.Constraint(
time=lambda cell: start_time
<= cell.bound[0]
< start_time + interval
)
components = fidelity_period_cube.extract(period_constraint)
component_cube = components.collapsed("time", iris.analysis.SUM)
enforce_time_point_standard(component_cube)
new_period_cubes.append(component_cube)
start_time += interval
new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()
new_period_cubes = iris.cube.CubeList()
# If the fidelity is the same as the target period, we can skip the step of
# summing up the fidelity period cubes into target period cubes seen in the else
# statement below and just enforce the time point standard on the fidelity period
# cubes.
if self.fidelity == self.target_period:
for time_slice in fidelity_period_cube.slices_over("time"):
enforce_time_point_standard(time_slice)
new_period_cubes.append(time_slice)
else:
while start_time < end_time:
period_constraint = iris.Constraint(
time=lambda cell: start_time
<= cell.bound[0]
< start_time + interval
)
components = fidelity_period_cube.extract(period_constraint)
component_cube = components.collapsed("time", iris.analysis.SUM)
enforce_time_point_standard(component_cube)
new_period_cubes.append(component_cube)
start_time += interval
# Recalculate the forecast periods relative to the cycletime for each
# of the target period cubes.
cycle_time = fidelity_period_cube.coord("forecast_reference_time").cell(0).point
new_period_cubes = unify_cycletime(new_period_cubes, cycle_time)
return new_period_cubes.merge_cube()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants