From dd41b82837ea8ff753845ff40f37a5048f5c0c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steinar=20Elgs=C3=A6ter?= Date: Wed, 27 Nov 2024 09:39:47 +0100 Subject: [PATCH] - fix an issue where loading TimeSeriesData from csvtext in some instances caused unhandled exceptions, now check input and return status flag --- .../Tests/PlantSimulatorSerialization.cs | 4 ++- TimeSeriesAnalysis.csproj | 2 +- TimeSeriesAnalysis/TimeSeriesDataSet.cs | 33 +++++++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorSerialization.cs b/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorSerialization.cs index d8698125..b0e9062d 100644 --- a/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorSerialization.cs +++ b/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorSerialization.cs @@ -97,7 +97,9 @@ public void DeserializedPlantSimulatorAndTimeSeriesDataObjects_AreAbleToSimulate var inputDataJsonTxt = new CsvContent(inputData.ToCsvText()); var inputData2 = new TimeSeriesDataSet(); - inputData2.LoadFromCsv(inputDataJsonTxt); + var isOkData = inputData2.LoadFromCsv(inputDataJsonTxt); + + Assert.IsTrue(isOkData,"uanble to load data from csv"); // 3. deserialize to a new object var plantSim2 = PlantSimulatorSerializer.LoadFromJsonTxt(plantsimJsonTxt); diff --git a/TimeSeriesAnalysis.csproj b/TimeSeriesAnalysis.csproj index 5dbff763..4f8d9404 100644 --- a/TimeSeriesAnalysis.csproj +++ b/TimeSeriesAnalysis.csproj @@ -14,7 +14,7 @@ False https://github.com/equinor/TimeSeriesAnalysis.git readme.md - 1.3.38 + 1.3.39 Equinor Equinor true diff --git a/TimeSeriesAnalysis/TimeSeriesDataSet.cs b/TimeSeriesAnalysis/TimeSeriesDataSet.cs index 250483e0..1e31165a 100644 --- a/TimeSeriesAnalysis/TimeSeriesDataSet.cs +++ b/TimeSeriesAnalysis/TimeSeriesDataSet.cs @@ -225,12 +225,21 @@ public TimeSeriesDataSet Combine(TimeSeriesDataSet inputDataSet) /// - /// Fills a dataset with variables, values and dates + /// Fills a dataset with variables, values and dates, removes "time" or "Time" from variableDict if present, and stores timestamps in internal dateTimes /// /// /// - private void Fill(DateTime[] dateTimes, Dictionary variableDict) + private bool Fill(DateTime[] dateTimes, Dictionary variableDict) { + if (dateTimes == null) + return false; + if (variableDict == null) + return false; + if (variableDict.Keys == null) + return false; + if (variableDict.Keys.Count() == 0) + return false; + if (variableDict.ContainsKey("Time")) { variableDict.Remove("Time"); @@ -239,12 +248,22 @@ private void Fill(DateTime[] dateTimes, Dictionary variableDic { variableDict.Remove("time"); } + N = variableDict[variableDict.Keys.First()].Length; + if (N == 0) + { + return false; + } + // load actual dataset dataset = variableDict; - N = dataset[dataset.Keys.First()].Length; if (dateTimes.Length > 1) { timeStamps = dateTimes.ToList(); } + else + { + return false; + } + return true; } @@ -481,9 +500,9 @@ public void InitNewSignal(string signalName, double initalValue, int N, double n out Dictionary variableDict, dateTimeFormat); if (isOK) { - Fill(dateTimes, variableDict); + return Fill(dateTimes, variableDict); } - return isOK; + return false; } @@ -546,9 +565,9 @@ public void CreateTimestamps(double timeBase_s, DateTime? t0 = null) out Dictionary variableDict, dateTimeFormat); if (isOk) { - Fill(dateTimes, variableDict); + return Fill(dateTimes, variableDict); } - return isOk; + return false; } ///