@@ -1275,6 +1275,42 @@ def __repr__(self):
12751275 return "<%s of %s>" % (self .__class__ .__name__ , ", " .join (result ))
12761276
12771277
1278+ class CFUGridGroup (CFGroup ):
1279+ """Represents a collection of CF Metadata Conventions variables and netCDF global attributes.
1280+
1281+ Represents a collection of 'NetCDF Climate and Forecast (CF) Metadata
1282+ Conventions' variables and netCDF global attributes.
1283+
1284+ Specialisation of :class:`~iris.fileformats.cf.CFGroup` that includes extra
1285+ collections for CF-UGRID-specific variable types.
1286+
1287+ """
1288+
1289+ @property
1290+ def connectivities (self ):
1291+ """Collection of CF-UGRID connectivity variables."""
1292+ return self ._cf_getter (CFUGridConnectivityVariable )
1293+
1294+ @property
1295+ def ugrid_coords (self ):
1296+ """Collection of CF-UGRID-relevant auxiliary coordinate variables."""
1297+ return self ._cf_getter (CFUGridAuxiliaryCoordinateVariable )
1298+
1299+ @property
1300+ def meshes (self ):
1301+ """Collection of CF-UGRID mesh variables."""
1302+ return self ._cf_getter (CFUGridMeshVariable )
1303+
1304+ @property
1305+ def non_data_variable_names (self ):
1306+ """:class:`set` of names of the CF-netCDF/CF-UGRID variables that are not the data pay-load."""
1307+ extra_variables = (self .connectivities , self .ugrid_coords , self .meshes )
1308+ extra_result = set ()
1309+ for variable in extra_variables :
1310+ extra_result |= set (variable )
1311+ return super ().non_data_variable_names | extra_result
1312+
1313+
12781314################################################################################
12791315class CFReader :
12801316 """Allows the contents of a netCDF file to be interpreted.
@@ -1299,7 +1335,7 @@ class CFReader:
12991335 CFUGridMeshVariable ,
13001336 )
13011337
1302- CFGroup = CFGroup
1338+ CFGroup = CFUGridGroup
13031339
13041340 def __init__ (self , file_source , warn = False , monotonic = False ):
13051341 # Ensure safe operation for destructor, should init fail.
@@ -1331,6 +1367,11 @@ def __init__(self, file_source, warn=False, monotonic=False):
13311367
13321368 self ._check_monotonic = monotonic
13331369
1370+ self ._with_ugrid = True
1371+ if not self ._has_meshes ():
1372+ self ._trim ()
1373+ self ._with_ugrid = False
1374+
13341375 self ._translate ()
13351376 self ._build_cf_groups ()
13361377 self ._reset ()
@@ -1348,6 +1389,26 @@ def __exit__(self, exc_type, exc_value, traceback):
13481389 # When used as a context-manager, **always** close the file on exit.
13491390 self ._close ()
13501391
1392+ def _has_meshes (self ):
1393+ result = False
1394+ for _ , variable in self ._dataset .variables .items ():
1395+ if hasattr (variable , "mesh" ):
1396+ result = True
1397+ break
1398+ return result
1399+
1400+ def _trim (self ):
1401+ self ._variable_types = (
1402+ CFAncillaryDataVariable ,
1403+ CFAuxiliaryCoordinateVariable ,
1404+ CFBoundaryVariable ,
1405+ CFClimatologyVariable ,
1406+ CFGridMappingVariable ,
1407+ CFLabelVariable ,
1408+ CFMeasureVariable ,
1409+ )
1410+ self .CFGroup = CFGroup
1411+
13511412 @property
13521413 def filename (self ):
13531414 """The file that the CFReader is reading."""
0 commit comments