" SS_2_CP1 - Analyze the Contact-time Array to Optimize tHX"

if ($#<3) then

// Analyze the Contact-Time Array
    
   $bestone=1 $counter=1 $best=0
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   select(1) SS_AutoCal_maxamp(100) noislm(1.0)
   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 Contact Time

   $tHX=tHX[$bestone]

// Save Data and Update tHX from a Contact-time Optimization 

   $tHXs='' format($tHX,6,4):$tHXs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP_contact_HCa')
      BPsetparams('tHX_HCa',$tHXs,'H1')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_CP_contact_HCO')
      BPsetparams('tHX_HCO',$tHXs,'H1')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_contact_Hai')
      BPsetparams('tHX_HNai',$tHXs,'H1')
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_contact_Ham')
      BPsetparams('tHX_HNam',$tHXs,'H1')
   endif 

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'BEST CONTACT TIME')
      write('line3','BEST CONTACT TIME')
      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 the Optimum tHX
   
   tHX=$tHX

// Set Up Phased Hi-Res Acquisition Conditions

   ph vp=0 at=0.05 sb= at/2.0 sb='n' lb=10 fn=16*np

// Obtain aHspinal and pwHspinal from the Probe File 

   if (($1='C13')and($2='alpha')) then  
      getparam('aHspinal_HCa','H1'):$aHspinal_sp
      getparam('pwHspinal_HCa','H1'):$pwHspinal_sp
   elseif (($1='C13')and($2='carbonyl')) then
      getparam('aHspinal_HCO','H1'):$aHspinal_sp
      getparam('pwHspinal_HCO','H1'):$pwHspinal_sp
   elseif (($1='N15')and($2='amide')) then
      getparam('aHspinal_HNai','H1'):$aHspinal_sp
      getparam('pwHspinal_HNai','H1'):$pwHspinal_sp
   elseif (($1='N15')and($2='amine')) then
      getparam('aHspinal_HNam','H1'):$aHspinal_sp
      getparam('pwHspinal_HNam','H1'):$pwHspinal_sp
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif

//Obtain and Phase a Spectrum

   aHspinal=$aHspinal_sp
   pwHspinal=$pwHspinal_sp
   phHspinal=10.0

   nt=4
   wnt=''
   wexp='SS_2_CP1(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Auto Phase

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

// Set Up Phased Hi-Res Acquisition Conditions

   ph vp=0 sb='n' lb=10 at=0.025 fn=16*np

// Obtain aHspinal and pwHspinal from the Probe File 

   if (($1='C13')and($2='alpha')) then  
      getparam('aHspinal_HCa','H1'):$aHspinal_sp 
      getparam('pwHspinal_HCa','H1'):$pwHspinal_sp
   elseif (($1='C13')and($2='carbonyl')) then 
      getparam('aHspinal_HCO','H1'):$aHspinal_sp 
      getparam('pwHspinal_HCO','H1'):$pwHspinal_sp
   elseif (($1='N15')and($2='amide')) then 
      getparam('aHspinal_HNai','H1'):$aHspinal_sp
      getparam('pwHspinal_HNai','H1'):$pwHspinal_sp
   elseif (($1='N15')and($2='amine')) then 
      getparam('aHspinal_HNam','H1'):$aHspinal_sp
      getparam('pwHspinal_HNam','H1'):$pwHspinal_sp
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif

