FNL by Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
import datetime
def builtSession():
email = "***"
passwd = "***"
loginurl = "https://rda.ucar.edu/cgi-bin/login"
params = {"email":email, "password":passwd, "action":"login"}
sess = requests.session()
sess.post(loginurl,data=params)
return sess
def download(sess, dt):
g1 = datetime.datetime(1999,7,30,18)
g2 = datetime.datetime(2007,12,6,12)
if dt >= g2:
suffix = "grib2"
elif dt >= g1 and dt <g2:
suffix = "grib1"
else:
raise StandardError("DateTime excess limit")
url = "http://rda.ucar.edu/data/ds083.2"
folder = "{}/{}/{}.{:0>2d}".format(suffix, dt.year, dt.year, dt.month)
filename = "fnl_{}.{}".format(dt.strftime('%Y%m%d_%H_00'), suffix)
fullurl = "/".join([url, folder, filename])
r = sess.get(fullurl)
with open(filename, "wb") as fw:
fw.write(r.content)
print(filename + " downloaded")
if __name__ == '__main__':
print("downloading...")
s = builtSession()
for i in range(12):
startdt = datetime.datetime(2018, 5, 16, 0)
interval = datetime.interval(hours = i * 6)
download(s,dt)
print("download completed!")
#Fnl & Gfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import sys
import datetime
import urllib2
import cookielib
__all__ = ['fnl', 'gfs', 'era']
def fnl(dt, locapath):
if not hasattr(dt, 'year'):
return
g1 = datetime.datetime(1999, 7, 30, 18)
g2 = datetime.datetime(2007, 12, 6, 12)
if dt >= g2:
suffix = 'grib2'
elif dt >= g1 and dt < g2:
suffix = 'grib1'
else:
raise StandardError('DateTime excess limit')
path = 'http://rda.ucar.edu/data/ds083.2'
folder = '{}/{}/{}.{:0>2d}'.format(suffix, dt.year, dt.year, dt.month)
filename = 'fnl_{}.{}'.format(dt.strftime('%Y%m%d_%H_00'), suffix)
url = '/'.join([path, folder, filename])
localfile = os.path.join(locapath, 'fnl', str(dt.year), filename)
if os.path.exists(localfile):
return
print(url, localfile)
urlopener = _loadcookie()
_downloader(urlopener, url, localfile)
return localfile
def gfs(dt, locapath):
if not hasattr(dt, 'year'):
return
g1 = datetime.datetime(2006, 11, 1, 0)
if dt < g1:
raise StandardError('DateTime excess limit')
else:
suffix = 'grib2'
path = 'http://rda.ucar.edu/data/ds335.0'
folder = 'GFS0p5/{}'.format(dt.year)
filename = 'GFS_Global_0p5deg_{}_anl.{}'.format(dt.strftime('%Y%m%d_%H00'), suffix)
url = '/'.join([path, folder, filename])
localfile = os.path.join(locapath, 'gfs', str(dt.year), filename)
if os.path.exists(localfile):
return
print(url, localfile)
urlopener = _loadcookie()
_downloader(urlopener, url, localfile)
return localfile
def _downloader(urlopener, url, localfile):
sys.stdout.write('downloading {}...\n'.format(url))
sys.stdout.flush()
try:
infile = urlopener.open(url)
if not os.path.isdir(os.path.dirname(localfile)):
os.makedirs(os.path.dirname(localfile))
outfile = open(localfile, "wb")
outfile.write(infile.read())
outfile.close()
except Exception as e:
print(e)
return
sys.stdout.write('finish download {} and save it into {}\n'.format(url, localfile))
sys.stdout.flush()
def _loadcookie(email='your email', passwd='your passwd'):
cj = cookielib.MozillaCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
do_authentication = False
cj.load("auth.rda.ucar.edu", False, True)
for cookie in cj:
if (cookie.name == "sess" and cookie.is_expired()):
do_authentication = True
if (do_authentication):
sys.stdout.write('Authentication start ...\n')
sys.stdout.flush()
opener.open('https://rda.ucar.edu/cgi-bin/login',
'email={}&password={}&action=login'.format(email, passwd))
cj.clear_session_cookies()
cj.save("auth.rda.ucar.edu", True, True)
sys.stdout.write('Authentication finish and cookie has save into auth.rda.ucar.edu\n')
sys.stdout.flush()
return opener
if __name__ == '__main__':
dt = datetime.datetime(2015, 10, 12, 00)
fnl(dt, r'D:\FnlData')
本文由 xigrug 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:2018-06-22 18:14:31