forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetIPTS.py
93 lines (72 loc) · 3.35 KB
/
GetIPTS.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
# 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 +
from mantid.api import AlgorithmFactory, FileFinder, PythonAlgorithm
from mantid.kernel import ConfigService, Direction, IntBoundedValidator, \
StringListValidator
class GetIPTS(PythonAlgorithm):
def category(self):
return "Utility\\ORNL"
def name(self):
return "GetIPTS"
def summary(self):
return "Extracts the IPTS number from a run using FileFinder"
def getValidInstruments(self):
instruments = ['']
for name in ['SNS', 'HFIR']:
facility = ConfigService.getFacility(name)
facilityInstruments = sorted([item.shortName()
for item in facility.instruments()
if item != 'DAS'])
instruments.extend(facilityInstruments)
return instruments
def findFile(self, instrument, runnumber):
# start with run and check the five before it
runIds = list(range(runnumber, runnumber-6, -1))
# check for one after as well
runIds.append(runnumber + 1)
runIds = [str(runId) for runId in runIds if runId > 0]
# prepend non-empty instrument name for FileFinder
if len(instrument) > 0:
runIds = ['%s_%s' % (instrument, runId) for runId in runIds]
# look for a file
for runId in runIds:
self.log().information("Looking for '%s'" % runId)
try:
return FileFinder.findRuns(runId)[0]
except RuntimeError:
pass # just keep looking
# failed to find any is an error
raise RuntimeError("Cannot find IPTS directory for '%s'"
% runnumber)
def getIPTSLocal(self, instrument, runnumber):
filename = self.findFile(instrument, runnumber)
# convert to the path to the proposal
location = filename.find('IPTS')
if location <= 0:
raise RuntimeError("Failed to determine IPTS directory "
+ "from path '%s'" % filename)
location = filename.find('/', location)
direc = filename[0:location+1]
return direc
def PyInit(self):
self.declareProperty('RunNumber', defaultValue=0,
direction=Direction.Input,
validator=IntBoundedValidator(lower=1),
doc="Extracts the IPTS number for a run")
instruments = self.getValidInstruments()
self.declareProperty('Instrument', '',
StringListValidator(instruments),
"Empty uses default instrument")
self.declareProperty('Directory', '',
direction=Direction.Output)
def PyExec(self):
instrument = self.getProperty('Instrument').value
runnumber = self.getProperty('RunNumber').value
direc = self.getIPTSLocal(instrument, runnumber)
self.setPropertyValue('Directory', direc)
self.log().notice('IPTS directory is: %s' % direc)
AlgorithmFactory.subscribe(GetIPTS)