Commit 0ff94a40 authored by Miguel Angel Gonzalez's avatar Miguel Angel Gonzalez
Browse files

Changes affecting only Figaro: handle correctly sample offset, shift in arrays...

Changes affecting only Figaro: handle correctly sample offset, shift in arrays when using nexus to have beam at same pixel position as in ascii, option to activate and deactivate gravity
parent 7194dfea
;MG 2022-04-13 Working version allowing to switch on/off gravity (gravity = 1/0) to check gravity effects
;PG 2022-04-08 Changed FIGARO sample-to-detector distance to sample-to-detector-center distance and updated the horizontal sample offset to be taken into account in d0 and sdet for FIGARO and added coherent DET option
;TS 2021-04-27 Bug fixes
;TS 2021-04-20 Implementation of recent NEXUS file changes
;TS 2021-03-31 Corrected TOF distance for interchopper distance
......@@ -2399,7 +2401,8 @@ pro cosmos_anal_run, result, groupedresult, direct, reflect, instr, theta, runno
cosmos_logmessage, ' Constant q-binning option. Reflect peak position (from gaussian fit) = ' + cosmos_str_make(newpeakref)
if (strcmp(strtrim(c_params.inst,2),'figaro',/FOLD_CASE)) then begin
trueSampleDetDistance = parref.sdetd - parref.offset ;PG on 27/7/15
;trueSampleDetDistance = parref.sdetd - parref.offset ;PG on 27/7/15
trueSampleDetDistance = parref.sdetd ;PG on 08/04/22: This is the correct distance now
if parref.refdown eq 0 then begin ;determine if reflection down
angle_bragg = (parref.san * !pi / 180.) + $
......@@ -2514,7 +2517,8 @@ pro cosmos_anal_run, result, groupedresult, direct, reflect, instr, theta, runno
cosmos_logmessage, ' Figaro: DAN option. Direct peak position (new from gaussian fit) = ' + cosmos_str_make(newpeakdir)
cosmos_logmessage, ' Figaro: DAN option. Reflect peak position (new from gaussian fit) = ' + cosmos_str_make(newpeakref)
trueSampleDetDistance = parref.sdetd - parref.offset
;trueSampleDetDistance = parref.sdetd - parref.offset
trueSampleDetDistance = parref.sdetd ;PG on 08/04/22: This is the correct distance now
if calc_method eq 'coherent' then begin
;Coherent analysis --> detector pixels projected onto a given pixel, so we need
......@@ -2551,7 +2555,8 @@ pro cosmos_anal_run, result, groupedresult, direct, reflect, instr, theta, runno
fit_gauss = gaussfit(fit_range, total(cntrefi[fit_range,*],2), fit_result, nterms=3)
newpeakref = fit_result[1]
trueSampleDetDistance = parref.sdetd - parref.offset
;trueSampleDetDistance = parref.sdetd - parref.offset
trueSampleDetDistance = parref.sdetd ;PG on 08/04/22: This is the correct distance now
;correction based on assuming that theta = parref.wantedtheta when
;reflection is in parref.wantedpixel and that the distance from sample
......@@ -2586,7 +2591,8 @@ pro cosmos_anal_run, result, groupedresult, direct, reflect, instr, theta, runno
cosmos_logmessage, ' Constant q-binning option. Reflect peak position (from gaussian fit) = ' + cosmos_str_make(newpeakref)
if (strcmp(strtrim(c_params.inst,2),'figaro',/FOLD_CASE)) then begin
trueSampleDetDistance = parref.sdetd - parref.offset
;trueSampleDetDistance = parref.sdetd - parref.offset
trueSampleDetDistance = parref.sdetd ;PG on 08/04/22: This is the correct distance now
if parref.refdown eq 0 then begin ;determine if reflection down
angle_bragg = (theta * !pi / 180.) + $
0.5*atan((float(peakref)-c_params.pcen)*c_params.pixelwidth/trueSampleDetDistance)- $ ;PG on 27/7/15:
......@@ -2633,18 +2639,28 @@ pro cosmos_anal_run, result, groupedresult, direct, reflect, instr, theta, runno
;Determine wavelength (in angstroms) from TOF channels --> lambda[t] (using RB)
corrected_sdetd = cosmos_anal_correctdistance(peakref, parref.x_min, parref.pixeldensity, parref.sdetd)
ref_total_tofd = parref.tofd - parref.sdetd + corrected_sdetd
print, 'CHECKING DISTANCES, RB # ', reflect
print, 'L = tofd - sdetd + corrected_sdetd'
print, 'total L (RB) = ', ref_total_tofd
print, 'parref.tofd, sdetd, d0, offset = ', parref.tofd, parref.sdetd, c_params.d0, parref.offset
print, 'corrected_sdetd (after call to cosmos_anal_correctdistance) = ', corrected_sdetd
if normal_run then begin
corrected_sdetd = cosmos_anal_correctdistance(peakdir, parref.x_min, parref.pixeldensity, pardir.sdetd)
dir_total_tofd = pardir.tofd - pardir.sdetd + corrected_sdetd
dir_ref_diff = abs(ref_total_tofd - dir_total_tofd)
cosmos_logmessage, ' Difference in corrected TOF distance between direct and reflect beams is ' + cosmos_str_make(dir_ref_diff)
if (dir_ref_diff / ref_total_tofd) gt 0.01 then cosmos_logmessage, ' Run no. ' + cosmos_str_make(runno) + ': Different TOF distances from direct and reflect runs.', /WARNING
print, 'DB # ', direct
print, 'total L (DB) = ', dir_total_tofd
print, 'pardir.tofd, sdetd, d0, offset = ', pardir.tofd, pardir.sdetd, c_params.d0, pardir.offset
print, 'corrected_sdetd (after call to cosmos_anal_correctdistance) = ', corrected_sdetd
print, ''
endif
lambda = 1e10 * (c_params.planckperkg * ((findgen(tsize) + 0.5) * parref.channelwidth + parref.delay) / ref_total_tofd)
;TS 2019-01-11 Lower and upper bound of wavelength for each time channel.
;lambdaLowerbound = 1e10 * (c_params.planckperkg * ((findgen(tsize)) * parref.channelwidth + parref.delay) / ref_total_tofd)
;lambdaUpperbound = 1e10 * (c_params.planckperkg * ((findgen(tsize)+1) * parref.channelwidth + parref.delay) / ref_total_tofd)
cosmos_logmessage, ' '
cosmos_logmessage, ' Constants used in calculating lambda :'
cosmos_logmessage, ' poff = ' + cosmos_str_make(c_params.poff)
......@@ -2658,21 +2674,38 @@ pro cosmos_anal_run, result, groupedresult, direct, reflect, instr, theta, runno
cosmos_logmessage, ' parref.offset = ' + cosmos_str_make(parref.offset)
;Reevaluation of lambda to take into account gravity effects (only for Figaro - horizontal reflectometer)
openofflam=lambda*0.0 ;PG on 7/8/17 for consistency with FIGARO
openofflam=lambda*0.0 ;PG on 7/8/17 for consistency with FIGARO
;Activate/deactivate gravity
gravity = 1
if (strcmp(strtrim(c_params.inst,2),'figaro',/FOLD_CASE)) then begin
newlambda = lambda
newtheta = fltarr(tsize)
newdelayangle = fltarr(tsize)
newdelay = fltarr(tsize)
xchopper = c_params.d0 ;distance mid-chopper to sample
xslits3 = parref.slit3 + parref.offset
xslits2 = parref.slit2 + parref.offset
cosmos_logmessage, ' Gravity correction with xslits2 = ' + cosmos_str_make(xslits2)
cosmos_logmessage, ' Gravity correction with xslits3 = ' + cosmos_str_make(xslits3)
cosmos_gravnew, angle_bragg, lambda, newtheta, delta, dz, poffoff, openofflam, xchopper, xslits3, xslits2, parref.refdown,c_params.chopsep,c_params.cr
newdelayangle = parref.delayangle - poffoff/2.
newdelay = parref.delay + ((parref.delayangle - newdelayangle)/360.)*parref.period
newlambda = 1e10 * (c_params.planckperkg * ((findgen(tsize)+0.5) * parref.channelwidth + newdelay) / ref_total_tofd)
if gravity then begin
newlambda = lambda
newtheta = fltarr(tsize)
newdelayangle = fltarr(tsize)
newdelay = fltarr(tsize)
xchopper = c_params.d0 ;distance mid-chopper to sample
xslits3 = parref.slit3 + parref.offset
xslits2 = parref.slit2 + parref.offset
cosmos_logmessage, ' Gravity correction with xslits2 = ' + cosmos_str_make(xslits2)
cosmos_logmessage, ' Gravity correction with xslits3 = ' + cosmos_str_make(xslits3)
cosmos_gravnew, angle_bragg, lambda, newtheta, delta, dz, poffoff, openofflam, xchopper, xslits3, xslits2, parref.refdown,c_params.chopsep,c_params.cr
newdelayangle = parref.delayangle - poffoff/2.
newdelay = parref.delay + ((parref.delayangle - newdelayangle)/360.)*parref.period
newlambda = 1e10 * (c_params.planckperkg * ((findgen(tsize)+0.5) * parref.channelwidth + newdelay) / ref_total_tofd)
cosmos_logmessage, 'RUNNING ANALYSIS WITH GRAVITY CORRECTIONS ON!', /WARNING
endif else begin
newlambda = lambda
newtheta = fltarr(tsize) + angle_bragg * !radeg
cosmos_logmessage, 'CARE: RUNNING ANALYSIS WITHOUT GRAVITY CORRECTIONS!', /WARNING
endelse
endif
;Write lambda[t] array to file
......@@ -3595,6 +3628,24 @@ function sample_to_detector_distance, sht1, dh1, dh2, pixel, xdh1, xdh2, xdet, y
end
;PG on 07/04/2022:
;New function for FIGARO to calculate the distance from the sample to the center of the detector as on D17
;(to replace sample_to_detector_distance)
function sample_to_detector_center_distance, dh1, dh2, xdh1, xdh2, xdet, offset
;first calculate theta based on DH1 and DH2 positions
theta = atan((dh2-dh1)/(xdh2-xdh1))
;calculate new detector distance
det = (xdet - offset)/cos(theta)
return, det
end
; reads from raw data files (specified by array of strings), returning
; summed detector counts (int array (x, t))
; parameters :
......@@ -3769,11 +3820,17 @@ pro cosmos_raw_read, files, monitors, counts, params, path, override, reflect=re
cosmos_logmessage, 'Using default value = ' + cosmos_str_make(c_params.poff), /WARNING
endelse
; sample position offset
; PG on 07/04/22: This has to be advanced to compute correctly d0 and sdetd since start
tmp = par1[23]
c_params.offset = tmp*1.0e-3
if (abs(tmp) gt 1.e-6) then cosmos_logmessage, 'Not zero offset for sample position = ' + cosmos_str_make(c_params.offset)
; d0
tmp = par1[55]
if (tmp gt 0.0) then begin
;distance chopper-sample written in mm in FIGARO numor. Converted in m.
c_params.d0 = tmp * 1e-3
c_params.d0 = tmp * 1e-3 + c_params.offset ;PG on 07/04/22: This has to be corrected for the horizontal sample offset now
endif else begin
cosmos_logmessage, 'No valid value for d0 in numor ' + file_name, /WARNING
cosmos_logmessage, 'Using default value = ' + cosmos_str_make(c_params.d0), /WARNING
......@@ -3807,11 +3864,6 @@ pro cosmos_raw_read, files, monitors, counts, params, path, override, reflect=re
cosmos_logmessage, 'Using default value = ' + cosmos_str_make(c_params.sdetd), /WARNING
endelse
; sample position offset
tmp = par1[23]
c_params.offset = tmp*1.0e-3
if (abs(tmp) gt 1.e-6) then cosmos_logmessage, 'Not zero offset for sample position = ' + cosmos_str_make(c_params.offset)
; TOF delay
tmp = par1[96] + par1[93] ;26.4.2013 MG added extra correction term required for Figaro
if (tmp gt 0.0) then begin
......@@ -4174,10 +4226,13 @@ pro cosmos_raw_read, files, monitors, counts, params, path, override, reflect=re
;sample to detector distance. Apply a correction only for numor version >= 9.
if (fix(par1[89]) ge 9) then begin
cosmos_logmessage, "Applying sample to detector distance correction."
temppar.sdetd = sample_to_detector_distance(par2[18] * 1e-3, par2[27] * 1e-3, par2[28] * 1e-3, c_params.pcen, c_params.xdh1, c_params.xdh2, c_params.sdetd, c_params.ydet, c_params.pixelwidth)
;PG on 07/04/22: changed to sample to detector center distance:
;temppar.sdetd = sample_to_detector_distance(par2[18] * 1e-3, par2[27] * 1e-3, par2[28] * 1e-3, c_params.pcen, c_params.xdh1, c_params.xdh2, c_params.sdetd, c_params.ydet, c_params.pixelwidth)
temppar.sdetd = sample_to_detector_center_distance(par2[27] * 1e-3, par2[28] * 1e-3, c_params.xdh1, c_params.xdh2, c_params.sdetd, c_params.offset)
endif else begin
temppar.sdetd = c_params.sdetd ;It should not use the numor parameter to allow for override to work
endelse
;tof distance from mid-chopper to detector (d0=mid-chopper to sample in case of FIGARO!)
temppar.tofd = temppar.sdetd + c_params.d0
;tof distance from chopper to monitor 2 in figaro
......@@ -4773,13 +4828,21 @@ pro cosmos_raw_read_nexus, files, monitors, counts, params, path, override, refl
cosmos_logmessage, 'Using default value = ' + cosmos_str_make(c_params.openoff), /WARNING
endelse
id = h5d_open(file, '/entry0/instrument/Distance/sample_changer_horizontal_offset')
tmp = (h5d_read(id))[0]
id2 = h5a_open_name(id, 'units')
units = h5a_read(id2)
tmp = cosmos_convert_units(tmp, units, 'm')
c_params.offset = tmp
if (abs(tmp) gt 1.e-6) then cosmos_logmessage, 'Not zero offset for sample position = ' + cosmos_str_make(c_params.offset)
id = h5d_open(file, '/entry0/instrument/ChopperSetting/chopperpair_sample_distance')
tmp = (h5d_read(id))[0]
id2 = h5a_open_name(id, 'units')
units = h5a_read(id2)
tmp = cosmos_convert_units(tmp, units, 'm')
if (tmp gt 0.0) then begin
c_params.d0 = tmp
if c_params.data_format gt 13 then c_params.d0 = tmp else c_params.d0 = tmp + c_params.offset
endif else begin
cosmos_logmessage, 'No valid value for d0 in numor ' + file_name, /WARNING
cosmos_logmessage, 'Using default value = ' + cosmos_str_make(c_params.d0), /WARNING
......@@ -4809,14 +4872,6 @@ pro cosmos_raw_read_nexus, files, monitors, counts, params, path, override, refl
cosmos_logmessage, 'Using default value = ' + cosmos_str_make(c_params.sdetd), /WARNING
endelse
id = h5d_open(file, '/entry0/instrument/Distance/sample_changer_horizontal_offset')
tmp = (h5d_read(id))[0]
id2 = h5a_open_name(id, 'units')
units = h5a_read(id2)
tmp = cosmos_convert_units(tmp, units, 'm')
c_params.offset = tmp
if (abs(tmp) gt 1.e-6) then cosmos_logmessage, 'Not zero offset for sample position = ' + cosmos_str_make(c_params.offset)
id = h5d_open(file, '/entry0/instrument/PSD/time_of_flight')
tmp = (h5d_read(id))[2]
tmp *= 1.0e-6 ;convert to s
......@@ -5217,6 +5272,7 @@ pro cosmos_raw_read_nexus, files, monitors, counts, params, path, override, refl
;TS 2021-05-12 The fl1_value from nexus version 3 is now an integer. Cannot do the "+", "-" comparison
temppar.f2 = tmp;if tmp eq '+' then temppar.f2 = 0 else if tmp eq '-' then temppar.f2 = 1 else temppar.f2 = tmp;to be consistent with Ascii; TS 2021-04-20 This line is only needed if the Flipper2 string is read. I left it here as an option.
;cosmos_logmessage, 'Detected F2: ' + cosmos_str_make(temppar.f2), /WARNING
endif else if (strcmp(strtrim(c_params.inst,2),'figaro',/FOLD_CASE)) then begin
c_params.chopwin = c_params.chopwin_fig
......@@ -5289,7 +5345,9 @@ pro cosmos_raw_read_nexus, files, monitors, counts, params, path, override, refl
units = h5a_read(id2)
dh2 = cosmos_convert_units(tmp, units, 'm')
temppar.sdetd = sample_to_detector_distance(sht1, dh1, dh2, c_params.pcen, c_params.xdh1, c_params.xdh2, c_params.sdetd, c_params.ydet, c_params.pixelwidth)
;PG on 07/04/22: The Nexus files have the corrected distance already
;temppar.sdetd = sample_to_detector_distance(sht1, dh1, dh2, c_params.pcen, c_params.xdh1, c_params.xdh2, c_params.sdetd, c_params.ydet, c_params.pixelwidth)
temppar.sdetd = c_params.sdetd
;tof distance from mid-chopper to detector (d0=mid-chopper to sample in case of FIGARO!)
temppar.tofd = temppar.sdetd + c_params.d0
......@@ -5487,6 +5545,9 @@ pro cosmos_raw_read_nexus, files, monitors, counts, params, path, override, refl
params.size_t = c_params.pixels_t
endif
; Circular shift of 2 pixels to return exactly same array than when reading ASCII
if (strcmp(strtrim(c_params.inst,2),'figaro',/FOLD_CASE)) then counts = shift(counts, [-2, 0])
return
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment