Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 33 additions & 1 deletion lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ def xml_element(self, doc):
element.setAttribute(
"climatological", str(self.climatological)
)

if self.guess_coord:
element.setAttribute("guess_coord", str(self.guess_coord))
if self.attributes:
attributes_element = doc.createElement("attributes")
for name in sorted(self.attributes.keys()):
Expand Down Expand Up @@ -1541,6 +1542,7 @@ def __init__(
attributes=None,
coord_system=None,
climatological=False,
guess_coord=True,
):
"""
Coordinate abstract base class. As of ``v3.0.0`` you **cannot** create an instance of :class:`Coord`.
Expand Down Expand Up @@ -1608,6 +1610,8 @@ def __init__(
self.bounds = bounds
self.climatological = climatological

self.guess_coord = guess_coord

def copy(self, points=None, bounds=None):
"""
Returns a copy of this coordinate.
Expand Down Expand Up @@ -1655,6 +1659,7 @@ def from_coord(cls, coord):
"attributes": coord.attributes,
"coord_system": copy.deepcopy(coord.coord_system),
"climatological": coord.climatological,
"guess_coord": coord.guess_coord,
}
if issubclass(cls, DimCoord):
# DimCoord introduces an extra constructor keyword.
Expand Down Expand Up @@ -1751,6 +1756,27 @@ def climatological(self, value):

self._metadata_manager.climatological = value

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

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

@guess_coord.setter
def guess_coord(self, value):
print(value)
if value is not True and value is not False:
emsg = "Guess_coord can only be set to True or False"
raise ValueError(emsg)
else:
pass
self._metadata_manager.guess_coord = value

def lazy_points(self):
"""
Return a lazy array representing the coord points.
Expand Down Expand Up @@ -2581,6 +2607,7 @@ def from_regular(
coord_system=None,
circular=False,
climatological=False,
guess_coord=True,
with_bounds=False,
):
"""
Expand Down Expand Up @@ -2630,6 +2657,7 @@ def from_regular(
coord_system=coord_system,
circular=circular,
climatological=climatological,
guess_coord=guess_coord,
)

def __init__(
Expand All @@ -2644,6 +2672,7 @@ def __init__(
coord_system=None,
circular=False,
climatological=False,
guess_coord=True,
):
"""
Create a 1D, numeric, and strictly monotonic coordinate with **immutable** points and bounds.
Expand Down Expand Up @@ -2697,6 +2726,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.

"""
# Configure the metadata manager.
Expand All @@ -2712,6 +2743,7 @@ def __init__(
attributes=attributes,
coord_system=coord_system,
climatological=climatological,
guess_coord=guess_coord,
)

#: Whether the coordinate wraps by ``coord.units.modulus``.
Expand Down
28 changes: 28 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,33 @@ def test_change_units(self):
self.assertFalse(coord.climatological)


@pytest.fixture
def coord():
coord = iris.coords.DimCoord(points=(1, 2, 3, 4, 5))
return coord


class Test_guess_coord:
def test_default(self, coord):
assert coord.guess_coord == True

def test_set_true(self, coord):
coord.guess_coord = True

assert coord.guess_coord == True

def test_set_false(self, coord):
coord.guess_coord = False

assert coord.guess_coord == False

def test_set_random_value(self, coord):
with pytest.raises(
ValueError, match=r"Guess_coord can only be set to True or False"
):
coord.guess_coord = "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"""

# 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():
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"

def test_guess_coord(self, coord):
coord.standard_name = "longitude"
coord.guess_coord = False

assert guess_coord_axis(coord) == 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`.

Can be skipped by setting the coordinate property of a coord guess_coord
to False.

"""

axis = None

if coord.standard_name in (
if coord.guess_coord == False:
return None

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