" tc macro by Alex Eletski 04/03/2005            "
" estimates rotational correlation time          "
" by integrating arrayed 1D relaxation spectra   "
" with 'intav' macro and analyzing them with     "
" 't1a' and 't2a' macros                         "

" Usage: tc([[T1 exp#, T2 exp#], left, right])   "
" First transform spectra with 'wft' and adjust  "
" phase                                          "

$T1expno=0
$T2expno=0
$curexpno=0
$seqfil=''
$antype=''
$t1=0
$t2=0
$t1error=0
$t2error=0
$tc=0
$tcerror=0
$dfrq2=0
$sp=0
$wp=0
$leftppm=10.5
$rightppm=8.5
$sfrq=0

if ($# > 1) then
   $T1expno=$1
   $T2expno=$2
else
   input('Enter the number of T1 experiment'):$T1expno
   input('Enter the number of T2 experiment'):$T2expno
endif

if (($T1expno > 9999) or ($T1expno < 1)) then
   write('line3', 'tc: Error - Invalid T1 experiment number %d', $T1expno)
   return
endif

if (($T2expno > 9999) or ($T2expno < 1)) then
   write('line3', 'tc: Error - Invalid T2 experiment number %d', $T2expno)
   return
endif

if ($# > 3) then
   if ($3 > $4) then
      $leftppm=$3
      $rightppm=$4
   endif
   if ($4 > $3) then
      $leftppm=$4
      $rightppm=$3
   endif
endif


jexp:$curexpno

jexp($T1expno)

exists('seqfil','parameter'):$e
if $e then
   getvalue('seqfil', 1, 'processed'):$seqfil
endif
if ($seqfil <> 'gNhsqc') then
   write('line3', 'tc: Warning - exp%d is not gNhsqc!', $T1expno)
endif


getvalue('sfrq'):$sfrq
$sp=$rightppm*$sfrq
$wp=($leftppm - $rightppm)*$sfrq

write('line3', 'Sfreq=%f', $sfrq)
write('line3', 'Using 1H range %2.1f - %2.1f ppm; sp=%d, wp=%d', $leftppm, $rightppm, $sp, $wp)

"setvalue('sp', $sp, 1, 'current')"
"setvalue('wp', $wp, 1, 'current')"
"ds"

intav($sp, $sp+$wp)
t1a

exists(curexp+'/analyze.list','file'):$e
if not($e) then
   write('line3', 'tc: Error - analyze.list file not found in exp%d!', $T1expno)
   jexp($curexpno)
   return
endif

lookup('file', curexp+'/analyze.list')
lookup('readline')
lookup('readline')
lookup('skip','read'):$antype
if ($antype <> 'T1') then
   write('line3', 'tc: Warning - analyze.list may not contain T1 data!')
endif
lookup('readline')
lookup('skip','read','read'):$t1,$t1error



jexp($T2expno)

exists('seqfil','parameter'):$e
if $e then
   getvalue('seqfil', 1, 'processed'):$seqfil
endif
if ($seqfil <> 'gNhsqc') then
   write('line3', 'tc: Warning - exp%d is not gNhsqc!', $T2expno)
endif

"setvalue('sp', $sp, 1, 'current')"
"setvalue('wp', $wp, 1, 'current')"
"ds"

intav($sp, $sp+$wp)
t2a

exists(curexp+'/analyze.list','file'):$e
if not($e) then
   write('line3', 'tc: Error - analyze.list file not found in exp%d!', $T2expno)
   jexp($curexpno)
   return
endif

lookup('file', curexp+'/analyze.list')
lookup('readline')
lookup('readline')
lookup('skip','read'):$antype
if ($antype <> 'T2') then
   write('line3', 'tc: Warning - analyze.list may not contain T2 data!')
endif
lookup('readline')
lookup('skip','read','read'):$t2,$t2error

jexp($curexpno)
getvalue('dfrq2'):$dfrq2

" $tc=sqrt(1.5 * $t1 / $t2 - 1)/(2 * 3.14159 * $dfrq2 * 0.001) "
$tc=sqrt(6 * $t1 / $t2 - 7) / 2 / (2 * 3.14159 * $dfrq2 * 0.001)

"absolute error in T1/T2: "
$tcerror=$t1 / $t2 * sqrt(($t1error/$t1)*($t1error/$t1)+($t2error/$t2)*($t2error/$t2))

"relative error of the argument of sqrt(): "
" $tcerror=(1.5*$tcerror)/(1.5 * $t1 / $t2 - 1) "
$tcerror=(6*$tcerror)/(6 * $t1 / $t2 - 7)

"absolute error in tc: "
$tcerror=$tc*0.5*$tcerror

write('line3', 'tc is estimated as %2.1f +/- %2.1f ns', $tc, $tcerror)

return

