" SS_3_DCP - Analyze the DCP Amplitudes, Beta, Delta and Contact-time"

if ($#<3) then

// Analyze the Joint aYyx and aXyx Amplitude Dependence

   $bestone=1 $counter=1 $best=0
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   repeat
      select($counter)
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter endif
      $counter=$counter+1
   until ($counter>arraydim)

// Add up the Intensities of Groups
   
   $sum_group=0 $index=1 $best_start=0
   $count=1 $sum_group_best=0
   $size_aYyx=size('aYyx')
   $size_aXyx=size('aXyx')
   repeat
      repeat
         select($index) dc peak:$ht
         $sum_group=$sum_group+$ht
         $index=$index+1
         $count=$count+1
      until ($count>$size_aXyx)
      if ($sum_group>$sum_group_best) then
         $sum_group_best=$sum_group
         $best_start=$index-$size_aXyx 
      endif
      write('line3','sum=%f',$sum_group)
      $sum_group=0
      $count=1
   until ($index>arraydim)

   $best_aYyx=(($best_start-1)/$size_aXyx) + 1
   $aYyx=aYyx[$best_aYyx]

// Auto Phase

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

// Plot Data

   sc=200 wc=10 ho=-2
   pps(150,90,wcmax-150,wc2max-90)
   pl('all','dodc') pap 
   dssh('all','dodc') BPpage

// Save the Data   

   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_aYyx_aXyx_array_NCa')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_aYyx_aXyx_array_NCO')
   endif

// Redo the aXyx Array with Increased Resolution

   if ($2='alpha') then
      getparam('aX32wr_HCa','C13'):$aX32wr_sp
      getparam('aX52wr_HCa','C13'):$aX52wr_sp
   elseif ($2='carbonyl') then
      getparam('aX32wr_HCO','C13'):$aX32wr_sp
      getparam('aX52wr_HCO','C13'):$aX52wr_sp
   endif

   aYyx=$aYyx
   $aXyx=aXyx[1]

   $stepX=trunc(($aX52wr_sp-$aX32wr_sp)*0.02 + 0.5)
   array('aXyx',41,$aXyx,$stepX)

   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('DCP aXyx 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
   atext(n1) atext(n2)
   wexp='SS_3_DCP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Analyze the Fine aXyx Dependence

   $bestone=1 $counter=1 $best=0
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   repeat
      select($counter)
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter endif
      $counter=$counter+1
   until ($counter>arraydim)

   $aYyx=aYyx
   $aXyx=aXyx[$bestone]

// Auto Phase

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

// Plot Data

   sc=200 wc=10 ho=-2
   pps(150,90,wcmax-150,wc2max-90)
   pl('all','dodc') pap 
   dssh('all','dodc') BPpage

// Save the Data and Write Best Match to Probe File

   $aYyxs='' format($aYyx,4,0):$aYyxs
   $aXyxs='' format($aXyx,4,0):$aXyxs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_aXyx_array_NCa')
      BPsetparams('aYyx_NCa',$aYyxs,'N15')
      BPsetparams('aXyx_NCa',$aXyxs,'C13')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_aXyx_array_NCO')
      BPsetparams('aXyx_NCO',$aXyxs,'N15')
      BPsetparams('aXyx_NCO',$aXyxs,'C13')
   endif

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'DCP AMPLITUDES')
      write('line3','DCP AMPLITUDES')
      write('file',$file,'aXyx = %6.0f aYyx = %6.0f',$aXyx,$aYyx)
      write('line3','aXyx = %6.0f aYyx = %6.0f',$aXyx,$aYyx)
   endif

   aXyx=$aXyx

