// 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.IO; using Deltares.DamEngine.Data.Standard.Calculation; using Deltares.Standard.Logging; namespace Deltares.Dam.Data.CsvImporters; public class CsvImporter { private const string fileExtension = "csv"; private const string filePattern = "{0}\\{1}." + fileExtension; private const string locationsFileNamePart = "locations"; private const string segmentsFileNamePart = "segments"; private const string soilProfilesFileNamePart = "soilprofiles"; private const string aquifersFileNamePart = "aquifers"; private const string surfaceLinesFileNamePart = "surfacelines"; private const string characteristicPointFileNamePart = "characteristicpoints"; private const string scenariosFileNamePart = "scenarios"; private const string soilsFileNamePart = "soils"; private const string sigmaTauCurvesFileNamePart = "sigmataucurves"; private const string suTablesFileNamePart = "sutables"; private string folderName; public List ErrorMessages { get; } = new(); public List LocationRecords { get; set; } = new(); public List SegmentRecords { get; private set; } = new(); public List CharacteristicPointsRecords { get; private set; } = new(); public List SoilProfilesRecords { get; private set; } = new(); public List AquifersRecords { get; private set; } = new(); public List SurfaceLinesRecords { get; private set; } = new(); public List ScenariosRecords { get; private set; } = new(); public List SoilsRecords { get; private set; } = new(); public List SigmaTauCurvesRecords { get; private set; } = new(); public List SuTablesRecords { get; private set; } = new(); /// /// Imports the CSV data from directory. /// /// The CSV dir. /// if set to true [is import only locations]. /// The progress. /// Type of the dam project. public void ImportCsvDataFromDirectory(string csvDir, ProgressDelegate progress, DamProjectType damProjectType) { folderName = csvDir; progress?.Invoke(0.03); ThrowHelper.ThrowIfDirectoryNotExist(csvDir, StringResourceNames.ImportFolderNotExist); ImportLocations(LocationsFileName); progress?.Invoke(0.08); ImportSegments(SegmentsFileName); progress?.Invoke(0.12); ImportCharacteristicPoints(CharacteristicPointsFileName); progress?.Invoke(0.16); ImportSoilProfiles(SoilProfilesFileName); progress?.Invoke(0.2); ImportAquifers(AquifersFileName); progress?.Invoke(0.2); ImportSurfaceLines(SurfaceLinesFileName); // Scenario file is required for design projects and optional for the rest if (damProjectType == DamProjectType.Design) { progress?.Invoke(0.25); ImportScenarios(ScenariosFileName); RemoveEveryLocationThatHasNoScenarios(); } else { if (File.Exists(ScenariosFileName)) { progress?.Invoke(0.25); ImportScenarios(ScenariosFileName); } } if (File.Exists(SoilsFileName)) { progress?.Invoke(0.27); ImportSoils(SoilsFileName); } if (File.Exists(SigmaTauCurvesFileName)) { progress?.Invoke(0.29); ImportSigmaTauCurves(SigmaTauCurvesFileName); } if (File.Exists(SuTablesFileName)) { progress?.Invoke(0.29); ImportSuTables(SuTablesFileName); } progress?.Invoke(0.30); } public override string ToString() { return "CsvImporter"; } private void RemoveEveryLocationThatHasNoScenarios() { //ToDo Check whether each location has at least one scenario defined for it. If not, remove location and add error. } private string ImportFolderLocation => (folderName != null) && folderName.EndsWith('\\') ? folderName.Substring(0, folderName.Length - 1) : folderName; private string LocationsFileName => string.Format(filePattern, ImportFolderLocation, locationsFileNamePart); private string SegmentsFileName => string.Format(filePattern, ImportFolderLocation, segmentsFileNamePart); private string SoilProfilesFileName => string.Format(filePattern, ImportFolderLocation, soilProfilesFileNamePart); private string AquifersFileName => string.Format(filePattern, ImportFolderLocation, aquifersFileNamePart); private string SurfaceLinesFileName => string.Format(filePattern, ImportFolderLocation, surfaceLinesFileNamePart); private string CharacteristicPointsFileName => string.Format(filePattern, ImportFolderLocation, characteristicPointFileNamePart); private string ScenariosFileName => string.Format(filePattern, ImportFolderLocation, scenariosFileNamePart); private string SoilsFileName => string.Format(filePattern, ImportFolderLocation, soilsFileNamePart); private string SigmaTauCurvesFileName => string.Format(filePattern, ImportFolderLocation, sigmaTauCurvesFileNamePart); private string SuTablesFileName => string.Format(filePattern, ImportFolderLocation, suTablesFileNamePart); /// /// Imports the locations. /// /// Name of the file. private void ImportLocations(string fileName) { // Locations are optional, so check if the locations are available before trying to import if (File.Exists(fileName)) { try { var csvImporterLocations = new CsvImporterLocations(fileName); LocationRecords = csvImporterLocations.ImportedItems; foreach (string errorMessage in csvImporterLocations.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } } /// /// Imports the segments. /// /// Name of the file. private void ImportSegments(string fileName) { try { var csvImporterSegments = new CsvImporterSegments(fileName); SegmentRecords = csvImporterSegments.ImportedItems; foreach (string errorMessage in csvImporterSegments.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } /// /// Imports the characteristic points. /// /// Name of the file. private void ImportCharacteristicPoints(string fileName) { try { var csvImporterCharacteristicPoints = new CsvImporterCharacteristicPoints(fileName); CharacteristicPointsRecords = csvImporterCharacteristicPoints.ImportedItems; foreach (string errorMessage in csvImporterCharacteristicPoints.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } /// /// Imports the soil profiles. /// /// Name of the file. private void ImportSoilProfiles(string fileName) { // 1D SoilProfiles are not mandatory. The project can use 2D-geometries if (File.Exists(fileName)) { try { var csvImporterSoilProfiles = new CsvImporterSoilProfiles(fileName); SoilProfilesRecords = csvImporterSoilProfiles.ImportedItems; foreach (string errorMessage in csvImporterSoilProfiles.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } } /// /// Imports the aquifers. /// /// Name of the file. private void ImportAquifers(string fileName) { // Only necessary for 2D-geometries if (File.Exists(fileName)) { try { var csvImportAquifers = new CsvImporterAquifers(fileName); AquifersRecords = csvImportAquifers.ImportedItems; foreach (string errorMessage in csvImportAquifers.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } } /// /// Imports the surface lines. /// /// Name of the file. private void ImportSurfaceLines(string fileName) { try { var csvImporterSurfaceLines = new CsvImporterSurfaceLines(fileName); SurfaceLinesRecords = csvImporterSurfaceLines.ImportedItems; foreach (string errorMessage in csvImporterSurfaceLines.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } /// /// Imports the scenarios. /// /// Name of the file. private void ImportScenarios(string fileName) { try { var csvImporterScenarios = new CsvImporterScenarios(fileName); ScenariosRecords = csvImporterScenarios.ImportedItems; foreach (string errorMessage in csvImporterScenarios.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } /// /// Imports the soils. /// /// Name of the file. private void ImportSoils(string fileName) { try { var csvImporterSoils = new CsvImporterSoils(fileName); SoilsRecords = csvImporterSoils.ImportedItems; foreach (string errorMessage in csvImporterSoils.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } /// /// Import the Sigma Tau Curves /// /// The filename of the file to read private void ImportSigmaTauCurves(string fileName) { try { var csvImporterSigmaTauCurves = new CsvImporterSigmaTauCurves(fileName); SigmaTauCurvesRecords = csvImporterSigmaTauCurves.ImportedItems; foreach (string errorMessage in csvImporterSigmaTauCurves.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } /// /// Import the Su Tables /// /// The filename of the file to read private void ImportSuTables(string fileName) { try { var csvImporterSuTables = new CsvImporterSuTables(fileName); SuTablesRecords = csvImporterSuTables.ImportedItems; foreach (string errorMessage in csvImporterSuTables.ErrorMessages) { var logMessage = new LogMessage(LogMessageType.Error, this, errorMessage); ErrorMessages.Add(logMessage); } } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } }