From ad9ec169efc55c91ce0b86801e084a9665153bc5 Mon Sep 17 00:00:00 2001
From: Stefanos Doumpoulakis <dubulak@gmail.com>
Date: Fri, 30 Sep 2022 17:22:10 +0300
Subject: [PATCH] THC pollutant metric is optional when CH4, NMHC present

---
 .../Reader/DrivingCycleDataReader.cs          | 32 +++++++++++++++++--
 .../Models/Simulation/DrivingCycleTests.cs    |  6 ++--
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/VectoCore/VectoCore/InputData/Reader/DrivingCycleDataReader.cs b/VectoCore/VectoCore/InputData/Reader/DrivingCycleDataReader.cs
index d31bbded99..36ea70b563 100644
--- a/VectoCore/VectoCore/InputData/Reader/DrivingCycleDataReader.cs
+++ b/VectoCore/VectoCore/InputData/Reader/DrivingCycleDataReader.cs
@@ -482,7 +482,28 @@ namespace TUGraz.VectoCore.InputData.Reader
 				return true;
 			}
 
-            public abstract IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired);
+			protected static bool CheckColumnsPredicatedOnAnotherMissing(DataColumnCollection header, string[] cols, string[] others, bool throwExceptions)
+			{ 
+				var otherCount = header.Cast<DataColumn>().Count(other => others.Select(x => x.ToLowerInvariant())
+					.Contains(other.ColumnName.ToLowerInvariant()));
+
+				if (otherCount < others.Count()) {
+					var headerStr = header.Cast<DataColumn>().Select(col => col.ColumnName.ToLowerInvariant()).ToArray();
+					var diff = cols.Select(x => x.ToLowerInvariant()).Except(headerStr).ToList();
+					
+					if (diff.Any()) {
+						if (throwExceptions) {
+							throw new VectoException("Column(s) required: " + string.Join(", ", diff));
+						}
+
+						return false;
+					}
+				}
+
+				return true;
+			}
+
+			public abstract IEnumerable<DrivingCycleData.DrivingCycleEntry> Parse(DataTable table, bool crossWindRequired);
 		}
 
 		private class DistanceBasedCycleDataParser : AbstractCycleDataParser
@@ -889,7 +910,6 @@ namespace TUGraz.VectoCore.InputData.Reader
 					Fields.CombustionEngineTorque,
 					Fields.COMassFlow,
 					Fields.NOxMassFlow,
-					Fields.THCMassFlow,
 					Fields.CO2MassFlow,
 					Fields.PMNumberFlow
 				};
@@ -926,7 +946,13 @@ namespace TUGraz.VectoCore.InputData.Reader
 						header, 
 						new[] { Fields.CH4MassFlow, Fields.NMHCMassFlow }, 
 						FuelTypeHelper.GetLabelsForNonGaseous("fc_"), 
-						throwExceptions);
+						throwExceptions)
+					&& CheckColumnsPredicatedOnAnotherMissing(
+						header,
+						new[] { Fields.THCMassFlow },
+						new[] { Fields.CH4MassFlow, Fields.NMHCMassFlow },
+						throwExceptions
+					);
 			}
 		}
 
diff --git a/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs b/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs
index 32726cc8ee..2f88661ba0 100644
--- a/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs
+++ b/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs
@@ -215,8 +215,8 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
 			// Verification test simulation
 			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_DIESEL CI>,<tq_eng>,<CO>,<NOx>,<THC>,<CO2>,<PN>", CycleType.VTP),
 			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_DIESEL CI>,<gear>,<tq_eng>,<CO>,<NOx>,<THC>,<CO2>,<PN>", CycleType.VTP),
-			TestCase("<t>,<v>,<n_eng>,<Pel_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_DIESEL CI>,<gear>,<tq_eng>,<CH4>,<CO>,<NMHC>,<NOx>,<THC>,<CO2>,<PN>", CycleType.VTP),
-			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_NG CI>,<tq_eng>,<CH4>,<CO>,<NMHC>,<NOx>,<THC>,<CO2>,<PN>", CycleType.VTP),
+			TestCase("<t>,<v>,<n_eng>,<Pel_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_DIESEL CI>,<gear>,<tq_eng>,<CH4>,<CO>,<NMHC>,<NOx>,<CO2>,<PN>", CycleType.VTP),
+			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_NG CI>,<tq_eng>,<CH4>,<CO>,<NMHC>,<NOx>,<CO2>,<PN>", CycleType.VTP),
 			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_NG CI>,<fc_DIESEL CI>,<tq_eng>,<CH4>,<CO>,<NMHC>,<NOx>,<THC>,<CO2>,<PN>", CycleType.VTP)
 		]
 		public void DrivingCycle_AutoDetect(string cycle, CycleType type)
@@ -244,6 +244,8 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
 				CycleType.VTP),
 			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_NG CI>,<tq_eng>,<CH4>,<NMHC>,<CO>,<NOx>,<THC>,<CO2>", 
 				CycleType.VTP),
+			TestCase("<t>,<v>,<n_eng>,<n_fan>,<tq_left>,<tq_right>,<n_wh_left>,<n_wh_right>,<fc_NG CI>,<tq_eng>,<CO>,<NOx>,<CO2>,<PN>", 
+				CycleType.VTP),
 		]
 		public void DrivingCycle_AutoDetect_Exception(string cycle, CycleType type)
 		{
-- 
GitLab