Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pylint: disable = no-init, invalid-name, line-too-long, eval-used, unused-argument
- from mantid.kernel import *
- from mantid.api import *
- from mantid.simpleapi import *
- class SaveTableAscii(PythonAlgorithm):
- """ Save a table workspace to an ascii file
- """
- # By Duc Le (2016)
- def category(self):
- return 'PythonAlgorithms'
- def name(self):
- return 'SaveTableAscii'
- def summary(self):
- return 'Saves a table workspace to an ascii file.'
- def PyInit(self):
- self.declareProperty(ITableWorkspaceProperty(name='InputWorkspace', defaultValue='', direction=Direction.Input),
- doc='Input TableWorkspace.')
- self.declareProperty(FileProperty(name='OutputFile', defaultValue='', action=FileAction.Save),
- doc='Output file name.')
- self.declareProperty(name='Separator', defaultValue='\t', validator=StringMandatoryValidator(),
- doc='Separator for columns (default: tab)')
- self.declareProperty(name='LineSeparator', defaultValue='\n', validator=StringMandatoryValidator(),
- doc='Separator for lines (default: new line)')
- self.declareProperty(name='CommentStyle', defaultValue='#', validator=StringMandatoryValidator(),
- doc='Symbol to denote a comment line (default: #)')
- def PyExec(self):
- table = mtd[self.getPropertyValue('InputWorkspace')]
- n = table.columnCount()
- m = table.rowCount()
- sep = self.getPropertyValue('Separator').decode('string_escape')
- nl = self.getPropertyValue('LineSeparator').decode('string_escape')
- cs = self.getPropertyValue('CommentStyle').decode('string_escape')
- output = open(self.getPropertyValue('OutputFile'), "w")
- # Prints the first line as a comment with the column titles.
- output.write(cs+" "+sep.join(table.keys())+nl)
- # Prints the other lines
- for lin in range(m):
- output.write(sep.join([str(table.row(lin)[keys]) for keys in table.keys()])+nl)
- output.close()
- AlgorithmFactory.subscribe(SaveTableAscii)
- def SaveTableAscii(*args,**kwargs):
- """ Saves a Table Workspace to a file
- SaveTableAscii('inws', 'outfile')
- SaveTableAscii(InputWorkspace='inws', OutputFile='outfile')
- SaveTableAscii(InputWorkspace='inws', OutputFile='outfile', Separator='\\t', LineSeparator='\\n', CommentStyle='#')
- """
- # Parses the arguments list
- argin = {}
- argc = ['InputWorkspace','OutputFile','Separator','LineSeparator','CommentStyle']
- argdef = ['','','\t','\n','#']
- for ia in range(5):
- argin[argc[ia]] = argdef[ia]
- for ia in range(len(args)):
- argin[argc[ia]] = args[ia]
- # In case the user puts one of the 'args' parameters in 'kwargs'
- for ia in range(5):
- for key, value in kwargs.iteritems():
- if key == argc[ia]:
- argin[key] = value
- table = mtd[argin['InputWorkspace']]
- n = table.columnCount()
- m = table.rowCount()
- sep = argin['Separator']
- nl = argin['LineSeparator']
- cs = argin['CommentStyle']
- output = open(argin['OutputFile'], "w")
- # Prints the first line as a comment with the column titles.
- output.write(cs+" "+sep.join(table.keys())+nl)
- # Prints the other lines
- for lin in range(m):
- output.write(sep.join([str(table.row(lin)[keys]) for keys in table.keys()])+nl)
- output.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement