public string Read() { _currentContext = new SQLAzureDataContext(); var elementId = _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value; var start = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value); var end = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value); long timestep = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "timeStep") > 0 ? int.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var allResults = _currentContext.GetTankData(start, end, elementId).ToList(); if (timestep != -1 && allResults.Any()) { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End List <DateTimeBlock> dateTimeBlockSet = new List <DateTimeBlock>(); for (var BlockIndex = 0; BlockIndex < numberOfBlock; BlockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(BlockIndex * timestepTicks), To = start.AddTicks((BlockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } var subResults = new List <GetTankDataResult>(); // loop through each block and and find any records that span that block, trimming those on the edges foreach (var thisBlock in dateTimeBlockSet) { var firstRecordkInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).FirstOrDefault(); var lastRecordInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).Reverse().LastOrDefault(); if (firstRecordkInSet != null && lastRecordInSet != null) { var recordsSpanningBlock = allResults.Where(x => x.From >= firstRecordkInSet.From && x.To <= lastRecordInSet.To); var getTankDataResults = recordsSpanningBlock as GetTankDataResult[] ?? recordsSpanningBlock.ToArray(); if (getTankDataResults.Any()) { var trimmedrecordsSpanningBlock = new List <GetTankDataResult>(); foreach (var record in getTankDataResults) { double percentageToTrim = 0; if (record.From < thisBlock.From) { long currentBlockTicks = record.To.Ticks - record.From.Ticks; long ticksToTrim = thisBlock.From.Ticks - record.From.Ticks; percentageToTrim += (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } if (record.To > thisBlock.To) { long currentBlockTicks = record.To.Ticks - record.From.Ticks; long ticksToTrim = record.To.Ticks - thisBlock.To.Ticks; percentageToTrim = (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } if (record.Inflow != null) { record.Inflow = record.Inflow - ((record.Inflow / 100) * percentageToTrim); } trimmedrecordsSpanningBlock.Add(record); } double?inflow = null; foreach (var reading in trimmedrecordsSpanningBlock) { if (reading.Inflow != null) { if (inflow == null) { inflow = reading.Inflow; } else { inflow += reading.Inflow; } } } var modifiedReading = trimmedrecordsSpanningBlock.FirstOrDefault(); if (inflow != null) { if (modifiedReading != null) { modifiedReading.Inflow = inflow / trimmedrecordsSpanningBlock.Count(); } } if (modifiedReading != null) { modifiedReading.From = thisBlock.From; modifiedReading.To = thisBlock.To; subResults.Add(modifiedReading); } } } } allResults = subResults; } var resultTxt = new StringBuilder(); foreach (var thisReading in allResults) { var volumeValue = thisReading.Volume != null ? "<variable name=\"volume\">" + $"<value>{thisReading.Volume}</value>" + "</variable>" : null; var levelValue = thisReading.Outflow != null ? "<variable name=\"outflow\">" + $"<value>{thisReading.Outflow}</value>" + "</variable>" : null; var inflowValue = thisReading.Inflow != null ? "<variable name=\"inflow\">" + $"<value>{thisReading.Inflow}</value>" + "</variable>" : null; resultTxt.Append("<record>" + $"<from>{thisReading?.From.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</from>" + $"<to>{thisReading?.To.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</to>{volumeValue}{levelValue}{inflowValue}</record>{Environment.NewLine}"); } var tankName = string.Empty; if (resultTxt.Length > 0) { tankName = _currentContext.Tanks.FirstOrDefault(x => x.Identifier == elementId)?.Name; } return("<response>" + "<recordSet>" + $"<elementId>{elementId}</elementId>" + $"<name>{tankName}</name>{resultTxt}</recordSet>" + "</response>"); }
public string Read() { _currentContext = new SQLAzureDataContext(); var elementId = _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value; var start = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value); var end = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value); long timestep = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "timeStep") > 0 ? int.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var allResults = _currentContext.GetWeatherPredictionData(start, end, elementId).ToList(); if (timestep != -1 && allResults.Any()) { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End var dateTimeBlockSet = new List <DateTimeBlock>(); for (var blockIndex = 0; blockIndex < numberOfBlock; blockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(blockIndex * timestepTicks), To = start.AddTicks((blockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } var subResults = new List <GetWeatherPredictionDataResult>(); // loop through each block and and find any records that span that block, trimming those on the edges foreach (var thisBlock in dateTimeBlockSet) { var firstRecordkInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).FirstOrDefault(); var lastRecordInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).LastOrDefault(); if (firstRecordkInSet != null && lastRecordInSet != null) { var recordsSpanningBlock = allResults.Where(x => x.From >= firstRecordkInSet.From && x.To <= lastRecordInSet.To); if (recordsSpanningBlock.Any()) { var trimmedrecordsSpanningBlock = new List <GetWeatherPredictionDataResult>(); foreach (var record in recordsSpanningBlock) { double percentageToTrim = 0; if (record.From < thisBlock.From) { long currentBlockTicks = record.To.Ticks - record.From.Ticks; long ticksToTrim = thisBlock.From.Ticks - record.From.Ticks; percentageToTrim += (double)(((long)100.0 / currentBlockTicks) * ticksToTrim); } if (record.To > thisBlock.To) { long currentBlockTicks = record.To.Ticks - record.From.Ticks; long ticksToTrim = record.To.Ticks - thisBlock.To.Ticks; percentageToTrim = (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } if (record.Precipitation != null) { record.Precipitation = record.Precipitation - ((record.Precipitation / 100) * percentageToTrim); } trimmedrecordsSpanningBlock.Add(record); } int? humidity = null; double?precipitation = null; int? pressure = null; double?solarRadiation = null; double?temperature = null; string windDirection = null; double?windVelocity = null; foreach (var reading in trimmedrecordsSpanningBlock) { if (reading.Humidity != null) { if (humidity == null) { humidity = reading.Humidity; } else if (reading.Humidity != -1) { humidity += reading.Humidity; } } if (reading.Precipitation != null) { if (precipitation == null) { precipitation = reading.Precipitation; } else if (reading.Precipitation != -1) { precipitation += reading.Precipitation; } } if (reading.Pressure != null) { if (pressure == null) { pressure = reading.Pressure; } else if (reading.Pressure != -1) { pressure += reading.Pressure; } } if (reading.Solar_radiation != null) { if (solarRadiation == null) { solarRadiation = reading.Solar_radiation; } else if (reading.Solar_radiation != -1) { solarRadiation += reading.Solar_radiation; } } if (reading.Temperature != null) { if (temperature == null) { temperature = reading.Temperature; } else if (reading.Temperature != -1) { temperature += reading.Temperature; } } windDirection = reading.Wind_direction; if (reading.Wind_velocity != null) { if (windVelocity == null) { windVelocity = reading.Wind_velocity; } else if (reading.Wind_velocity != -1) { windVelocity += reading.Wind_velocity; } } } var modifiedReading = trimmedrecordsSpanningBlock.FirstOrDefault(); if (humidity != null) { modifiedReading.Humidity = humidity / trimmedrecordsSpanningBlock.Count(); } if (precipitation != null) { modifiedReading.Precipitation = precipitation; } if (pressure != null) { modifiedReading.Pressure = pressure / trimmedrecordsSpanningBlock.Count(); } if (solarRadiation != null) { modifiedReading.Solar_radiation = solarRadiation / trimmedrecordsSpanningBlock.Count(); } if (temperature != null) { modifiedReading.Temperature = temperature / trimmedrecordsSpanningBlock.Count(); } modifiedReading.Wind_direction = windDirection; if (windVelocity != null) { modifiedReading.Wind_velocity = windVelocity / trimmedrecordsSpanningBlock.Count(); } modifiedReading.From = thisBlock.From; modifiedReading.To = thisBlock.To; subResults.Add(modifiedReading); } } } allResults = subResults; } var resultTxt = new StringBuilder(); foreach (var thisReading in allResults) { var precipitationValue = thisReading.Precipitation != null ? "<variable name=\"precipitation\">" + $"<value>{thisReading.Precipitation}</value>" + "</variable>" : null; var temperatureValue = thisReading.Temperature != null ? "<variable name=\"temperature\">" + $"<value>{thisReading.Temperature}</value>" + "</variable>" : null; var humidityValue = thisReading.Humidity != null ? "<variable name=\"humidity\">" + $"<value>{thisReading.Humidity}</value>" + "</variable>" : null; var pressureValue = thisReading.Pressure != null ? "<variable name=\"pressure\">" + $"<value>{thisReading.Pressure}</value>" + "</variable>" : null; var windDirectionValue = thisReading.Wind_direction != null ? "<variable name=\"wind_direction\">" + $"<value>{thisReading.Wind_direction}</value>" + "</variable>" : null; var windVelocityValue = thisReading.Wind_velocity != null ? "<variable name=\"wind_velocity\">" + $"<value>{thisReading.Wind_velocity}</value>" + "</variable>" : null; var solarRadiationValue = thisReading.Solar_radiation != null ? "<variable name=\"solar_radiation\">" + $"<value>{thisReading.Solar_radiation}</value>" + "</variable>" : null; var metadata = "<metadata baseTime=\"" + thisReading.BaseTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK") + "\" creationTime=\"" + thisReading.CreationTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK") + "\" generatedBy=\"" + thisReading.Identifier + "\" comment=\"" + thisReading.Comment + "\" />"; resultTxt.Append( $"<record>{metadata}<from>{thisReading?.From.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</from>" + $"<to>{thisReading?.To.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</to>{precipitationValue}{temperatureValue}{humidityValue}{pressureValue}{windDirectionValue}{windVelocityValue}{solarRadiationValue}</record>{Environment.NewLine}"); } return("<response><recordSet>" + $"<elementId>{elementId}</elementId>{resultTxt}</recordSet></response>"); }
public string Read() { _currentContext = new SQLAzureDataContext(); var elementId = _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value; var start = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value); var end = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value); var simulation = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "simulation") ? DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "simulation").Value) : DateTimeOffset.Now; long timestep = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "timeStep") > 0 ? int.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var allResults = _currentContext.GetPricingData(start, end, elementId, simulation).ToList(); if (timestep != -1 && allResults.Any()) { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End var dateTimeBlockSet = new List <DateTimeBlock>(); for (var blockIndex = 0; blockIndex < numberOfBlock; blockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(blockIndex * timestepTicks), To = start.AddTicks((blockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } var subResults = new List <GetPricingDataResult>(); // loop through each block and and find any records that span that block, trimming those on the edges foreach (DateTimeBlock thisBlock in dateTimeBlockSet) { var firstRecordkInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).FirstOrDefault(); var lastRecordInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).LastOrDefault(); if (firstRecordkInSet != null && lastRecordInSet != null) { var recordsSpanningBlock = allResults.Where(x => x.From >= firstRecordkInSet.From && x.To <= lastRecordInSet.To); var getPricingDataResults = recordsSpanningBlock as GetPricingDataResult[] ?? recordsSpanningBlock.ToArray(); if (getPricingDataResults.Any()) { decimal Price = getPricingDataResults.Sum(reading => reading.Price); var modifiedReading = getPricingDataResults.FirstOrDefault(); if (modifiedReading != null) { modifiedReading.Price = Price / getPricingDataResults.Count(); modifiedReading.From = thisBlock.From; modifiedReading.To = thisBlock.To; subResults.Add(modifiedReading); } } } } allResults = subResults; } var resultTxt = new StringBuilder(); var baseTime = string.Empty; var creationTime = string.Empty; var generatedBy = string.Empty; foreach (var thisReading in allResults) { if (baseTime == string.Empty) { baseTime = thisReading.BaseTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK"); creationTime = thisReading.CreationTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK"); } generatedBy = thisReading.Identifier; resultTxt.Append("<record>" + $"<from>{thisReading.From.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</from>" + $"<to>{thisReading.To.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</to>" + "<variable name=\"price\">" + $"<value>{thisReading.Price}</value>" + "</variable>" + $"</record>{Environment.NewLine}"); } var dmaName = string.Empty; if (resultTxt.Length > 0) { resultTxt.Insert(0, $"<elementId>{elementId}</elementId>{Environment.NewLine}<name>{dmaName}</name>{Environment.NewLine}" + $"<metadata baseTime=\"{baseTime}\" creationTime=\"{creationTime}\" generatedBy=\"{generatedBy}\" />{Environment.NewLine}"); } return("<response>" + $"<recordSet>{resultTxt}</recordSet>" + "</response>"); }
public string ReadSpecificProperty(List <int> indices) { _currentContext = new SQLAzureDataContext(); var elementId = _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value; var start = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "start") ? DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value) : DateTimeOffset.MinValue; var end = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "end") ? DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value) : DateTimeOffset.MaxValue; long timestep = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "timeStep") > 0 ? int.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var resultTxt = new StringBuilder(); var allResults = _currentContext.GetDMAData(start, end, elementId).ToList(); if (allResults.Any()) { if (timestep != -1 && allResults.Any()) { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End var dateTimeBlockSet = new List <DateTimeBlock>(); for (var blockIndex = 0; blockIndex < numberOfBlock; blockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(blockIndex * timestepTicks), To = start.AddTicks((blockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } var subResults = new List <GetDMADataResult>(); // loop through each block and and find any records that span that block, trimming those on the edges foreach (var thisBlock in dateTimeBlockSet) { var recordsSpanningBlock = allResults.Where(x => x.Time > thisBlock.From && x.Time <= thisBlock.To); var getDmaDataResults = recordsSpanningBlock as GetDMADataResult[] ?? recordsSpanningBlock.ToArray(); if (getDmaDataResults.Any()) { double?flow = null; double?pressure = null; double?Volume = null; foreach (var reading in getDmaDataResults) { if (reading.Flow != null) { if (flow == null) { flow = reading.Flow; } else { flow += reading.Flow; } } if (reading.Pressure != null) { if (pressure == null) { pressure = reading.Pressure; } else { pressure += reading.Pressure; } } if (reading.Volume != null) { if (Volume == null) { Volume = reading.Volume; } else { Volume += reading.Volume; } } } var modifiedReading = getDmaDataResults.FirstOrDefault(); if (flow != null) { if (modifiedReading != null) { modifiedReading.Flow = flow; } } if (pressure != null) { if (modifiedReading != null) { modifiedReading.Pressure = pressure / getDmaDataResults.Count(x => x.Pressure != null); } } if (Volume != null) { if (modifiedReading != null) { modifiedReading.Volume = Volume; } } if (modifiedReading != null) { modifiedReading.Time = thisBlock.From; subResults.Add(modifiedReading); } } } allResults = subResults; } foreach (var thisReading in allResults) { var flowValue = string.Empty; var pressureValue = string.Empty; var volumeValue = string.Empty; if (indices.Contains(16) && thisReading.Flow != null) { flowValue = "<variable name=\"flow\">" + $"<value>{thisReading.Flow}</value>" + "</variable>"; } if (indices.Contains(17) && thisReading.Pressure != null) { pressureValue = "<variable name=\"pressure\">" + $"<value>{thisReading.Pressure}</value>" + "</variable>"; } if (indices.Contains(18) && thisReading.Volume != null) { volumeValue = "<variable name=\"volume\">" + $"<value>{thisReading.Volume}</value>" + "</variable>"; } if (!string.IsNullOrEmpty(flowValue) || !string.IsNullOrEmpty(pressureValue) || !string.IsNullOrEmpty(volumeValue)) { resultTxt.Append($"<record><time>{thisReading.Time.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</time>{flowValue}{pressureValue}{volumeValue}</record>{Environment.NewLine}"); } } } return(resultTxt.ToString()); }
public string ReadSpecificProperty(List <int> indices) { _currentContext = new SQLAzureDataContext(); var elementId = _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value; var start = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "start") ? DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value) : DateTimeOffset.MinValue; var end = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "end") ? DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value) : DateTimeOffset.MaxValue; long timestep = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "timeStep") > 0 ? int.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var resultTxt = new StringBuilder(); var allResults = _currentContext.GetWeatherData(start, end, elementId).ToList(); if (allResults.Any()) { // start = allResults.Aggregate((acc, c) => acc.From < c.From ? acc : c).From; // end = allResults.Aggregate((acc, c) => acc.To > c.To ? acc : c).To; if (timestep != -1 && allResults.Any()) { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End var dateTimeBlockSet = new List <DateTimeBlock>(); for (var blockIndex = 0; blockIndex < numberOfBlock; blockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(blockIndex * timestepTicks), To = start.AddTicks((blockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } var subResults = new List <GetWeatherDataResult>(); // loop through each block and and find any records that span that block, trimming those on the edges foreach (var thisBlock in dateTimeBlockSet) { var firstRecordkInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From) .OrderBy(x => x.From) .FirstOrDefault(); var lastRecordInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From) .OrderBy(x => x.From) .Reverse() .LastOrDefault(); if (firstRecordkInSet != null && lastRecordInSet != null) { var recordsSpanningBlock = allResults.Where(x => x.From >= firstRecordkInSet.From && x.To <= lastRecordInSet.To); var getWeatherDataResults = recordsSpanningBlock as GetWeatherDataResult[] ?? recordsSpanningBlock.ToArray(); if (getWeatherDataResults.Any()) { var trimmedrecordsSpanningBlock = new List <GetWeatherDataResult>(); foreach (var record in getWeatherDataResults) { double percentageToTrim = 0; if (record.From < thisBlock.From) { long currentBlockTicks = record.To.Ticks - record.From.Ticks; long ticksToTrim = thisBlock.From.Ticks - record.From.Ticks; percentageToTrim += (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } if (record.To > thisBlock.To) { long currentBlockTicks = record.To.Ticks - record.From.Ticks; long ticksToTrim = record.To.Ticks - thisBlock.To.Ticks; percentageToTrim = (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } if (record.Precipitation != null) { record.Precipitation = record.Precipitation - ((record.Precipitation / 100) * percentageToTrim); } trimmedrecordsSpanningBlock.Add(record); } int? humidity = null; double?precipitation = null; int? pressure = null; double?solarRadiation = null; double?temperature = null; string windDirection = null; double?windVelocity = null; foreach (var reading in trimmedrecordsSpanningBlock) { if (reading.Humidity != null) { if (humidity == null) { humidity = reading.Humidity; } else { humidity += reading.Humidity; } } if (reading.Precipitation != null) { if (precipitation == null) { precipitation = reading.Precipitation; } else { precipitation += reading.Precipitation; } } if (reading.Pressure != null) { if (pressure == null) { pressure = reading.Pressure; } else { pressure += reading.Pressure; } } if (reading.Solar_radiation != null) { if (solarRadiation == null) { solarRadiation = reading.Solar_radiation; } else { solarRadiation += reading.Solar_radiation; } } if (reading.Temperature != null) { if (temperature == null) { temperature = reading.Temperature; } else { temperature += reading.Temperature; } } windDirection = reading.Wind_direction; if (reading.Wind_velocity != null) { if (windVelocity == null) { windVelocity = reading.Wind_velocity; } else { windVelocity += reading.Wind_velocity; } } } var modifiedReading = trimmedrecordsSpanningBlock.FirstOrDefault(); if (humidity != null) { modifiedReading.Humidity = humidity / trimmedrecordsSpanningBlock.Count(); } if (precipitation != null) { modifiedReading.Precipitation = precipitation; } if (pressure != null) { modifiedReading.Pressure = pressure / trimmedrecordsSpanningBlock.Count(); } if (solarRadiation != null) { modifiedReading.Solar_radiation = solarRadiation / trimmedrecordsSpanningBlock.Count(); } if (temperature != null) { modifiedReading.Temperature = temperature / trimmedrecordsSpanningBlock.Count(); } modifiedReading.Wind_direction = windDirection; if (windVelocity != null) { modifiedReading.Wind_velocity = windVelocity / trimmedrecordsSpanningBlock.Count(); } modifiedReading.From = thisBlock.From; modifiedReading.To = thisBlock.To; subResults.Add(modifiedReading); } } } allResults = subResults; } foreach (var thisReading in allResults) { var precipitationValue = string.Empty; var temperatureValue = string.Empty; var humidityValue = string.Empty; var pressureValue = string.Empty; var windVelocityValue = string.Empty; var windDirectionValue = string.Empty; var solarRadiationValue = string.Empty; if (indices.Contains(2) && thisReading.Precipitation != null) { precipitationValue = "<variable name=\"precipitation\">" + $"<value>{thisReading.Precipitation}</value></variable>"; } if (indices.Contains(3) && thisReading.Temperature != null) { temperatureValue = "<variable name=\"temperature\">" + $"<value>{thisReading.Temperature}</value></variable>"; } if (indices.Contains(4) && thisReading.Humidity != null) { humidityValue = "<variable name=\"humidity\">" + $"<value>{thisReading.Humidity}</value></variable>"; } if (indices.Contains(5) && thisReading.Pressure != null) { pressureValue = "<variable name=\"pressure\">" + $"<value>{thisReading.Pressure}</value></variable>"; } if (indices.Contains(6) && thisReading.Wind_velocity != null) { windVelocityValue = "<variable name=\"wind_velocity\">" + $"<value>{thisReading.Wind_velocity}</value></variable>"; } if (indices.Contains(7) && thisReading.Wind_direction != null) { windDirectionValue = "<variable name=\"wind_direction\">" + $"<value>{thisReading.Wind_direction}</value></variable>"; } if (indices.Contains(8) && thisReading.Solar_radiation != null) { solarRadiationValue = "<variable name=\"solar_radiation\">" + $"<value>{thisReading.Solar_radiation}</value></variable>"; } if (!string.IsNullOrEmpty(precipitationValue) || !string.IsNullOrEmpty(temperatureValue) || !string.IsNullOrEmpty(humidityValue) || !string.IsNullOrEmpty(pressureValue) || !string.IsNullOrEmpty(windVelocityValue) || !string.IsNullOrEmpty(windDirectionValue) || !string.IsNullOrEmpty(solarRadiationValue)) { resultTxt.Append("<record>" + $"<from>{thisReading?.From}</from>" + $"<to>{thisReading?.To}</to>{precipitationValue}{temperatureValue}{humidityValue}{pressureValue}{windDirectionValue}{windVelocityValue}{solarRadiationValue}</record>{Environment.NewLine}"); } } } return(resultTxt.ToString()); }
public string Read(bool bWrap) { _currentContext = new SQLAzureDataContext(); var meterId = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "elementId") != 0 ? _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value : string.Empty; var start = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "start") ? DateTime.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value) : DateTime.MinValue; var end = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "end") ? DateTime.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value) : DateTime.MaxValue; long timestep = _currentRequestManager.RootElements.Any(kvp => kvp.Key == "timeStep") ? Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var resultTxt = new StringBuilder(); var elementText = "<elementId>" + meterId + "</elementId>"; var site = _currentContext.Meters.FirstOrDefault(x => x.MeterIdentity == meterId)?.DMA.Site; if (!string.IsNullOrEmpty(meterId) && site != null) { var thisContext = new MeterReadingTableStorageContext(site.TableName); if (timestep == -1) { var finalFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, meterId); if (start != DateTime.MinValue) { var date1 = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, start.Ticks.ToString()); finalFilter = TableQuery.CombineFilters(finalFilter, TableOperators.And, date1); } if (end != DateTime.MaxValue) { var date2 = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, end.Ticks.ToString()); finalFilter = TableQuery.CombineFilters(finalFilter, TableOperators.And, date2); } var allRecords = thisContext.MeterReadingsFiltered(finalFilter); foreach (var record in allRecords) { resultTxt.Append("<record>" + $"<time>{record.CreatedOn.ToString("yyyy-MM-ddTHH:mm:sszzz")}</time><variable name=\"flow\">" + $"<value>{record.Reading}</value></variable>" + $"</record>{Environment.NewLine}"); } } else { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End var dateTimeBlockSet = new List <DateTimeBlock>(); for (var blockIndex = 0; blockIndex < numberOfBlock; blockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(blockIndex * timestepTicks), To = start.AddTicks((blockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } // loop through each block and and find any records that span that block, trimming those on the edges foreach (var thisBlock in dateTimeBlockSet) { var recordsSpanningBlock = new List <MeterReadingEntity>(); var finalFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, meterId); if (start != DateTime.MinValue) { var date1 = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, thisBlock.From.Ticks.ToString()); finalFilter = TableQuery.CombineFilters(finalFilter, TableOperators.And, date1); } if (end != DateTime.MaxValue) { var date2 = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, thisBlock.To.Ticks.ToString()); finalFilter = TableQuery.CombineFilters(finalFilter, TableOperators.And, date2); } recordsSpanningBlock.AddRange(thisContext.MeterReadingsFiltered(finalFilter)); if (recordsSpanningBlock.Any()) { // var minResult = recordsSpanningBlock.Min(x => x.Reading); // var maxResult = recordsSpanningBlock.Max(x => x.Reading); var minResult = float.Parse(recordsSpanningBlock.Min(x => x.Reading)); var maxResult = float.Parse(recordsSpanningBlock.Max(x => x.Reading)); var averageValue = (minResult + maxResult) / 2; // if (!string.IsNullOrEmpty(minResult) && !string.IsNullOrEmpty(maxResult)) { // var flow = Int32.Parse(maxResult) - Int32.Parse(minResult); resultTxt.Append("<record>" + $"<time>{thisBlock.From.ToString("yyyy-MM-ddTHH:mm:sszzz")}</time><variable name=\"flow\">" + $"<value>{averageValue}</value></variable>" + $"</record>{Environment.NewLine}"); } } } } } if (bWrap) { return("<response>" + $"<recordSet>{elementText}{resultTxt}</recordSet>" + "</response>"); } else { return(resultTxt.ToString()); } }
public string Read() { _currentContext = new SQLAzureDataContext(); var elementId = _currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "elementId").Value; var start = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "start").Value); var end = DateTimeOffset.Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "end").Value); long timestep = _currentRequestManager.RootElements.Count(kvp => kvp.Key == "timeStep") > 0 ? Parse(_currentRequestManager.RootElements.FirstOrDefault(kvp => kvp.Key == "timeStep").Value) : -1; var allResults = _currentContext.GetDemandPredictionData(start, end, elementId).ToList(); if (timestep != -1 && allResults.Any()) { const long multiplier = 10000000; long timestepTicks = timestep * multiplier; long numberOfTicksBetweenStartAndEnd = (long)(end.Ticks - start.Ticks); int numberOfBlock = (int)(numberOfTicksBetweenStartAndEnd / timestepTicks); // Create a collection of fixed blocks in time from supplied Start to End var dateTimeBlockSet = new List <DateTimeBlock>(); for (var blockIndex = 0; blockIndex < numberOfBlock; blockIndex++) { var newDateTimeBlock = new DateTimeBlock { From = start.AddTicks(blockIndex * timestepTicks), To = start.AddTicks((blockIndex + 1) * timestepTicks) }; dateTimeBlockSet.Add(newDateTimeBlock); } var subResults = new List <GetDemandPredictionDataResult>(); // loop through each block and and find any records that span that block, trimming those on the edges foreach (var thisBlock in dateTimeBlockSet) { var firstRecordkInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).FirstOrDefault(); var lastRecordInSet = allResults.Where(x => x.From <thisBlock.To && x.To> thisBlock.From).OrderBy(x => x.From).LastOrDefault(); if (firstRecordkInSet != null && lastRecordInSet != null) { var recordsSpanningBlock = allResults.Where(x => x.From >= firstRecordkInSet.From && x.To <= lastRecordInSet.To); var getDemandPredictionDataResults = recordsSpanningBlock as GetDemandPredictionDataResult[] ?? recordsSpanningBlock.ToArray(); if (getDemandPredictionDataResults.Any()) { var trimmedrecordsSpanningBlock = new List <GetDemandPredictionDataResult>(); foreach (var record in getDemandPredictionDataResults) { double percentageToTrim = 0; if (record.From < thisBlock.From) { var currentBlockTicks = record.To.Ticks - record.From.Ticks; var ticksToTrim = thisBlock.From.Ticks - record.From.Ticks; percentageToTrim += (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } if (record.To > thisBlock.To) { var currentBlockTicks = record.To.Ticks - record.From.Ticks; var ticksToTrim = record.To.Ticks - thisBlock.To.Ticks; percentageToTrim = (double)(((double)100.0 / (double)currentBlockTicks) * (double)ticksToTrim); } record.Demand = record.Demand - ((record.Demand / 100) * percentageToTrim); if (record.Uncertainty != null) { record.Uncertainty = record.Uncertainty - ((record.Uncertainty / 100) * percentageToTrim); } trimmedrecordsSpanningBlock.Add(record); } var demand = 0.0; double?uncertainty = null; foreach (var reading in trimmedrecordsSpanningBlock) { demand += reading.Demand; if (reading.Uncertainty != null) { if (uncertainty == null) { uncertainty = reading.Uncertainty; } else if (reading.Uncertainty != -1) { uncertainty += reading.Uncertainty; } } } var modifiedReading = trimmedrecordsSpanningBlock.FirstOrDefault(); if (modifiedReading != null) { modifiedReading.Demand = demand; if (uncertainty != null) { modifiedReading.Uncertainty = uncertainty; } modifiedReading.From = thisBlock.From; modifiedReading.To = thisBlock.To; subResults.Add(modifiedReading); } } } } allResults = subResults; } var resultTxt = new StringBuilder(); var baseTime = string.Empty; var creationTime = string.Empty; var generatedBy = string.Empty; var comment = string.Empty; foreach (var thisReading in allResults) { if (baseTime == string.Empty) { baseTime = thisReading.BaseTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK"); creationTime = thisReading.CreationTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK"); } generatedBy = thisReading.Identifier; var demandValue = "<variable name=\"demand\">" + $"<value>{thisReading.Demand}</value>" + "</variable>"; var uncertaintyValue = thisReading.Uncertainty != null ? "<variable name=\"uncertainty\">" + $"<value>{thisReading.Uncertainty}</value>" + "</variable>" : null; resultTxt.Append("<record>" + $"<from>{thisReading?.From.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</from>" + $"<to>{thisReading?.To.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK")}</to>{demandValue}{uncertaintyValue}</record>{Environment.NewLine}"); } var dmaName = string.Empty; if (resultTxt.Length > 0) { var firstOrDefault = _currentContext.DMAs.FirstOrDefault(x => x.Identifier == elementId); if (firstOrDefault != null) { dmaName = firstOrDefault.Name; } } return("<response>" + "<recordSet>" + $"<elementId>{elementId}</elementId>" + $"<name>{dmaName}</name>" + $"<metadata baseTime=\"{baseTime}\" creationTime=\"{creationTime}\" generatedBy=\"{generatedBy}\" comment=\"{comment}\" />{resultTxt}</recordSet>" + "</response>"); }