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