Skip to content

Error in cf.Field.derivative and cf.relative_vorticity when wrapping with periodic coordinates #289

@davidhassell

Description

@davidhassell

At v3.11.0, there is a bug in cf.Field.derivative which causes the returned boundary values to be incorrect when wrapping with periodic coordinates (such as longitudes).

The centred differences of the coordinates are used, so the wrapped values need to be adjusted by the period in order to be correct. This is not currently the case.

For example, if there are longitude coordinates of (22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5, 337.5) in degrees east, then their centred differences should be (90, 90, 90, 90, 90, 90, 90, 90). However, the current derivative method gives them as (-270, 90, 90, 90, 90, 90, 90, -270).

>>> # v3.11.0 situation - INCORRECT
>>> import cf
>>> f = cf.example_field(0)
>>> print(f)
Field: specific_humidity (ncvar%q)
----------------------------------
Data            : specific_humidity(latitude(5), longitude(8)) 1
Cell methods    : area: mean
Dimension coords: latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : time(1) = [2019-01-01 00:00:00]

>>> print(f.derivative('X').array[[0, -1]))
[[-1.85185185e-05 -4.44444444e-05 -2.22222222e-04  1.66666667e-04
   2.55555556e-04  6.66666667e-05 -8.88888889e-05  6.29629630e-05]
 [-8.51851852e-05  1.44444444e-04 -1.11111111e-05 -1.11111111e-05
   2.22222222e-05  1.77777778e-04 -2.66666667e-04  1.03703704e-04]]
# CORRECT VALUES:
[[ 5.55555556e-05 -4.44444444e-05 -2.22222222e-04  1.66666667e-04
   2.55555556e-04  6.66666667e-05 -8.88888889e-05 -1.88888889e-04]
 [ 2.55555556e-04  1.44444444e-04 -1.11111111e-05 -1.11111111e-05
   2.22222222e-05  1.77777778e-04 -2.66666667e-04 -3.11111111e-04]]

PR to follow

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions