Exemplo n.º 1
0
        public static Series SumSetMissingToZero(Series[] items)
        {
            var rval = new Series();

            if (items.Length > 0)
            {
                rval = items[0].Copy();
            }

            SeriesList list = new SeriesList();

            list.AddRange(items);

            rval = Math.FillMissingWithZero(rval, list.MinDateTime, list.MaxDateTime);
            for (int i = 1; i < items.Length; i++)
            {
                rval = rval + Math.FillMissingWithZero(items[i], list.MinDateTime, list.MaxDateTime);
            }

            return(rval);
        }
Exemplo n.º 2
0
        public virtual void Calculate(DateTime t1, DateTime t2)
        {
            var t1a = t1;
            var t2a = t2;

            Logger.OnLogEvent += Logger_OnLogEvent;

            var seriesBeforeCalc = this.Clone();

            //if( this.TimeInterval == TimeSeries.TimeInterval.Irregular)
            t2a = t2.AddDays(1);    // we may need midnight value in the next day.

            if (this.TimeInterval == TimeSeries.TimeInterval.Daily)
            {
                // daily_wrdo_pu needs AdjustStartingDate
                t1a = this.AdjustStartingDateFromProperties(t1, t2a);   // DO DO??? needed??
            }

            Exception error = new Exception();

            if (Expression != null && Expression.Trim() != "")
            {
                string tmpExpression = Expression;
                Logger.WriteLine("begin Calculate()");
                Expression = ExpressionPreProcessor();
                ParserResult result = null;
                try
                {
                    result = Parser.Evaluate(this.Expression, t1a, t2a, this.TimeInterval);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Logger.WriteLine(e.Message, "ui");
                    Messages.Add(e.Message);
                    error  = e;
                    result = null;
                }

                if (result != null && (result.IsDouble || result.IsInteger))
                { // constant expression, need to convert to a Series.
                    Series constant = new Series();
                    constant.TimeInterval = this.TimeInterval;
                    constant = Math.FillMissingWithZero(constant, t1, t2);
                    if (result.IsDouble)
                    {
                        constant = Math.Add(constant, result.Double);
                    }
                    else
                    {
                        constant = Math.Add(constant, result.Integer);
                    }
                    result = new ParserResult(constant);
                }

                if (result != null && result.IsSeries)
                {
                    result.Series.Trim(t1, t2);
                    //var s = result.Series;
                    //result.Series = Math.Subset(s, t1, t2); // trim extra data used for calculations

                    Logger.WriteLine("Calculation result has " + result.Series.Count + " rows");
                    this.TimeInterval = result.Series.TimeInterval;
                    string tableName = this.Table.TableName;

                    this.Table           = result.Series.Table;
                    this.Table.TableName = tableName;
                    if (m_db != null)
                    {
                        // Logger.WriteLine("Setting Flags");
                        m_db.Quality.SetFlags(this);
                    }

                    this.Table.AcceptChanges();// prevents error releated to Deleted rows from Trim() above.
                    foreach (DataRow row in this.Table.Rows)
                    {
                        row.SetAdded(); // so database will insert these rows.
                    }
                    if (m_db != null)   // database is not required for calculations.
                    {
                        //bool canSave = m_db.Server.HasSavePrivilge(this.Table.TableName);
                        //  if(canSave)
                        m_db.SaveTimeSeriesTable(this.ID, this, DatabaseSaveOptions.UpdateExisting);
                        Expression = tmpExpression;

                        if (seriesBeforeCalc.TimeInterval != this.TimeInterval ||
                            seriesBeforeCalc.Units != this.Units)
                        {
                            Logger.WriteLine("Warning Units or interval has changed.");
                            //if(canSave)
                            m_db.SaveProperties(this); // time interval, units, are dependent on calculation.
                        }
                    }
                }
                else
                {
                    Clear();
                    Console.WriteLine(error.Message);
                    Logger.WriteLine(error.Message);
                }

                Logger.WriteLine("Calculate() completed");
                Logger.OnLogEvent -= Logger_OnLogEvent;
            }
        }