1

我正在尝试.nc从 OpenDAP 下载多个文件。当我手动下载文件(没有脚本)时,文件按预期工作。为了加快这个过程,我有一个批量下载数据的脚本。但是,当我使用文件下载数据时xarray,文件大了 10 倍,并且文件似乎已损坏。

我的脚本如下所示:

import pandas as pd
import xarray as xr
import os
import numpy as np

dates = pd.date_range(start='2016-01-01',end='2016-01-05',freq='D')
my_url = "http://www.ifremer.fr/opendap/cerdap1/ghrsst/l4/saf/odyssea-nrt/data/"

print("  ")
print("Downloading data from OPeNDAP - sit back, relax, this will take a while...")
print("...")
print("...")

# Create a list of url's 
data_url = []
cnt = 0
for i in np.arange(1,5):
    ii = i+1

    data_url.append(my_url + str(dates[cnt].year)+"/"+ str('%03d'%+ii)+"/"\
        +str(dates[cnt+1].year)+str('%02d'%dates[cnt+1].month)+str('%02d'%dates[cnt+1].day)\
        +"-IFR-L4_GHRSST-SSTfnd-ODYSSEA-SAF_002-v2.0-fv1.0.nc?time[0:1:0],lat[0:1:1749],lon[0:1:2249],analysed_sst[0:1:0][0:1:1749][0:1:2249],analysis_error[0:1:0][0:1:1749][0:1:2249],mask[0:1:0][0:1:1749][0:1:2249],sea_ice_fraction[0:1:0][0:1:1749][0:1:2249]")

    cnt = cnt+1

url_list = data_url

# Download data from the url's
count = 0
for data in url_list:
    print('Downloading file:', str(count))
    ds = xr.open_dataset(data,autoclose=True)
    fname = 'SAFodyssea_sst'+str(dates[count+1].year)+str('%02d'%dates[count+1].month)+str('%02d'%dates[count+1].day)+'.nc'
    ds.to_netcdf(fname)
    count = count +1
    del ds, fname

print('DONE !!!')

我有xarray版本 0.10.8。我尝试使用 python 2.7 和 python 3.5.6 以及在 Windows 10 和 Ubuntu 16.04 上运行它,我得到了相同的结果。

非常感谢您的帮助。

4

1 回答 1

1

这些文件中的每一个都作为 netCDF 文件的关联 URL,例如 http://www.ifremer.fr/opendap/cerdap1/ghrsst/l4/saf/odyssea-nrt/data/2018/001/20180101-IFR-L4_GHRSST -SSTfnd-ODYSSEA-SAF_002-v2.0-fv1.0.nc

解决此问题的一种简单方法是使用诸如 requests 之类的库来下载每个文件,例如,如下所述: 如何使用 requests.py 在 python 中下载大文件?

于 2018-11-16T23:45:19.403 回答