# Renaming attributes/coordinates in cf-python

**URL:** <https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526>\
**Category:** General\
**Created:** [20 September 2024 15:36 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526 "2024-09-20T15:36:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shakka](https://dub1.discourse-cdn.com/flex013/user_avatar/cms-helpdesk.ncas.ac.uk/shakka/32/111_2.png) [@shakka](https://cms-helpdesk.ncas.ac.uk/u/shakka)\
**Post date:** [20 September 2024 15:36 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526/1 "2024-09-20T15:36:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grenville](https://dub1.discourse-cdn.com/flex013/user_avatar/cms-helpdesk.ncas.ac.uk/grenville/32/29_2.png) [@grenville](https://cms-helpdesk.ncas.ac.uk/u/grenville)\
**Post date:** [20 September 2024 15:48 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526/2 "2024-09-20T15:48:53Z")

</div>

Hi Ella

what are you using to do the cmorisation?

---

<div class="post-metadata">

**Author:** ![shakka](https://dub1.discourse-cdn.com/flex013/user_avatar/cms-helpdesk.ncas.ac.uk/shakka/32/111_2.png) [@shakka](https://cms-helpdesk.ncas.ac.uk/u/shakka)\
**Post date:** [23 September 2024 09:50 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526/4 "2024-09-23T09:50:23Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![sadielbartholomew](https://dub1.discourse-cdn.com/flex013/user_avatar/cms-helpdesk.ncas.ac.uk/sadielbartholomew/32/436_2.png) [@sadielbartholomew](https://cms-helpdesk.ncas.ac.uk/u/sadielbartholomew)\
**Post date:** [23 September 2024 13:38 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526/5 "2024-09-23T13:38:59Z")

</div>

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):

```python
>>> 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](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.

---

<div class="post-metadata">

**Author:** ![shakka](https://dub1.discourse-cdn.com/flex013/user_avatar/cms-helpdesk.ncas.ac.uk/shakka/32/111_2.png) [@shakka](https://cms-helpdesk.ncas.ac.uk/u/shakka)\
**Post date:** [23 September 2024 14:50 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526/6 "2024-09-23T14:50:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/cms_support/original/1X/1fd2411499ffcbc299fe756cd5cdf26e44956558.png) [@system](https://cms-helpdesk.ncas.ac.uk/u/system)\
**Post date:** [25 September 2024 14:51 UTC](https://cms-helpdesk.ncas.ac.uk/t/renaming-attributes-coordinates-in-cf-python/1526/7 "2024-09-25T14:51:01Z")

</div>

Closed on 25 September 2024 14:51 UTC

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