Advertisement
Guest User

Mantid SaveTableAscii

a guest
Apr 5th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. #pylint: disable = no-init, invalid-name, line-too-long, eval-used, unused-argument
  2. from mantid.kernel import *
  3. from mantid.api import *
  4. from mantid.simpleapi import *
  5.  
  6. class SaveTableAscii(PythonAlgorithm):
  7.     """ Save a table workspace to an ascii file
  8.    """
  9.    
  10.     # By Duc Le (2016)
  11.    
  12.     def category(self):
  13.         return 'PythonAlgorithms'
  14.  
  15.     def name(self):
  16.         return 'SaveTableAscii'
  17.  
  18.     def summary(self):
  19.         return 'Saves a table workspace to an ascii file.'
  20.  
  21.     def PyInit(self):
  22.         self.declareProperty(ITableWorkspaceProperty(name='InputWorkspace', defaultValue='', direction=Direction.Input),
  23.                              doc='Input TableWorkspace.')
  24.         self.declareProperty(FileProperty(name='OutputFile', defaultValue='', action=FileAction.Save),
  25.                              doc='Output file name.')
  26.         self.declareProperty(name='Separator', defaultValue='\t', validator=StringMandatoryValidator(),
  27.                              doc='Separator for columns (default: tab)')
  28.         self.declareProperty(name='LineSeparator', defaultValue='\n', validator=StringMandatoryValidator(),
  29.                              doc='Separator for lines (default: new line)')
  30.         self.declareProperty(name='CommentStyle', defaultValue='#', validator=StringMandatoryValidator(),
  31.                              doc='Symbol to denote a comment line (default: #)')
  32.  
  33.     def PyExec(self):
  34.         table = mtd[self.getPropertyValue('InputWorkspace')]
  35.         n = table.columnCount()
  36.         m = table.rowCount()
  37.         sep = self.getPropertyValue('Separator').decode('string_escape')
  38.         nl = self.getPropertyValue('LineSeparator').decode('string_escape')
  39.         cs = self.getPropertyValue('CommentStyle').decode('string_escape')
  40.         output = open(self.getPropertyValue('OutputFile'), "w")
  41.         # Prints the first line as a comment with the column titles.
  42.         output.write(cs+" "+sep.join(table.keys())+nl)
  43.         # Prints the other lines
  44.         for lin in range(m):
  45.             output.write(sep.join([str(table.row(lin)[keys]) for keys in table.keys()])+nl)
  46.         output.close()
  47.  
  48. AlgorithmFactory.subscribe(SaveTableAscii)
  49.  
  50. def SaveTableAscii(*args,**kwargs):
  51.     """ Saves a Table Workspace to a file
  52.    
  53.        SaveTableAscii('inws', 'outfile')
  54.        SaveTableAscii(InputWorkspace='inws', OutputFile='outfile')
  55.        SaveTableAscii(InputWorkspace='inws', OutputFile='outfile', Separator='\\t', LineSeparator='\\n', CommentStyle='#')
  56.    """
  57.    
  58.     # Parses the arguments list
  59.     argin = {}
  60.     argc = ['InputWorkspace','OutputFile','Separator','LineSeparator','CommentStyle']
  61.     argdef = ['','','\t','\n','#']
  62.     for ia in range(5):
  63.         argin[argc[ia]] = argdef[ia]
  64.     for ia in range(len(args)):
  65.         argin[argc[ia]] = args[ia]
  66.        
  67.     # In case the user puts one of the 'args' parameters in 'kwargs'
  68.     for ia in range(5):
  69.         for key, value in kwargs.iteritems():
  70.             if key == argc[ia]:
  71.                 argin[key] = value
  72.    
  73.     table = mtd[argin['InputWorkspace']]
  74.     n = table.columnCount()
  75.     m = table.rowCount()
  76.     sep = argin['Separator']
  77.     nl = argin['LineSeparator']
  78.     cs = argin['CommentStyle']
  79.     output = open(argin['OutputFile'], "w")
  80.     # Prints the first line as a comment with the column titles.
  81.     output.write(cs+" "+sep.join(table.keys())+nl)
  82.     # Prints the other lines
  83.     for lin in range(m):
  84.         output.write(sep.join([str(table.row(lin)[keys]) for keys in table.keys()])+nl)
  85.     output.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement