def filter_2D(data, std, dim, dtype=None): if astropy is None: raise RuntimeError( "Module `astropy` not found. Please install optional dependency with `conda install -c conda-forge astropy" ) if dtype is None: dtype = detect_dtype(data) kernel = Gaussian2DKernel(std) def smooth_raw(data): raw_data = getattr(data, "values", data) result = convolve_fft(raw_data, kernel, boundary="wrap") result[np.isnan(raw_data)] = np.nan return result def smoother(data): dims = dim # this is different from the 1d case return xr.apply_ufunc( smooth_raw, data, vectorize=True, dask="parallelized", input_core_dims=[dims], output_core_dims=[dims], output_dtypes=[dtype], ) return smoother(data)
def filter_2D(data, std, dim, dtype=None): if dtype is None: dtype = detect_dtype(data) kernel = Gaussian2DKernel(std) def smooth_raw(data): raw_data = getattr(data, "values", data) result = convolve_fft(raw_data, kernel, boundary="wrap") result[np.isnan(raw_data)] = np.nan return result def smoother(data): dims = dim # this is different from the 1d case return xr.apply_ufunc( smooth_raw, data, vectorize=True, dask="parallelized", input_core_dims=[dims], output_core_dims=[dims], output_dtypes=[dtype], ) return smoother(data)
def filter_1D(data, std, dim="time", dtype=None): if dtype is None: dtype = detect_dtype(data) kernel = Gaussian1DKernel(std) def smooth_raw(data): raw_data = getattr(data, "values", data) result = convolve_fft(raw_data, kernel, boundary="wrap") result[np.isnan(raw_data)] = np.nan return result def temporal_smoother(data): dims = [dim] return xr.apply_ufunc( smooth_raw, data, vectorize=True, dask="parallelized", input_core_dims=[dims], output_core_dims=[dims], output_dtypes=[dtype], ) return temporal_smoother(data)
def xr_linregress(a, b, dim="time", convert_to_dataset=True, dtype=None, nanmask=False): """Applies scipy.stats.linregress over two xr.DataArrays or xr.Datasets. Parameters ---------- a : {xr.DataArray} Independent variable for linear regression. E.g. time. b : {xr.DataArray, xr.Dataset} Dependent variable. dim : str Dimension over which to perform linear regression. Must be present in both `a` and `b` (the default is 'time'). convert_to_dataset : bool Converts the output parameter to data_variables instead of an additional dimension (the default is True). dtype : dtype Dtype for the output. If None, defaults to dtype of `b`, or `b`s first data variable(the default is None). nanmask: bool optional masking of nans in the data to avoid nan output from regression (the default is False) Returns ------- type(b) Returns a dataarray containing the parameter values of scipy.stats.linregress for each data_variable in `b`. """ if dtype is None: dtype = detect_dtype(b) stats = xr.apply_ufunc( _linregress_ufunc, a, b, nanmask, input_core_dims=[[dim], [dim], []], output_core_dims=[["parameter"]], vectorize=True, dask="parallelized", output_dtypes=[dtype], output_sizes={"parameter": 5}, ) stats["parameter"] = xr.DataArray( ["slope", "intercept", "r_value", "p_value", "std_err"], dims=["parameter"] ) if convert_to_dataset: # chug them all into a dataset ds_stats = xr.Dataset() for ff in stats.parameter.data: ds_stats[ff] = stats.sel(parameter=ff) out = ds_stats else: out = stats return out