[英]Add numpy array as variable to Xarray Dataset
This seems like a very basic operation, but I can't figure out how to do it using the xarray documentation. 这似乎是一个非常基本的操作,但我无法弄清楚如何使用xarray文档来完成它。
I have an xarray DataSet: 我有一个xarray DataSet:
dss
<xarray.DataArray (y: 1000, x: 1334)>
dask.array<shape=(1000, 1334), dtype=uint8, chunksize=(222, 58)>
Coordinates:
band int32 1
* y (y) float64 2.218e+06 2.218e+06 2.218e+06 2.218e+06 2.218e+06 ...
* x (x) float64 1.891e+06 1.891e+06 1.891e+06 1.891e+06 1.891e+06 ...
Attributes:
transform: (30.0, 0.0, -2493045.0, 0.0, -30.0, 3310005.0, 0.0, 0.0, 1.0)
crs: +ellps=GRS80 +lat_0=23 +lat_1=29.5 +lat_2=45.5 +lon_0=-96 +n...
res: (30.0, 30.0)
is_tiled: 1
nodatavals: (nan,)
and a numpy array with the correct dimensions: 和一个具有正确尺寸的numpy数组:
print(np.shape(nmap))
(1000, 1334)
nmap
array([[ 0.15, 0.1 , 0.15, ..., 0.05, 0.05, 0.02],
[ 0.15, 0.1 , 0.05, ..., 0.05, 0.05, 0.05],
[ 0.1 , 0.15, 0.15, ..., 0.05, 0.05, 0.02],
...,
[ 0.02, 0.02, 0.02, ..., 0.02, 0.02, 0.02],
[ 0.02, 0.09, 0.09, ..., 0.02, 0.02, 0.02],
[ 0.02, 0.09, 0.09, ..., 0.02, 0.02, 0.02]])
I would like to add the array to the DataSet. 我想将数组添加到DataSet。 My ultimate goal is to do spatial interpolation using x
and y
to extract interpolated values of nmap
on a new grid. 我的最终目标是使用x
和y
进行空间插值,以在新网格上提取nmap
插值。
Do you want to create a Dataset that contains your numpy array nmap? 是否要创建包含numpy数组nmap的数据集? Or do you want to make an arithmetic dss + nmap
? 或者你想制作算术dss + nmap
?
For the former case, you need to make a Dataset
from dss
first and assign nmap
to it, as your dss
is not a Dataset
but a DataArray
. 对于前一种情况,您需要首先从dss
创建Dataset
并为其分配nmap
,因为您的dss
不是Dataset
而是DataArray
。
To make a Dataset
from DataArray
s, you can pass a dictionary mapping the array name to the DataArray object. 要从DataArray
生成Dataset
,可以传递将数组名称映射到DataArray对象的字典。 If your array is not a DataArray
but a numpy array or dask array, you need a tuple (dimensions, array, [attribute]). 如果您的数组不是DataArray
而是numpy数组或dask数组,则需要一个元组(dimension,array,[attribute])。
ds = xr.Dataset({'dss': dss, 'nmap': (('y', 'x'), nmap)})
Or another way to do the same thing is 或者做同样事情的另一种方法是
ds = xr.Dataset({})
ds['dss'] = ds
ds['nmap'] = (('y', 'x'), nmap)
For the latter case, simply do 对于后一种情况,只需这样做
dss + nmap
Suppose you want to add temperature data to a data set: 假设您要将温度数据添加到数据集:
# Take the following dataset as an example
data_set=xr.Dataset( coords={'lon': (['x', 'y'], lon),
'lat': (['x', 'y'], lat),
'time': pd.date_range('2014-09-06', periods=3)})
temp=np.array([[25, 24, 20, -12],[23, 21, 22, -11]])
data_set["Temperature"]=(['x', 'y', 'time'], temp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.