maximum_wind_speed.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from abstract_api import AbstractApi
  2. from apis.operate_mysql import OperateMysql
  3. import datetime
  4. class Maximum_wind_speed(AbstractApi):
  5. """返回最大风速(给定时段内的每隔10分钟的平均风速中的最大值)"""
  6. def operation(self, request):
  7. operate_mysql = OperateMysql()
  8. wind_direction = request['basic_datas'][0] # 风向,如c1
  9. wind_speed = request['basic_datas'][1] # 风速,如c2
  10. res_wind_speed = operate_mysql.return_result(request, wind_speed.replace('c', ''))
  11. begin_time = res_wind_speed['begin_time']
  12. end_time = res_wind_speed['end_time']
  13. count = (int(request['end_time']) - int(request['begin_time'])) / 600
  14. max_speed = 0
  15. status = 0 # 状态,用于判断所查询的时间段内是否有数据
  16. while count > 0:
  17. time1 = begin_time + datetime.timedelta(minutes=10)
  18. sql1 = "SELECT avg(%s) FROM %s WHERE times >= \'%s\' and times < \'%s\';" % (wind_speed, res_wind_speed['table_name'], begin_time, time1)
  19. res1 = operate_mysql.execute_sql(sql1)
  20. speed = res1[0]["avg(" + wind_speed + ")"]
  21. if speed is not None:
  22. status = 1 # 如果有数据,修改status,进行下一步查询
  23. if speed > max_speed:
  24. max_speed = speed
  25. return_begin_time = begin_time
  26. return_end_time = time1
  27. count = count - 1
  28. begin_time = time1
  29. if status == 1:
  30. res_wind_direction = operate_mysql.return_result(request, wind_direction.replace('c', ''))
  31. sql2 = "SELECT avg(%s) FROM %s WHERE times >= \'%s\' and times < \'%s\';" % (wind_direction, res_wind_direction['table_name'], return_begin_time, return_end_time)
  32. res2 = operate_mysql.execute_sql(sql2)
  33. max_wind_direction = res2[0]['avg(' + wind_direction + ')']
  34. res = {request['keys'][0]: ("%.2f" % max_wind_direction), request['keys'][1]: ("%.2f" % max_speed), request['keys'][2]: return_begin_time.strftime('%Y-%m-%d %H:%M:%S')}
  35. return res
  36. else:
  37. return None