繁体   English   中英

使用 xarray 和 open_mfdataset 从 url 打开多个文件

[英]Open multiple files from urls using xarray and open_mfdataset

我正在尝试下载 2015-2050 年的多个 CMIP6 数据文件,用于高分辨率阵风。 此处获取的数据集中共有 432 个文件(用于缩小搜索范围的搜索词请参见屏幕截图)。

其中有 432 个文件,我可以通过右键单击 OpenDAP 下载按钮(在屏幕截图中以红色突出显示)并将 URL 粘贴到open_mfdataset函数中来单独打开它们,如下所示:

ds = xarray.open_mfdataset(('http://esgf-data3.ceda.ac.uk/thredds/dodsC/esg_cmip6/CMIP6/HighResMIP/CMCC/CMCC-CM2-VHR4/highres-future/r1i1p1f1/6hrPlevPt/sfcWind/gn/v20190509/sfcWind_6hrPlevPt_CMCC-CM2-VHR4_highres-future_r1i1p1f1_gn_201501010000-201501311800.nc',
                             'http://esgf-data3.ceda.ac.uk/thredds/dodsC/esg_cmip6/CMIP6/HighResMIP/CMCC/CMCC-CM2-VHR4/highres-future/r1i1p1f1/6hrPlevPt/sfcWind/gn/v20190509/sfcWind_6hrPlevPt_CMCC-CM2-VHR4_highres-future_r1i1p1f1_gn_201502010000-201502281800.nc'))

这工作正常,但是有 432 个文件,这样做需要很长时间 - 我已经尝试了其他方法,但觉得有一种方法可以使用 xarray 有效地执行此操作,但我缺少 - 我真的很感激一些帮助。 谢谢你。

编辑:我使用下面屏幕截图中的“THREDDS Catalog”链接和以下代码使其工作:

df = pd.read_html('http://esg.lasg.ac.cn/thredds/catalog/esgcet/180/CMIP6.HighResMIP.CAS.FGOALS-f3-H.highresSST-future.r1i1p1f1.6hrPlevPt.psl.gr.v20200521.html#CMIP6.HighResMIP.CAS.FGOALS-f3-H.highresSST-future.r1i1p1f1.6hrPlevPt.psl.gr.v20200521', skiprows = 1)

df = df[0]

#get all relevant (432 files)
df = df[:432]

#add the url to each datafile to create a downloadable link for each
df['url'] = 'http://esg.lasg.ac.cn/thredds/dodsC/esg_dataroot/CMIP6/HighResMIP/CAS/FGOALS-f3-H/highresSST-future/r1i1p1f1/6hrPlevPt/psl/gr/v20200521/' + df['CMIP6.HighResMIP.CAS.FGOALS-f3-H.highresSST-future.r1i1p1f1.6hrPlevPt.psl.gr'].astype(str)

filelist = df['url'].tolist()

#do the first 10 to see if it works (change the number)
test = filelist[:10]

#do the first 10 into a dataset
ds = xarray.open_mfdataset(test)

正如我们所知,我们不能使用通配符来访问来自 openDAP 服务器的数据。 但是我们可以利用 CMIP 模型输出文件的规则结构并手动构建文件名:

import calendar
import pandas as pd
import xarray as xr

# base url
base_url = 'http://esgf-data3.ceda.ac.uk/thredds/dodsC/esg_cmip6/CMIP6/HighResMIP/CMCC/CMCC-CM2-VHR4/highres-future/r1i1p1f1/6hrPlevPt/sfcWind/gn/v20190509/sfcWind_6hrPlevPt_CMCC-CM2-VHR4_highres-future_r1i1p1f1_gn_'

# period of interest
pr = pd.period_range(start='2015-01',end='2050-12', freq='M')

file_list=[]
for dt in pr:
    # get recent year and month
    year = dt.strftime('%Y')
    month = dt.strftime('%m')

    # get last day of month (no leap years in CMIP)
    last_day_of_month = str(calendar.monthrange(dt.year, dt.month)[1])
    if last_day_of_month == '29':
        last_day_of_month = '28'
        
    # build complete file name
    single_file=(base_url+year+month+'010000-'+year+month+last_day_of_month+'1800.nc')
    file_list.append(single_file)
    
ds=xr.open_mfdataset(file_list)

暂无
暂无

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

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