// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the application DAM - Live.
//
// DAM - Live is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Deltares.DamLive.Application;
using Deltares.Dam.Data;
using Deltares.Standard.IO;
using NUnit.Framework;
namespace Deltares.DamLive.TestHelper;
public class GeneralHelper
{
public const int CMinCores = 1;
public const int CMaxCores = 20;
public static void SetupIntegrationProject(int maxCores, string testWorkingFolder, string testIntegrationDataFolder,
string input1aFilename, string output1aFilename, string testFileName,
string calculationParametersIntegrationFilename, string projectIntegrationFilename,
out DamEngineRunner runner, out List locations)
{
string postfix = maxCores == 0 ? "" : maxCores.ToString();
SetupIntegrationProject(postfix, testWorkingFolder, testIntegrationDataFolder, input1aFilename, output1aFilename,
testFileName, calculationParametersIntegrationFilename, projectIntegrationFilename, out runner, out locations);
}
public static void SetupIntegrationProject(string testWorkingFolder, string testIntegrationDataFolder,
string projectIntegrationFilename,
out DamEngineRunner runner, out List locations)
{
const string inputFilename = @"live.InputTimeSeries.xml";
const string outputFilename = @"live.OutputTimeSeries.xml";
const string parametersFilename = @"live.ParametersFile.xml";
SetupIntegrationProject("", testWorkingFolder, testIntegrationDataFolder, inputFilename, outputFilename,
"test", parametersFilename, projectIntegrationFilename, out runner, out locations);
}
public static void SetupIntegrationProject(string postfix, string testWorkingFolder, string testIntegrationDataFolder,
string input1aFilename, string output1aFilename, string testFileName,
string calculationParametersIntegrationFilename, string projectIntegrationFilename,
out DamEngineRunner runner, out List locations)
{
string actualTestPath = Path.GetFullPath(testWorkingFolder + postfix);
Directory.CreateDirectory(actualTestPath);
DirectoryHelper.CopyRecursive(actualTestPath, testIntegrationDataFolder);
string inputFile = Path.Combine(actualTestPath, input1aFilename);
string outputFile = Path.Combine(actualTestPath, output1aFilename);
string parameterFile = Path.Combine(actualTestPath, calculationParametersIntegrationFilename);
string projectFile = Path.Combine(actualTestPath, projectIntegrationFilename);
string testFile = Path.Combine(actualTestPath, testFileName);
DamProject.SetTestProgramVersion("24.1");
DamProjectData damData = DamProject.LoadData(projectFile);
locations = damData.Locations.ToList();
// Load the sensor time serie data (see input file for details)
TimeSerieCollection inputTimeSeries = TimeSerieCollection.LoadFromFile(inputFile);
//ShortenTimeSeries(inputTimeSeries);
inputTimeSeries.Save(Path.ChangeExtension(inputFile, "short.xml"));
runner = new DamEngineRunner
{
DamXFile = new FileInfo(projectFile),
ParametersFile = new FileInfo(parameterFile),
FewsInputFile = new FileInfo(inputFile),
FewsOutputFile = new FileInfo(outputFile),
Filter = "",
InputTimeSeriesCollection = inputTimeSeries,
TestFileName = testFile
};
}
public static void InitializeRunner(DamEngineRunner runner, int maxCores)
{
runner.Initialize();
runner.DamProjectData.MaxCalculationCores = maxCores;
Assert.That(runner, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(runner.DamProjectData, Is.Not.Null);
});
}
public static void AssertNoErrors(DamEngineRunner runner)
{
if (runner.HasErrors)
{
Assert.Fail("The test failed. See the error log in the test output console for more info");
}
}
///
/// Make timeserie shorter for testing: the first, the last and the middle entry are only used
///
///
private static void ShortenTimeSeries(TimeSerieCollection inputTimeSeries) // ToDo the: Currently not used, but keep for future use
{
if (inputTimeSeries?.Series[0] != null && inputTimeSeries.Series[0].Entries.Count > 3)
{
double entriesCount = inputTimeSeries.Series[0].Entries.Count;
var middleIndex = (int)Math.Round(entriesCount / 2);
foreach (TimeSerie serie in inputTimeSeries.Series)
{
for (int i = serie.Entries.Count - 2; i > 0; i--)
{
if (i != middleIndex) // keep the time entry in the middle of the serie
{
serie.Entries.RemoveAt(i);
}
}
}
}
}
}