ningshuxia
2025-04-16 ed113213fc94c3d9886ea08dfddd09d08d9ba7d5
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
using IStation.Model;
 
namespace IStation.Test
{
    public class Station2TotalFlowDiffHelper
    {
        /// <summary>
        /// 输出偏差数据
        /// </summary>
        public static void Start()
        {
            var bll = new BLL.StationSignalRecordPacket();
            var projectId = 661070185922629;
            IStation.SettingsD.Project.ID = projectId;
            var monitorDataSourcesId = 663976941449285;//2024-202504 
            var stationId = 462958422204485;
            var ptFilterList = new List<PointViewModel>();
 
            var packets = bll.Get(monitorDataSourcesId, stationId);
            var ptList = new List<FlowDiffViewModel>();
 
            var records = packets.SelectMany(x => x.StationSignalRecords).ToList();
            foreach (var x in records)
            {
                if (x.TotalPressure > 0 && x.TotalFlow > 0)
                {
                    ptList.Add( new (x.TotalFlow, Math.Round(x.TotalPressure, 1), x.DiffFlow));
                }
            }
 
            var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "stationcsv");
            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
 
 
 
            var group = ptList.GroupBy(x => x.Pressure);
            string filePath = Path.Combine(fullPath, "flow_diff_range2.csv");
            using StreamWriter writer = new(filePath, false, System.Text.Encoding.UTF8);
            writer.WriteLine($"Pressure2,FlowDiffUpper,FlowDiffAverage,FlowDiffLower");
            var allList = new List<FlowDiffViewModel>();
            foreach (var item in group)
            {
                var tt = item.ToList();
                var fList = Filter(tt);
                allList.AddRange(fList);
                var x = item.Key;
                var yUpper = item.Max(x => x.FlowDiff);
                var yLower = item.Min(x => x.FlowDiff);
                var yAverage = item.Average(x => x.FlowDiff);
 
                if (fList != null && fList.Any())
                {
                    yUpper = fList.Max(x => x.FlowDiff);
                    yLower = fList.Min(x => x.FlowDiff);
                    yAverage = fList.Average(x => x.FlowDiff);
                }
 
                yUpper = Math.Round(yUpper,1);
                yLower = Math.Round(yLower, 1);
                yAverage = Math.Round(yAverage, 1);
 
                writer.WriteLine($"{x},{yUpper},{yAverage},{yLower}");
            }
            string filePathAll = Path.Combine(fullPath, "flow_diff2.csv");
            CsvHelper.ExportToCsv(allList, filePathAll);
 
            Console.WriteLine("ok");
            Console.ReadKey();
 
        }
 
        public static List<FlowDiffViewModel> Filter(List<FlowDiffViewModel> ptList)
        {
            var pressures = ptList.Select(p => p.FlowDiff).ToList();
 
            // 计算统计量
            var (mean, stdDev) = DynamicThresholdProcessorHelper.CalculateStats(pressures);
            double skewness = DynamicThresholdProcessorHelper.CalculateSkewness(pressures);
            // 动态调整σ倍数
            //double sigmaMultiplier = DynamicThresholdProcessorHelper.CalculateSigmaMultiplier(skewness);
            var sigmaMultiplier = 3;//目前默认 标准差
 
            // 计算边界
            double lower = mean - sigmaMultiplier * stdDev;
            double upper = mean + sigmaMultiplier * stdDev;
 
            return ptList.Where(p => p.FlowDiff >= lower && p.FlowDiff <= upper).ToList();
        }
 
        public class FlowDiffViewModel
        {
            public FlowDiffViewModel(double totalFlow, double pressure, double flowDiff)
            {
                TotalFlow = Math.Round(totalFlow, 1);
                Pressure = pressure;
                FlowDiff = Math.Round(flowDiff,1);
            }
            public double TotalFlow { get; set; }
            public double Pressure { get; set; }
            public double FlowDiff { get; set; }
        }
 
 
    }
}