Skip to content

Instantly share code, notes, and snippets.

@pelson
Last active April 14, 2021 15:39
Show Gist options
  • Save pelson/9763057 to your computer and use it in GitHub Desktop.
Save pelson/9763057 to your computer and use it in GitHub Desktop.
Sorting an Iris cube by coordinate.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pelson
Copy link
Author

pelson commented Oct 7, 2014

FWIW to upcast the cube's new coordinate to a DimCoord (such that when saving NetCDF the dimension name comes from the coord's var_name) one would need to do:

import iris.coords

tbd = cube.coord('tbd')
tbd_dims = cube.coord_dims(tbd)

# Create a new coordinate which is a DimCoord.
dim_tbd = iris.coords.DimCoord.from_coord(tbd)

# Remove the AuxCoord and add the DimCoord.
cube.remove_coord(tbd)
cube.add_dim_coord(dim_tbd, tbd_dims)

@pelson
Copy link
Author

pelson commented Apr 14, 2021

I like finding answers to questions that I wrote many years ago. That still work today! 🎉

Just wish I'd implemented multiple coordinate sorting, not just one...

@pelson
Copy link
Author

pelson commented Apr 14, 2021

I guess it would work just as well to split the cube into single length items on the dimension to sort then to merge them together again...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment