# 任务需求 从数据库取重点时刻前后各两小时的aqi求平均,画点图、面图
import pandas as pd
from datetime import timedelta, datetime
import numpy as np
from sqlalchemy import create_engine
import matplotlib as mpl
mpl.use("Agg")
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import matplotlib.font_manager as fm
zhfont1 = fm.FontProperties(fname='/home/gzblue/xgx/xx_copy_files/SIMKAI.TTF')
def get_station_aqi_mean(station, t_start, t_end):
"""求站点重点时刻平均AQI
:param station: 站点ID,例如'51010101001'
:param t_start: 开始时间,time类型或类似时间str
:param t_end: 结束时间,time类型或类似时间str
:return: 重点时刻平均AQI
"""
sql_engine = create_engine('mssql+pymssql://xxxx:xxxx@xxxx:xxxx/AQI_HKY')
data = pd.read_sql_query(
"SELECT MN_, DATA_TIME_,AQI_ FROM MS_HOUR_DATA where MN_='%s' and DATA_TIME_>='%s' and DATA_TIME_<='%s'"
% (station, t_start, t_end), sql_engine)
# print(data)
# print(data['AQI_'].mean())
return data['AQI_'].mean()
def main(station, goal_time):
titlename = str(goal_time)
savepath = r'/data01/home/gzblue/xgx/1205/chengduguoshi_plot/data/' + str(goal_time) + 'new_.png'
time_mid = datetime.strptime(goal_time, '%Y%m%d%H')
time_start = time_mid - timedelta(minutes=120)
time_end = time_mid + timedelta(minutes=120)
print(time_start)
print(time_end)
# 取数据
station['AQI'] = None
for mn in station['MN_'].values:
station.loc[mn, 'AQI'] = get_station_aqi_mean(mn, time_start, time_end)
print(station)
# 画图
station_list = ['51010101001', '51010401001', '51010501001', '51010501002',
'51010601002', '51010801001', '51010901001']
map = Basemap(llcrnrlon=102.8, llcrnrlat=30, urcrnrlon=105, urcrnrlat=31.5, projection='lcc',
lat_1=30, lat_2=60, lat_0=33.3, lon_0=103.3, resolution='l')
station.index = range(len(station))
for ss in station_list:
for i in range(40):
if station.loc[i, 'MN_'] == ss:
lons1 = station.loc[i, 'LONG_']
lats1 = station.loc[i, 'LAT_']
x1, y1 = map(lons1, lats1)
if station.loc[i, 'AQI'] < 50:
map.scatter(x1, y1, marker='s', color='limegreen', s=20)
elif station.loc[i, 'AQI'] < 100:
map.scatter(x1, y1, marker='s', color='yellow', s=20)
elif station.loc[i, 'AQI'] < 150:
map.scatter(x1, y1, marker='s', color='darkorange', s=20)
elif station.loc[i, 'AQI'] < 200:
map.scatter(x1, y1, marker='s', color