cjl
2025-01-06 27e6bc3df3e39e0d0b147b155a89ad6837ea972b
screen-common/src/main/java/com/moral/util/AmendUtils.java
@@ -53,10 +53,13 @@
        Object lower = params.get("o3Lower");
        double max;
        List<Double> avgs = new ArrayList<>();
        for (int i = 8; i <= 24; i++) {
        for (int i = 7; i <= 23; i++) {
            List<Double> data = new ArrayList<>();
            for (Map<String, Object> dataMap : list) {
                Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class);
                if (ObjectUtils.isEmpty(dataValue.get(Constants.SENSOR_CODE_O3))){
                    continue;
                }
                Double o3 = Double.parseDouble(dataValue.get(Constants.SENSOR_CODE_O3).toString());
                //O3数据标记位
@@ -68,20 +71,17 @@
                //剔除超过上下限的数据
                if (!ObjectUtils.isEmpty(upper)) {
                    if (o3 < (Double) upper) {
                    if (o3 > (Double) upper) {
                        continue;
                    }
                }
                if (!ObjectUtils.isEmpty(lower)) {
                    if (o3 > (Double) upper) {
                    if (o3 < (Double) lower) {
                        continue;
                    }
                }
                int hour = DateUtils.getHour((Date) dataMap.get("time"));
                if (hour == 0) {
                    hour = 24;
                }
                if (hour <= i && hour >= i - 7) {
                    data.add(o3);
                }
@@ -102,38 +102,38 @@
        return result;
    }
    //城市aqi日均值
    public static Double o3OfDay(List<Map<String, Object>> value) {
        List<Map<String, Object>> o3_8H = getO3_8H(value);
        if (!ObjectUtils.isEmpty(o3_8H)) {
            double o3 = 0d;
            for (Map<String, Object> o : o3_8H) {
                double temp = (double) o.get("O3");
                if (temp > o3) {
                    o3 = temp;
                }
            }
            return sciCal(o3, 0);
        }
        return null;
    }
    /**
     * @param data 数据 time:Date类型
     * @param data 数据 time:Date类型,臭氧key:o3
     * @return 功能:臭氧8小时滑动值计算
     */
    public static List<Map<String, Object>> getO3_8H(List<Map<String, Object>> data) {
        Date time1 = null;
        Date time2 = null;
        for (Map<String, Object> datum : data) {
            Date time = (Date) datum.get("time");
            if (DateUtils.getHour(time) == 0) {
                time2 = DateUtils.getDate(DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_EN));
            } else {
                time1 = DateUtils.getDate(DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_EN));
            }
        }
        Date date = (Date) data.get(0).get("time");
        List<Map<String, Object>> result = new ArrayList<>();
        for (int i = 8; i <= 24; i++) {
        for (int i = 7; i <= 23; i++) {
            Map<String, Object> map = new HashMap<>();
            if (i < 24) {
                map.put("time", ObjectUtils.isEmpty(time1) ? null : DateUtils.addHours(time1, i));
            } else {
                map.put("time", ObjectUtils.isEmpty(time2) ? null : time2);
            }
            map.put("time", DateUtils.addHours(date, i));
            List<Double> value = new ArrayList<>();
            for (Map<String, Object> dataMap : data) {
                Double o3 = Double.parseDouble(dataMap.get(Constants.SENSOR_CODE_O3).toString());
                Map<String, Object> sensorValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class);
                Double o3 = Double.parseDouble(sensorValue.get("O3").toString());
                Date time = (Date) dataMap.get("time");
                int hour = DateUtils.getHour(time);
                if (hour == 0) {
                    hour = 24;
                }
                if (hour <= i && hour >= i - 7) {
                    value.add(o3);
                }
@@ -142,7 +142,7 @@
                continue;
            }
            double average = value.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage();
            map.put(Constants.SENSOR_CODE_O3, average);
            map.put("O3", sciCal(average, 0));
            result.add(map);
        }
        return result;
@@ -171,8 +171,11 @@
            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(flagDir) && !ObjectUtils.isEmpty(flagSpeed)) {
                if (!Constants.MARKER_BIT_TRUE.equals(flagDir) || !Constants.MARKER_BIT_TRUE.equals(flagSpeed)) {
                    continue;
                }
            }
            if (ObjectUtils.isEmpty(wind) || ObjectUtils.isEmpty(speed)) {
@@ -183,25 +186,25 @@
            //剔除风速和风向超过上下限范围的数据
            if (!ObjectUtils.isEmpty(windDirUpper)) {
                if (windDir < (Double) windDirUpper) {
                if (windDir > (Double) windDirUpper) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(windDirLower)) {
                if (windDir > (Double) windDirLower) {
                if (windDir < (Double) windDirLower) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(windSpeedUpper)) {
                if (windSpeed < (Double) windSpeedUpper) {
                if (windSpeed > (Double) windSpeedUpper) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(windSpeedLower)) {
                if (windSpeed > (Double) windSpeedLower) {
                if (windSpeed < (Double) windSpeedLower) {
                    continue;
                }
            }
@@ -249,15 +252,12 @@
    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;
        }
        double v = MathUtils.division(MathUtils.mul(n, p),100d,1);
        v = Math.ceil(v)-1;
        return sciCal(data.get((int) v), 4);
    }
    //一氧化碳周月均值计算
    public static Map<String, Object> getCOAvgOfWeekOrMonth(Map<String, Object> params) {
@@ -270,22 +270,18 @@
        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) {
                if (co > (Double) upper) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(lower)) {
                if (co > (Double) upper) {
                if (co < (Double) lower) {
                    continue;
                }
            }
@@ -308,22 +304,18 @@
        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) {
                if (o3 > (Double) upper) {
                    continue;
                }
            }
            if (!ObjectUtils.isEmpty(lower)) {
                if (o3 > (Double) upper) {
                if (o3 < (Double) lower) {
                    continue;
                }
            }
@@ -335,4 +327,40 @@
        result.put(Constants.SENSOR_CODE_O3, percentile(data, 90));
        return result;
    }
    //各因子年均值计算
    /**
     * 年均值算法
     * PM2.5,PM10,CO,日均值95百分位
     * SO2,NO2,日均值98百分位
     * O3,日最大值90百分位
     *
     * @param list       数据
     * @param sensorCode 因子code
     */
    public static Double getAvgOfYear(List<Map<String, Object>> list, String sensorCode) {
        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(sensorCode);
            if (ObjectUtils.isEmpty(o)) {
                continue;
            }
            Double sensorValue = Double.parseDouble(o.toString());
            data.add(sensorValue);
        }
        if (data.size() == 0) {
            return null;
        }
        Double v = null;
        if ("PM2_5".equals(sensorCode) || "PM10".equals(sensorCode) || "CO".equals(sensorCode)) {
            v = percentile(data, 95);
        } else if ("SO2".equals(sensorCode) || "NO2".equals(sensorCode)) {
            v = percentile(data, 98);
        } else if ("O3".equals(sensorCode)) {
            v = percentile(data, 90);
        }
        return v;
    }
}