def get_vanadium(run_number, npy=False): """ Pre-process vanadium only the first time it's used """ vanadiums = sorted(int(n.replace('HB2C_', '').replace('.nxs.h5', '')) for n in os.listdir('/HFIR/HB2C/shared/Vanadium') if 'HB2C_' in n and '.nxs.h5' in n) van_run_number = vanadiums[np.searchsorted(vanadiums, run_number, side='right')-1] upstream_van_file = '/HFIR/HB2C/shared/Vanadium/HB2C_{}.nxs.h5'.format(van_run_number) if npy: npy_van_file = '/HFIR/HB2C/shared/autoreduce/vanadium/HB2C_{}.npy'.format(van_run_number) if os.path.exists(npy_van_file): return np.load(npy_van_file) else: with h5py.File(upstream_van_file, 'r') as f: bc = np.zeros((512*480*8)) for b in range(8): bc += np.bincount(f['/entry/bank'+str(b+1)+'_events/event_id'].value, minlength=512*480*8) bc = bc.reshape((480*8, 512)) bc = (bc[::4, ::4] + bc[1::4, ::4] + bc[2::4, ::4] + bc[3::4, ::4] + bc[::4, 1::4] + bc[1::4, 1::4] + bc[2::4, 1::4] + bc[3::4, 1::4] + bc[::4, 2::4] + bc[1::4, 2::4] + bc[2::4, 2::4] + bc[3::4, 2::4] + bc[::4, 3::4] + bc[1::4, 3::4] + bc[2::4, 3::4] + bc[3::4, 3::4]) np.save(npy_van_file, bc) return bc else: from mantid.simpleapi import LoadWAND, LoadNexus, SaveNexus nxs_van_file = '/HFIR/HB2C/shared/autoreduce/vanadium/HB2C_{}.nxs'.format(van_run_number) if os.path.exists(nxs_van_file): ws = LoadNexus(nxs_van_file) else: ws = LoadWAND(upstream_van_file, Grouping='4x4') SaveNexus(ws, nxs_van_file) return ws
def test(self): ws = LoadWAND('HB2C_7000.nxs.h5') self.assertTrue(ws) self.assertEquals(ws.blocksize(), 1) self.assertEquals(ws.getNumberHistograms(), 1966080) self.assertEquals(ws.readY(1031100), 5) self.assertEquals(ws.run().getProtonCharge(), 907880) self.assertAlmostEqual(ws.run().getGoniometer().getEulerAngles()[0], -142.6) self.assertEquals(ws.run().getLogData('Wavelength').value, 1.488) self.assertAlmostEqual(ws.run().getLogData('Ei').value, 36.94619794) # Check masking self.assertTrue(ws.detectorInfo().isMasked(0)) self.assertTrue(ws.detectorInfo().isMasked(1)) self.assertFalse(ws.detectorInfo().isMasked(2)) self.assertTrue(ws.detectorInfo().isMasked(512)) self.assertTrue(ws.detectorInfo().isMasked(480*512*8-256)) self.assertFalse(ws.detectorInfo().isMasked(480*512*8-256-512*32)) # Check x dimension x=ws.getXDimension() self.assertEquals(x.name, 'Wavelength') self.assertEquals(x.getNBins(), 1) self.assertEquals(x.getNBoundaries(), 2) self.assertAlmostEqual(x.getMinimum(), 1.487) self.assertAlmostEqual(x.getMaximum(), 1.489) ws.delete()
def test(self): ws = LoadWAND('HB2C_7000.nxs.h5', Grouping='2x2') self.assertTrue(ws) self.assertEqual(ws.blocksize(), 1) self.assertEqual(ws.getNumberHistograms(), 1966080 // 4) self.assertEqual(ws.readY(257775), 4) self.assertEqual(ws.run().getProtonCharge(), 907880) self.assertAlmostEqual(ws.run().getGoniometer().getEulerAngles()[0], -142.6) self.assertAlmostEqual(ws.run().getLogData('duration').value, 40.05) # Check masking self.assertTrue(ws.detectorInfo().isMasked(0)) self.assertTrue(ws.detectorInfo().isMasked(1)) self.assertFalse(ws.detectorInfo().isMasked(2)) self.assertTrue(ws.detectorInfo().isMasked(512)) self.assertTrue(ws.detectorInfo().isMasked(480 * 512 * 8 - 256)) self.assertFalse(ws.detectorInfo().isMasked(480 * 512 * 8 - 256 - 512 * 6)) ws.delete()
from mantid.simpleapi import LoadWAND, SetUB, ConvertToMD, PlusMD, mtd, DeleteWorkspace, CloneMDWorkspace import numpy as np import re if 'data' in mtd: DeleteWorkspace('data') for run in range(95409, 98459, 1): ws = LoadWAND(IPTS=21442, RunNumbers=run, Grouping='4x4') ub = np.array(re.findall( r'-?\d+\.*\d*', ws.run().getProperty('HB2C:CS:CrystalAlign:UBMatrix').value[0]), dtype=np.float).reshape(3, 3) sgl = np.deg2rad(ws.run().getProperty( 'HB2C:Mot:sgl.RBV').value[0]) # 'HB2C:Mot:sgl.RBV,1,0,0,-1' sgu = np.deg2rad(ws.run().getProperty( 'HB2C:Mot:sgu.RBV').value[0]) # 'HB2C:Mot:sgu.RBV,0,0,1,-1' sgl_a = np.array([[1, 0, 0], [0, np.cos(sgl), np.sin(sgl)], [0, -np.sin(sgl), np.cos(sgl)]]) sgu_a = np.array([[np.cos(sgu), np.sin(sgu), 0], [-np.sin(sgu), np.cos(sgu), 0], [0, 0, 1]]) UB = sgl_a.dot(sgu_a).dot( ub) # Apply the Goniometer tilts to the UB matrix SetUB(ws, UB=UB) md = ConvertToMD(ws, QDimensions='Q3D', dEAnalysisMode='Elastic', Q3DFrames='HKL', QConversionScales='HKL', OtherDimensions='HB2C:SE:SampleTemp', MinValues='-10,-10,-10,0',
normaliseBy='Monitor' # One on (None, Monitor, Time) units = 'Theta' # One of (Theta, ElasticQ, ElasticDSpacing) Binning = '20,140,2500' # Min,Max,Number_of_bins use_autoreduced = True use_autoreduced_van = True ############################################################################### iptsdir = '/HFIR/HB2C/IPTS-{}/'.format(IPTS) xmin, xmax, bins = Binning.split(',') if use_autoreduced: ws = LoadNexus(Filename=iptsdir+'shared/autoreduce/HB2C_{}.nxs'.format(run)) else: ws = LoadWAND(Filename=iptsdir+'nexus/HB2C_{}.nxs.h5'.format(run)) if vanadium is not None: if 'cal' in mtd: cal = mtd['cal'] else: if use_autoreduced_van: cal = LoadNexus(Filename=iptsdir+'shared/autoreduce/HB2C_{}.nxs'.format(vanadium)) else: cal = LoadWAND(Filename=iptsdir+'shared/autoreduce/HB2C_{}.nxs.h5'.format(vanadium)) else: cal = None reduceToPowder(ws, OutputWorkspace=name, cal=cal, target=units, XMin=xmin, XMax=xmax, NumberBins=bins, normaliseBy=normaliseBy)
############################################################################### iptsdir = '/HFIR/HB2C/IPTS-{}/'.format(IPTS) van_iptsdir = '/HFIR/HB2C/IPTS-{}/'.format(vanadium_IPTS) data = name + '_data' norm = name + '_norm' # Get UB from file CreateSingleValuedWorkspace(OutputWorkspace='__ub') LoadIsawUB('__ub', ub_file) ub = mtd['__ub'].sample().getOrientedLattice().getUB().copy() DeleteWorkspace(Workspace='__ub') if 'cal' not in mtd: # Only load vanadium once LoadWAND(Filename=van_iptsdir + 'nexus/HB2C_{}.nxs.h5'.format(vanadium), OutputWorkspace='cal') if data in mtd: DeleteWorkspace(data) if norm in mtd: DeleteWorkspace(norm) for run in range(first_run, last_run + 1, load_every): filename = iptsdir + 'nexus/HB2C_{}.nxs.h5'.format(run) LoadWAND(filename, OutputWorkspace='__ws') CopyInstrumentParameters('__ws', 'cal') mtd['cal'].run().getGoniometer().setR( mtd['__ws'].run().getGoniometer().getR()) convertToHKL('__ws', OutputWorkspace=data,
name_MDE = name + '_MDE' if not append and name_MDE in mtd: DeleteWorkspace(name_MDE) for run in range(first_run, last_run + 1, load_every): if use_autoreduced: filename = iptsdir + 'shared/autoreduce/HB2C_{}_MDE.nxs'.format(run) try: LoadMD(Filename=filename, LoadHistory=False, OutputWorkspace='__md') except ValueError: filename = iptsdir + 'nexus/HB2C_{}.nxs.h5'.format(run) LoadWAND(filename, OutputWorkspace='__ws') ConvertToMD('__ws', QDimensions='Q3D', dEAnalysisMode='Elastic', Q3DFrames='Q_sample', OutputWorkspace='__md', MinValues='-10,-1,-10', MaxValues='10,1,10') accumulateMD('__md', OutputWorkspace=name_MDE) SaveMD( name_MDE, iptsdir + 'shared/' + name + '_MDE_{}_to_{}_every_{}.nxs'.format(first_run, last_run, load_every)) # Convert MD Event workspace to MD Histo workspace
return bc else: from mantid.simpleapi import LoadWAND, LoadNexus, SaveNexus nxs_van_file = '/HFIR/HB2C/shared/autoreduce/vanadium/HB2C_{}.nxs'.format(van_run_number) if os.path.exists(nxs_van_file): ws = LoadNexus(nxs_van_file) else: ws = LoadWAND(upstream_van_file, Grouping='4x4') SaveNexus(ws, nxs_van_file) return ws if powder: from mantid.simpleapi import LoadWAND, WANDPowderReduction, SavePlot1D, SaveFocusedXYE, Scale data = LoadWAND(filename, Grouping='4x4') runNumber = data.getRunNumber() cal = get_vanadium(runNumber) WANDPowderReduction(InputWorkspace=data, CalibrationWorkspace=cal, Target='Theta', NumberBins=1200, OutputWorkspace='reduced') Scale(InputWorkspace='reduced',OutputWorkspace='reduced',Factor=100) SaveFocusedXYE('reduced', Filename=os.path.join(outdir, output_file+'.xye'), SplitFiles=False, IncludeHeader=False) div = SavePlot1D('reduced', OutputType='plotly') request = publish_plot('HB2C', runNumber, files={'file': div}) else: # Single Crystal with h5py.File(filename, 'r') as f:
outdir = sys.argv[2] powder = False with h5py.File(filename, 'r') as f: if '/entry/DASlogs/HB2C:CS:ITEMS:Nature' in f: nature = f['/entry/DASlogs/HB2C:CS:ITEMS:Nature/value'].value[0][0] if nature == 'Powder': powder = True if powder: sys.path.append("/opt/mantidnightly/bin") from mantid.simpleapi import LoadWAND, WANDPowderReduction, SavePlot1D data = LoadWAND(filename) runNumber = data.getRunNumber() cal = LoadWAND(IPTS=7776, RunNumbers=101567) WANDPowderReduction(InputWorkspace=data, CalibrationWorkspace=cal, Target='Theta', NumberBins=1000, OutputWorkspace='reduced') div = SavePlot1D('reduced', OutputType='plotly') request = publish_plot('HB2C', runNumber, files={'file': div}) else: # Single Crystal import matplotlib as mpl mpl.use("agg") import matplotlib.pyplot as plt