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); } }
/// <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 FixCOMTRADEData(COMTRADEData comtradeData, Parser parser, TimeSpan timeZoneOffset) { // Function to convert a digital channel from the COMTRADE files to COMTRADEChannelData Func<DigitalChannel, int, COMTRADEChannelData> toChannelData = (digitalChannel, index) => new COMTRADEChannelData() { GroupOrder = 1, LoadOrder = index, OriginalChannelIndex = index, Name = digitalChannel.Name, Data = new DataSeries(), OriginalDigitalChannel = digitalChannel }; // List of COMTRADEChannelData containing data about the digital channels in the COMTRADE file List<COMTRADEChannelData> originalDigitalChannelData = parser.Schema.DigitalChannels .Select(toChannelData) .ToList(); // The number of analog channels in the COMTRADE file int analogChannels = parser.Schema.AnalogChannels.Length; // Set COMTRADE schema information to match that of the original file comtradeData.StationName = parser.Schema.StationName; comtradeData.DeviceID = parser.Schema.DeviceID; // First map the analog channel data to the analog channels in the file, then update the analog channel data to match the information in the file foreach (var mapping in comtradeData.AnalogChannelData.Join(parser.Schema.AnalogChannels, channelData => channelData.OriginalChannelIndex, channel => channel.Index, (channelData, channel) => new { ChannelData = channelData, Channel = channel })) { mapping.ChannelData.Name = mapping.Channel.Name; mapping.ChannelData.OriginalAnalogChannel = mapping.Channel; } // Populate the new digital channels with data from the COMTRADE file while (parser.ReadNext()) { foreach (COMTRADEChannelData channelData in originalDigitalChannelData) { channelData.Data.DataPoints.Add(new DataPoint() { Time = parser.Timestamp + timeZoneOffset, Value = parser.Values[analogChannels + channelData.OriginalChannelIndex] }); } } // Add the new digital channels to the digitalChannelData collection comtradeData.DigitalChannelData.AddRange(originalDigitalChannelData); }
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); } }