Skip to content

Inconsistent arguments for gradient and advection #896

@sgdecker

Description

@sgdecker

The following code:

import numpy as np
import metpy.calc as mpcalc
from metpy.units import units

T = np.array(([1, 2, 3],
              [4, 4, 4],
              [1, 3, 7],
              [0, 2, 6])) * units.K
u = np.array(([5, 4, 4],
              [6, 6, 6],
              [6, 7, 6],
              [5, 5, 5])) * units.m/units.s
v = np.array(([4, 4, 4],
              [3, 2, 3],
              [1, 2, 3],
              [0, 1, 1])) * units.m/units.s

dx = np.array(([12, 10],
               [12, 10],
               [10, 8],
               [10, 8])) * units.m
dy = np.array(([6, 5, 5],
               [5, 5, 4],
               [5, 4, 4])) * units.m

np.set_printoptions(precision=4)

dTdy, dTdx = mpcalc.gradient(T, deltas=(dy, dx))
print(dTdx)
print(dTdy)

adv = mpcalc.advection(T, wind=(u, v), deltas=(dx, dy), dim_order='yx')
print(adv)

accurately computes the temperature gradient and temperature advection corresponding to the sample data provided in 'yx' order.

However, notice that doing this calculation properly requires providing the deltas as (dy, dx) in the gradient call, but (dx, dy) in the advection call. I would have expected the order to be the same for both calls. In particular, since the data is in yx order, I would expect the correct call to advection would include deltas=(dy, dx), but currently that is not the case.

A second inconsistency involves the dim_order argument: The gradient function doesn't use it, but omitting it from advection results in a stern warning from MetPy. I would expect:

  1. No warning from MetPy when dim_order is omitted in the advection call, and/or
  2. gradient to allow for the dim_order to be specified as well, interpreted the same way as in advection.

Version Info

Python 3.6.5
0.8.0+32.gb19c052

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: CalcPertains to calculationsType: API ChangeChanges to how existing functionality worksType: MaintenanceUpdates and clean ups (but not wrong)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions