" SS_2_DCP - Analyze the 15N Spinlock Array and Set Up DCP"

// Analyze the 15N 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.5*$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.

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

BPsvf(userdir+'/AutoTripRes/N15_CP_t1rholock_ai')
exists('BPrroveride','parameter','global'):$e
if ($e>0.5) then
   if (BPrroveride='') then
      BPsetparams('aX32wr_HNai',$aX32wrs,'N15')
      BPsetparams('aX52wr_HNai',$aX52wrs,'N15')
   endif
else
   BPsetparams('aX32wr_HNai',$aX32wrs,'N15')
   BPsetparams('aX52wr_HNai',$aX52wrs,'N15')
endif

// Write to Log

$file='Log_'+$1+'_'+$2
cd(userdir)
exists('AutoTripRes','file'):$e
if ($e=1) then
   cd('AutoTripRes')
   write('file',$file,'15N T1RHO SPINLOCK')
   write('line3','15N 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

// Save aHyx and Zero the Spinlock Array

$aHyx=aHlock
aXlock=0.0 tlock=0.0 aHlock=0.0

// Set Up DCP with 13C observe and 15N dec2 

if (BPpresent<>'y') then 
   dcptan
   tn='C13' dn='H1' dn2='N15' ampmode='dppp'
   getsolidsprobedata('tn') getsolidsprobedata('dn') getsolidsprobedata('dn2')
   ad=4.0 rd=4.0 ddrtc=8.0 lp=0 d1=2.0 nt=4 ss=2 bs=4
   sw=100000.0 at=0.01 fn=16.0*np sb=at/2.0 sb='n' sbs='n' lb=50 gain=30  
   ofHY=0.0 shHY='t' 
   if ($2='alpha') then
      setref setoffset('C13',60):tof sp=45p wp=30p setref
   elseif ($2='carbonyl') then 
      setref setoffset('C13',175):tof sp=165p wp=20p setref
   endif 

// Add 15N HY Parameters and Obtain Spinal Decoupling from Observe 15N_ai

   getparam('pwHspinal_HNai','H1'):pwHspinal
   getparam('aHspinal_HNai','H1'):aHspinal
   getparam('phHspinal_HNai','H1'):phHspinal
   getparam('pwH90_hp_HNai','H1'):pwH90
   getparam('aH90_hp_HNai','H1'):aH90
   getparam('aHhx_HNai','H1'):aHhy
   getparam('bHX_HNai','H1'):bHY
   getparam('dHX_HNai','H1'):dHY
   getparam('pwX90_HNai','N15'):pwY90
   getparam('aX90_HNai','N15'):aY90
   getparam('aXhx_HNai','N15'):aYhy
   getparam('tHX_HNai','H1'):tHY
else   
   $srate=srate $temp=temp $n1=n1 $n2=n2 $n3=n3  
   if ($2='alpha') then
      dcptan_HNCa 
   else
      dcptan_HNCO 
   endif
   ad=4.0 rd=4.0 ddrtc=8.0 lp=0 d1=3.0 nt=4 ss=2 bs=4
   sw=100000.0 at=0.01 fn=16.0*np sb=at/2.0 sb='n' sbs='n' lb=50 gain=30  
   ofHY=0.0 shHY='t' 
   srate=$srate temp=$temp n1=$n1 n2=$n2 n3=$n3
endif
cdc ai

// Set aHyx

aHyx = $aHyx

// Establish the Remaining Ramp Parameters

chYX='fr' frYX='dec2' toYX='obs' chHY='fr' frHY='dec' toHY='dec2'

// Set TPPM from SPINAL

aHtppm=aHspinal pwHtppm=pwHspinal phHtppm=phHspinal

// Estimate 15N-13C YX Parameters from Spinlock Data

getparam('aX32wr_HNai','N15'):$aY32wr_sp
getparam('aX52wr_HNai','N15'):$aY52wr_sp

if ($2='alpha') then
   getparam('aX32wr_HCa','C13'):$aX32wr_sp
   getparam('aX52wr_HCa','C13'):$aX52wr_sp
elseif ($2='carbonyl') then
   getparam('aX32wr_HCO','C13'):$aX32wr_sp
   getparam('aX52wr_HCO','C13'):$aX52wr_sp
endif

$stepY=trunc(($aY52wr_sp-$aY32wr_sp)*0.04 + 0.5)
$stepX=trunc(($aX52wr_sp-$aX32wr_sp)*0.04 + 0.5)
frYX='dec2' toYX='obs' chYX='fr' shYX='t' 
tYX=3000 bYX=10000 dYX=trunc($aY52wr_sp*0.01 + 0.5)
array('aYyx',11,$aY52wr_sp-5*$stepY,$stepY)
array('aXyx',22,$aX32wr_sp-10*$stepX,$stepX)

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('DCP 15N-13C Match Array')
if (($1='C13')and($2='alpha')) then atext('for C13-alpha') endif
if (($1='C13')and($2='carbonyl')) then atext('for C13-carbonyl') endif
atext(n1) atext(n2)
wexp='SS_3_DCP(\''+ $1 +'\',\'' + $2 + '\')'
au


