最近许多人问如何下载fnl,写了个脚本,只用修改email,password(你在官网注册的账号密码,CISL RDA: NCEP FNL Operational Model Global Tropospheric Analyses, continuing from July 1999),以及你需要下载的起始时间stime ,etime
环境python3.6
```
#!/usr/bin/env python
#阿蒋
import sys, os
import requests
import datetime
def check_file_status(filepath, filesize):
sys.stdout.write('\r')
sys.stdout.flush()
size = int(os.stat(filepath).st_size)
percent_complete = (size/filesize)*100
sys.stdout.write('%.3f %s' % (percent_complete, '% Completed'))
sys.stdout.flush()
url = 'https://rda.ucar.edu/cgi-bin/login'
values = {'email' : '******', 'passwd' : ******, 'action' : 'login'}
# Authenticate
ret = requests.post(url,data=values)
if ret.status_code != 200:
print('Bad Authentication')
print(ret.text)
exit(1)
dspath = 'http://rda.ucar.edu/data/ds083.2/'
stime = datetime.datetime(2020,3,27)
etime = datetime.datetime(2020,3,27)
shour = ['_00','_06','_12','_18']
filelist=[]
while stime <= etime:
for j in range(4):
print ('grib2/'+stime.strftime('%Y')+'/'+stime.strftime('%Y.%m')+'/fnl_'
+stime.strftime('%Y%m%d') + shour[j] + '_00.grib2')
filelist.append('grib2/'+stime.strftime('%Y')+'/'+stime.strftime('%Y.%m')+'/fnl_'
+stime.strftime('%Y%m%d') + shour[j] + '_00.grib2')
stime = stime + datetime.timedelta(days=1)
for file in filelist:
filename=dspath+file
file_base = os.path.basename(file)
print('Downloading',file_base)
req = requests.get(filename, cookies = ret.cookies, allow_redirects=True, stream=True)
filesize = int(req.headers['Content-length'])
with open(file_base, 'wb') as outfile:
chunk_size=1048576
for chunk in req.iter_content(chunk_size=chunk_size):
outfile.write(chunk)
if chunk_size < filesize:
check_file_status(file_base, filesize)
check_file_status(file_base, filesize)
print()
```