in5_vnorm.pro 16.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
FUNCTION in5_vnorm, w_in, w_van0, $
		 chmin=chmin, chmax=chmax, $
         tv=tv, Ns=Ns, $
		 ts=ts,$
         SMOOTH=smooth,$
		 verbose=verbose
;-------------------------------------------------------------------------------
;*******************************************************************************
;
;   For IN4, IN5, IN6, MIBEMOL  data
;
;+
;
;<h2>NAME:</h2>
;	vnorm
;
;<h2>PURPOSES:</h2>
; Normalises data in w_in with vanadium  in (w_van0) elastic peak integral.
;
;<h2>ARGUMENTS:</h2>
; (all optional)
;
; <b>chmin</b>		: lower time channel limit of integration over vanadium elastic peak
; <b>chmax</b>		: upper time channel limit
; <b>/verbose</b>       : more info during execution
;
;<h2>DIMENSIONS:</h2>
; w_in=w_out(nchannels, nspectra)
;
;<h2>COMMAND SYNTAX:</h2>
;
; w3 = vnorm(w1,w2,chmin=&lt;min&gt;,chmax=&lt;max&gt;[,/verbose])
;      - normalises data in w1 to vanadium data in w2 and store the result in w3
;        (optional arguments shown in square brackets).
;
;<h2>EXAMPLES:</h2>
;
; w3 = vnorm(w1,w2,/verbose) ; calculation of the integration limit are automatic.
;
; w3 = vnorm(w1,w2, chmin=490,chmax=520, /SMOOTH) ; only a few case require to put the limits
;                                                   by hand. Smooth, gaussian smothe the vana ELP integral
;  Absolute cross section calculation:
;
; w3 = in5_vnorm(w1,w2,tv=0.5,Ns=7.22305,ts=0.5, /verbose) 
;
;  Absolute cross section:
;  Ns is sample number density (in 10^22 at/cm^-3)
;  tv and ts are total volume [cm^3] seen by the beam that might be 
;  approximated by the effective sample thickness [cm] if the sample are 
;  flat slab and/or the the beam cross section is not known.
;
;
;<h2>VERSION HISTORY:</h2>
;
; Written by : JR. Stewart,KH.Andersen  15/11/02 
;
; Modified by: S. Rols (for mibemol data) 4/12/03
;              J. ollivier (2006-03-21) New version with vanadium temperature correction 
;                                       for the Debye-waller factor. Add verbosity on the 
;                                       standard output. Calculation of the integral limits
;                                       automatic.
;			                            WARNING: D7 case removed for clarity of the code!!!
;               JOR, Wed Nov 17 11:58:08 CET 2010: Absolute cross section calculation included.
;                                                  Version corrected from the error in JRS/KHA routine.
;               JOR, Mon Nov  7 17:30:04 CET 2011: Add the case of 3D data (PSD). Use CMreplicate to get an
;                                                  statistically averaged over the height of the detector.
;               JOR, Mon Apr 23 17:24:13 CEST 2012: Smoothing function before cmreplicate. Use the function
;                                                   FILTER_IMAGE from http://nstx.pppl.gov/
;               JOR, Fri Feb 15 19:07:51 CET 2013:  Debye-Waller corrections for PSD case.                                  
;               JOR, Wed Nov 20 14:47:36 CET 2013:  Add the absolute normnalisation for the PSD case. Changes have 
;                                                   been made in t2e_psd for correctness of the result vnorm+t2epsd.
72
73
;              
;               JOR, Mon Oct  3 10:35:44 CEST 2016  Remove the restriction on Nb channels
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
;
;-
;
;-------------------------------------------------------------------------------
;*******************************************************************************

	COMMON c_lamp_access, inst

; for functions that has to appear later but are unknown from here when saving
; using: save,/routines,'t2e',filename='t2e.sav'
forward_function str_fit


	take_datp, datp
   take_datp, datvan, /third

;  Some info along with the verbosity
   if keyword_set(verbose) then begin
       print,'VNORM: instrument is               : ',strupcase(inst)
        print, format = '("VNORM: incident wavelength         : ",F5.2," Angs")',  datvan.p[21]
   endif
                
;-------------------------------------------------------------------------------
;  Check workspace sizes
;
;  JOR, Mon Nov  7 17:30:04 CET 2011
;  If data is 3D => single crystal data presumably. In that case, date format is
;  [Phi,Z,ToF] (if data in ToF) and [hw,Phi,Z] if data in energy.
;  1st treat the simple case where data in ToF.
;
;-------------------------------------------------------------------------------
   isPSD = 0
   size_win = SIZE(w_in)
;  IF keyword_set(verbose) THEN PRINT,'SIZE(w_in)=',size_win
   nchannels = size_win[1]
109
   IF size_win[0] EQ 1 THEN  nspectra = 1       ; A single 1D spectrum
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

    IF size_win[0] EQ 2  THEN BEGIN             ; 2D [ToF,phi] Debye-Scherrer usual dataset 
        nspectra = size_win[2]
       nchannels = size_win[1]
    ENDIF
    
    IF size_win[0] EQ 3  THEN BEGIN              ; 3D [Phi,z,ToF] Debye-Scherrer usual dataset 
        isPSD = 1
        nchannels= size_win[3]
        nPhi     = size_win[1]
        nZ       = size_win[2]
        nspectra = nPhi*nZ
    ENDIF
        

;-------------------------------------------------------------------------------
;       External instrument cases
;-------------------------------------------------------------------------------
    CASE inst OF
      'QENS_raw':BEGIN
              w_out = vnorm_qens[w_in, w_van0, chmin, chmax]
              GOTO, finished
             END
      'QENS':    BEGIN
              w_out = vnorm_qens[w_in, w_van0, chmin, chmax]
              GOTO, finished0
             END
      'DCSasc':  BEGIN
              par  = datp.p
              e_in = datp.e
              chw  = FLOAT(par[14])
              L2   = FLOAT(par[6])
             END
      ELSE:      BEGIN
              par  = datp.p
              e_in = datp.e
              chw=par[18]
              size_err_in = SIZE(e_in)
             END
    ENDCASE
;-------------------------------------------------------------------------------
;;	IF keyword_set(verbose) THEN PRINT,'SIZE(e_in) = ',size_err_in

	size_err_in = SIZE(e_in)
     
 	IF (size_err_in[0] NE size_win[0]) OR $
 	   (size_err_in[1] NE size_win[1]) OR $
  	   (size_err_in[2] NE size_win[2]) THEN e_in = SQRT(w_in)
        
 	w_van = w_van0	
 	size_wvan = SIZE(w_van)
 	e_van = datvan.e
 	size_err_van = SIZE(e_van)
     
 	IF (size_err_van[0] NE size_wvan[0]) OR $
 	   (size_err_van[1] NE size_wvan[1]) OR $
 	   (size_err_van[2] NE size_wvan[2]) THEN e_van = SQRT(w_van)
        
     IF  (size_wvan[0] NE size_win[0])  THEN BEGIN
         print, 'VNORM: ERROR:  Win and Vanadium data have different dimensions:'
         print, ' Win  : ', size_win
         print, ' Vana : ', size_wvan
 		GOTO, fin
 	ENDIF
     
175
 	IF (size_wvan[2] NE size_win[2]) THEN BEGIN
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
 	    sstr = 'VNORM: ERROR:  Win and Vanadium data are not on the same format:'
         print, sstr
         print, ' Win  : ', size_win
         print, ' Vana : ', size_wvan
 	    GOTO, fin
 	ENDIF
;             
;-------------------------------------------------------------------------------------
;		Perform normalisation:
;		If chmin | chmax not defined, fit to a gaussian to find
;		the integrations limits on the elastic peak.
;		Then integrate as usual by a sum (thus, not a true integration, unless the 
;       parameters - Ns, ts, tv - are given for an absolute calculation). 
;-------------------------------------------------------------------------------------
     IF keyword_set(verbose) THEN BEGIN
         IF isPSD THEN $
             PRINT,'VNORM: nchannels = ',strtrim(nchannels,2),', nPhi = ',strtrim(nPhi,2), ', nZ = ',strtrim(nZ,2) $
         ELSE $   
             PRINT,'VNORM: nchannels = ',strtrim(nchannels,2),', nPhi = ',strtrim(nspectra,2)
     ENDIF
    
    IF ~isPSD THEN BEGIN     
         gauss = FLTARR(4)	
         ;   The average spectra center and width in safe mode
         x = datvan.x
         y = total(w_van,2)
         e = total(e_van,2)
         ;	formerly used: fitgauss, x,y,e, xmin, xmax, gauss, dgauss
         gauss = str_fit(y, Bkgd = 0, Xin = x)
 
 	    if strcmp(datvan.x_tit,'Energy Transfer (meV)',6, /FOLD_CASE) then begin
 		    print,'VNORM: warning: Vanadium is in energy!'
 		    xmin = gauss[2]-3*gauss[3]
 		    xmax = gauss[2]+3*gauss[3]
 		    ind=where(x lt xmin) & chmin=ind[n_elements(ind)-1]
 		    ind=where(x gt xmax) & chmax=ind[n_elements(0)]
 		    if keyword_set(verbose) then $
 			    PRINT,'VNORM: normalised to Vanadium elastic integral, energy ', $
 			    STRTRIM(STRING(xmin),2),' to ',STRTRIM(STRING(xmax),2)
 	    endif else begin
             if ~keyword_set(chmin) then chmin = fix(gauss[2]-3*gauss[3])
             if ~keyword_set(chmax) then chmax = fix(gauss[2]+3*gauss[3])
 		     if keyword_set(verbose) then $
 			    PRINT,'VNORM: normalised to Vanadium elastic integral, channels ', $
 			    STRTRIM(STRING(chmin),2),' to ',STRTRIM(STRING(chmax),2)
 	    endelse
         ; Sum is enough for non-absolute normalisation                    
 	    V  = TOTAL(w_van[chmin-1:chmax-1,*],1)
 	    dV = SQRT(TOTAL(e_van[chmin-1:chmax-1,*]^2,1))
    ENDIF ELSE BEGIN           ; PSD case
        x = datvan.z           ; --------
        ;   The average spectra center and width in safe mode
        y = transpose( total(w_van,1) )
        gauss = str_fit(y, Bkgd = 0, Xin = x)
                   
        if ~keyword_set(chmin) then chmin = fix(mean(gauss[*,2])-3*mean(gauss[*,3]))
        if ~keyword_set(chmax) then chmax = fix(mean(gauss[*,2])+3*mean(gauss[*,3]))
        
        if keyword_set(verbose) then $
 		    PRINT,'VNORM: PSD: 3D normalised to Vanadium elastic integral, channels ', $
            STRTRIM(STRING(chmin),2),' to ',STRTRIM(STRING(chmax),2)

        V  = TOTAL(w_van[*,*,chmin-1:chmax-1],3)
        if keyword_set(SMOOTH) then begin
;            V = smooth(V,SMOOTH,/edge_truncate) filter_image gives better result
            V = filter_image( V, /SMOOTH, /MEDIAN, /ALL )
            if keyword_set(verbose) THEN print, 'VNORM: Smoothing ELP with boxcar of ',strtrim(smooth,2),' channels'
        endif
        dV = SQRT(TOTAL(e_van[*,*,chmin-1:chmax-1]^2,3))
    
     ENDELSE
 
