V1.0功能:AQI计算
案例描述:
案例分析:
- 实现步骤:
- 实现每种污染物对应的AQI函数,一个输入参数,即Cp
- 实现线性缩放函数,5个输入参数
上机实验:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # author: Kevin.Wang 4 # time : 2018/10/16 5 6 7 """ 8 作者:王鑫正 9 版本:1.0 10 日期:2018/10/16 11 功能:AQI计算 12 """ 13 14 15 def cal_linear(iaqi_lo, iaqi_hi, bp_lo, bp_hi, cp): 16 """ 17 范围缩放 18 """ 19 iaqi = (iaqi_hi - iaqi_lo) * (cp - bp_lo) / (bp_hi - bp_lo) + iaqi_lo 20 return iaqi 21 22 23 def cal_pm_iaqi(pm_val): 24 """ 25 计算PM2.5的IAQI 26 """ 27 if 0 <= pm_val < 35: 28 iaqi = cal_linear(0, 50, 0, 35, pm_val) 29 elif 35 <= pm_val < 75: 30 iaqi = cal_linear(50, 100, 35, 75, pm_val) 31 elif 75 <= pm_val < 115: 32 iaqi = cal_linear(100, 150, 75, 115, pm_val) 33 else: 34 pass 35 36 return iaqi 37 38 39 def cal_co_iaqi(co_val): 40 """ 41 计算CO的IAQI 42 """ 43 if 0 <= co_val < 3: 44 iaqi = cal_linear(0, 50, 0, 3, co_val) 45 elif 3 <= co_val < 5: 46 iaqi = cal_linear(50, 100, 2, 4, co_val) 47 48 else: 49 pass 50 51 return iaqi 52 53 54 def cal_aqi(para_list): 55 """ 56 AQI计算 57 """ 58 pm_val = para_list[0] 59 co_val = para_list[1] 60 61 pm_iaqi = cal_pm_iaqi(pm_val) 62 co_iaqi = cal_co_iaqi(co_val) 63 64 iaqi_list = [] 65 iaqi_list.append(pm_iaqi) 66 iaqi_list.append(co_iaqi) 67 68 aqi = max(iaqi_list) 69 return aqi 70 71 72 def main(): 73 """ 74 主函数 75 """ 76 print(‘请输入以下信息,用空格分隔‘) 77 input_str = input(‘(1)PM2.5 (2)CO:‘) 78 str_list = input_str.split(‘ ‘) 79 pm_val = float(str_list[0]) 80 co_val = float(str_list[1]) 81 82 para_list = [] 83 para_list.append(pm_val) 84 para_list.append(co_val) 85 86 # 调用AQI计算函数 87 aqi_val = cal_aqi(para_list) 88 89 print(‘空气质量指数为:{}‘.format(aqi_val)) 90 91 92 if __name__ == ‘__main__‘: 93 main()
原文地址:https://www.cnblogs.com/Kevin-WangXinzheng/p/9801041.html
时间: 2024-10-25 04:57:48