" SS_1_DP - Analyze the Medium-Power Nutation Curve, Optimize aH90_mp and "
"           Setup High-Power Nutation.                                    "

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 sb='y' av wft
   if ($counter<(arraydim-1)) then
   repeat
      $counter=$counter+1
      write('line3','$counter=%d',$counter)
      if ($counter<=arraydim-1) 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
      write('line3','$counter=%d',$counter)
      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
      write('line3','$counter=%d',$counter)
      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
      write('line3','$counter=%d',$counter)
      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

// Reset the Offset and Repeat the MP Nutation

   exists('autooffsetflag','parameter'):$e
   if ($e>0.5) then
      if (autooffsetflag>0) then
         $tof=tof
         peak:$int,$freq
         cr=$freq nl
         $lockfreq=lockfreq
         rl(2.0p) calclockfreq(2.0p)        
         $lockfreq1=$lockfreq-lockfreq
         $dtof=$lockfreq1*1e6*sfrq/lockfreq 
         movetof 
         tof=tof + $dtof      
         abs($dtof):$tof1
         if ($dtof<0.05*sfrq) then
            autooffsetflag=0
         else
            autooffsetflag=autooffsetflag-1
            au
         endif
      endif
      if (autooffsetflag<=0) then
         destroy('autooffsetflag')
      endif
   endif
   sp=-8p wp=20p setref

// Update the Lock Frequency in Probe File

   $lockfreqs='' format(lockfreq,11,7):$lockfreqs
   BPsetparams('lockfreq',$lockfreqs,'Probe')
   BPsetparams('solvent',solvent,'Probe')

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

// 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/H1_nutation_mpHC')
   elseif (($1='N15')and($2='amino')) then
      BPsvf(userdir+'/AutoTripRes/H1_nutation_mpHN')
   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('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='adam')) then
      BPsetparams('R_mpHC',$Rs,'H1')
      BPsetparams('F_mpHC',$Fs,'H1')
      getparam('pwH90_mpHC','H1'):$pwH90_mp
   elseif (($1='N15')and($2='amino')) then
      BPsetparams('R_mpHN',$Rs,'H1')
      BPsetparams('F_mpHN',$Fs,'H1')
      getparam('pwH90_mpHN','H1'):$pwH90_mp
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif

   $aX90=aX90*($pwX90calc/$pwH90_mp) "adjust the amplitude"
   $aX90=trunc($aX90 + 0.5)
   $pwX90=$pwH90_mp/($R-0.5) "set the 180-degree pulse" 
   $pwX90=0.0125*trunc($pwX90/0.0125 + 0.5)
   pwX90=$pwX90
   
// Setup the Medium-Power 1H Amplitude-Optimization Experiment from the 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('1H Amplitude Optimization\\for a 180-Degree Null at\\Medium Power')
   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_1_DP(\''+ $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 the 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_mpHC','H1'):$R 
   elseif (($1='N15')and($2='amino')) then
      getparam('R_mpHN','H1'):$R
   else
      banner('Calibration Nucleus and Region Not Found')
      abort
   endif
   $pwX90=($R-0.5)*pwX90

// Save Data and Update aH90_mp and pwH90_mp

   $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_mpHC')   
      BPsetparams('pwH90_mpHC',$pwH90s,'H1')
      BPsetparams('aH90_mpHC',$aH90s,'H1')
   elseif (($1='N15')and($2='amino')) then 
      BPsvf(userdir+'/AutoTripRes/H1_pwH180_null_amp_mpHN')
      BPsetparams('pwH90_mpHN',$pwH90s,'H1')
      BPsetparams('aH90_mpHN',$aH90s,'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 

// Obtain aH90_hp and pwH90_hp from the Probe File 

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

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

   aX90=$aH90_hp 
   $pwX90step=$pwH90_hp/10.0

// 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('1H Nutation Experiment at\\High Power')
   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_2_DP(\''+ $1 +'\',\'' + $2 + '\')'
   au
endif
