ningshuxia
2025-04-11 7c34f948bd207c0b742bcd39a7d2f260487085e4
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
using IStation.Model;
using System.ComponentModel.DataAnnotations;
 
namespace IStation.Test
{
    /// <summary>
    /// 数据导入
    /// 根据3倍标准差粗过滤异常数据
    /// 再用数据用1hz分类,调用修正算法更新出最新的曲线
    /// 缺失的频率向上取最近的hz相似换算下去
    /// 现场调度用更新后的曲线调度
    /// </summary>
    public class Station2Helper
    {
        /// <summary>
        /// 绑定数据
        /// </summary>
        public static void Start()
        {
            var projectId = 661070185922629;
            IStation.SettingsD.Project.ID = projectId;
            var monitorDataSourcesId = 663976941449285;//2024-202504
            var stationIndex = 2;
            var stationId = 462958422204485;
            var dtStart = new DateTime(2024, 1, 1);
            var dtEnd = new DateTime(2025, 1, 1);
 
            var bll = new BLL.StationSignalRecordPacket();
            var bllEquipment = new BLL.Equipment();
            var bllCurve = new BLL.PumpCurve();
 
 
            var pumpList = bllEquipment.GetPumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, stationId);
            var flagList = pumpList.Select(x => x.SortCode).OrderBy(x => x).ToList();
            var flagPumpDict = pumpList.ToDictionary(x => x.SortCode, x => x.RatedParas);
            var flagQhCurveDict = new Dictionary<int, Model.CurveExpress>();
 
            foreach (var pump in pumpList)
            {
                Model.CurveExpress qh = null;
                var curveInfo = bllCurve.GetDefaultWorkingByPumpID(pump.ID)?.CurveInfo;
                if (curveInfo?.CurveQH != null)
                {
                    qh = curveInfo.CurveQH;
                }
                flagQhCurveDict.Add(pump.SortCode, qh);
            }
 
            var pipe_flow_id_list = GlobalHelperW.GetPipeFlowIdList(stationIndex);
            var pipe_pressure_id_list = GlobalHelperW.GetPipePressureIdList(stationIndex);
 
            var packet_list = bll.Get(monitorDataSourcesId, stationId);
      
            var allStationRecordList = packet_list.SelectMany(x => x.StationSignalRecords).ToList();
            Console.WriteLine($"泵站总数:{allStationRecordList.Count}");
            var stationRecordList = DynamicThresholdProcessorHelper.Filter(allStationRecordList);
            Console.WriteLine($"泵站总数:{stationRecordList.Count} 过滤:{allStationRecordList.Count - stationRecordList.Count}");
            var stationRecordList2 = stationRecordList.Where(x =>
            {
                var model_record_dict = x.ModelRecordDict;
                foreach (var id in pipe_flow_id_list)
                {
                    if (model_record_dict[id] < 1)
                    {
                        return false;
                    }
                }
                foreach (var id in pipe_pressure_id_list)
                {
                    var value = model_record_dict[id];
                    if (value < 1 || value > 40)
                    {
                        return false;
                    }
                }
                return true;
            }).ToList();
 
            Console.WriteLine($"泵站总数:{stationRecordList2.Count} 二次过滤:{allStationRecordList.Count - stationRecordList2.Count}");
            var allPumpRecordList = stationRecordList2.SelectMany(x => x.PumpSignalRecords).ToList();
            Console.WriteLine($"泵总数:{allPumpRecordList.Count}");
 
            var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pumpcsv");
            if (Directory.Exists(fullPath))
            {
                Directory.Delete(fullPath, true);
            }
            Directory.CreateDirectory(fullPath);
 
