using IStation.Model;
using System.ComponentModel.DataAnnotations;
namespace IStation.Test
{
///
/// 数据导入
/// 根据3倍标准差粗过滤异常数据
/// 再用数据用1hz分类,调用修正算法更新出最新的曲线
/// 缺失的频率向上取最近的hz相似换算下去
/// 现场调度用更新后的曲线调度
///
public class Station2Helper
{
///
/// 绑定数据
///
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();
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>();
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 PumpCurveDataFusionCorrectorHelper();
(double[] mergedX, double[] mergedY, double[] optimizedX, double[] optimizedY) = helper.Corrent(splineX, splineY, measuredXAll, measuredYAll);
if (optimizedX == null)
{
continue;
}
var optList = new List();
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 PumpCurveDataFusionCorrectorHelper();
(double[] mergedX, double[] mergedY, double[] optimizedX, double[] optimizedY) = helper.Corrent(splineX, splineY, measuredXAll, measuredYAll);
if (optimizedX == null)
{
continue;
}
var optList = new List();
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
}