GSF.EMAX.CorrectiveParser.ReadNext C# (CSharp) Method

ReadNext() public method

Reads next EMAX record.
public ReadNext ( ) : bool
return bool
        public bool ReadNext()
        {
            double frequency;
            double diff;
            int halfCycles;

            if (m_parser.ReadNext())
            {
                // Calculate the timestamp
                m_calculatedTimestamp = m_currentSecond.AddTicks(m_subsecondDistribution[m_currentIndex]);

                if (!m_parser.TimeError)
                {
                    // Correct the values
                    frequency = m_parser.ControlFile.SystemParameters.frequency;
                    diff = Math.Abs(m_calculatedTimestamp.Subtract(m_parser.Timestamp).TotalSeconds);
                    halfCycles = (int)Math.Round(diff * frequency * 2.0D);

                    if (halfCycles % 2 == 0)
                        Array.Copy(Values, m_correctedValues, m_correctedValues.Length);
                    else
                        Values.Select(v => -v).ToList().CopyTo(m_correctedValues);
                }
                else
                {
                    // We can't determine whether the values need correction
                    // without a proper timestamp from the underlying parser
                    Array.Copy(Values, m_correctedValues, m_correctedValues.Length);
                }

                // Move to the next subsecond in the distribution
                m_currentIndex++;

                if (m_currentIndex == m_subsecondDistribution.Length)
                {
                    // If we reach the end of the subsecond distribution,
                    // add one to currentSecond and reset the currentIndex to zero
                    m_currentSecond = m_currentSecond.AddSeconds(1.0D);
                    m_currentIndex = 0;
                }

                return true;
            }

            return false;
        }

Usage Example

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