From dc67866c9d833359d309fa00ec4fe846682d0264 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Tue, 23 Jul 2024 16:25:12 +0100 Subject: [PATCH] Style --- improver/utilities/spatial.py | 25 +++++++++---------- ...test_GradientBetweenAdjacentGridSquares.py | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/improver/utilities/spatial.py b/improver/utilities/spatial.py index 513e9c1e86..e392cedb8a 100644 --- a/improver/utilities/spatial.py +++ b/improver/utilities/spatial.py @@ -6,25 +6,20 @@ import copy import warnings -from typing import List, Optional, Tuple, Union from abc import ABC, abstractmethod +from typing import List, Optional, Tuple, Union import cartopy.crs as ccrs -from cartopy.crs import CRS +import iris import netCDF4 import numpy as np -from numpy import ndarray -from numpy.ma import MaskedArray +from cartopy.crs import CRS from cf_units import Unit -import iris -from iris.coord_systems import GeogCS +from iris.coord_systems import CoordSystem, GeogCS from iris.coords import AuxCoord, CellMethod, Coord, DimCoord from iris.cube import Cube, CubeList -from iris.coord_systems import ( - CoordSystem, - GeogCS, -) - +from numpy import ndarray +from numpy.ma import MaskedArray from scipy.ndimage.filters import maximum_filter from scipy.stats import circmean @@ -203,14 +198,18 @@ def get_midpoints(axis: Coord) -> np.ndarray: if axis.circular: endpoints_mean = np.deg2rad((axis.points[0] + axis.points[-1]) / 2) + # Force angle to sit on the upper quadrant so that eg. for endpoints of 10 degrees, + # and 350 degrees, midpoint is zero degrees rather than 180. extra_point = np.rad2deg( np.arctan(np.sin(endpoints_mean) / np.cos(endpoints_mean)) - ) # Forces angle to sit on the upper quadrant so that eg. for endpoints of 10 degrees, and 350 degrees, midpoint is zero degrees rather than 180. + ) if extra_point < 0: extra_point += 360 # Forces angle to be between 0 and 360 + # Stable sort fasted in this case, where list is already nearly sorted with only the + # one out-of-order element. midpoints = np.sort( np.hstack((midpoints, np.array(extra_point))), kind="stable" - ) # Stable sort fasted in this case, where list is already nearly sorted with only the one out-of-order element. + ) return midpoints.astype(axis.dtype) diff --git a/improver_tests/utilities/spatial/test_GradientBetweenAdjacentGridSquares.py b/improver_tests/utilities/spatial/test_GradientBetweenAdjacentGridSquares.py index d472c24d9d..f571ca780d 100644 --- a/improver_tests/utilities/spatial/test_GradientBetweenAdjacentGridSquares.py +++ b/improver_tests/utilities/spatial/test_GradientBetweenAdjacentGridSquares.py @@ -12,7 +12,6 @@ from improver.synthetic_data.set_up_test_cubes import set_up_variable_cube from improver.utilities.spatial import GradientBetweenAdjacentGridSquares - EXAMPLE_INPUT_DATA_1 = np.array( [[0, 100, 200], [300, 400, 500], [400, 500, 600]], dtype=np.float32 )