// 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.IO;
using log4net;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Service;
using Ringtoets.HydraRing.Calculation.Data;
using Ringtoets.HydraRing.Calculation.Data.Input.Hydraulics;
using Ringtoets.HydraRing.Calculation.Data.Output;
using Ringtoets.HydraRing.Calculation.Parsers;
using Ringtoets.HydraRing.Calculation.Services;
using Ringtoets.HydraRing.Data;
using Ringtoets.HydraRing.IO;
using Ringtoets.Integration.Service.Properties;
using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.Integration.Service
{
///
/// Service that provides methods for performing Hydra-Ring calculations for design water level.
///
internal static class DesignWaterLevelCalculationService
{
private static readonly ILog log = LogManager.GetLogger(typeof(DesignWaterLevelCalculationService));
///
/// Performs validation over the values on the given . Error information is logged during
/// the execution of the operation.
///
/// The for which to validate the values.
/// The for which to validate the values.
/// False if contains validation errors; True otherwise.
internal static bool Validate(HydraulicBoundaryDatabase hydraulicBoundaryDatabase, HydraulicBoundaryLocation hydraulicBoundaryLocation)
{
CalculationServiceHelper.LogValidationBeginTime(hydraulicBoundaryLocation.Id.ToString());
var validationProblem = HydraulicDatabaseHelper.ValidatePathForCalculation(hydraulicBoundaryDatabase.FilePath);
var hasErrors = string.IsNullOrEmpty(validationProblem);
if (!hasErrors)
{
CalculationServiceHelper.LogMessagesAsError(RingtoetsCommonFormsResources.Hydraulic_boundary_database_connection_failed_0_,
validationProblem);
}
CalculationServiceHelper.LogValidationEndTime(hydraulicBoundaryLocation.Id.ToString());
return hasErrors;
}
///
/// Performs a design water level calculation based on the supplied and sets
/// if the calculation was successful. Error and status information is logged during the execution of the operation.
///
/// The to base the input for the calculation upon.
/// The to base the input for the calculation upon.
/// The to perform the calculation for.
/// The id of the ring to perform the calculation for.
/// A on a successful calculation, null otherwise.
internal static TargetProbabilityCalculationOutput Calculate(IAssessmentSection assessmentSection, HydraulicBoundaryDatabase hydraulicBoundaryDatabase,
HydraulicBoundaryLocation hydraulicBoundaryLocation, string ringId)
{
var hlcdDirectory = Path.GetDirectoryName(hydraulicBoundaryDatabase.FilePath);
var input = CreateInput(assessmentSection, hydraulicBoundaryLocation);
var targetProbabilityCalculationParser = new TargetProbabilityCalculationParser();
CalculationServiceHelper.PerformCalculation(
hydraulicBoundaryLocation.Id.ToString(),
() =>
{
HydraRingCalculationService.PerformCalculation(
hlcdDirectory,
ringId,
HydraRingTimeIntegrationSchemeType.FerryBorgesCastanheta,
HydraRingUncertaintiesType.All,
input,
new[] { targetProbabilityCalculationParser });
VerifyOutput(targetProbabilityCalculationParser.Output, hydraulicBoundaryLocation.Id.ToString());
});
return targetProbabilityCalculationParser.Output;
}
private static void VerifyOutput(TargetProbabilityCalculationOutput output, string name)
{
if (output == null)
{
log.ErrorFormat(Resources.DesignWaterLevelCalculationService_Calculate_Error_in_design_water_level_0_calculation, name);
}
}
private static AssessmentLevelCalculationInput CreateInput(IAssessmentSection assessmentSection, HydraulicBoundaryLocation hydraulicBoundaryLocation)
{
return new AssessmentLevelCalculationInput(1, hydraulicBoundaryLocation.Id, assessmentSection.FailureMechanismContribution.Norm);
}
}
}