;-------------------------------------------------------------------------------------
;
;  - Correct for the vanadium Debye-Waller factor.
;    The vanadium temperature must be in vana par(11)
;
;  <u^2> = 0.0067*vana_temp/300.0 A^2  )
;
;-------------------------------------------------------------------------------------
;
;  Since recently, the MIBEMOL parameters are disposed the same way than the other 
;   ILL instruments: lambda_0 = p(21), Temp =  p(11), etc.
;
; So, remove the statement:
;  if strcmp(inst,'MIBEMOL',4,/fold_case) then $
;      vana_temp = datvan.p(59) $
;  else $  
;-------------------------------------------------------------------------------------
    vana_temp = datvan.p[11]
    
    IF ~isPSD THEN BEGIN
        if vana_temp gt 0 then begin
            lambda0 = datvan.p[21]
            Qelast  = 4.0*!PI/lambda0*sin(!PI/180.0*datvan.y/2) 
            corr_coef = exp(-0.0067*vana_temp/300.0*Qelast*Qelast)
            V  = V/corr_coef
            dV = dV/corr_coef
            if keyword_set(verbose) then $
            print,'VNORM: T_vana = ',strtrim(vana_temp,2),' K, Debye-Waller corrections performed.'
        endif else begin
            print, 'vnorm warning: vanadium temperature is unknown, no DW corrections performed!'
        endelse

        w_buf = FLTARR(nchannels,nspectra)
        e_buf = FLTARR(nchannels,nspectra)
    
        IF nspectra GT 1 THEN BEGIN
            V  = REFORM(V,1,nspectra)
            dV = REFORM(dV,1,nspectra)
            V  = (FLTARR(nchannels)+1.) # V
            dV = (FLTARR(nchannels)+1.) # dV
            ; --------------------------
            ; Check for the zeroed Vana
            ; --------------------------
            nz = WHERE(V GT 0)
            w_buf[nz] = w_in[nz]/V[nz]
            ;  --------------------------
            ; propagate errors 
            ;  --------------------------
            e_buf[nz] = SQRT((e_in[nz]/V[nz])^2 + (w_in[nz]*dV[nz]/V[nz]^2)^2) 
         ENDIF ELSE BEGIN  ; 1 spectrum only (not 0 of course)
            w_buf = w_in/V
            ; --------------------------
            ;  propagate errors 
            ; --------------------------
            e_buf = SQRT((e_in/V)^2 + (w_in*dV/V^2)^2) 
        ENDELSE
    ENDIF ELSE BEGIN ; isPSD: all calculations on 2D elastic vana
        
       ; ---------------------------------------------------------------------
       ;  Try to temperature DW correction in PSD 
       ; 1) compute the alpha = phi for each px
       ; 2) compute Qel for ech Alpha/px
       ; 3) compute the corr_coef as usual since Qel is def. for each px
       ; ---------------------------------------------------------------------
        if vana_temp gt 0 then begin
;           For the DW corrections calculation
            phi = datp.x
            y   = datp.y   ;  !!! problem for negative angles ??
            phi = phi # (fltarr(1,nZ)+1.0)
            y   = (fltarr(nPhi,1)+1.0) # y
            r = 400.0    ; distance IN5 detector in cm unit (as the height in y  )
            rcos2t  =  r*cos(phi*!pi/180.0)
            rsin2t2 = (r*sin(phi*!pi/180.0))^2
	        zq    = sqrt(rsin2t2 +  y*y) ; height -h/2 to +h/2
	        Alpha = atan(zq,rcos2t)      ; calculate 2theta values for each px 
            lambda0 = datvan.p[21]
            Qelast  = 4.0*!PI/lambda0*sin(Alpha/2) 
            corr_coef = exp(-0.0067*vana_temp/300.0*Qelast*Qelast)
            V  = V/corr_coef
            dV = dV/corr_coef
            if keyword_set(verbose) then $
            print,'VNORM: T_vana = ',strtrim(vana_temp,2),' K, Debye-Waller corrections performed.'
        endif else begin
            print, 'vnorm warning: vanadium temperature is unknown, no DW corrections performed!'
        endelse
        ; ----------------------------------------

        ; ----------------------------------------
        ;  replicate matrix to nchannels in 3rd dimension 
        ; ----------------------------------------
        V =  cmreplicate( V,nchannels )  
        dV=  cmreplicate( dV,nchannels )  
        
        ; --------------------------
        ; Check for the zeroed Vana
        ; --------------------------
        w_buf = FLTARR(nPhi,nZ,nchannels)
        e_buf = w_buf

        nz = WHERE(V GT 0)
        w_buf[nz] = w_in[nz]/V[nz]
        ;  --------------------------
        ; propagate errors 
        ;  --------------------------
        e_buf[nz] = SQRT((e_in[nz]/V[nz])^2 + (w_in[nz]*dV[nz]/V[nz]^2)^2) 
