def exec_xar(x, y, reduce=False): """ Compares whether `x` is strictly greater than or equal to `y`. Remarks: - If any operand is None, the return value is None. - If any operand is not a number or temporal string (date, time or date-time), the process returns False. - Temporal strings can not be compared based on their string representation due to the time zone / time-offset representations. Parameters ---------- x : xr.DataArray, integer, float First operand. y : xr.DataArray, integer, float Second operand. reduce : bool, optional If True, one value will be returned. If False, each value in `x` will be compared with the respective value in `y`. Defaults to False. Returns ------- bool : Returns True if `x` is strictly greater than or equal to `y`, None if any operand is None, otherwise False. """ if x is None or y is None: return None elif type(x) in [int, float, xr.DataArray ] and type(y) in [int, float, xr.DataArray]: gte_ar = ((x - y) >= 0) gte_ar = keep_attrs(x, y, gte_ar) if reduce: return gte_ar.all() else: return gte_ar return False
def exec_xar( x, y, delta=False, case_sensitive=True, reduce=False): # TODO: add equal checks for date strings in xar """ Compares whether `x` is strictly equal to `y`. Parameters ---------- x : xr.DataArray, integer, float First operand. y : xr.DataArray, integer, float Second operand. delta : float, optional Only applicable for comparing two arrays containing numbers. If this optional parameter is set to a positive non-zero number the equality of two numbers is checked against a delta value. This is especially useful to circumvent problems with floating point inaccuracy in machine-based computation. case_sensitive : bool, optional Only applicable for comparing two string arrays. Case sensitive comparison can be disabled by setting this parameter to False. reduce : bool, optional If True, one value will be returned, i.e. if the arrays are equal. If False, each value in `x` will be compared with the respective value in `y`. Defaults to False. Returns ------- bool or xr.DataArray : Returns True if `x` is equal to `y`, np.nan if any operand is np.nan, otherwise False. """ if x is None or y is None: return None x_type = x.dtype if isinstance(x, (xr.core.dataarray.DataArray, np.ndarray)) else type(x) y_type = y.dtype if isinstance(y, (xr.core.dataarray.DataArray, np.ndarray)) else type(y) if (x_type in [float, int]) and (y_type in [ float, int ]): # both arrays only contain numbers if type(delta) in [float, int]: ar_eq = (abs(x - y) <= delta) else: ar_eq = x == y elif (isinstance(x, str) or x_type.kind.lower() == 'u') and (isinstance(y, str) or y_type.kind.lower() == 'u'): # comparison of strings or dates # try to convert the string into a date x_time = None # str2time still missing y_time = None if x_time is None or y_time is None: # comparison of strings if case_sensitive: ar_eq = x == y else: x_l = x.str.lower() if isinstance( x, (xr.core.dataarray.DataArray, np.ndarray)) else x.lower() y_l = y.str.lower() if isinstance( y, (xr.core.dataarray.DataArray, np.ndarray)) else y.lower() ar_eq = x_l == y_l else: ar_eq = x_time == y_time # comparison of dates else: ar_eq = x == y ar_eq = keep_attrs(x, y, ar_eq) if reduce: return ar_eq.all() else: return ar_eq