in5_qstrip.pro 11.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
;--------------------------------------------------------------------------------
;********************************************************************************
;
	FUNCTION in5_qstrip, w_in, Q=Qvalue, dQ=dQ, pos_angles=pos_angles, $
		            neg_angles=neg_angles, all_angles=all_angles, qv, qb, ib, $
                    verbose=verbose
;
;
;+
;
; For IN4, IN5, IN6 and D7
;
;rebins output data from t2e and reb to regular-grid S(Q,w) data using the old
;KHA IN6 rebin algorithm. Selects a single Q-value. Input workspace must be in
;energy transfer versus scattering angle, i.e. only one component or spin phase.
;
;<h2>ARGUMENTS:</h2>
;	Qvalue:	Q-value at centre of extracted strip
;	dQ :	Width of Q-strip
;
;<h2>KEYWORDS</h2> (- only for D7 data)
; /neg_angles	: use only negative angles
; /pos_angles	: use only positive angles
; /all_angles	: use all angles (default)
;		input workspace must be in energy transfer versus scattering angle,
;		i.e. only one component or spin phase.
; (ev, eb, qb and ib are obsolete, kept for backwards compatability)
;
;<h2>DIMENSIONS:</h2>
; w_in(nE,nphi) -> w_out(nE)
;
;<h2>COMMAND SYNTAX:</h2>
; w10 = qstrip(w9,Q=<Qvalue>,dQ=<dQ>[,/neg_angles][,/pos_angles][,/all_angles],[/verbose])
;
; (optional keywords shown in square brackets)
;
; <h2>HISTORY:</h2>
;
; Creation      : KHA,JRS 9/02/06
; Modifications : JOR 2009/08 Reverse array direction for negative angles for D7 only!
;                             Add prefix in5_ (= trusted IN5) and on demand verbosity.
;-
;
;----------------------------------------------------------------------------------
;**********************************************************************************

	common c_lamp_access, inst
	common grid, Qmin, Qmax, Emin, Emax
49
	forward_function string_round, overlap
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
109
110
111
112
113
114
115
116
117
118
119
120
121

        iprint = 0

	take_datp, datp

	ibank=2
	IF(N_ELEMENTS(qv) GT 0) THEN Qvalue = qv
	IF(N_ELEMENTS(qb) GT 0) THEN dQ     = qb
	IF(N_ELEMENTS(ib) GT 0) THEN ibank  = ib

	IF KEYWORD_SET(pos_angles) THEN ibank=1
	IF KEYWORD_SET(neg_angles) THEN ibank=0
	IF KEYWORD_SET(all_angles) THEN ibank=2

	no_small = 0
	IF (datp.y[0] GT 10.) AND (inst EQ 'IN4') THEN BEGIN
		no_small = 1
		PRINT, 'Qstrip: IN4 data without small angle bank'
	ENDIF

; -------------------------------------------------------------------------------
;   Set up starting parameters
; -------------------------------------------------------------------------------
	IF (N_ELEMENTS(Qvalue) NE 1 OR N_ELEMENTS(dQ) NE 1) THEN BEGIN
		PRINT,'QSTRIP: Error - Qvalue, dQ must be specified'
		return, w_in
	ENDIF

	sw=SIZE(w_in)
    
;	IF keyword_set(verbose) THEN PRINT,'SIZE(w_in)=',sw

	IF (sw[0] NE 2) THEN BEGIN
		PRINT,'QSTRIP: Error - input workspace must be 2-D: E vs. phi'
		return,w_in
	ENDIF
	nx = sw[1]
	ny = sw[2]
    
;	IF keyword_set(verbose) THEN PRINT,'nx=',nx,' ny=',ny

	x_in = datp.x	& sx=SIZE(x_in)
	y_in = datp.y	& sy=SIZE(y_in)
	IF (nx NE sx[1]) OR (ny NE sy[1]) THEN BEGIN
		PRINT,'QSTRIP: Error - sx=',strtrim(string(sx),2),' sy=', strtrim(string(sy),2)
		return,w_in
	ENDIF

	e_in = datp.e        
	se   = SIZE(e_in)
	IF (se[0] NE sw[0] OR se[1] NE sw[1] OR se[2] NE sw[2]) THEN e_in=w_in*0.
     
	par = datp.p

	IF keyword_set(verbose) THEN $
        PRINT,'QSTRIP: Instrument = ',inst
	IF (inst EQ 'D7') THEN BEGIN
		lambda=par[4]
		IF (N_ELEMENTS(ibank) EQ 0) THEN ibank = 2
		TOF=FIX(par[8])
		IF (TOF NE 1) THEN BEGIN
			PRINT,'QSTRIP: Error - workspace data is not in TOF'
			return,w_in
		ENDIF
	ENDIF ELSE lambda=par[21]

	IF keyword_set(verbose) THEN $
        PRINT,'QSTRIP: lambda = ',strtrim(string(lambda),2),' A'

