// dcpYXprobe - A macro to get or set YX CP calibrations to or from the
//              probe file for 1D and 2D DCP experiments (AhYX). 

//       Note: (Only CP) Standard parameters are set by cpHXprobe and cpHYprobe. 

//             Argument 1 is:
//                        'set' loads values into the probe file. 
//                        'get' sets values into the workspace. 
//                        'init' creates and loads values inot the probe 
//                               file (identical to 'set'). 
//                        'calc' sets the probe file for DCP based on aX32wr 
//                               (X-region) and aX52wr (Y-region) from the 
//                               probe file. 

//             Argument 2 is the observe X nucleus 'C13' 
//                       (At present: 13C is always the X nucleus)

//             Argument 3 is the region:
//                       'alpha'(13C alpha),DCP code $reg2 = 'NCa', $reg = 'HCa'
//                       'carbonyl(13C carbonyl), DCP code $reg2 = 'NCO', $reg='HCO' 
//                        At present: the 15N region if fixed as 'amide, $reg1 = 'HNai'

//                  Also: For calibration with the 13C offset at 100 ppm set:
//                       'salpha'(13C alpha),DCP code $reg2 = 'sNCa', $reg = 'sHCa'
//                       'scarbonyl(13C carbonyl), DCP code $reg2 = 'sNCO', $reg='sHCO'

//             Argument 4 is a label: 
//                        '' is forced as the default, not used in this macro. 
//             Argument 5 is a numeral:
//                        usually 0 or 1 if present, but not used.
//
//             Set Parameters ('get','set','init'): 
//                      Nucleus                Workspace <-> Probe                 Region
//                     (Y = 15N) chYX,shYX,toYX,frYX,aYyx,bYX,dYX,tYX,ofYX <->
//                                 chYX,shYX,toYX,frYX,aYyx,bYX,dYX,tYX,ofYX   (C13, DCP alpha,DCP carbonyl)
//                     (X = 13C) aXyx <-> aXyx                                 (for all params)
//                           (H) aHyx <-> aHyx                                 
//                         
//        (B)  Calculate Parameters ('calc') 
//                       Nucleus               Value <-> Probe                     Region
//                     (Y = 15N) 'fr','t','obs','dec',$aX52wr,(*1),(*2),3000.0,0.0 <->
//                                 chYX,shYX,toYX,frYX,aYyx,bYX,dYX,tYX,ofYX   (C13, DCP alpha, DCP carbonyl)
//                     (X = 13C) aXyx <-> $aX32wr                              (for all params)
//                           (H) aHyx <-> $aH90_hp                                

//             NOTES: for Calculations Above '$' refers to values obtained 
//                    from the probe file. 

//             $aX32wr is obtained from the X region (C13, alpha, carbonyl only)
//             $aH90_hp is obtained from the X region (C13, alpha, carbonyl only)
//             $aX52wr is obtained from the Y region  (N15, amide only)
//             (*1) = 0.05*$aX52wr   
//             (*2) = 0.05*$aX52wr

// Initialize CP Parameters in the Probe File

