jinpengyong
2021-07-02 a437e96b9c1419cac50e6ea0bb65ff0352057e04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.moral.util;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.constant.Constants;
 
public class AmendUtils {
    /**
     * @param value 需要科学计算的数据
     * @param digit 保留的小数位
     * @return 功能:四舍六入五成双计算法
     */
    public static double sciCal(double value, int digit) {
        String result;
        try {
            double ratio = Math.pow(10, digit);
            double _num = value * ratio;
            double mod = _num % 1;
            double integer = Math.floor(_num);
            double returnNum;
            if (mod > 0.5) {
                returnNum = (integer + 1) / ratio;
            } else if (mod < 0.5) {
                returnNum = integer / ratio;
            } else {
                returnNum = (integer % 2 == 0 ? integer : integer + 1) / ratio;
            }
            BigDecimal bg = new BigDecimal(returnNum);
            result = bg.setScale(digit, BigDecimal.ROUND_HALF_UP).toString();
        } catch (RuntimeException e) {
            throw e;
        }
        return Double.parseDouble(result);
    }
 
    /**
     * @param list 参数,[value={"O3":12},.....]
     * @return 功能:臭氧日均值计算
     */
    public static Object getO3AvgOfDay(List<Map<String, Object>> list) {
        double max;
        List<Double> avgs = new ArrayList<>();
        for (int i = 8; i <= 24; i++) {
            List<Double> data = new ArrayList<>();
            for (Map<String, Object> dataMap : list) {
                Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class);
                Double o3 = Double.parseDouble(dataValue.get(Constants.SENSOR_CODE_O3).toString());
                int hour = DateUtils.getHour((Date) dataMap.get("time"));
                if (hour == 0) {
                    hour = 24;
                }
                if (hour <= i && hour >= i - 7) {
                    data.add(o3);
                }
            }
            if (data.size() < 6) {
                continue;
            }
            double average = data.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage();
            avgs.add(average);
        }
        max = avgs.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getMax();
        if (avgs.size() < 14) {
            if (max < 160d) {
                return null;
            }
        }
        return sciCal(max, 4);
    }
 
    /**
     * @param list 参数,[value={"风速":12,”风向“:200},.....]
     * @return 功能:风向均值计算
     */
    public static Object getWindDirAvg(List<Map<String, Object>> list) {
        double avgDir;
        double sumSin = 0d;
        double sumCos = 0d;
        int size = 0;
        for (Map<String, Object> map : list) {
            Map<String, Object> dataValue = JSONObject.parseObject((String) map.get("value"), Map.class);
            Object wind = dataValue.get(Constants.SENSOR_CODE_WIND_DIR);
            Object speed = dataValue.get(Constants.SENSOR_CODE_WIND_SPEED);
            if (wind == null || speed == null) {
                continue;
            }
            size++;
            double windDir = Double.parseDouble(wind.toString());
            double windSpeed = Double.parseDouble(speed.toString());
            double sin = windSpeed * Math.sin(windDir / 180d) * Math.PI;
            double cos = windSpeed * Math.cos(windDir / 180d) * Math.PI;
            sumSin += sin;
            sumCos += cos;
        }
        if (size == 0) {
            return null;
        }
        double avgSin = sumSin / size;
        double avgCos = sumCos / size;
        if (avgSin > 0 && avgCos > 0) {
            avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI;
        } else if ((avgSin > 0 && avgCos < 0) || (avgSin < 0 && avgCos < 0)) {
            avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI + 180;
        } else {
            avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI + 360;
        }
        return sciCal(avgDir, 4);
    }
 
    /**
     * @param data 需要求百分位的数据
     * @param p    百分位,例:95百分位,p=95
     * @return 功能:百分位数计算
     */
    public static double percentile(List<Double> data, int p) {
        int n = data.size();
        Collections.sort(data);
        double v = n / (100 / p);
        System.out.println(n % (100 / p));
        if (n % (100 / p) == 0) {
            if (v == n) {
                return data.get(n - 1);
            }
            return (data.get((int) v - 1) + data.get((int) v)) / 2;
        }
        return sciCal(data.get((int) v), 4);
    }
 
    public static Object getCOAvgOfWeek(List<Map<String, Object>> list) {
        List<Double> data = new ArrayList<>();
        for (Map<String, Object> dataMap : list) {
            Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class);
            Object o = dataValue.get(Constants.SENSOR_CODE_CO);
            if (o == null) {
                continue;
            }
            Double co = Double.parseDouble(o.toString());
            data.add(co);
        }
        if (data.size() == 0) {
            return null;
        }
        return percentile(data, 95);
    }
 
    public static Object getO3AvgOfWeek(List<Map<String, Object>> list) {
        List<Double> data = new ArrayList<>();
        for (Map<String, Object> dataMap : list) {
            Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class);
            Object o = dataValue.get(Constants.SENSOR_CODE_O3);
            if (o == null) {
                continue;
            }
            Double o3 = Double.parseDouble(o.toString());
            data.add(o3);
        }
        if (data.size() == 0) {
            return null;
        }
        return percentile(data, 90);
    }
}