Skip to content

Commit 76e4612

Browse files
authored
Merge pull request #860 from davidhassell/collapse-coordinate-direction
Fix bug that caused wrong directions from `cf.DimensionCoordinate.direction`
2 parents 601c14e + 0e24f3b commit 76e4612

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

Changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ version 3.17.0
1616
* Fix bug that caused `Data._axes` to be incorrect after a call to
1717
`cf.Field.collapse`
1818
(https://github.com/NCAS-CMS/cf-python/issues/857)
19+
* Fix bug that caused wrong directions from
20+
`cf.DimensionCoordinate.direction`
21+
(https://github.com/NCAS-CMS/cf-python/issues/859)
1922
* Changed dependency: ``Python>=3.9.0``
2023
* Changed dependency: ``numpy>=2.0.0``
2124
* Changed dependency: ``cfdm>=1.12.0.0, <1.12.1.0``

cf/dimensioncoordinate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def _infer_direction(self):
131131
"""Return True if a coordinate is increasing, otherwise return
132132
False.
133133
134-
A dimension coordinate construct is considered to be increasing if
135-
its data array values are increasing in index space, or if it has
136-
no data nor bounds.
134+
A dimension coordinate construct is considered to be
135+
increasing if its data array values are not strictly
136+
decreasing in index space, or if it has no data nor bounds.
137137
138138
If the direction can not be inferred from the data not bounds then
139139
the coordinate's units are used.
@@ -168,12 +168,12 @@ def _infer_direction(self):
168168
c = data._get_cached_elements()
169169
if c:
170170
try:
171-
return bool(c.get(0) < c.get(1))
171+
return bool(c.get(0) <= c.get(1))
172172
except TypeError:
173173
pass
174174

175175
data = data[:2].compute()
176-
return bool(data.item(0) < data.item(1))
176+
return bool(data.item(0) <= data.item(1))
177177

178178
# Still here?
179179
data = self.get_bounds_data(None, _fill_value=False)
@@ -182,12 +182,12 @@ def _infer_direction(self):
182182
c = data._get_cached_elements()
183183
if c:
184184
try:
185-
return bool(c.get(0) < c.get(1))
185+
return bool(c.get(0) <= c.get(1))
186186
except TypeError:
187187
pass
188188

189189
b = data[0].compute()
190-
return bool(b.item(0) < b.item(1))
190+
return bool(b.item(0) <= b.item(1))
191191

192192
# Still here? Then infer the direction from the units.
193193
return not self.Units.ispressure

cf/test/test_DimensionCoordinate.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,30 @@ def test_DimensionCoordinate_anchor(self):
809809
self.assertEqual(e[0].array, d[0].array - 360)
810810

811811

812+
def test_DimensionCoordinate_direction(self):
813+
"""Test DimensionCoordinate.direction"""
814+
d = self.dim.copy()
815+
816+
# Test the use case of
817+
# https://github.com/NCAS-CMS/cf-python/issues/859
818+
#
819+
# Create a coordinate with all equal values
820+
d.data[...] = d[0].array
821+
for i, x in enumerate(d.array):
822+
d.bounds.data[i, :] = x
823+
824+
d._custom["direction"] = None # Force a re-calculation of direction
825+
self.assertTrue(d.direction())
826+
d._custom["direction"] = None
827+
self.assertTrue(d[0].direction())
828+
829+
d.del_bounds()
830+
d._custom["direction"] = None
831+
self.assertTrue(d.direction())
832+
d._custom["direction"] = None
833+
self.assertTrue(d[0].direction())
834+
835+
812836
if __name__ == "__main__":
813837
print("Run date:", datetime.datetime.now())
814838
cf.environment()

0 commit comments

Comments
 (0)