/// <summary>
 /// Uses reflection to create an instance of <see cref="BinUniformTimeseriesFile{T}"/>.
 /// </summary>
 public static IBinUniformTimeseriesFile GenericNew(Type itemType, string fileName, TimeSpan itemTimeSpan,
     UtcDateTime firstTimestamp)
 {
     return (IBinUniformTimeseriesFile)
            Activator.CreateInstance(
                typeof (BinUniformTimeseriesFile<>).MakeGenericType(itemType),
                fileName, itemTimeSpan, firstTimestamp);
 }
Ejemplo n.º 2
0
 public TimeSpan Subtract(DateTimeOffset value)
 {
     return(UtcDateTime.Subtract(value.UtcDateTime));
 }
Ejemplo n.º 3
0
 // Returns the hash code for this DateTimeOffset.
 //
 public override int GetHashCode()
 {
     return(UtcDateTime.GetHashCode());
 }
Ejemplo n.º 4
0
 public virtual void MarkAsActioned(Result <string> data, string participant, UtcDateTime timestamp, object evaluationCtx)
 {
     Requests.Add(new RequestResponse <TRespData>(new Request(data, timestamp)));
 }
        private static SortedDictionary<UtcDateTime, double?> ReadPredictionFile(StreamReader reader, ForecastMetaData fmd, bool includeNegObs, bool useFixedHours, string siteId, string prevDateTimePattern,
            out UtcDateTime? firstTimePoint,
            out UtcDateTime? firstPossibleHour,
            out bool isNotPointingToANumber)
        {
            isNotPointingToANumber = false;
            firstTimePoint = null;
            firstPossibleHour = null;
            int lineNr = -1;
            string dateTimePattern;

            var predPowerInFarm = new SortedDictionary<UtcDateTime, double?>();
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (lineNr == -1)
                {
                    if (!line.Contains(fmd.ForecastSep))
                        throw new Exception("Forecast series does not contain the char: '" + fmd.ForecastSep +
                                            "' \ras column seperator. Open a document and check what \rdelimiter char is used to seperate the columns \rand specify this char in the Forecasts Column Seperator field.");
                    lineNr++;
                    continue;
                }
                if (string.IsNullOrEmpty(line)) continue;

                var cols = line.Split(fmd.ForecastSep);

                bool collect = false;
                if (!string.IsNullOrWhiteSpace(siteId))
                {
                    collect = (cols.Length > 2 && cols[1].Equals(siteId, StringComparison.InvariantCultureIgnoreCase));
                    if (!collect && siteId.Equals("N/A", StringComparison.InvariantCultureIgnoreCase)) collect = true;
                }
                else collect = true;

                string timeStampStr = cols[fmd.ForecastTimeIndex];
                string valueStr = cols[fmd.ForecastValueIndex];
                //int timeStepAhead = lineNr + fmd.OffsetHoursAhead;

                // TODO: IF LINE STARTS WITH TEXT, CONTINUE
                if (string.IsNullOrWhiteSpace(timeStampStr) || !char.IsNumber(timeStampStr[0])) continue;

                dateTimePattern = string.Empty;
                UtcDateTime? dateTime = DateTimeUtil.ParseTimeStamp(timeStampStr, prevDateTimePattern, out dateTimePattern);
                if (dateTime.HasValue)
                {
                    if (string.IsNullOrWhiteSpace(prevDateTimePattern) && !string.IsNullOrWhiteSpace(dateTimePattern)) prevDateTimePattern = dateTimePattern;

                    // Hack for calc SWM perf, REMOVE
                    // if (lineNr == 0 && dateTime.Hour== 23) continue;

                    if (lineNr == 0)
                    {
                        firstTimePoint = dateTime;
                        firstPossibleHour = firstTimePoint;
                        if (useFixedHours && dateTime.HasValue)
                        {
                            var firstHour = dateTime.Value.AddHours(1);
                            firstPossibleHour = new UtcDateTime(firstHour.Year, firstHour.Month, firstHour.Day, firstHour.Hour);
                        }
                    }

                    if (collect)
                    {
                        double? value = DoubleUtil.ParseDoubleValue(valueStr);
                        if (value.HasValue)
                        {
                            double val = value.Value;
                            if (fmd.ForecastUnitType == "MW") val *= 1000;
                            if (val < 0 && !includeNegObs)
                            {
                                if(!predPowerInFarm.ContainsKey(dateTime.Value)) predPowerInFarm.Add(dateTime.Value, null);
                            }
                            else
                            {
                                if(!predPowerInFarm.ContainsKey(dateTime.Value)) predPowerInFarm.Add(dateTime.Value, val);
                            }
                        }
                        else
                        {
                            if (lineNr <= 3) isNotPointingToANumber = true;
                            else
                            {
                                if(!predPowerInFarm.ContainsKey(dateTime.Value)) predPowerInFarm.Add(dateTime.Value, null);
                            }
                        }
                    }

                    lineNr++; // We only count lines with valid time stamps
                }
            }

            return predPowerInFarm;
        }
        public static ConcurrentDictionary<UtcDateTime, HourlySkillScoreCalculator> Calculate(ForecastMetaData fmd, string path, ObservationMetaData omd, string file, int[] scope, 
                                                                                              bool includeNegObs = false, bool useFixedHours = false, string siteId=null)
        {
            var startTimesInMinutes = FindUsualForecastStartTimePoints(path, fmd);

            var results = new ConcurrentDictionary<UtcDateTime, HourlySkillScoreCalculator>();
            bool isNotPointingToANumber = false;

            // 1. Load observations
            string[] obsLines = File.ReadAllLines(file);
            var obsPowerInFarm = new SortedDictionary<UtcDateTime, double?>();
            string dateTimePattern = string.Empty;
            string prevDateTimePattern = string.Empty;
            if (!obsLines[0].Contains(omd.ObservationSep)) throw new Exception("Observations series does not contain the char: '" + omd.ObservationSep + "' \ras column seperator. Open the document and check what \rdelimiter char is used to seperate the columns \rand specify this char in the Observations Column Seperator field.");
            for (int i=1; i<obsLines.Length; i++) // Skip the heading...
            {
                var line = obsLines[i];
                var cols = line.Split(omd.ObservationSep);
                if (cols.Length > 1)
                {
                    string timeStampStr = cols[omd.ObservationTimeIndex];
                    string valueStr = cols[omd.ObservationValueIndex];

                    UtcDateTime? dateTime = DateTimeUtil.ParseTimeStamp(timeStampStr, prevDateTimePattern, out dateTimePattern);
                    if (dateTime.HasValue)
                    {
                        if (string.IsNullOrWhiteSpace(prevDateTimePattern) && !string.IsNullOrWhiteSpace(dateTimePattern)) prevDateTimePattern = dateTimePattern;

                        double? value = DoubleUtil.ParseDoubleValue(valueStr);
                        if (value.HasValue)
                        {
                            double val = value.Value;
                            if (omd.ObservationUnitType == "MW") val *= 1000;
                            if (val < 0 && !includeNegObs) obsPowerInFarm.Add(dateTime.Value, null);
                            else obsPowerInFarm.Add(dateTime.Value, val);
                        }
                        else
                        {
                            if (i <= 3) isNotPointingToANumber = true;
                            obsPowerInFarm.Add(dateTime.Value, null);
                        }
                    }
                }
            }

            if (isNotPointingToANumber && obsPowerInFarm.Count == 0) throw new Exception("The Observation Column Index Value is not pointing to a column which contains a double value. Change index value!");

            int obsSteps;
            TimeResolutionType? obsResolution = IdentifyResolution(obsPowerInFarm, out obsSteps);

            // 2. Identify Time Resolution of obs series, if less than an hour, make it hourly avg.
            var firstObs = obsPowerInFarm.First().Key;
            var minutes = startTimesInMinutes.Keys.ToArray();
            foreach (var minute in minutes)
            {
                if (firstObs.Minute == minute)
                {
                    SortedDictionary<UtcDateTime, double?> convertedObsPowerInFarm = ConvertTimeSeriesToHourlyResolution(firstObs, obsPowerInFarm, useFixedHours);
                    startTimesInMinutes[minute] = convertedObsPowerInFarm;
                }
                else if (minute > 0)
                {
                    var first = new UtcDateTime(firstObs.Year, firstObs.Month, firstObs.Day, firstObs.Hour, minute);
                    SortedDictionary<UtcDateTime, double?> convertedObsPowerInFarm = ConvertTimeSeriesToHourlyResolution(first, obsPowerInFarm, useFixedHours, minute);
                    startTimesInMinutes[minute] = convertedObsPowerInFarm;
                }
                else
                {
                    var tmp = firstObs.AddHours(1);
                    firstObs = new UtcDateTime(tmp.Year, tmp.Month, tmp.Day, tmp.Hour);
                    SortedDictionary<UtcDateTime, double?> convertedObsPowerInFarm = ConvertTimeSeriesToHourlyResolution(firstObs, obsPowerInFarm, useFixedHours);
                    startTimesInMinutes[minute] = convertedObsPowerInFarm;
                }
            }

            // 3. Search forecastPath for forecast documents...
            dateTimePattern = string.Empty;
            prevDateTimePattern = string.Empty;
            FileInfo[] files;
            string filter = "*.csv";
            if (Directory.Exists(path))
            {
                if (!string.IsNullOrEmpty(fmd.ForecastFileFilter)) filter = fmd.ForecastFileFilter;
                var dir = new DirectoryInfo(path);
                files = dir.GetFiles(filter, SearchOption.AllDirectories);
            }
            else files = new FileInfo[]{new FileInfo(path) };

            // If we only have one single forecast file, then this forecast file is usually 99% of the time a long
            // time series with continous time steps, meaning there are no overlaps. Usually, only the unique hours
            // from the original forecasts have been extracted. In these situations, we are interested in comparing
            // the skill of all hours and only use a single skill-bucket. Instead of making this an
            // explicit setting in the view (which it should be ultimately), it is no supporting this case by
            // convention. This should be changed later for flexibility...
            bool continousSerie = files.Length == 1;
            prevDateTimePattern = string.Empty;
            foreach (var fileInfo in files)
            {
                isNotPointingToANumber = false;
                UtcDateTime? firstTimePoint = null;
                UtcDateTime? firstPossibleHour = null;
                SortedDictionary<UtcDateTime, double?> predPowerInFarm;
                var stream = fileInfo.OpenRead();
                using (var reader = new StreamReader(stream))
                {
                    predPowerInFarm = ReadPredictionFile(reader, fmd, includeNegObs, useFixedHours, siteId, prevDateTimePattern, out firstTimePoint, out firstPossibleHour, out isNotPointingToANumber);
                }

                if (isNotPointingToANumber) throw new Exception("The Forecast Column Index Value is not pointing to a column which contains a double value. Change index value!");
                if (predPowerInFarm.Count == 0) continue;
                if (!firstPossibleHour.HasValue) throw new Exception("The first possible forecast hour could not be determined. Please check forecast file: " + fileInfo.FullName);
                if (!firstTimePoint.HasValue) throw new Exception("First time point in forecast file is not specified. Please check forecast file: " + fileInfo.FullName);
                if (obsResolution.HasValue)
                {
                    var firstForecastHour = firstTimePoint.Value;
                    if (useFixedHours) firstForecastHour = firstPossibleHour.Value;
                    var convertedObsPowerInFarm = startTimesInMinutes[firstForecastHour.Minute];
                    var convertedPredPowerInFarm = ConvertTimeSeriesToHourlyResolution(firstForecastHour, predPowerInFarm, useFixedHours, firstForecastHour.Minute);
                    if (convertedPredPowerInFarm.Count > 0)
                    {
                        UtcDateTime first = convertedPredPowerInFarm.Keys.First();
                        var firstTimePointInMonth = new UtcDateTime(first.Year, first.Month, 1);

                        if (continousSerie)
                        {
                            // Split up into months
                            UtcDateTime last = convertedPredPowerInFarm.Keys.Last();
                            var firstTimePointInLastMonth = new UtcDateTime(last.Year, last.Month, 1);
                            for (var time = firstTimePointInMonth; time.UtcTime <= firstTimePointInLastMonth.UtcTime; time = new UtcDateTime(time.UtcTime.AddMonths(1)))
                            {
                                if (!results.ContainsKey(time))
                                    results.TryAdd(time, new HourlySkillScoreCalculator(new List<Turbine>(), scope, (int) omd.NormalizationValue));

                                HourlySkillScoreCalculator skillCalculator = results[time];
                                var enumer = new UtcDateTimeEnumerator(time, new UtcDateTime(time.UtcTime.AddMonths(1)), TimeResolution.Hour);
                                skillCalculator.AddContinousSerie(enumer, convertedPredPowerInFarm, convertedObsPowerInFarm);
                            }
                        }
                        else
                        {
                            if (!results.ContainsKey(firstTimePointInMonth))
                                results.TryAdd(firstTimePointInMonth, new HourlySkillScoreCalculator(new List<Turbine>(), scope, (int) omd.NormalizationValue));

                            HourlySkillScoreCalculator skillCalculator = results[firstTimePointInMonth];
                            var enumer = new UtcDateTimeEnumerator(first, convertedPredPowerInFarm.Keys.Last(), TimeResolution.Hour);
                            skillCalculator.Add(enumer, convertedPredPowerInFarm, convertedObsPowerInFarm, fmd.OffsetHoursAhead);
                        }
                    }
                }
            }

            // 2.1 Print hourly obs series to file for end-user debugging and verification:
            if(useFixedHours) PrintObsPowerInFarmToDebugFile(obsPowerInFarm);

            return results;
        }
        public static bool IsValid(UtcDateTime time, TimeResolution res, TimeSpan resolutionSpan)
        {
            var span = time.UtcTime.TimeOfDay;
            var span2 = resolutionSpan;
            if (Millisecond.ToTimeSpan() == resolutionSpan) return true;
            if (Second.ToTimeSpan() == resolutionSpan) return EqualMilliseconds(span, span2);
            if (Minute.ToTimeSpan() == resolutionSpan) return EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute5th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute5th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute6th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute6th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute10th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute10th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute12th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute12th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute15th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute15th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute20th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute20th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Minute30th.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, Minute30th.IntervalStepInResolution) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            if (Hour.ToTimeSpan() == resolutionSpan) return EqualMinutes(span, span2, 0) && EqualSeconds(span, span2) && EqualMilliseconds(span, span2);
            // Cannot implement the rest without time zone data and probably a base time point.

            throw new Exception("Unable to verify TimeResolution.Type and cannot infer provided time's resolution. Type: " + res.Type);
        }
Ejemplo n.º 8
0
 public static DateTime ToLocalTime(UtcDateTime utc_dt)
 {
     return utc_dt.ToLocalTime();
 }
Ejemplo n.º 9
0
 public int CompareTo(ZonedDateTime other)
 {
     return(UtcDateTime.CompareTo(other.UtcDateTime));
 }
 public static Result <RegistrationExamInfo> Create(SubjectId subjectId, LocationId locationId, UtcDateTime examDateTime)
 {
     return(Result.Ok(new RegistrationExamInfo(subjectId, locationId, examDateTime)));
 }
 public RegistrationExamInfo(SubjectId subjectId, LocationId locationId, UtcDateTime examDateTime)
 {
     SubjectId    = subjectId;
     LocationId   = locationId;
     ExamDateTime = examDateTime;
 }
Ejemplo n.º 12
0
 public Response(Result <T> data_, UtcDateTime createdOn_, bool isPredefined_)
 {
     Data         = data_;
     CreatedOn    = createdOn_;
     IsPredefined = isPredefined_;
 }
Ejemplo n.º 13
0
 public Request(Result <string> data_, UtcDateTime createdOn_, string key_ = "default")
 {
     Data      = data_;
     CreatedOn = createdOn_;
     Key       = key_;
 }
Ejemplo n.º 14
0
 public virtual void ReceiveResponse(Result <TRespData> msg, UtcDateTime timestamp, object evaluationCtx, bool isPredefined = false)
 {
     Requests.Last().Response = new Response <TRespData>(msg, timestamp, isPredefined);
 }
Ejemplo n.º 15
0
 public long ToFileTime()
 {
     return(UtcDateTime.ToFileTime());
 }
Ejemplo n.º 16
0
 public ZonedDateTime AddMinutes(double value)
 {
     return(new ZonedDateTime(UtcDateTime.AddMinutes(value), Zone));
 }
Ejemplo n.º 17
0
 public static int GetUtcOffset(UtcDateTime dt)
 {
     return GetUtcOffset(dt.ToLocalTime());
 }
Ejemplo n.º 18
0
 public ZonedDateTime AddSeconds(double value)
 {
     return(new ZonedDateTime(UtcDateTime.AddSeconds(value), Zone));
 }
Ejemplo n.º 19
0
 public static DateTime? ToLocalTime(UtcDateTime? utc_dt)
 {
     if (utc_dt == null)
         return null;
     return ToLocalTime((UtcDateTime)utc_dt);
 }
Ejemplo n.º 20
0
 internal DateTimeOffset ToLocalTime(bool throwOnOverflow)
 {
     return(new DateTimeOffset(UtcDateTime.ToLocalTime(throwOnOverflow)));
 }
 public bool IsValid(UtcDateTime time)
 {
     return IsValid(time, this, ToTimeSpan());
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Generates actual data to send to the client.
        /// </summary>
        /// <remarks>Data is sent according to the behavior of a typical GPS device: $GPGGA,
        /// $GPGSA, $GPRMC, $GPGSV sentences are sent every second, and a $GPGSV sentence
        /// is sent every five seconds.
        /// Developers who want to emulate a specific model of GPS device should override this
        /// method and generate the sentences specific to that device.</remarks>
        protected override void OnEmulation()
        {
            // Update real-time position, speed, bearing, etc.
            base.OnEmulation();

            if (Route.Count == 0)
            {
                CurrentPosition = EmulatePositionError(CurrentPosition);
            }

            /* NMEA devices will transmit "bursts" of NMEA sentences, followed by a one-second pause.
             * Other sentences (usually $GPGSV) are transmitted once every few seconds.  This emulator,
             * by default, will transmit the most common NMEA sentences.
             */

            // $GPGGA
            if (!_gpggaInterval.Equals(TimeSpan.Zero)
                // Has enough time elapsed to send the sentence?
                && UtcDateTime.Subtract(_gpggaLastSent) > _gpggaInterval)
            {
                // Get the tracked satellite count
                int trackedCount = Satellites.Count(item => item.SignalToNoiseRatio.Value > 0);

                // Yes
                _gpggaLastSent = UtcDateTime;

                // Queue the sentence to the read buffer
                WriteSentenceToClient(new GpggaSentence(UtcDateTime.TimeOfDay, CurrentPosition, _fixQuality, trackedCount,
                                                        _horizontalDOP, Altitude.Add(EmulateError(_verticalDOP)), Distance.Empty, TimeSpan.Zero, -1)); //Add an error to the altitude written to the client but don't change the actual value (otherwise it will "walk")
            }

            // $GPRMC
            if (!_gprmcInterval.Equals(TimeSpan.Zero)
                // Has enough time elapsed to send the sentence?
                && UtcDateTime.Subtract(_gprmcLastSent) > _gprmcInterval)
            {
                // Yes
                _gprmcLastSent = UtcDateTime;

                // Queue the sentence to the read buffer
                WriteSentenceToClient(new GprmcSentence(UtcDateTime, _fixStatus == FixStatus.Fix, CurrentPosition, Speed,
                                                        Bearing, _magneticVariation));
            }

            // $GPGLL
            if (!_gpgllInterval.Equals(TimeSpan.Zero)
                // Has enough time elapsed to send the sentence?
                && UtcDateTime.Subtract(_gpgllLastSent) > _gpgllInterval)
            {
                // Yes
                _gpgllLastSent = UtcDateTime;

                // Write a $GPGLL to the client
                WriteSentenceToClient(new GpgllSentence(CurrentPosition, UtcDateTime.TimeOfDay, _fixStatus));
            }

            // $GPGSA
            if (!_gpgsaInterval.Equals(TimeSpan.Zero)
                // Has enough time elapsed to send the sentence?
                && UtcDateTime.Subtract(_gpgsaLastSent) > _gpgsaInterval)
            {
                // Yes
                _gpgsaLastSent = UtcDateTime;

                // Queue the sentence to the read buffer
                WriteSentenceToClient(new GpgsaSentence(_fixMode, _fixMethod, Satellites,
                                                        _meanDOP, _horizontalDOP, _verticalDOP));
            }

            // $GPGSV
            if (!_gpgsvInterval.Equals(TimeSpan.Zero)
                // Has enough time elapsed to send the sentence?
                && UtcDateTime.Subtract(_gpgsvLastSent) > _gpgsvInterval)
            {
                // Build a list of sentences from our satellites
                IList <GpgsvSentence> sentences = GpgsvSentence.FromSatellites(Satellites);

                // Yes
                _gpgsvLastSent = UtcDateTime;

                // Write each sentence to the read buffer
                foreach (GpgsvSentence gpgsv in sentences)
                {
                    WriteSentenceToClient(gpgsv);
                }
            }

            // And signal that we have data (or not)
            if (ReadBuffer.Count == 0)
            {
                ReadDataAvailableWaitHandle.Reset();
            }
            else
            {
                ReadDataAvailableWaitHandle.Set();
            }
        }
        private static SortedDictionary<UtcDateTime, double?> ConvertTimeSeriesToHourlyResolution(UtcDateTime firstPossibleHour, SortedDictionary<UtcDateTime, double?> series, bool useFixedHours, int minuteOffset=0)
        {
            if(series.Count==0) return new SortedDictionary<UtcDateTime, double?>();

            var timePoints = series.Keys.Take(2).ToArray();
            TimeSpan span = timePoints[1].UtcTime - timePoints[0];
            int steps;
            var resolution = span.ToTimeResolution(out steps);

            if (resolution == TimeResolutionType.Hour && steps == 1)
            {
                /* Great, do nothing...*/
                return series;
            }
            /*if (resolution == TimeResolutionType.Minute && steps == 5)
            {
                var firstTimePoint = timePoints[0];
                if (useFixedHours && (firstTimePoint.Minute > 0 || firstTimePoint.Second > 0))
                {
                    firstTimePoint = firstTimePoint.AddHours(1);
                    firstTimePoint = new UtcDateTime(firstTimePoint.Year, firstTimePoint.Month, firstTimePoint.Day, firstTimePoint.Hour);
                }

                var lastPossibleHour = series.Keys.Last();
                if (useFixedHours && (lastPossibleHour.Minute > 0 || lastPossibleHour.Second > 0))
                {
                    lastPossibleHour = lastPossibleHour.AddHours(-1);
                    lastPossibleHour = new UtcDateTime(lastPossibleHour.Year, lastPossibleHour.Month, lastPossibleHour.Day, lastPossibleHour.Hour);
                }

                return ProductionDataHelper.CalculateMarketResolutionAverage(firstTimePoint, firstPossibleHour, lastPossibleHour, TimeSpan.FromHours(1), span, series);
            }
            if (resolution == TimeResolutionType.Minute && steps == 10)
            {
                // darn, typical scada resolution, we need to calc hourly avg...
                var lastPossibleHour = series.Keys.Last();
                lastPossibleHour = new UtcDateTime(lastPossibleHour.Year, lastPossibleHour.Month, lastPossibleHour.Day, lastPossibleHour.Hour);

                return ProductionDataHelper.CalculateMarketResolutionAverage(timePoints[0], firstPossibleHour, lastPossibleHour, TimeSpan.FromHours(1), span, series);
            }
            if (resolution == TimeResolutionType.Minute && steps == 15)
            {
                // darn, typical market resolution, we need to calc hourly avg...
                var firstTimePoint = timePoints[0];
                if(firstTimePoint > firstPossibleHour) throw new Exception("First time point is greater than first possible hour, this is wrong, first time point must always be lesser than first possible hour.");

                if (firstTimePoint.Minute > 0 || firstTimePoint.Second > 0)
                {
                    firstTimePoint = firstTimePoint.AddHours(1);
                    firstTimePoint = new UtcDateTime(firstTimePoint.Year, firstTimePoint.Month, firstTimePoint.Day, firstTimePoint.Hour);
                }
                var lastPossibleHour = series.Keys.Last();
                if (lastPossibleHour.Minute > 0 || lastPossibleHour.Second > 0)
                {
                    lastPossibleHour = lastPossibleHour.AddHours(-1);
                    lastPossibleHour = new UtcDateTime(lastPossibleHour.Year, lastPossibleHour.Month, lastPossibleHour.Day, lastPossibleHour.Hour);
                }

                return ProductionDataHelper.CalculateMarketResolutionAverage(firstTimePoint, firstPossibleHour, lastPossibleHour, TimeSpan.FromHours(1), span, series);
            }
            if (resolution == TimeResolutionType.Minute && steps == 30)
            {
                // darn, typical market resolution, we need to calc hourly avg...
                var firstTimePoint = timePoints[0];
                if (firstTimePoint > firstPossibleHour) throw new Exception("First time point is greater than first possible hour, this is wrong, first time point must always be lesser than first possible hour.");
                if (firstTimePoint.Minute > 0 || firstTimePoint.Second > 0)
                {
                    firstTimePoint = firstTimePoint.AddHours(1);
                    firstTimePoint = new UtcDateTime(firstTimePoint.Year, firstTimePoint.Month, firstTimePoint.Day, firstTimePoint.Hour);
                }
                var lastPossibleHour = series.Keys.Last();
                if (lastPossibleHour.Minute > 0 || lastPossibleHour.Second > 0)
                {
                    lastPossibleHour = lastPossibleHour.AddHours(-1);
                    lastPossibleHour = new UtcDateTime(lastPossibleHour.Year, lastPossibleHour.Month, lastPossibleHour.Day, lastPossibleHour.Hour);
                }

                return ProductionDataHelper.CalculateMarketResolutionAverage(firstTimePoint, firstPossibleHour, lastPossibleHour, TimeSpan.FromHours(1), span, series);
            }*/

            var firstTimePoint = timePoints[0];
            if (firstTimePoint > firstPossibleHour) throw new Exception("First time point is greater than first possible hour, this is wrong, first time point must always be lesser than first possible hour.");
            if (useFixedHours && (firstTimePoint.Minute > 0 || firstTimePoint.Second > 0))
            {
                firstTimePoint = firstTimePoint.AddHours(1);
                firstTimePoint = new UtcDateTime(firstTimePoint.Year, firstTimePoint.Month, firstTimePoint.Day, firstTimePoint.Hour);
            }

            var lastPossibleHour = series.Keys.Last();
            if (lastPossibleHour.Minute > 0 || lastPossibleHour.Second > 0)
            {
                lastPossibleHour = lastPossibleHour.AddHours(-1);
                lastPossibleHour = new UtcDateTime(lastPossibleHour.Year, lastPossibleHour.Month, lastPossibleHour.Day, lastPossibleHour.Hour);
            }
            if (!useFixedHours) lastPossibleHour = lastPossibleHour.Subtract(TimeSpan.FromMinutes(minuteOffset));

            return ProductionDataHelper.CalculateMarketResolutionAverage(firstTimePoint, firstPossibleHour, lastPossibleHour, TimeSpan.FromHours(1), span, series);
            //return new SortedDictionary<UtcDateTime, double?>();
        }
Ejemplo n.º 24
0
 public bool Equals(DateTimeOffset other)
 {
     return(UtcDateTime.Equals(other.UtcDateTime));
 }
        internal static SortedDictionary<UtcDateTime, double?> CalculateMarketResolutionAverage(UtcDateTime firstTimePoint, UtcDateTime first, UtcDateTime last, TimeSpan marketResolution, TimeSpan dataResolution, SortedDictionary<UtcDateTime, double?> prod, bool isForecast=false)
        {
            int steps;
            TimeResolutionType timeResolutionType = dataResolution.ToTimeResolution(out steps);

            // Create TimePoints that we are interested to aggregate data on:
            var time = last;//.Subtract(marketResolution);
            var timePoints = new List<UtcDateTime> { last };
            do
            {
                time = time.Subtract(marketResolution);
                if (time >= first) timePoints.Add(time);
            } while (time >= first);

            var resolutionSamples = new SortedDictionary<UtcDateTime, List<double?>>();
            for (int index = 0; index < timePoints.Count; index++)
            {
                if (index + 1 < timePoints.Count)
                {
                    UtcDateTime point = timePoints[index]; // this should be end time e.g 12:00
                    UtcDateTime prevPoint = timePoints[index + 1].Add(dataResolution); // this should be time resolution before, e.g. an hour, thus 11:00, but maybe adding data resolution e.g. 10.

                    var enumer = new UtcDateTimeEnumerator(prevPoint, point, timeResolutionType, steps);
                    foreach (UtcDateTime utcDateTime in enumer)
                    {
                        if (prod.ContainsKey(utcDateTime))
                        {
                            double? production = prod[utcDateTime];
                            if (!resolutionSamples.ContainsKey(point)) resolutionSamples.Add(point, new List<double?>());
                            resolutionSamples[point].Add(production);
                        }
                    }
                }
                else // This is for the last point...
                {
                    UtcDateTime point = timePoints[index]; // this should be end time e.g 12:00
                    UtcDateTime prevPoint = timePoints[index].Subtract(marketResolution).Add(dataResolution); // this should be time resolution before, e.g. an hour, thus 11:00, but maybe adding data resolution e.g. 10.
                    var enumer = new UtcDateTimeEnumerator(prevPoint, point, timeResolutionType, steps);
                    foreach (UtcDateTime utcDateTime in enumer)
                    {
                        if (prod.ContainsKey(utcDateTime))
                        {
                            double? production = prod[utcDateTime];
                            if (!resolutionSamples.ContainsKey(point)) resolutionSamples.Add(point, new List<double?>());
                            resolutionSamples[point].Add(production);
                        }
                    }
                }
            }

            int productionStepsInResolution = CalculateProductionStepsInResolution(marketResolution, dataResolution);
            var avgProds = new SortedDictionary<UtcDateTime, double?>();

            foreach (UtcDateTime dateTime in resolutionSamples.Keys)
            {
                var prodInResolution = resolutionSamples[dateTime];
                // When calculating observed production, we always calculate the average as sum devided by time steps in period (resolution)
                double? avgPower;
                if (!isForecast) avgPower = prodInResolution.Sum()/productionStepsInResolution;
                else
                {   // When calculating predicted production, we always calculate the pure average
                    double sum = 0;
                    int nrOfObsInPeriod = 0;
                    foreach (double? p in prodInResolution)
                    {
                        if (p.HasValue)
                        {
                            nrOfObsInPeriod++;
                            sum += p.Value;
                        }
                    }

                    if (nrOfObsInPeriod > 0) avgPower = sum/nrOfObsInPeriod;
                    else avgPower = null;
                }
                avgProds.Add(dateTime, avgPower);
            }
            return avgProds;
        }
Ejemplo n.º 26
0
 public abstract Result LoadPredefinedResponse(ActionResponse response, UtcDateTime now, object evaluationCtx);