【发布时间】:2020-12-29 10:49:07
【问题描述】:
我有几个逗号分隔的数据文件,我想将它们加载到 xarray 数据集中。每个文件中的每一行代表固定网格中字段的不同空间值,每个文件代表不同的时间点。网格间距是固定的,不会随时间变化。网格的间距不均匀。最终目标是计算 max_{x, y} { std_t[ value(x, y, t) * sqrt(y **2 + x ** 2)] },其中 sqrt 是平方根,std_t 是相对于时间的标准差,max_{x, y} 是所有空间的最大值。
我无法加载数据。我不清楚应该如何将多个 CSV 文件加载到 xarray 数据集中。有一个open_mfdataset 函数,旨在将多个数据文件加载到一个数据集中,但似乎需要 hdf5 或 netcdf 文件。
似乎无法将常规 CSV 文件加载到 xarray 数据集中,因此需要对数据进行预处理。在我的示例中,我决定预先将 csv 文件预处理为 hdf5 文件,以利用 h5netcdf 引擎。这对我来说似乎是一个 hdf5 特有的问题。
以下是我迄今为止加载数据的最佳尝试。不幸的是,它会导致一个空的 xarray 数据集。我在open_mfdataset函数中尝试了几个选项,下面的代码只是多次尝试使用该函数的一种实现。
如何将这些 csv 文件加载到单个 xarray 数据集中,以便自己找到感兴趣值的时间标准偏差的最大跨空间?
import xarray as xr
import numpy as np
import pandas as pd
'''
Create example files
- Each file contains a spatial-dependent value, f(x, y)
- Each file represents a different point in time, f(x, y, t)
'''
for ii in range(7):
# create csv file
fl = open('exampleFile%i.dat' % ii, 'w')
fl.write('time x1 x2 value\n')
for xx in range(10):
for yy in range(10):
fl.write('%i %i %i %i\n' %
(ii, xx, yy, (xx - yy) * np.exp(ii)))
fl.close()
# convert csv to hdf5
dat = pd.read_csv('exampleFile%i.dat' % ii)
dat.to_hdf('exampleFile%i.hdf5' % ii, 'data', mode='w')
'''
Read all files into xarray dataframe
(the ultimate goal is to find the
maximum across time of
the standard deviation across space
of the "value" column)
'''
result = xr.open_mfdataset('exampleFile*.hdf5', engine='h5netcdf', combine='nested')
...当我运行代码时,result 变量似乎不包含所需的数据:
In: result
Out:
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
PYTABLES_FORMAT_VERSION: 2.1
TITLE: Empty(dtype=dtype('S1'))
VERSION: 1.0
编辑
发布了一个假设均匀间隔的空间网格的答案。这是一个稍微修改的示例,它不假设空间点的均匀间隔网格。
该示例还假设了三个空间维度。这更符合我的实际问题,我意识到这可能是这个简单示例中的一个重要细节。
import xarray as xr
import numpy as np
import pandas as pd
'''
Create example files
- Each file contains a spatial-dependent value, f(x, y)
- Each file represents a different point in time, f(x, y, t)
'''
for ii in range(7):
# create csv file
fl = open('exampleFile%i.dat' % ii, 'w')
fl.write('time x y z value\n')
for xx in range(10):
for yy in range(int(10 + xx // 2)):
for zz in range(int(10 + xx //3 + yy // 3)):
fl.write('%i %f %f %f %f\n' %
(ii, xx * np.exp(- 1 * yy * zz) , yy * np.exp(xx - zz), zz * np.exp(xx * yy), (xx - yy) * np.exp(ii)))
fl.close()
# convert csv to hdf5
dat = pd.read_csv('exampleFile%i.dat' % ii)
dat.to_hdf('exampleFile%i.hdf5' % ii, 'data', mode='w')
'''
Read all files into xarray dataframe
(the ultimate goal is to find the
maximum across time of
the standard deviation across space
of the "value" column)
'''
result = xr.open_mfdataset('exampleFile*.hdf5', engine='h5netcdf', combine='nested')
【问题讨论】:
-
所以你的csv中的
x1是x坐标,x2是y? -
@Val 是的,没错。很抱歉造成混乱,感谢您的澄清。
-
有什么理由必须使用
xarray而不仅仅是pandas? -
@Val 基本上没有。我认为这个时空数据是一个 3D 数据框,所以我认为 xarray 是最合适的。一个功能性的 pandas 解决方案肯定会很有启发性,并会解决我的根本问题。
-
@kiloules 这实际上是您问题的核心 - xarray 并不擅长处理 ND 网格中的不规则点云。它设计用于操作规则网格。所以你有两个选择 - 如果你在每个观察集中有相同(或相似)数量的点,并且你将一次在一个暗淡上做很多操作(就像这个空间标准的例子)那么你可以将 obs_id 和 time 视为您的绳索,其中 x 和 y 是 obs_id 上的非索引坐标(本质上是元数据)。否则,我会推荐 pandas 并为此使用 group by ops。
标签: python python-xarray hdf