@@ -52,6 +52,15 @@ def n_mesh_variables(filename):
5252 return n
5353
5454
55+ def combinations (face , edge , point ):
56+ """Return combinations for field/domain indexing."""
57+ return [
58+ i
59+ for n in range (1 , 4 )
60+ for i in itertools .permutations ([face , edge , point ], n )
61+ ]
62+
63+
5564class UGRIDTest (unittest .TestCase ):
5665 """Test UGRID field constructs."""
5766
@@ -63,6 +72,10 @@ class UGRIDTest(unittest.TestCase):
6372 os .path .dirname (os .path .abspath (__file__ )), "ugrid_2.nc"
6473 )
6574
75+ filename3 = os .path .join (
76+ os .path .dirname (os .path .abspath (__file__ )), "ugrid_3.nc"
77+ )
78+
6679 def setUp (self ):
6780 """Preparations called immediately before each test method."""
6881 # Disable log messages to silence expected warnings
@@ -189,7 +202,6 @@ def test_read_write_UGRID_field(self):
189202 # Face, edge, and point fields that are all part of the same
190203 # UGRID mesh
191204 ugrid = cf .example_fields (8 , 9 , 10 )
192-
193205 face , edge , point = (0 , 1 , 2 )
194206
195207 tmpfile = "tmpfileu.nc"
@@ -202,19 +214,8 @@ def test_read_write_UGRID_field(self):
202214 self .assertEqual (len (g ), 1 )
203215 self .assertTrue (g [0 ].equals (f ))
204216
205- # Test round-tripping fields with multiple fields
206- #
207- # Get the indices of 'ugrid' for all possible combinations of
208- # fields:
209- #
210- # combinations = [(0,), (1,), ..., (2, 0, 1), (2, 1, 0)]
211- combinations = [
212- i
213- for n in range (1 , 4 )
214- for i in itertools .permutations ([face , edge , point ], n )
215- ]
216-
217- for cells in combinations :
217+ # Test round-tripping of field combinations
218+ for cells in combinations (face , edge , point ):
218219 f = []
219220 for cell in cells :
220221 f .append (ugrid [cell ])
@@ -241,7 +242,6 @@ def test_read_write_UGRID_domain(self):
241242 # Face, edge, and point fields/domains that are all part of
242243 # the same UGRID mesh
243244 ugrid = [f .domain for f in cf .example_fields (8 , 9 , 10 )]
244-
245245 face , edge , point = (0 , 1 , 2 )
246246
247247 # Test for equality with the fields defined in memory. Only
@@ -254,38 +254,46 @@ def test_read_write_UGRID_domain(self):
254254 self .assertTrue (e [0 ].equals (d ))
255255 self .assertEqual (e [1 ].domain_topology ().get_cell (), "point" )
256256
257- # Test round-tripping fields with all three domains
258- #
259- # combinations = [(0, 1, 2), (0, 2, 1), ..., (2, 0, 1), (2, 1, 0)]
260- combinations = list ( itertools . permutations ([ face , edge , point ], 3 ))
261- for cells in combinations :
262- d = []
263- for cell in cells :
264- d .append (ugrid [cell ])
257+ # Test round-tripping of domain combinations for the
258+ # example_field domains, and also the domain read from
259+ # 'ugrid_3.nc'.
260+ for iteration in ( "memory" , "file" ):
261+ for cells in combinations ( face , edge , point ) :
262+ d = []
263+ for cell in cells :
264+ d .append (ugrid [cell ])
265265
266- cf .write (d , tmpfile )
266+ if point not in cells :
267+ # When we write a non-point domains, we also get
268+ # the point locations.
269+ d .append (ugrid [point ])
270+ elif cells == (point ,):
271+ # When we write a point domain on its own, we also
272+ # get the edge location.
273+ d .append (ugrid [edge ])
267274
268- # Check that there's only one mesh variable in the file
269- self .assertEqual (n_mesh_variables (tmpfile ), 1 )
275+ cf .write (d , tmpfile )
270276
271- e = cf .read (tmpfile , domain = True )
277+ # Check that there's only one mesh variable in the file
278+ self .assertEqual (n_mesh_variables (tmpfile ), 1 )
272279
273- self . assertEqual ( len ( e ), len ( d ) )
280+ e = cf . read ( tmpfile , domain = True )
274281
275- cf . write ( e , tmpfile1 )
282+ self . assertEqual ( len ( e ), len ( d ) )
276283
277- # Check that there's only one mesh variable in the file
278- self .assertEqual (n_mesh_variables (tmpfile1 ), 1 )
284+ cf .write (e , tmpfile1 )
285+
286+ # Check that there's only one mesh variable in the file
287+ self .assertEqual (n_mesh_variables (tmpfile1 ), 1 )
279288
280- f = cf .read (tmpfile1 , domain = True )
281- self .assertEqual (len (f ), len (e ))
282- for i , j in zip (f , e ):
283- self .assertTrue (i .equals (j ))
289+ f = cf .read (tmpfile1 , domain = True )
290+ self .assertEqual (len (f ), len (e ))
291+ for i , j in zip (f , e ):
292+ self .assertTrue (i .equals (j ))
284293
285- # Note: Other combintations of domain read/write are tricky,
286- # because the mesh variable *and* the domain variable in
287- # the dataset *both* define domains. Let's not worry
288- # about that now!
294+ # Set up for the 'file' iteration
295+ ugrid = cf .read (self .filename3 , domain = True )
296+ face , edge , point = (2 , 1 , 0 )
289297
290298
291299if __name__ == "__main__" :
0 commit comments