Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace jnsoft.MDF.Examples.Reader
- {
- /// <summary>
- /// MDF Reader example.
- ///
- /// - Loading an MDF file
- /// - Read data as raw data samples.
- /// - Iterating over measurement data
- ///
- /// Usage: ASAP2MDFReaderExample MDFFile.mdf
- /// </summary>
- class Program
- {
- /// <summary>
- /// Main function.
- /// </summary>
- /// <param name="args">The commandline arguments</param>
- /// <returns>Console application exitcode (0 if successful)</returns>
- static int Main(string[] args)
- {
- if (args.Length != 1)
- { // no args -> present usage
- Console.WriteLine($"{Extensions.AppName}({Extensions.AppVersion})");
- Console.WriteLine("\t- Read raw data samples from a MDF file...");
- Console.WriteLine("\t- Read and display physical measurement data from a MDF file...");
- Console.WriteLine("Required input: An MDF (measurement data format) file");
- Console.WriteLine("$Usage: {Application.ProductName} MDFFile.mdf");
- Console.WriteLine("$Example: {Application.ProductName} MDFExample.mdf");
- return -1;
- }
- try
- { // Open specified MDF file
- using (var mdfFile = MDFReader.open(args[0]))
- {
- Console.WriteLine($"Timeline: {mdfFile.getLength()} seconds, {mdfFile.getSamplePointCount():N0} sample points");
- var channels = new List<ICNBLOCK>(mdfFile.OpenChannels);
- var channel = channels.FirstOrDefault(x => x.ChannelType == ChannelType.Data && x.SignalType < SignalType.STRING);
- Console.WriteLine($"First data channel contains {channel.CGBlock.RecordCount} records.");
- var firstUnit = string.IsNullOrEmpty(channel.Unit) ? string.Empty : $" {channel.Unit}";
- // Iterate samples over a single data channel for the first 1.4 seconds
- foreach (var sample in mdfFile.enumSamples(channel, ValueObjectFormat.Physical, 0, 1.4))
- Console.WriteLine($"Channel = {channel.Name}: Time {sample.X}[s]={sample.Y}{firstUnit}");
- Console.WriteLine();
- // Iterate raw data samples over a single data channel for the first 1.4 seconds
- IList<RawDataRecord> rawData; DataLimits rawLimitX;
- if (!mdfFile.getRawData(0, 1.4, channel, out rawData, out rawLimitX))
- // failed to read raw data
- return -2;
- foreach(var entry in rawData)
- Console.WriteLine($"Channel = {channel.Name}(Raw data): Time {entry.Timestamp}[s]={BitConverter.ToString(entry.Data)}");
- Console.WriteLine();
- // read all samples as physical values from the first 1.4 seconds
- foreach (var group in mdfFile.OpenChannelGroups)
- {
- if (!mdfFile.getData(group, out double[][] data, ValueObjectFormat.Physical, 0, 1.4))
- // no data available
- continue;
- var time = data[0];
- var sampleCnt = time.Length;
- for (int chnIdx = 1; chnIdx < group.CNBlocks.Count; ++chnIdx)
- { // iterate over data channels
- channel = group.CNBlocks[chnIdx];
- var unit = string.IsNullOrEmpty(channel.Unit) ? string.Empty : $" {channel.Unit}";
- for (int i = 0; i < sampleCnt; ++i)
- Console.WriteLine($"Channel = {channel.Name}: Time {time[i]}[s]={data[chnIdx][i]}{unit}");
- }
- }
- }
- return 0;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Something failed, file not found or unsupported\n{ex.Message}");
- return -2;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment