Skip to content

Commit a8de6af

Browse files
committed
remove UGRID constructs after collapse
1 parent 4129e98 commit a8de6af

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

cf/field.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6903,18 +6903,25 @@ def collapse(
69036903

69046904
# ---------------------------------------------------------
69056905
# Update dimension coordinates, auxiliary coordinates,
6906-
# cell measures and domain ancillaries
6906+
# cell measures, domain ancillaries, domain_topologies,
6907+
# and cell connectivities.
69076908
# ---------------------------------------------------------
69086909
for axis, domain_axis in collapse_axes.items():
69096910
# Ignore axes which are already size 1
69106911
size = domain_axis.get_size()
69116912
if size == 1:
69126913
continue
69136914

6914-
# REMOVE all cell measures and domain ancillaries
6915-
# which span this axis
6915+
# REMOVE all cell measures, domain ancillaries,
6916+
# domain_topologies, and cell connectivities which
6917+
# span this axis
69166918
c = f.constructs.filter(
6917-
filter_by_type=("cell_measure", "domain_ancillary"),
6919+
filter_by_type=(
6920+
"cell_measure",
6921+
"domain_ancillary",
6922+
"domain_topology",
6923+
"cell_connectivity",
6924+
),
69186925
filter_by_axis=(axis,),
69196926
axis_mode="or",
69206927
todict=True,

cf/test/test_collapse.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import atexit
12
import datetime
23
import faulthandler
34
import os
5+
import tempfile
46
import unittest
57

68
import numpy
@@ -9,6 +11,25 @@
911

1012
import cf
1113

14+
n_tmpfiles = 1
15+
tmpfiles = [
16+
tempfile.mkstemp("_test_collapse.nc", dir=os.getcwd())[1]
17+
for i in range(n_tmpfiles)
18+
]
19+
[tmpfile] = tmpfiles
20+
21+
22+
def _remove_tmpfiles():
23+
"""Try to remove defined temporary files by deleting their paths."""
24+
for f in tmpfiles:
25+
try:
26+
os.remove(f)
27+
except OSError:
28+
pass
29+
30+
31+
atexit.register(_remove_tmpfiles)
32+
1233

1334
class Field_collapseTest(unittest.TestCase):
1435
def setUp(self):
@@ -791,6 +812,19 @@ def test_Field_collapse_non_positive_weights(self):
791812
# compute time
792813
g.array
793814

815+
def test_Field_collapse_ugrid(self):
816+
"""Check that UGRID constructs are removed after collapsing."""
817+
f = cf.example_field(8)
818+
self.assertTrue(f.domain_topologies())
819+
self.assertTrue(f.cell_connectivities())
820+
821+
f = f.collapse("area: mean")
822+
self.assertFalse(f.domain_topologies())
823+
self.assertFalse(f.cell_connectivities())
824+
825+
# Check the collpsed fields writes
826+
cf.write(f, tmpfile)
827+
794828

795829
if __name__ == "__main__":
796830
print("Run date:", datetime.datetime.now())

0 commit comments

Comments
 (0)