Renaming attributes/coordinates in cf-python

Hi hi, cf-python question for you. I’m trying to CMORise a bunch of UM data: so far so good. I’ve noticed though that although the files have sensible names for latitude bounds ( “latitude_bounds” for latitude and “grid_latitude_bounds” for grid_latitude), the same can’t be said for longitude, where the bounds are called “bounds” and “bounds_1”, respectively. I’d like to change these to match, i.e. “longitude_bounds” and “grid_longitude_bounds”. I’d need to change both the bounds attributes in the coord variables, and the longitude / grid_longitude coordinates to match.

I can’t find in the docs how to change these names - can any cf-python whizzes help me out please?

Cheers
Ella

Hi Ella

what are you using to do the cmorisation?

I’m using cf-python to do the CMORisation - tried with CDDS but couldn’t get it to work properly and python works better with my workflow anyway.

(not sure why my previous response didn’t come through!)

Hi Ella, thanks for the query.

Here’s an example of changing the names for the longitude bounds which you can follow along with and should work on your data in question, noting you will have to adapt to identify the ‘construct’ (cf-python and CF Data Model terminology) of the longitude and its bounds by the name arguments to operate on in your case, and obviously apply the method to the fields in question whereas I am using an example file (which is a PP file, but this should also work if you have a fields file):

>>> import cf
>>> 
>>> f = cf.read("umfile.pp")
>>> f = f[0]
>>> f
<CF Field: surface_air_pressure(time(3), latitude(73), longitude(96)) Pa>
>>> 
>>> # 1. Get longitudes
>>> l = f.construct("longitude")
>>> l
<CF DimensionCoordinate: longitude(96) degrees_east>
>>> 
>>> # 2. Get bounds
>>> b = l.get_bounds()
>>> b
<CF Bounds: longitude(96, 2) degrees_east>
>>> # See full bounds info
>>> b.dump()
Bounds: longitude
    units = 'degrees_east'
    Data(96, 2) = [[-1.875, ..., 358.125]] degrees_east
>>> 
>>> # 3. Add or adjust the name - might be standard name or long name
>>> #    you want to edit, this example is for standard name but
>>> #    otherwise try 'long_name'
>>> b.standard_name = "longitude_bounds"  # change to whatever name you need
>>> b
<CF Bounds: longitude_bounds(96, 2) degrees_east>
>>> b.dump()
Bounds: longitude_bounds
    standard_name = 'longitude_bounds'
    units = 'degrees_east'
    Data(96, 2) = [[-1.875, ..., 358.125]] degrees_east
>>> # Note the name has been added to the bounds and this has
>>> # been adjusted in the longitude and overall field object too
>>> l.dump()
Dimension coordinate: longitude
    axis = 'X'
    standard_name = 'longitude'
    units = 'degrees_east'
    Data(96) = [0.0, ..., 356.25] degrees_east
    Bounds:standard_name = 'longitude_bounds'
    Bounds:units = 'degrees_east'
    Bounds:Data(96, 2) = [[-1.875, ..., 358.125]] degrees_east
>>> f.dump()
/home/slb93/miniconda3/envs/cf-env-312/lib/python3.12/site-packages/numpy/ma/core.py:467: RuntimeWarning: invalid value encountered in cast
  fill_value = np.array(fill_value, copy=False, dtype=ndtype)
-------------------------------------------------------
Field: surface_air_pressure (ncvar%UM_m01s00i001_vn405)
-------------------------------------------------------
Conventions = 'CF-1.11'
_FillValue = -1073741824.0
history = 'Converted from UM/PP by cf-python v3.16.2'
lbproc = '128'
lbtim = '122'
long_name = 'PSTAR AFTER TIMESTEP'
runid = 'aatzx'
source = 'UM'
standard_name = 'surface_air_pressure'
stash_code = '1'
submodel = '1'
um_stash_source = 'm01s00i001'
um_version = '4.5'
units = 'Pa'

Data(time(3), latitude(73), longitude(96)) = [[[102334.0, ..., 69123.0]]] Pa

Cell Method: time(3): mean

Domain Axis: latitude(73)
Domain Axis: longitude(96)
Domain Axis: time(3)

Dimension coordinate: time
    axis = 'T'
    calendar = '360_day'
    standard_name = 'time'
    units = 'days since 2159-1-1'
    Data(time(3)) = [2160-06-01 00:00:00, 2161-06-01 00:00:00, 2162-06-01 00:00:00] 360_day
    Bounds:calendar = '360_day'
    Bounds:units = 'days since 2159-1-1'
    Bounds:Data(time(3), 2) = [[2159-12-01 00:00:00, ..., 2162-12-01 00:00:00]] 360_day

Dimension coordinate: latitude
    axis = 'Y'
    standard_name = 'latitude'
    units = 'degrees_north'
    Data(latitude(73)) = [90.0, ..., -90.0] degrees_north
    Bounds:units = 'degrees_north'
    Bounds:Data(latitude(73), 2) = [[91.25, ..., -91.25]] degrees_north

Dimension coordinate: longitude
    axis = 'X'
    standard_name = 'longitude'
    units = 'degrees_east'
    Data(longitude(96)) = [0.0, ..., 356.25] degrees_east
    Bounds:standard_name = 'longitude_bounds'
    Bounds:units = 'degrees_east'
    Bounds:Data(longitude(96), 2) = [[-1.875, ..., 358.125]] degrees_east
>>> # (Do the above for all of the bounds names that need changing then write out etc.)

Note for future, with regards to cf-python and cf-plot (and the other NCAS cf* libraries) we mostly use/monitor the GitHub Issue Tracker for user support, so for likely quicker response times please use that instead of the Help Desk: https://github.com/NCAS-CMS/cf-python/issues (but if posted here someone will also pass the message onto us).

Please let us know if the above doesn’t do the trick or if I have misunderstood your intentions.

1 Like

Amazing, thanks Sadie. And thanks for pointing me towards the github - will head there in future!

1 Like

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