            var allPumpRecordList2 = allPumpRecordList.Where(x =>
            {
                if (x.Rpm == IStation.Error.Default || x.Head == IStation.Error.Default)
                {
                    return false;
                }
                if (x.FlowRate < 1 || x.FlowRate > 20000)
                {
                    return false;
                }
                return true;
            }).ToList();
            Console.WriteLine($"泵总数:{allPumpRecordList2.Count} 过滤:{allPumpRecordList.Count - allPumpRecordList2.Count}");
            var pumpFlagRecordGroup = allPumpRecordList.OrderBy(x => x.Flag).GroupBy(x => x.Flag);
            var pumpFlagHzRecordGroup = new Dictionary<(int Flag, int Hz), List<Model.PumpSignalRecord>>();
            foreach (var pumpFlag in pumpFlagRecordGroup)
            {
                var flag = pumpFlag.Key;
                var pump = flagPumpDict[flag];
                var pumpQh = flagQhCurveDict[flag];
                if (!pump.IsBp)
                {
                    var allFlagHzRecordList = pumpFlag.ToList();
                    if (allFlagHzRecordList.Count < 30)
                    {
                        Console.WriteLine($"{flag}泵-{50}hz 总数:{allFlagHzRecordList.Count},<5 跳过");
                    }
                    var flagHzRecordList = DynamicThresholdProcessorHelper.Filter(allFlagHzRecordList);
                    Console.WriteLine($"{flag}泵-{50}hz 总数:{allFlagHzRecordList.Count},过滤:{allFlagHzRecordList.Count - flagHzRecordList.Count}");
                    pumpFlagHzRecordGroup.Add((flag, 50), flagHzRecordList);
                    if (flagHzRecordList.Any())
                    {
                        var curvePtList = pumpQh.GetFitPoints(200).Select(x => new CurvePtViewModel(x)).ToList();
                        CsvHelper.ExportToCsv(curvePtList, Path.Combine(fullPath, $"{flag}_{50}_old_curve.csv"));
                        CsvHelper.ExportToCsv(flagHzRecordList, Path.Combine(fullPath, $"{flag}_{50}.csv"));
 
                        var splineX = curvePtList.Select(x => x.X).ToArray();
                        var splineY = curvePtList.Select(x => x.Y).ToArray();
                        var measuredXAll = flagHzRecordList.Select(x => x.FlowRate).ToArray();
                        var measuredYAll = flagHzRecordList.Select(x => x.Head).ToArray();
 
                        //var helper = new PumpCurveDataFusionCorrectorHelper2();
                        //(double[] mergedX, double[] mergedY, double[] optimizedX, double[] optimizedY) = helper.Corrent(splineX, splineY, measuredXAll, measuredYAll);
 
                        //if (optimizedX == null)
                        //{
                        //    continue;
                        //}
                        //var optList = new List<CurvePoint>();
                        //for (int i = 0; i < optimizedX.Length; i++)
                        //{
                        //    var x = optimizedX[i];
                        //    var y = optimizedY[i];
                        //    optList.Add(new CurvePoint(x, y));
                        //}
 
                        //CsvHelper.ExportToCsv(optList, Path.Combine(fullPath, $"{flag}_{50}_update_curve.csv"));
                    }
                }
                else
                {
                    var hzGroup = pumpFlag.OrderBy(x => x.Rpm).GroupBy(x =>
                    {
                        return Math.Round(x.Rpm / pump.Nr * 50, 0);
                    });
                    hzGroup = hzGroup.Where(x => x.Key > 10).ToList();
                    foreach (var pumpFlagHz in hzGroup)
                    {
                        var hz = pumpFlagHz.Key;
                        var allFlagHzRecordList = pumpFlagHz.ToList();
                        if (allFlagHzRecordList.Count < 30)
                        {
                            Console.WriteLine($"{flag}泵-{hz}hz 总数:{allFlagHzRecordList.Count},<5 跳过");
                        }
                        var flagHzRecordList = DynamicThresholdProcessorHelper.Filter(allFlagHzRecordList);
                        if (allFlagHzRecordList.Count < 30)
                        {
                            Console.WriteLine($"{flag}泵-{hz}hz 过滤后总数:{allFlagHzRecordList.Count},<5 跳过");
                        }
                        Console.WriteLine($"{flag}泵-{hz}hz 总数:{allFlagHzRecordList.Count},过滤:{allFlagHzRecordList.Count - flagHzRecordList.Count}");
                        pumpFlagHzRecordGroup.Add((flag, (int)hz), flagHzRecordList);
                        if (flagHzRecordList.Any())
                        {
                            var curvePtList = SimilarCalculateHelper.CalculateQH(pumpQh, 50, hz).GetFitPoints(100);
                            CsvHelper.ExportToCsv(curvePtList.Select(x => new CurvePtViewModel(x)).ToList(), Path.Combine(fullPath, $"{flag}_{hz}_old_curve.csv"));
                            CsvHelper.ExportToCsv(flagHzRecordList, Path.Combine(fullPath, $"{flag}_{hz}.csv"));
 
 
                            var splineX = curvePtList.Select(x => x.X).ToArray();
                            var splineY = curvePtList.Select(x => x.Y).ToArray();
                            var measuredXAll = flagHzRecordList.Select(x => x.FlowRate).ToArray();
                            var measuredYAll = flagHzRecordList.Select(x => x.Head).ToArray();
 
 
                            //var helper = new PumpCurveDataFusionCorrectorHelper2();
                            //(double[] mergedX, double[] mergedY, double[] optimizedX, double[] optimizedY) = helper.Corrent(splineX, splineY, measuredXAll, measuredYAll);
 
 
                            //if (optimizedX == null)
                            //{
                            //    continue;
                            //}
                            //var optList = new List<CurvePoint>();
                            //for (int i = 0; i < optimizedX.Length; i++)
                            //{
                            //    var x = optimizedX[i];
                            //    var y = optimizedY[i];
                            //    optList.Add(new CurvePoint(x, y));
                            //}
 
                            //CsvHelper.ExportToCsv(optList, Path.Combine(fullPath, $"{flag}_{hz}_update_curve.csv"));
                        }
                    }
 
                }
 
            }
 
 
 
 
        }
         
         
 
    }
 
 
 
    #region ViewModel
 
    public class CurvePtViewModel
    {
        public CurvePtViewModel() { }
        public CurvePtViewModel(CurvePoint rhs)
        {
            X = Math.Round(rhs.X, 6);
            Y = Math.Round(rhs.Y, 6);
        }
 
 
        [Display(Name = "X")]
        public double X { get; set; }
        [Display(Name = "Y")]
        public double Y { get; set; }
    }
      
    #endregion
}