@@ -478,6 +478,57 @@ def get_all_depth_names(self) -> list[Hashable]:
478478 """
479479 return [c .name for c in self .depth_coordinates ]
480480
481+ def get_depth_coordinate_for_data_array (
482+ self ,
483+ data_array : DataArrayOrName ,
484+ ) -> xarray .DataArray :
485+ """
486+ Find the depth coordinate for a particular data array.
487+ Some conventions will contain multiple depth coordinates,
488+ meaning that a default :attr:`depth_coordinate` value can be misleading.
489+
490+ Parameters
491+ ----------
492+ data_array : xarray.DataArray or Hashable
493+ A data array or the name of a data array in the dataset.
494+
495+ Returns
496+ -------
497+ xarray.DataArray
498+ The depth coordinate variable for the data array.
499+
500+ Raises
501+ ------
502+ NoSuchCoordinateError
503+ If data array does not have an associated depth coordinate
504+ ValueError
505+ If multiple depth coordinates matched the data array.
506+
507+ See also
508+ --------
509+ :attr:`depth_coordinate`
510+ The default or main depth coordinate for this dataset,
511+ but not necessarily the correct depth coordinate for all variables.
512+ :attr:`depth_coordinates`
513+ All the depth coordinates in this dataset.
514+ """
515+ data_array = utils .name_to_data_array (self .dataset , data_array )
516+ name = repr (data_array .name ) if data_array .name is not None else 'data array'
517+
518+ candidates = [
519+ coordinate
520+ for coordinate in self .depth_coordinates
521+ if coordinate .dims [0 ] in data_array .dims
522+ ]
523+ if len (candidates ) == 0 :
524+ raise NoSuchCoordinateError (f"No depth coordinate found for { name } " )
525+ if len (candidates ) > 1 :
526+ raise ValueError (
527+ f"Multiple possible depth coordinates found for { name } : "
528+ ", " .join (repr (c .name ) for c in candidates )
529+ )
530+ return candidates [0 ]
531+
481532 @abc .abstractmethod
482533 def ravel_index (self , index : Index ) -> int :
483534 """Convert a convention native index to a linear index.
0 commit comments