@@ -636,6 +636,63 @@ def test_alternate_connectivity_dim_order(self):
636636 self .assertEqual (mesh_props ["face_dimension" ], "Mesh2d_face" )
637637 self .assertEqual (mesh_props ["edge_dimension" ], "Mesh2d_edge" )
638638
639+ def test_nonuniform_connectivity (self ):
640+ # Check handling of connecitivities with missing points.
641+ n_faces = 7
642+ mesh = make_mesh (n_faces = n_faces )
643+
644+ # In this case, add on a partial face-face connectivity.
645+ # construct a vaguely plausible face-face index array
646+ indices = np .ma .arange (n_faces * 4 ).reshape ((7 , 4 ))
647+ indices = indices % 7
648+ # include some missing points -- i.e. not all faces have 4 neighbours
649+ indices [(2 , (2 , 3 ))] = np .ma .masked
650+ indices [(3 , (0 , 2 ))] = np .ma .masked
651+ indices [6 , :] = np .ma .masked
652+
653+ conn = Connectivity (
654+ indices ,
655+ cf_role = "face_face_connectivity" ,
656+ )
657+ mesh .add_connectivities (conn )
658+ cube = make_cube (mesh = mesh )
659+
660+ # Save and snapshot the result
661+ dims , vars = self .check_save (cube )
662+
663+ # Check that the mesh saved with the additional connectivity
664+ (mesh_name ,) = vars_meshnames (vars )
665+ mesh_props = vars [mesh_name ]
666+ self .assertIn ("face_face_connectivity" , mesh_props )
667+ ff_conn_name = mesh_props ["face_face_connectivity" ]
668+
669+ # check that the connectivity has the corrects dims and fill-property
670+ ff_props = vars [ff_conn_name ]
671+ self .assertEqual (
672+ ff_props ["_DIMS" ], ["Mesh2d_faces" , "Mesh2d_face_N_faces" ]
673+ )
674+ self .assertIn ("_FillValue" , ff_props )
675+ self .assertEqual (ff_props ["_FillValue" ], - 1 )
676+
677+ # Check that a 'normal' connectivity does *not* have a _FillValue
678+ fn_conn_name = mesh_props ["face_node_connectivity" ]
679+ fn_props = vars [fn_conn_name ]
680+ self .assertNotIn ("_FillValue" , fn_props )
681+
682+ # For what it's worth, *also* check the actual data array in the file
683+ ds = nc .Dataset (self .tempfile_path )
684+ try :
685+ conn_var = ds .variables [ff_conn_name ]
686+ data = conn_var [:]
687+ finally :
688+ ds .close ()
689+ self .assertIsInstance (data , np .ma .MaskedArray )
690+ self .assertEqual (data .fill_value , - 1 )
691+ # Compare raw values stored, to indices with -1 at missing points
692+ raw_data = data .data
693+ filled_indices = indices .filled (- 1 )
694+ self .assertArrayEqual (raw_data , filled_indices )
695+
639696
640697if __name__ == "__main__" :
641698 tests .main ()
0 commit comments