" SS_4_DP - Analyze the Match Array to Optimize aHhx and Obtain a 1D CP Spectrum"

if ($#<3) then

// Analyze the CP Match Curve

   $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)

// Auto Phase

   vp=0 ph sb='n' wft lp=0 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

// Select the best H1 CP Match 

   aHhx=aHhx[$bestone]

// Save Data and Update aHhx from a CP Optimization 

   $aHhxs='' format(aHhx,4,0):$aHhxs
   $dHXs='' format(dHX,6,0):$dHXs
   $bHXs='' format(bHX,6,0):$bHXs
   $ofHXs='' format(ofHX,6,1):$ofHXs
   $tHXs='' format(tHX,6,4):$tHXs
   if (($1='C13')and($2='adam')) then  
      BPsvf(userdir+'/AutoTripRes/C13_DP_match_HC')
      BPsetparams('aHhx_HC',$aHhxs,'H1')
      BPsetparams('dHX_HC',$dHXs,'H1')
      BPsetparams('bHX_HC',$bHXs,'H1')
      BPsetparams('ofHX_HC',$ofHXs,'H1')
      BPsetparams('tHX_HC',$tHXs,'H1')
      BPsetparams('shHX_HC',shHX,'H1')
      BPsetparams('chHX_HC',chHX,'H1')
   elseif (($1='N15')and($2='amino')) then 
      BPsvf(userdir+'/AutoTripRes/N15_DP_match_HN')
      BPsetparams('aHhx_HN',$aHhxs,'H1')
      BPsetparams('bHX_HN',$dHXs,'H1')
      BPsetparams('dHX_HN',$bHXs,'H1')
      BPsetparams('ofHX_HN',$ofHXs,'H1')
      BPsetparams('tHX_HN',$tHXs,'H1')
      BPsetparams('shHX_HN',shHX,'H1')
      BPsetparams('chHX_HN',chHX,'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,'1H MATCH AMPLITUDE')
      write('line3','1H MATCH AMPLITUDE')
      write('file',$file,'aHhx = %6.0f dHX = %6.4f bHX = %6.4f tHX = %6.4f',aHhx,dHX,bHX,tHX)
      write('line3','aHhx = %6.0f dHX = %6.4f bHX = %6.4f tHX = %6.4f',aHhx,dHX,bHX,tHX)
   endif 

// Setup the CP Signal-to-Noise Experiment

   ph sb='n' at=0.05 sb=at/2.0 fn=16*np     
   nt=4 d1=5 sb='n' lb=10 
   sc=10 wc=200 vp=0 sb='n' noislm(1.0)

// Get Next Method from AutoOrderFile

   $orderfile = userdir + '\AutoOrderFile'
   exists($orderfile,'file):$e
   if ($e > 0.5) then
      SS_AutoCal_NEXT:wexp
   endif
 
   wnt=''
   text('SN of an Optimized CP 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)
   wexp='SS_4_DP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Auto Phase

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

// Plot the Optimized CP Spectrum

   pps(150,90,wcmax-150,wc2max-90)
   pl pscale pap 
   ds BPpage

// Clear wnt and wexp

   wnt='' wexp=''

// Save the Optimized CP Spectrum

   if (($1='C13')and($2='adam')) then  
      BPsvf(userdir+'/AutoTripRes/C13_DP_1D_HC')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_DP_1D_HN')
   endif  
 
   $file='Log_'+$1+'_'+$2
   ptext($file)

   $date1 = ''
   $date2=''
   $date3=''
   substr(date,1):$date1
   substr(date,2):$date2
   substr(date,3):$date3
   $datestring = 'DP_' + $date1 + '_' + $date2 + '_' +$date3
   SS_saveAutoTripRes_ALL(samplename,$1,$2,$datestring)

   write('line3','CALIBRATION DONE')
endif