; -------------------------------------------------------------------------------------
;	Set constants and prepare arrays for rebinning to regular Q-E grid
; -------------------------------------------------------------------------------------
122
	const1 = 5.22697		    ; E(meV)=const1*V(m/ms)^2 for neutron
123
	const2 = 2.07193571	    ; E(meV)=const2*k(A^-1)^2 for neutron
124
125
	const3 = 3.956076		    ; V(m/ms)=const3/lambda(A) for neutron
	const4 = 81.8066		    ; E(meV)=const4/lambda(A)^2 for neutron
126
127
128
129
130
131
132
133
134

	Ei    = const4 / lambda^2
	ki    = SQRT(Ei / const2)
	y_in  = y_in*!pi/180.	; convert to radians

	nEps        = nx+1	& Eps = FLTARR(nEps)
	Eps[0]      = x_in[0]-(x_in[1]-x_in[0])/2.
	Eps[1:nx-1] = (x_in[0:nx-2]+x_in[1:nx-1])/2.
	Eps[nx]     = x_in[nx-1]+(x_in[nx-1]-x_in[nx-2])/2.
135
136
137
138
	
;	Seems to be a shift of 1/2 dE in the output energies ... ?
	Eps = Eps + 0.5*(Eps[1] - Eps[0])

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
175
176
177
178
179
180
181
182
183
184
185
186

;	IF keyword_set(verbose) THEN PRINT,'x=',x_in

	nEps=nx+1	& Emin=Eps[0]	& Emax=Eps[nEps-1]
	IF keyword_set(verbose) THEN PRINT,'QSTRIP: Emin = ',strtrim(string(Emin),2),$
                                             ', Emax = ',strtrim(string(Emax),2),' meV'

	w_out=FLTARR(nEps)	& w_out[*]=0.	& e_out=w_out-1.
        
;	IF keyword_set(verbose) THEN PRINT,'y_in=',y_in*180./!pi

	IF (inst EQ 'D7') THEN BEGIN
		i=WHERE(y_in GT 0.,n)
		IF (n LE 0 OR n EQ ny) THEN BEGIN
			PRINT,'QSTRIP: Error - For D7 both -ve and +ve angles must be present'
			return,w_in
		ENDIF
		IF (ibank EQ 2) THEN BEGIN
			twice=1
			iphi1=0		& iphi1next=i[0]
			iphi2=i[0]-1	& iphi2next=ny-1
		ENDIF ELSE BEGIN
			twice=0
			IF (ibank EQ 0) THEN BEGIN
				iphi1=0
				iphi2=i[0]-1
			ENDIF ELSE IF (ibank EQ 1) THEN BEGIN
				iphi1=i[0]
				iphi2=ny-1
			ENDIF ELSE BEGIN
				PRINT,'qstrip: Error - ibank =',ibank
				return,w_in
			ENDELSE
		ENDELSE
	ENDIF ELSE BEGIN
		twice=0
		iphi1=0	& iphi2=ny-1
	ENDELSE

start:
	IF inst EQ 'D7' THEN BEGIN
		nphi          = iphi2 - iphi1 + 2
		phi           = FLTARR(nphi)
		phi[0]        = y_buf1[iphi1] - (y_buf1[iphi1 + 1] - y_buf1[iphi1]) / 2.
		phi[1:nphi-2] = (y_buf1[iphi1:iphi2 - 1] + y_buf1[iphi1 + 1:iphi2]) / 2.
		phi[nphi-1]   = y_buf1[iphi2] + (y_buf1[iphi2] - y_buf1[iphi2 - 1])/2.
		w_buf = w_buf1[*,iphi1:iphi2]
		e_buf = e_buf1[*,iphi1:iphi2]
