reduce_HB2C.py 2.75 KB
Newer Older
Whitfield, Ross's avatar
Whitfield, Ross committed
1
#!/usr/bin/env python2
2
import os
Whitfield, Ross's avatar
Whitfield, Ross committed
3
import sys
4
import h5py
Whitfield, Ross's avatar
Whitfield, Ross committed
5

6
filename = sys.argv[1]
7
output_file = os.path.split(filename)[-1].replace('.nxs.h5', '')
8
outdir = sys.argv[2]
9

10
powder = False
11

12
13
14
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]
15
        if nature == 'Powder':
16
            powder = False
17
18

if powder:
19

20
    sys.path.append("/opt/mantidnightly/bin")
21
22
23
24
    from mantid.simpleapi import LoadWAND, WANDPowderReduction, SavePlot1D

    data = LoadWAND(filename)
    runNumber = data.getRunNumber()
25
    cal = LoadWAND(IPTS=7776, RunNumbers=101567)
26
27
28
29
30
31
32
33
34
    WANDPowderReduction(InputWorkspace=data,
                        CalibrationWorkspace=cal,
                        Target='Theta',
                        NumberBins=1000,
                        OutputWorkspace='reduced')
    div = SavePlot1D('reduced', OutputType='plotly')
    from postprocessing.publish_plot import publish_plot
    request = publish_plot('HB2C', runNumber, files={'file': div})

35
else:  # Single Crystal
36

37
    import matplotlib as mpl
38
    mpl.use("agg")
39
    import matplotlib.pyplot as plt
40
41
    import numpy as np
    with h5py.File(filename, 'r') as f:
42
43
44
45
        offset = f['/entry/DASlogs/HB2C:Mot:s2.RBV/average_value'].value[0]
        title = f['/entry/title'].value[0]
        mon = f['/entry/monitor1/total_counts'].value[0]
        duration = f['/entry/duration'].value[0]
46
        bc = np.zeros((512*480*8))
47
        for b in range(8):
48
49
            bc += np.bincount(f['/entry/bank'+str(b+1)+'_events/event_id'].value,
                              minlength=512*480*8)
50
        bc = bc.reshape((480*8, 512))
51
52
53
54
        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])
55

56
57
    vanadium = np.load('/HFIR/HB2C/shared/autoreduce/vanadium_101567.npy')
    vanadium_mon = 163519902
58
    bc = bc / vanadium * vanadium_mon / mon
59
60
61
62
    f, (ax1, ax2) = plt.subplots(2, figsize=(8, 4))
    ax1.set_title(u'{}, {}, s2={:.2f}, duration={:.1f}s'.format(title, output_file, offset, duration))
    ax1.plot(np.linspace(offset, 120+offset, 960), bc.sum(1)[::-1])
    ax1.set_xlim(offset, 120+offset)
63
    plt.setp(ax1.get_xticklabels(), visible=False)
64
    im = ax2.imshow(bc.T[::-1, ::-1], cmap='viridis', aspect=1/7.5, extent=(offset, 120+offset, 0, 128))
65
    ax2.set_xlabel(u'2theta')
66
67
    ax2.set_xlim(offset, 120+offset)
    ax2.set_ylim(0, 128)
68
    ax2.get_yaxis().set_visible(False)
69
70
    cb = f.colorbar(im)
    ax2.set_aspect('auto')
71
    f.tight_layout()
72
    plt.savefig(outdir+output_file)