亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 xarray 和 open_mfdataset 從 url 打開多個文件

使用 xarray 和 open_mfdataset 從 url 打開多個文件

侃侃無極 2023-07-27 10:29:47
我正在嘗試下載 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 eachdf['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 datasetds = xarray.open_mfdataset(test)
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

正如我們所知,我們不能使用通配符運算符來訪問 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)


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 282 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號