Skip to content

cf.Field.collapse: sum, sum_of_weights and sum_of_weights2 incorrect when weighted  #701

@davidhassell

Description

@davidhassell

Since at least v3.14.0, cf,Field.collapse has given incorrect results for the sum, sum_of_weights and sum_of_weights2 methods, only in the case that weights have been requested:

Many thanks to @CheeseFace for explaining the use case that uncovered this bug.

>>> import cf
>>> f = cf.example_field(0)
>>> weights = f.weights("area", measure=True)
>>> 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(weights.array)
[[4.27128714e+12 4.27128714e+12 4.27128714e+12 4.27128714e+12
  4.27128714e+12 4.27128714e+12 4.27128714e+12 4.27128714e+12]
 [1.16693735e+13 1.16693735e+13 1.16693735e+13 1.16693735e+13
  1.16693735e+13 1.16693735e+13 1.16693735e+13 1.16693735e+13]
 [3.18813213e+13 3.18813213e+13 3.18813213e+13 3.18813213e+13
  3.18813213e+13 3.18813213e+13 3.18813213e+13 3.18813213e+13]
 [1.16693735e+13 1.16693735e+13 1.16693735e+13 1.16693735e+13
  1.16693735e+13 1.16693735e+13 1.16693735e+13 1.16693735e+13]
 [4.27128714e+12 4.27128714e+12 4.27128714e+12 4.27128714e+12
  4.27128714e+12 4.27128714e+12 4.27128714e+12 4.27128714e+12]]
>>> g = f.collapse("area: sum", weights=True)
>>> g.array
array([[1.843]])  # WRONG
>>> (f * weights).sum().array
array(3.43567397e+13)  # RIGHT
>>> g = f.collapse("area: sum_of_weights", weights=weights)
>>> g.array
array([[40]])  # WRONG
>>> weights.sum().array
array(5.1010114e+14)  # RIGHT
>>> g = f.collapse("area: sum_of_weights2", weights=weights)
>>> g.array
array([[40]])  # WRONG
>>> (weights**2).sum().array
array(1.06020399e+28)  # RIGHT
>>> cf.environment(paths=False)
Platform: Linux-5.15.0-87-generic-x86_64-with-glibc2.35
HDF5 library: 1.14.2
netcdf library: 4.9.2
udunits2 library: libudunits2.so.0
esmpy/ESMF: 8.4.2
Python: 3.11.4
dask: 2023.9.3
netCDF4: 1.6.4
psutil: 5.9.5
packaging: 23.0
numpy: 1.25.2
scipy: 1.11.3
matplotlib: 3.8.0
cftime: 1.6.2
cfunits: 3.3.6
cfplot: 3.2.23
cfdm: 1.10.1.2
cf: 3.15.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions