// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the application DAM - Live.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU 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.Authorization;
using Deltares.Dam.Data;
using Deltares.Standard.Application;
using log4net.Config;
namespace Deltares.Dam.Application.Live
{
internal class Program
{
internal readonly static LogHelper Logger = LogHelper.Create("DamLive");
private static DAuthClient dAuthClient;
private const string dAuthFeature = "DGS_27_00";
private const string dAuthVersion = "17.0";
static void Main(string[] args)
{
var damLicenseType = DamLicenseType.None;
// new authorization client ?
dAuthClient = new DAuthClient(false, true);
if (dAuthClient.CheckOut(dAuthFeature, dAuthVersion))
{
damLicenseType = DamLicenseType.LFM;
}
XmlConfigurator.Configure();
try
{
if (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);
}
// check in license
if (dAuthClient != null)
{
dAuthClient.CheckIn(dAuthFeature);
dAuthClient.Dispose();
dAuthClient = null;
}
//Console.ReadKey();
}
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));
Logger.LogInfo("v18.1 - Deltares 2018");
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)
{
var filePath = Path.Combine(path, Path.GetFileName(fileName));
if (!File.Exists(filePath))
{
using (var stream = File.CreateText(filePath))
{
var xml =
"" + Environment.NewLine +
"" +
Environment.NewLine +
"\t0 " + Environment.NewLine +
"";
stream.Write(xml);
}
}
}
///
/// 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);
var 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,
};
damEngineRunner.Run();
}
}
}