python - 使用Python从OpenDAP批量下载数据

标签 python python-xarray opendap

我正在尝试从 OpenDAP 下载多个 .nc 文件。当我手动下载文件(没有脚本)时,文件按预期工作。为了尝试加快该过程,我有一个批量下载数据的脚本。但是,当我使用 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 运行此程序,得到相同的结果。

非常感谢您的帮助。

最佳答案

这些文件中的每一个都是 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

解决此问题的一种简单方法是使用库,例如​​下载每个文件的请求,例如,如此处所述: How to download large file in python with requests.py?

关于python - 使用Python从OpenDAP批量下载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53314156/

相关文章:

python - xarray - 将每日输入数据重新采样为带有闰年的半年数据

numpy - xarray : mean of data stored via OPeNDAP

python - xarray 波周期以秒为单位作为 timedelta64 摄取

python - 使用 Pycharm 的机器人框架——自动完成功能不起作用

python - 尝试将文件作为存档打开,否则作为普通文件读取

python-3.x - xarray 中的乘法

python - 将日期添加到 float 的 DataArray(或反之亦然)

java - 使用netcdf-java库或java中的其他方法访问OPeNDAP数据

python - 如何在 flask-admin 中创建一个额外的字段?

python - 如何在 PyTorch 中获取张量的值?