" SS_2_CP - Analyze the High-Power Nutation Curve, Optimize aH90_hp and "
"           Setup the X Nutation using Tancpxflip.                      "

if ($#<3) then

// Analyze the High-Power 1H Nutation Experiment

   $best90=0 $best180=0 $best270=0 $best360=0
   $rem90=0.0 $rem180=0.0 $rem270=0.0 $rem360=0.0
   $pw90=0.0 $pw180=0.0 $pw270=0.0 $pw360=0.0
   $counter=0 $trial=0

   fn=16*np vp=0 sb='n' av wft
   if ($counter<(arraydim-1)) then 
   repeat
      $counter=$counter+1
      if ($counter<=arraydim) then
         select($counter)
         peak:$ht
         if ($ht>$trial) then $trial=$ht $best90=$counter endif
      endif
   until (($ht<0.75*$trial)or($counter>=arraydim))
   $best901=$best90
   $pw90=pwH90[$best90]
   if (($best90>1)and($best90<arraydim-1)) then 
      SS_AutoCal_minmaxfit($best90,'max'):$best90,$rem90
      $pw90=pwH90[$best90] + $rem90*(pwH90[$best90+1]-pwH90[$best90])
   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 $best180=$counter endif
      endif
   until (($ht>1.25*$trial)or($counter>=arraydim))
   $pw180=pwH90[$best180]
   if (($best180>1)and($best180<arraydim-1)) then
      SS_AutoCal_minmaxfit($best180,'min'):$best180,$rem180
      $pw180=pwH90[$best180] + $rem180*(pwH90[$best180+1]-pwH90[$best180])
   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 $best270=$counter endif
      endif
   until (($ht<0.75*$trial)or($counter>arraydim))
   $pw270=pwH90[$best270]
   if (($best270>1)and($best270<arraydim-1)) then 
      SS_AutoCal_minmaxfit($best270,'max'):$best270,$rem270
      $pw270=pwH90[$best270] + $rem270*(pwH90[$best270+1]-pwH90[$best270])
   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 $best360=$counter endif
      endif
   until (($ht>1.25*$trial)or($counter>=arraydim))
   $pw360=pwH90[$best360]
   if (($best360>1)and($best360<arraydim-1)) then
      SS_AutoCal_minmaxfit($best360,'min'):$best360,$rem360  
      $pw360=pwH90[$best360] + $rem360*(pwH90[$best360+1]-pwH90[$best360])
   endif endif

// Auto Phase

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

// Plot Data

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

// Save the Data

   if (($1='C13')and($2='alpha')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_hpHCa')
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_hpHCO')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_hpHNai')
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_hpHNam')
   endif

// Analyze Data - Determine $pwX90calc from the (pw360-pw180)/2.0 if Possible, 
//              Use pw180/2.0 or pw90, depending on the Length of the Nutation

   $R=1 $F=0
   if ($best360>0) then 
      $R=$pw360/(2.0*$pw180)
      $pwH90calc=($R-0.5)*$pw180
      $F=4
   elseif ($best180>0) then 
      $pwH90calc=$pw180/2.0
      $F=2
   else
      $pwH90calc=$pw90
      $F=1
   endif

   if (($F=0)or($R<=0.75)) then 
      banner('High-Power H1 Nutation Failed')
      write('line3','Abort: R = %f\n F = %d\n',$R,$F)
      abort
   endif

// Write to Log 

   $pw90=0.0125*trunc($pw90/0.0125 + 0.5)
   $pw180=0.0125*trunc($pw180/0.0125 + 0.5)
   $pw270=0.0125*trunc($pw270/0.0125 + 0.5)
   $pw360=0.0125*trunc($pw360/0.0125 + 0.5)

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'HIGH-POWER 1H NUTATION') 
      write('line3','HIGH-POWER 1H NUTATION')
      write('file',$file,'pw90=%6.4f pw180=%6.4f pw270=%6.4f pw360=%6.4f',$pw90,$pw180,$pw270,$pw360)
      write('line3','pw90=%6.4f pw180=%6.4f pw270=%6.4f pw360=%6.4f',$pw90,$pw180,$pw270,$pw360)
      write('file',$file,'Ratio = %6.4f',$R)
      write('line3','Ratio = %6.4f',$R)
   endif 