// Set Up the Array of beta and delta 

   array('bYX',10,10,10)
   array('dYX',13,10,10)

   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('DCP beta-delta 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
   atext(n1) atext(n2)
   wexp='SS_3_DCP(\''+ $1 +'\',\'' + $2 + '\',\'PART2\')'
   au

elseif ($3='PART2') then

// Analyze the bYX dependence

   $bestone=1 $counter=1 $best=0
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft
   repeat
      select($counter)
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter endif
      $counter=$counter+1
   until ($counter>arraydim)

// Add up the Intensities of the Groups

   $sum_group=0 $index=1 $best_start=0
   $count=1 $sum_group_best=0
   $size_bYX=size('bYX') 
   $size_dYX=size('dYX')
   repeat
      repeat
         select($index) dc peak:$ht
         $sum_group=$sum_group+$ht
         $index=$index+1
         $count=$count+1
      until ($count>$size_dYX)
      if ($sum_group>$sum_group_best) then
         $sum_group_best=$sum_group
         $best_start=$index-$size_dYX 
      endif
      $sum_group=0
      $count=1
   until ($index>arraydim)

   $best_bYX_index=trunc(($best_start-1)/$size_dYX + 1)
   $bYX=bYX[$best_bYX_index]
   dssh($best_start,$best_start+$size_dYX-1,'dodc') s1
   vp=90 sp=155p wp=40p s2
   dsshn($best_start,$best_start+$size_dYX-1,'dodc')

   select($bestone)
   sp=0p wp=200p dc cz isadj(100)
   if (($1='C13')and($2='alpha')) then  
      z(75p,45p,44p,0p)
      integ(45p,75p):$best_int1  
      integ(0p,44p):$best_int2
   elseif (($1='C13')and($2='carbonyl')) then
      z(195p,155p,75p,0p)
      integ(155p,195p):$best_int1  
      integ(0p,75p):$best_int2
   endif

   $best_int1_counter=$bestone
   $best_int2_counter=$bestone
   $int2_at_best_int1_counter=$best_int2
   $int1_at_best_int2_counter=$best_int1

   $i=$best_start
   $c=1 $cbest=1 $best_ht=0
   repeat
      select($i)
      peak:$ht
      if ($ht>$best_ht) then $best_ht=$ht $cbest=$c endif
      if (($1='C13')and($2='alpha')) then  
         integ(45p,75p):$int1  
         integ(0p,44p):$int2
      elseif (($1='C13')and($2='carbonyl')) then
         integ(155p,195p):$int1  
         integ(0p,75p):$int2
      endif
      if ($int1>$best_int1) then
         $best_int1=$int1
         $best_int1_counter=$i
         $int2_at_best_int1_counter=$int2
      endif
      if ($int2<$best_int2) then
         $best_int2=$int2
         $best_int2_counter=$i 
         $int1_at_best_int2_counter=$int1
      endif
      $i=$i+1
      $c=$c+1
   until ($i=$best_start+$size_dYX-1)

   $best_dYX_index=trunc(($best_start-1)/$size_dYX +1)
   $best_dYX=dYX[$best_dYX_index]

   $dYX=dYX[$cbest]

// Auto Phase

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

// Plot Data

   sc=200 wc=10 ho=-2 sp=0p wp=200p
   pps(150,90,wcmax-150,wc2max-90)
   pl($best_start,$best_start+$size_dYX-1,'dodc') pap
   dssh($best_start,$best_start+$size_dYX-1,'dodc') BPpage

 // Make Full Plots of the Data
 
   vp=20 
   select($bestone) vsadj(100) s1
   vsadj(60) s2
   dssh('all','dodc') pl('all','dodc')
   if ($2='alpha') then
      write('plotter',0,0,' 45-90ppm Alpha Carbon Region: In groups of different bYX')
      sp=0p wp=45p vp=85 s3
      dsshn('all','dodc') pl('all','dodc')
      write('plotter',0,75,'0-45ppm Aliphatic Region: In groups of different bYX')
   else
      write('plotter',0,0,' 150-200ppm Carbonyl Carbon Region: In groups of different bYX')
      sp=150p wp=50p vp=85 s3
      dsshn('all','dodc') pl('all','dodc')
      write('plotter',0,75,'0-70ppm Aliphatic Region: In groups of different bYX')
   endif
   pltext BPpage

   r1
   dssh($best_start,$best_start+12,'dodc')
   pl($best_start,$best_start+12,'dodc')
   pap 
   if ($2='alpha') then
      write('plotter',wcmax/2,wc2max-15,'Best dYX value for Alpha C S/N is %4.0f',$dYX)
   else
      write('plotter',wcmax/2,wc2max-15,'Best dYX value for Carbonyl C S/N is %4.0f',$dYX)
   endif
   write('plotter',wcmax/2,wc2max-10,'Best bYX value is %4.0f',$bYX)
   write('plotter',wcmax/2,wc2max-20,'Best dYX value for Selectivity is %4.0f',$best_dYX) 
   BPpage

// Save Data and Write the Best Match to Probe File

   $bYXs='' format($bYX,4,0):$bYXs
   $dYXs='' format($dYX,4,0):$dYXs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_bYX_dYX_array_NCa')
      BPsetparams('bYX_NCa',$bYXs,'N15')
      BPsetparams('dYX_NCa',$dYXs,'N15')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_bYX_dYX_array_NCO')
      BPsetparams('bYX_NCO',$bYXs,'N15')
      BPsetparams('dYX_NCO',$dYXs,'N15')
   endif

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'TANGENT BETA AND DELTA')
      write('line3','TANGENT BETA and DELTA')
      write('file',$file,'bYX = %6.0f dYX = %6.0f',$bYX,$dYX)
      write('line3','bYX = %6.0f dYX = %6.0f',$bYX,$dYX)
   endif 

   bYX=$bYX
   dYX=$dYX

// Setup the Contact-time Array

   if (($1='C13')and($2='alpha')) then  
      sp=45p wp=30p
   elseif (($1='C13')and($2='carbonyl')) then
      sp=155p wp=40p
   endif

   array('tYX',10,1000,1000)
   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('DCP Contact-time 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
   atext(n1) atext(n2)
   wexp='SS_3_DCP(\''+ $1 +'\',\'' + $2 + '\',\'PART3\')'
   au

elseif ($3='PART3') then

// Analyze Contact-time Array

   $bestone=1 $counter=1 $best=0   
   fn=16*np vp=0 sb='n' if (BPplotmode='ph') then ph else av endif wft   
   repeat
      select($counter)
      peak:$ht
      if ($ht>$best) then $best=$ht $bestone=$counter endif
      $counter=$counter+1
   until ($counter>arraydim)

   $tYX=tYX[$bestone]

// Auto Phase

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

// Plot Data

   sc=200 wc=10 ho=-2
   pps(150,90,wcmax-150,wc2max-90)
   pl('all','dodc') pap 
   dssh('all','dodc') BPpage

// Save Data and Write the Best Match to Probe File

   $tYXs='' format($tYX,6,4):$tYXs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_tYX_array_NCa')
      BPsetparams('tYX_NCa',$tYXs,'N15')
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_dcptan_tYX_array_NCO')
      BPsetparams('tYX_NCO',$tYXs,'N15')
   endif

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'CONTACT TIME')
      write('line3','CONTACT TIME')
      write('file',$file,'tYX = %6.4f',$tYX)
      write('line3','tYX = %6.4f',$tYX)
   endif 

   tYX=$tYX

// Set Up a Final Optimized Spectrum 

   nt=4 d1=5 lb=0 ph bs=4
   wnt=''
   text('Optimized DCP Spectrum 64 scans')
   if (($1='C13')and($2='alpha')) then atext('for C13-alpha') endif
   if (($1='C13')and($2='carbonyl')) then atext('for C13-carbonyl') endif
   atext(n1) atext(n2)
   wexp='SS_3_DCP(\''+ $1 +'\',\'' + $2 + '\',\'PART4\')'
   au

elseif ($3='PART4') then

// Auto Phase

   fn=16*np vp=0 sb='n' ph lp=0 wft vsadj(100) noislm(1.0)
   aph0:$aphok
   if ($aphok=1) then
      aph0
      peak:$int
      if ($int<-10) then rp=rp+180 endif
   endif

// Find the Signal to Noise and Make a Final Plot

   sp=0p wp=250p delta=50p cr=50p dsnmax dsn:$SN
   vp=0 sc=10 wc=200
   pl pscale pap BPpage
   ds

   wexp=''

// Save the Optimized CP Spectrum

   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_DCP4_1D_NCa')
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/C13_DCP4_1D_NCO')
   endif

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'FINAL DCP SIGNAL TO NOISE')
      write('line3','FINAL DCP SIGNAL TO NOISE')
      write('file',$file,'SN = %6.0f',$SN)
      write('line3','SN = %6.0f',$SN)
   endif 

   ptext($file)

// Finish and Save AutoTripRes with samplename and date

   $copy = ''
   format(BPcopy,1,0):$copy
   $date1 = ''
   $date2=''
   $date3=''
   substr(date,1):$date1
   substr(date,2):$date2
   substr(date,3):$date3
   $datestring = 'DCP_' + $date1 + '_' + $date2 + '_' +$date3 + '_' + $copy
   SS_saveAutoTripRes_ALL(BPsample,$1,$2,$datestring)
   BPcopy = BPcopy + 1

   write('line3','CALIBRATION DONE')

   SS_AutoCal_CP($1,$2)

endif
