
exists('probe','parameter','global'):$probe
if ($probe<0.5) then 
   write('error','Error: Probe parameter does not exist')
   abort
else
   if (probe='') then
      write('error','Error: Probe parameter not set')
      abort
   endif
endif

$probedir=''
exists(userdir+'/probes/'+ probe,'file'):$e
if ($e > 0.5) then
   $probedir = userdir+'/probes/'+ probe
else 
   exists(systemdir+'/probes/'+ probe,'file'):$e1
   if $e1 > 0.50 then  
      $probedir = systemdir+'/probes/'+ probe
   else
      write('error','Error: Probe directory %s not found',probe)
      abort
   endif
endif

$probefile = $probedir + '/' + probe
exists($probefile,'file'):$e2
if ($e2<0.5) then
   write('error', 'Probefile is missing from probe directory')
   abort
endif

$probefile = $probedir + '/' + probe
exists($probefile,'file'):$e2
if ($e2<0.5) then
   write('error', 'Probefile is missing from probe directory')
   abort
endif

"******** get the appropriate nucleus ********"

$obsnuc = ''
$decnuc = ''
$dec2nuc = ''
$dec3nuc = ''
$dec4nuc = ''
$channel = ''

if ($# > 0) then
   $channel = $1
endif

exists('tn','parameter'):$tn
if (($tn > 0.5) and ($channel = 'tn')) then 
   $obsnuc = tn
endif

exists('dn','parameter'):$dn
if (($dn > 0.5) and ($channel = 'dn')) then 
   $decnuc = dn
endif

exists('dn2','parameter'):$dn2
if (($dn2 > 0.5) and ($channel = 'dn2')) then 
   $dec2nuc = dn2
endif

exists('dn3','parameter'):$dn3
if (($dn3 > 0.5) and ($channel = 'dn3')) then
   $dec3nuc = dn3
endif

exists('dn4','parameter'):$dn4
if (($dn4 > 0.5) and ($channel = 'dn4')) then 
   $dec4nuc = dn4
endif

"******** set values for the observe nucleus ********"

if ($obsnuc<>'') then 

   $obslabel = ''
   nm1('tn'):$obsval
   format($obsval,1,0):$obslabel

   $returnstring=''
   lookup('file',$probefile)
   lookup('seek',$obsnuc +':','skip',1,'read'):$returnstring
   if ($returnstring = '') then 
      addnucleus($obsnuc)
   endif
   lookup('file',$probefile)
   lookup('seek',$obsnuc +':','skip',1,'read'):$returnstring
   length($obsnuc):$nuclength 
   substr($returnstring,1,$nuclength):$returnstring
   if ($returnstring = $obsnuc) then

      $tofval='' format(tof,10,1):$tofval
      lookup('file',$probefile)   
      lookup('count',$obsnuc +'ofC' + $obslabel):$e
      if ($e > 0.0) then 
         setparams('ofC' + $obslabel,$tofval,$obsnuc)
      else 
         addparams('ofC' + $obslabel,$tofval,$obsnuc)
      endif

      $tpwrval='' format(tpwr,2,0):$tpwrval
      lookup('file',$probefile)
      lookup('count',$obsnuc +'pwrC' + $obslabel):$e
      if ($e > 0.0) then 
         setparams('pwrC' + $obslabel,$tpwrval,$obsnuc)
      else 
         addparams('pwrC' + $obslabel,$tpwrval,$obsnuc)
      endif

      $aX90val='' format(aX90,4,0):$aX90val
      lookup('file',$probefile)
      lookup('count',$obsnuc +'a90C' + $obslabel):$e
      if ($e > 0.0) then 
         setparams('a90C' + $obslabel,$aX90val,$obsnuc)
      else 
         addparams('a90C' + $obslabel,$aX90val,$obsnuc)
      endif  

      $pwX90val='' format(pwX90,11,4):$pwX90val
      lookup('file',$probefile)
      lookup('count',$obsnuc +'pw90C' + $obslabel):$e
      if ($e > 0.0) then 
         setparams('pw90C' + $obslabel,$pwX90val,$obsnuc)
      else 
         addparams('pw90C' + $obslabel,$pwX90val,$obsnuc)
      endif 
      write('line3','"%s" calibrations have been set in probe file: "%s"\n',$obsnuc,probe)    
   else
      write('error','Error: Nucleus "%s" is not present in probe file: "%s"',$obsnuc,probe)
   endif 
endif

"******** set values for the decoupler nucleus ********"

if ($decnuc<>'') then 

   $declabel = ''
   nm1('dn'):$decval
   format($decval,1,0):$declabel
   $returnstring=''
   lookup('file',$probefile)
   lookup('seek',$decnuc +':','skip',1,'read'):$returnstring
   if ($returnstring = '') then 
      addnucleus($decnuc)
   endif
   lookup('file',$probefile)
   lookup('seek',$decnuc +':','skip',1,'read'):$returnstring
   length($decnuc):$nuclength 
   substr($returnstring,1,$nuclength):$returnstring
   if ($returnstring = $decnuc) then

      $dofval='' format(dof,10,1):$dofval
      lookup('file',$probefile)
      lookup('count',$decnuc +'ofC' + $declabel):$e
      if ($e > 0.0) then 
         setparams('ofC' + $declabel,$dofval,$decnuc)
      else 
         addparams('ofC' + $declabel,$dofval,$decnuc)
      endif

      $dpwrval='' format(dpwr,2,0):$dpwrval
      lookup('file',$probefile)
      lookup('count',$decnuc +'pwrC' + $declabel):$e
      if ($e > 0.0) then 
         setparams('pwrC' + $declabel,$dpwrval,$decnuc)
      else 
         addparams('pwrC' + $declabel,$dpwrval,$decnuc)
      endif

      $aH90val='' format(aY90,4,0):$aH90val
      lookup('file',$probefile)
      lookup('count',$decnuc +'a90C' + $declabel):$e
      if ($e > 0.0) then 
         setparams('a90C' + $declabel,$aH90val,$decnuc)
      else 
         addparams('a90C' + $declabel,$aH90val,$decnuc)
      endif  

      $pwH90val='' format(pwY90,11,4):$pwH90val
      lookup('file',$probefile)
      lookup('count',$decnuc + 'pw90C' + $declabel):$e
      if ($e > 0.0) then 
         setparams('pw90C' + $declabel,$pwH90val,$decnuc)
      else 
         addparams('pw90C' + $declabel,$pwH90val,$decnuc)
      endif 
      write('line3','"%s" calibrations have been set in probe file: "%s"\n',$decnuc,probe) 
   else
      write('error','Error: Nucleus "%s" is not present in probe file: "%s"',$decnuc,probe)
   endif
endif

"******** set values for the decoupler2 nucleus ********"

if ($dec2nuc<>'') then 

   $dec2label = ''
   nm1('dn2'):$dec2val
   format($dec2val,1,0):$dec2label
   $returnstring=''
   lookup('file',$probefile)
   lookup('seek',$dec2nuc +':','skip',1,'read'):$returnstring
   if ($returnstring = '') then 
      addnucleus($dec2nuc)
   endif
   lookup('file',$probefile)
   lookup('seek',$dec2nuc +':','skip',1,'read'):$returnstring
   length($dec2nuc):$nuclength 
   substr($returnstring,1,$nuclength):$returnstring
   if ($returnstring = $dec2nuc) then 

      $dof2val='' format(dof2,10,1):$dof2val
      lookup('file',$probefile)
      lookup('count',$dec2nuc +'ofC' + $dec2label):$e
      if ($e > 0.0) then 
         setparams('ofC' + $dec2label,$dof2val,$dec2nuc)
      else 
         addparams('ofC' + $dec2label,$dof2val,$dec2nuc)
      endif

      $dpwr2val='' format(dpwr2,2,0):$dpwr2val
      lookup('file',$probefile)
      lookup('count',$dec2nuc +'pwrC' + $dec2label):$e
      if ($e > 0.0) then 
         setparams('pwrC' + $dec2label,$dpwr2val,$dec2nuc)
      else 
         addparams('pwrC' + $dec2label,$dpwr2val,$dec2nuc)
      endif

      $aY90val='' format(aH90,4,0):$aY90val
      lookup('file',$probefile)
      lookup('count',$dec2nuc +'a90C' + $dec2label):$e
      if ($e > 0.0) then 
         setparams('a90C' + $dec2label,$aY90val,$dec2nuc)
      else 
         addparams('a90C' + $dec2label,$aY90val,$dec2nuc)
      endif  

      $pwY90val='' format(pwH90,11,4):$pwY90val
      lookup('file',$probefile)
      lookup('count',$dec2nuc +'pw90C' + $dec2label):$e
      if ($e > 0.0) then 
         setparams('pw90C' + $dec2label,$pwY90val,$dec2nuc)
      else 
         addparams('pw90C' + $dec2label,$pwY90val,$dec2nuc)
      endif 
      write('line3','"%s" calibrations have been set in probe file: "%s"\n',$dec2nuc,probe)
   else
      write('error','Error: Nucleus "%s" is not present in probe file: "%s"',$dec2nuc,probe)
   endif
endif

"******** set values for the decoupler3 nucleus ********"

if ($dec3nuc<>'') then

   $dec3label = ''
   nm1('dn3'):$dec3val
   format($dec3val,1,0):$dec3label 
   $returnstring=''
   lookup('file',$probefile)
   lookup('seek',$dec3nuc +':','skip',1,'read'):$returnstring
   if ($returnstring = '') then 
      addnucleus($dec3nuc)
   endif
   lookup('file',$probefile)
   lookup('seek',$dec3nuc +':','skip',1,'read'):$returnstring
   length($dec3nuc):$nuclength 
   substr($returnstring,1,$nuclength):$returnstring
   if ($returnstring = $dec3nuc) then 

      $dof3val='' format(dof3,10,1):$dof3val
      lookup('file',$probefile)
      lookup('count',$dec3nuc +'ofC' + $dec3label):$e
      if ($e > 0.0) then 
         setparams('ofC' + $dec3label,$dof3val,$dec3nuc)
      else 
         addparams('ofC' + $dec3label,$dof3val,$dec3nuc)
      endif

      $dpwr3val='' format(dpwr3,2,0):$dpwr3val
      lookup('file',$probefile)
      lookup('count',$dec3nuc +'pwrC' + $dec3label):$e
      if ($e > 0.0) then 
         setparams('pwrC' + $dec3label,$dpwr3val,$dec3nuc)
      else 
         addparams('pwrC' + $dec3label,$dpwr3val,$dec3nuc)
      endif

      $aZ90val='' format(aZ90,4,0):$aZ90val
      lookup('file',$probefile)
      lookup('count',$dec3nuc +'a90C' + $dec3label):$e    
      if ($e > 0.0) then 
         setparams('a90C' + $dec3label,$aZ90val,$dec3nuc)
      else 
         addparams('a90C' + $dec3label,$aZ90val,$dec3nuc)
      endif  

      $pwZ90val='' format(pwZ90,11,4):$pwZ90val
      lookup('file',$probefile)
      lookup('count',$dec3nuc +'pw90C' + $dec3label):$e
      if ($e > 0.0) then 
         setparams('pw90C' + $dec3label,$pwZ90val,$dec3nuc)
      else 
         addparams('pw90C' + $dec3label,$pwZ90val,$dec3nuc)
      endif 
      write('line3','"%s" calibrations have been set in probe file: "%s"\n',$dec3nuc,probe)
   else
      write('error','Error: Nucleus "%s" is not present in probe file: "%s"',$dec3nuc,probe)
   endif
endif

"******** set values for the decoupler4 nucleus ********"

if ($dec4nuc<>'') then

   $dec4label = ''
   nm1('dn4'):$dec4val
   format($dec4val,1,0):$dec4label 
   $returnstring=''
   lookup('file',$probefile)
   lookup('seek',$dec4nuc +':','skip',1,'read'):$returnstring
   if ($returnstring = '') then 
      addnucleus($dec4nuc)
   endif
   lookup('file',$probefile)
   lookup('seek',$dec4nuc +':','skip',1,'read'):$returnstring
   length($dec4nuc):$nuclength 
   substr($returnstring,1,$nuclength):$returnstring
   if ($returnstring = $dec4nuc) then 

      $dof3val='' format(dof3,10,1):$dof3val
      lookup('file',$probefile)
      lookup('count',$dec4nuc +'ofC' + $dec4label):$e
      if ($e > 0.0) then 
         setparams('ofC' + $dec4label,$dof3val,$dec4nuc)
      else 
         addparams('ofC' + $dec4label,$dof3val,$dec4nuc)
      endif

      $dpwr3val='' format(dpwr3,2,0):$dpwr3val
      lookup('file',$probefile)
      lookup('count',$dec4nuc +'pwrC' + $dec4label):$e
      if ($e > 0.0) then 
         setparams('pwrC' + $dec4label,$dpwr3val,$dec4nuc)
      else 
         addparams('pwrC' + $dec4label,$dpwr3val,$dec4nuc)
      endif

      $aZ90val='' format(aZ90,4,0):$aZ90val
      lookup('file',$probefile)
      lookup('count',$dec4nuc +'a90C' + $dec4label):$e    
      if ($e > 0.0) then 
         setparams('a90C' + $dec4label,$aZ90val,$dec4nuc)
      else 
         addparams('a90C' + $dec4label,$aZ90val,$dec4nuc)
      endif  

      $pwZ90val='' format(pwZ90,11,4):$pwZ90val
      lookup('file',$probefile)
      lookup('count',$dec4nuc +'pw90C' + $dec4label):$e
      if ($e > 0.0) then 
         setparams('pw90C' + $dec4label,$pwZ90val,$dec4nuc)
      else 
         addparams('pw90C' + $dec4label,$pwZ90val,$dec4nuc)
      endif 
      write('line3','%s calibrations have been set in probe file: %s\n',$dec4nuc,probe)
   else
      write('error','Error: Nucleus "%s" is not present in probe file: "%s"',$dec4nuc,probe)
   endif
endif
