jinpengyong
2021-08-06 6ae790cc95cf87e40f0f1fa49ed04fb722530211
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.moral.util;
 
import org.springframework.util.ObjectUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
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 params 参数,包含数据data,类型type,下限upper,上限lower
     * @return 功能:臭氧日均值计算
     */
    public static Map<String, Object> getO3AvgOfDay(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list = (List<Map<String, Object>>) params.get("data");
        Object upper = params.get("o3Upper");
        Object lower = params.get("o3Lower");
        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());
 
                //O3数据标记位
                Object flag = dataValue.get(Constants.SENSOR_CODE_O3 + Constants.MARKER_BIT_KEY);
                //剔除有效性不足的数据
                if (!Constants.MARKER_BIT_TRUE.equals(flag)) {
                    continue;
                }
 
                //剔除超过上下限的数据
                if (!ObjectUtils.isEmpty(upper)) {
                    if (o3 < (Double) upper) {
                        continue;
                    }
                }
                if (!ObjectUtils.isEmpty(lower)) {
                    if (o3 > (Double) upper) {
                        continue;
                    }
                }
 
                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 result;
            }
        }
        result.put(Constants.SENSOR_CODE_O3, sciCal(max, 4));
        return result;
    }
 
    /**
     * @param params 参数
     * @return 功能:风向均值计算
     */
    public static Map<String, Object> getWindDirAvg(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> data = (List<Map<String, Object>>) params.get("data");
        String type = params.get("type").toString();
        Object windDirUpper = params.get("windDirUpper");
        Object windDirLower = params.get("windDirLower");
        Object windSpeedUpper = params.get("windSpeedUpper");
        Object windSpeedLower = params.get("windSpeedLower");
 
        double avgDir;
        double sumSin = 0d;
        double sumCos = 0d;
        int size = 0;
        for (Map<String, Object> map : data) {
            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);
            Object flagDir = dataValue.get(Constants.SENSOR_CODE_WIND_DIR + Constants.MARKER_BIT_KEY);
            Object flagSpeed = dataValue.get(Constants.SENSOR_CODE_WIND_SPEED + Constants.MARKER_BIT_KEY);
            if (!Constants.MARKER_BIT_TRUE.equals(flagDir) || !Constants.MARKER_BIT_TRUE.equals(flagSpeed)) {
                continue;
            }
 
            if (ObjectUtils.isEmpty(wind) || ObjectUtils.isEmpty(speed)) {
                continue;
            }
            double windDir = Double.parseDouble(wind.toString());
            double windSpeed = Double.parseDouble(speed.toString());
 
            //剔除风速和风向超过上下限范围的数据
            if (!ObjectUtils.isEmpty(windDirUpper)) {
                if (windDir < (Double) windDirUpper) {
                    continue;
                }
            }
 
            if (!ObjectUtils.isEmpty(windDirLower)) {
                if (windDir > (Double) windDirLower) {
                    continue;
                }
            }
 
            if (!ObjectUtils.isEmpty(windSpeedUpper)) {
                if (windSpeed < (Double) windSpeedUpper) {
                    continue;
                }
            }
 
            if (!ObjectUtils.isEmpty(windSpeedLower)) {
                if (windSpeed > (Double) windSpeedLower) {
                    continue;
                }
            }
 
            size++;
            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 result;
        }
 
        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;
        }
        double v = sciCal(avgDir, 4);
        result.put(Constants.SENSOR_CODE_WIND_DIR, v);
        if ("hour".equals(type)) {
            //有效值>=45个,打标记位 N,<45打H   H:有效性不足
            if (size >= 45) {
                result.put(Constants.SENSOR_CODE_WIND_DIR + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE);
            } else {
                result.put(Constants.SENSOR_CODE_WIND_DIR + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE);
            }
        }
        return result;
    }
 
    /**
     * @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);
        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 Map<String, Object> getCOAvgOfWeekOrMonth(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list = (List<Map<String, Object>>) params.get("data");
        Object upper = params.get("coUpper");
        Object lower = params.get("coLower");
 
        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);
            Object flag = dataValue.get(Constants.SENSOR_CODE_CO + Constants.MARKER_BIT_KEY);
            if (!Constants.MARKER_BIT_TRUE.equals(flag)) {
                continue;
            }
            if (ObjectUtils.isEmpty(o)) {
                continue;
            }
            Double co = Double.parseDouble(o.toString());
            //剔除超过上下限的数据
            if (!ObjectUtils.isEmpty(upper)) {
                if (co < (Double) upper) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(lower)) {
                if (co > (Double) upper) {
                    continue;
                }
            }
            data.add(co);
        }
        if (data.size() == 0) {
            return result;
        }
        result.put(Constants.SENSOR_CODE_CO, percentile(data, 95));
        return result;
    }
 
    //臭氧周月均值计算
    public static Map<String, Object> getO3AvgOfWeekOrMonth(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list = (List<Map<String, Object>>) params.get("data");
        Object upper = params.get("o3Upper");
        Object lower = params.get("o3Lower");
        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);
            Object flag = dataValue.get(Constants.SENSOR_CODE_O3 + Constants.MARKER_BIT_KEY);
            if (!Constants.MARKER_BIT_TRUE.equals(flag)) {
                continue;
            }
            if (ObjectUtils.isEmpty(o)) {
                continue;
            }
            Double o3 = Double.parseDouble(o.toString());
            //剔除超过上下限的数据
            if (!ObjectUtils.isEmpty(upper)) {
                if (o3 < (Double) upper) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(lower)) {
                if (o3 > (Double) upper) {
                    continue;
                }
            }
            data.add(o3);
        }
        if (data.size() == 0) {
            return result;
        }
        result.put(Constants.SENSOR_CODE_O3, percentile(data, 90));
        return result;
    }
}