// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// 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.Collections.Generic;
using System.IO;
using System.Linq;
using Deltares.Dam.StixFileReader;
using Deltares.Geotechnics.Soils;
using Deltares.Standard.Language;
namespace Deltares.Dam.Data.Geometry2DImporter;
///
/// Importer to import .
///
public static class SoilProfile2DImporter
{
///
/// Imports the that are contained within .
///
/// The directory to retrieve the soil profiles from.
/// The to import the soil profiles for.
/// The that contains all the valid soil materials.
/// The collection of that is imported from csv.>
///
/// A collection of .
/// Thrown when or is null.
///
/// Thrown when
/// is null, empty or consist of only whitespaces.
///
/// Thrown when the soil profiles could not be successfully imported.
public static IEnumerable Import(string soilProfileDirectory, Segment segment, SoilList availableSoils,
IEnumerable aquifers, double xSoilGeometry2DOrigin = 0.0)
{
if (segment == null)
{
throw new ArgumentNullException(nameof(segment));
}
if (availableSoils == null)
{
throw new ArgumentNullException(nameof(availableSoils));
}
if (string.IsNullOrWhiteSpace(soilProfileDirectory))
{
throw new ArgumentException($"'{nameof(soilProfileDirectory)}' cannot be null or consist of whitespaces only.");
}
// Extract all soil profile 2D from a segment
IEnumerable soilProfiles = segment.SoilProfileProbabilities.Where(sp => sp.SoilProfileType == SoilProfileType.SoilProfile2D);
var importedSoilProfiles = new List();
foreach (SoilGeometryProbability profile in soilProfiles)
{
string profileName = profile.SoilGeometry2DName;
SoilProfile2D importedSoilProfile = ReadSoilProfile(soilProfileDirectory, profileName, aquifers, xSoilGeometry2DOrigin);
foreach (string soilName in importedSoilProfile.Surfaces.Select(surface => surface.Soil.Name))
{
if (!IsSoilOfSurfacePresent(availableSoils.Soils, soilName))
{
string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorUndefinedMaterials");
throw new SoilProfileImporterException(string.Format(messageFormat, profileName, soilName));
}
}
TransferSoilLayerProperties(importedSoilProfile.Surfaces);
importedSoilProfiles.Add(importedSoilProfile);
}
return importedSoilProfiles;
}
///
/// Reads a soil profile.
///
/// The directory to read the from.
/// The name of the soil profile.
/// The collection of that is imported from csv.>
///
/// A .
/// Thrown when the soil profile could not be read.
private static SoilProfile2D ReadSoilProfile(string soilProfileDirectory, string soilProfileName,
IEnumerable aquifers, double xSoilGeometry2DOrigin)
{
string soilProfileFileName = StixFileReaderHelper.FetchFileNameWithStixExtension(soilProfileName);
string filePath = Path.Combine(soilProfileDirectory, soilProfileFileName);
if (File.Exists(filePath))
{
filePath = Path.Combine(soilProfileDirectory, soilProfileFileName);
try
{
SoilProfile2D readSoilProfile = null;
var reader = new StixFileReader.StixFileReader();
readSoilProfile = reader.ReadSoilProfile(filePath, xSoilGeometry2DOrigin);
TransferAquiferData(soilProfileFileName, readSoilProfile.Surfaces, aquifers);
return readSoilProfile;
}
catch (StixFileReadException e)
{
string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed");
throw new SoilProfileImporterException(string.Format(messageFormat, soilProfileName, e.Message), e);
}
}
string message = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed");
throw new SoilProfileImporterException(string.Format(message, soilProfileName, string.Empty));
}
private static bool IsSoilOfSurfacePresent(IEnumerable availableSoils, string soilName)
{
var soilNames = new HashSet(availableSoils.Select(s => s.Name));
return soilNames.Contains(soilName, StringComparer.OrdinalIgnoreCase);
}
private static void TransferSoilLayerProperties(IEnumerable soilLayers)
{
foreach (SoilLayer2D layer in soilLayers)
{
layer.WaterpressureInterpolationModel = WaterpressureInterpolationModel.Hydrostatic;
}
}
private static void TransferAquiferData(string soilProfileFileName, IEnumerable soilLayers, IEnumerable aquifers)
{
if (aquifers != null && aquifers.Any())
{
List aquiferList = aquifers.ToList();
foreach (SoilLayer2D layer in soilLayers)
{
Aquifer aquifer = aquiferList.Find(a => a.StixFileName == soilProfileFileName && a.LayerName == layer.GeometrySurface.Name);
if (aquifer != null)
{
layer.IsAquifer = true;
}
}
}
}
}