// 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.Globalization;
using System.IO;
using System.Threading;
using Deltares.Dam.Data.Importers;
using Deltares.DamEngine.Data.Standard.Language;
using Deltares.Standard.IO;
using LumenWorks.Framework.IO.Csv;
namespace Deltares.Dam.Data.CsvImporters;
public class CsvImporterSuTablesException(string message) : Exception(message);
public class CsvImporterSuTables
{
private readonly int currentColIndex = -1; // Keep track of column for error message
public CsvImporterSuTables(string fileName)
{
ErrorMessages.Clear();
ThrowHelper.ThrowIfStringArgumentNullOrEmpty(fileName, StringResourceNames.CsvFileNotValid);
ThrowHelper.ThrowIfFileNotExist(fileName, StringResourceNames.CsvFileNotFound);
CultureInfo oldCultureInfo = Thread.CurrentThread.CurrentCulture;
try
{
Thread.CurrentThread.CurrentCulture = CsvReaderUtilities.DetermineCultureForFile(fileName);
using (var csv = new CsvReader(new StreamReader(fileName), true, ';'))
{
string[] headers = CsvImporterHelper.GetFieldHeaders(this, csv);
if (headers.Length < 2)
{
string csvHeaderError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderError");
throw new CsvImporterSoilsException($"{fileName} : {csvHeaderError}");
}
int colIndexSuTableName = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SuTableNameColumnName);
CheckColumn(colIndexSuTableName, fileName, CsvColumnNames.SuTableNameColumnName);
int colIndexSigma = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SigmaColumnName);
CheckColumn(colIndexSigma, fileName, CsvColumnNames.SigmaColumnName);
int colIndexSu = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.SuColumnName);
CheckColumn(colIndexSu, fileName, CsvColumnNames.SuColumnName);
while (csv.ReadNextRecord())
{
var SuTableRecord = new SuTableRecord();
try
{
// Required columns
currentColIndex = colIndexSuTableName;
SuTableRecord.SuTableName = csv[currentColIndex];
currentColIndex = colIndexSigma;
SuTableRecord.Sigma = Convert.ToDouble(csv[currentColIndex]);
currentColIndex = colIndexSu;
SuTableRecord.Su = Convert.ToDouble(csv[currentColIndex]);
ImportedItems.Add(SuTableRecord);
}
catch (Exception e)
{
string csvSuTableError = string.Format(LocalizationManager.GetTranslatedText(GetType(), "csvSuTableError"),
SuTableRecord.SuTableName, currentColIndex + 1);
ErrorMessages.Add(csvSuTableError + e.Message);
}
}
}
}
finally
{
Thread.CurrentThread.CurrentCulture = oldCultureInfo;
}
}
public List ErrorMessages { get; } = new();
public List ImportedItems { get; } = new();
private void CheckColumn(int index, string fileName, string fieldName)
{
if (index < 0)
{
string csvHeaderFieldError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderFieldError");
throw new CsvImporterSuTablesException($"{fileName} : {csvHeaderFieldError} {fieldName}");
}
}
public class SuTableRecord
{
public string SuTableName { get; set; }
public double Sigma { get; set; }
public double Su { get; set; }
}
}