Commit a5a9a08d authored by Owen Arnold's avatar Owen Arnold
Browse files

Process fits script v2

Version 2 should be much faster than the original
parent 7a3bbda3
import os.path
import numpy as np
try:
from astropy.io import fits
except ImportError:
import pyfits as fits
from mantid.simpleapi import SaveNexusProcessed, mtd
from mantid.api import AlgorithmManager as am
def createWorkspace(data_x, data_y, data_e, n_spec):
alg = am.create("CreateWorkspace")
alg.setChild(True)
alg.initialize()
alg.setProperty("DataX", data_x)
alg.setProperty("DataY", data_y)
alg.setProperty("DataE", data_e)
alg.setProperty("NSpec", n_spec)
alg.setProperty("OutputWorkspace", "dummy")
alg.execute()
return alg.getProperty("OutputWorkspace").value
this_dir = os.path.dirname(os.path.abspath(__file__))
# Open the file containing the TOF values. I assume that these are ordered in an ascending fashion according to the filenames.
spectra = open(os.path.join(this_dir, 'Scan000_Spectra.txt'),'r')
tofs = []
for line in spectra:
tofs.append(float(line.split('\t')[0].strip()))
spectra.close()
size = len(os.listdir(this_dir))
yslices = []
yslices = []
eslices = []
xValues = []
validslices = 0
for i in range(0, size):
items = len(os.listdir(this_dir))
for i in range(0, items):
try:
file = os.path.join(this_dir, 'Scan000_{:05d}.fits'.format(i))
ws =LoadFITS(file)
inner = ws[0]
yslices.append(inner.extractY())
eslices.append(inner.extractE())
if not os.path.exists(file):
continue
print("{0} of {1}".format(i, items))
f = fits.open(file, mode='readonly')
image_data = f[0].data.copy().astype(float)
as_vertical_bin = np.reshape(image_data, [1, 512*512])# entire image represents 1 TOF bin
yslices.append(as_vertical_bin)
tofcenter = tofs[i]
xValues.append(tofcenter)
DeleteWorkspace(ws)
validslices +=1
f.close()
except ValueError:
# ignore anything that isn't one or our image fit files
pass
yValues = np.hstack(yslices).flatten()
eValues = np.hstack(eslices).flatten()
# Concatinate bin data along x axis
yValues = np.vstack(yslices)
# Transpose for CreateWorkspace. Otherwise our bin data is treated as spectrum data.
yValues = np.transpose(yValues)
eValues = np.sqrt(yValues)
print("Combining {0} images".format(validslices))
combined = CreateWorkspace(DataX=xValues, DataY=yValues, DataE=eValues, NSpec=512*512)
# Optionally save the output
SaveNexus(InputWorkspace=combined, Filename=os.path.join(this_dir, 'combined'))
xValues = np.array(xValues)
combined = createWorkspace(data_x=xValues, data_y=yValues, data_e=eValues, n_spec=512*512)
# Issue with SaveProcessNexus and workspaces without a name
mtd["combined"] = combined
SaveNexusProcessed(Filename=os.path.join(this_dir,'combined.nxs'),InputWorkspace=combined)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment