示例#1
0
 public static void SaveAs(this TimeStampSeries ts, string Filename)
 {
     using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Filename))
     {
         foreach (var v in ts.Items)
         {
             sw.WriteLine(v.Time.ToShortDateString() + "; " + v.Value);
         }
     }
 }
示例#2
0
 public static void LimitTimeSeries(TimeStampSeries Data, DateTime Start, DateTime End)
 {
     for (int i = Data.Items.Count - 1; i >= 0; i--)
     {
         if (Data.Items[i].Time <Start || Data.Items[i].Time> End)
         {
             Data.Items.RemoveAt(i);
         }
     }
 }
示例#3
0
        /// <summary>
        /// First aligns the series and then combines
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="Combiner"></param>
        /// <returns></returns>
        public static TimeStampSeries CombineSeries(TimeStampSeries t1, TimeStampSeries t2, Func <double?, double?, double> Combiner)
        {
            TimeStampValue[] ta1;
            TimeStampValue[] ta2;
            AlignSeries(t1, t2, out ta1, out ta2);


            List <TimeStampValue> newvalues = new List <TimeStampValue>();

            for (int i = 0; i < ta1.Count(); i++)
            {
                newvalues.Add(new TimeStampValue(ta1[i].Time, Combiner(ta1[i].Value, ta2[i].Value)));
            }
            return(new TimeStampSeries(newvalues));
        }
示例#4
0
        /// <summary>
        /// Creates a timespanseries from an accumulated series
        /// </summary>
        /// <param name="data"></param>
        /// <param name="LastUpdate"></param>
        /// <returns></returns>
        public static TimeSpanSeries GetTimeSpanDataFromAccumulated(TimeStampSeries data)
        {
            if (data.Count < 2)
            {
                return(null);
            }
            List <TimeSpanValue> timespans = new List <TimeSpanValue>();

            for (int i = 1; i < data.Count; i++)
            {
                double Currentvalue = Math.Round(data.Items[i].Value - data.Items[i - 1].Value, 3);
                timespans.Add(new TimeSpanValue(data.Items[i - 1].Time, data.Items[i].Time, Currentvalue));
            }
            return(new TimeSpanSeries(timespans));
        }
示例#5
0
        public TimeSpanSeries(TimeStampSeries ts, TimeSpan TimeStep)
            : this()
        {
            this.DeleteValue = ts.DeleteValue;
            TimeStepSize     = ts.TimeStepSize;
            List <TimeSpanValue> templist = new List <TimeSpanValue>();

            for (int i = 0; i < ts.Count; i++)
            {
                templist.Add(new TimeSpanValue(ts.Items[i].Time.Subtract(TimeStep), ts.Items[i].Time, ts.Items[i].Value));
            }
            AddRange(templist);
            if (ts.Count != 0)
            {
                TimeStepSize = TSTools.GetTimeStep(StartTime, StartTime.Add(TimeStep));
            }
        }
示例#6
0
        /// <summary>
        /// Checks if there are any holes in the data series. Returns the active periods.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="Interval"></param>
        /// <returns></returns>
        public static TimeSpanSeries GetActivePeriods(TimeStampSeries data, TimeSpan Interval)
        {
            TimeSpanSeries toreturn = new TimeSpanSeries();

            if (data.Count > 0)
            {
                TimeSpanValue currentvalue = new TimeSpanValue(data.StartTime, data.StartTime, 1);
                for (int i = 0; i < data.Count; i++)
                {
                    if (data.Items[i].Time <= currentvalue.EndTime.Add(Interval))
                    {
                        currentvalue.EndTime = data.Items[i].Time;
                    }
                    else
                    {
                        toreturn.Items.Add(currentvalue);
                        currentvalue = new TimeSpanValue(data.Items[i].Time, data.Items[i].Time, 1);
                    }
                }
                toreturn.Items.Add(currentvalue);
            }
            return(toreturn);
        }
示例#7
0
        /// <summary>
        /// Aligns two series be returning only the common time values
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <param name="tout1"></param>
        /// <param name="tout2"></param>
        public static void AlignSeries(TimeStampSeries t1, TimeStampSeries t2, out TimeStampValue[] tout1, out TimeStampValue[] tout2)
        {
            int t1count = 0;
            int t2count = 0;

            List <TimeStampValue> newT1values = new List <TimeStampValue>();
            List <TimeStampValue> newT2values = new List <TimeStampValue>();

            if (t1.Count != 0 & t2.Count != 0)
            {
                while (t1count < t1.Count && t1.Items[t1count].Time < t2.StartTime)
                {
                    t1count++;
                }

                while (t2count < t2.Count && t2.Items[t2count].Time < t1.StartTime)
                {
                    t2count++;
                }

                for (int i = t1count; i < t1.Count; i++)
                {
                    while (t2count < t2.Count - 1 & t2.Items[t2count].Time < t1.Items[i].Time)
                    {
                        t2count++;
                    }

                    if (t1.Items[i].Time == t2.Items[t2count].Time)
                    {
                        newT1values.Add(t1.Items[i]);
                        newT2values.Add(t2.Items[t2count]);
                    }
                }
            }
            tout1 = newT1values.ToArray();
            tout2 = newT2values.ToArray();
        }
示例#8
0
        /// <summary>
        /// Creates a timespanseries from an accumulated series
        /// </summary>
        /// <param name="data"></param>
        /// <param name="LastUpdate"></param>
        /// <returns></returns>
        public static TimeSpanSeries GetTimeSpanDataFromAccumulated(TimeStampSeries data, TimeSpan MaxInterval)
        {
            if (data.Count < 2)
            {
                return(null);
            }
            List <TimeSpanValue> timespans = new List <TimeSpanValue>();

            for (int i = 1; i < data.Count; i++)
            {
                double   Currentvalue = data.Items[i].Value - data.Items[i - 1].Value;
                DateTime start;
                if (data.Items[i - 1].Time < data.Items[i].Time.Subtract(MaxInterval))
                {
                    start = data.Items[i].Time.Subtract(MaxInterval);
                }
                else
                {
                    start = data.Items[i - 1].Time;
                }
                timespans.Add(new TimeSpanValue(start, data.Items[i].Time, Currentvalue));
            }
            return(new TimeSpanSeries(timespans));
        }
示例#9
0
        public TimeSpanSeries(TimeStampSeries ts) : this()
        {
            if (ts != null)
            {
                this.DeleteValue = ts.DeleteValue;
                TimeStepSize     = ts.TimeStepSize;

                if (ts.Count > 1)
                {
                    List <TimeSpanValue> templist = new List <TimeSpanValue>();
                    templist.Add(new TimeSpanValue(ts.Items[0].Time.Subtract(ts.Items[1].Time.Subtract(ts.Items[0].Time)), ts.Items[0].Time, ts.Items[0].Value));

                    for (int i = 1; i < ts.Count; i++)
                    {
                        templist.Add(new TimeSpanValue(ts.Items[i - 1].Time, ts.Items[i].Time, ts.Items[i].Value));
                    }
                    AddRange(templist);
                }
                if (Items.Count > 0)
                {
                    TimeStepSize = TSTools.GetTimeStep(Items[0].StartTime, Items[0].EndTime);
                }
            }
        }
示例#10
0
        public static TimeStampSeries ChangeZoomLevel(TimeStampSeries Data, TimeStepUnit NewZoomLevel, bool Accumulate)
        {
            TimeStampSeries ToReturn = new TimeStampSeries();

            ToReturn.DeleteValue  = Data.DeleteValue;
            ToReturn.Name         = Data.Name;
            ToReturn.ID           = Data.ID;
            ToReturn.TimeStepSize = NewZoomLevel;


            DateTime start      = Data.StartTime;
            DateTime end        = Data.EndTime;
            int      localcount = 0;

            switch (NewZoomLevel)
            {
            case TimeStepUnit.Year:
            {
                int currentyear = start.Year;
                ToReturn.Items.Add(new TimeStampValue(new DateTime(start.Year, 1, 1), 0));
                foreach (var v in Data.Items.Where(dv => dv.Value != Data.DeleteValue))
                {
                    if (v.Time.Year == currentyear)
                    {
                        ToReturn.Items.Last().Value += v.Value;
                    }
                    else
                    {
                        currentyear = v.Time.Year;
                        if (!Accumulate)
                        {
                            ToReturn.Items.Last().Value /= localcount;
                        }
                        localcount = 0;
                        ToReturn.Items.Add(new TimeStampValue(new DateTime(v.Time.Year, 1, 1), v.Value));
                    }
                    localcount++;
                }
                if (!Accumulate)
                {
                    ToReturn.Items.Last().Value /= localcount;
                }
            }
            break;

            case TimeStepUnit.Month:
                int currentmonth = start.Month;
                ToReturn.Items.Add(new TimeStampValue(new DateTime(start.Year, start.Month, 1), 0));
                foreach (var v in Data.Items.Where(dv => dv.Value != Data.DeleteValue))
                {
                    if (v.Time.Month == currentmonth)
                    {
                        ToReturn.Items.Last().Value += v.Value;
                    }
                    else
                    {
                        currentmonth = v.Time.Month;
                        if (!Accumulate)
                        {
                            ToReturn.Items.Last().Value /= localcount;
                        }
                        localcount = 0;
                        ToReturn.Items.Add(new TimeStampValue(new DateTime(v.Time.Year, v.Time.Month, 1), v.Value));
                    }
                    localcount++;
                }
                if (!Accumulate)
                {
                    ToReturn.Items.Last().Value /= localcount;
                }

                break;

            case TimeStepUnit.Day:
                break;

            case TimeStepUnit.Hour:
                break;

            case TimeStepUnit.Minute:
                break;

            case TimeStepUnit.Second:
                break;

            case TimeStepUnit.None:
                break;

            default:
                break;
            }


            return(ToReturn);
        }