// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets 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 Core.Common.Base.Data;
using Ringtoets.ClosingStructures.Data;
using Ringtoets.ClosingStructures.IO.Configurations.Helpers;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.DikeProfiles;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Data.Structures;
using Ringtoets.Common.IO.Configurations;
using Ringtoets.Common.IO.Configurations.Helpers;
using Ringtoets.Common.IO.Configurations.Import;
using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources;
namespace Ringtoets.ClosingStructures.IO.Configurations
{
///
/// Class for importing a configuration of from an XML file and storing
/// it on a .
///
public class ClosingStructuresCalculationConfigurationImporter
: CalculationConfigurationImporter
{
private readonly IEnumerable availableHydraulicBoundaryLocations;
private readonly IEnumerable availableForeshoreProfiles;
private readonly IEnumerable availableStructures;
///
/// Create new instance of
///
/// The path to the XML file to import from.
/// The calculation group to update.
/// The hydraulic boundary locations
/// used to check if the imported objects contain the right location.
/// The foreshore profiles used to check if
/// the imported objects contain the right foreshore profile.
/// The structures used to check if
/// the imported objects contain the right structure.
/// Thrown when any parameter is null.
public ClosingStructuresCalculationConfigurationImporter(
string xmlFilePath,
CalculationGroup importTarget,
IEnumerable hydraulicBoundaryLocations,
IEnumerable foreshoreProfiles,
IEnumerable structures)
: base(xmlFilePath, importTarget)
{
if (hydraulicBoundaryLocations == null)
{
throw new ArgumentNullException(nameof(hydraulicBoundaryLocations));
}
if (foreshoreProfiles == null)
{
throw new ArgumentNullException(nameof(foreshoreProfiles));
}
if (structures == null)
{
throw new ArgumentNullException(nameof(structures));
}
availableHydraulicBoundaryLocations = hydraulicBoundaryLocations;
availableForeshoreProfiles = foreshoreProfiles;
availableStructures = structures;
}
protected override ClosingStructuresCalculationConfigurationReader CreateCalculationConfigurationReader(string xmlFilePath)
{
return new ClosingStructuresCalculationConfigurationReader(xmlFilePath);
}
protected override ICalculation ParseReadCalculation(ClosingStructuresCalculationConfiguration readCalculation)
{
var calculation = new StructuresCalculation
{
Name = readCalculation.Name
};
if (TryReadStructure(readCalculation.StructureName, calculation)
&& TryReadHydraulicBoundaryLocation(readCalculation.HydraulicBoundaryLocationName, calculation)
&& TryReadForeshoreProfile(readCalculation.ForeshoreProfileName, calculation)
&& TryReadStochasts(readCalculation, calculation)
&& TryReadOrientation(readCalculation, calculation)
&& TryReadFailureProbabilityStructureWithErosion(readCalculation, calculation)
&& TryReadFailureProbabilityOpenStructure(readCalculation, calculation)
&& TryReadFailureProbabilityReparation(readCalculation, calculation)
&& TryReadProbabilityOrFrequencyOpenStructureBeforeFlooding(readCalculation, calculation)
&& TryReadInflowModelType(readCalculation, calculation)
&& TryReadIdenticalApertures(readCalculation, calculation)
&& readCalculation.WaveReduction.ValidateWaveReduction(calculation.InputParameters.ForeshoreProfile, calculation.Name, Log))
{
ReadFactorStormDurationOpenStructure(readCalculation, calculation);
ReadWaveReductionParameters(readCalculation.WaveReduction, calculation.InputParameters);
return calculation;
}
return null;
}
private bool TryReadStochasts(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (!readCalculation.ValidateStructureBaseStochasts(Log))
{
return false;
}
if (!ValidateStochasts(readCalculation))
{
return false;
}
return TryReadStandardDeviationStochast(
ClosingStructuresConfigurationSchemaIdentifiers.LevelCrestStructureNotClosingStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.LevelCrestStructureNotClosing,
i => i.LevelCrestStructureNotClosing, (i, d) => i.LevelCrestStructureNotClosing = d)
&& TryReadStandardDeviationStochast(
ClosingStructuresConfigurationSchemaIdentifiers.AreaFlowAperturesStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.AreaFlowApertures,
i => i.AreaFlowApertures, (i, d) => i.AreaFlowApertures = d)
&& TryReadStandardDeviationStochast(
ClosingStructuresConfigurationSchemaIdentifiers.DrainCoefficientStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.DrainCoefficient,
i => i.DrainCoefficient, (i, d) => i.DrainCoefficient = d)
&& TryReadStandardDeviationStochast(
ClosingStructuresConfigurationSchemaIdentifiers.InsideWaterLevelStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.InsideWaterLevel,
i => i.InsideWaterLevel, (i, d) => i.InsideWaterLevel = d)
&& TryReadStandardDeviationStochast(
ClosingStructuresConfigurationSchemaIdentifiers.ThresholdHeightOpenWeirStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.ThresholdHeightOpenWeir,
i => i.ThresholdHeightOpenWeir, (i, d) => i.ThresholdHeightOpenWeir = d)
&& TryReadStandardDeviationStochast(
ConfigurationSchemaIdentifiers.AllowedLevelIncreaseStorageStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.AllowedLevelIncreaseStorage,
i => i.AllowedLevelIncreaseStorage, (i, d) => i.AllowedLevelIncreaseStorage = d)
&& TryReadStandardDeviationStochast(
ConfigurationSchemaIdentifiers.FlowWidthAtBottomProtectionStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.FlowWidthAtBottomProtection,
i => i.FlowWidthAtBottomProtection, (i, d) => i.FlowWidthAtBottomProtection = d)
&& TryReadStandardDeviationStochast(
ConfigurationSchemaIdentifiers.ModelFactorSuperCriticalFlowStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.ModelFactorSuperCriticalFlow,
i => i.ModelFactorSuperCriticalFlow, (i, d) => i.ModelFactorSuperCriticalFlow = d)
&& TryReadStandardDeviationStochast(
ConfigurationSchemaIdentifiers.WidthFlowAperturesStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.WidthFlowApertures, i => i.WidthFlowApertures, (i, d) => i.WidthFlowApertures = d)
&& TryReadVariationCoefficientStochast(
ConfigurationSchemaIdentifiers.CriticalOvertoppingDischargeStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.CriticalOvertoppingDischarge,
i => i.CriticalOvertoppingDischarge, (i, d) => i.CriticalOvertoppingDischarge = d)
&& TryReadVariationCoefficientStochast(
ConfigurationSchemaIdentifiers.StorageStructureAreaStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.StorageStructureArea,
i => i.StorageStructureArea, (i, d) => i.StorageStructureArea = d)
&& TryReadVariationCoefficientStochast(
ConfigurationSchemaIdentifiers.StormDurationStochastName,
calculation.Name,
calculation.InputParameters,
readCalculation.StormDuration,
i => i.StormDuration, (i, d) => i.StormDuration = d);
}
private bool ValidateStochasts(ClosingStructuresCalculationConfiguration configuration)
{
if (configuration.DrainCoefficient?.StandardDeviation != null
|| configuration.DrainCoefficient?.VariationCoefficient != null)
{
Log.LogCalculationConversionError(RingtoetsCommonIOResources.CalculationConfigurationImporter_ValidateStochasts_Cannot_define_spread_for_DrainCoefficient,
configuration.Name);
return false;
}
return true;
}
///
/// Reads the orientation.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the orientation is invalid or when there is an orientation but
/// no structure defined, true otherwise.
private bool TryReadOrientation(StructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.StructureNormalOrientation.HasValue)
{
if (calculation.InputParameters.Structure == null)
{
Log.LogCalculationConversionError(string.Format(RingtoetsCommonIOResources.CalculationConfigurationImporter_TryParameter_No_Structure_to_assign_Parameter_0_,
RingtoetsCommonIOResources.CalculationConfigurationImporter_Orientation_DisplayName),
calculation.Name);
return false;
}
double orientation = readCalculation.StructureNormalOrientation.Value;
try
{
calculation.InputParameters.StructureNormalOrientation = (RoundedDouble) orientation;
}
catch (ArgumentOutOfRangeException e)
{
Log.LogOutOfRangeException(string.Format(RingtoetsCommonIOResources.TryReadParameter_Value_0_ParameterName_1_is_invalid,
orientation,
RingtoetsCommonIOResources.CalculationConfigurationImporter_Orientation_DisplayName),
calculation.Name,
e);
return false;
}
}
return true;
}
///
/// Reads the failure probability structure with erosion.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the orientation is invalid or when there is a failure probability
/// structure with erosion but no structure defined, true otherwise.
private bool TryReadFailureProbabilityStructureWithErosion(StructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.FailureProbabilityStructureWithErosion.HasValue)
{
double failureProbability = readCalculation.FailureProbabilityStructureWithErosion.Value;
try
{
calculation.InputParameters.FailureProbabilityStructureWithErosion = (RoundedDouble) failureProbability;
}
catch (ArgumentOutOfRangeException e)
{
Log.LogOutOfRangeException(string.Format(
RingtoetsCommonIOResources.TryReadParameter_Value_0_ParameterName_1_is_invalid,
failureProbability,
RingtoetsCommonIOResources.CalculationConfigurationImporter_FailureProbabilityStructureWithErosion_DisplayName),
calculation.Name,
e);
return false;
}
}
return true;
}
///
/// Reads the factor storm duration.
///
/// The calculation read from the imported file.
/// The calculation to configure.
private void ReadFactorStormDurationOpenStructure(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.FactorStormDurationOpenStructure.HasValue)
{
calculation.InputParameters.FactorStormDurationOpenStructure = (RoundedDouble) readCalculation.FactorStormDurationOpenStructure.Value;
}
}
///
/// Reads the failure probability open structure.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the failure probability open structure is invalid or when there is a failure probability
/// open structure but no structure defined, true otherwise.
private bool TryReadFailureProbabilityOpenStructure(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.FailureProbabilityOpenStructure.HasValue)
{
if (calculation.InputParameters.Structure == null)
{
Log.LogCalculationConversionError(string.Format(RingtoetsCommonIOResources.CalculationConfigurationImporter_TryParameter_No_Structure_to_assign_Parameter_0_,
RingtoetsCommonIOResources.CalculationConfigurationImporter_FailureProbabilityOpenStructure_DisplayName),
calculation.Name);
return false;
}
double failureProbability = readCalculation.FailureProbabilityOpenStructure.Value;
try
{
calculation.InputParameters.FailureProbabilityOpenStructure = (RoundedDouble) failureProbability;
}
catch (ArgumentOutOfRangeException e)
{
Log.LogOutOfRangeException(string.Format(
RingtoetsCommonIOResources.TryReadParameter_Value_0_ParameterName_1_is_invalid,
failureProbability,
RingtoetsCommonIOResources.CalculationConfigurationImporter_FailureProbabilityOpenStructure_DisplayName),
calculation.Name,
e);
return false;
}
}
return true;
}
///
/// Reads the failure probability reparation.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the failure probability reparation is invalid or when there is a failure probability
/// reparation but no structure defined, true otherwise.
private bool TryReadFailureProbabilityReparation(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.FailureProbabilityReparation.HasValue)
{
if (calculation.InputParameters.Structure == null)
{
Log.LogCalculationConversionError(string.Format(RingtoetsCommonIOResources.CalculationConfigurationImporter_TryParameter_No_Structure_to_assign_Parameter_0_,
RingtoetsCommonIOResources.CalculationConfigurationImporter_FailureProbabilityReparation_DisplayName),
calculation.Name);
return false;
}
double failureProbability = readCalculation.FailureProbabilityReparation.Value;
try
{
calculation.InputParameters.FailureProbabilityReparation = (RoundedDouble) failureProbability;
}
catch (ArgumentOutOfRangeException e)
{
Log.LogOutOfRangeException(string.Format(
RingtoetsCommonIOResources.TryReadParameter_Value_0_ParameterName_1_is_invalid,
failureProbability,
RingtoetsCommonIOResources.CalculationConfigurationImporter_FailureProbabilityReparation_DisplayName),
calculation.Name,
e);
return false;
}
}
return true;
}
///
/// Reads the probability or frequency open structure before flooding.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the probability or frequency open structure before flooding is invalid or when there is a
/// probability or frequency open structure before flooding but no structure defined, true otherwise.
private bool TryReadProbabilityOrFrequencyOpenStructureBeforeFlooding(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.ProbabilityOrFrequencyOpenStructureBeforeFlooding.HasValue)
{
if (calculation.InputParameters.Structure == null)
{
Log.LogCalculationConversionError(string.Format(RingtoetsCommonIOResources.CalculationConfigurationImporter_TryParameter_No_Structure_to_assign_Parameter_0_,
RingtoetsCommonIOResources.CalculationConfigurationImporter_ProbabilityOrFrequencyOpenStructureBeforeFlooding_DisplayName),
calculation.Name);
return false;
}
double failureProbability = readCalculation.ProbabilityOrFrequencyOpenStructureBeforeFlooding.Value;
try
{
calculation.InputParameters.ProbabilityOrFrequencyOpenStructureBeforeFlooding = (RoundedDouble) failureProbability;
}
catch (ArgumentOutOfRangeException e)
{
Log.LogOutOfRangeException(string.Format(
RingtoetsCommonIOResources.TryReadParameter_Value_0_ParameterName_1_is_invalid,
failureProbability,
RingtoetsCommonIOResources.CalculationConfigurationImporter_ProbabilityOrFrequencyOpenStructureBeforeFlooding_DisplayName),
calculation.Name,
e);
return false;
}
}
return true;
}
///
/// Reads the inflow model type.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the inflow model type is invalid or when there is a
/// inflow model type but no structure defined, true otherwise.
private bool TryReadInflowModelType(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.InflowModelType.HasValue)
{
if (calculation.InputParameters.Structure == null)
{
Log.LogCalculationConversionError(string.Format(RingtoetsCommonIOResources.CalculationConfigurationImporter_TryParameter_No_Structure_to_assign_Parameter_0_,
RingtoetsCommonIOResources.CalculationConfigurationImporter_InflowModelType_DisplayName),
calculation.Name);
return false;
}
calculation.InputParameters.InflowModelType =
(ClosingStructureInflowModelType) new ConfigurationClosingStructureInflowModelTypeConverter()
.ConvertTo(readCalculation.InflowModelType.Value, typeof(ClosingStructureInflowModelType));
}
return true;
}
///
/// Reads the number of identical apertures.
///
/// The calculation read from the imported file.
/// The calculation to configure.
/// false when the number of identical apertures is invalid or when there is a
/// number of identical apertures but no structure defined, true otherwise.
private bool TryReadIdenticalApertures(ClosingStructuresCalculationConfiguration readCalculation, StructuresCalculation calculation)
{
if (readCalculation.IdenticalApertures.HasValue)
{
if (calculation.InputParameters.Structure == null)
{
Log.LogCalculationConversionError(string.Format(RingtoetsCommonIOResources.CalculationConfigurationImporter_TryParameter_No_Structure_to_assign_Parameter_0_,
RingtoetsCommonIOResources.CalculationConfigurationImporter_IdenticalApertures_DisplayName),
calculation.Name);
return false;
}
calculation.InputParameters.IdenticalApertures = readCalculation.IdenticalApertures.Value;
}
return true;
}
private bool TryReadHydraulicBoundaryLocation(string locationName, StructuresCalculation calculation)
{
HydraulicBoundaryLocation location;
if (TryReadHydraulicBoundaryLocation(locationName, calculation.Name, availableHydraulicBoundaryLocations, out location))
{
calculation.InputParameters.HydraulicBoundaryLocation = location;
return true;
}
return false;
}
private bool TryReadStructure(string structureName, StructuresCalculation calculation)
{
ClosingStructure structure;
if (TryReadStructure(structureName, calculation.Name, availableStructures, out structure))
{
calculation.InputParameters.Structure = structure;
return true;
}
return false;
}
private bool TryReadForeshoreProfile(string foreshoreProfileName, StructuresCalculation calculation)
{
ForeshoreProfile foreshoreProfile;
if (TryReadForeshoreProfile(foreshoreProfileName, calculation.Name, availableForeshoreProfiles, out foreshoreProfile))
{
calculation.InputParameters.ForeshoreProfile = foreshoreProfile;
return true;
}
return false;
}
}
}