187
		y_buf = y_buf1[iphi1:iphi2]
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
	ENDIF ELSE IF (inst EQ 'IN4') AND (NOT no_small)  THEN BEGIN
		nphi          = ny+2
		phi           = FLTARR(nphi)
		i1=(WHERE(y_in GT 10.*!pi/180.))[0]-1
		phi[0]    = y_in[0] - (y_in[1] - y_in[0]) / 2.
		phi[1:i1] = (y_in[0:i1-1]+y_in[1:i1])/2.
		phi[i1+1] = phi[i1]+(y_in[i1]-y_in[i1-1])
		phi[i1+3:nphi-2] = (y_in[i1+1:ny-2]+y_in[i1+2:ny-1])/2.
		phi[i1+2] = phi[i1+3]-(y_in[i1+2]-y_in[i1+1])
		phi[nphi-1] = phi[nphi-2]+(y_in[ny-1]-y_in[ny-2])
		w_buf = [[w_in[*,0:i1]],[FLTARR(nx)],[w_in[*,i1+1:ny-1]]]
		e_buf = [[e_in[*,0:i1]],[FLTARR(nx)-1],[e_in[*,i1+1:ny-1]]]
		y_buf = (phi[0:nphi-2]+phi[1:nphi-1])/2.
	ENDIF ELSE BEGIN
		nphi          = iphi2 - iphi1 + 2
		phi           = FLTARR(nphi)
		phi[0]        = y_in[iphi1] - (y_in[iphi1 + 1] - y_in[iphi1]) / 2.
		phi[1:nphi-2] = (y_in[iphi1:iphi2 - 1] + y_in[iphi1 + 1:iphi2]) / 2.
		phi[nphi-1]   = y_in[iphi2] + (y_in[iphi2] - y_in[iphi2 - 1])/2.
		w_buf         = w_in[*,iphi1:iphi2]
		e_buf         = e_in[*,iphi1:iphi2]
		y_buf         = y_in[iphi1:iphi2]
	ENDELSE
	COSphi = COS(phi)

213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
; reverse array direction for negative angles
; IN5: negative angles doesn't mean as on D7
        if inst eq 'D7' then begin
            IF phi[0] LT 0. THEN BEGIN 
                w_buf =     REVERSE(w_buf,2)
                e_buf =     REVERSE(e_buf,2)
                y_buf = ABS(REVERSE(y_buf))
                phi   = ABS(REVERSE(phi))
                COSphi=     REVERSE(COSphi)
            ENDIF
        endif

	IF keyword_set(verbose) THEN $
            PRINT, format = '("QSTRIP: phi  = [",F7.3,",",F7.3,"]")',min(phi)*180./!pi,max(phi)*180./!pi


; -------------------------------------------------------------------------------------
;	Rebin to constant Q grid
; -------------------------------------------------------------------------------------
	a=const2		;	E(meV)=a*Q(A**-1)**2   for neutron
	oldymin=0.

236
	Qmin = Qvalue-dQ/2.
237
238
239
240
    Qmax = Qvalue+dQ/2.
	Q00  = [Qmin,Qmin,Qmax,Qmax]
;	IF keyword_set(verbose) THEN PRINT,Qmin,' < Q <',Qmax
	FOR iEps = 0,nEps-2 DO BEGIN
241
		Emin     = Eps[iEps]
242
243
244
245
246
247
248
        Emax     = Eps[iEps+1]
		corrarea = dQ*(Emax-Emin)
		Eps0     = [Emin,Emax,Emax,Emin]
		COSphi0  = ( 2.*Ei - Eps0 - a*Q00^2 )/( 2.*SQRT( Ei*( Ei - Eps0) ) )
		IF (MAX(ABS(COSphi0)) GE 1.) THEN $
            GOTO, outside
		phi0     = ACOS(COSphi0)
249
		phimin   = MIN(phi0)
250
251
252
253
254
255
256
257
258
259
        phimax   = MAX(phi0)
		IF (phimax LT phi[0] OR phimin GT phi[nphi-1]) THEN $
            GOTO, outside
		iphi  = WHERE(phi GT phimin AND phi LT phimax, nlines)
		iphi0 = (iphi[0]-1) > 0
		IF (nlines EQ 0) THEN BEGIN
			phimean = (phimin+phimax)/2.
			ip      = WHERE(phi LT phimean, np)
			iphi0   = ip[np-1]
		ENDIF
260
startrebin:
261
262
263
        Areasum    = 0.
		wsum       = 0.
		e2sum      = 0.
264
		phiminmeas = 7.
265
266
        phimaxmeas = 0.
		FOR iphi = iphi0,(iphi0 + nlines) < (nphi-2) DO BEGIN
