public bool CanParse(string filePath, DateTime fileCreationTime) { string schemaFileName = Path.ChangeExtension(filePath, "cfg"); string extension = FilePath.GetExtension(filePath); string[] fileList = FilePath.GetFileList(Path.ChangeExtension(filePath, "*")); bool multipleDataFiles = !extension.Equals(".dat", StringComparison.OrdinalIgnoreCase); if (!File.Exists(schemaFileName)) return false; if (fileList.Any(file => !FilePath.TryGetReadLockExclusive(file))) return false; if (multipleDataFiles && DateTime.UtcNow - fileCreationTime < m_minWaitTime) return false; try { m_parser = new Parser(); m_parser.Schema = new Schema(schemaFileName); m_parser.FileName = filePath; m_parser.InferTimeFromSampleRates = true; m_parser.OpenFiles(); } catch (IOException) { return false; } return true; }
/// <summary> /// Populate known voltage and current data from PQDIF file. /// </summary> /// <param name="faultDataSet">Fault data set to be populated.</param> /// <param name="settings">Source parameters.</param> /// <param name="line">Associated XML event file definition.</param> public static void PopulateDataSet(FaultLocationDataSet faultDataSet, Dictionary<string, string> settings, Line line) { string fileName; if ((object)line == null) throw new ArgumentNullException("line"); if (!settings.TryGetValue("fileName", out fileName) || !File.Exists(fileName)) throw new ArgumentException("Parameters must define a valid \"fileName\" setting."); // Comtrade parsing will require a CFG file, make sure this exists... string directory = Path.GetDirectoryName(fileName) ?? string.Empty; string rootFileName = FilePath.GetFileNameWithoutExtension(fileName); string configurationFileName = Path.Combine(directory, rootFileName + ".cfg"); if (!File.Exists(configurationFileName)) throw new FileNotFoundException(string.Format("Associated CFG file \"{0}\" for COMTRADE data file does not exist - cannot parse COMTRADE file.", configurationFileName)); // Parse configuration file Schema schema = new Schema(configurationFileName); // Find <Channels> element in XML line definition XElement channels = line.ChannelsElement; if ((object)channels == null) throw new NullReferenceException("No \"<channels>\" element was found in event file definition - cannot load COMTRADE data file."); // Extract COMTRADE channel ID's for desired voltage and current elements IEnumerable<Tuple<int, int>> vaIndexes = GetValueIndex(schema, channels, "VA").ToList(); IEnumerable<Tuple<int, int>> vbIndexes = GetValueIndex(schema, channels, "VB").ToList(); IEnumerable<Tuple<int, int>> vcIndexes = GetValueIndex(schema, channels, "VC").ToList(); IEnumerable<Tuple<int, int>> iaIndexes = GetValueIndex(schema, channels, "IA").ToList(); IEnumerable<Tuple<int, int>> ibIndexes = GetValueIndex(schema, channels, "IB").ToList(); IEnumerable<Tuple<int, int>> icIndexes = GetValueIndex(schema, channels, "IC").ToList(); List<long> times = new List<long>(); List<double> vaValues = new List<double>(); List<double> vbValues = new List<double>(); List<double> vcValues = new List<double>(); List<double> iaValues = new List<double>(); List<double> ibValues = new List<double>(); List<double> icValues = new List<double>(); SampleRate sampleRate; ValidateIndexes("VA", vaIndexes); ValidateIndexes("VB", vbIndexes); ValidateIndexes("VC", vcIndexes); ValidateIndexes("IA", iaIndexes); ValidateIndexes("IB", ibIndexes); ValidateIndexes("IC", icIndexes); // Create a new COMTRADE file parser using (Parser parser = new Parser() { Schema = schema, FileName = fileName, InferTimeFromSampleRates = true }) { // Open COMTRADE data files parser.OpenFiles(); faultDataSet.Frequency = schema.NominalFrequency; sampleRate = schema.SampleRates.First(); if (sampleRate.Rate != 0) faultDataSet.SetSampleRates((int)(sampleRate.Rate / faultDataSet.Frequency)); // Read all COMTRADE records while (parser.ReadNext()) { times.Add(parser.Timestamp.Ticks); vaValues.Add(GetValue(parser, vaIndexes)); vbValues.Add(GetValue(parser, vbIndexes)); vcValues.Add(GetValue(parser, vcIndexes)); iaValues.Add(GetValue(parser, iaIndexes)); ibValues.Add(GetValue(parser, ibIndexes)); icValues.Add(GetValue(parser, icIndexes)); } } // Populate voltage data set faultDataSet.Voltages.AN.Times = times.ToArray(); faultDataSet.Voltages.AN.Measurements = vaValues.ToArray(); faultDataSet.Voltages.BN.Times = times.ToArray(); faultDataSet.Voltages.BN.Measurements = vbValues.ToArray(); faultDataSet.Voltages.CN.Times = times.ToArray(); faultDataSet.Voltages.CN.Measurements = vcValues.ToArray(); // Populate current data set faultDataSet.Currents.AN.Times = times.ToArray(); faultDataSet.Currents.AN.Measurements = iaValues.ToArray(); faultDataSet.Currents.BN.Times = times.ToArray(); faultDataSet.Currents.BN.Measurements = ibValues.ToArray(); faultDataSet.Currents.CN.Times = times.ToArray(); faultDataSet.Currents.CN.Measurements = icValues.ToArray(); }
private void COMTRADEButton_Click(object sender, EventArgs e) { string directory; string rootFileName; string configurationFileName; DateTime?startTime = null; using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "COMTRADE Files|*.dat;*.d00|All Files|*.*"; dialog.Title = "Browse COMTRADE Files"; if (dialog.ShowDialog() == DialogResult.Cancel) { return; } if (!File.Exists(dialog.FileName)) { return; } // Comtrade parsing will require a CFG file, make sure this exists... directory = Path.GetDirectoryName(dialog.FileName) ?? string.Empty; rootFileName = FilePath.GetFileNameWithoutExtension(dialog.FileName); configurationFileName = Path.Combine(directory, rootFileName + ".cfg"); if (!File.Exists(configurationFileName)) { return; } using (Parser parser = new Parser()) { parser.Schema = new Schema(configurationFileName); parser.FileName = dialog.FileName; parser.InferTimeFromSampleRates = true; // Open COMTRADE data files parser.OpenFiles(); // Parse COMTRADE schema into channels m_channels = parser.Schema.AnalogChannels .Select(channel => new ParsedChannel() { Index = channel.Index, Name = ((object)channel.ChannelName != null) ? string.Format("{0} ({1})", channel.StationName, channel.ChannelName) : channel.StationName, TimeValues = new List <DateTime>(), XValues = new List <object>(), YValues = new List <object>() }).ToList(); // Read values from COMTRADE data file while (parser.ReadNext()) { if ((object)startTime == null) { startTime = parser.Timestamp; } for (int i = 0; i < m_channels.Count; i++) { m_channels[i].TimeValues.Add(parser.Timestamp); m_channels[i].XValues.Add(parser.Timestamp.Subtract(startTime.Value).TotalSeconds); m_channels[i].YValues.Add(parser.Values[i]); } } } // Clear the list box and data chart ChannelListBox.Items.Clear(); DataChart.Series.Clear(); // Populate the list box with channel names ChannelListBox.Items.AddRange(m_channels .Select(channel => string.Format("[{0}] {1}", channel.Index, channel.Name)) .Cast <object>() .ToArray()); // Select the first channel in the list ChannelListBox.SelectedIndex = 0; // Change the title text of the window to show what file the user has open m_fileName = dialog.SafeFileName; Text = string.Format("COMTRADE - [{0}]", dialog.SafeFileName); } }
private void COMTRADEButton_Click(object sender, EventArgs e) { string directory; string rootFileName; string configurationFileName; DateTime? startTime = null; using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "COMTRADE Files|*.dat;*.d00|All Files|*.*"; dialog.Title = "Browse COMTRADE Files"; if (dialog.ShowDialog() == DialogResult.Cancel) return; if (!File.Exists(dialog.FileName)) return; // Comtrade parsing will require a CFG file, make sure this exists... directory = Path.GetDirectoryName(dialog.FileName) ?? string.Empty; rootFileName = FilePath.GetFileNameWithoutExtension(dialog.FileName); configurationFileName = Path.Combine(directory, rootFileName + ".cfg"); if (!File.Exists(configurationFileName)) return; using (Parser parser = new Parser()) { parser.Schema = new Schema(configurationFileName); parser.FileName = dialog.FileName; parser.InferTimeFromSampleRates = true; // Open COMTRADE data files parser.OpenFiles(); // Parse COMTRADE schema into channels m_channels = parser.Schema.AnalogChannels .Select(channel => new ParsedChannel() { Index = channel.Index, Name = ((object)channel.ChannelName != null) ? string.Format("{0} ({1})", channel.StationName, channel.ChannelName) : channel.StationName, TimeValues = new List<DateTime>(), XValues = new List<object>(), YValues = new List<object>() }).ToList(); // Read values from COMTRADE data file while (parser.ReadNext()) { if ((object)startTime == null) startTime = parser.Timestamp; for (int i = 0; i < m_channels.Count; i++) { m_channels[i].TimeValues.Add(parser.Timestamp); m_channels[i].XValues.Add(parser.Timestamp.Subtract(startTime.Value).TotalSeconds); m_channels[i].YValues.Add(parser.Values[i]); } } } // Clear the list box and data chart ChannelListBox.Items.Clear(); DataChart.Series.Clear(); // Populate the list box with channel names ChannelListBox.Items.AddRange(m_channels .Select(channel => string.Format("[{0}] {1}", channel.Index, channel.Name)) .Cast<object>() .ToArray()); // Select the first channel in the list ChannelListBox.SelectedIndex = 0; // Change the title text of the window to show what file the user has open m_fileName = dialog.SafeFileName; Text = string.Format("COMTRADE - [{0}]", dialog.SafeFileName); } }