Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing exemption to axis guessing on coords #5551

Merged
merged 32 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6b49b10
allowing excemption to axis guessing on coords
HGWright Oct 25, 2023
093372c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2023
eafa1d0
updating pr
HGWright Nov 7, 2023
d42a2b7
merge commit
HGWright Nov 7, 2023
e942b32
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 7, 2023
875f827
remove from metadata
HGWright Nov 9, 2023
056defa
merge conflict
HGWright Nov 9, 2023
8cd64b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2023
047f58b
remove merge clash
HGWright Nov 9, 2023
247feb1
Merge branch 'guess_coord' of github.com:HGWright/iris into guess_coord
HGWright Nov 9, 2023
724d05f
adding review comments
HGWright Nov 9, 2023
3d183f5
more review changes
HGWright Nov 9, 2023
c901c12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2023
a121ec4
parametrise and add tests
HGWright Nov 9, 2023
a7e6aa9
precommit changes
HGWright Nov 9, 2023
b5da396
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2023
6020a63
fix last test
HGWright Nov 9, 2023
cffa5b6
Merge branch 'guess_coord' of github.com:HGWright/iris into guess_coord
HGWright Nov 9, 2023
a9a45d1
addressing review comments
HGWright Nov 17, 2023
4151a7a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2023
c35c3d1
fix test failure
HGWright Nov 17, 2023
3e6fba8
Merge branch 'guess_coord' of github.com:HGWright/iris into guess_coord
HGWright Nov 17, 2023
d643915
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2023
5033391
add whatsnew and conftest files
HGWright Nov 17, 2023
faf4188
fix merge conflict
HGWright Nov 17, 2023
ed4c4c2
fix sentence
HGWright Nov 17, 2023
0180f58
Merge branch 'main' into guess_coord
HGWright Nov 17, 2023
cfbe34f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2023
6af7f81
fix flake8
HGWright Nov 17, 2023
c8854d3
Merge branch 'guess_coord' of github.com:HGWright/iris into guess_coord
HGWright Nov 17, 2023
19dd66e
fix last test
HGWright Nov 17, 2023
b01f3f8
update whatsnew
HGWright Nov 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import iris.time
import iris.util

#: The default value for ignore_axis which controls guess_coord_axis' behaviour
DEFAULT_IGNORE_AXIS = False


class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta):
"""
Expand Down Expand Up @@ -861,7 +864,8 @@ def xml_element(self, doc):
element.setAttribute(
"climatological", str(self.climatological)
)

if self.guess_coord:
element.setAttribute("guess_coord", str(self.guess_coord))
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
if self.attributes:
attributes_element = doc.createElement("attributes")
for name in sorted(self.attributes.keys()):
Expand Down Expand Up @@ -1608,6 +1612,8 @@ def __init__(
self.bounds = bounds
self.climatological = climatological

self._ignore_axis = DEFAULT_IGNORE_AXIS

def copy(self, points=None, bounds=None):
"""
Returns a copy of this coordinate.
Expand Down Expand Up @@ -1640,6 +1646,10 @@ def copy(self, points=None, bounds=None):
# self.
new_coord.bounds = bounds

# The state of ignore_axis is controlled by the coordinate rather than
# the metadata manager
new_coord.ignore_axis = self.ignore_axis

return new_coord

@classmethod
Expand All @@ -1659,7 +1669,14 @@ def from_coord(cls, coord):
if issubclass(cls, DimCoord):
# DimCoord introduces an extra constructor keyword.
kwargs["circular"] = getattr(coord, "circular", False)
return cls(**kwargs)

new_coord = cls(**kwargs)

# The state of ignore_axis is controlled by the coordinate rather than
# the metadata manager
new_coord.ignore_axis = coord.ignore_axis

return new_coord

@property
def points(self):
Expand Down Expand Up @@ -1751,6 +1768,24 @@ def climatological(self, value):

self._metadata_manager.climatological = value

@property
def ignore_axis(self):
"""
A boolean that controls whether guess_coord_axis acts on this
coordinate.

Defaults to False, and when set to True it will be skipped by
guess_coord_axis.
"""
return self._ignore_axis

@ignore_axis.setter
def ignore_axis(self, value):
if not isinstance(value, bool):
emsg = "Ignore_axis can only be set to True or False"
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(emsg)
self._ignore_axis = value

def lazy_points(self):
"""
Return a lazy array representing the coord points.
Expand Down Expand Up @@ -2697,6 +2732,8 @@ def __init__(
Will set to True when a climatological time axis is loaded
from NetCDF.
Always False if no bounds exist.
* guess_coord (bool):
When True: guess_coord will guess the coord, when False it won't.
bjlittle marked this conversation as resolved.
Show resolved Hide resolved

"""
# Configure the metadata manager.
Expand Down
22 changes: 22 additions & 0 deletions lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import dask.array as da
import numpy as np
import pytest

import iris
from iris.coords import AuxCoord, Coord, DimCoord
Expand Down Expand Up @@ -1150,6 +1151,27 @@ def test_change_units(self):
self.assertFalse(coord.climatological)


@pytest.fixture
def coord():
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
coord = iris.coords.DimCoord(points=(1, 2, 3, 4, 5))
return coord


class Test_ignore_axis:
def test_default(self, coord):
assert coord.ignore_axis is False

def test_set_true(self, coord):
coord.ignore_axis = True
assert coord.ignore_axis is True
bjlittle marked this conversation as resolved.
Show resolved Hide resolved

def test_set_random_value(self, coord):
with pytest.raises(
ValueError, match=r"Ignore_axis can only be set to True or False"
):
coord.ignore_axis = "foo"


class Test___init____abstractmethod(tests.IrisTest):
def test(self):
emsg = (
Expand Down
50 changes: 50 additions & 0 deletions lib/iris/tests/unit/util/test_guess_coord_axis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright Iris contributors
#
# This file is part of Iris and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
"""Test function :func:`iris.util.guess_coord_axis"""
bjlittle marked this conversation as resolved.
Show resolved Hide resolved

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests # isort:skip

import numpy as np
import pytest

import iris.coords
from iris.util import guess_coord_axis


@pytest.fixture
def coord():
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
coord = iris.coords.DimCoord(points=(1, 2, 3, 4, 5))
return coord


class TestCoords:
def test_longitude(self, coord):
coord.standard_name = "longitude"

assert guess_coord_axis(coord) == "X"

def test_latitude(self, coord):
coord.standard_name = "latitude"

assert guess_coord_axis(coord) == "Y"

def test_pressure_units(self, coord):
coord.units = "hPa"

assert guess_coord_axis(coord) == "Z"

def test_time_units(self, coord):
coord.units = "days since 1970-01-01 00:00:00"

assert guess_coord_axis(coord) == "T"
bjlittle marked this conversation as resolved.
Show resolved Hide resolved

def test_guess_coord(self, coord):
coord.standard_name = "longitude"
coord.ignore_axis = True

assert guess_coord_axis(coord) is None
9 changes: 8 additions & 1 deletion lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,17 @@ def guess_coord_axis(coord):
This function maintains laziness when called; it does not realise data.
See more at :doc:`/userguide/real_and_lazy_data`.

``guess_coord_axis`` can be skipped by setting the coordinate property of a coord ignore_axis
to False.
bjlittle marked this conversation as resolved.
Show resolved Hide resolved

"""

axis = None

if coord.standard_name in (
if coord.ignore_axis is True:
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
return axis

elif coord.standard_name in (
"longitude",
"grid_longitude",
"projection_x_coordinate",
Expand Down
Loading