alesi2000

ASAP2MDFReaderExample

May 30th, 2014 (edited)
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using jnsoft.Helpers;
  2.  
  3. namespace jnsoft.MDF.Examples.Reader;
  4.  
  5. /// <summary>
  6. /// MDF Reader example.
  7. ///
  8. /// - Loading an MDF file
  9. /// - Read data as raw data samples.
  10. /// - Iterating over measurement data
  11. ///
  12. /// Usage: ASAP2MDFReaderExample MDFFile.mdf
  13. /// </summary>
  14. class Program
  15. {
  16.   /// <summary>
  17.   /// Main function.
  18.   /// </summary>
  19.   /// <param name="args">The commandline arguments</param>
  20.   /// <returns>Console application exitcode (0 if successful)</returns>
  21.   static int Main(string[] args)
  22.   {
  23.     if (args.Length != 1)
  24.     { // no args -> present usage
  25.       Console.WriteLine($"{Extensions.AppName}({Extensions.AppVersion})");
  26.       Console.WriteLine("\t- Read raw data samples from a MDF file...");
  27.       Console.WriteLine("\t- Read and display physical measurement data from a MDF file...");
  28.       Console.WriteLine("Required input: An MDF (measurement data format) file");
  29.       Console.WriteLine("$Usage: {Application.ProductName} MDFFile.mdf");
  30.       Console.WriteLine("$Example: {Application.ProductName} MDFExample.mdf");
  31.       return -1;
  32.     }
  33.  
  34.     try
  35.     { // Open specified MDF file
  36.       using var mdfFile = MDFReader.open(args[0]);
  37.       Console.WriteLine($"Timeline: {mdfFile.getLength()} seconds, {mdfFile.getSamplePointCount():N0} sample points");
  38.  
  39.       var channels = new List<ICNBLOCK>(mdfFile.OpenChannels);
  40.       var channel = channels.FirstOrDefault(x => x.ChannelType == ChannelType.Data && x.SignalType < SignalType.STRING);
  41.  
  42.       Console.WriteLine($"First data channel contains {channel.CGBlock.RecordCount} records.");
  43.       var firstUnit = string.IsNullOrEmpty(channel.Unit) ? string.Empty : $" {channel.Unit}";
  44.  
  45.       // Iterate samples over a single data channel for the first 1.4 seconds
  46.       foreach (var sample in mdfFile.enumSamples(channel, ValueObjectFormat.Physical, 0, 1.4))
  47.         Console.WriteLine($"Channel = {channel.Name}: Time {sample.X}[s]={sample.Y}{firstUnit}");
  48.       Console.WriteLine();
  49.  
  50.       // Iterate raw data samples over a single data channel for the first 1.4 seconds
  51.       if (!mdfFile.getRawData(0, 1.4, channel, out var rawData, out var rawLimitX))
  52.         // failed to read raw data
  53.         return -2;
  54.       foreach (var entry in rawData)
  55.         Console.WriteLine($"Channel = {channel.Name}(Raw data): Time {entry.Timestamp}[s]={BitConverter.ToString(entry.Data)}");
  56.       Console.WriteLine();
  57.  
  58.       // read all samples as physical values from the first 1.4 seconds
  59.       foreach (var group in mdfFile.OpenChannelGroups)
  60.       {
  61.         if (!mdfFile.getData(group, out double[][] data, ValueObjectFormat.Physical, 0, 1.4))
  62.           // no data available
  63.           continue;
  64.  
  65.         var time = data[0];
  66.         var sampleCnt = time.Length;
  67.         for (int chnIdx = 1; chnIdx < group.CNBlocks.Count; ++chnIdx)
  68.         { // iterate over data channels
  69.           channel = group.CNBlocks[chnIdx];
  70.           var unit = string.IsNullOrEmpty(channel.Unit) ? string.Empty : $" {channel.Unit}";
  71.           for (int i = 0; i < sampleCnt; ++i)
  72.             Console.WriteLine($"Channel = {channel.Name}: Time {time[i]}[s]={data[chnIdx][i]}{unit}");
  73.         }
  74.       }
  75.       return 0;
  76.     }
  77.     catch (Exception ex)
  78.     {
  79.       Console.WriteLine($"Something failed, file not found or unsupported\n{ex.Message}");
  80.       return -2;
  81.     }
  82.   }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment