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

Adds handling of a model-id-attr to wxcode-modal #1634

Merged
merged 12 commits into from
Jan 18, 2022
Merged
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
8 changes: 6 additions & 2 deletions improver/cli/wxcode_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

@cli.clizefy
@cli.with_output
def process(*cubes: cli.inputcube):
def process(*cubes: cli.inputcube, model_id_attr: str = None):
"""Generates a modal weather symbol for the period covered by the input
weather symbol cubes. Where there are different weather codes available
for night and day, the modal code returned is always a day code, regardless
Expand All @@ -46,6 +46,10 @@ def process(*cubes: cli.inputcube):
cubes (iris.cube.CubeList):
A cubelist containing weather symbols cubes that cover the period
over which a modal symbol is desired.
model_id_attr (str):
Name of attribute recording source models that should be
inherited by the output cube. The source models are expected as
a space-separated string.

Returns:
iris.cube.Cube:
Expand All @@ -56,4 +60,4 @@ def process(*cubes: cli.inputcube):
if not cubes:
raise RuntimeError("Not enough input arguments. See help for more information.")

return ModalWeatherCode()(cubes)
return ModalWeatherCode(model_id_attr=model_id_attr)(cubes)
24 changes: 22 additions & 2 deletions improver/wxcode/modal_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,20 @@ class ModalWeatherCode(BasePlugin):
covered by the input files.
"""

def __init__(self):
"""Create an aggregator instance for reuse"""
def __init__(self, model_id_attr: str = None):
"""
Set up plugin and create an aggregator instance for reuse

Args:
model_id_attr:
Name of attribute recording source models that should be
inherited by the output cube. The source models are expected as
a space-separated string.
"""
self.aggregator_instance = Aggregator("mode", self.mode_aggregator)

self.model_id_attr = model_id_attr

# Create the expected cell method for use with single cube inputs
# that do not pass through the aggregator.
self.mode_cell_method = iris.coords.CellMethod("mode", coords="time")
Expand Down Expand Up @@ -205,6 +215,16 @@ def process(self, cubes: CubeList) -> Cube:
result = cube.collapsed("time", self.aggregator_instance)
self._set_blended_times(result)

if self.model_id_attr:
# Update contributing models
contributing_models = set()
for source_cube in cubes:
for model in source_cube.attributes[self.model_id_attr].split(" "):
contributing_models.update([model])
result.attributes[self.model_id_attr] = " ".join(
sorted(list(contributing_models))
)

# Handle any unset points where it was hard to determine a suitable mode
if (result.data == UNSET_CODE_INDICATOR).any():
self._group_codes(result, cube)
Expand Down
Loading