// Setup an Array of phHspinal and pwHspinal

   aHspinal=$aHspinal_sp
   $pwHspinal_sp=0.2*trunc($pwHspinal_sp/0.2 + 0.5)
   $pwHspinal_min=$pwHspinal_sp - 0.6
   array('phHspinal',6.0,2.0,4.0)
   array('pwHspinal',10,$pwHspinal_min,0.2)

   nt=1   
   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('SPINAL 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)
   wexp='SS_2_CP1(\''+ $1 +'\',\'' + $2 + '\',\'PART2\')'
   au

elseif ($3='PART2') then

// Analyze the Array of pwHspinal and phHspinal 
    
   $bestone=1 $bestone1=1 $bestone2=1
   $counter=1 $counter1=1 $counter2=1
   $size_pwHspinal=size('pwHspinal')

   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   select(1) SS_AutoCal_maxamp(100) noislm(1.0) $best=3
   repeat
      select($counter)
      $counter2=trunc(($counter-1)/$size_pwHspinal) + 1
      $counter1=($counter-1)%$size_pwHspinal + 1
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter $bestone1=$counter1 $bestone2=$counter2 endif
      $counter=$counter+1
   until ($counter>arraydim)

// Auto Phase

   ph vp=0 sb='n' wft select($bestone) lp=0 vsadj(100) noislm(1.0)
   aph0:$aphok
   if ($aphok=1) then
      aph0
      peak:$int if ($int<-10) then rp=rp+180 endif
   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 pwHspinal and phHspinal

   $pwHspinal=pwHspinal[$bestone1]
   $phHspinal=phHspinal[$bestone2]

// Save Data and Update pwHspinal and phHspinal in the Probe File

   $pwHspinals='' format($pwHspinal,6,4):$pwHspinals
   $phHspinals='' format($phHspinal,6,4):$phHspinals
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP_spinal_HCa')
      BPsetparams('pwHspinal_HCa',$pwHspinals,'H1')
      BPsetparams('phHspinal_HCa',$phHspinals,'H1')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_CP_spinal_HCO')
      BPsetparams('pwHspinal_HCO',$pwHspinals,'H1')
      BPsetparams('phHspinal_HCO',$phHspinals,'H1')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_spinal_HNai')
      BPsetparams('pwHspinal_HNai',$pwHspinals,'H1')
      BPsetparams('phHspinal_HNai',$phHspinals,'H1')
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_spinal_HNam')
      BPsetparams('pwHspinal_HNam',$pwHspinals,'H1')
      BPsetparams('phHspinal_HNam',$phHspinals,'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 SPINAL DECOUPLING')
      write('line3','OPTIMUM SPINAL DECOUPLING')
      write('file',$file,'aHspinal = %6.0f pwHspinal = %6.4f phHspinal = %6.2f',aHspinal,$pwHspinal,$phHspinal)
      write('line3','aHspinal = %6.0f pwHspinal = %6.4f phHspinal = %6.2f',aHspinal,$pwHspinal,$phHspinal)
   endif 

// Set Optimum pwHspinal and phHspinal

   pwHspinal=$pwHspinal   
   phHspinal=$phHspinal

// Obtain a Final Spectrum with Optimum Parameters. 

   nt=4 d1=5
   wnt=''
   text('Optimized Spectrum 4 scans')
   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)
   wexp='SS_2_CP1(\''+ $1 +'\',\'' + $2 + '\',\'PART3\')'
   au

elseif ($3='PART3') then

// Auto Phase

   fn=16*np vp=0 sb='n' ph wft vsadj(100) noislm(1.0)
   aph0:$aphok
   if ($aphok=1) then
      aph0
      peak:$int
      if ($int<-10) then rp=rp+180 endif
   endif

// Find the Signal to Noise and Make a Final Plot

   sp=0p wp=250p delta=50p cr=50p dsnmax dsn:$SN
   vp=0 sc=10 wc=200
   pl pscale pap BPpage
   ds

   wexp=''

// Save the Optimized CP Spectrum

   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP4_1D_HCa')
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/C13_CP4_1D_HCO')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP4_1D_HNai')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP4_1D_HNam')
   endif

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'FINAL SIGNAL TO NOISE')
      write('line3','FINAL SIGNAL TO NOISE')
      write('file',$file,'SN = %6.0f',$SN)
      write('line3','SN = %6.0f',$SN)
   endif 

   ptext($file)

// Finish and Save AutoTripRes with samplename and date

   $copy = ''
   format(BPcopy,1,0):$copy
   $date1 = ''
   $date2=''
   $date3=''
   substr(date,1):$date1
   substr(date,2):$date2
   substr(date,3):$date3
   $datestring = 'CP1_' + $date1 + '_' + $date2 + '_' +$date3 + '_' + $copy
   SS_saveAutoTripRes_ALL(BPsample,$1,$2,$datestring)

   write('line3','CALIBRATION DONE')

// Call Next Method

   if (BPchain = 'y') then 
      if ($1 = 'C13') then
         SS_AutoCal_CP('N15','amide')
      else
         SS_AutoCal_DCP('C13',BPregion)
      endif
   endif

endif
