Skip to content

Commit

Permalink
Improve agreement with GEMPAK
Browse files Browse the repository at this point in the history
* Adjust to change to radians at the last possible time. At least in the
GFS case, this keeps the data exactly representable in floating point
(e.g. 0.5, 0.25 spacing), which lets the math work better, or at least
provide uniform output spacing given uniform input.

* Working in degrees makes everything consistent with our handling of
forward_az in the aftermath of the function. If radians=True, then
forward_az comes out in radians.
  • Loading branch information
dopplershift committed Oct 19, 2022
1 parent d2dd039 commit 10307bb
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/metpy/calc/tools.py
Expand Up @@ -868,21 +868,18 @@ def nominal_lat_lon_grid_deltas(longitude, latitude, geod=None):
else:
g = geod

print(g.a)
if longitude.ndim != 1 or latitude.ndim != 1:
raise ValueError(
'Cannot calculate nominal grid spacing from longitude and latitude arguments '
'that are not one dimensional.'
)

dx = units.Quantity(
geod.a * np.diff(longitude.m_as('radian')),
'meter'
)
lat = latitude.m_as('radian')
lon_meridian_diff = np.zeros(len(lat) - 1)
forward_az, _, dy = geod.inv(
lon_meridian_diff, lat[:-1], lon_meridian_diff, lat[1:], radians=True
)
dx = units.Quantity(g.a * np.diff(longitude).m_as('radian'), 'meter')
lat = latitude.m_as('degree')
lon_meridian_diff = np.zeros(len(lat) - 1, dtype=lat.dtype)
forward_az, _, dy = g.inv(lon_meridian_diff, lat[:-1], lon_meridian_diff, lat[1:],
radians=False)
dy[(forward_az < -90.) | (forward_az > 90.)] *= -1
dy = units.Quantity(dy, 'meter')

Expand Down Expand Up @@ -1028,7 +1025,7 @@ def wrapper(f, **kwargs):

def parse_grid_arguments(func):
"""Parse arguments to functions involving derivatives on a grid.
TODO: use this to completely replace add_grid_arguments_from_xarray
"""
# u, v, *, dx=None, dy=None, x_dim=-1, y_dim=-2, longitude=None, latitude=None, crs=None,
Expand Down Expand Up @@ -1148,7 +1145,7 @@ def wrapper(*args, **kwargs):
except:
# Fall back to cartesian calculation if CRS is not provided or invalid
cartesian = True

lat = bound_args.arguments['latitude']
lon = bound_args.arguments['longitude']

Expand Down Expand Up @@ -1524,7 +1521,7 @@ def _vector_derivative(
):
# Determine which derivatives to calculate
derivatives = {
component: None
component: None
for component in ('du/dx', 'du/dy', 'dv/dx', 'dv/dy')
if (return_only is None or component in return_only)
}
Expand Down

0 comments on commit 10307bb

Please sign in to comment.