Skip to content

Commit

Permalink
Use iris function to promote AuxCoord
Browse files Browse the repository at this point in the history
There is a builtin iris function that does this, so no sense writing our own.
  • Loading branch information
jfrost-mo committed Aug 15, 2024
1 parent f7f9a4a commit 732dfd9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/CSET/operators/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import iris
import iris.coords
import iris.cube
import iris.util
import numpy as np


Expand Down Expand Up @@ -258,20 +259,14 @@ def _lfric_time_coord_fix_callback(cube: iris.cube.Cube, field, filename):
The coordinate is converted and replaced if not. SLAMed LFRic data has this
issue, though the coordinate satisfies all the properties for a DimCoord.
Scalar time values are left as AuxCoords.
"""
if cube.coords("time"):
time_coord = cube.coord("time")
if not isinstance(time_coord, iris.coords.DimCoord):
dim_time_coord = iris.coords.DimCoord.from_coord(time_coord)
coord_dim = cube.coord_dims(time_coord)
cube.remove_coord(time_coord)
# Add dimension coordinate back to cover correct array dimension.
# Scalar coordinates will not have an array dimension so we add it
# as an auxiliary coordinate instead.
if coord_dim:
cube.add_dim_coord(dim_time_coord, coord_dim)
else:
cube.add_aux_coord(dim_time_coord)
if not isinstance(time_coord, iris.coords.DimCoord) and cube.coord_dims(
time_coord
):
iris.util.promote_aux_coord_to_dim_coord(cube, time_coord)


def _check_input_files(input_path: Path | str, filename_pattern: str) -> Iterable[Path]:
Expand Down
2 changes: 1 addition & 1 deletion tests/operators/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_lfric_time_coord_fix_callback_scalar_time():
cube = iris.cube.Cube([0, 0, 0], aux_coords_and_dims=[(length_coord, 0)])
cube.add_aux_coord(time_coord)
read._lfric_time_coord_fix_callback(cube, None, None)
assert isinstance(cube.coord("time"), iris.coords.DimCoord)
assert isinstance(cube.coord("time"), iris.coords.AuxCoord)
assert cube.coord_dims("time") == ()


Expand Down

0 comments on commit 732dfd9

Please sign in to comment.