// Copyright (C) Stichting Deltares 2024. 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 surfaceLinesFileNamePart = "surfacelines"; private const string characteristicPointFileNamePart = "characteristicpoints"; private const string scenariosFileNamePart = "scenarios"; private const string soilsFileNamePart = "soils"; 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 SurfaceLinesRecords { get; private set; } = new(); public List ScenariosRecords { get; private set; } = new(); public List SoilsRecords { 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, bool isImportOnlyLocations, ProgressDelegate progress, DamProjectType damProjectType) { folderName = csvDir; progress?.Invoke(0.03); ImportLocations(LocationsFileName); if (!isImportOnlyLocations) { progress?.Invoke(0.08); ImportSegments(SegmentsFileName); progress?.Invoke(0.12); ImportCharacteristicPoints(CharacteristicPointsFileName); progress?.Invoke(0.16); ImportSoilProfiles(SoilProfilesFileName); 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); } else { if (File.Exists(ScenariosFileName)) { progress?.Invoke(0.25); ImportScenarios(ScenariosFileName); } } if (File.Exists(SoilsFileName)) { progress?.Invoke(0.27); ImportSoils(SoilsFileName); } progress?.Invoke(0.30); } } public override string ToString() { return "CsvImporter"; } 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 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); /// /// 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 (CsvImporterSegmentsException 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 (CsvImporterSegmentsException 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 (CsvImporterCharacteristicPointsException 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 (CsvImporterSoilProfilesException 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 (CsvImporterSurfaceLinesException 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 (CsvImporterScenariosException e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); 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 (CsvImporterSoilsException e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } catch (Exception e) { var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message); ErrorMessages.Add(logMessage); } } }