MetOffice / MetOffice/CSET

Review and improve rolling data and coordinate points for aggregation by hour_of_day

Open
#1,454 0 comments 0 reactions 1 assignee View on GitHub

@jfrost-mo is already working on this.

Since Jun 17, 2025.

cleanup enhancement
Dominant language
Python
Stars
33
Forks
19
Avg merge
1d 22h
Merged PRs (30d)
30

Description

In #1450, the following technical review comment and suggestion was raised in relation to method to ensure time/hour coordinate and the data array are sorted in time order.

====

I'm going to suggest a different approach to sorting the cube. The current method works, but has two major flaws.

1. It relies on the layout of data array within the cube remaining consistent. While this may be the case for UM/LFRic data, we can't rely on it in general.
2. The use of the `.data` cube attribute causes the data to be realised. Currently there are a few places in CSET where this will happen, but it is something we are going to have to get rid of if we want to support high resolution or long duration data sets, such as K-scale.

Instead of dealing with the underlying data array, we can blow the cube apart and construct a new one, with the relevant coordinate sorted. Some code to do this is below.

```python
def sort_cube_by_coordinate(cube: iris.cube.Cube, coordinate: str) -> iris.cube.Cube:
"""Sort a cube by one of its coordinates.

The cube is broken apart and reassembled to sort the specified coordinate.

Arguments
---------
cube: iris.cube.Cube
Cube to sort.
coordinate: str
Name of coordinate to sort. It must exist on the cube.

Returns
-------
iris.cube.Cube
Sorted version of the input cube.
"""
slices = cube.slices_over(coordinate)
# Sort slices by the (scalar) points of the coordinate.
sorted_slices = sorted(slices, key=lambda c: c.coord(coordinate).points)
return iris.cube.CubeList(sorted_slices).merge_cube()

by_hour = sort_cube_by_coordinate(by_hour, "hour")
```

_Originally posted by @jfrost-mo in https://github.com/MetOffice/CSET/pull/1450#discussion_r2152893658_

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.