" SS_2_DP - Analyze the High-Power Nutation Curve, Optimize aH90_hp and "
"           Setup X Nutation.                                           "

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 sb='y' 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

// 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='adam')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_hpHC')
   elseif (($1='N15')and($2='amino')) then 
      BPsvf(userdir+'/AutoTripRes/H1_nutation_hpHN')
   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('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,1,0):$Fs
   if (($1='C13')and($2='adam')) then
      BPsetparams('R_hpHC',$Rs,'H1')
      BPsetparams('F_hpHC',$Fs,'H1')
      getparam('pwH90_hpHC','H1'):$pwH90_hp
   elseif (($1='N15')and($2='amino')) then
      BPsetparams('R_hpHN',$Rs,'H1')
      BPsetparams('F_hpHN',$Fs,'H1')
      getparam('pwH90_hpHN','H1'):$pwH90_hp
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif

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

// 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='adam')) then atext('High Power for C13-alpha') endif
   if (($1='N15') and ($2='amino')) then atext('High Power for N15-amino') endif
   atext(n1) atext(n2)
   array('aX90',21,1.2*$aX90,-0.02*$aX90)
   wexp='SS_2_DP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   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)

   $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

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

   $R=1
   if (($1='C13')and($2='adam')) then          
      getparam('R_hpHC','H1'):$R
   elseif (($1='N15')and($2='amino')) then
      getparam('R_hpHN','H1'):$R
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif
   $pwX90=($R-0.5)*pwX90

// Save Data and Update aH90_hp and pwH90_hp

   $pwX90=0.0125*trunc($pwX90/0.0125 + 0.5)
   $aX90=trunc($aX90 + 0.5)
   $pwH90s='' format($pwX90,6,4):$pwH90s
   $aH90s='' format($aX90,4,0):$aH90s  
   if (($1='C13')and($2='adam')) then  
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_hpHC')
      BPsetparams('pwH90_hpHC',$pwH90s,'H1')
      BPsetparams('aH90_hpHC',$aH90s,'H1')
   elseif (($1='N15')and($2='amino')) then
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_hp_HN')
      BPsetparams('pwH90_hpHN',$pwH90s,'H1')
      BPsetparams('aH90_hpHN',$aH900s,'H1')
   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,'pwH90 = %6.4f aH90 = %6.0f dpwr = %6.0f dof = %6.1f',$pwX90,$aX90,tpwr,tof)  
      write('line3','pwH90 = %6.4f aH90 = %6.0f dpwr = %6.0f dof = %6.1f',$pwX90,$aX90,tpwr,tof)  
   endif 

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

   $pwHspinal=2.0*$pwX90*17.0/18.0 $aHspinal=$aX90 $phHspinal=10.0
   $pwHspinal=0.2*trunc($pwHspinal/.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='adam')) then  
      BPsetparams('pwHspinal_HC',$pwHspinals,'H1')
      BPsetparams('aHspinal_HC',$aHspinals,'H1')
      BPsetparams('phHspinal_HC',$phHspinals,'H1')
   elseif (($1='N15')and($2='amino')) then 
      BPsetparams('pwHspinal_HN',$pwHspinals,'H1')
      BPsetparams('aHspinal_HN',$aHspinals,'H1')
      BPsetparams('phHspinal_HN',$phHspinals,'H1')
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif 

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

   $pwHtppm=2.0*$pwX90*17.0/18.0 $aHtppm=$aX90 $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='adam')) then  
      BPsetparams('pwHtppm_HC',$pwHtppms,'H1')
      BPsetparams('aHtppm_HC',$aHtppms,'H1')
      BPsetparams('phHtppm_HC',$phHtppms,'H1')
   elseif (($1='N15')and($2='amino')) then 
      BPsetparams('pwHtppm_HN',$pwHtppms,'H1')
      BPsetparams('aHtppm_HN',$aHtppms,'H1')
      BPsetparams('phHtppm_HN',$phHtppms,'H1')
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   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 

// Restablish and then Save the Basic 1H Calibrations

   pwX90=$pwX90
   aX90=$aX90
   setsolidsprobedata('tn')
   
// Setup the 13C Nutation Experiment

   if (BPpresent<>'y') then 
      if (($1='C13')and($2='adam')) then
         tn='C13' dn='H1' dn2='N15' ampmode='pdpp'
         getsolidsprobedata('tn') getsolidsprobedata('dn') getsolidsprobedata('dn2')
         setref 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
         tn='C13' dn='H1' dn2='N15' ampmode='pdpp'
         getsolidsprobedata('tn') getsolidsprobedata('dn') getsolidsprobedata('dn2')
         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' sbs='n' lb=25 gain=30 
      else
         banner('Calibration Nucleus and Region Not Found')
         abort
      endif
   else
      $srate=srate $temp=temp $n1=n1 $n2=n2 $n3=n3 
      if (($1='C13')and($2='adam')) then
         onepul_HC
         setref 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
         onepul_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
   endif 
   cdc ai  

// Get X Calibrations and High Power 1H Calibrations to Set Decoupling 

   if (($1='C13')and($2='adam')) then  
      getparam('aX90_HC','C13'):$aX90   
      getparam('pwX90_HC','C13'):$pwX90 
      getparam('aH90_hpHC','H1'):$aH90_hp   
      getparam('pwH90_hpHC','H1'):$pwH90_hp 
   elseif (($1='N15')and($2='amino')) then
      getparam('aX90_HN','N15'):$aX90   
      getparam('pwX90_HN','N15'):$pwX90 
      getparam('aH90_hpHN','H1'):$aH90_hp   
      getparam('pwH90_hpHN','H1'):$pwH90_hp 
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif

// Set Up Low Power Decoupling 

   aHspinal=$aH90_hp*$pwH90_hp/5.0 phHspinal=0.0 pwHspinal=8.0 Hseq='spinal'
   aHtppm=aHspinal phHtppm=phHspinal pwHtppm=pwHspinal

// Set Up the Arrays for 13C or 15N Nutation Using the Probe File

   aX90=$aX90
   $pwX90step=$pwX90/10.0

// Reset the Basic Proton Calibrations From the Probe File 

   aH90=$aH90_hp
   pwH90=$pwH90_hp

// 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('X Nutation Experiment')
   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('pwX90',51,$pwX90step,$pwX90step)
   wexp='SS_3_DP(\''+ $1 +'\',\'' + $2 + '\')'

   exists('autooffsetflag','parameter'):$e
   if ($e<0.5) then 
      create('autooffsetflag')
   endif
   autooffsetflag=1
   au   
endif
