// dpsXref -      Obtain a spectrum with direct polarization and no 
//                decoupling and process with standard linebroadening in 
//                av mode. 

//                Argument 2 = 'adam' generates and references a shim dataset
//                with at= .4, d1 = 4.0 and aHspinal = aH90*pwH90/7.0 
//                (35 kHz) in ph mode. 

 //               Argument 3 is the region number with a default of 1. 
//                

//===================
// The SETUP macro
//===================

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

// Read Macro Arguments

   if ($# < 2) then $2 = 'adam' endif 
   if ($# < 3) then $3 = 1 endif

// Set Sequence and Read Modules 
   
   parammodule('setsampleglobals')
   Settancpx
   Onepul
   parammodule('updatesampleglobals')
   parammodule('studydir')
   $ret = 0 $ret2 = 0 $reg = '' $tnuc = '' $dnuc = '' 
   parammodule('setacqregion',$3):$ret,$reg,$tnuc,$dnuc
   parammodule('errorcheck',$ret,$3,'setacqregion')
   $ret2 = 0
   parammodule('setacqdisplay',$3,''):$ret2
   parammodule('errorcheck',$ret2,$3,'setacqdisplay')
   $ret3 = 0
   parammodule('setsample1D',1):$ret3
   parammodule('infocheck',$ret3,1,'setsample1D','sample')

// Decoupler Off

   Hseq = 'spinal'   
   aHspinal = 0.0 phHspinal = 0.0
   aHtppm = 0.0 phHtppm = 0.0

// Initialize Decoupling and reset at and d1

   if ($2 = 'adam') then
      Hseq = 'spinal'   
      aHspinal = aH90*pwH90/7.0 phHspinal = 0.0
      aHtppm = aH90*pwH90/7.0 phHtppm = 0.0
      at = 0.4 d1 = 4.0
   endif

// Obtain Standard X and H2 Parameters from Probe File

   probeparamgroup('getlocal',$reg,$dnuc,'HX','srate'):srate
   probeparamgroup('get','hp'+$reg,$dnuc,'90H','a','pw')
   probeparamgroup('get',$reg,$tnuc,'90X','a','pw')

// Calibrate setddrtc for CP

   exists('ddrpm','parameter'):$e
   if ($e < 0.5) then
      create('ddrpm','string')
      ddrpm='r'
   endif   
   rof2=rd-0.6 alfa=ad

// Set Automatic Linebroadening 

   fn=16*np lb = 2.0*sw/(3.14159*np) 

   if ($2 = 'adam') then 
      sb = 'n' sbs = 'n'
   else 
      sb = at/2.0 sbs = 0.0 sbs='n' lb = 0
   endif

// Linear Prediction

   parlp
   proc = 'ft'

// Set Macro-Specific Processing and Plotting 

   wnt=''
   wexp = 'chempackQ'
   $arg3 = '' format($3,1,0):$arg3
   execprocess = $0 + '(\'process\',\'' + $2 + '\',' + $arg3 + ')'
   execplot = ''
   clear(2)
   write('line3','%s Setup is Complete\n',$0)
   return
endif

//===========================
// Macro-Specific Processing
//===========================

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

// Read Macro Arguments

   if ($# < 2) then $2 = 'adam' endif 
   if ($# < 3) then $3 = 1 endif

// Set the Region

   $ret = 0 $reg = '' $tnuc = '' $dnuc = '' 
   parammodule('setacqlabel',1):$ret,$reg,$tnuc,$dnuc
  
// Auto Phase

   fn=16*np lb = 2.0*sw/(3.14159*np) 
   if ($2 = 'adam') then 
      sb='n' ph wft lp=4 vsadj(100) noislm(1.0)
      aph0:$aphok
      if ($aphok=1) then
         aph0
         peak:$int
         if ($int<-10) then rp=rp+180 endif
      endif
   else 
      sb = at/2.0 sbs=0.0 sbs='n'
      vp=0 av wft vsadj(100) noislm(1.0)
   endif

// Find Peak

   peak:$ht,cr

// Set Reference Mode if Possible

   exists('uselockref','parameter','global'):$e
   if ($e < 0) then 
      write('line3','Error: Reference Software not Enabled, Create uselockref')
      return
   endif
   uselockref='n'
   if ($reg ='HC') then 
      setBaseref(37.77p)
      write('line3','User Base Reference Set from adamantane, 37.77p')
   else 
      write('line3', 'Reference Value not Available\n')
   endif
   write('line3','H1reffrq = %f\n',H1reffrq)
   probeparamgroup('setlocal',$reg,$tnuc,'','baserefX',H1reffrq)

   if ($2 = 'adam') then 
      probeparamgroup('set',$reg,$dnuc,'tppmH','a','pw','ph')
      probeparamgroup('set',$reg,$dnuc,'spinalH','a','pw','ph')
   endif
endif

//==========================
// Macro-Specific Plotting
//==========================

if (($# > 0) and ($1 = 'plot')) then 
   wc = 50 sc = 50
   plot1d
   return  
endif 

//==================================
// Begin AutoCalibration Utilities
//==================================

//--------------------
// The maxamp function
//--------------------

if (($# > 0) and ($1 = 'maxamp')) then
   select(celem) 
   if (celem=1) then 
      vsadj($2) 
   endif
   peak:$ht
   if ($ht>$2) then 
      vsadj($2) 
   endif 
   return    
endif  

//--------------------------
// The  minmaxfit function
//--------------------------

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

   $bestone=$2
   $lim=2
   if ($#>3) then $lim=$4 endif

// Prepare 'analyze.inp'

   $file=curexp+'/analyze.inp'
   exists($file,'file'):$a
   if ($a>0.5) then 
      write('reset',$file) 
   endif

   format((2*$lim+1),2,0):$sizes

   write('file',$file,'AMPLITUDE')
   write('file',$file,'INTENSITY')
   write('file',$file,'%12s%13s%7s%7s\n','1',$sizes,'linear','linear')
   format((2*$lim+1),2,0):$sizes
   write('file',$file,'%8s%5s','NEXT',$sizes)
   write('file',$file,'1')

   $step=$bestone-$lim
   repeat
      select($step)
      peak:$ht
      write('file',$file,'%14.1f%12.2f',$step,$ht)
      $step=$step+1
   until ($step>$bestone+$lim)

// 'poly2 Analysis with 'expfit'

   analyze('expfit','regression','poly2')
   $file=curexp+'/analyze.out'
   lookup('file',$file)
   lookup('seek','NEXT','skip', '2','read','4'):$a0s,$a1s,$a2s
   format($a0s,8,4):$a0
   format($a1s,8,4):$a1
   format($a2s,8,4):$a2
   $inf=-$a1/(2.0*$a2)
   $infval=$a0+$a1*$inf+$a2*$inf*$inf

// Mean Value from 'poly0' 

   analyze('expfit','regression','poly0')
   lookup('file',$file)
   lookup('seek','NEXT','skip', '2','read','4'):$a0s,$a1s,$a2s
   format($a0s,8,4):$a0
   $mean=$a0
//   write('line3','$inf=%f $infval=%f $mean=%f',$inf,$infval,$mean)
//   write('line3','$2 = %f',$2)

// Min ($minmax<0) or Max ($minmax>0) or none ($minmax=0)

   $minmax=0 $ret=0.0
   if (($bestone-$lim)<$inf)and($inf<($bestone+$lim)) then 
      $ret=$inf
      if ($infval<$mean) then 
         $minmax=-1
      else
         $minmax=1
      endif
   endif

//   write('line3','$xmin = %f $ret = %f $xmax = %f $minmax=%f',$bestone-$lim,$ret,$bestone+$lim,$minmax)

// Return the new $bestone and a Fraction After $bestone,
// else Return $bestone=$2 and $remainder=0.0 

   $remainder=0 
   if (($3='min')and($minmax<0)) then 
      $bestone=trunc($ret) 
      $remainder=$ret-$bestone
   endif
   if (($3='max')and($minmax>0)) then 
      $bestone=trunc($ret) 
      $remainder=$ret-$bestone
   endif
   if ($minmax=0) then
      $bestone=$2 
      $remainder=0.0
   endif
//   write('line3','$bestone=%f',$bestone)
   return($bestone,$remainder)
endif 
 

