// Copyright (C) Stichting Deltares 2020. All rights reserved.
//
// This file is part of the application DAM - Clients Library.
//
// 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.Geotechnics.Soils;
using Deltares.Standard.Language;
namespace Deltares.Dam.Data.StiImporter
{
///
/// 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.
/// 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)
{
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
var soilProfiles = segment.SoilProfileProbabilities
.Where(sp => sp.SoilProfileType == SoilProfileType.SoilGeometryStiFile);
var importedSoilProfiles = new List();
foreach (SoilGeometryProbability profile in soilProfiles)
{
string profileName = profile.SoilGeometry2DName;
SoilProfile2D readSoilProfile = ReadSoilProfile(soilProfileDirectory, profileName);
if (!IsValidSoilProfile(availableSoils, readSoilProfile))
{
string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorUndefinedMaterials");
throw new SoilProfileImporterException(string.Format(messageFormat, profileName));
}
TransferSoilLayerProperties(availableSoils, readSoilProfile.Surfaces);
importedSoilProfiles.Add(readSoilProfile);
}
return importedSoilProfiles;
}
///
/// Reads a soil profile.
///
/// The directory to read the from.
/// The name of the soil profile.
/// A .
/// Thrown when the soil profile could not be read.
private static SoilProfile2D ReadSoilProfile(string soilProfileDirectory, string soilProfileName)
{
string soilProfileFileName = HasStiFileExtension(soilProfileName) ? soilProfileName : $"{soilProfileName}.sti";
string filePath = Path.Combine(soilProfileDirectory, soilProfileFileName);
try
{
var reader = new StiFileReader();
SoilProfile2D readSoilProfile = reader.ReadSoilProfile(filePath);
readSoilProfile.Name = soilProfileName;
return readSoilProfile;
}
catch (StiFileReadException e)
{
string messageFormat = LocalizationManager.GetTranslatedText(typeof(SoilProfile2DImporter), "ImportSoilProfileErrorReadFailed");
throw new SoilProfileImporterException(string.Format(messageFormat, soilProfileName, e.Message), e);
}
}
private static bool HasStiFileExtension(string profileName)
{
return string.Equals(Path.GetExtension(profileName), ".sti", StringComparison.OrdinalIgnoreCase);
}
private static bool IsValidSoilProfile(SoilList availableSoils, SoilProfile2D profile)
{
var soilNames = new HashSet(availableSoils.Soils.Select(s => s.Name));
return profile.Surfaces.All(soilLayer => soilNames.Contains(soilLayer.Name));
}
private static void TransferSoilLayerProperties(SoilList availableSoils, IEnumerable soilLayers)
{
foreach (SoilLayer2D layer in soilLayers)
{
Soil soil = availableSoils.Soils.Single(s => string.Equals(s.Name, layer.Name));
layer.IsAquifer = availableSoils.AquiferDictionary[soil];
layer.WaterpressureInterpolationModel = WaterpressureInterpolationModel.Hydrostatic;
}
}
}
}