alesi2000

ASAP2MDFReaderExample

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