// Copyright (C) Stichting Deltares 2020. All rights reserved.
//
// This file is part of the Layer On Slope Tool.
//
// The Layer On Slope Tool 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 Deltares.LayerOnSlopeTool.Data;
using Deltares.LayerOnSlopeTool.Importer;
using Deltares.LayerOnSlopeTool.StiFileCreator;
namespace Deltares.LayerOnSlopeTool.LayerCreator
{
///
public class LayerCreator
{
private List locations = new List();
private List surfaceLines = new List();
private List errors = new List();
private bool errorFound;
private string fullLogFilePath;
public string LogFileName = "LayerOnSlopeTool.log";
/// Initializes a new instance of the class.
/// Name of the input folder.
/// Name of the geometries folder.
/// Name of the output folder.
///
public LayerCreator(string inputFolderName, string geometriesFolderName, string outputFolderName, string logFileName)
{
InputFolderName = inputFolderName;
GeometriesFolderName = geometriesFolderName;
OutputFolderName = outputFolderName;
if (!string.IsNullOrEmpty(logFileName))
{
LogFileName = logFileName;
}
fullLogFilePath = Path.Combine(OutputFolderName, LogFileName);
}
/// Gets the name of the output folder.
/// The name of the output folder.
public string OutputFolderName { get; }
/// Gets the name of the geometries folder.
/// The name of the geometries folder.
public string GeometriesFolderName { get;}
/// Gets the name of the input folder.
/// The name of the input folder.
public string InputFolderName { get; }
///
/// Executes this instance.
///
public void Execute()
{
PrepareOutputFolder();
errorFound = false;
locations = ReadLocations(InputFolderName);
if (locations != null && locations.Count > 0)
{
surfaceLines = ReadSurfaceLines(InputFolderName);
if (surfaceLines != null && surfaceLines.Count > 0)
{
ProcessLocations();
}
}
if (errorFound)
{
throw new Exception(String.Format("Error(s) found, see log file {0}.", fullLogFilePath));
}
}
private void ProcessLocations()
{
errors.Clear();
var loggedLocations = new List();
foreach (var location in locations)
{
loggedLocations.Clear();
loggedLocations.Add(string.Format("Handling location {0} with surface line {1}", location.LocationId, location.SurfacelineId));
WriteToLog(loggedLocations);
SurfaceLine surfaceLine = FindSurfaceLine(location.SurfacelineId);
var loweredSurfaceLine = DetermineSurfaceLineForGivenLayerData(surfaceLine, location.LayerThickness);
try
{
CreateGeometryWithLoweredSurfaceLine(loweredSurfaceLine, location);
CreateGeometryWithOriginalSurfaceLine(surfaceLine, location);
}
catch (Exception e)
{
errorFound = true;
errors.Add(e.Message);
}
}
if (errors.Count > 0)
{
WriteToLog(errors);
}
}
private void PrepareOutputFolder()
{
if (!Directory.Exists(OutputFolderName))
{
Directory.CreateDirectory(OutputFolderName);
}
else
{
if (File.Exists(fullLogFilePath))
{
File.Delete(fullLogFilePath);
}
}
}
private SurfaceLine FindSurfaceLine(string surfaceLineId)
{
foreach (var surfaceLine in surfaceLines)
{
if (surfaceLine.SurfaceLineId == surfaceLineId)
{
return surfaceLine;
}
}
return null;
}
private List ReadSurfaceLines(string inputFolderName)
{
errors.Clear();
if (CsvImporter.ReadSurfaceLines(inputFolderName, out surfaceLines, out errors))
{
return surfaceLines;
}
WriteToLog(errors);
errorFound = true;
return null;
}
private List ReadLocations(string inputFolderName)
{
errors.Clear();
if (CsvImporter.ReadLocations(inputFolderName, out locations, out errors))
{
return locations;
}
WriteToLog(errors);
errorFound = true;
return null;
}
private void WriteToLog(List linesToAdd)
{
File.AppendAllLines(fullLogFilePath, linesToAdd);
}
private void CreateGeometryWithLoweredSurfaceLine(SurfaceLine surfaceLine, Location location)
{
var stiFileCreatorInput = new StiFileCreatorInput
{
XOffset = location.XOffset,
SurfaceLine = surfaceLine,
DikeMaterialName = location.LayerMaterial,
InputFilename = Path.Combine(InputFolderName, location.SoilGeometryName),
OutputFilename = Path.Combine(OutputFolderName, "Interim" + location.SoilGeometryName)
};
StiFileCreator.StiFileCreator.ProcessFile(stiFileCreatorInput);
}
private void CreateGeometryWithOriginalSurfaceLine(SurfaceLine surfaceLine, Location location)
{
var stiFileCreatorInput = new StiFileCreatorInput
{
// Note that for second generation the offset MUST be 0 as any offset is already taken care of in the first generation.
XOffset = 0,
SurfaceLine = surfaceLine,
DikeMaterialName = location.LayerMaterial,
InputFilename = Path.Combine(OutputFolderName, "Interim" + location.SoilGeometryName),
OutputFilename = Path.Combine(OutputFolderName, location.SoilGeometryName)
};
StiFileCreator.StiFileCreator.ProcessFile(stiFileCreatorInput);
}
private SurfaceLine DetermineSurfaceLineForGivenLayerData(SurfaceLine originalSurfaceLine, double layerThickness)
{
var surfaceLineProcessor = new SurfacelineProcessor(originalSurfaceLine);
return surfaceLineProcessor.CreateLoweredSurfaceLine(layerThickness);
}
}
}