123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- package common
- import (
- "math"
- "strconv"
- )
- func CalculateAirQuality(d float64) string {
-
-
-
-
- val := "-"
- if d <= 50 {
- val = "优"
- } else if d > 50 && d <= 100 {
- val = "良"
- } else if d > 100 && d <= 150 {
- val = "轻度污染"
- } else if d > 150 && d <= 200 {
- val = "中度污染"
- } else if d > 200 && d < 300 {
- val = "重度污染"
- } else if d > 300 {
- val = "严重污染"
- }
- return val
- }
- func getCoIAQI(co float64) float64 {
- if co > 0 {
- return countPerIaqi(co, 3)
- }
- return 0
- }
- func getNo2IAQI(no2 float64) float64 {
- if no2 > 0 {
- return countPerIaqi(no2, 4)
- }
- return 0
- }
- func getO3OneHourIAQI(o3One float64) float64 {
- if o3One > 0 {
- return countPerIaqi(o3One, 5)
- }
- return 0
- }
- func getSo2IAQI(so2 float64) float64 {
- if so2 > 0 {
- return countPerIaqi(so2, 6)
- }
- return 0
- }
- func CalculateDirection(windDirection string) string {
- d, _ := strconv.ParseFloat(windDirection, 64)
- val := "-"
- if d > 337.5 || d < 22.5 {
-
- val = "北"
- } else if d > 22.5 && d < 67.5 {
-
- val = "东北"
- } else if d > 67.5 && d < 112.5 {
-
- val = "东"
- } else if d > 112.5 && d < 157.5 {
-
- val = "东南"
- } else if d > 157.5 && d < 202.5 {
-
- val = "南"
- } else if d > 202.5 && d < 247.5 {
-
- val = "西南"
- } else if d > 247.5 && d < 292.5 {
-
- val = "西"
- } else if d > 292.5 && d < 337.5 {
-
- val = "西北"
- }
- return val
- }
- func CalculateSpeed(speed string) string {
-
- d, _ := strconv.ParseFloat(speed, 64)
- val := "-"
- if d >= 0.0 && d <= 0.2 {
- val = "无风"
- } else if d >= 0.3 && d <= 1.5 {
- val = "软风(一级)"
- } else if d >= 1.6 && d <= 3.3 {
- val = "轻风(二级)"
- } else if d >= 3.4 && d <= 5.4 {
- val = "微风(三级)"
- } else if d >= 5.5 && d <= 7.9 {
- val = "和风(四级)"
- } else if d >= 8.0 && d <= 10.7 {
- val = "清劲风(五级)"
- } else if d >= 10.8 && d <= 13.8 {
- val = "强风(六级)"
- } else if d >= 13.9 && d <= 17.1 {
- val = "疾风(七级)"
- } else if d >= 17.2 && d <= 20.7 {
- val = "大风(八级)"
- } else if d >= 20.8 && d <= 24.4 {
- val = "烈风(九级)"
- } else if d >= 24.5 && d <= 28.4 {
- val = "狂风(十级)"
- } else if d >= 28.5 && d <= 32.6 {
- val = "暴风(11级)"
- } else if d > 32.6 {
- val = "台风(12级)"
- }
- return val
- }
- func getPollutionDegree(aqi float64) int {
- var pollutionDegree int = 1
- if aqi <= 50 {
- pollutionDegree = 1
- } else if aqi > 50 && aqi <= 100 {
- pollutionDegree = 2
- } else if aqi > 100 && aqi <= 150 {
- pollutionDegree = 3
- } else if aqi > 150 && aqi <= 200 {
- pollutionDegree = 4
- } else if aqi > 200 && aqi <= 250 {
- pollutionDegree = 5
- } else if aqi > 250 && aqi <= 300 {
- pollutionDegree = 6
- } else if aqi > 300 {
- pollutionDegree = 7
- }
- return pollutionDegree
- }
- func getDegree(pollutionDegree int) string {
- if pollutionDegree == 1 {
- return "优"
- } else if pollutionDegree == 2 {
- return "良"
- } else if pollutionDegree == 3 {
- return "轻微污染"
- } else if pollutionDegree == 4 {
- return "轻度污染"
- } else if pollutionDegree == 5 {
- return "中度污染"
- } else if pollutionDegree == 6 {
- return "中度重污染"
- } else if pollutionDegree == 7 {
- return "重度污染"
- }
- return "良"
- }
- func countPerIaqi(cp float64, r int) float64 {
- var bph float64 = 0
- var bpl float64 = 0
- var iaqih float64 = 0
- var iaqil float64 = 0
- var iaqip float64 = 0
-
- 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}}
- var min float64 = aqiArr[r][0]
- var index int = len(aqiArr[r]) - 1
- var max float64 = aqiArr[r][index]
- if cp <= min || cp >= max {
- return 0.0
- } else {
-
- for i := r; i < (r + 1); i++ {
- for j := 0; j < len(aqiArr[0]); j++ {
- if cp < aqiArr[i][j] {
- bph = aqiArr[i][j]
- bpl = aqiArr[i][j-1]
- iaqih = aqiArr[0][j]
- iaqil = aqiArr[0][j-1]
- break
- }
- }
- }
-
- iaqip = (iaqih-iaqil)/(bph-bpl)*(cp-bpl) + iaqil
- return iaqip
- }
- }
- func getPm10IAQI(pmte float64) float64 {
- if pmte > 0 {
- return countPerIaqi(pmte, 1)
- }
- return 0
- }
- func getPm25IAQI(pmtw float64) float64 {
- if pmtw > 0 {
- return countPerIaqi(pmtw, 2)
- }
- return 0
- }
- func CountAqi(pmtw, pmte float64) float64 {
- var pmtwIaqi float64 = getPm25IAQI(pmtw)
- var pmteIaqi float64 = getPm10IAQI(pmte)
- return math.Max(pmteIaqi, pmtwIaqi)
- }
- func GetDegree(pmtw, pmte float64) string {
- return getDegree(getPollutionDegree(CountAqi(pmtw, pmte)))
- }
- func GetAqiAndDegree(pmtw, pmte float64) (float64, string) {
- aqi := CountAqi(pmtw, pmte)
- degree := getDegree(getPollutionDegree(aqi))
- return aqi, degree
- }
|