" SS_3_CP - Analyze the X Tancpxflip Nutation Curve, Optimize aX90 and "
"           Setup the Match Array of aHhx as B1_X +/- 3*srate          "

if ($#<3) then

// Analyze the Tancpxflip X 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 

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

// Auto Phase

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

   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_nutation_HCa')
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/C13_nutation_HCO')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_nutation_HNai')
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_nutation_HNam')
   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)
      $pwX90calc=($R-0.5)*$pw180
      $F=4
   elseif ($best180>0) then 
      $pwX90calc=$pw180/2.0
      $F=2
   else
      $pwX90calc=$pw90
      $F=1
   endif

   if (($F=0)or($R<=0.75)) then 
      banner('X 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,'TANCPXFLIP X NUTATION')
      write('line3','TANCPXFLIP X 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 $pwX90 to Run the 90-Degree Amplitude Optimization

   $Rs='' format($R,6,4):$Rs
   $Fs='' format($F,6,4):$Fs
   if (($1='C13')and($2='alpha')) then
      BPsetparams('R_HCa',$Rs,'C13')
      BPsetparams('F_HCa',$Fs,'C13')
      if (BPpresent='y') then 
         BPsetparams('R_HC',$Rs,'C13')
         BPsetparams('F_HC',$Fs,'C13')
         getparam('pwX90_HC','C13'):$pwX90_sp
      else
         getparam('pwX90_HCa','C13'):$pwX90_sp
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsetparams('R_HCO',$Rs,'C13')
      BPsetparams('F_HCO',$Fs,'C13')
      if (BPpresent='y') then 
         BPsetparams('R_HC',$Rs,'C13')
         BPsetparams('F_HC',$Fs,'C13')
         getparam('pwX90_HC','C13'):$pwX90_sp
      else
         getparam('pwX90_HCO','C13'):$pwX90_sp
      endif 
   elseif (($1='N15')and($2='amide')) then
      BPsetparams('R_HNai',$Rs,'N15')
      BPsetparams('F_HNai',$Fs,'N15')
      if (BPpresent='y') then 
         BPsetparams('R_HN',$Rs,'N15')
         BPsetparams('F_HN',$Fs,'N15')
         getparam('pwX90_HN','N15'):$pwX90_sp
      else
         getparam('pwX90_HNai','N15'):$pwX90_sp
      endif 
   elseif (($1='N15')and($2='amine')) then
      BPsetparams('R_HNam',$Rs,'N15')
      BPsetparams('F_HNam',$Fs,'N15')
      if (BPpresent='y') then 
         BPsetparams('R_HN',$Rs,'N15')
         BPsetparams('F_HN',$Fs,'N15')
         getparam('pwX90_HN','N15'):$pwX90_sp
      else
         getparam('pwX90_HNam','N15'):$pwX90_sp
      endif
   endif 

   $aX90=aX90*($pwX90calc/$pwX90_sp)  "adjust the amplitude"
   $pwX90=$pwX90_sp                   "set the 90-degree pulse"
   $pwX90=0.0125*trunc($pwX90/0.0125 + 0.5)
   pwX90=$pwX90

// Setup X Amplitude Optimization from the Tancpxflip 90-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('C13 Amplitude from the 90-Degree Null')
   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('aX90',21,1.2*$aX90,-0.02*$aX90)
   wexp='SS_3_CP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Analyze X Amplitude Optimization from the Tancpxflip 90-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)
   select($bestone)

// 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',$lim):$bestone1,$remainder
   else 
      write('line3','Abort: (aX90) 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  
  
   $aX90=(aX90[$bestone1] + (aX90[$bestone1+1] - aX90[$bestone1])*$remainder)
   $aX90=trunc($aX90 + 0.5) 
   $pwX90=pwX90

// aXhx is always set equal to aX90

   $aXhx=$aX90

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'pwX90 = %6.4f aX90 = %6.0f aXhx = %6.0f tpwr = %6.0f tof = %6.1f',$pwX90,$aX90,$aXhx,tpwr,tof)  
      write('line3','pwX90 = %6.4f aX90 = %6.0f aXhx = %6.0f tpwr = %6.0f tof = %6.1f',$pwX90,$aX90,$aXhx,tpwr,tof)  
   endif 

// Save Data and Update aX90, pwX90 and aXhx

   $pwX90s='' format($pwX90,6,4):$pwX90s
   $aX90s='' format($aX90,4,0):$aX90s
   $aXhxs='' format($aXhx,4,0):$aXhxs
   display('dmg','processed')
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_null_amp_HCa')
      BPsetparams('pwX90_HCa',$pwX90s,'C13')
      BPsetparams('aX90_HCa',$aX90s,'C13')
      BPsetparams('aXhx_HCa',$aXhxs,'C13')
      if (BPpresent='y') then  
         BPsetparams('pwX90_HC',$pwX90s,'C13')
         BPsetparams('aX90_HC',$aX90s,'C13')
         BPsetparams('aXhx_HC',$aXhxs,'C13')
      endif
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/C13_null_amp_HCO')
      BPsetparams('pwX90_HCO',$pwX90s,'C13')
      BPsetparams('aX90_HCO',$aX90s,'C13')
      BPsetparams('aXhx_HCO',$aXhxs,'C13')
      if (BPpresent='y') then  
         BPsetparams('pwX90_HC',$pwX90s,'C13')
         BPsetparams('aX90_HC',$aX90s,'C13')
         BPsetparams('aXhx_HC',$aXhxs,'C13')
      endif
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_null_amp_HNai')
      BPsetparams('pwX90_HNai',$pwX90s,'N15')
      BPsetparams('aX90_HNai',$aX90s,'N15')
      BPsetparams('aXhx_HNai',$aXhxs,'N15')
      if (BPpresent='y') then  
         BPsetparams('pwX90_HN',$pwX90s,'N15')
         BPsetparams('aX90_HN',$aX90s,'N15')
         BPsetparams('aXhx_HN',$aXhxs,'N15')
      endif
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_null_amp_HNam')
      BPsetparams('pwX90_HNam',$pwX90s,'N15')
      BPsetparams('aX90_HNam',$aX90s,'N15')
      BPsetparams('aXhx_HNam',$aXhxs,'N15')
      if (BPpresent='y') then  
         BPsetparams('pwX90_HN',$pwX90s,'N15')
         BPsetparams('aX90_HN',$aX90s,'N15')
         BPsetparams('aXhx_HN',$aXhxs,'N15')
      endif
   endif 

// Obtain Tancpx Parameters from the Probe File 

   if (($1='C13')and($2='alpha')) then          
      getparam('pwH90_mpHCa','H1'):$pwH90_mp
      getparam('aH90_mpHCa','H1'):$aH90_mp
      getparam('aH90_hpHCa','H1'):$aH90_hp
   elseif (($1='C13')and($2='carbonyl')) then
      getparam('pwH90_mpHCO','H1'):$pwH90_mp
      getparam('aH90_mpHCO','H1'):$aH90_mp
      getparam('aH90_hpHCO','H1'):$aH90_hp
   elseif (($1='N15')and($2='amide')) then
      getparam('pwH90_mpHNai','H1'):$pwH90_mp
      getparam('aH90_mpHNai','H1'):$aH90_mp
      getparam('aH90_hpHNai','H1'):$aH90_hp
   elseif (($1='N15')and($2='amine')) then
      getparam('pwH90_mpHNam','H1'):$pwH90_mp
      getparam('aH90_mpHNam','H1'):$aH90_mp
      getparam('aH90_hpHNam','H1'):$aH90_hp
   endif

// Set the new aX90, aXhx for Tancpx Match Curve - pwX90 is correct.

   aX90=$aX90
   aXhx=$aXhx

// Setup the Tancpx Match Array for the Medium-Power $B1 +/- 3.0*srate

   seqfil='tancpx' 
   $B1=1.0e6/(4.0*$pwH90_mp) 
   $aHhxmax=$aH90_mp*($B1 + 3.0*srate)/$B1
   if ($aHhxmax>$aH90_hp) then $aHhxmax=$aH90_hp endif
   $aHhxmin=$aH90_mp*($B1 - 3.0*srate)/$B1
   if ($aHhxmin<50.0) then $aHhxmin=50 endif  
   $aHhxstep=($aHhxmax - $aHhxmin)/40.0

   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('CP 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
   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('aHhx',41,$aHhxmax,-$aHhxstep)
   wexp='SS_4_CP(\''+ $1 +'\',\'' + $2 + '\')'
   au
endif 
