简体   繁体   English

如何从 xyz 点数据创建 xarray 数据集?

[英]How to create an xarray dataset from xyz point data?

I am new to xarray and confused at how I am supposed to construct Datasets and DataArrays.我是 xarray 的新手,对我应该如何构建数据集和 DataArrays 感到困惑。 I have xyz point data and each point has 2 data values.我有 xyz 点数据,每个点都有 2 个数据值。

Below is my attempt to do this but I am receiving the error ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): ... to Variable.下面是我这样做的尝试,但我收到错误ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): ... to Variable. I believe this is telling me that my point_data1 and point_data2 need to be 3 dimensional, but I am confused on how to do that in a way that makes sense for my use case.我相信这告诉我我的 point_data1 和 point_data2 需要是 3 维的,但我对如何以对我的用例有意义的方式做到这一点感到困惑。

import numpy as np

num_points = 20
point_locations = np.random.randint(99, size=(num_points, 3))
point_data1= np.ones(num_points)
point_data2 = np.random.randint(5, size=num_points)

ds = xr.Dataset({'point_data1': (['x', 'y', 'z'], point_data1 ),
                 'point_data2 ': (['x', 'y', 'z'], point_data2 )},
                coords={'x': point_locations[:,0], 'y': point_locations[:,1], 'z': point_locations[:,2]})

This seemed to accomplish what I want.这似乎完成了我想要的。 This also allows you to pass in sparse=True to from_dataframe .这也允许您将sparse=True传递给from_dataframe

import numpy as np
import pandas as pd
import xarray as xr

num_points = 20
point_locations = np.random.randint(99, size=(num_points, 3))
point_data1 = np.ones(num_points)
point_data2 = np.random.randint(5, size=num_points)

df = pd.DataFrame()
df['x'] = point_locations[:, 0]
df['y'] = point_locations[:, 1]
df['z'] = point_locations[:, 2]
df['point_data1'] = np.ones(num_points)
df['point_data2'] = np.random.randint(5, size=num_points)
df = df.set_index(['x', 'y', 'z'])
ds = xr.Dataset.from_dataframe(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM