!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
symbol pause continue
!
destroy all
clear alpha
!
if .not.exist(subscan_dir) then
   define character subscan_dir*256 /global
endif
define integer ndat nsol
define double fmt_perror[4] fmt_dperror[4]
define real perror dperror tpeak offset slope iwidth mwidth
define real ithrow mthrow iampl mampl maratio mwratio
define real nsdev psdev osdev ssdev wsdev tsdev arsdev wrsdev
let ndat   50          ! Number of points
let nsol  100          ! Number of solution points
let tpeak   2          ! Ideal peak temperature
let iwidth  5          ! Ideal width
let ithrow 20          ! Ideal throw
let iampl  ithrow/2
let nsdev  tpeak/5     ! Noise rms (SNR=5)
let psdev  9           ! Pointing error rms
let osdev  0.05*tpeak  ! Base offset rms
let ssdev  0.05*tpeak  ! Base slope rms
let wsdev  iwidth/10   ! Line width rms
let tsdev  ithrow/10   ! Beam throw rms
let arsdev tpeak/20    ! Area ratio rms
let wrsdev iwidth/20   ! Width ratio rms
!
point /init dual ndat nsol
define real x_isub[ndat]
!
let fmt_perror  0.0
let fmt_dperror 0.0
!
for isub 1 to 4
   pause "Type C to fake next subscan end"
   !
   let perror        noise(psdev) ! Random pointing error
   let offset        noise(osdev) ! Random offset
   let slope         noise(ssdev) ! Random slope
   let mwidth iwidth+noise(wsdev) ! Measured width = True width + Random value
   let mthrow ithrow+noise(tsdev) ! Measured throw = True throw + Random value
   let mampl  0.5*mthrow
   let maratio -1+noise(arsdev)
   let mwratio  1+noise(wrsdev)
   let x_isub[i] i-ndat/2             ! Ideal x axis
   let pcross%sub'isub'%dat%x x_isub  ! Measured x axis
   let pcross%sub'isub'%dat%y tpeak*exp(-((x_isub-perror+mampl)*2*sqrt(log(2))/mwidth)^2) ! Gaussian y axis
   let pcross%sub'isub'%dat%y pcross%sub'isub'%dat%y+maratio*tpeak*exp(-((x_isub-perror-mampl)*2*sqrt(log(2))/mwidth/mwratio)^2) ! Gaussian y axis
   let pcross%sub'isub'%dat%y pcross%sub'isub'%dat%y+(offset+slope*x_isub) ! Gaussian + base y axis
   let pcross%sub'isub'%dat%w 1/nsdev^2
   for idat 1 to ndat
      let pcross%sub'isub'%dat%y[idat] pcross%sub'isub'%dat%y[idat]+noise(nsdev) ! Additional thermal noise
   next
   !
   let pcross%sub'isub'%fun%flag 2.
   point /solve iwidth ithrow
   !
   dperror = (pcross%sub'isub'%fun%par2%value+0.5*pcross%sub'isub'%fun%par5%value)-perror
   let fmt_perror[isub]  nint(100*perror)/100
   let fmt_dperror[isub] nint(100*dperror)/100
   !
   greg1\set expand 0.8
   for jsub 1 to 4
      @ telcal-let.telcal subscan_dir pcross%sub'jsub'%dir
      @ window_xy 2 2 'jsub'
      if (subscan_dir.eq."+LAM".or.subscan_dir.eq."-LAM") then
         draw text 1.2 2 "Ideal \gDAZ = "'fmt_perror[jsub]'"\r` ("'abs(fmt_dperror[jsub])'"\r`)" 6 0 /char 7
      else if (subscan_dir.eq."+BET".or.subscan_dir.eq."-BET") then
         draw text 1.2 2 "Ideal \gDEL = "'fmt_perror[jsub]'"\r` ("'abs(fmt_dperror[jsub])'"\r`)" 6 0 /char 7
      else
         say "W-POINT, Unknown scanning direction "'subscan_dir'
      endif
   next jsub
   greg1\set expand 1
   !
   if exist(ksub) then
      let ksub ksub+1
      let dperrors[ksub] dperror
      let perrors[ksub]  perror
      let mareas[ksub]   0.5*tpeak*mwidth*sqrt(pi/log(2.0))
      let mwidths[ksub]  mwidth
      let mthrows[ksub]  mthrow
      let maratios[ksub] maratio
      let mwratios[ksub] mwratio
      let offsets[ksub]  offset
      let slopes[ksub]   slope
   endif
   !
   say " ------"
   say " True values:"
   say " "
   say " AREA1               "'nint(0.5*tpeak*mwidth*sqrt(pi/log(2.0))*1000)/1000'
   say " POSITION1           "'nint(perror*1000)/1000'
   say " WIDTH1              "'nint(mwidth*1000)/1000'
   say " AREA2/AREA1         "'nint(maratio*1000)/1000'
   say " POSITION2-POSITION1 "'nint(mthrow*1000)/1000'
   say " WIDTH2/WIDTH1       "'nint(mwratio*1000)/1000'
   say " OFFSET              "'nint(offset*1000)/1000'
   say " SLOPE               "'nint(slope*1000)/1000'
next
!
delete /var subscan_dir
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
