aqi.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package util
  2. import "math"
  3. func getPollutionDegree(aqi float64) int {
  4. var pollutionDegree int = 1
  5. if aqi <= 50 {
  6. pollutionDegree = 1
  7. } else if aqi > 50 && aqi <= 100 {
  8. pollutionDegree = 2
  9. } else if aqi > 100 && aqi <= 150 {
  10. pollutionDegree = 3
  11. } else if aqi > 150 && aqi <= 200 {
  12. pollutionDegree = 4
  13. } else if aqi > 200 && aqi <= 250 {
  14. pollutionDegree = 5
  15. } else if aqi > 250 && aqi <= 300 {
  16. pollutionDegree = 6
  17. } else if aqi > 300 {
  18. pollutionDegree = 7
  19. }
  20. return pollutionDegree
  21. }
  22. func getDegree(pollutionDegree int) string {
  23. if pollutionDegree == 1 {
  24. return "优"
  25. } else if pollutionDegree == 2 {
  26. return "良"
  27. } else if pollutionDegree == 3 {
  28. return "轻微污染"
  29. } else if pollutionDegree == 4 {
  30. return "轻度污染"
  31. } else if pollutionDegree == 5 {
  32. return "中度污染"
  33. } else if pollutionDegree == 6 {
  34. return "中度重污染"
  35. } else if pollutionDegree == 7 {
  36. return "重度污染"
  37. }
  38. return "良"
  39. }
  40. func countPerIaqi(cp float64, r int) float64 {
  41. var bph float64 = 0 // 与 cp相近的污染物浓度限值的高位值
  42. var bpl float64 = 0 // 与 cp相近的污染物浓度限值的低位值
  43. var iaqih float64 = 0 // 与 bph对应的空气质量分指数
  44. var iaqil float64 = 0 // 与 bpl对应的空气质量分指数
  45. var iaqip float64 = 0 // 当前污染物项目P的空气质量分指数
  46. // 空气质量分指数及对应的污染物项目浓度限值
  47. var aqiArr [3][8]float64 = [3][8]float64{{0, 50, 100, 150, 200, 300, 400, 500}, {0, 50, 150, 250, 350, 420, 500, 600}, {0, 35, 75, 115, 150, 250, 350, 500}}
  48. var min float64 = aqiArr[r][0]
  49. var index int = len(aqiArr[r]) - 1
  50. var max float64 = aqiArr[r][index]
  51. if cp <= min || cp >= max {
  52. return 0.0
  53. } else {
  54. // 对每种污染物的bph、bpl、iaqih、iaqil进行赋值
  55. for i := r; i < (r + 1); i++ {
  56. for j := 0; j < len(aqiArr[0]); j++ {
  57. if cp < aqiArr[i][j] {
  58. bph = aqiArr[i][j]
  59. bpl = aqiArr[i][j-1]
  60. iaqih = aqiArr[0][j]
  61. iaqil = aqiArr[0][j-1]
  62. break
  63. }
  64. }
  65. }
  66. // 计算污染物项目P的空气质量分指数
  67. iaqip = (iaqih-iaqil)/(bph-bpl)*(cp-bpl) + iaqil
  68. return iaqip
  69. }
  70. }
  71. func getPm10IAQI(pmte float64) float64 {
  72. if pmte > 0 {
  73. return countPerIaqi(pmte, 1)
  74. }
  75. return 0
  76. }
  77. func getPm25IAQI(pmtw float64) float64 {
  78. if pmtw > 0 {
  79. return countPerIaqi(pmtw, 2)
  80. }
  81. return 0
  82. }
  83. func CountAqi(pmtw, pmte float64) float64 {
  84. var pmtwIaqi float64 = getPm25IAQI(pmtw)
  85. var pmteIaqi float64 = getPm10IAQI(pmte)
  86. return math.Max(pmteIaqi, pmtwIaqi)
  87. }
  88. func GetDegree(pmtw, pmte float64) string {
  89. return getDegree(getPollutionDegree(CountAqi(pmtw, pmte)))
  90. }
  91. func GetAqiAndDegree(pmtw, pmte float64) (float64, string) {
  92. aqi := CountAqi(pmtw, pmte)
  93. degree := getDegree(getPollutionDegree(aqi))
  94. return aqi, degree
  95. }