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);
}
}