Ticket #6114: apply_masks.py

File apply_masks.py, 4.7 KB (added by Peter Peterson, 8 years ago)

Python script to generate bug

Line 
1import numpy as np
2
3run ='9356'
4
5mask_folder = '/SNS/SNAP/IPTS-6996/shared/Masks/'
6
7cal_file = '/SNS/SNAP/IPTS-6996/shared/calibration/SNAP_calibrate_d9352_2012_10_24.cal'
8
9#The naming convention for the masks was "mask_0_5.xml" where "0_5" is the frame that STARTS at 0.5 d_spacing , note the underscore instead of dot
10
11scales = dict(zip(['0.5','0.6','0.7','0.8','0.9','1.0','1.1','1.2','1.3','1.4','1.5','1.6','1.7','1.8','1.9','2.0','2.1'], [1.008757, 1.024455, 1.031911, 1.054462, 1.002775, 1.007764, 1.014643, 1.077643, 1.049419, 1.029146, 1.00,1.00,1.00,1.00,1.00,1.00,1.00]))
12
13initial_range = 0.5
14final_diamond_range = 2.1
15final_range = 4.0
16step  = 0.005
17
18masking_step = 0.1
19
20# I need to make masks "masking = 0"; I already made masks =  "masking = 1"
21masking = 1
22
23
24
25
26#My naming convention was "mask_0_5.xml" where "0_5" is the frame that STARTS at 0.5, note the underscore instead of dot
27#you should not need to edit below here
28
29
30
31bin_params = str(initial_range)+','+str(step)+','+str(final_range)
32
33### run this part to generate summed neighbors to define masks ####################
34
35
36
37while 1:
38        if masking == 1 : break
39        LoadSNSEventNexus(Filename=run ,OutputWorkspace='data_%s' %run)
40        CompressEvents(InputWorkspace='data_%s' %run,OutputWorkspace='data_%s' %run)
41        AlignDetectors(InputWorkspace='data_%s' %run, CalibrationFile= cal_file, OutputWorkspace='data_d_%s' %run)
42        Rebin(InputWorkspace='data_d_%s' %run,OutputWorkspace='data_d_%s' %run,Params=bin_params,PreserveEvents='1') 
43        SumSpectra(Inputworkspace='data_d_%s' %run, OutputWorkspace='raw_run_%s'%run)
44        SumNeighbours(InputWorkspace='data_d_%s' %run,OutputWorkspace='data_d_8_%s' %run,SumX='8',SumY='8')
45        Rebin(InputWorkspace='data_d_8_%s' %run,OutputWorkspace='data_d_8_%s' %run,Params=bin_params,PreserveEvents='0') 
46
47
48
49
50        for d_range in np.arange(initial_range, final_diamond_range+masking_step, masking_step):
51                print d_range, d_range+masking_step
52                binning='%s,%s, %s' %(d_range, step, d_range+masking_step)
53                Rebin(InputWorkspace='data_d_8_%s' %run, Params=binning, OutputWorkspace='data_d_8_%s_%s' %(run, d_range))
54                SumSpectra(Inputworkspace='data_d_8_%s_%s' %(run, d_range), OutputWorkspace='data_d_8_%s_%s_sum' %(run, d_range))
55               
56        break
57
58### run this part to load and apply  the masks ####################
59
60LoadSNSEventNexus(Filename=run ,OutputWorkspace='data_%s' %run)
61CompressEvents(InputWorkspace='data_%s' %run,OutputWorkspace='data_%s' %run)
62AlignDetectors(InputWorkspace='data_%s' %run, CalibrationFile= cal_file, OutputWorkspace='data_d_%s' %run)
63Rebin(InputWorkspace='data_d_%s' %run,OutputWorkspace='data_d_%s' %run,Params=bin_params,PreserveEvents='0') 
64SumSpectra(Inputworkspace='data_d_%s' %run, OutputWorkspace='raw_run_%s'%run)
65
66count = 0
67
68#for d_range in np.arange(initial_range, final_diamond_range+masking_step, masking_step):
69#       LoadMask(InputFile= mask_folder + 'mask_'+ str(d_range).replace('.','_') +'.xml', Instrument='SNAP', OutputWorkspace='mask_%s'%d_range)
70
71for d_range in np.arange(initial_range, final_diamond_range+masking_step, masking_step):
72        print d_range, d_range+masking_step
73        binning='%s,%s, %s' %(d_range, step, d_range+masking_step)
74        Rebin(InputWorkspace='data_d_%s' %run, Params=binning, OutputWorkspace='temp',PreserveEvents='0')
75        #CloneWorkspace(InputWorkspace='temp', OutputWorkspace='pre_masked_%s'%d_range)
76        LoadMask(InputFile= mask_folder + 'mask_'+ str(d_range).replace('.','_') +'.xml', Instrument='SNAP', OutputWorkspace='mask_%s'%d_range)
77        MaskDetectors(Workspace='temp', MaskedWorkSpace='mask_%s'%d_range)
78        #CloneWorkspace(InputWorkspace='temp', OutputWorkspace='masked_%s'%d_range)
79        key='%.1f'%d_range
80        Scale(InputWorkspace='temp',OutputWorkspace='temp',Factor=scales[key],Operation='Multiply')
81        SumNeighbours(InputWorkspace='temp',OutputWorkspace='temp',SumX='8',SumY='8')
82        #CloneWorkspace(InputWorkspace='temp', OutputWorkspace='masked_8_%s'%d_range)
83        Rebin(InputWorkspace='temp', OutputWorkspace='test_d_%s'%d_range, Params=bin_params)
84        if count == 0 : CloneWorkspace(InputWorkspace='test_d_%s'%d_range, OutputWorkspace='CleanOutput')
85        if count != 0 : Plus(LHSWorkspace='CleanOutput',RHSWorkspace='test_d_%s'%d_range,OutputWorkspace='CleanOutput')
86        SumSpectra(Inputworkspace='test_d_%s'%d_range, OutputWorkspace='test_d_%s_sum'%d_range)
87       
88       
89        count = count+1
90
91
92end_bin=str(final_diamond_range+masking_step) + ',' + str(step) + ',' + str(final_range)
93
94
95SumNeighbours(InputWorkspace='data_d_%s' %run,OutputWorkspace='temp',SumX='8',SumY='8')
96Rebin(InputWorkspace='temp', Params=end_bin, OutputWorkspace='temp',PreserveEvents='0')
97Rebin(InputWorkspace='temp', Params=bin_params, OutputWorkspace='temp',PreserveEvents='0')
98Plus(LHSWorkspace='CleanOutput',RHSWorkspace='temp',OutputWorkspace='CleanOutput')
99SumSpectra(Inputworkspace='CleanOutput', OutputWorkspace='Clean_run_%s'%run)