立即注册 登录
气象家园 返回首页

许市的个人空间 http://bbs.06climate.com/?11488 [收藏] [复制] [分享] [RSS]

日志

python 下fnl

已有 145 次阅读2020-3-31 21:19 |个人分类:python

最近许多人问如何下载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()

全部作者的其他最新日志

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

返回顶部