public static List <Round> FindRoundsInSamples(BenchVueTrace trace)
        {
            var   rounds    = new List <Round>();
            Round currRound = null;

            for (int i = 0; i < trace.Samples.Count; i++)
            {
                var currSample = trace.Samples[i];

                /*
                 * isciphersample &&
                 * nextsamplesarecipher || lastsampleswerecipher
                 *
                 *
                 * isIdle && (
                 *  nextsamplesarecipher && lastsampleswerecipher
                 *
                 *
                 */

                var nextSamplesAreCipher  = NextSamplesAreCipher(trace.Samples, i);
                var lastSamplesWereCipher = LastSamplesWereCipher(trace.Samples, i);
                var isCipher = IsCipherSample(currSample);
                var isIdle   = IsIdleSample(currSample);

                if (isCipher &&
                    (
                        (currRound == null && nextSamplesAreCipher && lastSamplesWereCipher) ||
                        (currRound != null && (nextSamplesAreCipher || lastSamplesWereCipher))
                    )
                    )
                {
                    if (currRound == null)
                    {
                        currRound = new Round();
                        rounds.Add(currRound);
                    }

                    currRound.Samples.Add(currSample);
                }
                else if (isIdle && (
                             nextSamplesAreCipher && lastSamplesWereCipher)
                         )
                {
                    currRound?.Samples.Add(currSample);
                }
                else
                {
                    currRound = null;
                }
            }

            rounds = rounds
                     .Where(x => x.Samples.Count > 10)
                     .ToList();

            rounds
            .ForEach(x =>
            {
                for (int i = 0; i < x.Samples.Count; i++)
                {
                    if (i >= x.Samples.Count)
                    {
                        break;
                    }

                    if (IsIdleSample(x.Samples[i]))
                    {
                        x.Samples.Remove(x.Samples[i]);
                    }
                    else
                    {
                        break;
                    }
                }

                for (int i = x.Samples.Count - 1; i >= 0; i--)
                {
                    if (i >= x.Samples.Count)
                    {
                        break;
                    }

                    if (IsIdleSample(x.Samples[i]))
                    {
                        x.Samples.Remove(x.Samples[i]);
                    }
                    else
                    {
                        break;
                    }
                }

                var byTimestamp = x.Samples.OrderBy(s => s.Timestamp).ToArray();
                x.Start         = byTimestamp.First().Timestamp;
                x.End           = byTimestamp.Last().Timestamp;

                x = CalculateResults(x.Samples.Select(s => s.Value).ToList(), x);
            });

            return(rounds);
        }
        private static void WriteToExcel(BenchVueTrace trace)
        {
            var newFile = Path.Combine(Path.GetDirectoryName(trace.File), CalcPrefix + Path.GetFileName(trace.File));

            using (var writer = new StreamWriter(newFile))
                using (var csv = new CsvWriter(writer, new Configuration()
                {
                    Delimiter = ";"
                }))
                {
                    csv.WriteField("Source File");
                    csv.WriteField(trace.File);
                    csv.NextRecord();

                    csv.WriteField("Start");
                    csv.WriteField(trace.Start);

                    csv.WriteField("End");
                    csv.WriteField(trace.End);
                    csv.NextRecord();

                    csv.WriteField("Min");
                    csv.WriteField(trace.Min.ToString(CultureInfo));

                    csv.WriteField("Max");
                    csv.WriteField(trace.Max.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Count");
                    csv.WriteField(trace.Count);
                    csv.NextRecord();

                    csv.WriteField("Average");
                    csv.WriteField(trace.Mittelwert.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Standardabweichung");
                    csv.WriteField(trace.Standardabweichung.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Average Watt");
                    csv.WriteField(trace.MittelwertWatt.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Standardabweichung Watt");
                    csv.WriteField(trace.StandardabweichungWattbefore.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Hours");
                    csv.WriteField(trace.Hours.ToString(CultureInfo));
                    csv.WriteField(TimeSpan.FromHours((double)trace.Hours).Hours);
                    csv.WriteField(TimeSpan.FromHours((double)trace.Hours).Minutes);
                    csv.WriteField(TimeSpan.FromHours((double)trace.Hours).Seconds);
                    csv.NextRecord();

                    csv.WriteField("Watt Hours");
                    csv.WriteField(trace.Watthours.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Within Sigma 1 in %");
                    csv.WriteField(trace.PercentageOfValuesWithin1Sigma.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Within Sigma 2 in %");
                    csv.WriteField(trace.PercentageOfValuesWithin2Sigma.ToString(CultureInfo));
                    csv.NextRecord();

                    csv.WriteField("Within Sigma 3 in %");
                    csv.WriteField(trace.PercentageOfValuesWithin3Sigma.ToString(CultureInfo));
                    csv.NextRecord();

                    if (trace.Rounds.Any())
                    {
                        csv.WriteField("Standardabweichung Watt min");
                        csv.WriteField(trace.Rounds.Select(x => x.StandardabweichungWattbefore).Min()
                                       .ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Standardabweichung Watt max");
                        csv.WriteField(trace.Rounds.Select(x => x.StandardabweichungWattbefore).Max()
                                       .ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Watt hours per round min");
                        csv.WriteField(trace.Rounds.Select(x => x.Watthours).Min().ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Watt hours per round max");
                        csv.WriteField(trace.Rounds.Select(x => x.Watthours).Max().ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Average time per round (based on 100)");
                        csv.WriteField(trace.Rounds.Average(x => x.Hours / 100 * 60 * 60).ToString(CultureInfo));
                        csv.NextRecord();
                    }

                    for (int i = 0; i < trace.Rounds.Count; i++)
                    {
                        var round = trace.Rounds[i];
                        csv.NextRecord();

                        csv.WriteField("Round");
                        csv.WriteField(i);
                        csv.NextRecord();

                        csv.WriteField("Start");
                        csv.WriteField(round.Start);

                        csv.WriteField("End");
                        csv.WriteField(round.End);
                        csv.NextRecord();

                        csv.WriteField("Min");
                        csv.WriteField(round.Min.ToString(CultureInfo));

                        csv.WriteField("Max");
                        csv.WriteField(round.Max.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Count");
                        csv.WriteField(round.Count);
                        csv.NextRecord();

                        csv.WriteField("Average");
                        csv.WriteField(round.Mittelwert.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Standardabweichung");
                        csv.WriteField(round.Standardabweichung.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Average Watt");
                        csv.WriteField(round.MittelwertWatt.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Standardabweichung Watt");
                        csv.WriteField(round.StandardabweichungWattbefore.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Hours");
                        csv.WriteField(round.Hours.ToString(CultureInfo));
                        csv.WriteField(TimeSpan.FromHours((double)round.Hours).Hours);
                        csv.WriteField(TimeSpan.FromHours((double)round.Hours).Minutes);
                        csv.WriteField(TimeSpan.FromHours((double)round.Hours).Seconds);
                        csv.NextRecord();

                        csv.WriteField("Watt Hours");
                        csv.WriteField(round.Watthours.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Within Sigma 1 in %");
                        csv.WriteField(round.PercentageOfValuesWithin1Sigma.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Within Sigma 2 in %");
                        csv.WriteField(round.PercentageOfValuesWithin2Sigma.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.WriteField("Within Sigma 3 in %");
                        csv.WriteField(round.PercentageOfValuesWithin3Sigma.ToString(CultureInfo));
                        csv.NextRecord();

                        csv.NextRecord();
                    }

                    csv.Flush();
                }
        }
        public static BenchVueTrace ConvertFileToTrace(string file, bool isIdle = false)
        {
            CultureInfo enCulture;
            var         trace = new BenchVueTrace();

            trace.File = file;
            using (var reader = new StreamReader(file))
                using (var csv = new CsvReader(reader, new Configuration()
                {
                    Delimiter = ";"
                }))
                {
                    for (int i = 0; i < 4; i++)
                    {
                        csv.Read();
                    }

                    csv.Read();
                    trace.Start = csv.GetField <DateTime>(1);
                    try
                    {
                        csv.Read();
                        trace.End = csv.GetField <DateTime>(1);
                    }
                    catch (Exception)
                    {
                        trace.End = DateTime.MaxValue;
                    }

                    while (!csv.GetField <string>(0).StartsWith("Time"))
                    {
                        csv.Read();
                    }

                    while (csv.Read())
                    {
                        var     value = csv.GetField <string>(1);
                        decimal dValue;
                        if (value.Contains(","))
                        {
                            dValue = (decimal)double.Parse(value);
                        }
                        else
                        {
                            dValue = (decimal)double.Parse(value, CultureInfo);
                        }

                        trace.Samples.Add(new Sample()
                        {
                            Timestamp = csv.GetField <DateTime>(0),
                            Value     = dValue
                        });
                    }
                }

            trace.Samples = trace.Samples.OrderBy(x => x.Timestamp).ToList();

            if (isIdle)
            {
                var samples = trace.Samples.Select(x => x.Value).ToList();

                trace = (BenchVueTrace)CalculateResults(samples, trace);
            }
            else
            {
                trace.Rounds = FindRoundsInSamples(trace);

                var samples = trace.Rounds.Select(x => x.Mittelwert).ToList();
                trace = (BenchVueTrace)CalculateResults(samples, trace);
            }

            return(trace);
        }