python-xarray

Apply function along time dimension of XArray

放肆的年华 提交于 2020-04-30 04:10:30
问题 I have an image stack stored in an XArray DataArray with dimensions time, x, y on which I'd like to apply a custom function along the time axis of each pixel such that the output is a single image of dimensions x,y. I have tried: apply_ufunc but the function fails stating that I need to first load the data into RAM (i.e. cannot use a Dask Array). Ideally, I'd like to keep the DataArray as Dask Arrays internally as it isn't possible to load the entire stack into RAM. The exact error message is

Apply function along time dimension of XArray

本小妞迷上赌 提交于 2020-04-30 04:09:49
问题 I have an image stack stored in an XArray DataArray with dimensions time, x, y on which I'd like to apply a custom function along the time axis of each pixel such that the output is a single image of dimensions x,y. I have tried: apply_ufunc but the function fails stating that I need to first load the data into RAM (i.e. cannot use a Dask Array). Ideally, I'd like to keep the DataArray as Dask Arrays internally as it isn't possible to load the entire stack into RAM. The exact error message is

Grouping by multiple dimensions

谁都会走 提交于 2020-04-11 06:59:23
问题 Grouping by a single dimension works fine for xarray DataArrays: d = xr.DataArray([1, 2, 3], coords={'a': ['x', 'x', 'y']}, dims=['a']) d.groupby('a').mean()) # -> DataArray (a: 2) array([1.5, 3. ])` However, this is only supported for a single dimension, grouping by multiple dimensions does thus not work: d = DataAssembly([[1, 2, 3], [4, 5, 6]], coords={'a': ('multi_dim', ['a', 'b']), 'c': ('multi_dim', ['c', 'c']), 'b': ['x', 'y', 'z']}, dims=['multi_dim', 'b']) d.groupby(['a', 'b']) #

Calculate Root Squared Error of xarray dataset

假装没事ソ 提交于 2020-04-11 06:38:10
问题 I have xarray dataset monthly_data of just January's with following info: lat: float64 (192) lon: float64 (288) time: object (1200)(monthly data) Data Variables: tas: (time, lat, lon)[[[45,78,...],...]...] I have ground truth value grnd_trth which has true data of January Coordinates: lat: float64 (192) lon: float64 (288) Data Variables: tas(lat and lon) Now I want to calculate root squared error for each month from monthly_data with respect to grnd_trth , I tried using loops and I guess it's

How to apply linear regression to every pixel in a large multi-dimensional array containing NaNs?

℡╲_俬逩灬. 提交于 2020-03-18 11:14:54
问题 I have a 1D array of independent variable values ( x_array ) that match the timesteps in a 3D numpy array of spatial data with multiple time-steps ( y_array ). My actual data is much larger: 300+ timesteps and up to 3000 * 3000 pixels: import numpy as np from scipy.stats import linregress # Independent variable: four time-steps of 1-dimensional data x_array = np.array([0.5, 0.2, 0.4, 0.4]) # Dependent variable: four time-steps of 3x3 spatial data y_array = np.array([[[-0.2, -0.2, -0.3], [-0.3

How to apply linear regression to every pixel in a large multi-dimensional array containing NaNs?

回眸只為那壹抹淺笑 提交于 2020-03-18 11:13:09
问题 I have a 1D array of independent variable values ( x_array ) that match the timesteps in a 3D numpy array of spatial data with multiple time-steps ( y_array ). My actual data is much larger: 300+ timesteps and up to 3000 * 3000 pixels: import numpy as np from scipy.stats import linregress # Independent variable: four time-steps of 1-dimensional data x_array = np.array([0.5, 0.2, 0.4, 0.4]) # Dependent variable: four time-steps of 3x3 spatial data y_array = np.array([[[-0.2, -0.2, -0.3], [-0.3

Disparity between result of numpy gradient applied directly and applied using xarray.apply_ufunc

ぃ、小莉子 提交于 2020-03-16 07:09:09
问题 I'm trying to use xarray's apply_ufunc to wrap numpy's gradient function, in order to take gradients along one dimension. However, apply_ufunc is returning an array with a different shape to the one which using np.gradient directly returns: import xarray as xr import numpy as np def wrapped_gradient(da, coord): """Finds the gradient along a given dimension of a dataarray.""" dims_of_coord = da.coords[coord].dims if len(dims_of_coord) == 1: dim = dims_of_coord[0] else: raise ValueError(

How to use apply_ufunc with numpy.digitize for each image along time dimension of xarray.DataArray?

雨燕双飞 提交于 2020-02-24 05:44:20
问题 I've rephrased my earlier question substantially for clarity. Per Ryan's suggestion on a separate channel, numpy.digitize looks is the right tool for my goal. I have of an xarray.DataArray of shape x, y, and time. I've trying to puzzle out what values I should supply to the apply_ufunc function's 'input_core_dims' and 'output_core_dims' arguments in order to apply numpy.digitize to each image in the time series. Intuitively, I want the output dimensions to be ['time', 'x', 'y']. I think the

averaging 2 decades of data on 6 hourly timestep using netcdf data and python

大城市里の小女人 提交于 2020-02-04 04:53:04
问题 I have 2 decades of spatially variable wind data recorded at six-hourly intervals. I need to average the 2 decades of data across each six-hourly time interval, so I end up with 365 * 4 time steps. The data is in netcdf format. Here's what the data looks like: import xarray as xr filename = 'V-01011999-01012019.nc' ds = xr.open_dataset(filename) print(ds) <xarray.Dataset> Dimensions: (lat: 8, lon: 7, time: 29221) Coordinates: * lat (lat) float32 -2.5 -5.0 -7.5 -10.0 -12.5 -15.0 -17.5 -20.0 *

Plotting 2D data using Xarray takes a surprisingly long time?

半腔热情 提交于 2020-01-25 07:12:41
问题 I am reading NetCDF files using xarray. Each variable have 4 dimensions ( Times, lev, y, x ). After reading the variable, I calculate the mean of the variable QVAPOR along ( Times,lev ) dimensions. After calculation I get variable QVAPOR_mean which is a 2D variable with shape ( y: 699, x: 639 ). Xarray took only 10micro seconds to read the data with shape ( Times:2918, lev:36, y:699, x:639 ); but took more than 60 minutes to plot the filled contour of the data of shape ( y: 699, x: 639 ). I