!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! J.Pety 2007
! S.Guilloteau Dec 2014
!   add plotting of flagged visibilities,
!   of specified antenna/date combination and
!   mechanism to be used by UV_FLAG
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure uvshow-head
  !
  if (do_header) then
     let aspect_ratio no
     @ header_position
     @ window_xy 1 1 1
     box n n n
     !
     define character text*32 mymark*8
     !
     set expand 1.0
     let text 'header_name_type'
     dra text .5 -1 'text' 6 /box 7
     !
     set expand 0.8
     let text "\\iSource:\\i "'&1%source'
     dra text .5 -2 'text' 6 /box 7
     !
     let text "\\iLine:\\i "'&1%line'
     dra text .5 -3 'text' 6 /box 7
     !
     let text "\\iFrequency:\\i "'1d-6*nint(&1%restfre*1000)'" GHz"
     dra text .5 -4 'text' 6 /box 7
     !
     if (first.eq.0).and.(last.eq.0) then
       let text "\\iAll Channels"
     else
       let text "\\iChannels:\\I "'first'" to "'last'
     endif
     dra text .5 -5 'text' 6 /box 7
     !
     let text 'ytype'" \\ivs.\\i "'xtype'
     dra text .5 -6 'text' 6 /box 7
     !
     if (mark.ne."frequency".and.mark.ne."channel".and.mark.ne."none") then
       let mymark "VELOCITY"
     else
       let mymark 'mark' /upper
     endif
     let text "\\iBox marking:\\i "'mymark'
     dra text .5 -7 'text' 6 /box 7
     !
     if (uvfit%mod.ne." ") then
       let text "\\iFitted model:\\i "'uvfit%mod'
       dra text .5 -8 'text' 6 /box 7
     endif
     !
     sic date
     draw text 0.0 2.0 "'sys_info'" 5 /box 2
     draw text 0.0 1.0 "'sys_date'" 5 /box 2
  endif
  !
end procedure uvshow-head
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure uvshow-sub
  !
  destroy all
  !
  ! Define useful variables
  define double xmark
  define real uu vv l1 l2 l3 l4 dl uvm
  define integer imark ifirst ilast nelt ktrack
  define integer iimag ireal iweig nchan nvisi nbox
  define integer true_first true_last true_step
  define character xtext*30 ytext*30 atext*30 atype*8 cmark*12
  define integer ne nd
  !
  ! Define marker
  symbol mymarker 'uvshow%marker'
  set marker 'mymarker'
  delete /symbol mymarker
  !
  ! Ensure case-independant behavior
  let xtype 'xtype' /lower
  let ytype 'ytype' /lower
  let mark  'mark'  /lower
  !
  ktrack = 0
  uvshow%antenna = 0
  if pro%narg.eq.3 then
    ktrack = 'pro%arg[3]'
    uvshow%antenna = 'pro%arg[2]'
  else if pro%narg.eq.2 then
    uvshow%antenna = 'pro%arg[2]'
  endif
  !
  ! Do we use the buffer or do we start from the file?
  if ("&1none".eq."none") then
     ! Transfer uv table in mapping buffer when needed
     read uv 'name'
     load uv
     ! Transpose and sort by increasing time/base order
     ! This is required to plot different colors per date
     uv_sort time
  endif
  !
  ! Find problem size and define useful variables
  let nchan uv_nchan
  let nvisi uv_dim[2]
  define real uvd[nvisi,2] uvn[nvisi]
  !
  ! Open fit results when asked by user
  define character filename*256
  if exist(uvshow%fit) then
     if (uvshow%fit) then
        let filename 'name'"-res.uvt"
        if (file(filename)) then
           message i uvshow "Define uvf & uvr variable"
           read uv 'name'"-res"
           load uv
           uv_sort time
           define real uvr /like uvs
           let uvr uvs
           !
           read uv 'name'
           load uv
           uv_sort time
           !
           define real uvf /like uvd
        else
           message e uvshow ""'filename'" file (needed by the uvshow%fit option) does not exist"
           return base
        endif
     endif
  endif
  !
  ! Plot header
  @ uvshow-head w
  !
  ! Definition of first and last plotted channels.
  if (first.eq.0) then
     let true_first 1
  else
     let true_first min(first,nchan)
  endif
  if (last.eq.0) then
     let true_last nchan
  else
     let true_last max(1,last)
  endif
  if (true_first.gt.true_last) then
     let true_step -1
  else
     let true_step +1
  endif
  let nbox abs(true_last-true_first)+1
  !
  ! Definition of box characteristics.
  let aspect_ratio no ! No aspect ratio
  let fixed_trc yes   ! Top right corner is fixed
  let inter 0.0 0.0   ! No interspace between individual boxes (unit: % of box sizes)
  @ plot_position     ! Definition of total available plotting place
  @ find_nx_ny 'nbox' ! Compute number of boxes in the x and y direction
  @ find_size_margin 'nx_box' 'ny_box' ! Compute individual box size and margin needed to keep aspect
  if (expand.eq.0) then
     greg1\set expand min(6.0/(nx_box+ny_box),1.) ! Automatic definition character size
  else
     greg1\set expand 'expand' ! User defined character size
  endif
  !
  ! First pass to get limits when needed
  if (box_limits.eq." ") then
     message i uvshow "Finding limits"
     for ichan true_first to true_last by true_step
        let ireal 5+3*ichan
        let iimag 6+3*ichan
        let iweig 7+3*ichan
        for iaxis 1 to 2
           if (iaxis.eq.1) then
              let atype 'xtype'
           else if (iaxis.eq.2) then
              let atype 'ytype'
           endif
           if (atype.eq."u") then
              let uvd[iaxis] uvs[1]
           else if (atype.eq."v") then
              let uvd[iaxis] uvs[2]
           else if (atype.eq."angle") then
              let uvd[iaxis] 180/pi*atan2(uvs[2],uvs[1])
           else if (atype.eq."radius") then
              let uvd[iaxis] sqrt(uvs[1]**2+uvs[2]**2)
           else if (atype.eq."time") then
              let uvd[iaxis] 24.0d0*(uvs[4]-uvs[1,4])+uvs[5]/3600.d0
           else if (atype.eq."date") then
              let uvd[iaxis] uvs[4]-uvs[1,4]
           else if (atype.eq."scan") then
              let uvd[iaxis] uvs[3]
           else if (atype.eq."number") then
              let uvn[i] i
              let uvd[iaxis] uvn
           else if (atype.eq."amp") then
              let uvd[iaxis] sqrt(uvs[ireal]**2+uvs[iimag]**2)
           else if (atype.eq."phase") then
              let uvd[iaxis] 180/pi*atan2(uvs[iimag],uvs[ireal])
           else if (atype.eq."real") then
              let uvd[iaxis] uvs[ireal]
           else if (atype.eq."imag") then
              let uvd[iaxis] uvs[iimag]
           else if (atype.eq."weight") then
              let uvd[iaxis] uvs[iweig]
           else if (atype.eq."iant") then
              let uvd[iaxis] uvs[6]
           else if (atype.eq."jant") then
              let uvd[iaxis] uvs[7]
           else if (atype.eq."base") then
              let uvd[iaxis] 100*uvs[6]+uvs[7]
           else
              message e uvshow "Type not supported " 'atype'
           endif
        next iaxis
        limits /var uvd[1] uvd[2]
        if (ichan.eq.true_first) then
           let l1 user_xmin
           let l2 user_xmax
           let l3 user_ymin
           let l4 user_ymax
        else
           let l1 min(user_xmin,l1)
           let l2 max(user_xmax,l2)
           let l3 min(user_ymin,l3)
           let l4 max(user_ymax,l4)
        endif
     next ichan
     if (l1.ne.l2) then
        let dl 0.025*(l2-l1)
        let l1 l1-dl
        let l2 l2+dl
        if (l3.ne.l4) then
           let dl 0.025*(l4-l3)
           let l3 l3-dl
           let l4 l4+dl
           symbol alimits 'l1'" "'l2'" "'l3'" "'l4'
        else
           symbol alimits 'l1'" "'l2'" -1 1"
        endif
     else
        let dl 0.025*(l4-l3)
        let l3 l3-dl
        let l4 l4+dl
        if (l3.ne.l4) then
           symbol alimits "-1 1 "'l3'" "'l4'
        else
           message e uvshow "Found only blanked data"
           return
        endif
     endif
  else
     symbol alimits 'box_limits'
  endif
  ! Limits are identical for all panels
  if ((xtype.eq."u").and.(ytype.eq."v")) then
     greg1\limits 'alimits' /var uvd[1] uvd[2]
     let uu max(abs(user_xmin),abs(user_xmax))
     let vv max(abs(user_ymin),abs(user_ymax))
     let uu max(uu,vv)
     let vv uu
  else
     greg1\limits 'alimits' /var uvd[1] uvd[2]
  endif
  !
  ! Divide data in track
  define integer ntrack uv_edges[9]
  let uv_edges 0
  !
  if (uvshow%track.or.ktrack.ne.0) then
     define real uv_time[nvisi] uv_dtime[nvisi]
     let uv_time 24.0d0*(uvs[4]-uvs[1,4])+uvs[5]/3600
     let uv_dtime[1:'nvisi-1'] uv_time[1:'nvisi-1']-uv_time[2:nvisi]
     let uv_dtime[nvisi] 0
     let uv_dtime abs(uv_dtime)
     let uv_time 0
     let uv_time 1 /where uv_dtime.gt.uvshow%step
     define real rtrack
     compute rtrack sum uv_time
     let ntrack 1+nint(rtrack)
     message i uvshow "Number of found tracks: "'ntrack'
     if (ntrack.gt.7) then
        let ntrack 1
        let uv_edges[1] 1
        let uv_edges[2] nvisi
     else
        let uv_time[i] i /where uv_time.ne.0
        let uv_edges[1] 1
        let uv_edges['ntrack+1'] nvisi
        for itrack ntrack to 2 by -1
           compute rtrack max uv_time
           let uv_edges[itrack] nint(rtrack)+1
           let uv_time['uv_edges[itrack]-1'] 0
        next itrack
     endif
     delete /var uv_time uv_dtime rtrack
  else
     let ntrack 1
     let uv_edges[1] 1
     let uv_edges[2] nvisi
  endif
  !
  ! Second pass to plot
  for ichan true_first to true_last by true_step
     ! Define current box position
     @ find_window_position 'abs(ichan-true_first+true_step)'
     !
     let ireal 5+3*ichan
     let iimag 6+3*ichan
     let iweig 7+3*ichan
     !
     for iaxis 1 to 2
        if (iaxis.eq.1) then
           let atype 'xtype'
        else if (iaxis.eq.2) then
           let atype 'ytype'
        endif
        if (atype.eq."u") then
           let uvd[iaxis] uvs[1]
           let atext "U [m]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."v") then
           let uvd[iaxis] uvs[2]
           let atext "V [m]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."angle") then
           let uvd[iaxis] 180/pi*atan2(uvs[2],uvs[1])
           let atext "UV position angle [deg]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."radius") then
           let uvd[iaxis] sqrt(uvs[1]**2+uvs[2]**2)
           let atext "UV radius [m]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."time") then
           let uvd[iaxis] 24.0d0*(uvs[4]-uvs[1,4])+uvs[5]/3600.d0
           let atext "Time [hrs]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."date") then
           let uvd[iaxis] uvs[4]-uvs[1,4] ! +UVS[5]/86400.D0
           let atext "Date [day]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."scan") then
           let uvd[iaxis] uvs[3]
           let atext "Scan number"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."number") then
           let uvn[i] i
           let uvd[iaxis] uvn
           let atext "Visibility number"
           if (uvshow%fit) then
              let uvf[iaxis] uvn
           endif
        else if (atype.eq."amp") then
           let uvd[iaxis] sqrt(uvs[ireal]**2+uvs[iimag]**2)
           let atext "Amplitude [Jy]"
           if (uvshow%fit) then
              let uvf[iaxis] sqrt((uvs[ireal]-uvr[ireal])**2+(uvs[iimag]-uvr[iimag])**2)
           endif
        else if (atype.eq."phase") then
           let uvd[iaxis] 180/pi*atan2(uvs[iimag],uvs[ireal])
           let atext "Phase [deg]"
           if (uvshow%fit) then
              let uvf[iaxis] 180/pi*atan2(uvs[iimag]-uvr[iimag],uvs[ireal]-uvr[ireal])
           endif
        else if (atype.eq."real") then
           let uvd[iaxis] uvs[ireal]
           let atext "Real part [Jy]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]-uvr[ireal]
           endif
        else if (atype.eq."imag") then
           let uvd[iaxis] uvs[iimag]
           let atext "Imaginary part [Jy]"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]-uvr[iimag]
           endif
        else if (atype.eq."weight") then
           let uvd[iaxis] uvs[iweig]
           let atext "Relative Weight"
           if (uvshow%fit) then
              let uvf[iaxis] uvd[iaxis]
           endif
        else if (atype.eq."iant") then
          let uvd[iaxis] uvs[6]
          let atext "First Ant."
        else if (atype.eq."jant") then
          let uvd[iaxis] uvs[7]
          let atext "Last Ant."
        else if (atype.eq."base") then
          let uvd[iaxis] 100*uvs[6]+uvs[7]
          let atext "Baseline"
        else
           message e uvshow "Unsupported type: "'atype'
        endif
        if (iaxis.eq.1) then
           let xtext 'atext'
        else if (iaxis.eq.2) then
           let ytext 'atext'
        endif
     next iaxis
     !
     if ktrack.eq.0 then
       for itrack 1 to ntrack
          let ifirst uv_edges['itrack']
          let ilast  uv_edges['itrack+1']
          let nelt ilast-ifirst+1
          if exist(uvx) then
             delete /var uvx uvy uve
          endif
          define real uvx[nelt] uvy[nelt] uve[nelt]
          let uvx uvd[ifirst:ilast,1]
          let uvy uvd[ifirst:ilast,2]
          pen 'itrack-1'
          if ((xtype.eq."u").and.(ytype.eq."v")) then
             greg1\limits uu -uu vv -vv
             greg1\point uvx uvy
             greg1\limits -uu uu -vv vv
             greg1\point uvx uvy
          else
             point uvx uvy
          endif
          ! Weight if not flagged ...
          if (error_bars) then
             if (ytype.eq."amp".or.ytype.eq."real".or.ytype.eq."imag") then
                let uve 1/sqrt(uvs[ifirst:ilast,iweig]*1e6) /where -
                      uvs[ifirst:ilast,iweig].gt.0
                error y uvx uvy uve
             endif
          endif
       next itrack
     endif
     !
     if (uvshow%fit) then
        pen 1
        points uvf[1] uvf[2]
     endif
     !
     ! S.Guilloteau: highlight Antenna of specified date
     if (uvshow%antenna.ne.0) then
        !
        if (exist(uvx)) then
           delete /var uvx uvy
        endif
        if (ktrack.ne.0) then
           pen 0
           if ((xtype.eq."u").and.(ytype.eq."v")) then
              let uu max(-user_xmin,user_xmax)
              let vv -user_ymin
              limits uu -uu vv -vv
              point uvd[1] uvd[2]
              limits -uu uu -vv vv
              point uvd[1] uvd[2]
           else
              point uvd[1] uvd[2]
           endif
           !
           let ifirst uv_edges[ktrack]
           let ilast  uv_edges['ktrack+1']
           let nelt ilast-ifirst+1
           define real uvx[nelt] uvy[nelt]
           let uvx uvd[ifirst:ilast,1] /where uvs[ifirst:ilast,6].eq.uvshow%antenna
           let uvy uvd[ifirst:ilast,2] /where uvs[ifirst:ilast,6].eq.uvshow%antenna
           let uvx uvd[ifirst:ilast,1] /where uvs[ifirst:ilast,7].eq.uvshow%antenna
           let uvy uvd[ifirst:ilast,2] /where uvs[ifirst:ilast,7].eq.uvshow%antenna
        else
           define real uvx[nvisi] uvy[nvisi]
           let uvx uvd[1] /where uvs[6].eq.uvshow%antenna
           let uvy uvd[2] /where uvs[6].eq.uvshow%antenna
           let uvx uvd[1] /where uvs[7].eq.uvshow%antenna
           let uvy uvd[2] /where uvs[7].eq.uvshow%antenna
        endif
        pen 0 /col 1 ! Red flag for the antenna
        if ((xtype.eq."u").and.(ytype.eq."v")) then
           let uu max(-user_xmin,user_xmax)
           let vv -user_ymin
           limits uu -uu vv -vv
           point uvx uvy
           limits -uu uu -vv vv
           point uvx uvy
        else
           point uvx uvy
        endif
        pen 0 /col 0
     endif
     !
     ! Highlight flagged data...
     ! Last pass for flagged data
     compute uvm min uvs[iweig]
     if (uvm.le.0) then
        if (exist(uvx)) then
           delete /var uvx uvy
        endif
        define real uvx[nvisi] uvy[nvisi]
        let uvx uvd[1] /where uvs[iweig].le.0  ! [1] Was uvs[ne] = nd-1 before..
        let uvy uvd[2] /where uvs[iweig].le.0  ! [2] was uvs[nd] before...
        pen 0 /col 10 ! Light grey by default
        if ((xtype.eq."u").and.(ytype.eq."v")) then
           let uu max(-user_xmin,user_xmax)
           let vv -user_ymin
           limits uu -uu vv -vv
           point uvx uvy
           limits -uu uu -vv vv
           point uvx uvy
        else
           point uvx uvy
        endif
        pen 0 /col 0
     endif
     !
     ! This is required for UV_FLAG, which uses the
     ! last 2 extra columns of UVS to handle the coordinates
     ! and compare it to the polygon drawn by the user
     let nd uvs_dim[2]
     let ne nd-1
     let uvs[ne] uvd[1]
     let uvs[nd] uvd[2]
     ! end S.Guilloteau
     !
     if (uvshow%zero) then
        pen 1
        draw relo 'user_xmin' 0 /user
        draw line 'user_xmax' 0 /user /clip
        draw relo 0 'user_ymin' /user
        draw line 0 'user_ymax' /user /clip
     endif
     !
     pen 0
     if (mark.ne."none") then
        let xmark w%veloff+(ichan-w%convert[1,1])*w%velres
        if (mark.eq."frequency") then
           let imark nint((-(xmark-w%veloff)*w%restfre/299792.458+w%restfre)*1e3)
           let xmark 1d-6*imark
        else if (mark.eq."channel") then
           let xmark ichan
        else
           let mark "velocity"
           let imark nint(xmark*10.)
           let xmark 1d-1*imark
        endif
        let cmark 'xmark'
        draw text 0.1 -0.1 'cmark' 3 /box 7
     endif
     !
     box n n
     !
  next ichan
  !
  delete /symbol alimits
  !
  @ window_xy 'nx_box' 'ny_box' 1 1
  box
  label 'xtext' /x
  label 'ytext' /y
  !
  set expand 1.
  !
