Esempio n. 1
0
        /// <summary>
        /// Initialise the column instance.
        /// </summary>
        /// <param name="aggFunction">The aggregation function.</param>
        /// <param name="varName">The name of the variable to get from APSIM.</param>
        /// <param name="on">The collection event.</param>
        /// <param name="alias">The alias.</param>
        /// <param name="from">The from variable.</param>
        /// <param name="to">The to variable.</param>
        private void Initialise(string aggFunction, string varName, string on, string alias,
                                string from, string to)
        {
            aggregationFunction = aggFunction;
            variableName        = varName;
            fromString          = from;
            toString            = to;
            Name = alias;

            // specify a column heading if alias was not specified.
            if (string.IsNullOrEmpty(Name))
            {
                // Look for an array specification. The aim is to encode the starting
                // index of the array into the column name. e.g.
                // for a variableName of [2:4], columnName = [2]
                // for a variableName of [3:], columnName = [3]
                // for a variableName of [:5], columnNamne = [0]

                Regex regex = new Regex("\\[([0-9]+):*[0-9]*\\]");

                Name = regex.Replace(variableName.Replace("[:", "[1:"), "($1)");

                // strip off square brackets.
                Name = Name.Replace("[", string.Empty).Replace("]", string.Empty);
            }

            // Try and get units.
            try
            {
                IVariable var = locator.GetObjectProperties(variableName);
                if (var != null)
                {
                    Units = var.UnitsLabel;
                    if (Units != null && Units.StartsWith("(") && Units.EndsWith(")"))
                    {
                        Units = Units.Substring(1, Units.Length - 2);
                    }
                }
            }
            catch (Exception)
            {
            }

            if (string.IsNullOrEmpty(fromString))
            {
                inCaptureWindow = true;
            }
            else
            {
                // temporarly aggregated variable
                // subscribe to the capture event
                collectionEventName = "[Clock].DoReportCalculations";
                if (!string.IsNullOrEmpty(on))
                {
                    collectionEventName = on;
                }
                events.Subscribe(collectionEventName, OnDoReportCalculations);

                // subscribe to the start of day event so that we can determine if we're in the capture window.
                events.Subscribe("[Clock].DoDailyInitialisation", OnStartOfDay);
                events.Subscribe("[Clock].EndOfSimulation", OnEndOfSimulation);
                fromVariable = (clock as IModel).FindByPath(fromString);
                toVariable   = (clock as IModel).FindByPath(toString);
                if (fromVariable != null)
                {
                    // A from variable name  was specified.
                }
                else if (DateTime.TryParseExact(fromString, "d-MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date) ||
                         DateTime.TryParse(fromString, out date))
                {
                    // The from date is a static, hardcoded date string. ie 1-Jan, 1/1/2012, etc.
                    fromVariable = new VariableObject(date);

                    // If the date string does not contain a year (ie 1-Jan), we ignore year and
                    fromHasNoYear = !fromString.Contains(date.Year.ToString());
                }
                else
                {
                    // Assume the string is an event name.
                    events.Subscribe(fromString, OnFromEvent);
                    inCaptureWindow = true;
                }

                if (toVariable != null)
                {
                    // A to variable name  was specified.
                }
                else if (DateTime.TryParseExact(toString, "d-MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date) ||
                         DateTime.TryParse(toString, out date))
                {
                    // The from date is a static, hardcoded date string. ie 1-Jan, 1/1/2012, etc.
                    toVariable = new VariableObject(date);

                    // If the date string does not contain a year (ie 1-Jan), we ignore year and
                    toHasNoYear = !toString.Contains(date.Year.ToString());
                }
                else
                {
                    // Assume the string is an event name.
                    events.Subscribe(toString, OnToEvent);
                }
            }
        }