-
Notifications
You must be signed in to change notification settings - Fork 122
/
ExampleSaveAscii.py
84 lines (62 loc) · 3.45 KB
/
ExampleSaveAscii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=no-init
"""
This is an example Python algorithm, showing how
to write a workspace to file in ascii format.
Note that the SaveAscii algorithm should be used instead in most cases.
"""
# This __future__ import is for Python 2/3 compatibility
from mantid.kernel import *
from mantid.api import *
class ExampleSaveAscii(PythonAlgorithm):
def category(self):
# defines the category the algorithm will be put in the algorithm browser
return 'Examples'
def PyInit(self):
# Declare properties
# Declare a property for the output filename with a default of default_output.txt
self.declareProperty(FileProperty(name='OutputFilename',
defaultValue='default_output.txt',
action=FileAction.Save,
extensions=['txt']))
# Declare a property for the input workspace which will be written to file
self.declareProperty(WorkspaceProperty(name='InputWorkspace',
defaultValue='',
direction=Direction.Input),
doc='Documentation for this property')
def PyExec(self):
# Save the workspace to file in ascii format
input_workspace = self.getProperty('InputWorkspace').value
# Open the file with write permissions
# The 'with' statement will take care of closing the file when we are done,
# or if an error occurs
with open(self.getPropertyValue('OutputFilename'), 'w') as file_handle:
# Get the units from the workspace to use in the file header
x_label = input_workspace.getAxis(0).getUnit().caption()
y_label = input_workspace.getAxis(1).getUnit().caption()
# Write column header to file
file_handle.write('# ' + x_label + ' , ' + y_label + ' , E\n')
# Loop through each spectrum histogram
for histogram_n in range(input_workspace.getNumberHistograms()):
# Read the histogram data from the workspace
xdata = input_workspace.readX(histogram_n)
ydata = input_workspace.readY(histogram_n)
edata = input_workspace.readE(histogram_n)
# Write the spectrum histogram index to file
file_handle.write(str(histogram_n+1) + '\n') # +1 to convert to 1 indexed
# Loop through each bin
for bin_n in range(input_workspace.blocksize()):
# Calculate bin center from bin boundaries
bin_center = xdata[bin_n]+(xdata[bin_n+1]-xdata[bin_n])/2.
# Write the data for the nth bin to file
# with a precision of 4 decimal places
file_handle.write('{0:.4f},{1:.4f},{2:.4f}\n'.format(bin_center,
ydata[bin_n],
edata[bin_n]))
# Register algorithm with Mantid
AlgorithmFactory.subscribe(ExampleSaveAscii)