gpt4 book ai didi

dask - python-xarray:open_mfdataset 沿二维连接

转载 作者:行者123 更新时间:2023-12-04 10:34:37 29 4
gpt4 key购买 nike

我有由 10 个合奏和 35 个时间文件组成的文件。这些文件之一看起来像:

>>> xr.open_dataset('ens1/CCSM4_ens1_07ic_19820701-19820731_NPac_Jul.nc')
<xarray.Dataset>
Dimensions: (ensemble: 1, latitude: 66, longitude: 191, time: 31)
Coordinates:
* ensemble (ensemble) int32 1
* latitude (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude (longitude) float32 100.0 101.0 102.0 103.0 104.0 105.0 106.0 ...
* time (time) datetime64[ns] 1982-07-01 1982-07-02 1982-07-03 ...
Data variables:
u10m (time, latitude, longitude) float64 -1.471 -0.05933 -1.923 ...
Attributes:
CDI: Climate Data Interface version 1.6.5 (http://c...
history: Wed Nov 22 21:54:08 2017: ncks -O -d longitude...
Conventions: CF-1.4
CDO: Climate Data Operators version 1.6.5 (http://c...
nco_openmp_thread_number: 1
NCO: 4.3.7

当我使用 open_mfdataset 文件沿时间维度连接,整体维度被删除(可能是因为它的大小为 1)?
>>> xr.open_mfdataset('ens*/*NPac*.nc')
<xarray.Dataset>
Dimensions: (latitude: 66, longitude: 191, time: 10850)
Coordinates:
* latitude (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude (longitude) float32 100.0 101.0 102.0 103.0 104.0 105.0 106.0 ...
* time (time) datetime64[ns] 1982-07-01 1982-07-02 1982-07-03 ...
Data variables:
u10m (time, latitude, longitude) float64 -1.471 -0.05933 -1.923 ...

我不确定是否也可以沿着整体维度进行连接?

我使用 merge 做了一个简单的测试如此处所示 Error on using xarray open_mfdataset function但它失败了:
>>> ds = xr.open_mfdataset('ens1/*NPac*')
<xarray.Dataset>
Dimensions: (ensemble: 1, latitude: 66, longitude: 191, time: 1085)
Coordinates:
* ensemble (ensemble) int32 1
* latitude (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude (longitude) float32 100.0 101.0 102.0 103.0 104.0 105.0 106.0 ...
* time (time) datetime64[ns] 1982-07-01 1982-07-02 1982-07-03 ...
Data variables:
u10m (time, latitude, longitude) float64 -1.471 -0.05933 -1.923 ...
>>> ds2 = xr.open_mfdataset('ens2/*NPac*')
<xarray.Dataset>
Dimensions: (ensemble: 1, latitude: 66, longitude: 191, time: 1085)
Coordinates:
* ensemble (ensemble) int32 2
* latitude (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude (longitude) float32 100.0 101.0 102.0 103.0 104.0 105.0 106.0 ...
* time (time) datetime64[ns] 1982-07-01 1982-07-02 1982-07-03 ...
Data variables:
u10m (time, latitude, longitude) float64 3.992 2.099 -0.3162 ...
>>> ds3 = xr.merge([ds, ds2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/nethome/rxb826/local/bin/miniconda3/lib/python3.6/site-packages/xarray/core/merge.py", line 513, in merge
variables, coord_names, dims = merge_core(dict_like_objects, compat, join)
File "/nethome/rxb826/local/bin/miniconda3/lib/python3.6/site-packages/xarray/core/merge.py", line 432, in merge_core
variables = merge_variables(expanded, priority_vars, compat=compat)
File "/nethome/rxb826/local/bin/miniconda3/lib/python3.6/site-packages/xarray/core/merge.py", line 166, in merge_variables
merged[name] = unique_variable(name, variables, compat)
File "/nethome/rxb826/local/bin/miniconda3/lib/python3.6/site-packages/xarray/core/merge.py", line 85, in unique_variable
% (name, out, var))
xarray.core.merge.MergeError: conflicting values for variable 'u10m' on objects to be combined:
first value: <xarray.Variable (time: 1085, latitude: 66, longitude: 191)>
dask.array<shape=(1085, 66, 191), dtype=float64, chunksize=(31, 66, 191)>
Attributes:
long_name: 10m U component of wind
units: m s**-1
second value: <xarray.Variable (time: 1085, latitude: 66, longitude: 191)>
dask.array<shape=(1085, 66, 191), dtype=float64, chunksize=(31, 66, 191)>
Attributes:
long_name: 10m U component of wind
units: m s**-1

我正在使用 v0.10.0(感谢最近的更新!)

最佳答案

xarray.open_mfdataset不支持二维合并。您需要做的是使用 concat沿着第二个维度:

import os
import xarray as xr

ens_list = []
for num in range(1, 11):
ens = 'ens%d' % num
ens_list.append(xr.open_mfdataset(os.path.join(ens, '*NPac*')))
ds = xr.concat(ens_list, dim='ensemble')

这是 xarray 用户遇到的常见问题。然而,编写一个通用的 ND concat 例程是相当困难的。

关于dask - python-xarray:open_mfdataset 沿二维连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47545138/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com