using System.Diagnostics;
using System.IO;
using System.Linq;
using Deltares.DamEngine.Calculators.KernelWrappers.Common;
using Deltares.DamEngine.Calculators.Uplift;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.Geometry;
using Deltares.DamEngine.Data.Geotechnics;
namespace Deltares.DamEngine.Calculators.KernelWrappers.Interfaces
{
///
/// Defines the interface between DAM and python scripts
///
public class PythonInterface
{
public string PythonInformationPath = Directory.GetCurrentDirectory() + "//PythonInformation";
public string PythonPath = "";
public string PythonScriptsLocation = "";
public bool AddForbiddenLine = false;
public bool AddMaxXEntrance = false;
///
/// Finds the path where the python exe is stored. Where the path location is read from a txt file
///
private void GetPythonInformation()
{
if (File.Exists(PythonInformationPath))
{
System.IO.StreamReader pythonInfoFile =
new System.IO.StreamReader(PythonInformationPath);
PythonPath = pythonInfoFile.ReadLine();
PythonScriptsLocation = pythonInfoFile.ReadLine();
string addForbiddenLineString = pythonInfoFile.ReadLine();
string addMaxXEntranceString = pythonInfoFile.ReadLine();
AddForbiddenLine = addForbiddenLineString.Split(' ').Last().Equals("1");
AddMaxXEntrance = addMaxXEntranceString.Split(' ').Last().Equals("1");
}
}
///
/// Add a forbidden line and fill in the maxXentrance in the sti file
///
///
///
private void AddForbiddenLineToSti(DamKernelInput damKernelInput, string stiFileName)
{
GeometryPoint forbiddenLinePoint1;
GeometryPoint forbiddenLinePoint2;
if (damKernelInput.DamFailureMechanismeCalculationSpecification.FailureMechanismSystemType ==
FailureMechanismSystemType.StabilityOutside)
{
forbiddenLinePoint1 =
damKernelInput.Location.SurfaceLine.CharacteristicPoints.GetGeometryPoint(
CharacteristicPointType.NonWaterRetainingObjectPoint4);
forbiddenLinePoint2 =
damKernelInput.Location.SurfaceLine.CharacteristicPoints.GetGeometryPoint(
CharacteristicPointType.NonWaterRetainingObjectPoint3);
}
else
{
forbiddenLinePoint1 =
damKernelInput.Location.SurfaceLine.CharacteristicPoints.GetGeometryPoint(
CharacteristicPointType.NonWaterRetainingObjectPoint1);
forbiddenLinePoint2 =
damKernelInput.Location.SurfaceLine.CharacteristicPoints.GetGeometryPoint(
CharacteristicPointType.NonWaterRetainingObjectPoint2);
}
if (forbiddenLinePoint1 != null && forbiddenLinePoint2 != null)
{
if (damKernelInput.SubSoilScenario.SoilProfileType == SoilProfileType.ProfileType1D)
{
forbiddenLinePoint2.Z = damKernelInput.SubSoilScenario.SoilProfile1D.TopAquiferLayer.TopLevel;
}
if (damKernelInput.SubSoilScenario.SoilProfileType == SoilProfileType.ProfileType2D)
{
forbiddenLinePoint2.Z = damKernelInput.SubSoilScenario.SoilProfile2D.GetSoilProfile1D(forbiddenLinePoint2.X)
.TopAquiferLayer.TopLevel;
}
stiFileName = '"' + stiFileName + '"';
string pythonScriptName ='"'+ PythonScriptsLocation+"//AddForbiddenLine.py"+'"';
Process process = new Process();
process.StartInfo.FileName = PythonPath;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = pythonScriptName + " " +
forbiddenLinePoint1.X + " " + forbiddenLinePoint2.X + " " + forbiddenLinePoint1.Z + " " + forbiddenLinePoint2.Z + " " + stiFileName; // start the python program
process.Start();
process.WaitForExit();
}
}
private void DefineMaxXEntranceInSti(DamKernelInput damKernelInput, string stiFileName)
{
string isStabilityOutside = "False";
GeometryPoint dikeTop;
if (damKernelInput.DamFailureMechanismeCalculationSpecification.FailureMechanismSystemType ==
FailureMechanismSystemType.StabilityOutside)
{
dikeTop = damKernelInput.Location.SurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeTopAtRiver);
isStabilityOutside = "True";
}
else
{
dikeTop = damKernelInput.Location.SurfaceLine.CharacteristicPoints.GetGeometryPoint(CharacteristicPointType.DikeTopAtPolder);
}
if (dikeTop != null)
{
stiFileName = '"' + stiFileName + '"';
string pythonScriptName ='"'+ PythonScriptsLocation+ "//DefineXEntryMax.py"+'"';
Process process = new Process();
process.StartInfo.FileName = PythonPath;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = pythonScriptName + " " + dikeTop.X + " " + isStabilityOutside + " " + stiFileName; // start the python program
process.Start();
process.WaitForExit();
}
}
///
/// Perform all required actions in python scripts
///
///
///
public void PerformPythonActions(DamKernelInput damKernelInput, string stiFileName="")
{
GetPythonInformation();
if (AddForbiddenLine)
{
AddForbiddenLineToSti(damKernelInput, stiFileName);
}
if (AddMaxXEntrance)
{
DefineMaxXEntranceInSti(damKernelInput, stiFileName);
}
}
///
/// Delete the python information
///
public void ClearPythonActions()
{
if (File.Exists(PythonInformationPath))
{
File.Delete(PythonInformationPath);
}
}
}
}