1
- from mantid .simpleapi import LoadVesuvio , SaveNexus
1
+ from mantid .simpleapi import Load , LoadVesuvio , SaveNexus
2
2
from pathlib import Path
3
3
from EVSVesuvio .scripts import handle_config
4
4
@@ -40,10 +40,7 @@ def completeICFromInputs(IC, scriptName, wsIC):
40
40
IC .InstrParsPath = wsIC .ipfile
41
41
42
42
# Sort out input and output paths
43
- rawPath , emptyPath = inputDirsForSample (wsIC , scriptName )
44
- if not (rawPath .is_file ()) or not (emptyPath .is_file ()):
45
- print (f"\n Workspaces not found.\n Saving Workspaces:\n { rawPath .name } \n { emptyPath .name } " )
46
- saveWSFromLoadVesuvio (wsIC , rawPath , emptyPath )
43
+ rawPath , emptyPath = setInputWSForSample (wsIC , scriptName )
47
44
48
45
IC .userWsRawPath = rawPath
49
46
IC .userWsEmptyPath = emptyPath
@@ -79,25 +76,37 @@ def completeICFromInputs(IC, scriptName, wsIC):
79
76
return
80
77
81
78
82
- def inputDirsForSample (wsIC , sampleName ):
79
+ def setInputWSForSample (wsIC , sampleName ):
83
80
inputWSPath = experimentsPath / sampleName / "input_ws"
84
81
inputWSPath .mkdir (parents = True , exist_ok = True )
85
82
86
- if int (wsIC .spectra .split ("-" )[1 ])< 135 :
87
- runningMode = "backward"
88
- elif int (wsIC .spectra .split ("-" )[0 ])>= 135 :
89
- runningMode = "forward"
90
- else :
91
- print ("Problem in loading workspaces: invalid range of spectra." )
83
+ runningMode = getRunningMode (wsIC )
92
84
93
85
rawWSName = sampleName + "_" + "raw" + "_" + runningMode + ".nxs"
94
86
emptyWSName = sampleName + "_" + "empty" + "_" + runningMode + ".nxs"
95
87
96
88
rawPath = inputWSPath / rawWSName
97
89
emptyPath = inputWSPath / emptyWSName
90
+
91
+ if not wsHistoryMatchesInputs (wsIC .runs , wsIC .mode , wsIC .ipfile , rawPath ):
92
+ saveWSFromLoadVesuvio (wsIC .runs , wsIC .mode , wsIC .ipfile , rawPath )
93
+
94
+ if not wsHistoryMatchesInputs (wsIC .empty_runs , wsIC .mode , wsIC .ipfile , emptyPath ):
95
+ saveWSFromLoadVesuvio (wsIC .empty_runs , wsIC .mode , wsIC .ipfile , emptyPath )
96
+
98
97
return rawPath , emptyPath
99
98
100
99
100
+ def getRunningMode (wsIC ):
101
+ if wsIC .__class__ .__name__ == "LoadVesuvioBackParameters" :
102
+ runningMode = "backward"
103
+ elif wsIC .__class__ .__name__ == "LoadVesuvioFrontParameters" :
104
+ runningMode = "forward"
105
+ else :
106
+ raise ValueError (f"Input class for loading workspace not valid: { wsIC .__class__ .__name__ } " )
107
+ return runningMode
108
+
109
+
101
110
def setOutputDirsForSample (IC , sampleName ):
102
111
outputPath = experimentsPath / sampleName / "output_files"
103
112
outputPath .mkdir (parents = True , exist_ok = True )
@@ -117,31 +126,53 @@ def setOutputDirsForSample(IC, sampleName):
117
126
return
118
127
119
128
120
- def saveWSFromLoadVesuvio ( wsIC , rawPath , emptyPath ):
129
+ def wsHistoryMatchesInputs ( runs , mode , ipfile , localPath ):
121
130
122
- print (f"\n Loading and storing workspace sample runs: { wsIC .runs } \n " )
131
+ if not (localPath .is_file ()):
132
+ return False
123
133
124
- rawVesuvio = LoadVesuvio (
125
- Filename = wsIC .runs ,
126
- SpectrumList = wsIC .spectra ,
127
- Mode = wsIC .mode ,
128
- InstrumentParFile = str (wsIC .ipfile ),
129
- OutputWorkspace = rawPath .name
130
- )
134
+ local_ws = Load (Filename = str (localPath ))
135
+ ws_history = local_ws .getHistory ()
136
+ metadata = ws_history .getAlgorithmHistory (0 )
137
+
138
+ saved_runs = metadata .getPropertyValue ("Filename" )
139
+ if saved_runs != runs :
140
+ print (f"Filename in saved workspace did not match: { saved_runs } and { runs } " )
141
+ return False
142
+
143
+ saved_mode = metadata .getPropertyValue ("Mode" )
144
+ if saved_mode != mode :
145
+ print (f"Mode in saved workspace did not match: { saved_mode } and { mode } " )
146
+ return False
131
147
132
- SaveNexus (rawVesuvio , str (rawPath ))
133
- print ("\n Raw workspace stored locally.\n " )
148
+ saved_ipfile_name = Path (metadata .getPropertyValue ("InstrumentParFile" )).name
149
+ if saved_ipfile_name != ipfile .name :
150
+ print (f"IP files in saved workspace did not match: { saved_ipfile_name } and { ipfile .name } " )
151
+ return False
134
152
135
- emptyVesuvio = LoadVesuvio (
136
- Filename = wsIC .empty_runs ,
137
- SpectrumList = wsIC .spectra ,
138
- Mode = wsIC .mode ,
139
- InstrumentParFile = str (wsIC .ipfile ),
140
- OutputWorkspace = emptyPath .name
153
+ return True
154
+
155
+
156
+ def saveWSFromLoadVesuvio (runs , mode , ipfile , localPath ):
157
+
158
+ if "backward" in localPath .name :
159
+ spectra = '3-134'
160
+ elif "forward" in localPath .name :
161
+ spectra = '135-198'
162
+ else :
163
+ raise ValueError (f"Invalid name to save workspace: { localPath .name } " )
164
+
165
+ vesuvio_ws = LoadVesuvio (
166
+ Filename = runs ,
167
+ SpectrumList = spectra ,
168
+ Mode = mode ,
169
+ InstrumentParFile = str (ipfile ),
170
+ OutputWorkspace = localPath .name ,
171
+ LoadLogFiles = False
141
172
)
142
173
143
- SaveNexus (emptyVesuvio , str (emptyPath ))
144
- print (" \n Empty workspace stored locally. \n " )
174
+ SaveNexus (vesuvio_ws , str (localPath ))
175
+ print (f"Workspace saved locally at: { localPath } " )
145
176
return
146
177
147
178
0 commit comments