diff --git a/Code/Mantid/Framework/Algorithms/src/AddSampleLog.cpp b/Code/Mantid/Framework/Algorithms/src/AddSampleLog.cpp index 5660a83383c57594440dc293fd3809720eb316ba..31036f03bad4cc7d2f6b4222ea3a2680810c30f0 100644 --- a/Code/Mantid/Framework/Algorithms/src/AddSampleLog.cpp +++ b/Code/Mantid/Framework/Algorithms/src/AddSampleLog.cpp @@ -36,6 +36,7 @@ void AddSampleLog::init() { declareProperty("LogType", "String", boost::make_shared<StringListValidator>(propOptions), "The type that the log data will be."); + declareProperty("LogUnit", "", "The units of the log"); } void AddSampleLog::exec() { @@ -48,6 +49,7 @@ void AddSampleLog::exec() { // get the data that the user wants to add std::string propName = getProperty("LogName"); std::string propValue = getProperty("LogText"); + std::string propUnit = getProperty("LogUnit"); std::string propType = getPropertyValue("LogType"); // Remove any existing log @@ -57,6 +59,7 @@ void AddSampleLog::exec() { if (propType == "String") { theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue)); + theRun.getProperty(propName)->setUnits(propUnit); return; } @@ -94,6 +97,7 @@ void AddSampleLog::exec() { theRun.addLogData(tsp); } } + theRun.getProperty(propName)->setUnits(propUnit); } } // namespace Algorithms diff --git a/Code/Mantid/Framework/Algorithms/test/AddSampleLogTest.h b/Code/Mantid/Framework/Algorithms/test/AddSampleLogTest.h index 59b1298df8a23cbdf57d0229de8869fd2d36f100..a3d1eee5ef7e86ec80a46c6d05f72ae96c1c06b9 100644 --- a/Code/Mantid/Framework/Algorithms/test/AddSampleLogTest.h +++ b/Code/Mantid/Framework/Algorithms/test/AddSampleLogTest.h @@ -71,9 +71,18 @@ public: ExecuteAlgorithm(ws, "Another Name", "Number Series", "123456789", 123456789); } + void test_Units() + { + MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(10,10); + ws->mutableRun().setStartAndEndTime(DateAndTime("2013-12-18T13:40:00"),DateAndTime("2013-12-18T13:42:00")); + ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234,false,"myUnit"); + ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963,false,"differentUnit"); + ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0,false,"stringUnit"); + } + template<typename T> void ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName, std::string LogType, std::string LogText, - T expectedValue, bool fails=false) + T expectedValue, bool fails=false, std::string LogUnit="") { //add the workspace to the ADS AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary", testWS); @@ -86,6 +95,7 @@ public: alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary"); alg.setPropertyValue("LogName", LogName); alg.setPropertyValue("LogText", LogText); + alg.setPropertyValue("LogUnit", LogUnit); alg.setPropertyValue("LogType", LogType); TS_ASSERT_THROWS_NOTHING(alg.execute()) if (fails) @@ -123,7 +133,6 @@ public: TS_ASSERT_EQUALS(testProp->firstTime(),DateAndTime("2013-12-18T13:40:00")); TS_ASSERT_DELTA(testProp->firstValue(), expectedValue, 1e-5); } - //cleanup AnalysisDataService::Instance().remove(output->getName()); diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py index 181f645ca825577de0a02e7540e11838493e5fc7..9695bea07ae335e3cdef9886cc30cd810b0cf1cf 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/AddSampleLogMultiple.py @@ -24,6 +24,9 @@ class AddSampleLogMultiple(PythonAlgorithm): self.declareProperty(StringArrayProperty('LogValues', ''), doc='Comma separated list of log values') + self.declareProperty(StringArrayProperty('LogUnits', ''), + doc='Comma separated list of log units') + self.declareProperty('ParseType', True, doc='Determine the value type by parsing the string') @@ -32,12 +35,17 @@ class AddSampleLogMultiple(PythonAlgorithm): workspace = self.getPropertyValue('Workspace') log_names = self.getProperty('LogNames').value log_values = self.getProperty('LogValues').value + log_units = self.getProperty('LogUnits').value parse_type = self.getProperty('ParseType').value + if len(log_units) == 0: + log_units = [""]*len(log_names) + for idx in range(0, len(log_names)): - # Get the name and value + # Get the name, value, and unit name = log_names[idx] value = log_values[idx] + unit = log_units[idx] # Try to get the correct type value_type = 'String' @@ -57,6 +65,7 @@ class AddSampleLogMultiple(PythonAlgorithm): alg.setProperty('LogType', value_type) alg.setProperty('LogName', name) alg.setProperty('LogText', value) + alg.setProperty('LogUnit', unit) alg.execute() @@ -65,9 +74,11 @@ class AddSampleLogMultiple(PythonAlgorithm): log_names = self.getProperty('LogNames').value log_values = self.getProperty('LogValues').value + log_units = self.getProperty('LogUnits').value num_names = len(log_names) num_values = len(log_values) + num_units = len(log_units) # Ensure there is at leats 1 log name if num_names == 0: @@ -80,6 +91,9 @@ class AddSampleLogMultiple(PythonAlgorithm): if num_names > 0 and num_values > 0 and num_names != num_values: issues['LogValues'] = 'Number of log values must match number of log names' + if num_names > 0 and num_units != 0 and num_units != num_names: + issues['LogUnits'] = 'Number of log units must be 0 or match the number of log names' + return issues diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py index 8dda9002746d6dbda8878e86e9abecc2d45e1793..07ab2000ef89762a197ce4e5768e0a9b75f9b442 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/AddSampleLogMultipleTest.py @@ -24,16 +24,19 @@ class AddSampleLogMultipleTest(unittest.TestCase): DeleteWorkspace(self._workspace) - def _validate_sample_logs(self, names, values, types): + def _validate_sample_logs(self, names, values, types, units=None): """ Validates sample logs set on workspace. @param names List of sample log names @param values List of sample log values @param types List of sample log types + @param units List of sample unit names """ - logs = self._workspace.getSampleDetails().getLogData() + logs = self._workspace.getRun().getProperties() matched_logs = list() + if units==None: + units=['']*len(names) for log in logs: if log.name in names: @@ -42,6 +45,7 @@ class AddSampleLogMultipleTest(unittest.TestCase): self.assertEqual(log.value, values[idx]) self.assertEqual(log.type, types[idx]) + self.assertEqual(log.units, units[idx]) self.assertEqual(matched_logs, names) @@ -75,6 +79,35 @@ class AddSampleLogMultipleTest(unittest.TestCase): self._validate_sample_logs(names, values, types) + def test_units(self): + """ + Test validation for wrong number of units + """ + names = ['a', 'b', 'c'] + values = ['one', 'two', 'three'] + units = ['unit_a', 'unit_b', 'unit_c'] + types = ['string', 'string', 'string'] + + AddSampleLogMultiple(Workspace=self._workspace, + LogNames=names, + LogValues=values, + LogUnits=units) + self._validate_sample_logs(names, values, types,units) + + def test_validation_wrong_units(self): + """ + Test validation for wrong number of units + """ + names = ['a', 'b', 'c'] + values = ['one', 'two', 'three'] + units = ['unit_a', 'unit_b'] + + self.assertRaises(RuntimeError, + AddSampleLogMultiple, + Workspace=self._workspace, + LogNames=names, + LogValues=values, + LogUnits=units) def test_validation_no_names(self): """