Dealing with a dimension and auxiliary coordinate with the same name ('time')

1,133 views
Skip to first unread message

Jonny Williams

unread,
Nov 2, 2016, 10:16:25 PM11/2/16
to Iris
Hi everyone

I have a cube where a dimension coordinate and an auxiliary coordinate are the same and hence I am getting an error when I try to do an average over time.

Does anyone know how I can rename one of them, or perhaps delete the auxiliary one?

Thanks!

Jonny

sea_water_practical_salinity / (1e-3) (time: 12; -- : 332; -- : 362)
     Dimension coordinates:
          time                             x        -         -
     Auxiliary coordinates:
          time                             x        -         -
          latitude                         -        x         x
          longitude                        -        x         x
     Scalar coordinates:
          depth: 0.50576 m, bound=(0.0, 1.02391) m
     Attributes:
          Conventions: CF-1.5
          description: ocean T grid variables
          ibegin: 1
          interval_operation: 10 d
          interval_write: 10 d
          jbegin: 1
          ni: 362
          nj: 5
          online_operation: average
          production: An IPSL model
          title: ocean T grid variables
     Cell methods:
          mean: time

RuthC

unread,
Nov 3, 2016, 11:25:57 AM11/3/16
to Iris
Hi Jonny, welcome to Iris!

can you get hold of just one of them by doing

coord = cube.dim_coords[0]

or

coord = cube.aux_coords[0]

if so, you should then be able to use the rename method on whichever coord:

coord.rename('other_time')

I'm curious as to how Iris managed to load this cube without throwing any errors though!

Ruth

Jonny Williams

unread,
Nov 3, 2016, 4:59:35 PM11/3/16
to Iris
Hi Ruth!

Thanks a lot for this :)

I have pasted the code below using your suggestion. The dimension coordinate has been renamed successfully (awesome!) but the auxiliary coordinate but has renamed 'depth' to aux_time.

Thanks again for your help; really appreciated.

Jonny

time1 = cubes.dim_coords[0]
time2 = cubes.aux_coords[0]
time1.rename('dim_time')
time2.rename('aux_time')

print(cubes)

sea_water_practical_salinity / (1e-3) (dim_time: 241; -- : 332; -- : 362)
     Dimension coordinates:
          dim_time                             x         -         -
     Auxiliary coordinates:
          time                                 x         -         -
          latitude                             -         x         x
          longitude                            -         x         x
     Scalar coordinates:
          aux_time: 0.50576 m, bound=(0.0, 1.02391) m
     Attributes:
          Conventions: CF-1.5
          description: ocean T grid variables
          ibegin: 1
          interval_operation: 10 d
          interval_write: 10 d
          jbegin: 1
          ni: 362
          nj: 5
          online_operation: average
          production: An IPSL model
          title: ocean T grid variables
     Cell methods:
          mean: time

Andrew Dawson

unread,
Nov 4, 2016, 4:43:11 AM11/4/16
to Iris
Hi Jonny

The solution that Ruth provided is a good first attempt, but it has one obvious pitfall which you have already discovered: it requires knowing the order of dimensions before you can apply the operation. One of the major advantages of iris is that you almost never need to do this, it is usually possible to write a program in such a way that it will work without relying on dimension order. I have a few suggestions that may help get you on your way.

Firstly, the good news is that you don't need to rename the coordinates at all, iris is perfectly happy having two coordinates with the same name like this (although you may not be, we'll get to that later) and can tell the difference. I think all functions that accept a coordinate name as input will accept an actual coordinate object as input also (certainly true in the case of collapsed), which means you just need to be able to find your coordinate by something other than just name in order to collapse it. If you want to get the coordinate named "time" that is also a dimension coordinate you can do this:

time_dimcoord = cube.coord("time", dim_coords=True)

you can now pass this to the collapsed method to average over the time dimension:

mean = cube.collapsed(time_dimcoord, iris.analysis.MEAN)

If on the other hand you wanted to extract the auxiliary coordinate  named "time", you could use

time_auxcoord = cube.coord("time", dim_coords=False)

This might be useful if did want to rename the time auxiliary coordinate to something else:

time_auxcoord.rename('something_else')
print(cube)

Hope that helps

Jonny Williams

unread,
Nov 7, 2016, 5:06:42 AM11/7/16
to Iris
Hi Andrew

Thanks very much for your reply. your suggestions were very helpful!

Ruth's original reply as well as help from colleagues here at NIWA in NZ and your reply have enabled me to attack this issue from all sides, using multiple approaches which have aided my learning no end :)

Thanks again, much appreciated.

Jonny
Reply all
Reply to author
Forward
0 new messages