if (($#>0) and ($1='calc')) then

// Set AhYX

   AhYX

// Set the X Region

   $reg='' $pwr='' $nuc='' $4=''
   $macroname = $0 + '(\'region\',$2,$3,$4,0):$reg,$pwr,$nuc'
   exec($macroname)
   $pwr=''

// Set the Region-code for YX CP, $reg2 from the X Region, $reg

   $reg2=''
   if ($reg='HCa') then 
      $reg2='NCa'
   elseif ($reg = 'sHCa') then 
      $reg2='sNCa'
   elseif ($reg = 'HCO') then 
      $reg2='NCO'
   else 
      $reg2='sNCO'
   endif  

// Calculate the $reg2 Parameters of the Probe File from 
// aX52wr ('N15', amide) and aX32wr of $reg ('C13', alpha, carbonyl)
// Calculate aHyx from aH90 of $reg ('C13', alpha, carbonyl)
// Value of "$srate" is already built into the $aX52wr and $aX32wr

   probeparamgroup('getlocal','HNai','N15','','aX52wr'):$aX52wr
   probeparamgroup('setlocal',$reg2,'N15','YX','ch','fr',
                                               'sh','t',
                                               'to','obs',
                                               'fr','dec2',
                                               'aY',$aX52wr,
                                               'b',0.05*$aX52wr,
                                               'd',0.05*$aX52wr,
                                               't',3000.0,
                                               'of',0.0)
   probeparamgroup('getlocal',$reg,'C13','','aX32wr'):$aX32wr
   probeparamgroup('setlocal',$reg2,$nuc,'YX','aX',$aX32wr)
   probeparamgroup('getlocal','hp'+$reg,'H1','','aH90'):$aH90
   probeparamgroup('setlocal',$reg2,'H1','','aHyx',$aH90)
endif

// Initialize CP Parameters in the Probe File

if (($#>0) and ($1='init')) then

// Set AhYX

   AhYX

// Set the X Region

   $reg='' $pwr='' $nuc='' $4=''
   $macroname = $0 + '(\'region\',$2,$3,$4,0):$reg,$pwr,$nuc'
   exec($macroname)
   $pwr=''

// Set the Region-code for YX CP, $reg2 from the X Region, $reg

   $reg2=''
   if ($reg='HCa') then 
      $reg2='NCa'
   elseif ($reg = 'sHCa') then 
      $reg2='sNCa'
   elseif ($reg = 'HCO') then 
      $reg2='NCO'
   else 
      $reg2='sNCO'
   endif 

// Set $reg2 Parameters of the Probe File from YX CP and Spinrate

   probeparamgroup('setlocal',$reg2,'N15','','srateYX',0.0)
   probeparamgroup('set',$reg2,'N15','YX','ch','sh','to','fr',
                                         'aY','b','d','t','of')
   probeparamgroup('set',$reg2,$nuc,'YX','aX')
   probeparamgroup('set',$reg2,'H1','','aHyx')
endif

// Set YX CP Parameters From the Probe File

if (($#>0) and ($1='set')) then 

// Set AhYX

   AhYX

// Set the X Region

   $reg='' $pwr='' $nuc='' $4=''
   $macroname = $0 + '(\'region\',$2,$3,$4,0):$reg,$pwr,$nuc'
   exec($macroname)
   $pwr=''

// Set the Region-code for YX CP, $reg2 from the X Region, $reg

   $reg2=''
   if ($reg='HCa') then 
      $reg2='NCa'
   elseif ($reg = 'sHCa') then 
      $reg2='sNCa'
   elseif ($reg = 'HCO') then 
      $reg2='NCO'
   else 
      $reg2='sNCO'
   endif 

// Determine the Spinrate if SPspinrateon='y', Otherwise do Nothing.

   exists('SPspinrateon','parameter','global'):$e
   if ($e<0.5) then 
      create('SPspinrateon','string','global')
      SPspinrateon='n'
   endif
   if (SPspinrateon='y') then 
      $macronamespin = $0 + '(\'spinrate\'):$srate'
      exec($macronamespin)
   endif

// Set $reg2 Parameters of the Probe File from YX CP with Spinrate

   probeparamgroup('getlocal',$reg2,'N15','','srateYX'):$srate
   if (((srate < 0.995*$srate) or (srate > 1.005*$srate)) and (SPspinrateon='y')) then       
      write('error','Warning: A New Value of srateYX=%f has Been Set in the Probe File',srate)
   endif
   probeparamgroup('setlocal',$reg2,'N15','','srateYX',srate)

   probeparamgroup('set',$reg2,'N15','YX','ch','sh','to','fr',
                                         'aY','b','d','t','of') 
   probeparamgroup('set',$reg2,$nuc,'YX','aX')
   probeparamgroup('set',$reg2,'H1','','aHyx')
endif

// Get YX CP Parameters from the Probe File

if (($#>0) and ($1='get')) then

// Set AhYX

   AhYX

// Set the X Region

   $reg='' $pwr='' $nuc='' $4=''
   $macroname = $0 + '(\'region\',$2,$3,$4,0):$reg,$pwr,$nuc'
   exec($macroname)
   $pwr=''

// Set the Region-code for YX CP from the X Region

   $reg2=''
   if ($reg='HCa') then 
      $reg2='NCa'
   elseif ($reg = 'sHCa') then 
      $reg2='sNCa'
   elseif ($reg = 'HCO') then 
      $reg2='NCO'
   else 
      $reg2='sNCO'
   endif 

// Get the Spinrate

   exists('SPspinrateon','parameter','global'):$e
   if ($e<0.5) then 
      create('SPspinrateon','string','global')
      SPspinrateon='n'
   endif
   if (SPspinrateon='y') then 
      $macronamespin = $0 + '(\'spinrate\'):$srate'
      exec($macronamespin)
   else 
      probeparamgroup('getlocal',$reg,'H1','','srateHX'):srate
   endif

// Obtain YX CP Parameters from $reg2 of the Probe File and spinrate

   probeparamgroup('getlocal',$reg2,'N15','','srateYX'):$srate
   if (((srate < 0.995*$srate) or (srate > 1.005*$srate)) and (SPspinrateon='y')) then       
      write('error','Warning: Warning: $srate=%f Hz and srateYX=%f in Probe File Hz Disagree\n',srate,$srate)
   endif

   probeparamgroup('get',$reg2,'N15','YX','ch','sh','to','fr',
                                         'aY','b','d','t','of') 
   probeparamgroup('get',$reg2,$nuc,'YX','aX')
   probeparamgroup('get',$reg2,'H1','','aHyx')
endif

//-------------------
// The Region Macro
//-------------------

if (($# > 0) and ($1 = 'region')) then
   $nuc=$2
   $reg=''
   $pwr=$4
   if (($# > 4) and ($5 = 1)) then 
      if ($2='C13') then 
         tn='C13' dn='H1' dn2='N15' dn3=''   
         wc = 200 sc =10
         ad=4.0 rd=4.0 ddrtc=8.0 rp=0 lp=0 d1=2.0 nt=1 ss=2 bs=4
         sw=100000.0 at=0.01 fn=16.0*np sb=at/2.0 sb='y' sbs='n' lb=50 gain=30
         ofHX=0.0
         if ($3='alpha') then
            d1=2.0 nt=1
            setref setoffset('C13',63):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=38p wp=50p
            $reg='HCa'
         elseif ($3='salpha') then
            d1=2.0 nt=1
            setref setoffset('C13',175):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=38p wp=50p
            $reg='sHCa'
         elseif ($3='carbonyl') then 
            d1=2.0 nt=1
            setref setoffset('C13',175):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=165p wp=20p
            $reg='HCO'
         elseif ($3='scarbonyl') then 
            d1=2.0 nt=1
            setref setoffset('C13',63):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=165p wp=20p
            $reg='sHCO'
         elseif ($3='adam') then 
            d1=2.0 nt=1.0
            setref setoffset('C13',33.3):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=28p wp=20p
            $reg='HC'
         endif
      elseif ($2='N15') then 
         tn='N15' dn='H1' dn2='C13'  dn3=''   
         wc = 200 sc =10
         ad=16.0 rd=4.0 ddrtc=20.0 rp=0 lp=0 ss=2 bs=4
         sw=100000.0 at=0.01 fn=16.0*np sb=at/2.0 sb='y' sbs='n' lb=0  lb='n' gain=30  
         ofHX=0.0
         if ($3='amide') then
            d1=2.0 nt=1
            setref setoffset('N15',115):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=90p wp=50p
            $reg='HNai'
         elseif ($3='amine') then
            d1=2.0 nt=1 
            setref setoffset('N15',35):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=15p wp=40p
            $reg='HNam'
         elseif ($3='amino') then 
            d1=2.0 nt=1
            setref setoffset('N15',35):$tof tof=0.1*trunc($tof/0.1 + 0.5) sp=15p wp=40p
            $reg='HN'
         endif         
      endif
      probeparamgroup('setlocal',$reg,$nuc,'','ofX90',tof)
   else
      if ($2='C13') then 
         if ($3='alpha') then
            $reg='HCa'
         elseif ($3='salpha') then
            $reg='sHCa'
         elseif ($3='carbonyl') then 
            $reg='HCO'
         elseif ($3='scarbonyl') then 
            $reg='sHCO'
         elseif ($3='adam') then 
            $reg='HC'
         endif
      elseif ($2='N15') then 
         if ($3='amide') then
            $reg='HNai'
         elseif ($3='amine') then 
            $reg='HNam'
         elseif ($3='amino') then 
            $reg='HN'
         endif
      endif
   endif
   return($reg,$pwr,$nuc)
endif

//---------------------
// The Spin Rate Macro
//---------------------

if (($# > 0) and ($1 = 'spinrate')) then

// Get the Current Spin Rate with an Average of 10

   banner('Measuring Spinning Rate and Stability')
   $count=1 srate=0
   repeat
      $index=''
      if ($count<10) then
         format($count,1,0):$index
      else
         format($count,2,0):$index
      endif
      $dest=curexp+'/stat'+$index
      shell('showstat > ',$dest):$dummy
 
      lookup('file',$dest,'currently', 'at:','read'):$masrate,$ret 
      if $ret then
         format($masrate,5,0):srate[$count]
      endif
//    echo($masrate)
      shell('sleep 1')
      $count=$count+1
   until ($count>10)

   averag(srate[1],srate[2],srate[3],srate[4],srate[5],srate[6],
       srate[7],srate[8],srate[9],srate[10]):$avgsrate,$dev

   $avgsrate = trunc($avgsrate + 0.5)  

//Check the Spin Rate 

   if ($avgsrate<500) then 
      $message='Warning: MAS Spin Rate is Less Than 500 Hz - Set srate=0.0'
      banner($message)
      srate = 0.0
   else
      srate = $avgsrate
   endif 

// Remove Files Generated by the Spin Rate Measurement

   $count=1
   repeat
      $index=''
      if ($count<10) then
         format($count,1,0):$index
      else
         format($count,2,0):$index
      endif
      $dest=curexp+'/stat'+$index
      rm($dest):$dummy
      $count=$count+1
   until ($count>10)
   return(srate)
endif 
