forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SANSUserFileParser.py
233 lines (200 loc) · 9.36 KB
/
SANSUserFileParser.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# 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=invalid-name
from collections import namedtuple
import re
# A settings tuple for dark runs.
DarkRunSettings = namedtuple("DarkRunSettings", "run_number time mean mon mon_number")
#pylint: disable=too-many-instance-attributes
class BackCommandParser(object):
def __init__(self):
super(BackCommandParser, self).__init__()
self._uniform_key = ['TIME', 'UAMP']
self._mean_key = ['MEAN', 'TOF']
self._run_key = ['RUN=']
# Standard because they all have the same pattern, eg /TIME/MEAN/RUN=1234
self._first_level_keys_standard = self._uniform_key
# Special because they are not standard, eg MON/RUN=1234/MEAN/TIME or M4/RUN=1234/MEAN/TIME
self._first_level_keys_special = ['MON', "^M[1-9][0-9]*$"]
# Evaluation chains
self._standard_chain = [self._first_level_keys_standard, self._mean_key, self._run_key]
self._special_chain = [self._first_level_keys_special, self._run_key, self._uniform_key, self._mean_key]
self._evaluation_chain = None
# Parse results
self._use_mean = None
self._use_time = None
self._mon = False # This is not being parsed by the standard chain
self._run_number = None
self._mon_number = None
# Key - method mapping
self._method_map = {''.join(self._first_level_keys_standard): self._evaluate_uniform,
''.join(self._first_level_keys_special): self._evaluate_mon,
''.join(self._uniform_key): self._evaluate_uniform,
''.join(self._mean_key):self._evaluate_mean,
''.join(self._run_key):self._evaluate_run}
def _reset_parse_results(self):
self._use_mean = None
self._use_time = None
self._mon = False # This is not being parsed by the standard chain
self._run_number = None
self._mon_number = None
def _get_method(self, key_list):
return self._method_map[''.join(key_list)]
def _evaluate_mean(self, argument):
'''
Evaluates if the argument is either MEAN, TOF or something else.
@param argument: string to investigate
@raise RuntimeError: If the argument cannot be parsed correctly
'''
if argument == self._mean_key[0]:
self._use_mean = True
elif argument == self._mean_key[1]:
self._use_mean = False
else:
raise RuntimeError("BackCommandParser: Cannot parse the MEAN/TOF value. "
"Read in " + argument + ". "
+ "Make sure it is set correctly.")
def _evaluate_uniform(self, argument):
'''
Evaluates if the argument is either TIME, UAMP or something else.
@param argument: string to investigate
@raise RuntimeError: If the argument cannot be parsed correctly
'''
if argument == self._uniform_key[0]:
self._use_time = True
elif argument == self._uniform_key[1]:
self._use_time = False
else:
raise RuntimeError("BackCommandParser: Cannot parse the TIME/UAMP value. "
"Read in " + argument + ". "
+ "Make sure it is set correctly.")
def _evaluate_run(self, argument):
'''
Evaluates if the argument is RUN=
@param argument: string to investigate
@raise RuntimeError: If the argument cannot be parsed correctly
'''
if not argument.startswith(self._run_key[0]):
raise RuntimeError("BackCommandParser: Cannot parse the RUN= value. "
"Read in " + argument + ". "
+ "Make sure it is set correctly.")
# Remove the Run= part and take the rest as the run parameter. At this point we cannot
# check if it is a valid run
self._run_number = argument.replace(self._run_key[0], "")
def _evaluate_mon(self, argument):
'''
Evaluates which detector to use. At this point the validty of this has already been checked, so
we can just take it as is.
@param argument: string to investigate
@raise RuntimeError: If the argument cannot be parsed correctly
'''
if argument == self._first_level_keys_special[0]: # Check if MON
self._mon = True
elif re.match(self._first_level_keys_special[1], argument):
self._mon = True
mon_number = argument.replace("M", "")
self._mon_number=int(mon_number)
else:
raise RuntimeError("BackCommandParser: Cannot parse the MON value. "
"Read in " + argument + ". "
+ "Make sure it is set correctly.")
def can_attempt_to_parse(self, arguments):
'''
Check if the parameters can be parsed with this class
@param param: a string to be parsed
@returns true if it can be parsed
'''
# Convert to capital and split the string
to_check = self._prepare_argument(arguments)
# We expect 3 arguemnts for the standard chain and 4 arguments
# for the special monitor chain
if len(to_check) != 4 and len(to_check) != 3:
return False
can_parse = False
# Check if part of the first entry of the standard chain
if self._is_parsable_with_standard_chain(to_check):
self._evaluation_chain = self._standard_chain
can_parse = True
elif self._is_parsable_with_special_chain(to_check):
self._evaluation_chain = self._special_chain
can_parse = True
return can_parse
def _is_parsable_with_special_chain(self, argument):
'''
Check if the first entry corresponds to a standard chain, i.e. starting with monitor and followed by run spec.
@param arguments: the string list containing the arguments
'''
can_parse = False
if argument[0] == self._first_level_keys_special[0] and argument[1].startswith(self._special_chain[1][0]):
can_parse = True
elif re.match(self._first_level_keys_special[1], argument[0]) and argument[1].startswith(self._special_chain[1][0]):
can_parse = True
return can_parse
def _is_parsable_with_standard_chain(self, argument):
'''
Check if the first entry corresponds to a standard chain, i.e. not starting with a monitor
@param arguments: the string list containing the arguments
'''
if argument[0] in self._standard_chain[0]:
return True
else:
return False
def _prepare_argument(self, argumentstring):
'''
Takes an argument string and returns splits it into a list
@param argumentstring: the input
@returns a list
'''
# Convert to capital and split the string
split_arguments = argumentstring.split('/')
# Make everything to upper except for the file name
for index in range(0, len(split_arguments)):
arg = split_arguments[index]
if "=" in arg:
parts = arg.split("=")
# If there are not exactly two elements, then the input is invalid
# We need Run=somerun
if len(parts) != 2:
return []
new_arg = "RUN=" + parts[1].strip()
split_arguments[index] = new_arg
else:
split_arguments[index] = arg.strip().upper()
return [element.strip() for element in split_arguments]
def parse_and_set(self, arguments):
'''
Parse the values of the parameter string and set them on the reducer
@param arguments: the string containing the arguments
@returns an error message if something went wrong or else nothing
'''
# Parse the arguments.
self._parse(arguments)
# Now pass the arguments back in a defined format
setting = DarkRunSettings(mon = self._mon,
run_number = self._run_number,
time = self._use_time,
mean = self._use_mean,
mon_number = self._mon_number)
# Reset the parse results just in case we want to use it again
self._reset_parse_results()
return setting
def _parse(self, arguments):
'''
Parse the arguments and store the results
@param arguments: the string containing the arguments
@raise RuntimeError: If the argument cannot be parsed correctly
'''
to_parse = self._prepare_argument(arguments)
if not self.can_attempt_to_parse(arguments):
raise RuntimeError("BackCommandParser: Cannot parse provided arguments."
"They are not compatible")
index = 0
for element in to_parse:
key = self._evaluation_chain[index]
evaluation_method = self._get_method(key)
evaluation_method(element)
index += 1