//----------------------------------------------------------------------- // // Copyright (c) 2009 Deltares. All rights reserved. // // B. Faassen // barry.faassen@deltares.nl // 22-6-2009 // n.a. //----------------------------------------------------------------------- namespace Deltares.Dam.Data { using System; using System.Collections.Generic; public class TimeSerie { /// /// List of parameter identifiers used in Fews timeseries /// public const string WaterLevelParameterId = "Waterlevel"; public const string WaterLevelMeanParameterId = "WaterlevelMean"; public const string WaterLevelStdParameterId = "WaterlevelStdev"; public const string WaterLevelDistParameterId = "WaterlevelDistribution"; public const string WaterPressureParameterId = "Waterpressure"; public const string PiezoMetricHeadId = "PiezoMetricHead"; private readonly TimeStep timeStep; //Bka: read only not possible to serialize to DB private readonly List entries; private IList entries; public const double DefaultMissingValue = -999.0; public TimeSerie() { this.timeStep = new TimeStep(); this.entries = new List(); ForecastDateTime = DateTime.MinValue; } public string Type { get; set; } public string LocationId { get; set; } public string ParameterId { get; set; } public TimeStep TimeStep { get { return this.timeStep; } } public DateTime StartDateTime { get; set; } public DateTime EndDateTime { get; set; } public DateTime ForecastDateTime { get; set; } public double MissVal { get; set; } public string LongName { get; set; } public string StationName { get; set; } public string Units { get; set; } public string SourceOrganisation { get; set; } public string SourceSystem { get; set; } public string FileDescription { get; set; } public DateTime? CreationDateTime { get; set; } public string Region { get; set; } public virtual IList Entries { get { return this.entries; } set { this.entries = value; } } public string Comment { get; set; } /// /// Creates a shallow copy of the current time serie and applies the map function to each of its entries /// /// The time serie entry function which maps uses the current value as input /// Time serie (clone) with new calculated entries public TimeSerie Map(Func function) { return this.Map(this.ParameterId, function); } /// /// Creates a shallow copy of the current time serie and applies the map function to each of its entries /// /// The (new) id of the parameter in the cloned time serie /// The (new) location id of the cloned time serie /// The time serie entry function which maps uses the current value as input /// Time serie (clone) with new calculated entries public TimeSerie Map(string parameterId, string locationId, Func function) { return Map(this, parameterId, locationId, function); } /// /// Creates a shallow copy of the current time serie and applies the map function to each of its entries /// /// The (new) id of the parameter in the cloned time serie /// The time serie entry function which maps uses the current value as input /// Time serie (clone) with new calculated entries public TimeSerie Map(string parameterId, Func function) { return Map(this, parameterId, function); } /// /// Creates a shallow copy of the current time serie and applies the map function to each of its entries /// /// The time serie to map from /// The (new) id of the parameter in the cloned time serie /// The time serie entry function which maps uses the current value as input /// Time serie (clone) with new calculated entries public static TimeSerie Map(TimeSerie timeSerie, string parameterId, Func function) { return Map(timeSerie, parameterId, null, function); } /// /// Creates a shallow copy of the current time serie and applies the map function to each of its entries /// /// The time serie to map from /// The (new) id of the parameter in the cloned time serie /// The (new) location id of the cloned time serie /// The time serie entry function which maps uses the current value as input /// Time serie (clone) with new calculated entries public static TimeSerie Map(TimeSerie timeSerie, string parameterId, string locationId, Func function) { var result = CreateTimeSerie(timeSerie, parameterId); if (!(string.IsNullOrEmpty(locationId) || locationId.Trim() == "")) result.LocationId = locationId; foreach (var entry in timeSerie.Entries) result.Entries.Add(function(entry)); return result; } /// /// Gets a shallow copy of the time serie without the time serie entries /// /// A copy of this time serie public TimeSerie GetShallowCopy() { var serie = new TimeSerie { Type = Type, ParameterId = ParameterId, LocationId = LocationId, StartDateTime = StartDateTime, EndDateTime = EndDateTime, ForecastDateTime = ForecastDateTime, MissVal = MissVal, LongName = LongName, StationName = StationName, SourceOrganisation = SourceOrganisation, SourceSystem = SourceSystem, FileDescription = FileDescription, CreationDateTime = CreationDateTime, Region = Region, Units = Units, TimeStep = { Multiplier = TimeStep.Multiplier, Divider = TimeStep.Divider, Unit = TimeStep.Unit } }; serie.Entries.Clear(); return serie; } public static TimeSerie CreateTimeSerie(string locationId) { var timeSerie = new TimeSerie { MissVal = DefaultMissingValue, ParameterId = WaterLevelParameterId, LocationId = locationId, StationName = locationId, Type = "instantaneous", Units = "m", Comment = "" }; return timeSerie; } /// /// Creates a time serie by copying data from the source time serie without /// time serie entries (shallow copy) /// /// The source time serie used to copy from /// The new parameter ID /// A time serie copy without time entries public static TimeSerie CreateTimeSerie(TimeSerie source, string parameterId) { var serie = source.GetShallowCopy(); serie.ParameterId = parameterId; serie.Units = "-"; serie.Comment = ""; return serie; } public double GetValue(DateTime dateTime) { foreach (TimeSerieEntry entry in this.Entries) { if (entry.DateTime >= dateTime) { return entry.Value; } } return -1; } public string GetNearestBasisFileName(DateTime dateTime) { string res = ""; double mindiff = 1e99; double diff = 0; foreach (TimeSerieEntry entry in this.Entries) { diff = Math.Abs(entry.DateTime.ToFileTimeUtc() - dateTime.ToFileTimeUtc()); if (diff < mindiff) { res = entry.BasisFileName; mindiff = diff; } } return res; } } }