// Update $R and $F and get $pwH90_hp to Set the 180-Degree Pulse For Optimization

   $Rs='' format($R,6,4):$Rs
   $Fs='' format($F,6,4):$Fs
   if (($1='C13')and($2='alpha')) then
      BPsetparams('R_hpHCa',$Rs,'H1')
      BPsetparams('F_hpHCa',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_hpHC',$Rs,'H1')
         BPsetparams('F_hpHC',$Fs,'H1')
         getparam('pwH90_hpHC','H1'):$pwH90_hp
      else 
         getparam('pwH90_hpHCa','H1'):$pwH90_hp
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsetparams('R_hpHCO',$Rs,'H1')
      BPsetparams('F_hpHCO',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_hpHC',$Rs,'H1')
         BPsetparams('F_hpHC',$Fs,'H1')
         getparam('pwH90_hpHC','H1'):$pwH90_hp
      else 
         getparam('pwH90_hpHCO','H1'):$pwH90_hp
      endif
   elseif (($1='N15')and($2='amide')) then
      BPsetparams('R_hpHNai',$Rs,'H1')
      BPsetparams('F_hpHNai',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_hpHN',$Rs,'H1')
         BPsetparams('F_hpHN',$Fs,'H1')
         getparam('pwH90_hpHN','H1'):$pwH90_hp
      else 
         getparam('pwH90_hpHNai','H1'):$pwH90_hp
      endif
   elseif (($1='N15')and($2='amine')) then
      BPsetparams('R_hpHNam',$Rs,'H1')
      BPsetparams('F_hpHNam',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_hpHN',$Rs,'H1')
         BPsetparams('F_hpHN',$Fs,'H1')
         getparam('pwH90_hpHN','H1'):$pwH90_hp
      else 
         getparam('pwH90_hpHNam','H1'):$pwH90_hp
      endif
   endif

   $aH90=aH90*($pwH90calc/$pwH90_hp) "adjust the amplitude"  
   $aH90=trunc($aH90 + 0.5)
   $pwH90=$pwH90_hp/($R-0.5) "set the 180-degree pulse" 
   $pwH90=0.0125*trunc($pwH90/0.0125 + 0.5)
   pwH90=$pwH90

// Setup the High-Power 1H Amplitude Optimization from the 180-Degree Null

   if (BPplotmode='ph') then 
      wnt='vp=50 sb=\'n\' wft setref ph SS_AutoCal_aph0(\'array\'):$ok if ($ok<>1) then sb=\'y\' wft av endif SS_AutoCal_maxamp(50) noislm(0.5) dssh'
   else 
      wnt='vp=0 sb=\'y\' wft setref av SS_AutoCal_maxamp(100) noislm(1.0) dssh'
   endif 
   text('1H Amplitude Optimization\\for 180-Degree Null At')
   if (($1='C13')and($2='alpha')) then atext('High Power for C13-alpha') endif
   if (($1='C13')and($2='carbonyl')) then atext('High Power for C13-carbonyl') endif
   if (($1='N15')and($2='amide')) then atext('High Power for N15-amide') endif
   if (($1='N15')and($2='amine')) then atext('High Power for N15-amine') endif  
   atext(n1) atext(n2) 
   wexp='SS_2_CP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   array('aH90',21,1.2*$aH90,-0.02*$aH90) 
   au

elseif ($3='PART1') then

// Analyze the High-Power 1H Amplitude Optimization from the 180-Degree Null

   $bestone=1 $counter=1
   fn=16*np vp=0 sb='n' av wft
   select(1) vsadj(100) $best=100
   repeat
      select($counter)
      peak:$ht
      if ($ht<$best) then $best=$ht $bestone=$counter endif
      $counter=$counter+1
   until ($counter>arraydim)

// Fit the Minimum with $bestone+/-$lim Points

   $bestone1=$bestone $remainder=0.0  
   $lim=5
   $summ=$bestone-$lim
   while ($summ<1) do
      $lim=$lim-1
      $summ=$summ+1
   endwhile 
   $summ=$bestone+$lim
   while ($summ>arraydim) do
      $lim=$lim-1
      $summ=$summ-1
   endwhile 

   if ($lim>1) then 
      SS_AutoCal_minmaxfit($bestone,'min'):$bestone1,$remainder
   else 
      write('line3','Abort: (aH90_hp) Insufficient Points for a Fit')
      abort
   endif

// Plot Data 

   if (BPplotmode='ph') then 
      vp=50 sb='n' ph wft select($bestone) vsadj(50) noislm(0.5)
   else 
      vp=0 sb='y' av wft select($bestone) vsadj(100) noislm(1.0)
   endif
   sc=200 wc=10 ho=-4
   pl('all','dodc') pap 
   pps(150,90,wcmax-150,wc2max-90)
   dssh('all','dodc') BPpage

// Analyze the Data - Find Best Amplitude and Reset pwH90 and aH90 

   $aH90=(aH90[$bestone1] + (aH90[$bestone1+1] - aH90[$bestone1])*$remainder)
   aH90=$aH90

   $R=1
   if (($1='C13')and($2='alpha')) then          
      getparam('R_hpHCa','H1'):$R
   elseif (($1='C13')and($2='carbonyl')) then
      getparam('R_hpHCO','H1'):$R
   elseif (($1='N15')and($2='amide')) then
      getparam('R_hpHNai','H1'):$R
   elseif (($1='N15')and($2='amine')) then
      getparam('R_hpHNam','H1'):$R
   endif
   $pwH90=($R-0.5)*pwH90

// Save Data and Update aH90_hp and pwH90_hp

   $pwH90=0.0125*trunc($pwH90/0.0125 + 0.5)
   $aH90=trunc($aH90 + 0.5)
   $pwH90s='' format($pwH90,4,2):$pwH90s
   $aH90s='' format($aH90,4,0):$aH90s  
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_hpHCa')
      BPsetparams('pwH90_hpHCa',$pwH90s,'H1')
      BPsetparams('aH90_hpHCa',$aH90s,'H1')
      if (BPpresent='y') then 
         BPsetparams('pwH90_hpHC',$pwH90s,'H1')
         BPsetparams('aH90_hpHC',$aH90s,'H1')
      endif 
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_hpHCO')
      BPsetparams('pwH90_hpHCO',$pwH90s,'H1')
      BPsetparams('aH90_hpHCO',$aH90s,'H1') 
      if (BPpresent='y') then 
         BPsetparams('pwH90_hpHC',$pwH90s,'H1')
         BPsetparams('aH90_hpHC',$aH90s,'H1')
      endif 
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_hpHNai')
      BPsetparams('pwH90_hpHNai',$pwH90s,'H1')
      BPsetparams('aH90_hpHNai',$aH90s,'H1')
      if (BPpresent='y') then 
         BPsetparams('pwH90_hpHN',$pwH90s,'H1')
         BPsetparams('aH90_hpHN',$aH90s,'H1')
      endif 
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_hpHNam')
      BPsetparams('pwH90_hpHNam',$pwH90s,'H1')
      BPsetparams('aH90_hpHNam',$aH90s,'H1')
      if (BPpresent='y') then 
         BPsetparams('pwH90_hpHN',$pwH90s,'H1')
         BPsetparams('aH90_hpHN',$aH90s,'H1')
      endif 
   endif 

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes') 
      write('file',$file,'pwH90 = %6.4f aH90 = %6.0f dpwr = %6.0f dof = %6.1f',$pwH90,$aH90,dpwr,dof)  
      write('line3','pwH90 = %6.4f aH90 = %6.0f dpwr = %6.0f dof = %6.1f',$pwH90,$aH90,dpwr,dof)  
   endif 

// Update SPINAL in the Probe File using the High-Power Amplitude

   $pwHspinal=2.0*$pwH90*17.0/18.0 $aHspinal=$aH90 $phHspinal=10.0
   $pwHspinal=0.2*trunc($pwHspinal/0.2 + 0.5)
   $aHspinal=trunc($aHspinal + 0.5)
   $pwHspinals='' format($pwHspinal,6,4):$pwHspinals
   $aHspinals='' format($aHspinal,4,0):$aHspinals
   $phHspinals='' format($phHspinal,6,1):$phHspinals
   if (($1='C13')and($2='alpha')) then 
      BPsetparams('pwHspinal_HCa',$pwHspinals,'H1')
      BPsetparams('aHspinal_HCa',$aHspinals,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHspinal_HC',$pwHspinals,'H1')
         BPsetparams('aHspinal_HC',$aHspinals,'H1')
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsetparams('pwHspinal_HCO',$pwHspinals,'H1')
      BPsetparams('aHspinal_HCO',$aHspinals,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHspinal_HC',$pwHspinals,'H1')
         BPsetparams('aHspinal_HC',$aHspinals,'H1')
      endif
   elseif (($1='N15')and($2='amide')) then 
      BPsetparams('pwHspinal_HNai',$pwHspinals,'H1')
      BPsetparams('aHspinal_HNai',$aHspinals,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHspinal_HN',$pwHspinals,'H1')
         BPsetparams('aHspinal_HN',$aHspinals,'H1')
      endif
   elseif (($1='N15')and($2='amine')) then 
      BPsetparams('pwHspinal_HNam',$pwHspinals,'H1')
      BPsetparams('aHspinal_HNam',$aHspinals,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHspinal_HN',$pwHspinals,'H1')
         BPsetparams('aHspinal_HN',$aHspinals,'H1')
      endif
   endif 

// Update TPPM in the Probe File using the High-Power Amplitude

   $pwHtppm=2.0*$pwH90*17.0/18.0 $aHtppm=$aH90 $phHtppm=10.0
   $pwHtppm=0.2*trunc($pwHtppm/0.2 + 0.5)
   $aHtppm=trunc($aHtppm + 0.5)
   $pwHtppms='' format($pwHtppm,6,4):$pwHtppms
   $aHtppms='' format($aHtppm,4,0):$aHtppms
   $phHtppms='' format($phHtppm,6,1):$phHtppms
   if (($1='C13')and($2='alpha')) then  
      BPsetparams('pwHtppm_HCa',$pwHtppms,'H1')
      BPsetparams('aHtppm_HCa',$aHtppms,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHtppm_HC',$pwHtppms,'H1')
         BPsetparams('aHtppm_HC',$aHtppms,'H1')
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsetparams('pwHtppm_HCO',$pwHtppms,'H1')
      BPsetparams('aHtppm_HCO',$aHtppms,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHtppm_HC',$pwHtppms,'H1')
         BPsetparams('aHtppm_HC',$aHtppms,'H1')
      endif
   elseif (($1='N15')and($2='amide')) then 
      BPsetparams('pwHtppm_HNai',$pwHtppms,'H1')
      BPsetparams('aHtppm_HNai',$aHtppms,'H1')
      if (BPpresent='y') then  
         BPsetparams('pwHtppm_HN',$pwHtppms,'H1')
         BPsetparams('aHtppm_HN',$aHtppms,'H1')
      endif
   elseif (($1='N15')and($2='amine')) then 
      BPsetparams('pwHtppm_HNam',$pwHtppms,'H1')
      BPsetparams('aHtppm_HNam',$aHtppms,'H1')
      if (BPpresent='y') then 
         BPsetparams('pwHtppm_HN',$pwHtppms,'H1')
         BPsetparams('aHtppm_HN',$aHtppms,'H1')
      endif
   endif

 // Write to Log

   if ($e=1) then
      write('file',$file,'SPINAL AND TPPM DECOUPLING')
      write('line3','SPINAL AND TPPM DECOUPLING')
      write('file',$file,'aH = %6.0f pwH = %6.4f phH = %6.1f',$aHspinal,$pwHspinal,$phHspinal)
      write('line3','aH = %6.0f pwH = %6.4f phH = %6.1f',$aHspinal,$pwHspinal,$phHspinal)
   endif

// Set the New High Power pwH90 for continued CP

   pwHspinal=$pwHspinal
   aHspinal=$aHspinal 
   pwHtppm=pwHspinal
   aHtppm=aHspinal
   pwH90=$pwH90

// Setup the Tancpxflip Experiment for the pwX90 Calibration

   seqfil='tancpxflip'  
   $pwX90step=pwX90/10.0

   if (BPplotmode='ph') then 
      wnt='vp=50 sb=\'n\' wft setref ph SS_AutoCal_aph0(\'array\'):$ok if ($ok<>1) then sb=\'y\' wft av endif SS_AutoCal_maxamp(50) noislm(0.5) dssh'
   else 
      wnt='vp=0 sb=\'y\' wft setref av SS_AutoCal_maxamp(100) noislm(1.0) dssh'
   endif 
   text('C13 Nutation from the First Null\\For C-alpha')
   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('pwX90',51,$pwX90step,$pwX90step)
   wexp='SS_3_CP(\''+ $1 +'\',\'' + $2 + '\')'
   au   
endif
