将 xarray.Dataset 列表合并/展平为单个 xarray 可读的 netcdf 格式

问题描述 投票:0回答:1

我循环了几个多维

NetCDF
文件,使用
xarray
函数提取感兴趣的变量,并将输出存储为
xarray.core.dataset.Dataset
列表。我不熟悉
xarray
或这种数据格式。我需要将列表合并或展平为单个 xarray 可读的
NetCDF
文件或
Dataset
格式。任何帮助,将不胜感激。这是我的代码:

import glob
import xarray as xr

all_fwi = glob.glob("D:/FWI_future/fwi_intermediary/fwi_day_CanESM5_ssp245*.nc")


fwi_list = []
for i in all_fwi:
    infile = xr.open_dataset(i, drop_variables=['TEMP_wDC_2014','ffmcPREV_2014', 'dcPREV_2014', 'dmcPREV_2014','SeasonActive_2014', 'DCf_2014', 'rw_2014', 'CounterSeasonActive_2014','ffmc', 'dc','dmc', 'isi', 'bui', 'TEMP', 'RH', 'RAIN', 'WIND'])
    yt = infile.sel(lon=slice(218.9931, 236.2107), lat=slice(60, 69.64794))
    yt1 = yt.sel(time=slice('2020-01-01', '2060-12-31'))
    yt2 = yt1.sel(time=yt1.time.dt.month.isin([3, 4, 5, 6, 7, 8, 9]))
    fwi_list.extend(yt2)

这是 yt2 数据结构:

yt2
<xarray.Dataset> Size: 1MB
Dimensions:    (time: 8774, lat: 3, lon: 6, bnds: 2, days_wDC: 5)
Coordinates:
  * time       (time) object 70kB 2020-03-01 12:00:00 ... 2060-09-30 12:00:00
  * lat        (lat) float64 24B 62.79 65.58 68.37
  * lon        (lon) float64 48B 219.4 222.2 225.0 227.8 230.6 233.4
  * days_wDC   (days_wDC) <U5 100B 'day-2' 'day-1' 'day' 'day+1' 'day+2'
Dimensions without coordinates: bnds
Data variables:
    time_bnds  (time, bnds) object 140kB ...
    lat_bnds   (lat, bnds) float64 48B ...
    lon_bnds   (lon, bnds) float64 96B ...
    fwi        (time, lat, lon) float64 1MB ...

rioxarray.merge
功能本来是理想的,但现在似乎已被弃用,没有替代品。我尝试在
Dataset
中使用
NETCDF4
但收到错误消息:

fin_dat = netCDF4.Dataset(fwi_list, 'w', format='NETCDF4')

ermissionError:[Errno 13]权限被拒绝:“['time_bnds','lat_bnds','lon_bnds','fwi'...

python python-xarray netcdf
1个回答
0
投票

想通了。需要用

extend
替换
append
来生成列表,然后使用
xarrary.concact
连接列表。具体来说:

fwi_list = []
for i in all_fwi:
    infile = xr.open_dataset(i, drop_variables=['TEMP_wDC_2014','ffmcPREV_2014', 'dcPREV_2014', 'dmcPREV_2014','SeasonActive_2014', 'DCf_2014', 'rw_2014', 'CounterSeasonActive_2014','ffmc', 'dc','dmc', 'isi', 'bui', 'TEMP', 'RH', 'RAIN', 'WIND'])
    yt = infile.sel(lon=slice(218.9931, 236.2107), lat=slice(60, 69.64794))
    yt1 = yt.sel(time=slice('2020-01-01', '2060-12-31'))
    yt2 = yt1.sel(time=yt1.time.dt.month.isin([3, 4, 5, 6, 7, 8, 9]))
    fwi_list.append(yt2) # switch extend with append here

final_dat = xr.concat(fwi_list, dim = 'time') #careful not to concatenate inside the loop, it would prolong the run.
© www.soinside.com 2019 - 2024. All rights reserved.