public override void Initialize()
        {
            Status = LinkableComponentStatus.Initializing;

            Id = "Rainfall Measurements";

            _adaptedOutputFactories = new List <IAdaptedOutputFactory>();
            _adaptedOutputFactories.Add(new AdaptedOutputFactory("RainRRCFTestAdaptedOutput"));

            // Create measurements
            _timeSet =
                new RainfallMeasurementsTimeSet(_MeasurementCount, _measurementsStartTimeAsMJD, _measurementsEndTimeAsMJD);

            IDictionary <string, IElementSet> elementSets = RainRrCfElementSets.CreateRainfallMeasurementsElementSets();

            const string rainId = "Rain";
            Quantity     rainfallValueDefinition = new Quantity(new Unit("mm/day", 1.15741E-08, 0), rainId,
                                                                "Rainfall in mm/day");
            double startValue = 100;

            foreach (ElementSet elmSet in elementSets.Values)
            {
                Output outputItem = new RainfallOutputItem(elmSet.Caption + "." + rainId, startValue)
                {
                    ValueDefinition   = rainfallValueDefinition,
                    SpatialDefinition = elmSet,
                    TimeSet           = _timeSet,
                    Component         = this,
                };
                _outputItems.Add(outputItem);
                startValue += 300;
            }
            Status = LinkableComponentStatus.Initialized;
        }
Esempio n. 2
0
        override public bool UpdateRequired(ITimeSet request, ITimeSet current)
        {
            if (Values == null || ValueSet.GetElementCount(Values) * ValueSet.GetTimesCount(Values) != 1)
            {
                return(true);
            }

            ValidTimeSet(request);
            ValidTimeSet(current);

            double dRequest = request.Times[0].StampAsModifiedJulianDay;
            double dCurrent = current.Times[0].StampAsModifiedJulianDay;

            if (Math.Abs(dCurrent - dRequest) <= _timeTolerance)
            {
                return(false); // Equal within tolerance
            }
            // TODO Could check cache and just throw if cache inadequate

            if (dRequest < dCurrent)
            {
                throw new InvalidOperationException("Earlier time request");
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Set a single time stamp in the <see cref="ITimeSet"/>
        /// </summary>
        /// <param name="timeSet">Time set to update</param>
        /// <param name="timeStamp">Time to use</param>
        public static void SetSingleTime(this ITimeSet timeSet, ITime timeStamp)
        {
            if (timeStamp.DurationInDays == 0 && timeSet.HasDurations)
            {
                throw new Exception("Can not set single time STAMP in a time set with duration");
            }
            else if (timeStamp.DurationInDays > 0 && !timeSet.HasDurations)
            {
                throw new Exception("Can not set single time SPAN in a time set without duration");
            }


            if (timeSet.Times.Count > 1)
            {
                timeSet.Times.Clear();
            }
            if (timeSet.Times.Count == 0)
            {
                timeSet.Times.Add(timeStamp);
            }
            else
            {
                timeSet.Times[0] = timeStamp;
            }
        }
Esempio n. 4
0
 public SimpleLinkableComponent(string id)
 {
     _id         = id;
     _timeExtent = new SimpleTimeSet(StartTimeAsMJD, EndTimeAsMJD - StartTimeAsMJD);
     _inputs.Add(new SimpleInput("loc.1", "quant.a", this));
     _outputs.Add(new SimpleOutput("loc.2", "quant.b", this));
 }
Esempio n. 5
0
 /// <summary>
 /// Set the time horizon for an <see cref="ITimeSet"/>, assuming it is a
 /// <see cref="TimeSet"/>, if not, an exception is thrown.
 /// <para>
 /// Convenience methods, since the <see cref="ITimeSet.TimeHorizon"/> does
 /// not have any setter.
 /// </para>
 /// </summary>
 /// <param name="timeSet">Time set to update</param>
 /// <param name="timeHorizon">Time horizon to use</param>
 public static void SetTimeHorizon(this ITimeSet timeSet, ITime timeHorizon)
 {
     if (!(timeSet is TimeSet))
     {
         throw new Exception("SetTimeHorizon: TimeSet is not of type Backbone.TimeSet");
     }
     ((TimeSet)timeSet).TimeHorizon = timeHorizon;
 }
Esempio n. 6
0
        /// <summary>
        /// Set the time horizon start time for an <see cref="ITimeSet"/>, assuming it is a
        /// <see cref="TimeSet"/>, if not, an exception is thrown. End time is unchanged,
        /// i.e. the duration will change.
        /// </summary>
        /// <param name="timeSet">Time set to update</param>
        /// <param name="timeHorizonStart">New time horizon start time</param>
        public static void SetTimeHorizonStart(this ITimeSet timeSet, ITime timeHorizonStart)
        {
            double startMjd   = timeHorizonStart.StampAsModifiedJulianDay;
            double endMjd     = timeSet.TimeHorizon.EndStampAsModifiedJulianDay();
            ITime  newHorizon = new Time(startMjd, endMjd - startMjd);

            timeSet.SetTimeHorizon(newHorizon);
        }
Esempio n. 7
0
        // Called from component
        public void Update(ITimeSet timeSet, IList value)
        {
            CachePush(timeSet, value);

            if (ItemChanged != null)
            {
                ItemChanged(this, new ExchangeItemChangeEventArgs {
                    ExchangeItem = this, Message = CurrentState()
                });
            }
        }
Esempio n. 8
0
        public ItemInDoubleBase(string id, ITimeSet componentTimeExtent, double defaultValue, double timeTolerance)
            : base(id)
        {
            TimeSet ts = new TimeSet();

            ts.HasDurations         = false;
            ts.OffsetFromUtcInHours = 0;
            double startTime = componentTimeExtent.TimeHorizon.StampAsModifiedJulianDay;

            ts.TimeHorizon = componentTimeExtent.TimeHorizon;
            ts.SetSingleTimeStamp(startTime);

            _currentTimeSet = ts;
            _defaultValue   = defaultValue;
            _timeTolerance  = timeTolerance;

            DefaultValues();

            ValidTimeSet(_currentTimeSet);
            ValidValue(Values);
        }
Esempio n. 9
0
        public void Update(ITimeSet required)
        {
            if (Provider == null)
            {
                return;
            }

            if (!UpdateRequired(required, _currentTimeSet))
            {
                return;
            }

            _currentTimeSet = required;

            if (ItemChanged != null)
            {
                ItemChanged(this, new ExchangeItemChangeEventArgs {
                    ExchangeItem = this, Message = CurrentState()
                });
            }

            Values.Values2D.Clear();

            if (Component == typeof(LinkableComponent))
            {
                ((LinkableComponent)Component).Status
                    = LinkableComponentStatus.WaitingForData;
            }

            // TODO: Change status for non LinkableComponent? Standard needs this?

            Values = (ITimeSpaceValueSet)Provider.GetValues(this);

            if (ItemChanged != null)
            {
                ItemChanged(this, new ExchangeItemChangeEventArgs {
                    ExchangeItem = this, Message = CurrentState()
                });
            }
        }
Esempio n. 10
0
 override public void ValidTimeSet(ITimeSet iTimeSet)
 {
     if (iTimeSet == null)
     {
         throw new ArgumentException("iTimeSet == null");
     }
     if (iTimeSet.HasDurations)
     {
         throw new ArgumentException("iTimeSet.HasDurations");
     }
     if (iTimeSet.OffsetFromUtcInHours != 0)
     {
         throw new ArgumentException("iTimeSet.OffsetFromUtcInHours != 0");
     }
     if (iTimeSet.Times == null)
     {
         throw new ArgumentException("iTimeSet.Times == null");
     }
     if (iTimeSet.Times.Count != 1)
     {
         throw new ArgumentException("iTimeSet.Times.Count != 1");
     }
 }
Esempio n. 11
0
 override public void ValidTimeSet(ITimeSet iTimeSet)
 {
     // TODO Standard move ValidTimeSet into IExchangeItem?
     if (iTimeSet == null)
     {
         throw new ArgumentException("iTimeSet == null");
     }
     if (iTimeSet.HasDurations)
     {
         throw new ArgumentException("iTimeSet.HasDurations");
     }
     if (iTimeSet.OffsetFromUtcInHours != 0)
     {
         throw new ArgumentException("iTimeSet.OffsetFromUtcInHours != 0");
     }
     if (iTimeSet.Times == null)
     {
         throw new ArgumentException("iTimeSet.Times == null");
     }
     if (iTimeSet.Times.Count != 1)
     {
         throw new ArgumentException("iTimeSet.Times.Count != 1");
     }
 }
Esempio n. 12
0
 /// <summary>
 /// Set a single time stamp in the <see cref="ITimeSet"/>
 /// </summary>
 /// <param name="timeSet">Time set to update</param>
 /// <param name="dateTime">Time to use</param>
 /// <param name="durationInDays">Duration of span</param>
 public static void SetSingleTimeSpan(this ITimeSet timeSet, DateTime dateTime, double durationInDays)
 {
     timeSet.SetSingleTime(new Time(dateTime, durationInDays));
 }
Esempio n. 13
0
 public abstract void ValidTimeSet(ITimeSet iTimeSet);
Esempio n. 14
0
 public abstract void CachePush(ITimeSet iTimeSet, IList value);
Esempio n. 15
0
        public override void CachePush(ITimeSet timeSet, IList values)
        {
            if (Consumers == null || Consumers.Count == 0)
            {
                return;
            }

            ValidTimeSet(timeSet);
            ValidValue(values);

            _cache.Add(new DataPair(timeSet, values));

            // Tidy up cache

            double?timeLowest = null;
            double timeTarget;

            foreach (ITimeSpaceInput item in Consumers)
            {
                ValidTimeSet(item.TimeSet);

                timeTarget = item.TimeSet.Times[0].StampAsModifiedJulianDay;

                if (timeLowest == null)
                {
                    timeLowest = item.TimeSet.Times[0].StampAsModifiedJulianDay;
                }
                else if (timeTarget < timeLowest)
                {
                    timeLowest = timeTarget;
                }
            }

            if (timeLowest == null)
            {
                _cache.Clear();
            }
            else if (_cache.Count > 2)
            {
                // Only need to keep one value below lowest of consumers
                // for one point interpolation and minimum of 2 points
                // total for extrapolation

                int nLowest = 0;

                while (nLowest < _cache.Count &&
                       _cache[nLowest].Time < timeLowest.Value)
                {
                    ++nLowest;
                }

                if (nLowest == _cache.Count - 1)
                {
                    --nLowest; // maintain at least two items
                }
                if (nLowest < _cache.Count && nLowest > 0)
                {
                    _cache.RemoveRange(0, nLowest); // O(n)
                }
            }
        }
Esempio n. 16
0
 public DataPair(ITimeSet timeSet, IList values)
 {
     _time = timeSet.Times[0];
     values.CopyTo(_values, 0);
 }
Esempio n. 17
0
        public static bool OutputAndInputTimeSetsFit(ITimeSpaceExchangeItem sourceItem, ITimeSpaceExchangeItem targetItem)
        {
            if (sourceItem == null)
            {
                throw new ArgumentNullException("sourceItem");
            }

            if (targetItem == null)
            {
                throw new ArgumentNullException("targetItem");
            }

            bool     timeFits      = true;
            ITimeSet sourceTimeSet = sourceItem.TimeSet;
            ITimeSet targetTimeSet = targetItem.TimeSet;

            if (sourceTimeSet == null)
            {
                if (targetTimeSet != null)
                {
                    // NOTE: Source has no timeset specification, source has.
                    // Source fits target if target requires only one time step.
                    timeFits = targetTimeSet.Times.Count == 1;
                }
            }
            else
            {
                if (targetTimeSet == null)
                {
                    // NOTE: Target has no timeset specification, source has.
                    // Source fits target if source has values for only one time step available.
                    timeFits = sourceTimeSet.Times.Count == 1;
                }
                else
                {
                    /*
                     * SH/AM: TODO I Think this code is wrong, IOutput and IAdaptedOutput should be treated the same
                     * (SH: reactivated (if (sourceItem is IAdaptedOutput) code
                     * to make things work for time extrapolators again.
                     */
                    // Both source and target have time set specification
                    if (sourceItem is ITimeSpaceAdaptedOutput)
                    {
                        // NOTE: Source is an adaptedOutput that has a time set.
                        // Most probably a timeinterpolator, but:
                        // TODO: Check how we can find out that it is a time interpolator.
                        // For now: check if the target's last required time is included in the source's time horizon
                        if (sourceTimeSet.TimeHorizon == null)
                        {
                            throw new Exception("Error when checking if the times of AdaptedOutput \"" + sourceItem.Id +
                                                " fits the times required by inputItem \"" + targetItem.Id +
                                                "\": no time horizon available in the adaptedOutput");
                        }
                        ITime  lastRequiredTime      = targetTimeSet.Times[targetTimeSet.Times.Count - 1];
                        double lastRequiredTimeAsMJD = lastRequiredTime.StampAsModifiedJulianDay +
                                                       lastRequiredTime.DurationInDays;
                        double endOfSourceTimeHorizon = sourceTimeSet.TimeHorizon.StampAsModifiedJulianDay +
                                                        sourceTimeSet.TimeHorizon.DurationInDays;
                        timeFits = lastRequiredTimeAsMJD <= (endOfSourceTimeHorizon + Time.EpsilonForTimeCompare);
                    }
                    else
                    {
                        timeFits = false;
                        // regular (output) exchange item, check if all times fit
                        IList <ITime> sourceTimes   = sourceTimeSet.Times;
                        IList <ITime> requiredTimes = targetTimeSet.Times;
                        if (sourceTimes.Count == requiredTimes.Count)
                        {
                            timeFits = true;
                            for (int timeIndex = 0; timeIndex < requiredTimes.Count; timeIndex++)
                            {
                                if ((requiredTimes[timeIndex].DurationInDays > 0 && !(sourceTimes[timeIndex].DurationInDays > 0)) ||
                                    (sourceTimes[timeIndex].DurationInDays > 0 && !(requiredTimes[timeIndex].DurationInDays > 0)))
                                {
                                    throw new Exception("Incompatible times (stamp versus span) between outputItem \"" + sourceItem.Id +
                                                        " and inputItem \"" + targetItem.Id + "\"");
                                }
                                if (requiredTimes[timeIndex].Equals(sourceTimes[timeIndex]))
                                {
                                    continue;
                                }
                                timeFits = false;
                                break;
                            }
                        }
                    }
                }
            }
            return(timeFits);
        }
Esempio n. 18
0
 public TimeSet(ITimeSet iTimeSet)
 {
     _iTimeSet = iTimeSet;
     _horizon  = new Time(_iTimeSet.TimeHorizon);
 }
Esempio n. 19
0
 /// <summary>
 /// Set a single time stamp in the <see cref="ITimeSet"/>
 /// </summary>
 /// <param name="timeSet">Time set to update</param>
 /// <param name="startTime">Start Time as Modified Julian Day</param>
 /// <param name="endTime">End Time as Modified Julian Day</param>
 public static void SetSingleTimeSpan(this ITimeSet timeSet, ITime startTime, ITime endTime)
 {
     timeSet.SetSingleTimeSpan(startTime.StampAsModifiedJulianDay, endTime.StampAsModifiedJulianDay + endTime.DurationInDays);
 }
Esempio n. 20
0
 /// <summary>
 /// Set a single time stamp in the <see cref="ITimeSet"/>
 /// </summary>
 /// <param name="timeSet">Time set to update</param>
 /// <param name="dateTime">Time to use</param>
 public static void SetSingleTimeStamp(this ITimeSet timeSet, DateTime dateTime)
 {
     timeSet.SetSingleTime(new Time(dateTime));
 }
Esempio n. 21
0
 /// <summary>
 /// Set a single time stamp in the <see cref="ITimeSet"/>
 /// </summary>
 /// <param name="timeSet">Time set to update</param>
 /// <param name="timestampAsMJD">Time to use</param>
 public static void SetSingleTimeStamp(this ITimeSet timeSet, double timestampAsMJD)
 {
     timeSet.SetSingleTime(new Time(timestampAsMJD));
 }
Esempio n. 22
0
 public DataPair(ITimeSet timeSet, IList value)
 {
     _time  = timeSet.Times[0];
     _value = (double)value[0];
 }
Esempio n. 23
0
 /// <summary>
 /// Set a single time stamp in the <see cref="ITimeSet"/>
 /// </summary>
 /// <param name="timeSet">Time set to update</param>
 /// <param name="startAsMJD">Start Time as Modified Julian Day</param>
 /// <param name="endAsMJD">End Time as Modified Julian Day</param>
 public static void SetSingleTimeSpan(this ITimeSet timeSet, double startAsMJD, double endAsMJD)
 {
     timeSet.SetSingleTime(new Time(startAsMJD, endAsMJD - startAsMJD));
 }
Esempio n. 24
0
 public abstract bool UpdateRequired(ITimeSet request, ITimeSet current);