267
			COSphi1 = COSphi[iphi]
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
            COSphi2 = COSphi[iphi+1]
			COSphi0 = [COSphi1,COSphi1,COSphi2,COSphi2]
			Q0      = SQRT( (2.*Ei - Eps0 - 2.*SQRT(Ei*(Ei - Eps0))*COSphi0)/a)
			area   = overlap(Q0,Eps0,iprint,oldymin)
			IF (area GT 0.) THEN BEGIN
				w=w_buf[iEps,iphi]	& e=e_buf[iEps,iphi]
				IF (w NE 0. OR e GE 0.) THEN BEGIN
					areasum    = areasum+area
					wsum       = wsum+area*w
					e2sum      = e2sum+(area*e)^2
					phiminmeas = phiminmeas < phi[iphi]
					phimaxmeas = phimaxmeas > phi[iphi+1]
				ENDIF
			ENDIF
		ENDFOR
		IF (areasum NE 0.) THEN BEGIN
			w_out[iEps]=wsum/areasum
			e_out[iEps]=SQRT(e2sum)/areasum
			GOTO, binned
		ENDIF
288
outside:
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		w_out[iEps]  = 0.
		e_out[iEps] = -1.
		GOTO, nextpoint
binned:
		p1=phimin>phiminmeas
		p2=phimax<phimaxmeas
		IF (p2-p1 LT (phimax-phimin)/2.) THEN BEGIN
			w_out[iEps]=0.
			e_out[iEps]=-1.
		ENDIF 
nextpoint:
	ENDFOR

	IF twice THEN BEGIN
		IF (iphi1 EQ 0) THEN BEGIN
304
			w_out1 = w_out
305
306
307
308
309
310
311
            e_out1 = e_out
			iphi1  = iphi1next
			iphi2  = iphi2next
			GOTO, start
		ENDIF ELSE BEGIN
			w_out2    = w_out
            e_out2    = e_out
312
			w_out[*]  = 0.
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
            e_out[*]  = 0.
			not1      = WHERE( e_out1 LE 0.,n1 )
			IF (n1 NE 0) THEN e_out1[not1] = 1.
			not2      = WHERE( e_out2 LE 0.,n2 )
			IF (n2 NE 0) THEN e_out2[not2]=1.
			w_out     = (w_out1/e_out1^2 + w_out2/e_out2^2)/(1./e_out1^2 + 1./e_out2^2)
			e_out     = 1./SQRT(1./e_out1^2 + 1./e_out2^2)
			IF (n1 NE 0) THEN e_out1[not1] = -1.
			IF (n2 NE 0) THEN e_out2[not2] = -1.
			IF (n1 NE 0) THEN BEGIN
				w_out[not1]=w_out2[not1]
				e_out[not1]=e_out2[not1]
			ENDIF
			IF (n2 NE 0) THEN BEGIN
				w_out[not2]=w_out1[not2]
				e_out[not2]=e_out1[not2]
			ENDIF
		ENDELSE
	ENDIF

; -------------------------------------------------------------------------------------
;	Chop off superfluous bits
; -------------------------------------------------------------------------------------
	iOK = WHERE(e_out GE 0.,nOK)
       
	IF (nOK LE 0) THEN BEGIN
		PRINT,'QSTRIP: Error - no data within this Q-strip'
		GOTO, finished
	ENDIF ELSE BEGIN
		w_out = w_out[iOK]
		e_out = e_out[iOK]
		Eps   = Eps[iOK]
	ENDELSE

    if keyword_set(verbose) then $
        print, FORMAT = '("QSTRIP: final energy range: hW = [",F8.2," ; ",F6.2," ] meV")', min(Eps), max(Eps)
; -------------------------------------------------------------------------------------
;	Return parameters and exit
; -------------------------------------------------------------------------------------
	datp.x_tit = 'Energy Transfer (meV)'
	datp.y_tit = datp.z_tit

	mod_datp, datp, "x", Eps
	mod_datp, datp, "e", e_out

	if keyword_set(verbose) then $
        PRINT, FORMAT = '("QSTRIP: Rebinned to constant Q-w: Q=",F4.2," +/- ",F4.2," A-1")', Qvalue, dQ/2.

361
	s      = STRTRIM(STRING(FLOAT(Qvalue)),2)
362
    Qvalue = string_round(s)
363
	s      = STRTRIM(STRING(FLOAT(dQ)),2)
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
    dQ     = string_round(s)
	s      =' -qs(Q='+Qvalue+',dQ='+dQ
	CASE ibank OF
		0: bs = '/neg'
		1: bs = '/pos'
		2: bs = '/all'
	ENDCASE
	IF (inst EQ 'D7') THEN s=s+bs ELSE s=s+')'
	datp.other_tit=datp.other_tit+s

	give_datp, datp

finished:
	RETURN, w_out
	END