" SS_1_CP1 - Analyze the Match Array to Optimize aHhx"

if ($#<3) then

// Analyze the CP Match Curve for the First Upper Proton Sideband
    
   $bestone=1 $counter=1 $best=0
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   repeat
      select($counter)
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter endif
      $counter=$counter+1
   until ($counter>arraydim)
 
// Auto Phase

   vp=0 sb='n' ph lp=0 wft select($bestone) 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($bestone) vsadj(100) noislm(1.0)
   endif
   if (BPplotmode<>'ph') then av vp=0 sb='y' wft select($bestone) 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

// Select the best 1st Upper H1 CP Match 

   $aHhx=aHhx[$bestone]

// Save Data and Update aHhx from a CP Optimization 

   $aHhxs='' format($aHhx,3,0):$aHhxs
   $dHXs='' format(dHX,6,4):$dHXs
   $bHXs='' format(bHX,6,4):$bHXs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP_upper_match_HCa')
      BPsetparams('aHhx_HCa',$aHhxs,'H1')
      BPsetparams('bHX_HCa',$bHXs,'H1')
      BPsetparams('dHX_HCa',$dHXs,'H1')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_CP_upper_match_HCO')
      BPsetparams('aHhx_HCO',$aHhxs,'H1')
      BPsetparams('bHX_HCO',$bHXs,'H1')
      BPsetparams('dHX_HCO',$dHXs,'H1')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_upper_match_HNai')
      BPsetparams('aHhx_HNai',$aHhxs,'H1')
      BPsetparams('bHX_HNai',$bHXs,'H1')
      BPsetparams('dHX_HNai',$dHXs,'H1')
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_upper_match_HNam')
      BPsetparams('aHhx_HNam',$aHhxs,'H1')
      BPsetparams('bHX_HNam',$bHXs,'H1')
      BPsetparams('dHX_HNam',$dHXs,'H1')
   endif 

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'1st UPPER 1H MATCH AMPLITUDE')
      write('line3','1st UPPER 1H MATCH AMPLITUDE')
      write('file',$file,'aHhx = %6.0f dHX = %6.4f bHX = %6.4f tHX = %6.4f',$aHhx,dHX,bHX,tHX)
      write('line3','aHhx = %6.0f dHX = %6.4f bHX = %6.4f tHX = %6.4f',$aHhx,dHX,bHX,tHX)
   endif 

//Set the Optimum aHhx

   aHhx=$aHhx

// Setup an Array of bHX and dHX 

   $dHXstep= $aHhx*0.1/5.0
   $dHXstep=trunc($dHXstep + 0.5)
   $bHXstep=$dHXstep

   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('Ramped CP Array of beta \\and delta')
   if (($1='C13')and($2='alpha')) then atext('for C13-alpha') endif
   if (($1='C13')and($2='carbonyl')) then atext('for C13-carbonyl') endif
   if (($1='C13')and($2='amide')) then atext('for N15-amide') endif
   if (($1='C13')and($2='amine')) then atext('for N15-amine') endif
   atext(n1) atext(n2)
   array('bHX',6,0,$bHXstep) bHX[1]=1
   array('dHX',6,0,$dHXstep)
   wexp='SS_1_CP1(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Analyze the Array of beta and delta 
    
   $bestone=1 $bestone1=1 $bestone2=1
   $counter=1 $counter1=1 $counter2=1 $best=0
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   $size_dHX=size('dHX')
   repeat
      select($counter)
      $counter2=trunc(($counter-1)/$size_dHX) + 1
      $counter1=($counter-1)%$size_dHX + 1
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter $bestone1=$counter1 $bestone2=$counter2 endif
      $counter=$counter+1
   until ($counter>arraydim)

// Auto Phase

   vp=0 sb='n' ph lp=0 wft select($bestone) 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($bestone) vsadj(100) noislm(1.0)
   endif
   if (BPplotmode<>'ph') then av vp=0 sb='y' wft select($bestone) SS_AutoCal_maxamp(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

// Select the Best Values of beta delta 

   $bHX=bHX[$bestone2]
   $dHX=dHX[$bestone1]

// Save Data and Update bHX and dHX 

   $dHXs='' format($dHX,6,4):$dHXs
   $bHXs='' format($bHX,6,4):$bHXs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP_beta_delta_HCCa')
      BPsetparams('bHX_HCa',$bHXs,'H1')
      BPsetparams('dHX_HCa',$dHXs,'H1')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_CP_beta_delta_HCCO')
      BPsetparams('bHX_HCO',$bHXs,'H1')
      BPsetparams('dHX_HCO',$dHXs,'H1')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_beta_delta_HNai')
      BPsetparams('bHX_HNai',$bHXs,'H1')
      BPsetparams('dHX_HNai',$dHXs,'H1')
   elseif (($1='N15')and($2 ='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_beta_delta_HNam')
      BPsetparams('bHX_HNam',$bHXs,'H1')
      BPsetparams('dHX_HNam',$dHXs,'H1')
   endif 

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'OPTIMUM TANGENT BETA and DELTA')
      write('line3','OPTIMUM TANGENT BETA and DELTA')
      write('file',$file,'aHhx = %6.0f dHX = %6.0f bHX = %6.0f tHX = %6.4f',aHhx,$dHX,$bHX,tHX)
      write('line3','aHhx = %6.0f dHX = %6.0f bHX = %6.0f tHX = %6.4f',aHhx,$dHX,$bHX,tHX)
   endif 

// Set Optimum beta and delta

   bHX=$bHX
   dHX=$dHX 

// Set Up a Contact Time Array

   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('Contact Time 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
   if (($1='N15')and($2='amide')) then atext('for N15-amide') endif
   if (($1='N15')and($2='amine')) then atext('for N15-amine') endif
   atext(n1) atext(n2)
   array('tHX',10,500,500)
   wexp='SS_2_CP1(\''+ $1 +'\',\'' + $2 + '\')'
   au 
endif
