" SS_1_DCP - Analyze the 13C Spinlock Array and Set the 15N Array"

// Analyze the 13C Spinlock Array

$bestwr1=0 $bestwr2=0 
$remwr1=0.0 $remwr2=0.0
$ampwr1=0.0 $ampwr2=0.0
$counter=8

fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
select(8) vsadj(100) $trial=100  
if ($counter<(arraydim-1)) then 
repeat
   $counter=$counter+1
   if ($counter<=arraydim) then 
      select($counter)
      peak:$ht
      if ($ht<$trial) then $trial=$ht $bestwr1=$counter endif
   endif
until (($ht>1.5*$trial)or($counter>=arraydim))
$ampwr1=aXlock[$bestwr1]
if (($bestwr1>1)and($bestwr1<arraydim-1)) then 
   SS_AutoCal_minmaxfit($bestwr1,'min'):$bestwr1,$remwr1
   $ampwr1=aXlock[$bestwr1] + $remwr1*(aXlock[$bestwr1+1]-aXlock[$bestwr1])
endif endif 

if ($counter<(arraydim-1)) then 
repeat
   $counter=$counter+1
   if ($counter<=arraydim) then 
      select($counter)
      peak:$ht
      if ($ht>$trial) then $trial=$ht $bestwr1=$counter endif
   endif
until (($ht<0.75*$trial)or($counter>=arraydim))
endif

if ($counter<(arraydim-1)) then 
repeat
   $counter=$counter+1
   if ($counter<=arraydim) then 
      select($counter)
      peak:$ht
      if ($ht<$trial) then $trial=$ht $bestwr2=$counter endif
   endif
until (($ht>1.50*$trial)or($counter>=arraydim))
$ampwr2=aXlock[$bestwr2]
if (($bestwr2>1)and($bestwr2<arraydim-1)) then 
   SS_AutoCal_minmaxfit($bestwr2,'min'):$bestwr2,$remwr2
   $ampwr2=aXlock[$bestwr2] + $remwr2*(aXlock[$bestwr2+1]-aXlock[$bestwr2])
endif endif
   
$best=trunc($bestwr1/2.0) 

// Auto Phase

vp=0 sb='n' ph lp=0 wft select($best) vsadj(100) noislm(1.0)
aph0:$aphok
if ($aphok=1) then
   aph0
   peak:$int if ($int<-10) then rp=rp+180 endif
else 
   av vp=0 sb='y' wft select($best) vsadj(100) noislm(1.0)
endif
if (BPplotmode<>'ph') then av vp=0 sb='y' wft select($best) vsadj(100) noislm(1.0) endif 

// Plot Data 

sc=200 wc=10 ho=-2
pps(150,90,wcmax-150,wc2max-90)
pl('all','dodc') pap 
dssh('all','dodc') BPpage

$srate=trunc(srate + 0.5)
$ampwr1=trunc($ampwr1 + 0.5)
$ampwr2=trunc($ampwr2 + 0.5)
$aX32wr=trunc(($ampwr2 - $ampwr1)/2.0 + 0.5) + $ampwr1
$aX52wr=trunc(3.0*($ampwr2 - $ampwr1)/2.0 + 0.5) + $ampwr1

// Create BPrroveride if it Does not Exist

exists('BPrroveride','parameter','global'):$e
if ($e<0.5) then 
   create('BPrroveride','string','global')
   BPrroveride=''
endif

// Save Data and Update aX32wr and aX52wr
// If BPrroveride Exists and is Not '' Do 
// Not Update aX32wr and aX52wr.

if (($1='C13')and($2='alpha')) then  
   BPsvf(userdir+'/AutoTripRes/C13_CP_t1rholock_Ca')
elseif (($1='C13')and($2='carbonyl')) then
   BPsvf(userdir+'/AutoTripRes/C13_CP_t1rholock_CO')
endif

$aX32wrs='' format($aX32wr,4,0):$aX32wrs
$aX52wrs='' format($aX52wr,4,0):$aX52wrs

exists('BPrroveride','parameter','global'):$e
if ($e>0.5) then
   if (BPrroveride='') then
      if (($1='C13')and($2='alpha')) then  
         BPsetparams('aX32wr_HCa',$aX32wrs,'C13')
         BPsetparams('aX52wr_HCa',$aX52wrs,'C13')  
      elseif (($1='C13')and($2='carbonyl')) then
         BPsetparams('aX32wr_HCO',$aX32wrs,'C13')
         BPsetparams('aX52wr_HCO',$aX52wrs,'C13')
      endif
   endif
else 
   if (($1='C13')and($2='alpha')) then  
      BPsetparams('aX32wr_HCa',$aX32wrs,'C13')
      BPsetparams('aX52wr_HCa',$aX52wrs,'C13')  
   elseif (($1='C13')and($2='carbonyl')) then
      BPsetparams('aX32wr_HCO',$aX32wrs,'C13')
      BPsetparams('aX52wr_HCO',$aX52wrs,'C13')
   endif
endif

// Write to log 

$file='Log_'+$1+'_'+$2
cd(userdir)
exists('AutoTripRes','file'):$e
if ($e=1) then
   cd('AutoTripRes')
   write('file',$file,'13C T1RHO SPINLOCK')
   write('line3','13C T1RHO SPINLOCK')
   exists('BPrroveride','parameter','global'):$e
   if ($e>0.5) then
      if (BPrroveride='') then
         write('file',$file,'aX32wr = %6.0f aX52wr = %6.0f',$aX32wrs,$aX52wr)
         write('line3','aX32wr = %6.0f aX52wr = %6.0f',$aX32wrs,$aX52wr)
      endif
   else
      write('file',$file,'aX32wr = %6.0f aX52wr = %6.0f',$aX32wrs,$aX52wr)
      write('line3','aX32wr = %6.0f aX52wr = %6.0f',$aX32wrs,$aX52wr)
   endif
endif 

// Get Parameters for the 15N Resonance to be Calibrated

if (BPpresent<>'y') then 
   tancpxt1rho
   tn='N15' dn='H1' dn2='C13' ampmode='dppp'
   getsolidsprobedata('tn') getsolidsprobedata('dn') getsolidsprobedata('dn2')
   ad=4.0 rd=16.0 ddrtc=20.0 lp=0 d1=2.0 nt=1 ss=2 bs=4
   sw=50000.0 at=0.01 fn=16.0*np sb=at/2.0 sb='n' sbs='n' lb=50 gain=30 
   ofHX=0.0 shHX='t' 
   setref setoffset('N15',115):tof tof=0.1*trunc(tof/0.1 + 0.5) sp=90p wp=50p setref
   getparam('pwHspinal_HNai','H1'):pwHspinal
   getparam('aHspinal_HNai','H1'):aHspinal
   getparam('phHspinal_HNai','H1'):phHspinal
   getparam('pwH90_hpHNai','H1'):pwH90
   getparam('aH90_hpHNai','H1'):aH90
   getparam('aHhx_HNai','H1'):aHhx
   getparam('bHX_HNai','H1'):bHX
   getparam('dHX_HNai','H1'):dHX
   getparam('pwX90_HNai','N15'):pwX90
   getparam('aX90_HNai','N15'):aX90
   getparam('aXhx_HNai','N15'):aXhx
   getparam('tHX_HNai','H1'):tHX
else
   $srate=srate $temp=temp $n1=n1 $n2=n2 $n3=n3
   tancpxt1rho_Nai   
   ad=4.0 rd=16.0 ddrtc=20.0 lp=0 d1=2.0 nt=1 ss=2 bs=4
   sw=50000.0 at=0.01 fn=16.0*np sb=at/2.0 sb='n' sbs='n' lb=50 gain=30 
   ofHX=0.0 shHX='t' 
   srate=$srate temp=$temp n1=$n1 n2=$n2 n3=$n3
endif
cdc ai

// Establish the Remaining Ramp Parameters

chHX='fr' frHX='dec' toHX='obs' 

// Obtain aH90_hp, aX90 and pwX90 from the Probe File 

   getparam('aX90_HNai','N15'):$aX90_sp
   getparam('pwX90_HNai','N15'):$pwX90_sp
   getparam('aH90_hpHNai','H1'):$aH90_hp

// Set TPPM from SPINAl, use SPINAL 

Hseq='spinal' aHtppm=aHspinal pwHtppm=pwHspinal phHtppm=phHspinal

// Setup the 15N Spinlock Array Based on the Medium-Power $B1 0.0 to +3.0*srate

$B1=1.0e6/(4.0*$pwX90_sp)
$aXlockmax=$aX90_sp*(3.0*srate)/$B1
if ($aXlockmax>$aX90_sp) then $aXlockmax=$aX90_sp endif
$stepsize=trunc(($aXlockmax-120)/40)
array('aXlock',41,120,$stepsize)
aHlock=$aH90_hp
tlock=3000
if (BPplotmode='ph') then 
   wnt='vp=0 sb=\'n\' wft setref ph SS_AutoCal_aph0(\'array\'):$ok if ($ok<>1) then sb=\'y\' wft av endif SS_AutoCal_maxamp(100) noislm(1.0) dssh'
else 
   wnt='vp=0 sb=\'y\' wft setref av SS_AutoCal_maxamp(100) noislm(1.0) dssh'
endif 
text('N15 T1rho Spinlock Array')
atext('for N15-amide')
atext(n1) atext(n2)
wexp='SS_2_DCP(\''+ $1 +'\',\'' + $2 + '\')'
au
