forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoadLamp.py
73 lines (60 loc) · 2.88 KB
/
LoadLamp.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
# 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 FileProperty, WorkspaceProperty, PythonAlgorithm, AlgorithmFactory, FileAction
from mantid.kernel import Direction
from mantid.simpleapi import CreateWorkspace, AddSampleLogMultiple
import numpy
import h5py
class LoadLamp(PythonAlgorithm):
def name(self):
return 'LoadLamp'
def category(self):
return 'DataHandling\\Nexus'
def summary(self):
return 'Loads HDF files exported from LAMP program at the ILL'
def PyInit(self):
self.declareProperty(FileProperty(name="InputFile", defaultValue="", action=FileAction.Load, extensions=["hdf"]))
self.declareProperty(WorkspaceProperty(name="OutputWorkspace", defaultValue="", direction=Direction.Output))
def PyExec(self):
input_file = self.getProperty("InputFile").value
output_ws = self.getPropertyValue("OutputWorkspace")
logs = ''
with h5py.File(input_file, 'r') as hf:
data = numpy.array(hf.get('entry1/data1/DATA'), dtype='float')
if data.ndim > 2:
raise RuntimeError('Data with more than 2 dimensions are not supported.')
errors = numpy.array(hf.get('entry1/data1/errors'), dtype='float')
x = numpy.array(hf.get('entry1/data1/X'), dtype='float')
if "entry1/data1/PARAMETERS" in hf:
logs = str(hf.get('entry1/data1/PARAMETERS')[0].decode('UTF-8'))
y = numpy.array([0])
nspec = 1
if data.ndim == 2:
y = numpy.array(hf.get('entry1/data1/Y'), dtype='float')
nspec = data.shape[0]
if x.ndim == 1:
x = numpy.tile(x, nspec)
CreateWorkspace(DataX=x, DataY=data, DataE=errors, NSpec=nspec, VerticalAxisUnit='Label',
VerticalAxisValues=y, OutputWorkspace=output_ws)
if logs:
log_names = []
log_values = []
for log in logs.split('\n'):
split = log.strip().split('=')
if len(split) == 2:
name = split[0]
value = split[1]
if name and value:
log_names.append(name)
log_values.append(value)
if log_names:
try:
AddSampleLogMultiple(Workspace=output_ws, LogNames=log_names, LogValues=log_values)
except RuntimeError as e:
self.log().warning('Unable to set the sample logs, reason: '+str(e))
self.setProperty('OutputWorkspace', output_ws)
AlgorithmFactory.subscribe(LoadLamp)