private void EMAXButton_Click(object sender, EventArgs e)
{
string directory;
string rootFileName;
string controlFileName;
DateTime? startTime = null;
DateTime timestamp;
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "EMAX Files|*.rcd;*.rcl|All Files|*.*";
dialog.Title = "Browse EMAX Files";
if (dialog.ShowDialog() == DialogResult.Cancel)
return;
if (!File.Exists(dialog.FileName))
return;
// EMAX parsing will require a CTL file, make sure this exists...
directory = Path.GetDirectoryName(dialog.FileName) ?? string.Empty;
rootFileName = FilePath.GetFileNameWithoutExtension(dialog.FileName);
controlFileName = Path.Combine(directory, rootFileName + ".ctl");
if (!File.Exists(controlFileName))
return;
using (CorrectiveParser parser = new CorrectiveParser())
{
parser.ControlFile = new ControlFile(controlFileName);
parser.FileName = dialog.FileName;
// Open EMAX data file
parser.OpenFiles();
// Parse EMAX control file into channels
m_channels = parser.ControlFile.AnalogChannelSettings.Values
.Select(channel => new ParsedChannel()
{
Index = Convert.ToInt32(channel.chanlnum),
Name = channel.title,
TimeValues = new List<DateTime>(),
XValues = new List<object>(),
YValues = new List<object>()
})
.OrderBy(channel => channel.Index)
.ToList();
// Read values from EMAX data file
while (parser.ReadNext())
{
timestamp = parser.CalculatedTimestamp;
// If this is the first frame, store this frame's
// timestamp as the start time of the file
if ((object)startTime == null)
startTime = timestamp;
// Read the values from this frame into
// x- and y-value collections for each channel
for (int i = 0; i < m_channels.Count; i++)
{
m_channels[i].TimeValues.Add(timestamp);
m_channels[i].XValues.Add(timestamp.Subtract(startTime.Value).TotalSeconds);
m_channels[i].YValues.Add(parser.CorrectedValues[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("EMAX - [{0}]", dialog.SafeFileName);
}
}