def units2pint(value):
    """Return the pint Unit for the DataArray units.

    Parameters
    ----------
    value : xr.DataArray or string
      Input data array or expression.

    Returns
    -------
    pint.Unit
      Units of the data array.

    """

    def _transform(s):
        """Convert a CF-unit string to a pint expression."""
        return re.subn(r'\^?(-?\d)', r'**\g<1>', s)[0]

    if isinstance(value, str):
        unit = value
    elif isinstance(value, xr.DataArray):
        unit = value.attrs['units']
    elif isinstance(value, units.Quantity):
        return value.units
    else:
        raise NotImplementedError("Value of type {} not supported.".format(type(value)))

    try:  # Pint compatible
        return units.parse_expression(unit).units
    except (pint.UndefinedUnitError, pint.DimensionalityError):  # Convert from CF-units to pint-compatible
        return units.parse_expression(_transform(unit)).units
def units2pint ( value ) : def _transform ( s ) : return re . subn ( r'\^?(-?\d)' , r'**\g<1>' , s ) [ 0 ] if isinstance ( value , str ) : unit = value elif isinstance ( value , xr . DataArray ) : unit = value . attrs [ 'units' ] elif isinstance ( value , units . Quantity ) : return value . units else : raise NotImplementedError ( "Value of type {} not supported." . format ( type ( value ) ) ) try : return units . parse_expression ( unit ) . units except ( pint . UndefinedUnitError , pint . DimensionalityError ) : return units . parse_expression ( _transform ( unit ) ) . units
Return the pint Unit for the DataArray units.

    Parameters
    ----------
    value : xr.DataArray or string
      Input data array or expression.

    Returns
    -------
    pint.Unit
      Units of the data array.