" SS_4_CP - 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 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

// 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,4,0):$dHXs
   $bHXs='' format(bHX,4,0):$bHXs
   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP_match_HCa')
      BPsetparams('aHhx_HCa',$aHhxs,'H1')
      BPsetparams('bHX_HCa',$bHXs,'H1')
      BPsetparams('dHX_HCa',$dHXs,'H1')
      if (BPpresent='y') then 
         BPsetparams('aHhx_HC',$aHhxs,'H1')
         BPsetparams('bHX_HC',$bHXs,'H1')
         BPsetparams('dHX_HC',$dHXs,'H1')
      endif 
   elseif (($1='C13')and($2='carbonyl')) then
      BPsvf(userdir+'/AutoTripRes/C13_CP_match_HCO')
      BPsetparams('aHhx_HCO',$aHhxs,'H1')
      BPsetparams('bHX_HCO',$bHXs,'H1')
      BPsetparams('dHX_HCO',$dHXs,'H1')
      if (BPpresent='y') then 
         BPsetparams('aHhx_HC',$aHhxs,'H1')
         BPsetparams('bHX_HC',$bHXs,'H1')
         BPsetparams('dHX_HC',$dHXs,'H1')
      endif
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_match_HNai')
      BPsetparams('aHhx_HNai',$aHhxs,'H1')
      BPsetparams('bHX_HNai',$bHXs,'H1')
      BPsetparams('dHX_HNai',$dHXs,'H1')
      if (BPpresent='y') then 
         BPsetparams('aHhx_HN',$aHhxs,'H1')
         BPsetparams('bHX_HN',$bHXs,'H1')
         BPsetparams('dHX_HN',$dHXs,'H1')
      endif 
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_match_HNam')
      BPsetparams('aHhx_HNam',$aHhxs,'H1')
      BPsetparams('bHX_HNam',$bHXs,'H1')
      BPsetparams('dHX_HNam',$dHXs,'H1')
      if (BPpresent='y') then 
         BPsetparams('aHhx_HN',$aHhxs,'H1')
         BPsetparams('bHX_HN',$bHXs,'H1')
         BPsetparams('dHX_HN',$dHXs,'H1')
      endif 
   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.0f bHX = %6.0f tHX = %6.4f',aHhx,dHX,bHX,tHX)
      write('line3','aHhx = %6.0f dHX = %6.0f bHX = %6.0f tHX = %6.4f',aHhx,dHX,bHX,tHX)
   endif 

// Setup the CP Signal-to-Noise Experiment

   ph sb='n' at=0.025 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)

   wnt=''
   text('SN of an Optimized CP \\Experiment')
   if (($1='C13')and($2='alpha')) then atext('for C13-alpha') endif
   if (($1='C13')and($2='carbonyl')) then atext('for C13-carbonyl') endif
   if (($1='N15')and($2='amide')) then atext('for N15-amide') endif
   if (($1='N15')and($2='amine')) then atext('for N15-amine') endif
   atext(n1) atext(n2)
   wexp='SS_4_CP(\''+ $1 +'\',\'' + $2 + '\',\'PART1\')'
   au

elseif ($3='PART1') then

// Auto Phase

   fn=16*np sb='n' ph 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

   if (($1='C13')and($2='alpha')) then 
      sp=0p wp=250p delta=50p cr=50p dc dsnmax dsn:$SN
   endif
   if (($1='C13')and($2='carbonyl')) then
      sp=0p wp=250p delta=50p cr=50p dc dsnmax dsn:$SN
   endif
   if (($1='N15')and($2='amide')) then 
      sp=0p wp=300p delta=50p cr=50p dc dsnmax dsn:$SN
   endif
   if (($1='N15')and($2='amine')) then 
      sp=0p wp=300p delta=50p cr=50p dc dsnmax dsn:$SN
   endif
   vp=0 sc=10 wc=200
   pl pscale pap BPpage
   ds

// Clear wnt and wexp

   wnt='' wexp=''

// Save the Optimized CP Spectrum

   if (($1='C13')and($2='alpha')) then  
      BPsvf(userdir+'/AutoTripRes/C13_CP_1D_HCa')
   elseif (($1='C13')and($2='carbonyl')) then 
      BPsvf(userdir+'/AutoTripRes/C13_CP_1D_HCO')
   elseif (($1='N15')and($2='amide')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_1D_HNai')
   elseif (($1='N15')and($2='amine')) then 
      BPsvf(userdir+'/AutoTripRes/N15_CP_1D_HNam')
  endif

// Write to Log 

   $file='Log_'+$1+'_'+$2
   cd(userdir)
   exists('AutoTripRes','file'):$e
   if ($e=1) then
      cd('AutoTripRes')
      write('file',$file,'FINAL SIGNAL TO NOISE')
      write('line3','FINAL 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 = 'CP_' + $date1 + '_' + $date2 + '_' +$date3 + '_' + $copy

   SS_saveAutoTripRes_ALL(BPsample,$1,$2,$datestring)

   write('line3','CALIBRATION DONE')

// Call Next Method

   if (BPchain = 'y') then 
      SS_AutoCal_CP1($1,$2)
   endif
endif


