forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConjoinFiles.py
77 lines (64 loc) · 2.82 KB
/
ConjoinFiles.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
# 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,invalid-name
from mantid.api import *
from mantid.kernel import *
from mantid.simpleapi import *
import os
class ConjoinFiles(PythonAlgorithm):
def category(self):
return "DataHandling\\Text"
def seeAlso(self):
return [ "ConjoinWorkspaces" ]
def name(self):
return "ConjoinFiles"
def summary(self):
return "Conjoin two file-based workspaces."
#pylint: disable=too-many-arguments
def __load(self, directory, instr, run, loader, exts, wksp):
filename = None
for ext in exts:
filename = "%s_%s%s" % (instr, str(run), ext)
if len(directory) > 0:
filename = os.path.join(directory, filename)
if not os.path.exists(filename):
continue
try:
self.log().information("Trying to load '%s'" % filename)
loader(Filename=filename, OutputWorkspace=wksp)
return
except (RuntimeError, IOError, ValueError) as e:
logger.information(str(e))
raise RuntimeError("Failed to load run %s from file %s" % (str(run), filename))
def PyInit(self):
greaterThanZero = IntArrayBoundedValidator(lower=0)
self.declareProperty(IntArrayProperty("RunNumbers",values=[0],validator=greaterThanZero), doc="Run numbers")
self.declareProperty(WorkspaceProperty("OutputWorkspace", "", direction=Direction.Output))
self.declareProperty(FileProperty("Directory", "", FileAction.OptionalDirectory))
def PyExec(self):
# generic stuff for running
wksp = self.getPropertyValue("OutputWorkspace")
runs = self.getProperty("RunNumbers")
instr = config.getInstrument().shortName()
directory = self.getPropertyValue("Directory").strip()
# change here if you want something other than gsas files
exts = ['.txt', '.gsa']
loader = LoadGSS
# load things and conjoin them
first = True
for run in runs.value:
run = str(run)
if first:
self.__load(directory, instr, run, loader, exts, wksp)
first = False
else:
self.__load(directory, instr, run, loader, exts, run)
ConjoinWorkspaces(InputWorkspace1=wksp, InputWorkspace2=run, CheckOverlapping=False)
if mtd.doesExist(run):
DeleteWorkspace(run)
self.setProperty("OutputWorkspace", mtd[wksp])
AlgorithmFactory.subscribe(ConjoinFiles)