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));
program.StartCalculations(
commandLineArguments.DamXFileName,
commandLineArguments.TimeSeriesInputFileName,
commandLineArguments.TimeSeriesOutputFileName,
commandLineArguments.ParameterFileName,
commandLineArguments.Filter);
}
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);
}
Logger.LogInfo("v1.0 - Deltares 2014");
Logger.LogInfo("Model runner started");
Console.WriteLine();
var modelRunner = new ModelRunner
{
ParametersFile = new FileInfo(calculationParametersFile),
FewsInputFile = new FileInfo(fewsInputFile),
FewsOutputFile = new FileInfo(fewsOutputFile),
DamXFile = new FileInfo(damXFile),
Filter = filter,
RunnerDelegate = new SensorModelRunner()
};
modelRunner.Run();
Logger.LogInfo("Model runner ended");
}
}
}