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

if ($#<3) then

// Analyze the C13 or 15N Nutation Experiments

   $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=pwX90[$best90]
   if (($best90>1)and($best90<arraydim-1)) then 
      SS_AutoCal_minmaxfit($best90,'max'):$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>1.25*$trial)or($counter>=arraydim))
   $pw180=pwX90[$best180]
   if (($best180>1)and($best180<arraydim-1)) then
      SS_AutoCal_minmaxfit($best180,'min'):$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<0.75*$trial)or($counter>arraydim))
   $pw270=pwX90[$best270]
   if (($best270>1)and($best270<arraydim-1)) then 
      SS_AutoCal_minmaxfit($best270,'max'):$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>1.25*$trial)or($counter>=arraydim))
   $pw360=pwX90[$best360]
   if (($best360>1)and($best360<arraydim-1)) then
      SS_AutoCal_minmaxfit($best360,'min'):$best360,$rem360 
      $pw360=pwX90[$best360] + $rem360*(pwX90[$best360+1]-pwX90[$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

// Apodize the Data and Repeat the Analysis

   exists('autooffsetflag','parameter'):$e
   if ($e>0.5) then
      if (autooffsetflag>0) then
         autooffsetflag=0
         sb=0.1 lb=5 wft select($best90)
         peak:$int,$freq
         cr=$freq
         dres:$linewidth 
         write('line3','linewidth=%f',$linewidth)
         if ($linewidth<5.0) then $linewidth=5.0 endif
         $lb=$linewidth
         lb=trunc($lb +0.5)          
         $sb=1.0/$linewidth
         sb=0.0001*trunc($sb/0.0001 + 0.5)
      endif
      if (autooffsetflag<=0) then
         destroy('autooffsetflag')
         if (($1='C13')and($2='adam')) then  
            SS_3_DP('C13','adam')   
         elseif (($1='N15')and($2='amino')) then 
            SS_3_DP('N15','amino') 
         endif
      endif
   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='adam')) then  
      BPsvf(userdir+'/AutoTripRes/C13_nutation_hpHC')
   elseif (($1='N15') and ($2='amino')) then 
      BPsvf(userdir+'/AutoTripRes/N15_nutation_hpHN')
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   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,'X NUTATION')
      write('line3','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 Set the 180-Degree Pulse For Optimization

   $Rs='' format($R,6,4):$Rs
   $Fs='' format($F,1,0):$Fs
   if (($1='C13')and($2='adam')) then
      BPsetparams('R_HC',$Rs,'C13')
      BPsetparams('F_HC',$Fs,'C13')
      getparam('pwX90_HC','C13'):$pwX90_sp
   elseif (($1='N15')and($2='amino')) then
      BPsetparams('R_HN',$Rs,'N15')
      BPsetparams('F_HN',$Fs,'N15')
      getparam('pwX90_HN','N15'):$pwX90_sp
   endif 

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

// Setup X Amplitude Optimization from the X 180-Degree Null

// Set the Auto Display to ph or av

   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 180-Degree Null')
   if (($1='C13')and($2='adam')) then atext('for C13-adam') endif
   if (($1='N15')and($2='amino')) then atext('for N15-amino') endif
   atext(n1) atext(n2)
   array('aX90',21,1.2*$aX90,-0.02*$aX90)
   wexp='SS_3_DP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Analyze X 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',$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)

   $R=1
   if (($1='C13')and($2='adam')) then          
      getparam('R_HC','C13'):$R
   elseif (($1='N15') and ($2='amino')) then
      getparam('R_HN','C13'):$R
   endif
   $pwX90=($R-0.5)*pwX90

// aXhx is always set equal to aX90

   $aXhx=$aX90