;        if keyword_set(verbose) then $
;            print,'VNORM: WARNING: PSD: Debye-Waller corrections not implemented.'
    ENDELSE
; -------------------------------------------------------------------------
;  Absolute cross section:
;  Ns, Nv are number density (in 10^22 at/cm^-3)
;  tv and ts are total volume [cm^3] seen by the beam that might be 
;  approximated by the effective sample thickness [cm] if the sample are 
;  flat slab and/or the the beam cross section is not known.
;  vanadium:
;  density = 6.11 g/cm^3
;  M       = 50.9415 g/mol
;  Nav     = 6.022142e23 at/mol 
;  Sigma_i = 5.08 barns (Sears tables, 1993) 
;  Sigma_s = 5.10 barns (Sears tables, 1993) 
; -------------------------------------------------------------------------
    Sigma_s = 5.10
    Sigma_i = 5.08 
    
;  Assumes, w_buf correctly treated so far even for single crystal - 3D spectra
    IF( N_ELEMENTS(Ns) GT 0 ) THEN BEGIN
        Ns = Ns*1.E22
    ;    Nv = 7.197E22
        Nv = 6.11/50.9415*6.022142e23
        tv = FLOAT(tv)
        Ns = FLOAT(Ns)
        ts = FLOAT(ts)
        IF keyword_set(verbose) THEN BEGIN
            PRINT, 'VNORM: Absolute Normalisation performed:'
            PRINT, format='("VNORM: Ns = ",E10.4," at/cm^3, ts = ",F5.2," cm^3/cm")',Ns,ts
            PRINT, format='("VNORM: Nv = ",E10.4," at/cm^3, tv = ",F5.2," cm^3/cm")',Nv,tv
        ENDIF
        w_out = ((Nv*tv)/(Ns*ts))*w_buf*Sigma_i/(4*!pi*chw)
        e_out = ((Nv*tv)/(Ns*ts))*e_buf*Sigma_i/(4*!pi*chw)
    ENDIF ELSE BEGIN
        w_out = w_buf
        e_out = e_buf
    ENDELSE
          
; -------------------------------------------------------------------------
;  Treat the error in vanadium corrections 
; -------------------------------------------------------------------------
    novan = WHERE(V LE 0., n)
    IF n GE 1 THEN BEGIN
        V[novan]=1.
        w_out[novan] = 0.
        e_out[novan] = -1.
    ENDIF
    
    if n_elements(datp.e) eq n_elements(e_out) then begin
        datp.e = e_out
    endif else $
        print, 'VNORM: WARNING: Error_out has a different size from Error_in!' 

finished0:
;  numor = '#'+numor
   numor = '#'
   s=' -vn('+numor+',min='+STRTRIM(STRING(chmin),2)+',max='+STRTRIM(STRING(chmax),2)+')'
   
        datp.other_tit = datp.other_tit+s
        give_datp, datp

finished:
	RETURN, w_out
fin:
	RETURN, -1
    
	END