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

Hi CMS Helpdesk:
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 Andy Heaps 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 Jasmin, I do this:

module load jaspy

python ~pmcguire/from_andyh/cf1dland_v1.py ~pmcguire/for_andyh/JULES2013.nc

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 “”, line 1, in

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

This is the output when I turn debugging on. Any suggestions?
Is there another helpdesk that would work better for this?
Patrick

>>>cf.log_level('DEBUG')
>>>print(xx[0].collapse(‘mean’,axes=‘area’,measure=‘true’,weights=‘area’).array)
DEBUG
    all_methods, all_axes, all_within, all_over = ('mean',) [['domainaxis2', 'domainaxis1']] (None,) (None,)
    axes                    = ['domainaxis2', 'domainaxis1']
    method                  = mean
    collapse_axes_all_sizes = Constructs:
{'domainaxis1': <CF DomainAxis: size(112)>,
 'domainaxis2': <CF DomainAxis: size(192)>}
    collapse_axes           = {'domainaxis1': <CF DomainAxis: size(112)>, 'domainaxis2': <CF DomainAxis: size(192)>}
    collapse_axes_sizes     = [112, 192]
    Input weights           = 'area'
Traceback (most recent call last):
  File "/home/users/pmcguire/for_andyh/cf1dland_v2.py", line 207, in <module>
    print(xx[0].collapse('mean',axes='area',weights='area',measure='true').array)
  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

I’m now putting print statements in my own copy of the cf module code.
Maybe that will sort it out.
Patrick

With the print statements, it seems that the _weights_yyy function is being called for some reason with domain_axis=None and geometry_type=polygon. It looks like the next loop over auxiliary_coordinates_1d.items() is not getting entered into (maybe there are no items?).
Patrick

I’ve been told by someone who works on CF, that if we have questions or queries regarding cf-python, cf-plot or any other related libraries, we can raise them on their GitHub Issue Trackers, since the CF people don’t monitor the NCAS CMS Helpdesk but they monitor the GitHub Issue Trackers very closely. You can find these at:

I have now opened this as a GitHub issue on the cf-python repository:

One of the CF-python managers has said:
this looks like it might be a cf-python bug (which might be fixed by Fix bug when calculating UGRID cell areas when non-spatial coordinates span the discrete axis · Issue #721 · NCAS-CMS/cf-python · GitHub, publicly released today!).
(although it might be something else, too!)

I have created the lat/lon bounds for the CF field. And it worked great!

See the attached plots that I made after doing this, for the global GPP vs. year (1860-2013) and for the global GPP vs. atmospheric CO2.
I did this with a collapse(‘integral’,axes=‘area’,weights=‘area’) to get the global spatially-summed GPP, and a collapse(‘maximum’,axes=‘area’,weights=‘area’) to get the max value of the CO2 over the area. The CO2 should be the same everywhere each year.
(GPP=Gross Primary Productivity, in Petagrams of Carbon per year, which is a measure of planet growth and carbon assimilation).
GPPvYear_v1
GPPvCO2_v1

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.