ningshuxia
8 天以前 1519533649b43337d214523f7cd075edf237b3f7
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace IStation.WinFrmUI
{
    /// <summary>
    /// 导出 分析系数
    /// </summary>
    public partial class AnalysisHelper
    {
        #region Model
 
        public class ScadaDiffViewModel
        {
            public DateTime Time { get; set; }
            public int Flag { get; set; }
            public double Rpm { get; set; }
            public double Hz { get; set; }
            public double Hz0 { get; set; }
            public double Flow { get; set; }
            public double Head { get; set; }
            public double CurveHead { get; set; }
            public double HeadDiff { get; set; }
            public double CorrectCurveHead { get; set; }
            public double CorrectHeadDiff { get; set; }
        }
 
        public class ModelDiffViewMdoel
        {
            public string Time { get; set; }
            public string MonitorID { get; set; }
            public string ModelID { get; set; }
            public double ModelValue { get; set; }
            public double MonitorValue { get; set; }
            public double DiffVlaue { get; set; }
            public int? Flag { get; set; }
            public double? Hz { get; set; }
            public double? Hz0 { get; set; }
        }
 
        public class AnalysisFactorDto
        {
            /// <summary>
            /// 泵站
            /// </summary>
            public int Station { get; set; }
 
            /// <summary>
            /// 泵标志
            /// </summary>
            public int Flag { get; set; }
 
            /// <summary>
            /// 频率
            /// </summary>
            public double Hz { get; set; }
 
            /// <summary>
            /// 扬程标准差
            /// </summary>
            public double? HeadSTDP { get; set; }
 
            /// <summary>
            /// 扬程偏差
            /// </summary>
            public double? HeadDeviation { get; set; }
 
            /// <summary>
            /// 准确度
            /// </summary>
            public double Accuracy { get; set; }
 
            /// <summary>
            /// 数量
            /// </summary>
            public int Count { get; set; }
 
        }
 
        public class AnalysisDeviationDto
        {
            /// <summary>
            /// 泵站
            /// </summary>
            public int Station { get; set; }
 
            /// <summary>
            /// 运行组合
            /// </summary>
            public List<int> RunFlags { get; set; }
 
            public double MinFlow { get; set; }
 
            public double MaxFlow { get; set; }
 
            public Dictionary<int, double> PressureDiff { get; set; }
 
            public Dictionary<int,double > StdDev { get; set; } 
 
            public int Count { get; set; }
        }
 
 
        public class PumpMapping
        {
            public int Flag { get; set; }
            public string PumpId { get; set; }
            public string FlowId { get; set; }
            public string PressureId { get; set; }
            public string CurveId { get; set; }
            public int PumpIndex { get; set; }
            public int FlowIndex { get; set; }
            public int PressureIndex { get; set; }
            public int CurveIndex { get; set; }
 
        }
 
        public class StationMapping
        {
            public string Name { get; set; }
            public string ScadaFlowId { get; set; }
            public string ScadaPressureId { get; set; }
            public string FlowId { get; set; }
            public string PressureId { get; set; }
 
            public int FlowIndex { get; set; }
            public int PressureIndex { get; set; }
 
        }
 
        #endregion
 
 
        #region Function 
 
        public static bool SaveAnalysisFactorDtoList(int station_index, List<Model.Equipment<Model.Pump>> eq_list, List<Tuple<int, double, double>> flag_hz_head_diff_list)
        {
            if (eq_list == null || !eq_list.Any())
            {
                return default;
            }
            if (flag_hz_head_diff_list == null || !flag_hz_head_diff_list.Any())
            {
                return default;
            }
 
            var flag_pump_dict = eq_list.ToDictionary(x => x.SortCode, x => x.RatedParas);
            var all_list = GetAnalysisFactorDtoList(flag_pump_dict, flag_hz_head_diff_list);
            if (all_list == null || !all_list.Any())
            {
                return default;
            }
            all_list.ForEach(x => x.Station = station_index);
            var root_folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "分析");
            if (!Directory.Exists(root_folder))
            {
                Directory.CreateDirectory(root_folder);
            }
            List<AnalysisFactorDto> exist_list = null;
            var fileName = root_folder + "\\" + "AnalysisFactor.json";
            if (File.Exists(fileName))
            {
                var exist_json = File.ReadAllText(fileName);
                var all_exist_list = JsonHelper.Json2Object<List<AnalysisFactorDto>>(exist_json);
                exist_list = all_exist_list?.Where(x => x.Station != station_index).ToList();
                File.Delete(fileName);
            }
            if (exist_list != null)
            {
                all_list.AddRange(exist_list);
            }
            all_list = all_list.OrderBy(x => x.Flag).ThenBy(x => x.Hz).ToList();
            var json = JsonHelper.Object2Json(all_list);
            File.WriteAllText(fileName, json);
            return true;
        }
 
        public static bool SaveAnalysisDeviationDtoList(int station_index, List<Model.Equipment<Model.Pump>> eq_list, List<Tuple<string, double, List<Tuple<int, double, double>>>> run_flags_flow_pressure_diff_dict, double minQ = 0, double maxQ = 100000, double spaceQ = 1000)
        {
            if (eq_list == null || !eq_list.Any())
            {
                return default;
            }
            if (run_flags_flow_pressure_diff_dict == null || !run_flags_flow_pressure_diff_dict.Any())
            {
                return default;
            }
 
            var flag_pump_dict = eq_list.ToDictionary(x => x.SortCode, x => x.RatedParas);
            var all_list = GetAnalysisDeviationDtoList(flag_pump_dict, run_flags_flow_pressure_diff_dict, minQ, maxQ, spaceQ);
            if (all_list == null || !all_list.Any())
            {
                return default;
            }
            all_list.ForEach(x => x.Station = station_index);
            var root_folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "分析");
            if (!Directory.Exists(root_folder))
            {
                Directory.CreateDirectory(root_folder);
            }
 
            List<AnalysisDeviationDto> exist_list = null;
            var fileName = root_folder + "\\" + "AnalysisDeviation.json";
            if (File.Exists(fileName))
            {
                var exist_json = File.ReadAllText(fileName);
                var all_exist_list = JsonHelper.Json2Object<List<AnalysisDeviationDto>>(exist_json);
                exist_list = all_exist_list?.Where(x => x.Station != station_index).ToList();
                File.Delete(fileName);
            }
            if (exist_list != null)
            {
                all_list.AddRange(exist_list);
            }
            all_list = all_list.OrderBy(x => x.Station).ThenBy(x => x.MinFlow).ToList();
            var json = JsonHelper.Object2Json(all_list);
            File.WriteAllText(fileName, json);
            return true;
        }
 
 
        public static bool SaveAnalysisFactorDtoListAdd(string name, List<Model.Equipment<Model.Pump>> eq_list, List<Tuple<int, double, double>> flag_hz_head_diff_list)
        {
            if (eq_list == null || !eq_list.Any())
            {
                return default;
            }
            if (flag_hz_head_diff_list == null || !flag_hz_head_diff_list.Any())
            {
                return default;
            }
 
            var flag_pump_dict = eq_list.ToDictionary(x => x.SortCode, x => x.RatedParas);
            var all_analysis_factor_list = GetAnalysisFactorDtoList(flag_pump_dict, flag_hz_head_diff_list);
            if (all_analysis_factor_list == null || !all_analysis_factor_list.Any())
            {
                return default;
            }
            var root_folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "分析系数");
            if (!Directory.Exists(root_folder))
            {
                Directory.CreateDirectory(root_folder);
            }
            var fileName = root_folder + "\\" + name + ".json";
            if (File.Exists(fileName))
            {
                var exist_json = File.ReadAllText(fileName);
                var exist_all_analysis_factor_list = JsonHelper.Json2Object<List<AnalysisFactorDto>>(exist_json);
                if (exist_all_analysis_factor_list != null && exist_all_analysis_factor_list.Any())
                {
                    foreach (var item in all_analysis_factor_list)
                    {
                        var exist = exist_all_analysis_factor_list.Find(x => x.Flag == item.Flag && x.Hz == item.Hz);
                        if (exist == null)
                            continue;
                        item.HeadDeviation += exist.HeadDeviation;
                    }
                }
            }
            var json = JsonHelper.Object2Json(all_analysis_factor_list);
            File.WriteAllText(fileName, json);
            return true;
        }
 
 
        public static List<AnalysisDeviationDto> GetAnalysisDeviationDtoList(Dictionary<int, Model.Pump> flag_pump_dict, List<Tuple<string, double, List<Tuple<int, double, double>>>> run_flags_flow_pressure_diff_dict, double minQ = 0, double maxQ = 100000, double spaceQ = 1000)
        {
            if (flag_pump_dict == null || !flag_pump_dict.Any())
            {
                return default;
            }
            if (run_flags_flow_pressure_diff_dict == null || !run_flags_flow_pressure_diff_dict.Any())
            {
                return default;
            }
 
            var list = new List<AnalysisDeviationDto>();
            var error_value = false;
            for (double current_minQ = minQ; current_minQ <= maxQ; current_minQ += spaceQ)
            {
                var current_maxQ = current_minQ + spaceQ;
                var combine_list = run_flags_flow_pressure_diff_dict.Where(x => x.Item2 >= current_minQ && x.Item2 <= current_maxQ).ToList();
                if (combine_list == null || !combine_list.Any())
                    continue;
                var group_by_run_flags = combine_list.GroupBy(x => x.Item1);
                foreach (var item_run_flags in group_by_run_flags)
                {
                    if (item_run_flags.Count() < 5)
                        continue;
                    var flags = item_run_flags.Key;
                    var vm = new AnalysisDeviationDto();
                    vm.RunFlags = IStation.Untity.IntListHelper.ToList(flags);
                    vm.MinFlow = current_minQ;
                    vm.MaxFlow = current_maxQ;
                    var dev_dict = new Dictionary<int, double>();
                    List<Tuple<int, double, double>> tuple_list = item_run_flags.SelectMany(x => x.Item3).ToList();
                    var group_by_flag = tuple_list.GroupBy(x => x.Item1);
                    foreach (var flag_item in group_by_flag)
                    {
                        var flag = flag_item.Key;
                        var head_diff_array = flag_item.Select(x => x.Item3).ToArray();
                        var filter_std_dev_pop_tuple = AnalysisHelper.GetFilterBySTDP(head_diff_array);
                        var filter_std_dev_pop_array = filter_std_dev_pop_tuple.Item1;
                        var std_dev_pop = filter_std_dev_pop_tuple.Item2;
                        var std_dev_pop_head_avg = filter_std_dev_pop_array.Average();
                        var head_diff_avg = std_dev_pop_head_avg;
                        head_diff_avg = Math.Round(head_diff_avg, 5);
                        dev_dict.Add(flag, head_diff_avg);
                    }
                    vm.Count = item_run_flags.Count();
                    vm.PressureDiff = dev_dict;
                    error_value = false;
                    foreach (var dev in dev_dict)
                    {
                        if (Math.Abs(dev.Value) > 3)
                        {
                            error_value = true;
                            break;
                        }
                    }
                    if (error_value)
                    {
                        continue;
                    }
                    list.Add(vm);
                }
            }
 
            return list;
        }
 
        public static List<AnalysisDeviationDto> GetCalcFlowAnalysisDeviationDtoList(Dictionary<int, Model.Pump> flag_pump_dict, List<Tuple<string, double, List<Tuple<int, double, double>>>> run_flags_flow_pressure_diff_dict, double minQ = 0, double maxQ = 100000, double spaceQ = 1000)
        {
            if (flag_pump_dict == null || !flag_pump_dict.Any())
            {
                return default;
            }
            if (run_flags_flow_pressure_diff_dict == null || !run_flags_flow_pressure_diff_dict.Any())
            {
                return default;
            }
 
            var list = new List<AnalysisDeviationDto>();
            var error_value = false;
            var opt_std_dev_pop = 0.3;
            for (double current_minQ = minQ; current_minQ <= maxQ; current_minQ += spaceQ)
            {
                var current_maxQ = current_minQ + spaceQ;
                var combine_list = run_flags_flow_pressure_diff_dict.Where(x => x.Item2 >= current_minQ && x.Item2 <= current_maxQ).ToList();
                if (combine_list == null || !combine_list.Any())
                    continue;
                var group_by_run_flags = combine_list.GroupBy(x => x.Item1);
                foreach (var item_run_flags in group_by_run_flags)
                {
                    if (item_run_flags.Count() < 5)
                        continue;
                    var flags = item_run_flags.Key;
                    var vm = new AnalysisDeviationDto();
                    vm.RunFlags = IStation.Untity.IntListHelper.ToList(flags);
                    vm.MinFlow = current_minQ;
                    vm.MaxFlow = current_maxQ;
                    var dev_dict = new Dictionary<int, double>();
                    List<Tuple<int, double, double>> tuple_list = item_run_flags.SelectMany(x => x.Item3).ToList();
                    var group_by_flag = tuple_list.GroupBy(x => x.Item1);
                    foreach (var flag_item in group_by_flag)
                    {
                        var flag = flag_item.Key;
                        var pump = flag_pump_dict[flag];
 
                        var head_diff_list = flag_item.Select(x => x.Item3).ToList();
                        var head_filter_tuple = AnalysisHelper.GetFilterBySTDP(head_diff_list.ToArray(), opt_std_dev_pop);
                        var head_filter_array = head_filter_tuple.Item1;
                        var head_std_dev_pop = head_filter_tuple.Item2;
                        var head_std_dev_pop_head_avg = head_filter_array.Average();
 
                        var flow_list = flag_item.Select(x => x.Item2).ToList();
                        var flow_filter_tuple = AnalysisHelper.GetFilterBySTDP(flow_list.ToArray(), 50, 1, 3);
                        var flow_filter_array = flow_filter_tuple.Item1;
                        var flow_std_dev_pop = flow_filter_tuple.Item2;
                        var flow_std_dev_pop_head_avg = flow_filter_array.Average();
 
                        var other_press = Model.CurveCalcuHelper.CalculateOtherPress(flow_std_dev_pop_head_avg, pump.Ic, pump.Oc, null, null);
                        //if (!pump.IsBp)
                        //{
                        //    other_press = 0;
                        //}
                        var head_diff_avg = head_std_dev_pop_head_avg - other_press;
                        head_diff_avg = Math.Round(head_diff_avg, 6);
                        dev_dict.Add(flag, head_diff_avg);
                    }
                    vm.Count = item_run_flags.Count();
                    vm.PressureDiff = dev_dict;
                    error_value = false;
                    foreach (var dev in dev_dict)
                    {
                        if (Math.Abs(dev.Value) > 3)
                        {
                            error_value = true;
                            break;
                        }
                    }
                    if (error_value)
                    {
                        continue;
                    }
                    list.Add(vm);
                }
            }
 
            return list;
        }
 
 
 
        public static List<AnalysisFactorDto> GetAnalysisFactorDtoList(Dictionary<int, Model.Pump> flag_pump_dict, List<Tuple<int, double, double>> flag_hz_head_diff_list)
        {
            if (flag_pump_dict == null || !flag_pump_dict.Any())
            {
                return default;
            }
            if (flag_hz_head_diff_list == null || !flag_hz_head_diff_list.Any())
            {
                return default;
            }
 
            var exist_data_analysis_factor_list = new List<AnalysisFactorDto>();
            var opt_std_dev_pop = 0.3;
 
            //分析5hz范围内的系数
            var group_by_flag = flag_hz_head_diff_list.GroupBy(x => x.Item1);
            var flag_range5_hz_factor_dict = new Dictionary<int, Dictionary<double, Tuple<double, double, int>>>();
            foreach (var flag_item in group_by_flag)
            {
                var flag = flag_item.Key;
                var pump = flag_pump_dict[flag];
                if (!pump.IsBp)
                    continue;
 
                for (double i = 20; i <= 50; i += 5)
                {
                    var min_hz = i - 5;
                    double max_hz = i;
 
                    var list = flag_item.Where(x => x.Item2 >= min_hz && x.Item2 <= max_hz).ToList();
                    if (list == null || list.Count() < 10)
                        continue;
 
                    var head_diff_list = list.Select(x => x.Item3).ToList();
 
                    var filter_tuple = GetFilterBySTDP(head_diff_list.ToArray(), opt_std_dev_pop);
                    var filter_array = filter_tuple.Item1;
                    var std_dev_pop = filter_tuple.Item2;
                    var std_dev_pop_head_avg = filter_array.Average();
 
                    if (!flag_range5_hz_factor_dict.ContainsKey(flag))
                    {
                        flag_range5_hz_factor_dict.Add(flag, new Dictionary<double, Tuple<double, double, int>>());
                    }
 
                    flag_range5_hz_factor_dict[flag].Add(max_hz, new Tuple<double, double, int>(std_dev_pop, std_dev_pop_head_avg, filter_array.Count()));
                }
            }
 
 
            //分析1hz范围内的系数
            var group_by_flag_hz = flag_hz_head_diff_list.GroupBy(x => new { x.Item1, x.Item2 });
            foreach (var item in group_by_flag_hz)
            {
                var hz = item.Key.Item2;
                if (hz < 20)
                    continue;
                if (item.Count() < 5)
                    continue;
 
 
                var flag = item.Key.Item1;
                var pump = flag_pump_dict[flag];
                var head_diff_list = item.Select(x => x.Item3).ToList();
                var filter_std_dev_pop_tuple = GetFilterBySTDP(head_diff_list.ToArray(), opt_std_dev_pop);
                var filter_std_dev_pop_array = filter_std_dev_pop_tuple.Item1;
                var std_dev_pop = filter_std_dev_pop_tuple.Item2;
                var std_dev_pop_head_avg = filter_std_dev_pop_array.Average();
                var head_diff_avg = std_dev_pop_head_avg;
                if (Math.Abs(head_diff_avg) > 5)
                    continue;
                if (Math.Abs(std_dev_pop) > 1)
                    continue;
 
                if (Math.Abs(std_dev_pop) > 0.5 && item.Count() < 10)
                    continue;
 
                var analysis_facotr = new AnalysisFactorDto();
                analysis_facotr.Flag = flag;
                analysis_facotr.Hz = hz;
                analysis_facotr.HeadSTDP = std_dev_pop;
                analysis_facotr.HeadDeviation = std_dev_pop_head_avg;
                analysis_facotr.Accuracy = 0;
                exist_data_analysis_factor_list.Add(analysis_facotr);
            }
            exist_data_analysis_factor_list = exist_data_analysis_factor_list.OrderBy(x => x.Flag).ThenBy(x => x.Hz).ToList();
 
            //相似换算修正系数(不存在监测数据的频率)
            var similar_analysis_factor_list = new List<AnalysisFactorDto>();
            foreach (var pump_item in flag_pump_dict)
            {
                var flag = pump_item.Key;
                var pump = pump_item.Value;
                if (!pump.IsBp)
                    continue;
                if (!flag_range5_hz_factor_dict.ContainsKey(flag))
                    continue;
                var range5_hz_item = flag_range5_hz_factor_dict[flag];
                for (int hz = 1; hz <= 50; hz++)
                {
                    if (exist_data_analysis_factor_list.Exists(x => x.Flag == flag && x.Hz == hz))
                        continue;
 
                    var hz_range_num = hz / 5;
                    var hz_key = (hz_range_num + 1) * 5;
                    if (hz_key > 50)
                        hz_key = 50;
                    if (range5_hz_item.ContainsKey(hz_key))
                    {
                        var item = range5_hz_item[hz_key];
                        var analysis_facotr = new AnalysisFactorDto();
                        analysis_facotr.Flag = flag;
                        analysis_facotr.Hz = hz;
                        analysis_facotr.HeadSTDP = item.Item1;
                        analysis_facotr.HeadDeviation = item.Item2;
                        analysis_facotr.Accuracy = 0;
                        similar_analysis_factor_list.Add(analysis_facotr);
                    }
                    else
                    {
                        var hz_list = range5_hz_item.Select(x => x.Key).ToList();
                        var similar_hz = hz_list.Where(x => x >= hz_key)?.OrderBy(x => x).FirstOrDefault();
                        if (similar_hz == null || similar_hz.Value == 0)
                        {
                            similar_hz = hz_list.OrderByDescending(x => x).FirstOrDefault();
                        }
                        if (similar_hz == null)
                        {
                            throw new Exception("similar_hz error");
                        }
                        var tuple = range5_hz_item[similar_hz.Value];
                        var similar_ratio = hz / similar_hz.Value;
                        var head_dev = tuple.Item2 * similar_ratio;
                        var head_stdp = tuple.Item1 * similar_ratio;
 
                        var analysis_facotr = new AnalysisFactorDto();
                        analysis_facotr.Flag = flag;
                        analysis_facotr.Hz = hz;
                        analysis_facotr.HeadSTDP = head_stdp;
                        analysis_facotr.HeadDeviation = head_dev;
                        analysis_facotr.Accuracy = -1;
                        similar_analysis_factor_list.Add(analysis_facotr);
                    }
                }
            }
 
            var all_analysis_factor_list = new List<AnalysisFactorDto>();
            all_analysis_factor_list.AddRange(exist_data_analysis_factor_list);
            all_analysis_factor_list.AddRange(similar_analysis_factor_list);
            all_analysis_factor_list = all_analysis_factor_list.OrderBy(x => x.Flag).ThenBy(x => x.Hz).ToList();
            return all_analysis_factor_list;
        }
 
        public static Tuple<double[], double> GetFilterBySTDP(double[] array, double min_dev = 0.3, int start_index = 0, int max_iteration_count = 1)
        {
            if (array == null || !array.Any())
            {
                return default;
            }
            var array_avg = array.Average();
            var array_count = array.Count();
 
 
            FilterOutliersHelper.STDEV(array, out double std_dev, out double std_dev_pop);
            if (Math.Abs(std_dev_pop) < min_dev)
            {
                return new Tuple<double[], double>(array, std_dev_pop);
            }
            if (start_index > max_iteration_count)
            {
                return new Tuple<double[], double>(array, std_dev_pop);
            }
            var filter_array = array.Where(x => !(Math.Abs(x - array_avg) > Math.Abs(std_dev_pop * 2))).ToArray();
            if (filter_array == null || !filter_array.Any())
            {
                return default;
            }
            start_index++;
            return GetFilterBySTDP(filter_array, min_dev, start_index, max_iteration_count);
        }
 
        public static List<StationMapping> GetStationMappingList()
        {
            var list = new List<StationMapping>();
            var dn2400 = new StationMapping();
            var dn2700 = new StationMapping();
            var jd1 = new StationMapping();
            var jd2 = new StationMapping();
            var jd3 = new StationMapping();
 
 
 
            jd1.Name = "JD1";
            jd2.Name = "JD2";
            jd3.Name = "JD3";
            dn2400.Name = "DN2400";
            dn2700.Name = "SN2700";
 
 
            jd1.ScadaFlowId = "SFJD1";
            jd2.ScadaFlowId = "SFJD2";
            jd3.ScadaFlowId = "SFJD3";
            dn2400.ScadaFlowId = "SFDN2400";
            dn2700.ScadaFlowId = "SFDN2700";
 
 
            jd1.ScadaPressureId = "SPJD1";
            jd2.ScadaPressureId = "SPJD2";
            jd3.ScadaPressureId = "SPJD3";
            dn2400.ScadaPressureId = "SPDN2400";
            dn2700.ScadaPressureId = "SPDN2700";
 
            jd1.FlowId = "Pjd1";
            jd2.FlowId = "Pjd2";
            jd3.FlowId = "Pjd3";
            dn2400.FlowId = "Pdn2400";
            dn2700.FlowId = "Pdn2700";
 
            jd1.PressureId = "Jjd1";
            jd2.PressureId = "Jjd2";
            jd3.PressureId = "Jjd3";
            dn2400.PressureId = "Jdn2400";
            dn2700.PressureId = "Jdn2700";
 
            list.Add(jd1);
            list.Add(jd2);
            list.Add(jd3);
            list.Add(dn2400);
            list.Add(dn2700);
            return list;
        }
 
        public static List<PumpMapping> GetPumpModelMappingList()
        {
            var list = new List<PumpMapping>();
 
            var pump11 = new PumpMapping();
            var pump12 = new PumpMapping();
            var pump13 = new PumpMapping();
            var pump14 = new PumpMapping();
            var pump15 = new PumpMapping();
            var pump16 = new PumpMapping();
            var pump17 = new PumpMapping();
            var pump18 = new PumpMapping();
            var pump21 = new PumpMapping();
            var pump22 = new PumpMapping();
            var pump23 = new PumpMapping();
            var pump24 = new PumpMapping();
            var pump25 = new PumpMapping();
            var pump26 = new PumpMapping();
            var pump27 = new PumpMapping();
 
            pump11.Flag = GlobalHelperW.Flag11;
            pump12.Flag = GlobalHelperW.Flag12;
            pump13.Flag = GlobalHelperW.Flag13;
            pump14.Flag = GlobalHelperW.Flag14;
            pump15.Flag = GlobalHelperW.Flag15;
            pump16.Flag = GlobalHelperW.Flag16;
            pump17.Flag = GlobalHelperW.Flag17;
            pump18.Flag = GlobalHelperW.Flag18;
            pump21.Flag = GlobalHelperW.Flag21;
            pump22.Flag = GlobalHelperW.Flag22;
            pump23.Flag = GlobalHelperW.Flag23;
            pump24.Flag = GlobalHelperW.Flag24;
            pump25.Flag = GlobalHelperW.Flag25;
            pump26.Flag = GlobalHelperW.Flag26;
            pump27.Flag = GlobalHelperW.Flag27;
 
            pump11.PumpId = "Pump11";
            pump12.PumpId = "Pump12";
            pump13.PumpId = "Pump13";
            pump14.PumpId = "Pump14";
            pump15.PumpId = "Pump15";
            pump16.PumpId = "Pump16";
            pump17.PumpId = "Pump17";
            pump18.PumpId = "Pump18";
            pump21.PumpId = "Pump21";
            pump22.PumpId = "Pump22";
            pump23.PumpId = "Pump23";
            pump24.PumpId = "Pump24";
            pump25.PumpId = "Pump25";
            pump26.PumpId = "Pump26";
            pump27.PumpId = "Pump27";
 
            pump11.FlowId = "";
            pump12.FlowId = "";
            pump13.FlowId = "";
            pump14.FlowId = "";
            pump15.FlowId = "";
            pump16.FlowId = "";
            pump17.FlowId = "";
            pump18.FlowId = "";
            pump21.FlowId = "Ppump21";
            pump22.FlowId = "Ppump22";
            pump23.FlowId = "Ppump23";
            pump24.FlowId = "Ppump24";
            pump25.FlowId = "Ppump25";
            pump26.FlowId = "Ppump26";
            pump27.FlowId = "Ppump27";
 
            pump11.PressureId = "Jpump11";
            pump12.PressureId = "Jpump12";
            pump13.PressureId = "Jpump13";
            pump14.PressureId = "Jpump14";
            pump15.PressureId = "Jpump15";
            pump16.PressureId = "Jpump16";
            pump17.PressureId = "Jpump17";
            pump18.PressureId = "Jpump18";
            pump21.PressureId = "Jpump21";
            pump22.PressureId = "Jpump22";
            pump23.PressureId = "Jpump23";
            pump24.PressureId = "Jpump24";
            pump25.PressureId = "Jpump25";
            pump26.PressureId = "Jpump26";
            pump27.PressureId = "Jpump27";
 
            pump11.CurveId = "11";
            pump12.CurveId = "12";
            pump13.CurveId = "13";
            pump14.CurveId = "14";
            pump15.CurveId = "15";
            pump16.CurveId = "16";
            pump17.CurveId = "17";
            pump18.CurveId = "18";
            pump21.CurveId = "21";
            pump22.CurveId = "22";
            pump23.CurveId = "23";
            pump24.CurveId = "24";
            pump25.CurveId = "25";
            pump26.CurveId = "26";
            pump27.CurveId = "27";
 
 
            list.Add(pump11);
            list.Add(pump12);
            list.Add(pump13);
            list.Add(pump14);
            list.Add(pump15);
            list.Add(pump16);
            list.Add(pump17);
            list.Add(pump18);
            list.Add(pump21);
            list.Add(pump22);
            list.Add(pump23);
            list.Add(pump24);
            list.Add(pump25);
            list.Add(pump26);
            list.Add(pump27);
            return list;
        }
 
        public static List<string> GetPatternIdList()
        {
            var list = new List<string>();
            list.Add("Pump11");
            list.Add("Pump12");
            list.Add("Pump13");
            list.Add("Pump14");
            list.Add("Pump15");
            list.Add("Pump16");
            list.Add("Pump17");
            list.Add("Pump18");
            list.Add("R3");
            list.Add("R2");
            list.Add("R1");
            list.Add("SFJD1");
            list.Add("SFJD2");
            list.Add("SFJD3");
            list.Add("RPump21");
            list.Add("RPump22");
            list.Add("RPump23");
            list.Add("RPump24");
            list.Add("RPump25");
            list.Add("RPump26");
            list.Add("RPump27");
            list.Add("SFPump21");
            list.Add("SFPump22");
            list.Add("SFPump23");
            list.Add("SFPump24");
            list.Add("SFPump25");
            list.Add("SFPump26");
            list.Add("SFPump27");
            list.Add("Pump21");
            list.Add("Pump22");
            list.Add("Pump23");
            list.Add("Pump24");
            list.Add("Pump25");
            list.Add("Pump26");
            list.Add("Pump27");
            list.Add("SFDN2400");
            list.Add("SFDN2700");
            return list;
        }
 
        public static Dictionary<string, double> GetWLDict(Dictionary<int, double> flag_pressure_diff_dev)
        {
            if (flag_pressure_diff_dev == null || !flag_pressure_diff_dev.Any())
            {
                return default;
            }
 
            var wl_dict = new Dictionary<string, double>();
            wl_dict.Add("R3", 0);
            wl_dict.Add("R2", 0);
            wl_dict.Add("R1", 0);
 
            wl_dict.Add("RPump21", 0);
            wl_dict.Add("RPump22", 0);
            wl_dict.Add("RPump23", 0);
            wl_dict.Add("RPump24", 0);
            wl_dict.Add("RPump25", 0);
            wl_dict.Add("RPump26", 0);
            wl_dict.Add("RPump27", 0);
 
 
            double? r1 = null, r2 = null;
            foreach (var item in flag_pressure_diff_dev)
            {
                var flag = item.Key;
                var pressure_diff_dev = item.Value;
 
                if (flag < 16 && r2 == null)
                {
                    r2 = pressure_diff_dev;
                    wl_dict["R2"] = r2 ?? 0;
                }
                else if (flag < 21 && r1 == null)
                {
                    r1 = pressure_diff_dev;
                    wl_dict["R1"] = r1 ?? 0;
                }
                else
                {
                    switch (flag)
                    {
                        case 21: wl_dict["RPump21"] = pressure_diff_dev; break;
                        case 22: wl_dict["RPump22"] = pressure_diff_dev; break;
                        case 23: wl_dict["RPump23"] = pressure_diff_dev; break;
                        case 24: wl_dict["RPump24"] = pressure_diff_dev; break;
                        case 25: wl_dict["RPump25"] = pressure_diff_dev; break;
                        case 26: wl_dict["RPump26"] = pressure_diff_dev; break;
                        case 27: wl_dict["RPump27"] = pressure_diff_dev; break;
                        default:
                            break;
                    }
                }
            }
 
            return wl_dict;
 
        }
 
        #endregion
 
    }
}