Skip to content

Commit

Permalink
Enabled support for UTC time. Refs #10845."
Browse files Browse the repository at this point in the history
On branch feature/10845_export_utc
* modified:   Mantid/Framework/PythonInterface/plugins/algorithms/ExportExperimentLog.py
* modified:   Mantid/Framework/PythonInterface/test/python/plugins/algorithms/ExportExperimentLogTest.py
  • Loading branch information
wdzhou committed Dec 29, 2014
1 parent cb39e21 commit a53cc8e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
Expand Up @@ -453,7 +453,7 @@ def _getSampleLogsValue(self):
if logclass == "StringPropertyWithValue":
propertyvalue = logproperty.value
# operationtype = self._sampleLogOperations[il]
if operationtype.lower() == "localtime":
if operationtype.lower().count("time") > 0:
propertyvalue = self._convertLocalTimeString(propertyvalue)
elif logclass == "FloatPropertyWithValue":
propertyvalue = logproperty.value
Expand Down Expand Up @@ -487,20 +487,41 @@ def _convertLocalTimeString(self, utctimestr):
from datetime import datetime
from dateutil import tz

# Make certain that the input is utc time string
utctimestr = str(utctimestr)

self.log().information("Input UTC time = %s" % (utctimestr))
# Return if time zone is UTC (no need to convert)
if self._timezone == "UTC":
return utctimestr

# Convert
self.log().debug("Input UTC time = %s" % (utctimestr))

from_zone = tz.gettz('UTC')
to_zone = tz.gettz(self._timezone)

# Determine the parsing format
if utctimestr.count("T") == 0:
srctimeformat = '%Y-%m-%d %H:%M:%S.%f'
else:
srctimeformat = '%Y-%m-%dT%H:%M:%S.%f'

try:
extra = ""
if utctimestr.count(".") == 1:
# Time format's microsecond part %.f can take only 6 digit
tail = utctimestr.split(".")[1]
extralen = len(tail)-6
extra = utctimestr[-extralen:]
utctimestr = utctimestr[0:-extralen]
utctime = datetime.strptime(utctimestr, '%Y-%m-%dT%H:%M:%S.%f')
if extralen > 0:
extra = utctimestr[-extralen:]
utctimestr = utctimestr[0:-extralen]
elif utctimestr.count(".") == 0:
# There is no .%f part in source time string:
srctimeformat = srctimeformat.split(".")[0]
else:
# Un perceived situation
raise NotImplementedError("Is it possible to have time as %s?" % (utctimestr))
utctime = datetime.strptime(utctimestr, srctimeformat)
except ValueError as err:
self.log().error("Unable to convert time string %s. Error message: %s" % (utctimestr, str(err)))
raise err
Expand Down
Expand Up @@ -629,6 +629,78 @@ def test_sortRecordFileOverride(self):

return

def test_exportFileUTC(self):
""" Test to export logs without header file
"""
print "TEST UTC"
# Generate the matrix workspace with some logs
ws = self.createTestWorkspace()
AnalysisDataService.addOrReplace("TestMatrixWS", ws)

# Test algorithm
alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS",
OutputFilename = "TestRecord001utc.txt",
SampleLogNames = ["run_number", "duration", "run_start", "proton_charge", "proton_charge", "proton_charge"],
SampleLogTitles = ["RUN", "Duration", "StartTime", "ProtonCharge", "MinPCharge", "MeanPCharge"],
SampleLogOperation = [None, None, "time", "sum", "min", "average"],
TimeZone = 'UTC',
FileMode = "new")

# Validate
self.assertTrue(alg_test.isExecuted())

# Locate file
outfilename = alg_test.getProperty("OutputFilename").value
try:
print "Output file is %s. " % (outfilename)
ifile = open(outfilename)
lines = ifile.readlines()
ifile.close()
except IOError as err:
print "Unable to open file %s. " % (outfilename)
self.assertTrue(False)
return

# Last line cannot be empty, i.e., before EOF '\n' is not allowed
lastline = lines[-1]
self.assertTrue(len(lastline.strip()) > 0)

# Number of lines
self.assertEquals(len(lines), 2)

# Check line
firstdataline = lines[1]
terms = firstdataline.strip().split("\t")
self.assertEquals(len(terms), 6)

# Get property
runstarttime = ws.run().getProperty("run_start").value
pchargelog = ws.getRun().getProperty("proton_charge").value
sumpcharge = numpy.sum(pchargelog)
minpcharge = numpy.min(pchargelog)
avgpcharge = numpy.average(pchargelog)

# run start time
v2 = str(terms[2])
self.assertEqual(runstarttime, v2.split(".")[0])

v3 = float(terms[3])
self.assertAlmostEqual(sumpcharge, v3)

v4 = float(terms[4])
self.assertAlmostEqual(minpcharge, v4)

v5 = float(terms[5])
self.assertAlmostEqual(avgpcharge, v5)

#
# # Remove generated files
#os.remove(outfilename)
AnalysisDataService.remove("TestMatrixWS")

return


def createTestWorkspace(self, run=23456):
""" Create a workspace for testing against with ideal log values
Expand Down

0 comments on commit a53cc8e

Please sign in to comment.