1 | # |
---|
2 | # 25/10/11 takes slices out of SANS2d event mode file and turn to histogram |
---|
3 | # 11/12/12 SER corrected spectrum range, was adrfit by 4 spectra (oops) |
---|
4 | # |
---|
5 | import time as tim |
---|
6 | import calendar as cal |
---|
7 | #from datetime import timedelta |
---|
8 | from ISISCommandInterface import * |
---|
9 | # example time handling from Rob Dalgleish (thanks !) 1319280436.54 |
---|
10 | #t1="2011-Oct-22 10:47:16.539999961" |
---|
11 | #t3=t1.split('.') |
---|
12 | #print t3 |
---|
13 | #t4=cal.timegm(tim.strptime(t3[0],"%Y-%b-%d %H:%M:%S")) |
---|
14 | #print t4+float('0.'+t3[1]) |
---|
15 | # |
---|
16 | # ================================================ INPUT DATA HERE ================================= |
---|
17 | DataPath("y:/data/cycle_12_4/") |
---|
18 | samplefilename='SANS2d00016380' |
---|
19 | # list of times to slice between, |
---|
20 | slicelist=[0,2,8,20,40,500,1020] |
---|
21 | # extensions .n001, .n002, .n003 etc, give first extension number here |
---|
22 | savext=2 |
---|
23 | # ichan is the index for "time zero" in the per pulse proton log. NOTE presume the proton log is for previous 0.1 sec |
---|
24 | # so if you want 0.1 sec resolution you may need to adjust the slicelist numbers very carefully ! |
---|
25 | ichan=0 |
---|
26 | # =================================================================================================== |
---|
27 | sampleext='.nxs' |
---|
28 | # note LoadEventNexus can also do the additional filtering. It does not by default load the monitors! |
---|
29 | # (with LoadMonitors=True a separate workspace appears called SampleEvent_monitors with their 8 histogram spectra) |
---|
30 | LoadEventNexus(samplefilename+sampleext,'SampleEvents',LoadMonitors=False) |
---|
31 | # We noted that LoadEventNexus does not load all the neccessary matadata, hence use LoadNexus to get the metadata and monitors. |
---|
32 | LoadNexus(samplefilename,'SampleMonHist',SpectrumMin=1,SpectrumMax=8) |
---|
33 | #CropWorkspace('SampleMonHist','SampleMonHist',StartWorkspaceIndex=0,EndWorkspaceIndex=7) |
---|
34 | # |
---|
35 | ws=mtd["SampleEvents"] |
---|
36 | r= ws.getRun() |
---|
37 | time_array = r.getLogData("proton_charge").times |
---|
38 | value_array = r.getLogData("proton_charge").value |
---|
39 | # |
---|
40 | unit_array = r.getLogData("proton_charge").units |
---|
41 | #print dir(r.getLogData("proton_charge")) |
---|
42 | #print dir(time_array) |
---|
43 | # |
---|
44 | tarr=[] |
---|
45 | # converting individual elements of the time_arry with str(time_array[i]) results in format change and loss of the fractions of sec |
---|
46 | # however Rob discovered (by accident) that str( ) of the whole array at once does work, then need to split it up again ... |
---|
47 | tarr2str=str(time_array) |
---|
48 | tarr2str=tarr2str[1:len(tarr2str)-1].split(',') |
---|
49 | print " some debugging info ..." |
---|
50 | print time_array[0],tarr2str[0] |
---|
51 | print time_array[1],tarr2str[1] |
---|
52 | print time_array[2],tarr2str[2] |
---|
53 | print time_array[3],tarr2str[3] |
---|
54 | print time_array[4],tarr2str[4] |
---|
55 | # |
---|
56 | # choose start time index |
---|
57 | ichan=0 |
---|
58 | t1=tarr2str[ichan] |
---|
59 | t2=t1.split('.') |
---|
60 | t3=cal.timegm(tim.strptime(t2[0],"%Y-%b-%d %H:%M:%S")) |
---|
61 | print t1,t2,t3 |
---|
62 | tstart=float(t3)+float('0.'+t2[1]) |
---|
63 | npts=len(tarr2str) |
---|
64 | print "start index=",ichan," ",t1," mintime=",tstart," npts=",npts |
---|
65 | # |
---|
66 | print " more debugging info ..." |
---|
67 | for i in range(npts): |
---|
68 | t1=tarr2str[i] |
---|
69 | t2=t1.split('.') |
---|
70 | # aagh is falling over when there are no fractions of a second, so no '.' and no t2[1] |
---|
71 | t2.append("0") |
---|
72 | t3=cal.timegm(tim.strptime(t2[0],"%Y-%b-%d %H:%M:%S")) |
---|
73 | |
---|
74 | tarr.append(float(t3)+float('0.'+t2[1])-tstart) |
---|
75 | if i<20: |
---|
76 | print t1,t3,tarr[i],value_array[i] |
---|
77 | tmax=tarr[npts-1] |
---|
78 | print "first 20 times =",tarr[0:20] |
---|
79 | print "last 20 times =",tarr[npts-21:npts-1] |
---|
80 | # |
---|
81 | def findCharge(tarr,varr,tmin,tmax): |
---|
82 | imin=len(tarr) |
---|
83 | imax=0 |
---|
84 | for i in range(len(tarr)): |
---|
85 | imin=i |
---|
86 | if tarr[i]>=tmin: |
---|
87 | break |
---|
88 | for i in range(len(tarr)): |
---|
89 | imax=i |
---|
90 | if tarr[i]>=tmax: |
---|
91 | break |
---|
92 | tCharge=0.0 |
---|
93 | for i in range(imin,imax): |
---|
94 | tCharge=tCharge+varr[i] |
---|
95 | return tCharge |
---|
96 | |
---|
97 | totaluamphr=findCharge(tarr,value_array,0.0,tmax) |
---|
98 | print "total proton charge=",totaluamphr |
---|
99 | # |
---|
100 | nslices=len(slicelist)-1 |
---|
101 | for i in range(nslices): |
---|
102 | nzeros=3-len(str(savext)) |
---|
103 | fpad=".n" |
---|
104 | for ii in range(nzeros): |
---|
105 | fpad+="0" |
---|
106 | newfilename=samplefilename+fpad+str(savext) |
---|
107 | savext=savext+1 |
---|
108 | # cut out a range of data collection times |
---|
109 | FilterByTime('SampleEvents','SampleEventsCut',StartTime=slicelist[i],Stoptime=slicelist[i+1]) |
---|
110 | #FilterByTime('SampleEvents','SampleEventsCut',AbsoluteStartTime='2011-10-09T22:27:00',AbsoluteStoptime='2011-10-09T22:43:00') |
---|
111 | # impose time channels, identical to ones used in histogram part of file (blame Kelvin for the odd choices here) |
---|
112 | params='5.5,44.5,50,50,1000,500,1500,750,99750,255,100005' |
---|
113 | Rebin('SampleEventsCut','SampleHist',params,PreserveEvents=False) |
---|
114 | #Rebin('SampleEvents','SampleHist',params,PreserveEvents=False) |
---|
115 | # use call to crop to make a copy of the monitor workspace to put at front of new file |
---|
116 | # will later rescale the new monitors to the correct run time fraction |
---|
117 | # was CropWorkspace('SampleEvents_monitors','SampleHistNew',StartWorkspaceIndex=0,EndWorkspaceIndex=7) |
---|
118 | uamphr=findCharge(tarr,value_array,slicelist[i],slicelist[i+1]) |
---|
119 | print "slice ",i+1," from",slicelist[i]," to ",slicelist[i+1]," uamphr=",uamphr," of total=",totaluamphr |
---|
120 | rescale=uamphr/totaluamphr |
---|
121 | Scale('SampleMonHist','SampleHistNew',rescale) |
---|
122 | # 8 + 192^2 -1 =36871 use only the spectra we need, |
---|
123 | # BEWARE due to Kelvin's wring tables Spectrum 9 is in ID=4 |
---|
124 | #Note SER changed StartWorkspaceIndex to zero and not 4 and changed EndWorkspaceIndex to 36863 and not 36867 to correct mapping! |
---|
125 | CropWorkspace('SampleHist','SampleHistCropped',StartWorkspaceIndex=0,EndWorkspaceIndex=36863) |
---|
126 | # conjoin expands the first workspace by tagging the second to the end of it, time channels I think have to |
---|
127 | # be identical and spectrum number ranges must not overlap. |
---|
128 | ConjoinWorkspaces('SampleHistNew','SampleHistCropped') |
---|
129 | # |
---|
130 | # SampleHist seems to have main detector counts OK, but the monitor spectra which were in hist mode to start with are ZERO |
---|
131 | SaveNexusProcessed('SampleHistNew',newfilename,PreserveEvents=False) |
---|
132 | print "saved file: ",newfilename |
---|
133 | print 'DELETE all the temp workspaces else you may run out of memory!!' |
---|
134 | print 'BEWARE Mantid reduction does not delete _n001 etc workspaces, and you need to rename the output ones before reducing another file!' |
---|