; calibdosy { grad_length grad_delay grad_calib grad_shape }
;
; computes theoretical dfactor from experimental parameters
; suppose the acquisition is done in 1H NMR
;
; does the work if called with propoer argument,
; builds a formbox if called with no arguments
;
; see also: DMIN DMAX DFACTOR calib

set gamah = 2.675E8                           ; rad.s-1.T-1
if (!$arg) then
  formbox 'calibrate dosy' \
    'calibdosy $delta $bdelta $g $exp' \
    'Gradient length (in second)' real delta 0.001 \
    'Inter-gradient delay (in second)' real Bdelta 0.1 \
    'Intensity in G/cm of a gradient of 1.0' real g 0.5 \
    'Gradient Shape' enum 'Square,Sine' exp Square \
    *
else
  message 'Gradient length (in second)'
  set delta = $_                                ; s
  message 'Inter-gradient delay (in second)'
  set Bdelta = $_                               ; s
  message 'Intensity in G/cm of a gradient of 1.0'
  set g = $_                                    ; E-4 T . cm-1
  set g = ($g * 1e-4)
  message 'Gradient Shape (Square/Sine/Bipol)'
  set exp = $_
  if ('SQUARE' s= toupper($exp)) then    ; correct for gradient shape
     set g = $g
  elsif ('SINE' s= toupper($exp)) then
     set g = ($g/(2*atan(1.0)))
  else
     error 'Unknown shape'
  endif
  set K = (($gamah * $g * $delta)^2)
  set K = ($K * ($Bdelta + $delta/3))   ; cm 2 sec-1
  set K = ($K * 1.e-8)

  dfactor  (1/$K)

  print ('DFACTOR :'; $dfactor; '    Values are in micron^2 / sec')
endif

