// Copyright (C) Stichting Deltares 2017. 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 System.Globalization;
using System.IO;
using System.Text;
using Core.Common.Base.Data;
using Core.Common.IO.Exceptions;
using Ringtoets.Common.Forms.TypeConverters;
using Ringtoets.DuneErosion.Data;
using Ringtoets.DuneErosion.IO.Properties;
using CoreCommonUtilResources = Core.Common.Util.Properties.Resources;
using DuneErosionDataResources = Ringtoets.DuneErosion.Data.Properties.Resources;
namespace Ringtoets.DuneErosion.IO
{
///
/// Csv file writer for writing objects to *.bnd file.
///
internal static class DuneLocationCalculationsWriter
{
private const string separator = "\t";
///
/// Writes dune location calculations to a bnd file.
///
/// The dune location calculations to be written to the file.
/// The path to the file.
/// Thrown when any parameter is null.
/// Thrown when unable to write to .
public static void WriteDuneLocationCalculations(IEnumerable exportableDuneLocationCalculations,
string filePath)
{
if (exportableDuneLocationCalculations == null)
{
throw new ArgumentNullException(nameof(exportableDuneLocationCalculations));
}
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
var stringBuilder = new StringBuilder(Resources.DuneLocationCalculationsWriter_WriteDuneLocationCalculations_HeaderLine + Environment.NewLine);
stringBuilder.AppendLine(Resources.DuneLocationCalculationsWriter_WriteDuneLocationCalculations_DisplayNameLine);
stringBuilder.AppendLine(Resources.DuneLocationCalculationsWriter_WriteDuneLocationCalculations_UnitsLine);
try
{
foreach (ExportableDuneLocationCalculation calculation in exportableDuneLocationCalculations)
{
stringBuilder.AppendLine(CreateCsvLine(calculation));
}
File.WriteAllText(filePath, stringBuilder.ToString());
}
catch (SystemException e)
{
throw new CriticalFileWriteException(string.Format(CoreCommonUtilResources.Error_General_output_error_0, filePath), e);
}
}
private static string CreateCsvLine(ExportableDuneLocationCalculation calculation)
{
DuneLocation duneLocation = calculation.Calculation.DuneLocation;
var stringComponents = new List
{
duneLocation.CoastalAreaId.ToString(null, CultureInfo.InvariantCulture),
duneLocation.Offset.ToString(DuneErosionDataResources.DuneLocation_Offset_format, CultureInfo.InvariantCulture),
Resources.DuneLocationCalculationsWriter_CreateCsvLine_Parameter_without_value,
duneLocation.D50.ToString(null, CultureInfo.InvariantCulture),
calculation.CategoryBoundaryName + " (Pfdsn = " + new NoProbabilityValueDoubleConverter().ConvertToInvariantString(calculation.Norm) + " jaar)",
calculation.CategoryBoundaryName,
calculation.Norm.ToString(CultureInfo.InvariantCulture)
};
stringComponents.InsertRange(2, GetOutputValues(calculation.Calculation.Output));
return string.Join(separator, stringComponents);
}
private static IEnumerable GetOutputValues(DuneLocationCalculationOutput calculationOutput)
{
return calculationOutput == null
? new[]
{
Resources.DuneLocationCalculationsWriter_CreateCsvLine_Parameter_without_value,
Resources.DuneLocationCalculationsWriter_CreateCsvLine_Parameter_without_value,
Resources.DuneLocationCalculationsWriter_CreateCsvLine_Parameter_without_value
}
: new[]
{
GetOutputValue(calculationOutput.WaterLevel),
GetOutputValue(calculationOutput.WaveHeight),
GetOutputValue(calculationOutput.WavePeriod)
};
}
private static string GetOutputValue(RoundedDouble value)
{
return !double.IsNaN(value)
? value.ToString(null, CultureInfo.InvariantCulture)
: Resources.DuneLocationCalculationsWriter_CreateCsvLine_Parameter_without_value;
}
}
}