Index: DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/CsvToDamxConverter.csproj =================================================================== diff -u -r5368 -r5384 --- DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/CsvToDamxConverter.csproj (.../CsvToDamxConverter.csproj) (revision 5368) +++ DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/CsvToDamxConverter.csproj (.../CsvToDamxConverter.csproj) (revision 5384) @@ -15,9 +15,14 @@ - + + + + PreserveNewest + + Index: DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/App.config =================================================================== diff -u --- DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/App.config (revision 0) +++ DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/App.config (revision 5384) @@ -0,0 +1,6 @@ + + + +
+ + \ No newline at end of file Index: DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/Program.cs =================================================================== diff -u -r5368 -r5384 --- DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/Program.cs (.../Program.cs) (revision 5368) +++ DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/Program.cs (.../Program.cs) (revision 5384) @@ -38,745 +38,749 @@ using Deltares.Standard.IO.Xml; using log4net.Config; -namespace Deltares.Dam.Tools.CsvToDamxConverter +namespace CsvToDamxConverter; + +internal class Program { - internal class Program - { - internal readonly static LogHelper Logger = LogHelper.Create("Main Program."); - const string DamLiveDataFolderName = "DamLiveData"; - const string DamXFileExtension = "damx"; - const string SensorProfileSheetName = "SensorProfileID"; - const string SensorGroupSheetName = "SensorGroupID"; - const string SensorSheetName = "SensorID"; - const string DikeLineInProfileSheetName = "DikeLineInProfile"; - const string IDColumnName = "ID"; - const string SensorNameColumnName = "SensorName"; - const string PLLineMappingColumnName = "PLLine-Mapping"; - const string ProfileColumnName = "Profile"; + private const string DamLiveDataFolderName = "DamLiveData"; + private const string DamXFileExtension = "damx"; + private const string SensorProfileSheetName = "SensorProfileID"; + private const string SensorGroupSheetName = "SensorGroupID"; + private const string SensorSheetName = "SensorID"; + private const string DikeLineInProfileSheetName = "DikeLineInProfile"; + private const string IDColumnName = "ID"; + private const string SensorNameColumnName = "SensorName"; + private const string PLLineMappingColumnName = "PLLine-Mapping"; + private const string ProfileColumnName = "Profile"; + internal static readonly LogHelper Logger = LogHelper.Create("Main Program."); - static bool hasWarnings; - private static string damLiveFolder; + private static bool hasWarnings; + private static string damLiveFolder; - static void Main(string[] args) + private static void Main(string[] args) + { + XmlConfigurator.Configure(); + try { - XmlConfigurator.Configure(); - try - { - Run(args); - } - catch (Exception e) - { - Logger.LogFatal("There was an unexpected error. Program terminated", e); - } - //Console.ReadKey(); + Parser.Default.ParseArguments(args) + .WithParsed(opts => RunOptionsAndReturnExitCode(opts)) + .WithNotParsed(errs => HandleParseError(errs)); } - - /// - /// Runs with the specified arguments. - /// - /// The arguments. - /// - /// - /// An unknown error occurred reading file: + file - /// - private static void Run(string[] args) + catch (Exception e) { - var commandLineArguments = new CommandOptions(); - ICommandLineParser parser = new CommandLineParser(); - bool success = parser.ParseArguments(args, commandLineArguments); - if (success) - { - var program = new Program(); + Logger.LogFatal("There was an unexpected error. Program terminated", e); + } + } + /// + /// Runs with the specified options. + /// + /// The options. + /// + /// + /// An unknown error occurred reading file: + file + /// + private static int RunOptionsAndReturnExitCode(CommandOptions commandOptions) + { + var program = new Program(); + #if (AUTO_DELETE_EXISTING_OUTPUT_FOLDER) - if (Directory.Exists(commandLineArguments.OuputDataDirectory)) - { - const bool recursive = true; - Directory.Delete(commandLineArguments.OuputDataDirectory, recursive); - } + if (Directory.Exists(commandOptions.OuputDataDirectory)) + { + const bool recursive = true; + Directory.Delete(commandOptions.OuputDataDirectory, recursive); + } #endif - if (Directory.Exists(commandLineArguments.OuputDataDirectory)) - { - string fullPath = Path.GetFullPath(commandLineArguments.OuputDataDirectory); - string msg = string.Format("Output directory {0} already exists. Please delete or use another directory. Program terminated", - fullPath); + if (Directory.Exists(commandOptions.OuputDataDirectory)) + { + var fullPath = Path.GetFullPath(commandOptions.OuputDataDirectory); + var msg = string.Format( + "Output directory {0} already exists. Please delete or use another directory. Program terminated", + fullPath); - throw new DirectoryNotFoundException(msg); - } + throw new DirectoryNotFoundException(msg); + } - string projectName = Path.GetFileNameWithoutExtension(commandLineArguments.DamXFileName); - if (string.IsNullOrWhiteSpace(projectName)) - { - const string msg = "Invalid output file name. A valid ." + DamXFileExtension + " file name is required to continue. Program terminated"; - throw new InvalidOperationException(msg); - } + var projectName = Path.GetFileNameWithoutExtension(commandOptions.DamXFileName); + if (string.IsNullOrWhiteSpace(projectName)) + { + const string msg = "Invalid output file name. A valid ." + DamXFileExtension + + " file name is required to continue. Program terminated"; + throw new InvalidOperationException(msg); + } - string outputDataDirectory = commandLineArguments.OuputDataDirectory; - Directory.CreateDirectory(outputDataDirectory); + var outputDataDirectory = commandOptions.OuputDataDirectory; + Directory.CreateDirectory(outputDataDirectory); - string damLiveDataFolderName = string.IsNullOrWhiteSpace(Settings.Default.DamLiveDataFolderName) ? DamLiveDataFolderName : Settings.Default.DamLiveDataFolderName; - damLiveFolder = Path.Combine(commandLineArguments.InputDataDirectory, damLiveDataFolderName); + var damLiveDataFolderName = string.IsNullOrWhiteSpace(Settings.Default.DamLiveDataFolderName) + ? DamLiveDataFolderName + : Settings.Default.DamLiveDataFolderName; + damLiveFolder = Path.Combine(commandOptions.InputDataDirectory, damLiveDataFolderName); - // Calculation folder (ProjectName.Cacl) - string calcFolder = Path.Combine(commandLineArguments.OuputDataDirectory, projectName + ".Calc"); - Directory.CreateDirectory(calcFolder); + // Calculation folder (ProjectName.Cacl) + var calcFolder = Path.Combine(commandOptions.OuputDataDirectory, projectName + ".Calc"); + Directory.CreateDirectory(calcFolder); - // Reading input files - DamProjectData damProjectData = null; - try - { - // old importer - damProjectData = program.LoadDike(commandLineArguments.InputDataDirectory); + // Reading input files + DamProjectData damProjectData = null; + try + { + // old importer + damProjectData = program.LoadDike(commandOptions.InputDataDirectory); - Dike dike = damProjectData.WaterBoard.Dikes.First(); + var dike = damProjectData.WaterBoard.Dikes.First(); - // Write output files - CopyGeometryData(projectName, commandLineArguments, dike); + // Write output files + CopyGeometryData(projectName, commandOptions, dike); - string sensorDataFileName = commandLineArguments.SensorDataFileName; - string excelFile = Path.Combine(commandLineArguments.InputDataDirectory, sensorDataFileName); - if (File.Exists(excelFile)) - { - CopySensorDataFromExcel(sensorDataFileName, commandLineArguments, dike); - } + var sensorDataFileName = commandOptions.SensorDataFileName; + var excelFile = Path.Combine(commandOptions.InputDataDirectory, sensorDataFileName); + if (File.Exists(excelFile)) CopySensorDataFromExcel(sensorDataFileName, commandOptions, dike); - CreateDamXFile(damProjectData, commandLineArguments.OuputDataDirectory, projectName); + CreateDamXFile(damProjectData, commandOptions.OuputDataDirectory, projectName); - if (Directory.Exists(damLiveFolder)) - { - string[] files = Directory.GetFiles(damLiveFolder); - foreach (string file in files) - { - string fileName = Path.GetFileName(file); - if (file == null) - { - throw new InvalidOperationException("An unknown error occurred reading file: " + file); - } + if (Directory.Exists(damLiveFolder)) + { + var files = Directory.GetFiles(damLiveFolder); + foreach (var file in files) + { + var fileName = Path.GetFileName(file); + if (file == null) + throw new InvalidOperationException("An unknown error occurred reading file: " + file); - File.Copy(file, Path.Combine(outputDataDirectory, fileName)); - } - } - - Console.WriteLine(hasWarnings - ? "Converted the data to a DAMX file, but there are warnings." - : "Conversion of the source data to DAMX file, was successful"); + File.Copy(file, Path.Combine(outputDataDirectory, fileName)); } - finally - { - if (damProjectData != null) - { - damProjectData.Dispose(); - } - } } - else - { - Console.WriteLine(); - Console.WriteLine(commandLineArguments.GetUsage()); - } - } - /// - /// Creates the damx file. - /// - /// The dam project data. - /// The output dir. - /// Name of the project. - private static void CreateDamXFile(DamProjectData damProjectData, string outputDir, - string projectName) + Console.WriteLine(hasWarnings + ? "Converted the data to a DAMX file, but there are warnings." + : "Conversion of the source data to DAMX file, was successful"); + } + finally { - string fileName = Path.Combine(outputDir, projectName + "." + DamXFileExtension); - fileName = Path.ChangeExtension(fileName, DamXFileExtension); + if (damProjectData != null) damProjectData.Dispose(); + } - var xmlSerializer = new XmlSerializer(); - xmlSerializer.Serialize(damProjectData, fileName); + return 0; + } + + private static void HandleParseError(IEnumerable errs) + { + foreach (var error in errs) + { + Logger.LogError(error.ToString()); } + } - /// - /// Copies the sensor data from excel. - /// - /// The source file. - /// The command options. - /// The dike. - /// - /// Error reading excel file - /// or - /// The + PLLineMappingColumnName + column has an error. Make sure the cells are not empty and are formatted as text - /// - /// - private static void CopySensorDataFromExcel(string sourceFile, CommandOptions commandOptions, Dike dike) + /// + /// Creates the damx file. + /// + /// The dam project data. + /// The output dir. + /// Name of the project. + private static void CreateDamXFile(DamProjectData damProjectData, string outputDir, + string projectName) + { + var fileName = Path.Combine(outputDir, projectName + "." + DamXFileExtension); + fileName = Path.ChangeExtension(fileName, DamXFileExtension); + + var xmlSerializer = new XmlSerializer(); + xmlSerializer.Serialize(damProjectData, fileName); + } + + /// + /// Copies the sensor data from excel. + /// + /// The source file. + /// The command options. + /// The dike. + /// + /// Error reading excel file + /// or + /// The + PLLineMappingColumnName + column has an error. Make sure the cells are not empty and are formatted as text + /// + /// + private static void CopySensorDataFromExcel(string sourceFile, CommandOptions commandOptions, Dike dike) + { + string[] sheets = { - string[] sheets = - { - SensorProfileSheetName, - SensorGroupSheetName, - SensorSheetName, - DikeLineInProfileSheetName - }; + SensorProfileSheetName, + SensorGroupSheetName, + SensorSheetName, + DikeLineInProfileSheetName + }; - DataSet ds = null; - string fileName = Path.Combine(commandOptions.InputDataDirectory, sourceFile); - string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName); - using (var conn = new OleDbConnection(connectionString)) - { - ds = FillDataSet(conn, ds, sheets); - } + DataSet ds = null; + var fileName = Path.Combine(commandOptions.InputDataDirectory, sourceFile); + var connectionString = + string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", + fileName); + using (var conn = new OleDbConnection(connectionString)) + { + ds = FillDataSet(conn, ds, sheets); + } - if (ds == null) - { - throw new InvalidOperationException("Error reading excel file"); - } + if (ds == null) throw new InvalidOperationException("Error reading excel file"); - EnumerableRowCollection profiles = ds.Tables[SensorProfileSheetName].AsEnumerable(); - EnumerableRowCollection groups = ds.Tables[SensorGroupSheetName].AsEnumerable(); - EnumerableRowCollection dikeLinePositions = ds.Tables[DikeLineInProfileSheetName].AsEnumerable(); + var profiles = ds.Tables[SensorProfileSheetName].AsEnumerable(); + var groups = ds.Tables[SensorGroupSheetName].AsEnumerable(); + var dikeLinePositions = ds.Tables[DikeLineInProfileSheetName].AsEnumerable(); - var sensors = new List(); - EnumerableRowCollection sensorRows = ds.Tables[SensorSheetName].AsEnumerable(); - var count = 0; + var sensors = new List(); + var sensorRows = ds.Tables[SensorSheetName].AsEnumerable(); + var count = 0; - foreach (DataRow row in sensorRows) + foreach (var row in sensorRows) + { + if (row.IsNull(IDColumnName)) { - if (row.IsNull(IDColumnName)) - { - Logger.LogWarning(string.Format("Error reading row {0} in worksheet {1}. This row will be skipped.", count + 2, SensorSheetName)); - continue; - } + Logger.LogWarning(string.Format("Error reading row {0} in worksheet {1}. This row will be skipped.", + count + 2, SensorSheetName)); + continue; + } - count++; + count++; - var id = (int) row.Field(IDColumnName); - var name = row.Field(SensorNameColumnName); - var relativeLocation = row.Field("RelativeLocationSensorAlongProfileManual"); - var depth = row.Field("DepthSensor"); - var enumTypeIgnoreCase = row.Field("SensorType").ToEnumTypeIgnoreCase(); + var id = (int)row.Field(IDColumnName); + var name = row.Field(SensorNameColumnName); + var relativeLocation = row.Field("RelativeLocationSensorAlongProfileManual"); + var depth = row.Field("DepthSensor"); + var enumTypeIgnoreCase = row.Field("SensorType").ToEnumTypeIgnoreCase(); - var sensor = new Sensor - { - ID = id, - Name = name, - RelativeLocation = relativeLocation, - Depth = depth, - Type = enumTypeIgnoreCase - //1 255364,05597700000 598646,53034800000 - //XRd = 255364.05597700000, - //YRd = 598646.53034800000, - }; + var sensor = new Sensor + { + ID = id, + Name = name, + RelativeLocation = relativeLocation, + Depth = depth, + Type = enumTypeIgnoreCase + //1 255364,05597700000 598646,53034800000 + //XRd = 255364.05597700000, + //YRd = 598646.53034800000, + }; - try - { - string[] mappings = row.Field(PLLineMappingColumnName) - .Split(new[] - { - ';' - }, StringSplitOptions.RemoveEmptyEntries) - .Select(tag => tag.Trim()) - //.Where(tag => !string.IsNullOrWhiteSpace(tag)) - .ToArray(); + try + { + var mappings = row.Field(PLLineMappingColumnName) + .Split(new[] + { + ';' + }, StringSplitOptions.RemoveEmptyEntries) + .Select(tag => tag.Trim()) + //.Where(tag => !string.IsNullOrWhiteSpace(tag)) + .ToArray(); - foreach (string mapping in mappings) + foreach (var mapping in mappings) + switch (mapping) { - switch (mapping) - { - case "1": - sensor.Add(PLLineType.PL1); - break; - case "2": - sensor.Add(PLLineType.PL2); - break; - case "3": - sensor.Add(PLLineType.PL3); - break; - case "4": - sensor.Add(PLLineType.PL4); - break; - } + case "1": + sensor.Add(PLLineType.PL1); + break; + case "2": + sensor.Add(PLLineType.PL2); + break; + case "3": + sensor.Add(PLLineType.PL3); + break; + case "4": + sensor.Add(PLLineType.PL4); + break; } - } - catch (Exception e) - { - throw new InvalidOperationException("The " + PLLineMappingColumnName + " column has an error. Make sure the cells are not empty and are formatted as text", e); - } - - Logger.LogInfo(sensor.ToString()); - sensors.Add(sensor); } - - Logger.LogInfo(string.Format("Successfully imported {0} sensor(s).", count)); - - if (!Directory.Exists(damLiveFolder)) + catch (Exception e) { - throw new DirectoryNotFoundException(string.Format("The DAM (CSV) data folder '{0}' doesn't exist. Program terminated.", damLiveFolder)); + throw new InvalidOperationException( + "The " + PLLineMappingColumnName + + " column has an error. Make sure the cells are not empty and are formatted as text", e); } - CreateFEWSFilesIfNeeded(commandOptions, sensors); + Logger.LogInfo(sensor.ToString()); + sensors.Add(sensor); + } - count = 0; - foreach (Location location in dike.Locations) - { - var repository = new SensorRepository(location); + Logger.LogInfo(string.Format("Successfully imported {0} sensor(s).", count)); - int id; - if (ParseSensorProfileWorksheet(location, profiles, out id)) - { - hasWarnings = true; - continue; - } + if (!Directory.Exists(damLiveFolder)) + throw new DirectoryNotFoundException( + string.Format("The DAM (CSV) data folder '{0}' doesn't exist. Program terminated.", damLiveFolder)); - // Parsing sensor groups - if (ParseSensorGroups(groups, id, sensors, repository)) - { - hasWarnings = true; - continue; - } + CreateFEWSFilesIfNeeded(commandOptions, sensors); - count++; + count = 0; + foreach (var location in dike.Locations) + { + var repository = new SensorRepository(location); + + int id; + if (ParseSensorProfileWorksheet(location, profiles, out id)) + { + hasWarnings = true; + continue; } - Logger.LogInfo(string.Format("Successfully imported {0} sensor groups(s).", count)); - Logger.LogInfo(string.Format("Successfully imported {0} sensor profile(s).", count)); + // Parsing sensor groups + if (ParseSensorGroups(groups, id, sensors, repository)) + { + hasWarnings = true; + continue; + } + + count++; } - /// - /// Parses the sensor groups. - /// - /// The sensor groups. - /// The identifier. - /// The sensors. - /// The repository. - /// - /// - /// - private static bool ParseSensorGroups(IEnumerable sensorGroups, int id, List sensors, SensorRepository repository) + Logger.LogInfo(string.Format("Successfully imported {0} sensor groups(s).", count)); + Logger.LogInfo(string.Format("Successfully imported {0} sensor profile(s).", count)); + } + + /// + /// Parses the sensor groups. + /// + /// The sensor groups. + /// The identifier. + /// The sensors. + /// The repository. + /// + /// + /// + private static bool ParseSensorGroups(IEnumerable sensorGroups, int id, List sensors, + SensorRepository repository) + { + const string sensorSelectionColumnName = "SensorSelection"; + var currentSensorId = ""; + try { - const string sensorSelectionColumnName = "SensorSelection"; - var currentSensorId = ""; - try + string[] groupIds = null; + foreach (var sensorGroup in sensorGroups) { - string[] groupIds = null; - foreach (DataRow sensorGroup in sensorGroups) - { - var idValue = (int) sensorGroup.Field(IDColumnName); - if (idValue != id) - { - continue; - } + var idValue = (int)sensorGroup.Field(IDColumnName); + if (idValue != id) continue; - var groupIdValue = sensorGroup.Field(sensorSelectionColumnName); + var groupIdValue = sensorGroup.Field(sensorSelectionColumnName); - if (groupIdValue == null) - { - Logger.LogWarning(string.Format("Error reading the value from column {0} for group id {1}", - sensorSelectionColumnName, id)); - } - else - { - groupIds = groupIdValue.Split(new[] - { - ';' - }, StringSplitOptions.RemoveEmptyEntries); - break; - } - } - - if (groupIds == null) + if (groupIdValue == null) { - Logger.LogWarning(string.Format("Couldn't find group id '{0}' in worksheet {1}", id, - SensorGroupSheetName)); - return true; + Logger.LogWarning(string.Format("Error reading the value from column {0} for group id {1}", + sensorSelectionColumnName, id)); } - - foreach (string groupId in groupIds) + else { - currentSensorId = groupId; - Sensor single = sensors.Single(s => s.ID.ToString() == groupId); - repository.Add(single); + groupIds = groupIdValue.Split(new[] + { + ';' + }, StringSplitOptions.RemoveEmptyEntries); + break; } } - catch (InvalidOperationException ioe) + + if (groupIds == null) { - if (!string.IsNullOrWhiteSpace(currentSensorId)) - { - string message = string.Format("Error finding sensor. The sensor with ID '{0}' was not found in the Excel worksheet '{1}'", currentSensorId, SensorSheetName); - throw new InvalidOperationException(message, ioe); - } + Logger.LogWarning(string.Format("Couldn't find group id '{0}' in worksheet {1}", id, + SensorGroupSheetName)); + return true; + } - throw; + foreach (var groupId in groupIds) + { + currentSensorId = groupId; + var single = sensors.Single(s => s.ID.ToString() == groupId); + repository.Add(single); } - catch (Exception e) + } + catch (InvalidOperationException ioe) + { + if (!string.IsNullOrWhiteSpace(currentSensorId)) { - throw new InvalidOperationException( - string.Format("An unknown error occurred in column " + sensorSelectionColumnName + " in the Excel sheet " + SensorGroupSheetName + - ". Used the following location ID: " + id, e)); + var message = + string.Format( + "Error finding sensor. The sensor with ID '{0}' was not found in the Excel worksheet '{1}'", + currentSensorId, SensorSheetName); + throw new InvalidOperationException(message, ioe); } - return false; + throw; } - - /// - /// Parses the sensor profile worksheet. - /// - /// The location. - /// The sensor locations. - /// The identifier. - /// - /// An error occurred while parsing row in worksheet + SensorProfileSheetName - private static bool ParseSensorProfileWorksheet(Location location, EnumerableRowCollection sensorLocations, out int id) + catch (Exception e) { - id = -1; - try - { - Location location1 = location; - var record = sensorLocations - .Where(x => x.Field(ProfileColumnName) == location1.Name) - .Select(x => new - { - ID = GetLocationIDField(x), - GroupID = GetGroupIDField(x), - Alias = GetNameAliasField(x), - InputPL1OuterWaterLevel = GetPL1WaterLevelRiverSide(x), - InputPL1PLLineOffsetBelowDikeTopAtRiver = GetPL1OffsetBelowDikeTopAtRiver(x), - InputPL1PLLineOffsetBelowDikeTopAtPolder = GetPL1OffsetBelowDikeTopAtPolder(x), - InputPL1PLLineOffsetBelowShoulderBaseInside = GetPL1OffsetBelowShoulderBaseInside(x), - InputPL1PLLineOffsetBelowDikeToeAtPolder = GetPL1OffsetBelowDikeToeAtPolder(x), - InputPL1PolderLevel = GetPL1PolderLevelSensorType(x), - InputPL3 = GetInputPl4(x), - InputPL4 = GetInputPl3(x) - }).SingleOrDefault(); + throw new InvalidOperationException( + string.Format("An unknown error occurred in column " + sensorSelectionColumnName + + " in the Excel sheet " + SensorGroupSheetName + + ". Used the following location ID: " + id, e)); + } - if (record == null) + return false; + } + + /// + /// Parses the sensor profile worksheet. + /// + /// The location. + /// The sensor locations. + /// The identifier. + /// + /// + /// An error occurred while parsing row in worksheet + + /// SensorProfileSheetName + /// + private static bool ParseSensorProfileWorksheet(Location location, EnumerableRowCollection sensorLocations, + out int id) + { + id = -1; + try + { + var location1 = location; + var record = sensorLocations + .Where(x => x.Field(ProfileColumnName) == location1.Name) + .Select(x => new { - Logger.LogWarning(string.Format("Location profile ID '{0}' not found in worksheet {1}. Location profile skipped.", - location.Name, SensorProfileSheetName)); - return true; - } + ID = GetLocationIDField(x), + GroupID = GetGroupIDField(x), + Alias = GetNameAliasField(x), + InputPL1OuterWaterLevel = GetPL1WaterLevelRiverSide(x), + InputPL1PLLineOffsetBelowDikeTopAtRiver = GetPL1OffsetBelowDikeTopAtRiver(x), + InputPL1PLLineOffsetBelowDikeTopAtPolder = GetPL1OffsetBelowDikeTopAtPolder(x), + InputPL1PLLineOffsetBelowShoulderBaseInside = GetPL1OffsetBelowShoulderBaseInside(x), + InputPL1PLLineOffsetBelowDikeToeAtPolder = GetPL1OffsetBelowDikeToeAtPolder(x), + InputPL1PolderLevel = GetPL1PolderLevelSensorType(x), + InputPL3 = GetInputPl4(x), + InputPL4 = GetInputPl3(x) + }).SingleOrDefault(); - id = record.GroupID; - - SensorLocation sensorData = location.SensorLocation; - sensorData.Group.ID = record.GroupID; - sensorData.Alias = record.Alias; - sensorData.SourceTypePl1WaterLevelAtRiver = record.InputPL1OuterWaterLevel; - sensorData.SourceTypePl1PlLineOffsetBelowDikeTopAtRiver = record.InputPL1PLLineOffsetBelowDikeTopAtRiver; - sensorData.SourceTypePl1PlLineOffsetBelowDikeTopAtPolder = record.InputPL1PLLineOffsetBelowDikeTopAtPolder; - sensorData.SourceTypePl1PlLineOffsetBelowShoulderBaseInside = record.InputPL1PLLineOffsetBelowShoulderBaseInside; - sensorData.SourceTypePl1PlLineOffsetBelowDikeToeAtPolder = record.InputPL1PLLineOffsetBelowDikeToeAtPolder; - sensorData.SourceTypePl1WaterLevelAtPolder = record.InputPL1PolderLevel; - sensorData.SourceTypePl3 = record.InputPL3; - sensorData.SourceTypePl4 = record.InputPL4; - } - catch (Exception e) + if (record == null) { - throw new InvalidOperationException("An error occurred while parsing row in worksheet " + SensorProfileSheetName, e); + Logger.LogWarning(string.Format( + "Location profile ID '{0}' not found in worksheet {1}. Location profile skipped.", + location.Name, SensorProfileSheetName)); + return true; } - return false; + id = record.GroupID; + + var sensorData = location.SensorLocation; + sensorData.Group.ID = record.GroupID; + sensorData.Alias = record.Alias; + sensorData.SourceTypePl1WaterLevelAtRiver = record.InputPL1OuterWaterLevel; + sensorData.SourceTypePl1PlLineOffsetBelowDikeTopAtRiver = record.InputPL1PLLineOffsetBelowDikeTopAtRiver; + sensorData.SourceTypePl1PlLineOffsetBelowDikeTopAtPolder = record.InputPL1PLLineOffsetBelowDikeTopAtPolder; + sensorData.SourceTypePl1PlLineOffsetBelowShoulderBaseInside = + record.InputPL1PLLineOffsetBelowShoulderBaseInside; + sensorData.SourceTypePl1PlLineOffsetBelowDikeToeAtPolder = record.InputPL1PLLineOffsetBelowDikeToeAtPolder; + sensorData.SourceTypePl1WaterLevelAtPolder = record.InputPL1PolderLevel; + sensorData.SourceTypePl3 = record.InputPL3; + sensorData.SourceTypePl4 = record.InputPL4; } + catch (Exception e) + { + throw new InvalidOperationException( + "An error occurred while parsing row in worksheet " + SensorProfileSheetName, e); + } - /// - /// Creates the fews files if needed. - /// - /// The command options. - /// The sensors. - private static void CreateFEWSFilesIfNeeded(CommandOptions commandOptions, List sensors) + return false; + } + + /// + /// Creates the fews files if needed. + /// + /// The command options. + /// The sensors. + private static void CreateFEWSFilesIfNeeded(CommandOptions commandOptions, List sensors) + { + var files = Directory.GetFiles(damLiveFolder); + var noFilesIncluded = files.Length == 0; + var inputFileIncluded = files.Any(f => !f.Contains("Parameter")); + + if (noFilesIncluded || !inputFileIncluded) { - string[] files = Directory.GetFiles(damLiveFolder); - bool noFilesIncluded = files.Length == 0; - bool inputFileIncluded = files.Any(f => !f.Contains("Parameter")); + var fewsInputFile = Path.Combine(commandOptions.OuputDataDirectory, "live.FEWS.InputFile.xml"); + WriteFEWSSensorFiles(new FileInfo(fewsInputFile), sensors); + } + } - if (noFilesIncluded || !inputFileIncluded) + /// + /// Fills the data set. + /// + /// The connection. + /// The ds. + /// The sheets. + /// + private static DataSet FillDataSet(OleDbConnection conn, DataSet ds, string[] sheets) + { + conn.Open(); + ds = new DataSet(); + + foreach (var sheet in sheets) + { + // string columns = String.Join(",", columnNames.ToArray()); + const string columns = "*"; + using (var da = new OleDbDataAdapter( + "SELECT " + columns + " FROM [" + sheet + "$]", conn)) { - string fewsInputFile = Path.Combine(commandOptions.OuputDataDirectory, "live.FEWS.InputFile.xml"); - WriteFEWSSensorFiles(new FileInfo(fewsInputFile), sensors); + var dt = new DataTable(sheet); + da.Fill(dt); + ds.Tables.Add(dt); } } - /// - /// Fills the data set. - /// - /// The connection. - /// The ds. - /// The sheets. - /// - private static DataSet FillDataSet(OleDbConnection conn, DataSet ds, string[] sheets) - { - conn.Open(); - ds = new DataSet(); + return ds; + } - foreach (string sheet in sheets) - { - // string columns = String.Join(",", columnNames.ToArray()); - const string columns = "*"; - using (var da = new OleDbDataAdapter( - "SELECT " + columns + " FROM [" + sheet + "$]", conn)) - { - var dt = new DataTable(sheet); - da.Fill(dt); - ds.Tables.Add(dt); - } - } + /// + /// Gets the p l1 water level river side. + /// + /// The x. + /// + private static DataSourceTypeSensors GetPL1WaterLevelRiverSide(DataRow x) + { + DataSourceTypeSensors pl1WaterLevelRiverSide; - return ds; - } + pl1WaterLevelRiverSide = + x.Field("InputPL1OuterWaterLevel").ToEnumTypeIgnoreCase(); - /// - /// Gets the p l1 water level river side. - /// - /// The x. - /// - private static DataSourceTypeSensors GetPL1WaterLevelRiverSide(DataRow x) - { - DataSourceTypeSensors pl1WaterLevelRiverSide; + return pl1WaterLevelRiverSide; + } - pl1WaterLevelRiverSide = x.Field("InputPL1OuterWaterLevel").ToEnumTypeIgnoreCase(); + /// + /// Gets the p l1 offset below dike top at river. + /// + /// The x. + /// + private static DataSourceTypeSensors GetPL1OffsetBelowDikeTopAtRiver(DataRow x) + { + const string columnName = "InputPL1PLLineOffsetBelowDikeTopAtRiver"; - return pl1WaterLevelRiverSide; - } + DataSourceTypeSensors pl1OffsetBelowDikeTopAtRiver; + pl1OffsetBelowDikeTopAtRiver = x.Field(columnName).ToEnumTypeIgnoreCase(); - /// - /// Gets the p l1 offset below dike top at river. - /// - /// The x. - /// - private static DataSourceTypeSensors GetPL1OffsetBelowDikeTopAtRiver(DataRow x) - { - const string columnName = "InputPL1PLLineOffsetBelowDikeTopAtRiver"; + return pl1OffsetBelowDikeTopAtRiver; + } - DataSourceTypeSensors pl1OffsetBelowDikeTopAtRiver; - pl1OffsetBelowDikeTopAtRiver = x.Field(columnName).ToEnumTypeIgnoreCase(); + /// + /// Gets the p l1 offset below dike top at polder. + /// + /// The x. + /// + private static DataSourceTypeSensors GetPL1OffsetBelowDikeTopAtPolder(DataRow x) + { + const string columnName = "InputPL1PLLineOffsetBelowDikeTopAtPolder"; - return pl1OffsetBelowDikeTopAtRiver; - } + DataSourceTypeSensors pl1OffsetBelowDikeTopAtPolder; + pl1OffsetBelowDikeTopAtPolder = x.Field(columnName).ToEnumTypeIgnoreCase(); - /// - /// Gets the p l1 offset below dike top at polder. - /// - /// The x. - /// - private static DataSourceTypeSensors GetPL1OffsetBelowDikeTopAtPolder(DataRow x) - { - const string columnName = "InputPL1PLLineOffsetBelowDikeTopAtPolder"; + return pl1OffsetBelowDikeTopAtPolder; + } - DataSourceTypeSensors pl1OffsetBelowDikeTopAtPolder; - pl1OffsetBelowDikeTopAtPolder = x.Field(columnName).ToEnumTypeIgnoreCase(); + /// + /// Gets the p l1 offset below shoulder base inside. + /// + /// The x. + /// + private static DataSourceTypeSensors GetPL1OffsetBelowShoulderBaseInside(DataRow x) + { + DataSourceTypeSensors pl1OffsetBelowShoulderBaseInside; + pl1OffsetBelowShoulderBaseInside = x.Field("InputPL1PLLineOffsetBelowShoulderBaseInside") + .ToEnumTypeIgnoreCase(); - return pl1OffsetBelowDikeTopAtPolder; - } + return pl1OffsetBelowShoulderBaseInside; + } - /// - /// Gets the p l1 offset below shoulder base inside. - /// - /// The x. - /// - private static DataSourceTypeSensors GetPL1OffsetBelowShoulderBaseInside(DataRow x) - { - DataSourceTypeSensors pl1OffsetBelowShoulderBaseInside; - pl1OffsetBelowShoulderBaseInside = x.Field("InputPL1PLLineOffsetBelowShoulderBaseInside").ToEnumTypeIgnoreCase(); + /// + /// Gets the p l1 offset below dike toe at polder. + /// + /// The x. + /// + private static DataSourceTypeSensors GetPL1OffsetBelowDikeToeAtPolder(DataRow x) + { + DataSourceTypeSensors pl1OffsetBelowDikeToeAtPolder; - return pl1OffsetBelowShoulderBaseInside; - } + pl1OffsetBelowDikeToeAtPolder = x.Field("InputPL1PLLineOffsetBelowDikeToeAtPolder") + .ToEnumTypeIgnoreCase(); + return pl1OffsetBelowDikeToeAtPolder; + } - /// - /// Gets the p l1 offset below dike toe at polder. - /// - /// The x. - /// - private static DataSourceTypeSensors GetPL1OffsetBelowDikeToeAtPolder(DataRow x) - { - DataSourceTypeSensors pl1OffsetBelowDikeToeAtPolder; + /// + /// Gets the type of the p l1 polder level sensor. + /// + /// The x. + /// + private static DataSourceTypeSensors GetPL1PolderLevelSensorType(DataRow x) + { + const string columnName = "InputPL1PolderLevel"; + DataSourceTypeSensors pl1PolderLevelSensorType; - pl1OffsetBelowDikeToeAtPolder = x.Field("InputPL1PLLineOffsetBelowDikeToeAtPolder").ToEnumTypeIgnoreCase(); - return pl1OffsetBelowDikeToeAtPolder; - } + pl1PolderLevelSensorType = x.Field(columnName).ToEnumTypeIgnoreCase(); - /// - /// Gets the type of the p l1 polder level sensor. - /// - /// The x. - /// - private static DataSourceTypeSensors GetPL1PolderLevelSensorType(DataRow x) - { - const string columnName = "InputPL1PolderLevel"; - DataSourceTypeSensors pl1PolderLevelSensorType; + return pl1PolderLevelSensorType; + } - pl1PolderLevelSensorType = x.Field(columnName).ToEnumTypeIgnoreCase(); + /// + /// Gets the input PL4. + /// + /// The x. + /// + private static DataSourceTypeSensors GetInputPl4(DataRow x) + { + DataSourceTypeSensors dataSourceTypeSensors; - return pl1PolderLevelSensorType; - } + dataSourceTypeSensors = x.Field("InputPL4").ToEnumTypeIgnoreCase(); - /// - /// Gets the input PL4. - /// - /// The x. - /// - private static DataSourceTypeSensors GetInputPl4(DataRow x) - { - DataSourceTypeSensors dataSourceTypeSensors; + return dataSourceTypeSensors; + } - dataSourceTypeSensors = x.Field("InputPL4").ToEnumTypeIgnoreCase(); + /// + /// Gets the input PL3. + /// + /// The x. + /// + private static DataSourceTypeSensors GetInputPl3(DataRow x) + { + DataSourceTypeSensors pl3; - return dataSourceTypeSensors; - } + pl3 = x.Field("InputPL3").ToEnumTypeIgnoreCase(); - /// - /// Gets the input PL3. - /// - /// The x. - /// - private static DataSourceTypeSensors GetInputPl3(DataRow x) - { - DataSourceTypeSensors pl3; + return pl3; + } - pl3 = x.Field("InputPL3").ToEnumTypeIgnoreCase(); + /// + /// Gets the name alias field. + /// + /// The x. + /// + /// nameAliasField + private static string GetNameAliasField(DataRow x) + { + var nameAliasField = x.Field("NameAlias"); + if (nameAliasField == null) throw new ArgumentNullException("nameAliasField"); - return pl3; - } + return nameAliasField; + } - /// - /// Gets the name alias field. - /// - /// The x. - /// - /// nameAliasField - private static string GetNameAliasField(DataRow x) + /// + /// Gets the group identifier field. + /// + /// The x. + /// + /// Error parsing value : + x.Field(columnName) + private static int GetGroupIDField(DataRow x) + { + int groupIdField; + const string columnName = "SensorGroup"; + try { - var nameAliasField = x.Field("NameAlias"); - if (nameAliasField == null) - { - throw new ArgumentNullException("nameAliasField"); - } - - return nameAliasField; + groupIdField = (int)x.Field(columnName); } - - /// - /// Gets the group identifier field. - /// - /// The x. - /// - /// Error parsing value : + x.Field(columnName) - private static int GetGroupIDField(DataRow x) + catch (InvalidCastException) { - int groupIdField; - const string columnName = "SensorGroup"; - try - { - groupIdField = (int) x.Field(columnName); - } - catch (InvalidCastException) - { - throw new InvalidCastException("Error parsing value : " + x.Field(columnName)); - } - - return groupIdField; + throw new InvalidCastException("Error parsing value : " + x.Field(columnName)); } - /// - /// Gets the location identifier field. - /// - /// The x. - /// - /// Error parsing value : + x.Field(columnName) + . LocationID should be a numeric value. - private static int GetLocationIDField(DataRow x) - { - int locationIdField; - const string columnName = "locationID"; + return groupIdField; + } - try - { - locationIdField = (int) x.Field(columnName); - } - catch (InvalidCastException) - { - throw new InvalidCastException("Error parsing value : " + x.Field(columnName) + ". LocationID should be a numeric value."); - } + /// + /// Gets the location identifier field. + /// + /// The x. + /// + /// + /// Error parsing value : + x.Field + /// (columnName) + . LocationID should be a numeric value. + /// + private static int GetLocationIDField(DataRow x) + { + int locationIdField; + const string columnName = "locationID"; - return locationIdField; + try + { + locationIdField = (int)x.Field(columnName); } + catch (InvalidCastException) + { + throw new InvalidCastException("Error parsing value : " + x.Field(columnName) + + ". LocationID should be a numeric value."); + } - /// - /// Copies the geometry data. - /// - /// Name of the project. - /// The command options. - /// The dike. - private static void CopyGeometryData(string projectName, CommandOptions commandOptions, Dike dike) + return locationIdField; + } + + /// + /// Copies the geometry data. + /// + /// Name of the project. + /// The command options. + /// The dike. + private static void CopyGeometryData(string projectName, CommandOptions commandOptions, Dike dike) + { + const string geomFolderName = "Geometries"; + var inputGeomFolder = Path.Combine(commandOptions.InputDataDirectory, geomFolderName); + // Calculation folder (ProjectName.Geometries) + var destFolderName = projectName + "." + geomFolderName; + var geomDestFolder = Path.Combine(commandOptions.OuputDataDirectory, destFolderName); + var hasExternalGeometryFiles = Directory.Exists(inputGeomFolder); + if (hasExternalGeometryFiles) { - const string geomFolderName = "Geometries"; - string inputGeomFolder = Path.Combine(commandOptions.InputDataDirectory, geomFolderName); - // Calculation folder (ProjectName.Geometries) - string destFolderName = projectName + "." + geomFolderName; - string geomDestFolder = Path.Combine(commandOptions.OuputDataDirectory, destFolderName); - bool hasExternalGeometryFiles = Directory.Exists(inputGeomFolder); - if (hasExternalGeometryFiles) - { - Directory.CreateDirectory(geomDestFolder); + Directory.CreateDirectory(geomDestFolder); - // update folder references - dike.MapForSoilGeometries2D = destFolderName; - foreach (Location location in dike.Locations) - { - location.MapForSoilGeometries2D = destFolderName; - } + // update folder references + dike.MapForSoilGeometries2D = destFolderName; + foreach (var location in dike.Locations) location.MapForSoilGeometries2D = destFolderName; - foreach (string file in Directory.GetFiles(inputGeomFolder)) + foreach (var file in Directory.GetFiles(inputGeomFolder)) + { + var fileName = Path.GetFileName(file); + if (fileName != null) { - string fileName = Path.GetFileName(file); - if (fileName != null) - { - string destFile = Path.Combine(geomDestFolder, fileName); - File.Copy(file, destFile); - Logger.LogInfo("Copying geometry file: " + destFile); - } + var destFile = Path.Combine(geomDestFolder, fileName); + File.Copy(file, destFile); + Logger.LogInfo("Copying geometry file: " + destFile); } } } + } - /// - /// Loads the dike. - /// - /// The project data folder. - /// - private DamProjectData LoadDike(string projectDataFolder) + /// + /// Loads the dike. + /// + /// The project data folder. + /// + private DamProjectData LoadDike(string projectDataFolder) + { + var damProjectData = new DamProjectData(); + var waterBoard = ImportDirector.Construct(projectDataFolder, null); + var dike = waterBoard.Dikes[0]; + try { - var damProjectData = new DamProjectData(); - WaterBoard waterBoard = ImportDirector.Construct(projectDataFolder, null); - Dike dike = waterBoard.Dikes[0]; - try - { - dike.Validate(); - damProjectData.WaterBoard = waterBoard; - } - catch (Exception) - { - damProjectData.Dispose(); - dike.Dispose(); //dike was not added to Waterboard; Dispose and discard - throw; - } - - return damProjectData; + dike.Validate(); + damProjectData.WaterBoard = waterBoard; } - - /// - /// Writes the fews sensor files. - /// - /// The file. - /// The sensors. - private static void WriteFEWSSensorFiles(FileInfo file, IEnumerable sensors) + catch (Exception) { - var collection = new TimeSerieCollection(); + damProjectData.Dispose(); + dike.Dispose(); //dike was not added to Waterboard; Dispose and discard + throw; + } - foreach (Sensor sensor in sensors) - { - TimeSerie series = collection.AddNewSeries(sensor.Name); - series.TimeStep.Multiplier = 3600; - series.TimeStep.Unit = TimeStepUnit.Second; - series.StartDateTime = new DateTime(2012, 12, 31); - series.EndDateTime = new DateTime(2012, 12, 31, 1, 0, 0); - } + return damProjectData; + } - collection.Save(file.FullName); + /// + /// Writes the fews sensor files. + /// + /// The file. + /// The sensors. + private static void WriteFEWSSensorFiles(FileInfo file, IEnumerable sensors) + { + var collection = new TimeSerieCollection(); + + foreach (var sensor in sensors) + { + var series = collection.AddNewSeries(sensor.Name); + series.TimeStep.Multiplier = 3600; + series.TimeStep.Unit = TimeStepUnit.Second; + series.StartDateTime = new DateTime(2012, 12, 31); + series.EndDateTime = new DateTime(2012, 12, 31, 1, 0, 0); } + + collection.Save(file.FullName); } } \ No newline at end of file Index: DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/CommandOptions.cs =================================================================== diff -u -r5368 -r5384 --- DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/CommandOptions.cs (.../CommandOptions.cs) (revision 5368) +++ DamClients/DamUI/trunk/src/Dam/Tools/CsvToDamxConverter/CommandOptions.cs (.../CommandOptions.cs) (revision 5384) @@ -19,60 +19,23 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. -using System; using CommandLine; -using CommandLine.Text; -using Deltares.Standard.Reflection; -namespace Deltares.Dam.Tools.CsvToDamxConverter +namespace CsvToDamxConverter { - internal class CommandOptions : CommandLineOptionsBase + internal class CommandOptions { - [Option("i", "inputDir", Required = true, HelpText = "Input dike data folder to read. (CSV source files)")] + [Option('i', "inputDir", Required = true, HelpText = "Input dike data folder to read. (CSV source files)")] public string InputDataDirectory { get; set; } - [Option("o", "outputDir", Required = false, DefaultValue = "CsvToDamxOutput", HelpText = "Output folder to save/copy data to. Default value: CsvToDamxOutput")] + [Option('o', "outputDir", Required = false, Default = "CsvToDamxOutput", HelpText = "Output folder to save/copy data to. Default value: CsvToDamxOutput")] public string OuputDataDirectory { get; set; } - [Option("d", "damxFileName", Required = false, DefaultValue = "input.damx", HelpText = "Name of the DAMX file (without a path). Default value: input.damx")] + [Option('d', "damxFileName", Required = false, Default = "input.damx", HelpText = "Name of the DAMX file (without a path). Default value: input.damx")] public string DamXFileName { get; set; } - [Option("s", "sensorDataFileName", Required = false, DefaultValue = "sensordata.xls", HelpText = "Name of the sensor data file. Default value: sensordata.xls")] + [Option('s', "sensorDataFileName", Required = false, Default = "sensordata.xls", HelpText = "Name of the sensor data file. Default value: sensordata.xls")] public string SensorDataFileName { get; set; } - [HelpOption] - public string GetUsage() - { - var help = new HelpText - { - Heading = new HeadingInfo(ThisAssembly.Title, ThisAssembly.AssemblyVersion), - Copyright = new CopyrightInfo(ThisAssembly.Company, 2012), - AdditionalNewLineAfterOption = true, - AddDashesToOption = true - }; - - HandleParsingErrorsInHelp(help); - - //help.AddPreOptionsLine("<>"); - help.AddPreOptionsLine("Usage: CsvToDamXConverter -i DikeCSVInputFolderName [-o OutputFolderName (default=CsvToDamXOutput)] [-d DamXFileName (default=input.damx)] [-s SensorDataFileName (default=sensordata.xls)]"); - help.AddOptions(this); - - return help; - } - - private AssemblyInfoHelper ThisAssembly { get; } = new AssemblyInfoHelper(typeof(Program)); - - void HandleParsingErrorsInHelp(HelpText help) - { - if (LastPostParsingState.Errors.Count > 0) - { - string errors = help.RenderParsingErrorsText(this, 2); // indent with two spaces - if (!string.IsNullOrEmpty(errors)) - { - help.AddPreOptionsLine(string.Concat(Environment.NewLine, "ERROR(S):")); - help.AddPreOptionsLine(errors); - } - } - } } } \ No newline at end of file