@@ -268,21 +268,19 @@ def bind(self) -> None:
268268 "cannot assign a new convention." )
269269 state .bind_convention (self )
270270
271+ @abc .abstractmethod
271272 def _get_data_array (self , data_array : DataArrayOrName ) -> xarray .DataArray :
272273 """
273274 Utility to help get a data array for this dataset.
274275 If a string is passed in, the matching data array is fetched from the dataset.
275- If a data array is passed in, it is inspected to ensure the dimensions match
276+ If a data array is passed in,
277+ it is inspected to ensure the surface dimensions align
276278 before being returned as-is.
277279
278280 This is useful for methods that support being passed either
279281 the name of a data array or a data array instance.
280282 """
281- if isinstance (data_array , xarray .DataArray ):
282- utils .check_data_array_dimensions_match (self .dataset , data_array )
283- return data_array
284- else :
285- return self .dataset [data_array ]
283+ pass
286284
287285 @cached_property
288286 def time_coordinate (self ) -> xarray .DataArray :
@@ -1011,6 +1009,9 @@ def animate_on_figure(
10111009 if coordinate is None :
10121010 # Assume the user wants to plot along the time axis by default.
10131011 coordinate = self .get_time_name ()
1012+ if isinstance (coordinate , xarray .DataArray ):
1013+ utils .check_data_array_dimensions_match (
1014+ self .dataset , coordinate , dimensions = coordinate .dims )
10141015
10151016 coordinate = self ._get_data_array (coordinate )
10161017
@@ -1787,6 +1788,23 @@ def get_grid_kind(self, data_array: xarray.DataArray) -> GridKind:
17871788 return kind
17881789 raise ValueError ("Unknown grid kind" )
17891790
1791+ def _get_data_array (self , data_array : DataArrayOrName ) -> xarray .DataArray :
1792+ if isinstance (data_array , xarray .DataArray ):
1793+ grid_kind = self .get_grid_kind (data_array )
1794+ grid_dimensions = self .grid_dimensions [grid_kind ]
1795+ for dimension in grid_dimensions :
1796+ # The data array already has matching dimension names
1797+ # as we found the grid kind using `Convention.get_grid_kind()`.
1798+ if self .dataset .sizes [dimension ] != data_array .sizes [dimension ]:
1799+ raise ValueError (
1800+ f"Mismatched dimension { dimension !r} , "
1801+ "dataset has size {self.dataset.sizes[dimension]} but "
1802+ "data array has size {data_array.sizes[dimension]}!"
1803+ )
1804+ return data_array
1805+ else :
1806+ return self .dataset [data_array ]
1807+
17901808 @abc .abstractmethod
17911809 def unpack_index (self , index : Index ) -> Tuple [GridKind , Sequence [int ]]:
17921810 """Convert a native index in to a grid kind and dimension indices.
0 commit comments