Example #1
0
        public IFunction CreateFunction(IEnumerable <IVariable> variables)
        {
            IVariable gridComponent = variables.First(f => f.Attributes.ContainsKey("coordinates"));

            IVariable <double>   x    = null;
            IVariable <double>   y    = null;
            IVariable <DateTime> time = null;

            foreach (IVariable argument in gridComponent.Arguments)
            {
                if (!argument.Attributes.ContainsKey(FunctionAttributes.StandardName))
                {
                    continue;
                }

                if (argument.Attributes[FunctionAttributes.StandardName].Equals("projection_x_coordinate"))
                {
                    x = (IVariable <double>)argument;
                }
                if (argument.Attributes[FunctionAttributes.StandardName].Equals("projection_y_coordinate"))
                {
                    y = (IVariable <double>)argument;
                }

                if (argument.Attributes[FunctionAttributes.StandardName].Equals("time"))
                {
                    time = (IVariable <DateTime>)argument;
                }
            }

            var gridName = gridComponent.Name;

            if (gridComponent.Attributes.ContainsKey("function_name"))
            {
                gridName = gridComponent.Attributes[FunctionAttributes.FunctionName];
            }

            // create a new grid coverage and replace components / arguments
            IRegularGridCoverage gridCoverage = new RegularGridCoverage {
                Name = gridName
            };

            if (time != null)
            {
                gridCoverage.Arguments = new EventedList <IVariable>(new IVariable[] { time, y, x });
            }
            else
            {
                gridCoverage.Arguments = new EventedList <IVariable>(new IVariable[] { y, x });
            }
            gridCoverage.Components = new EventedList <IVariable>(new[] { gridComponent });

            return(gridCoverage);
        }
        /// <summary>
        /// Aggregate along given variable.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TAccumulate"></typeparam>
        /// <param name="v"></param>
        /// <param name="startValue"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public IRegularGridCoverage Aggregate <T, TAccumulate>(IVariable v, TAccumulate startValue, Func <TAccumulate, T, TAccumulate> func)
        {
            IRegularGridCoverage resultCoverage = new RegularGridCoverage(SizeX, SizeY, DeltaX, DeltaY);

            resultCoverage.Components.Clear();

            var variable = (IVariable)TypeUtils.CreateGeneric(typeof(Variable <>), typeof(TAccumulate));

            resultCoverage.Components.Add(variable);

            var values = resultCoverage.Components[0].Values;

            //integrate values over time
            int i = 0, j = 0;

            foreach (var x in X.Values)
            {
                foreach (var y in Y.Values)
                {
                    var values1D = (IList <T>)Components[0].GetValues(
                        new VariableValueFilter <double>(X, new[] { x }),
                        new VariableValueFilter <double>(Y, new[] { y }),
                        new VariableReduceFilter(X),
                        new VariableReduceFilter(Y)
                        );

                    TAccumulate value = values1D.Aggregate(startValue, func);

                    values[j, i] = value;
                    j++;
                }
                i++;
                j = 0;
            }

            return(resultCoverage);


            // if (v is DateTime > .....)
        }