Exemplo n.º 1
0
        /// <summary>
        /// Setup the profile grid based on the properties in the model.
        /// The column index of the cell that has changed.
        /// </summary>
        /// <returns>The filled data table. Never returns null.</returns>
        private DataTable CreateTable()
        {
            DataTable table = new DataTable();

            foreach (VariableProperty property in this.propertiesInGrid)
            {
                string columnName = property.Description;
                if (property.UnitsLabel != null)
                {
                    columnName += "\r\n" + property.UnitsLabel;
                }

                Array values = property.Value as Array;

                if (table.Columns.IndexOf(columnName) == -1)
                {
                    table.Columns.Add(columnName, property.DataType.GetElementType());
                }
                else
                {
                }

                DataTableUtilities.AddColumnOfObjects(table, columnName, values);
            }

            return(table);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Setup the profile grid based on the properties in the model.
        /// The column index of the cell that has changed.
        /// </summary>
        /// <returns>The filled data table. Never returns null.</returns>
        private DataTable CreateTable()
        {
            DataTable table = new DataTable();

            foreach (VariableProperty property in this.propertiesInGrid)
            {
                string columnName = property.Description;
                if (property.Units != null)
                {
                    columnName += "\r\n(" + property.Units + ")";
                }

                // add a total to the column header if necessary.
                double total = property.Total;
                if (!double.IsNaN(total))
                {
                    columnName = columnName + "\r\n" + total.ToString("N1");
                }

                Array values = property.Value as Array;

                if (table.Columns.IndexOf(columnName) == -1)
                {
                    table.Columns.Add(columnName, property.DataType.GetElementType());
                }
                else
                {
                }

                DataTableUtilities.AddColumnOfObjects(table, columnName, values);
            }

            return(table);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Setup the profile grid based on the properties in the model.
        /// The column index of the cell that has changed.
        /// </summary>
        /// <returns>The filled data table. Never returns null.</returns>
        private DataTable CreateTable()
        {
            DataTable table = new DataTable();

            foreach (VariableProperty property in this.propertiesInGrid)
            {
                string columnName    = property.Description;
                string columnCaption = property.Caption;
                if (property.UnitsLabel != null)
                {
                    columnName    += "\r\n" + property.UnitsLabel;
                    columnCaption += "\r\n" + property.UnitsLabel;
                }

                // add a total to the column header if necessary.
                double total;
                try
                {
                    total = property.Total;
                }
                catch (Exception err)
                {
                    total = double.NaN;
                    explorerPresenter.MainPresenter.ShowError(err);
                }
                if (!double.IsNaN(total))
                {
                    columnName    = columnName + "\r\n" + total.ToString("N1");
                    columnCaption = columnCaption + "\r\n" + total.ToString("N1");
                }

                Array values = null;
                try
                {
                    values = property.Value as Array;
                }
                catch (Exception err)
                {
                    explorerPresenter.MainPresenter.ShowError(err);
                }

                if (table.Columns.IndexOf(columnName) == -1)
                {
                    DataColumn newCol = table.Columns.Add(columnName, property.DataType.GetElementType());
                    newCol.Caption = columnCaption;
                }
                else
                {
                    // empty
                }

                DataTableUtilities.AddColumnOfObjects(table, columnName, values);
            }

            return(table);
        }
Exemplo n.º 4
0
        /// <summary>Add a date column as the first column of a datatable.</summary>
        /// <param name="data">The data table to add the date to.</param>
        private static void AddDateColumn(DataTable data)
        {
            List <DateTime> dates = new List <DateTime>();

            foreach (DataRow Row in data.Rows)
            {
                dates.Add(DataTableUtilities.GetDateFromRow(Row));
            }
            DataTableUtilities.AddColumnOfObjects(data, "Date", dates.ToArray());
            data.Columns["Date"].SetOrdinal(0);
        }
Exemplo n.º 5
0
        /// <summary>Sets the year in date column.</summary>
        /// <remarks>
        ///     The patch data can go over a year i.e. starts in 2014 and goes into 2015.
        ///     This method doesn't want to set all years to the one specified, rather
        ///     it needs to work out what value needs to be subtracted from all years
        ///     such that the first year of patch data is the same as the year specified.
        /// </remarks>
        /// <param name="patchData">The patch data.</param>
        /// <param name="year">The year to set the date to.</param>
        private static void SetYearInDateColumn(DataTable patchData, int year)
        {
            int firstYear = DataTableUtilities.GetDateFromRow(patchData.Rows[0]).Year;
            int offset    = year - firstYear;

            DateTime[] dates = DataTableUtilities.GetColumnAsDates(patchData, "Date");
            for (int i = 0; i < dates.Length; i++)
            {
                if (DateTime.IsLeapYear(dates[i].Year) && !DateTime.IsLeapYear(dates[i].Year + offset) &&
                    dates[i].Month == 2 && dates[i].Day == 29)
                {
                    dates[i] = new DateTime(dates[i].Year + offset, dates[i].Month, 28);
                }
                else
                {
                    dates[i] = new DateTime(dates[i].Year + offset, dates[i].Month, dates[i].Day);
                }
            }
            DataTableUtilities.AddColumnOfObjects(patchData, "Date", dates);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Add year, month, day and date columns to the specified Table.
        /// </summary>
        public static void AddDateToTable(DataTable table)
        {
            if (!table.Columns.Contains("Date"))
            {
                List <DateTime> dates = new List <DateTime>();
                foreach (DataRow Row in table.Rows)
                {
                    dates.Add(DataTableUtilities.GetDateFromRow(Row));
                }
                DataTableUtilities.AddColumnOfObjects(table, "Date", dates.ToArray());
                table.Columns["Date"].SetOrdinal(0);

                // remove year, day, pan, vp, code columns.
                int yearColumn = table.Columns.IndexOf("Year");
                if (yearColumn != -1)
                {
                    table.Columns.RemoveAt(yearColumn);
                }
                int dayColumn = table.Columns.IndexOf("Day");
                if (dayColumn != -1)
                {
                    table.Columns.RemoveAt(dayColumn);
                }
                int panColumn = table.Columns.IndexOf("pan");
                if (panColumn != -1)
                {
                    table.Columns.RemoveAt(panColumn);
                }
                int vpColumn = table.Columns.IndexOf("vp");
                if (vpColumn != -1)
                {
                    table.Columns.RemoveAt(vpColumn);
                }
                int codeColumn = table.Columns.IndexOf("code");
                if (codeColumn != -1)
                {
                    table.Columns.RemoveAt(codeColumn);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>Create a datatable based on the properties.</summary>
        /// <returns>The filled data table. Never returns null.</returns>
        private DataTable CreateTable()
        {
            DataTable table = new DataTable();

            foreach (var column in this.columnMetadata)
            {
                string columnName = column.ColumnName;

                // add a total to the column name if necessary.
                if (column.AddTotalToColumnName)
                {
                    columnName = GetColumnNameWithTotal(column.Values, columnName);
                }

                // Get values
                DataTableUtilities.AddColumnOfObjects(table, columnName, column.Values as IEnumerable);
            }
            // Add in a dummy column so that the far right column with data isn't really wide.
            table.Columns.Add(" ");

            return(table);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Give values from the source data for all variable names in the expression.
        /// </summary>
        /// <param name="expression">The expression evaluator.</param>
        /// <param name="sourceData">The source data.</param>
        private void FillVariableNamesInExpression(ExpressionEvaluator expression, DataTable sourceData)
        {
            var simulationData = new List <Data>();

            foreach (var variable in expression.Variables)
            {
                if (simulationData.Find(data => data.Name == variable.m_name) == null)
                {
                    simulationData.Add(new Data(sourceData, variable.m_name));
                }
            }

            // Need to get a list of common values for the key across the datasets.
            IEnumerable <object> keyValues = null;

            foreach (var dataset in simulationData)
            {
                if (keyValues == null)
                {
                    keyValues = dataset.GetValues(FieldNameToMatchOn);
                }
                else
                {
                    keyValues = keyValues.Intersect(dataset.GetValues(FieldNameToMatchOn));
                }
            }

            // Create a new data set with the key field.
            var newTable = new DataTable(Name);

            DataTableUtilities.AddColumnOfObjects(newTable, FieldNameToMatchOn, keyValues.ToArray());

            // Add all the double fields into the table.
            foreach (DataColumn column in sourceData.Columns)
            {
                if (column.DataType == typeof(double) && column.ColumnName != FieldNameToMatchOn)
                {
                    newTable.Columns.Add(column.ColumnName, typeof(double));
                }
            }

            // Get each dataset to return
            foreach (DataColumn column in sourceData.Columns)
            {
                if (column.DataType == typeof(double) && column.ColumnName != FieldNameToMatchOn)
                {
                    var expressionVariables = new List <Symbol>();
                    foreach (var variable in expression.Variables)
                    {
                        var dataset = simulationData.Find(data => data.Name == variable.m_name);
                        var symbol  = new Symbol();
                        symbol.m_name   = variable.m_name;
                        symbol.m_values = dataset.GetDoubleValues(column.ColumnName);
                        if (symbol.m_values.Length != keyValues.Count())
                        {
                            throw new Exception($"Incorrect number of values for simulation {dataset.Name}");
                        }
                        expressionVariables.Add(symbol);
                    }
                    expression.Variables = expressionVariables;

                    expression.EvaluatePostfix();
                    if (expression.Error)
                    {
                        throw new Exception($"Error while evaluating expression for column {column.ColumnName}. {expression.ErrorDescription}");
                    }

                    // Add data to new data table.
                    DataTableUtilities.AddColumn(newTable, column.ColumnName, expression.Results);
                }
            }

            // Give the new data table to the data store.
            dataStore.Writer.WriteTable(newTable);
        }