Beispiel #1
0
        private TimeSeries[] ParseCSV()
        {
            if (DetailsAsCSV == null)
            {
                return(new TimeSeries[0]);
            }

            string[] unitsStrings = (UnitsForNewTS ?? "").Split(',');
            Unit[]   columnUnits  = new Unit[0];
            if (unitsStrings.Length > 0)
            {
                columnUnits = unitsStrings.Select(s => Unit.parse(s)).ToArray();
            }
            string[] lines  = DetailsAsCSV.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
            string   header = lines[0];

            lines = lines.Skip(1).ToArray();

            string[]   columnNames = SplitCsvLine(header).Skip(1).ToArray();
            string[][] elements    = lines.Select(SplitCsvLine).ToArray();
            string[]   dates       = elements.Select(l => l[0]).ToArray();
            elements = elements.Select(l => l.Skip(1).ToArray()).ToArray();
            DateTime startT = DateTime.Parse(dates.First(), CultureInfo.InvariantCulture);
            //DateTime endT = DateTime.Parse(dates.Last(),CultureInfo.InvariantCulture);
            var ts = TimeStep.Daily;

            if (dates.Length > 1)
            {
                DateTime secondT = DateTime.Parse(dates[1], CultureInfo.InvariantCulture);
                var      delta   = secondT - startT;
                ts = TimeStep.FromSeconds(delta.TotalSeconds);
            }

            return(columnNames.Indices().Select(i =>
            {
                var name = columnNames[i];
                var values = elements.Select(line =>
                {
                    double d;
                    if (!double.TryParse(line[i], out d))
                    {
                        d = double.NaN;
                    }
                    return d;
                }).ToArray();
                var result = new TimeSeries(startT, ts, values);
                result.name = name;
                if (columnUnits.Length > 0)
                {
                    result.units = columnUnits[i % columnUnits.Length];
                }
                return result;
            }).ToArray());
        }