Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhassell committed Nov 4, 2024
1 parent 736d03c commit 3e3e5bd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ version NEXTVERSION
* Fix bug where `cf.normalize_slice` doesn't correctly
handle certain cyclic slices
(https://github.com/NCAS-CMS/cf-python/issues/774)
* Fix bug where `cf.Field.subspace` doesn't also correctly
handle some global cyclic subspaces
(https://github.com/NCAS-CMS/cf-python/issues/828)

----

Expand Down
64 changes: 64 additions & 0 deletions cf/test/test_DimensionCoordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,70 @@ def test_DimensionCoordinate_cell_characteristics(self):
with self.assertRaises(ValueError):
d.get_cell_characteristics()

def test_DimensionCoordinate_anchor(self):
"""Test the DimensionCoordinate.anchor"""
f = cf.example_field(0)
d = f.dimension_coordinate("X")

self.assertEqual(d.period(), 360)

e = d.anchor(-1)
self.assertIsInstance(e, cf.DimensionCoordinate)
self.assertTrue(e.equals(d))

# Increasing
e = d.anchor(15)
self.assertTrue(e[0].equals(d[0]))
self.assertEqual(e[0].array, d[0].array)

e = d.anchor(30)
self.assertEqual(e[0].array, d[1].array)

e = d.anchor(361)
self.assertEqual(e[0].array, d[0].array + 360)

e = d.anchor(721)
self.assertEqual(e[0].array, d[0].array + 720)

e = d.anchor(15, cell=True)
self.assertEqual(e[0].array, d[0].array)

e = d.anchor(30, cell=True)
self.assertEqual(e[0].array, d[0].array)

e = d.anchor(361, cell=True)
self.assertEqual(e[0].array, d[0].array + 360)

e = d.anchor(721, cell=True)
self.assertEqual(e[0].array, d[0].array + 720)

# Decreasing
d = d[::-1]

e = d.anchor(721)
self.assertEqual(e[0].array, d[0].array + 360)

e = d.anchor(361)
self.assertEqual(e[0].array, d[0].array)

e = d.anchor(30)
self.assertEqual(e[0].array, d[-1].array)

e = d.anchor(15)
self.assertEqual(e[0].array, d[0].array - 360)

e = d.anchor(721, cell=True)
self.assertEqual(e[0].array, d[0].array + 360)

e = d.anchor(361, cell=True)
self.assertEqual(e[0].array, d[0].array)

e = d.anchor(30, cell=True)
self.assertEqual(e[0].array, d[0].array - 360)

e = d.anchor(15, cell=True)
self.assertEqual(e[0].array, d[0].array - 360)


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down
32 changes: 26 additions & 6 deletions cf/test/test_Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,14 +1396,34 @@ def test_Field_indices(self):
x = g.dimension_coordinate("X").array
self.assertTrue((x == [80, 120, 160, 200, 240, 280, 320]).all())

indices = f.indices(grid_longitude=cf.wi(-45, 45))
indices = f.indices(grid_longitude=cf.wo(-45, 45))
g = f[indices]
self.assertEqual(g.shape, (1, 10, 9))
self.assertEqual(g.shape, (1, 10, 6))
x = g.dimension_coordinate("X").array
print(x)
self.assertTrue(
(x == [-160, -120, -80, -40, 0, 40, 80, 120, 160]).all()
)
self.assertTrue((x == [80, 120, 160, 200, 240, 280]).all())

indices = f.indices(grid_longitude=cf.wo(35, 85))
g = f[indices]
x = g.dimension_coordinate("X").array
self.assertEqual(g.shape, (1, 10, 7))
x = g.dimension_coordinate("X").array
self.assertTrue((x == [-240, -200, -160, -120, -80, -40, 0]).all())

indices = f.indices(grid_longitude=cf.wo(35, 85))
g = f[indices]
x = g.dimension_coordinate("X").array
self.assertEqual(g.shape, (1, 10, 7))
x = g.dimension_coordinate("X").array
self.assertTrue((x == [-240, -200, -160, -120, -80, -40, 0]).all())

with self.assertRaises(ValueError):
f.indices(grid_longitude=cf.wo(0, 360))

with self.assertRaises(ValueError):
f.indices(grid_longitude=cf.wo(-90, 270))

with self.assertRaises(ValueError):
f.indices(grid_longitude=cf.wo(-180, 180))

# 2-d
lon = f.construct("longitude").array
Expand Down

0 comments on commit 3e3e5bd

Please sign in to comment.