/// <summary> /// Parses an impedance from a line in the report file /// </summary> /// <param name="marker"></param> /// <param name="text"></param> /// <returns></returns> private static Impedance GetImpedance(string marker, string text) { int index = text.IndexOf(marker); if (index == -1) { return new Impedance { Reactance = 0, Resistance = 0 } } ; int start = index + marker.Length; string substr = text.Substring(start); Regex r = new Regex("(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?"); var matches = r.Matches(substr); if (matches.Count < 2) { throw new Exception($"Failed to find impedance [{marker}] in string [{text}]"); } Impedance result = new Impedance { Reactance = double.Parse(matches[1].Value), Resistance = double.Parse(matches[0].Value) }; return(result); }
public void Initialize() { SelfImpedance = new Impedance[3]; MutualImpedance = new Impedance[3]; }
/// <summary> /// Looks for a leika file on the path, and tries to extract the impedance data from it /// </summary> /// <param name="fileName"></param> /// <param name="path"></param> /// <returns></returns> private static LineParameters?GetLeikaData(string fileName, string path, int phases) { try { if (phases < 1 || phases > 3) { Console.WriteLine($"Invalid number of phases for file {fileName}"); return(null); } //get all the report files var files = Directory.GetFiles(path, "*.txt"); //what is this? //TODO: just get the file we need for (int i = 0; i < files.Length; i++) { files[i] = Path.GetFileName(files[i]); } if (files.Contains(fileName + ".txt")) { var lines = File.ReadAllLines(Path.Combine(path, fileName) + ".txt"); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("IMPEDANCES AFTER ELIMINATION OF EARTHED CONDUCTORS")) { if (i + 18 > lines.Length) { Console.WriteLine("Was expecting more lines in file"); break; } Impedance zero = new Impedance { Reactance = 0, Resistance = 0 }; LineParameters p = new LineParameters(); //TODO: we should be handling the per phase cases here p.Initialize(); p.SelfImpedance[0] = GetImpedance("1L1", lines[i + 9]); if (phases >= 2) { p.SelfImpedance[1] = GetImpedance("1L2", lines[i + 9]); } else { p.SelfImpedance[1] = zero; } if (phases == 3) { p.SelfImpedance[2] = GetImpedance("1L3", lines[i + 9]); } else { p.SelfImpedance[2] = zero; } if (phases >= 2) { p.MutualImpedance[0] = GetImpedance("1L1-1L2", lines[i + 17]); } else { p.MutualImpedance[0] = zero; } if (phases == 3) { p.MutualImpedance[1] = GetImpedance("1L1-1L3", lines[i + 18]); p.MutualImpedance[2] = GetImpedance("1L2-1L3", lines[i + 18]); } else { p.MutualImpedance[1] = zero; p.MutualImpedance[2] = zero; } return(p); } } } else { Console.WriteLine($"File {fileName}.txt was not found on the leika path"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(null); }