" SS_1_CP - Analyze the Medium-Power Nutation Curve, Optimize aH90_mp and "
"           Setup High-Power Nutation using Tancpxflip.                   "

if ($#<3) then

// Analyze the Medium-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

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'REFERENCED LOCK FREQUENCY') 
      write('line3','REFERENCED LOCK FREQUENCY')
      write('file',$file,'lockfreq = %11.7f solvent = %6s',lockfreq,solvent)
      write('line3','lockfreq = %11.7f solvent = %6s',lockfreq,solvent)
   endif 

// Save Data 

   if (($1='C13')and($2='alpha')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_mpHCa')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/H1_nutation_mpHCO')
   elseif (($1='N15')and($2='amide')) then
      BPsvf(userdir+'/AutoTripRes/H1_nutation_mpHNai')
   elseif (($1='N15')and($2='amine')) then
      BPsvf(userdir+'/AutoTripRes/H1_nutation_mpHNam')
   endif

// Analyze Data - Determine $pwH90calc 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('Medium-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,'MEDIUM-POWER 1H NUTATION') 
      write('line3','MEDIUM-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_mp to Set the 180-Degree Pulse For Optimization

   $Rs='' format($R,6,4):$Rs
   $Fs='' format($F,1,0):$Fs
   if (($1='C13')and($2='alpha')) then
      BPsetparams('R_mpHCa',$Rs,'H1')
      BPsetparams('F_mpHCa',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_mpHC',$Rs,'H1')
         BPsetparams('F_mpHC',$Fs,'H1')
         getparam('pwH90_mpHC','H1'):$pwH90_mp
      else 
         getparam('pwH90_mpHCa','H1'):$pwH90_mp
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsetparams('R_mpHCO',$Rs,'H1')
      BPsetparams('F_mpHCO',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_mpHC',$Rs,'H1')
         BPsetparams('F_mpHC',$Fs,'H1')
         getparam('pwH90_mpHC','H1'):$pwH90_mp
      else 
         getparam('pwH90_mpHCO','H1'):$pwH90_mp
      endif     
   elseif (($1='N15')and($2='amide')) then
      BPsetparams('R_mpHNai',$Rs,'H1')
      BPsetparams('F_mpHNai',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_mpHN',$Rs,'H1')
         BPsetparams('F_mpHN',$Fs,'H1')
         getparam('pwH90_mpHN','H1'):$pwH90_mp
      else 
         getparam('pwH90_mpHNai','H1'):$pwH90_mp
      endif
   elseif (($1='N15')and($2='amine')) then
      BPsetparams('R_mpHNam',$Rs,'H1')
      BPsetparams('F_mpHNam',$Fs,'H1')
      if (BPpresent='y') then 
         BPsetparams('R_mpHN',$Rs,'H1')
         BPsetparams('F_mpHN',$Fs,'H1')
         getparam('pwH90_mpHN','H1'):$pwH90_mp
      else 
         getparam('pwH90_mpHNam','H1'):$pwH90_mp
      endif
   endif

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

// Setup the Medium-Power 1H Amplitude-Optimization Experiment 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 a 180-Degree Null at\\Medium Power')
   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('aH90',21,1.4*$aH90,-0.04*$aH90)
   wexp='SS_1_CP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Analyze the Medium-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)

   $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_mp) 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

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

   $R=1
   if (($1='C13')and($2='alpha')) then          
      getparam('R_mpHCa','H1'):$R 
   elseif (($1='C13')and($2='carbonyl')) then
      getparam('R_mpHCO','H1'):$R 
   elseif (($1='N15')and($2='amide')) then
      getparam('R_mpHNai','H1'):$R
   elseif (($1='N15')and($2='amine')) then
      getparam('R_mpHNam','H1'):$R
   endif
   $pwH90=($R-0.5)*pwH90

// Save Data and Update aH90_mp and pwH90_mp

   $pwH90=0.0125*trunc($pwH90/0.0125 + 0.5)
   $aH90=trunc($aH90 + 0.5)
   $pwH90s='' format($pwH90,6,4):$pwH90s
   $aH90s='' format($aH90,4,0):$aH90s
   write('line3','BPpresent=%s',BPpresent)
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_mpHCa')   
      BPsetparams('pwH90_mpHCa',$pwH90s,'H1')
      BPsetparams('aH90_mpHCa',$aH90s,'H1')
      if (BPpresent='y') then
         write('line3','$aH90s = %s',$aH90s)
         BPsetparams('pwH90_mpHC',$pwH90s,'H1')
         BPsetparams('aH90_mpHC',$aH90s,'H1')
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_mpHCO')
      BPsetparams('pwH90_mpHCO',$pwH90s,'H1')
      BPsetparams('aH90_mpHCO',$aH90s,'H1') 
      if (BPpresent='y') then 
         BPsetparams('pwH90_mpHC',$pwH90s,'H1')
         BPsetparams('aH90_mpHC',$aH90s,'H1')
      endif
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_mpHai')
      BPsetparams('pwH90_mpHNai',$pwH90s,'H1')
      BPsetparams('aH90_mpHNai',$aH90s,'H1')
      if (BPpresent='y') then 
         BPsetparams('pwH90_mpHN',$pwH90s,'H1')
         BPsetparams('aH90_mpHN',$aH90s,'H1')
      endif
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_mpHam')
      BPsetparams('pwH90_mpHNam',$pwH90s,'H1')
      BPsetparams('aH90_mpHNam',$aH90s,'H1')
      if (BPpresent='y') then 
         BPsetparams('pwH90_mpHN',$pwH90s,'H1')
         BPsetparams('aH90_mpHN',$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 

// Obtain aH90_hp and pwH90_hp from the Probe File 

   if (($1='C13')and($2='alpha')) then 
      if (BPpresent<>'y') then 
         getparam('aH90_hpHCa','H1'):$aH90_hp   
         getparam('pwH90_hpHCa','H1'):$pwH90_hp 
      else 
         getparam('aH90_hpHC','H1'):$aH90_hp
         getparam('pwH90_hpHC','H1'):$pwH90_hp
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      if (BPpresent<>'y') then    
         getparam('aH90_hpHCO','H1'):$aH90_hp
         getparam('pwH90_hpHCO','H1'):$pwH90_hp
      else 
         getparam('aH90_hpHC','H1'):$aH90_hp
         getparam('pwH90_hpHC','H1'):$pwH90_hp
      endif
   elseif (($1='N15')and($2='amide')) then 
      if (BPpresent<>'y') then 
         getparam('aH90_hpHNai','H1'):$aH90_hp
         getparam('pwH90_hpHNai','H1'):$pwH90_hp
      else 
         getparam('aH90_hpHN','H1'):$aH90_hp
         getparam('pwH90_hpHN','H1'):$pwH90_hp
      endif
   elseif (($1='N15')and($2='amine')) then 
      if (BPpresent<>'y') then 
         getparam('aH90_hpHNam','H1'):$aH90_hp
         getparam('pwH90_hpHNam','H1'):$pwH90_hp
      else 
         getparam('aH90_hpHN','H1'):$aH90_hp
         getparam('pwH90_hpHN','H1'):$pwH90_hp
      endif
   endif

// Set Up the High-Power 1H Nutation Experiment

   aH90=$aH90_hp 
   $pwH90step=$pwH90_hp/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('1H Nutation Experiment at\\High Power 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('pwH90',51,$pwH90step,$pwH90step)
   wexp='SS_2_CP(\''+ $1 +'\',\'' + $2 + '\')'
   au
endif