end procedure uvshow-sub
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure uvshow-loop
  !
!  on error "return base"
  !
  if .not.exist(uvshow%ifield) then
     define integer uvshow%ifield /global
     let uvshow%ifield 1
  else
     let uvshow%ifield max(1,min(uvshow%ifield,setup%int%nf))
  endif
  @ uvshow-sub
!!$  define logical loop
!!$  let loop yes
!!$  for /while loop
!!$     @ uvshow-sub
!!$     greg1\draw relo
!!$     if (cursor_code.eq."E") then
!!$        let loop no
!!$     else if (cursor_code.eq."F") then
!!$        let uvshow%ifield 1
!!$     else if (cursor_code.eq."N") then
!!$        let uvshow%ifield mod(uvshow%ifield,setup%int%nf)+1
!!$     else if (cursor_code.eq."P") then
!!$        let uvshow%ifield mod(uvshow%ifield-2,setup%int%nf)+1
!!$     endif
!!$  next
!!$  let name 'setup%int%name'
  !
end procedure uvshow-loop
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
on error "return base"
!
@ window_tools.greg
@ window_init
!
if ("&1none".eq."none") then
   @ p_setup inter
   if (setup%int%mosaic) then
      @ uvshow-loop
   else
      @ uvshow-sub "" &2 &3
   endif
else
   @ uvshow-sub "&1" &2 &3
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

