forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoadSINQFile.py
118 lines (98 loc) · 4.65 KB
/
LoadSINQFile.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
# 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
#--------------------------------------------------------------
# Algorithm which loads a SINQ file. It matches the instrument
# and the right dictionary file and then goes away and calls
# LoadFlexiNexus and others to load the file.
#
# Mark Koennecke, November 2012
#--------------------------------------------------------------
from mantid.api import AlgorithmFactory
from mantid.api import PythonAlgorithm, FileProperty, FileAction, WorkspaceProperty
from mantid.kernel import Direction, StringListValidator
import mantid.simpleapi
from mantid import config
import os.path
import numpy as np
import re
#--------- place to look for dictionary files
class LoadSINQFile(PythonAlgorithm):
def category(self):
return "DataHandling\\Nexus"
def summary(self):
return "Load a SINQ file with the right dictionary."
def PyInit(self):
#global dictsearch
instruments=["AMOR","BOA","DMC","FOCUS","HRPT","MARSI","MARSE","POLDI",
"RITA-2","SANS","SANS2","TRICS"]
self.declareProperty("Instrument","AMOR",
StringListValidator(instruments),
"Choose Instrument",direction=Direction.Input)
self.declareProperty(FileProperty(name="Filename",defaultValue="",
action=FileAction.Load, extensions=[".h5",".hdf"]))
self.declareProperty(WorkspaceProperty("OutputWorkspace","",direction=Direction.Output))
def PyExec(self):
inst=self.getProperty('Instrument').value
fname = self.getProperty('Filename').value
diclookup = {
"AMOR":"amor.dic",
"BOA":"boa.dic",
"DMC":"dmc.dic",
"FOCUS":"focus.dic",
"HRPT":"hrpt.dic",
"MARSI":"marsin.dic",
"MARSE":"marse.dic",
"POLDI_legacy":"poldi_legacy.dic",
"POLDI":"poldi.dic",
"RITA-2":"rita.dic",
"SANS":"sans.dic",
"SANS2":"sans.dic",
"TRICS":"trics.dic"
}
lookupInstrumentName = inst
if inst == 'POLDI':
lookupInstrumentName = self._getPoldiLookupName(fname, lookupInstrumentName)
dictsearch = os.path.join(config['instrumentDefinition.directory'],"nexusdictionaries")
dicname = os.path.join(dictsearch, diclookup[lookupInstrumentName])
wname = "__tmp"
ws = mantid.simpleapi.LoadFlexiNexus(fname,dicname,OutputWorkspace=wname)
if inst == "POLDI":
if ws.getNumberHistograms() == 800:
ws.maskDetectors(SpectraList=list(range(0,800))[::2])
config.appendDataSearchDir(config['groupingFiles.directory'])
grp_file = "POLDI_Grouping_800to400.xml"
ws = mantid.simpleapi.GroupDetectors(InputWorkspace=ws,
OutputWorkspace=wname,
MapFile=grp_file, Behaviour="Sum")
# Reverse direction of POLDI data so that low index corresponds to low 2theta.
histogramCount = ws.getNumberHistograms()
oldYData = []
for i in range(histogramCount):
oldYData.append([x for x in ws.readY(i)])
for i in range(histogramCount):
ws.setY(i, np.array(oldYData[histogramCount - 1 - i]))
elif inst == "TRICS":
ws = mantid.simpleapi.LoadFlexiNexus(fname,dicname,OutputWorkspace=wname)
ws = mantid.simpleapi.SINQTranspose3D(ws,OutputWorkspace=wname)
# Attach workspace to the algorithm property
self.setProperty("OutputWorkspace", ws)
# delete temporary reference
mantid.simpleapi.DeleteWorkspace(wname,EnableLogging=False)
def _getPoldiLookupName(self, fname, lookupInstrumentName):
year = self._extractYearFromFileName(fname)
if year < 2015:
return lookupInstrumentName + '_legacy'
# Otherwise, this is the current POLDI format.
return lookupInstrumentName
def _extractYearFromFileName(self, filename):
pureFileName = os.path.basename(filename)
pattern = re.compile(r'\w+(\d{4})n[\w\d\.]+')
matches = re.match(pattern, pureFileName)
return int(matches.group(1))
#---------- register with Mantid
AlgorithmFactory.subscribe(LoadSINQFile)