Skip to content

Weighted-mean by area for 1D land-compressed CF Field #731

@mcguirepatr

Description

@mcguirepatr

Hi cf-python'ers
I am trying to do a global land-only area-weighted average with CF Python of a field that is a 1D land-only array (instead of 2D). I'm using a script that someone else made to get these in the right 1D format instead of the 1D format that JULES makes. But I get an error when I try to do this in CF Python. Can you advise me on this? See below.
Patrick

On the CEDA Jasmin supercomputer, I do this:

module load jaspy
python ~pmcguire/cf1dland_v1.py ~pmcguire/for_andyh/JULES2013.nc #I'm attaching the other person's python code[cf1dland_v1.txt](https://github.com/NCAS-CMS/cf-python/files/14462867/cf1dland_v1.txt) on the GitHUB repository as well
cp -p converted.nc ~pmcguire/for_andyh/converted.nc

python
>>> import cf
>>> xx=cf.read('~pmcguire/for_andyh/converted.nc')
>>> xx[0]
<CF Field: long_name=Gridbox surface temperature(time(1), latitude(112), longitude(192)) K>
>>> print(xx[0])
Field: long_name=Gridbox surface temperature (ncvar%Ts)
-------------------------------------------------------
Data            : long_name=Gridbox surface temperature(time(1), latitude(112), longitude(192)) K
Cell methods    : time : mean
Dimension coords: time(1) = [2014-01-01 00:00:00] 365_day
                : latitude(112) = [-55.625, ..., 83.125] degrees_north
                : longitude(192) = [0.9375, ..., 359.0625] degrees_east

print(xx[0].collapse('mean',axes='area',measure='true').array)

[[[281.92977735622253]]] #this seems right for Kelvin, but it isn't area-weighted.

But when I try to do an area-weighted average, I get this error.

>>> print(xx[0].collapse('mean',axes='area',measure='true',weights='area').array)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

  File "/home/users/pmcguire/.local/lib/python3.10/site-packages/cf/decorators.py", line 71, in precede_with_kwarg_deprecation_check
    operation_method_result = operation_method(self, *args, **kwargs)

  File "/home/users/pmcguire/.local/lib/python3.10/site-packages/cfdm/decorators.py", line 171, in verbose_override_wrapper
    return method_with_verbose_kwarg(*args, **kwargs)

  File "/home/users/pmcguire/.local/lib/python3.10/site-packages/cf/field.py", line 8016, in collapse
    d_weights = f.weights(

  File "/home/users/pmcguire/.local/lib/python3.10/site-packages/cf/field.py", line 4923, in weights
    self._weights_geometry_area(

  File "/home/users/pmcguire/.local/lib/python3.10/site-packages/cf/field.py", line 2237, in _weights_geometry_area
    axis, aux_X, aux_Y, aux_Z = self._weights_yyy(

  File "/home/users/pmcguire/.local/lib/python3.10/site-packages/cf/field.py", line 3036, in _weights_yyy
    raise ValueError(
ValueError: Can't create weights: Need both X and Y nodes to calculate polygon geometry weights

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