// 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.IO;
using CommandLine;
using Deltares.Dam.Data;
using Deltares.Dam.Data.License;
using Deltares.Standard.Application;
using Deltares.Standard.Reflection;
using log4net.Config;
namespace Deltares.DamLive.Application;
internal class Program
{
internal static readonly LogHelper Logger = LogHelper.Create("DamLive");
private const string dAuthFeature = "DGS_27_00";
static readonly AssemblyInfoHelper info = new AssemblyInfoHelper(typeof(Program));
///
/// Starts the calculations.
///
/// The path of the .damx file
/// The FEWS input file.
/// The FEWS output file.
/// Settings of calculation parameters
/// Filter to specify locations to handle
internal void StartCalculations(string damXFile, string fewsInputFile, string fewsOutputFile, string calculationParametersFile = "", string filter = "")
{
if (string.IsNullOrWhiteSpace(damXFile))
{
throw new ArgumentException("damXFile");
}
if (string.IsNullOrWhiteSpace(fewsInputFile))
{
throw new ArgumentException("fewsOutputFile");
}
if (string.IsNullOrWhiteSpace(fewsOutputFile))
{
throw new ArgumentException("fewsExportFile");
}
if (!File.Exists(damXFile))
{
throw new FileNotFoundException(damXFile);
}
string workDir = Path.GetDirectoryName(damXFile) ?? ".\\";
if (!File.Exists(fewsInputFile))
{
fewsInputFile = Path.Combine(workDir, fewsInputFile);
if (!File.Exists(fewsInputFile))
{
throw new FileNotFoundException(fewsInputFile);
}
}
if (!File.Exists(fewsOutputFile))
{
fewsOutputFile = Path.Combine(workDir, fewsOutputFile);
if (!File.Exists(fewsOutputFile))
{
throw new FileNotFoundException(fewsOutputFile);
}
}
if (!File.Exists(calculationParametersFile))
{
calculationParametersFile = Path.Combine(workDir, calculationParametersFile);
if (!File.Exists(calculationParametersFile))
{
throw new FileNotFoundException(calculationParametersFile);
}
}
var damEngineRunner = new DamEngineRunner
{
ParametersFile = new FileInfo(calculationParametersFile),
FewsInputFile = new FileInfo(fewsInputFile),
FewsOutputFile = new FileInfo(fewsOutputFile),
DamXFile = new FileInfo(damXFile),
Filter = filter
};
DamProject.SetTestProgramVersion("19.1");
damEngineRunner.Run();
}
static void Main(string[] args)
{
DamLicense.CheckoutLicense(dAuthFeature, info.AssemblyVersion);
XmlConfigurator.Configure();
try
{
if (DamLicense.DamLicenseType == DamLicenseType.None)
{
throw new Exception("No license found for DamLive");
}
Run(args);
}
catch (Exception e)
{
Console.WriteLine();
Logger.LogFatal("There was an unexpected error. Program terminated", e);
}
DamLicense.CheckinLicense();
}
static void Run(string[] args)
{
var commandLineArguments = new CommandOptions();
ICommandLineParser parser = new CommandLineParser();
bool success = parser.ParseArguments(args, commandLineArguments);
if (success)
{
var program = new Program();
CreateOutputTimeSeriesFileIfNeeded(
commandLineArguments.TimeSeriesOutputFileName,
Path.GetDirectoryName(commandLineArguments.TimeSeriesOutputFileName));
var thisAssembly = new AssemblyInfoHelper(typeof(Program));
Logger.LogInfo(thisAssembly.Title + " " + thisAssembly.AssemblyVersion);
Logger.LogInfo(thisAssembly.Copyright);
Logger.LogInfo("Model runner started");
Console.WriteLine();
program.StartCalculations(
commandLineArguments.DamXFileName,
commandLineArguments.TimeSeriesInputFileName,
commandLineArguments.TimeSeriesOutputFileName,
commandLineArguments.ParameterFileName,
commandLineArguments.Filter);
Console.WriteLine();
Logger.LogInfo("Model runner ended");
}
else
{
Console.WriteLine();
Console.WriteLine(commandLineArguments.GetUsage());
}
}
private static void CreateOutputTimeSeriesFileIfNeeded(string fileName, string path)
{
string filePath = Path.Combine(path, Path.GetFileName(fileName));
if (!File.Exists(filePath))
{
using (StreamWriter stream = File.CreateText(filePath))
{
string xml =
"" + Environment.NewLine +
"" +
Environment.NewLine +
"\t0 " + Environment.NewLine +
"";
stream.Write(xml);
}
}
}
}