Fix get_current_weather function.

This commit is contained in:
NMC-DAVE 2021-02-17 13:20:28 +08:00
parent c019192c4b
commit 1c73a442c7
2 changed files with 23 additions and 14 deletions

@ -8,17 +8,28 @@ Retrieve current weather from CMA restful API.
"""
from datetime import datetime
import urllib3
import hashlib
def get_current_weather(lon, lat, apikey, pwd, elements=None, url_only=False):
"""
通过基于位置的天气实况服务接口简称位置服务接口调用实况天气.
位置服务接口支持两种应用场景的应用场景和线的应用场景
根据点或线的经纬度信息获取气温相对湿度风速风向天气现象能见度
总云量海表温度以及降水等气象要素数据
在中国气象数据网http://data.cma.cn上申请API账户审核通过后
获得API账户, 加上用户注册密码即可获得数据.
Args:
points ([type]): [description]
apikey ([type], optional): [description]. Defaults to None.
elements (str, optional): [description]. Defaults to None.
lon (float or list): longitudes
lat (float or list): latitudes
apikey (str): apikey.
pwd (str): user password.
elements (str, optional): weather elements. Defaults to None.
url_only: only retur url string.
"""
# construct parameters
@ -59,3 +70,12 @@ def get_current_weather(lon, lat, apikey, pwd, elements=None, url_only=False):
url_str = 'https://music.data.cma.cn/lbs/api?' + sign_str
if url_only:
return url_str
# request http contents
http = urllib3.PoolManager()
req = http.request('GET', url_str)
if req.status != 0:
print('Can not access the url: ' + url_str)
return None
return req.data

@ -104,17 +104,6 @@ def get_rest_result(interface_id, params, url_only=False,
return req.data
params = {
'serviceNodeId':'NMIC_MUSIC_CMADAAS',
'userId':'******',
'pwd':'******',
'dataCode':'SURF_CHN_MUL_HOR_N',
'elements':'Datetime,Station_Id_d,Lat,Lon,PRE_24h',
'times':'20200910000000',
'dataFormat':'json',
'limitCnt':'10'}
url = get_rest_result('getSurfEleByTime', params)
def cmadaas_obs_convert_type(obs_data):
"""