python-xarray

Expand dimensions xarray

↘锁芯ラ 提交于 2019-12-10 19:17:59
问题 Is there an existing method or approach to expand the dimensions (and coordinates) of an xarray.DataArray object? I would like to obtain something similar to np.expand_dims while at the same time defining a new dimension and coordinate variable for the new expanded DataArray . Using DataArray.assign_coords() I can create a new coordinate variable but the array itself is not expanded with a new axis. 回答1: In xarray v0.10.0, I use a combination of assign_coords() and expand_dims() to add a new

Error on using xarray open_mfdataset function

爱⌒轻易说出口 提交于 2019-12-10 10:37:18
问题 I am trying to combine multiple netCDF files with the same dimensions, their dimensions are as follows: OrderedDict([(u'lat', <type 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 720 ), (u'lon', <type 'netCDF4._netCDF4.Dimension'>: name = 'lon', size = 1440 ), (u'time', <type 'netCDF4._netCDF4.Dimension'>: name = 'time', size = 96 ), (u'nv', <type 'netCDF4._netCDF4.Dimension'>: name = 'nv', size = 2 )]) OrderedDict([(u'lat', <type 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 720 )

Concise way to filter data in xarray

醉酒当歌 提交于 2019-12-09 16:49:37
问题 I need to apply a very simple 'match statement' to the values in an xarray array: Where the value > 0, make 2 Where the value == 0, make 0 Where the value is NaN , make NaN Here's my current solution. I'm using NaN s, .fillna , & type coercion in lieu of 2d indexing. valid = date_by_items.notnull() positive = date_by_items > 0 positive = positive * 2 result = positive.fillna(0.).where(valid) result This changes this: In [20]: date_by_items = xr.DataArray(np.asarray((list(range(3)) * 10))

calculating cross-correlation function in xarray

谁都会走 提交于 2019-12-08 04:50:00
问题 I have a dataset res_1 with Dimensions: (space: 726, time: 579) Coordinates: * space (space) MultiIndex - latitude (space) float64 -90.0 -82.5 -82.5 -82.5 -82.5 -82.5 -82.5 ... - longitude (space) float64 0.0 0.0 60.0 120.0 180.0 240.0 300.0 0.0 30.0 ... * time (time) datetime64[ns] 1980-06-01 1980-06-02 1980-06-03 ... Data variables: mx2t (time, space) float64 -1.768 -0.6035 -1.286 -1.291 1.144 ... dayofyear (time) int64 153 154 155 156 157 158 159 160 161 162 163 164 ... the space variable

How to subset data using multidimensional coordinates using python xarray?

天大地大妈咪最大 提交于 2019-12-07 23:53:06
问题 I have a netcdf file that uses multidimensional coordinates. My xarray dataset looks like this <xarray.Dataset> Dimensions: (Time: 48, bottom_top: 50, bottom_top_stag: 51, soil_layers_stag: 4, south_north: 1015, south_north_stag: 1016, west_east: 1359, west_east_stag: 1360) Coordinates: XLAT (Time, south_north, west_east) float32 18.1363 18.1456 ... XLAT_U (Time, south_north, west_east_stag) float32 18.1316 ... XLAT_V (Time, south_north_stag, west_east) float32 18.1198 ... XLONG (Time, south

Can I parallelize `numpy.bincount` using `xarray.apply_ufunc`?

我是研究僧i 提交于 2019-12-06 15:52:18
I want to parallelize the numpy.bincount function using the apply_ufunc API of xarray and the following code is what I've tried: import numpy as np import xarray as xr da = xr.DataArray(np.random.rand(2,16,32), dims=['time', 'y', 'x'], coords={'time': np.array(['2019-04-18', '2019-04-19'], dtype='datetime64'), 'y': np.arange(16), 'x': np.arange(32)}) f = xr.DataArray(da.data.reshape((2,512)),dims=['time','idx']) x = da.x.values y = da.y.values r = np.sqrt(x[np.newaxis,:]**2 + y[:,np.newaxis]**2) nbins = 4 if x.max() > y.max(): ri = np.linspace(0., y.max(), nbins) else: ri = np.linspace(0., x

How to subset data using multidimensional coordinates using python xarray?

夙愿已清 提交于 2019-12-06 11:56:35
I have a netcdf file that uses multidimensional coordinates. My xarray dataset looks like this <xarray.Dataset> Dimensions: (Time: 48, bottom_top: 50, bottom_top_stag: 51, soil_layers_stag: 4, south_north: 1015, south_north_stag: 1016, west_east: 1359, west_east_stag: 1360) Coordinates: XLAT (Time, south_north, west_east) float32 18.1363 18.1456 ... XLAT_U (Time, south_north, west_east_stag) float32 18.1316 ... XLAT_V (Time, south_north_stag, west_east) float32 18.1198 ... XLONG (Time, south_north, west_east) float32 -122.884 ... XLONG_U (Time, south_north, west_east_stag) float32 -122.901 ...

Create DataArray from Dict of 2D DataFrames/Arrays

♀尐吖头ヾ 提交于 2019-12-06 11:45:31
I'm trying to transition from Pandas into Xarray for N-Dimensional DataArrays to expand my repertoire. Realistically, I'm going to have a bunch of different pd.DataFrames (in this case row=month, col=attribute) along a particular axis (patients in the mock example below) that I would like to merge (w/o using panels or multindex :), thank you). I want to convert them to xr.DataArrays so I can build dimensions upon them. I made a mock dataset to give a gist of what I'm talking about. For this dataset I made up, imagine 100 patients, 12 months, 10000 attributes, 3 replicates (per attribute) which

Error on using xarray open_mfdataset function

雨燕双飞 提交于 2019-12-06 09:23:06
I am trying to combine multiple netCDF files with the same dimensions, their dimensions are as follows: OrderedDict([(u'lat', <type 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 720 ), (u'lon', <type 'netCDF4._netCDF4.Dimension'>: name = 'lon', size = 1440 ), (u'time', <type 'netCDF4._netCDF4.Dimension'>: name = 'time', size = 96 ), (u'nv', <type 'netCDF4._netCDF4.Dimension'>: name = 'nv', size = 2 )]) OrderedDict([(u'lat', <type 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 720 ), (u'lon', <type 'netCDF4._netCDF4.Dimension'>: name = 'lon', size = 1440 ), (u'time', <type 'netCDF4.