|||
代码运行条件:1.气象部门内网;2.MUSIC接口需要开通API权限(用户名和密码)
# -*- coding: utf-8 -*- """ Modified in 2020年6月3日2 该代码用于下载全国2018年的气象数据,要素主要包括: 1.每小时的降水; 2.每小时的温度; @author: 朱永超 """ # 导入库文件 import suds from datetime import timedelta from datetime import datetime import os # 定义开始和结束时间 onedate = timedelta(days=1) # 定义1天时间 datetime_sta = datetime(2019,1,1,0,0,0) # 开始的时间 datetime_end = datetime(2020,1,1,0,0,0) # 结束的时间 time1 = datetime_sta cwd = os.getcwd() if not os.path.exists(cwd+'\progress.ini'): with open('progress.ini', 'w+', newline='') as f1: f1.write(datetime_sta.strftime('%Y%m%d%H%M%S')) # time1为当前时间 time1 = datetime_sta else: with open('progress.ini', 'r+', newline='') as f1: timeread=datetime.strptime(f1.read(),'%Y%m%d%H%M%S') if timeread>time1: time1 = timeread # MUSIC接口的webservices wsdl = "http://10.20.76.55/cimiss-web/services/ws?wsdl" # 2. 调用接口 client = suds.client.Client(wsdl) # 最大尝试下载次数 tryMax = 0; # 下载数据 while True: # 定义搜索数据的开始和结束时间 time1_str1 = time1.strftime('%Y%m%d%H%M%S') time1_str2 = (time1+onedate).strftime('%Y%m%d%H%M%S') # 搜索数据的参数parameters params = 'userId=username\ &pwd=password\ &interfaceId=getSurfEleByTimeRange\ &dataCode=SURF_CHN_MUL_HOR_N\ &elements=Station_ID_C,Province,Lat,Lon,Year,Mon,Day,Hour,PRE_1h,TEM\ &timeRange=['+time1_str1+','+time1_str2+')\ &orderby=Station_ID_C:ASC\ &dataFormat=csv' # 连接服务器,下载数据 try: result = client.service.callAPI_to_serializedStr(params) tryMax=0 # reset tryMax value except: print('下载数据错误,第'+str(tryMax)+1+'尝试!') if tryMax<10: tryMax=tryMax+1 continue else: print('已超过最大尝试次数,程序结束!') break with open('./2018气象数据/降水和温度数据_,'+time1.strftime('%Y%m%d')+'.csv', 'w+', newline='') as w: w.write(result) print('已完成'+time1.strftime('%Y%m%d')+'数据的下载和写入') time1 = time1+onedate #时间增加一天 with open('progress.ini', 'w+', newline='') as f2: f2.write(time1.strftime('%Y%m%d%H%M%S')) #如果时间大于2018年,则跳出循环 if time1>=datetime_end: break
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 09:06
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社