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

More cli refactoring #2075

Merged
merged 6 commits into from
Jan 21, 2025
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
12 changes: 0 additions & 12 deletions improver/cli/time_lagged_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ def process(*cubes: cli.inputcube):
Raises:
ValueError: If ensembles have mismatched validity times
"""
import warnings

from improver.utilities.time_lagging import GenerateTimeLaggedEnsemble

if len(cubes) == 1:
warnings.warn("Only a single cube input, so time lagging will have no effect.")
return cubes[0]

# raise error if validity times are not all equal
time_coords = [cube.coord("time") for cube in cubes]
time_coords_match = [coord == time_coords[0] for coord in time_coords]
if not all(time_coords_match):
raise ValueError("Cubes with mismatched validity times are not compatible.")

return GenerateTimeLaggedEnsemble()(cubes)
1 change: 0 additions & 1 deletion improver/cli/vicinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ def process(cube: cli.inputcube, vicinity: cli.comma_separated_list = None):
"""
from improver.utilities.spatial import OccurrenceWithinVicinity

vicinity = [float(x) for x in vicinity]
return OccurrenceWithinVicinity(radii=vicinity).process(cube)
3 changes: 1 addition & 2 deletions improver/cli/wind_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ def process(wind_direction: cli.inputcube, *, backup_method="neighbourhood"):
"""
from improver.wind_calculations.wind_direction import WindDirection

result = WindDirection(backup_method=backup_method)(wind_direction)
return result
return WindDirection(backup_method=backup_method)(wind_direction)
3 changes: 1 addition & 2 deletions improver/cli/wind_gust_diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def process(
"""
from improver.wind_calculations.wind_gust_diagnostic import WindGustDiagnostic

result = WindGustDiagnostic(wind_gust_percentile, wind_speed_percentile)(
return WindGustDiagnostic(wind_gust_percentile, wind_speed_percentile)(
wind_gust, wind_speed
)
return result
5 changes: 4 additions & 1 deletion improver/utilities/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def create_difference_cube(
points = axis.points
if self._axis_wraps_around_meridian(axis, cube):
points = np.hstack((points, 360 + points[0]))
if type(axis.coord_system) != GeogCS:
if type(axis.coord_system) is not GeogCS:
raise NotImplementedError(
"DifferenceBetweenAdjacentGridSquares does not support cubes with "
"circular x-axis that do not use a geographic (i.e. latlon) coordinate system."
Expand Down Expand Up @@ -871,6 +871,9 @@ def __init__(
ValueError: If a provided vicinity radius is negative.
ValueError: Land mask not named land_binary_mask.
"""
if radii:
radii = [float(x) for x in radii]

if radii and grid_point_radii:
raise ValueError(
"Vicinity processing requires that only one of radii or "
Expand Down
19 changes: 17 additions & 2 deletions improver/utilities/time_lagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
# See LICENSE in the root of the repository for full licensing details.
"""Provide support utilities for time lagging ensembles"""

from typing import List, Union
import warnings
from typing import Union

import numpy as np
from iris.cube import Cube, CubeList

from improver import BasePlugin
from improver.metadata.forecast_times import rebadge_forecasts_as_latest_cycle
from improver.utilities.common_input_handle import as_cubelist
from improver.utilities.cube_manipulation import MergeCubes


class GenerateTimeLaggedEnsemble(BasePlugin):
"""Combine realizations from different forecast cycles into one cube"""

def process(self, cubelist: Union[List[Cube], CubeList]) -> Cube:
def process(self, *cubes: Union[Cube, CubeList]) -> Cube:
"""
Take an input cubelist containing forecasts from different cycles and
merges them into a single cube.
Expand All @@ -36,6 +38,19 @@ def process(self, cubelist: Union[List[Cube], CubeList]) -> Cube:
Returns:
Concatenated forecasts
"""
cubelist = as_cubelist(cubes)
if len(cubelist) == 1:
Copy link
Contributor

@cpelley cpelley Jan 10, 2025

Choose a reason for hiding this comment

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

Not a required change, but I typically would use as_cubelist(cubes) before this and adding *cubes to the args of the function. This makes it a bit more flexible to how one calls and interacts with the plugin.

warnings.warn(
"Only a single cube input, so time lagging will have no effect."
)
return cubelist[0]

# raise error if validity times are not all equal
time_coords = [cube.coord("time") for cube in cubelist]
time_coords_match = [coord == time_coords[0] for coord in time_coords]
if not all(time_coords_match):
raise ValueError("Cubes with mismatched validity times are not compatible.")

cubelist = rebadge_forecasts_as_latest_cycle(cubelist)

# Take all the realizations from all the input cube and
Expand Down
2 changes: 1 addition & 1 deletion improver_tests/regrid/test_RegridLandSea.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_basic_regrid(self):
def test_access_regrid_with_landmask(self):
"""Test the AdjustLandSeaPoints module is correctly called when using
landmask arguments. Diagnosed by identifiable error."""
msg = "Distance of 1000m gives zero cell extent"
msg = "Distance of 1000.0m gives zero cell extent"
with self.assertRaisesRegex(ValueError, msg):
RegridLandSea(
regrid_mode="nearest-with-mask",
Expand Down