// Save Data and Update aX90, pwX90 and aXhx in the Probe File

   $pwX90=0.0125*trunc($pwX90/0.0125 + 0.5)
   $aX90=trunc($aX90 + 0.5)
   $aXhx=trunc($aXhx + 0.5)
   $pwX90s='' format($pwX90,6,4):$pwX90s
   $aX90s='' format($aX90,4,0):$aX90s
   $aXhxs='' format($aXhx,4,0):$aXhxs
   if (($1='C13')and($2='adam')) then  
      BPsvf(userdir+'/AutoTripRes/C13_null_amp_HC')
      BPsetparams('pwX90_HC',$pwX90s,'C13')
      BPsetparams('aX90_HC',$aX90s,'C13')
      BPsetparams('aXhx_HC',$aXhxs,'C13')
   elseif (($1='N15')and($2='amino')) then 
      BPsvf(userdir+'/AutoTripRes/N15_null_amp_HN')
      BPsetparams('pwX90_HN',$pwX90s,'N15')
      BPsetparams('aX90_HN',$aX90s,'N15')
      BPsetparams('aXhx_HN',$aXhxs,'N15')
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif 

// 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 

// Setup the Tancpx Match Experiment

   if (BPpresent<>'y') then
      tancpx
   else
      $srate=srate $temp=temp $n1=n1 $n2=n2 $n3=n3
      $aHspinal=aHspinal $pwHspinal=pwHspinal $phHspinal=phHspinal $Hseq=Hseq
      $aHtppm=aHtppm $pwHtppm=pwHtppm $phHtppm=phHtppm
      if (($1='C13')and($2='adam')) then
         tancpx_HC
         setoffset('C13',33.3):tof tof=0.1*trunc(tof/0.1 + 0.5) sp=33p wp=10p setref
         ad=4.0 rd=4.0 ddrtc=8.0 lp=0 d1=3.0 nt=4 ss=2 bs=4
         sw=10000.0 at=0.1 fn=16.0*np sb=at/2.0 sb='n' sbs='n' lb=10 gain=30
      elseif (($1='N15')and($2='amino')) then
         tancpx_HN
         setref setoffset('N15',20.0):tof tof=0.1*trunc(tof/0.1 + 0.5) sp=5p wp=30p setref
         ad=4.0 rd=16.0 ddrtc=20.0 lp=0 d1=3.0 nt=4 ss=2 bs=4
         sw=20000.0 at=0.05 fn=16.0*np sb=at/2.0 sb='n' sb='n' sbs='n' lb=10 gain=30
      else
         banner('Calibration Nucleus and Region Not Found')
         abort
      endif
      srate=$srate temp=$temp n1=$n1 n2=$n2 n3=$n3
      aHspinal=$aHspinal pwHspinal=$pwHspinal phHspinal=$phHspinal Hseq=$Hseq
      aHtppm=$aHtppm pwHtppm=$pwHtppm phHtppm=$phHtppm
   endif
   cdc ai

// Establish the Remaining Ramp Parameters

chHX='fr' frHX='dec' toHX='obs' 

// Set Current aX90, pwX90 and aXhx for Tancpx Match Curve

   aX90=$aX90
   pwX90=$pwX90
   aXhx=$aXhx

// Setup Tancpx Parameters from the Probe File 

   if (($1='C13')and($2='adam')) then          
      getparam('pwH90_mpHC','H1'):$pwH90_mp
      getparam('aH90_mpHC','H1'):$aH90_mp
      getparam('pwH90_hpHC','H1'):$pwH90_hp         
      getparam('aH90_hpHC','H1'):$aH90_hp
      tHX=3000
   elseif (($1='N15')and($2='amino')) then
      getparam('pwH90_mpHN','H1'):$pwH90_mp
      getparam('aH90_mpHN','H1'):$aH90_mp
      getparam('pwH90_hpHN','H1'):$pwH90_hp
      getparam('aH90_hpHN','H1'):$aH90_hp       
      tHX=1000 
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif

   aH90=$aH90_hp pwH90=$pwH90_hp
   ofHX=0.0 shHX='t' chHX='fr' tHX=3000.0 frHX='dec' toHX='obs'
   dHX = 0.1*$aH90_mp bHX=10000.0

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

   $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

// Set the Auto Display to ph or av

   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='adam')) then atext('for C13-adam') endif
   if (($1='C13')and($2='amino')) then atext('for N15-amino') endif
   atext(n1) atext(n2)
   array('aHhx',41,$aHhxmax,-$aHhxstep)
   wexp='SS_4_DP(\''+ $1 +'\',\'' + $2 + '\')'
   au
endif 
