!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! 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
! S.Guilloteau 2019
!   Adapted for IMAGER
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure uvshow-head
  !
  if (.not.do_header) return
  !
  let aspect_ratio no
  @ header_position
  @ window_xy 1 1 1
  greg1\box n n n
  !
  define character text*32 mymark*8
  !
  greg1\set expand 1.0
  let text 'header_name_type'
  greg1\draw text .5 -1 'text' 6 /box 7
  !
  greg1\set expand 0.8
  let text "\\iSource:\\i "'&1%source'
  greg1\draw text .5 -2 'text' 6 /box 7
  !
  let text "\\iLine:\\i "'&1%line'
  greg1\draw text .5 -3 'text' 6 /box 7
  !
  let text "\\iFrequency:\\i "'1d-6*nint(&1%restfre*1000)'" GHz"
  greg1\draw 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
  greg1\draw text .5 -5 'text' 6 /box 7
  !
  let text 'ytype'" \\ivs.\\i "'xtype'
  greg1\draw 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'
  greg1\draw text .5 -7 'text' 6 /box 7
  !
  if (uvf%mod.ne." ") then
   let text "\\iFitted model:\\i "'uvf%mod'
   greg1\draw text .5 -8 'text' 6 /box 7
  endif
  !
  sic date
  greg1\draw text 0.0 2.0 "'sys_info'" 5 /box 2
  greg1\draw text 0.0 1.0 "'sys_date'" 5 /box 2
end procedure uvshow-head
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure uvshow-sub
  !
  gtvl\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*16 cmark*12
  define integer ne nd
  define logical show_uvfit uvshow_fit
  !
  ! Define marker
  symbol mymarker 'uvshow%marker'
  greg1\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
  !
  ! Find problem size and define useful variables
  let nchan uv%nchan
  let nvisi uv%dim[2]
  define real uvd[nvisi,2] uvn[nvisi] uvdmax uvdmin
  define logi is_first
  !
  ! Open fit results when asked by user, unless MODEL is to be shown...
  let uvshow%fit 'uvshow%fit' /upper
  let uvshow_fit yes
  if uvshow%fit.eq."NO" let uvshow_fit no
  let show_uvfit no
  !
  if (current_uv.ne."MODEL_UV") then
    if (uvshow%fit.ne."NO") then
      define char*12 uv_current
      define integer mvisi
      let uv_current 'current_uv'
      if (uvshow%fit.ne."MODEL") MODEL ! Compute Model unless specified
      calibrate\uv_select UV_MODEL
      calibrate\uv_sort TIME
      define real uvrr /like uvs   ! The sorted model UV values
      let uvrr uvs
      let mvisi size(uvs,1)
      define real uvff[mvisi,2]    ! The values to be displayed
      ! Re-sort the UV data ...
      calibrate\uv_select 'UV_CURRENT'
      calibrate\uv_sort TIME
    endif
  endif
  !
  ! 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
  !
  ! Check parameters and define channel range
  define integer icol
  define character*12 vocab[17] vchan[5]
  let vchan "amp" "phase" "real" "imag" "weight"
  define logical found xfound yfound
  let vocab "u" "v" "radius" "angle" "kwave" "time" "date" "w" -
    "number" "amp" "phase" "real" "imag" "weight" "iant" "jant" "base" ! /resize
  let found yes
  let xfound no
  let yfound no
  for search /in vchan
    if (search.eq.xtype) let xfound yes
    if (search.eq.ytype) let yfound yes
  next
  !
  ! Only plot 1 channel if information is not channel dependent
  if (.not.(xfound.or.yfound)) then
    let true_first (true_first+true_last)/2
    let true_step 1
    let true_last true_first
  endif
  for search /in vocab
    if (search.eq.xtype) let xfound yes
    if (search.eq.ytype) let yfound yes
  next
  if (exist(uv%extra_names)) then
    for search /in uv%extra_names
      if (search.eq.xtype) let xfound yes
      if (search.eq.ytype) let yfound yes
    next
  endif
  if .not.xfound then
    message e uvshow "Invalid XTYPE "'xtype'
    found = no
  endif
  if .not.yfound then
    message e uvshow "Invalid YTYPE "'ytype'
    found = no
  endif
  if .not.found then
    return
    ! This is only possible with some (minor) SIC modification
    message i uvshow "Possible values"
    say vocab /format A12
    if (exist(uv%extra_names)) then
      message i uvshow "Available additional columns are "
      say uv%extra_names /format A12
    endif
  endif
  !
  ! Plot header
  @ uvshow-head &1
  !
  ! Define boxes
  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"
    let is_first yes
    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."kwave") then
          let uvd[iaxis] sqrt(uvs[1]**2+uvs[2]**2)*uv%restfre*1D3/299702458D0
        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."w") 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 if (exist(uv%extra_names)) then
          for l 1 to size(uv%extra_names)
            if uv%extra_names[l].eq.atype then
              let icol uv%extra_columns[l]
              let uvd[iaxis] uvs[icol]
              if (atype.eq."para_angle") then
                let uvd[iaxis] 180*uvd[iaxis]/pi
              else if (atype.eq."doppler") then
                let uvd[iaxis] 299792.458*uvd[iaxis]
              endif
              let atext 'atype'
            endif
          next
        else
          message e uvshow "Type not supported " 'atype'
        endif
      next iaxis
      compute uvdmax max uvd[2]
      compute uvdmin min uvd[2]
!!        if (uvdmax.ne.uvdmin)
      greg1\limits /var uvd[1] uvd[2]
      if (is_first) then
         let l1 user_xmin
         let l2 user_xmax
         let l3 user_ymin
         let l4 user_ymax
         let is_first no
      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
!!        endif
    next ichan
    !
    ! Only consider positive weights
    if (xtype.eq."weight") let l1 0
    if (ytype.eq."weight") let l3 0
    !
    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"
         exa l1 l2 l3 l4
         SAY "Examine UVD ?"
         PAUSE
         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
    !
    let show_uvfit uvshow_fit
    !
    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) let uvff[iaxis] uvrr[1]
      else if (atype.eq."v") then
        let uvd[iaxis] uvs[2]
        let atext "V [m]"
        if (uvshow_fit) let uvff[iaxis] uvrr[2]
      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) let uvff[iaxis] 180/pi*atan2(uvrr[2],uvrr[1])
      else if (atype.eq."radius") then
        let uvd[iaxis] sqrt(uvs[1]**2+uvs[2]**2)
        let atext "UV radius [m]"
        if (uvshow_fit) let uvff[iaxis] sqrt(uvrr[1]**2+uvrr[2]**2)
      else if (atype.eq."kwave") then
        let uvd[iaxis] sqrt(uvs[1]**2+uvs[2]**2)*uv%restfre*1D3/299702458D0
        let atext "UV radius [k\gl]"
        ! This is not the same frequency...
        if (uvshow_fit) let uvff[iaxis] sqrt(uvrr[1]**2+uvrr[2]**2)*uv_model%restfre*1D3/299702458D0
      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) let uvff[iaxis] 24.0d0*(uvrr[4]-uvrr[1,4])+uvrr[5]/3600.d0
      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) let uvff[iaxis] uvrr[4]-uvrr[1,4] ! +UVRR[5]/86400.D0
      else if (atype.eq."w") then
        let uvd[iaxis] uvs[3]
        let atext "W (m)"
        if (uvshow_fit) let uvff[iaxis] uvrr[3]
      else if (atype.eq."amp") then
        let uvd[iaxis] sqrt(uvs[ireal]**2+uvs[iimag]**2)
        let atext "Amplitude [Jy]"
        if (uvshow_fit) let uvff[iaxis] sqrt(uvrr[ireal]**2+uvrr[iimag]**2)
      else if (atype.eq."phase") then
        let uvd[iaxis] 180/pi*atan2(uvs[iimag],uvs[ireal])
        let atext "Phase [deg]"
        if (uvshow_fit) let uvff[iaxis] 180/pi*atan2(uvrr[iimag],uvrr[ireal])
      else if (atype.eq."real") then
        let uvd[iaxis] uvs[ireal]
        let atext "Real part [Jy]"
        if (uvshow_fit) let uvff[iaxis] uvrr[ireal]
      else if (atype.eq."imag") then
        let uvd[iaxis] uvs[iimag]
        let atext "Imaginary part [Jy]"
        if (uvshow_fit) let uvff[iaxis] uvrr[iimag]
      else if (atype.eq."weight") then
        let uvd[iaxis] uvs[iweig]
        let atext "Relative Weight"
        if (uvshow_fit) let uvff[iaxis] uvrr[iweig]
      else
        !
        ! Here it does not make sense to show a Model fit...
        let show_uvfit no
        if (atype.eq."number") then
          let uvn[i] i
          let uvd[iaxis] uvn
          let atext "Visibility number"
        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
          for l 1 to size(uv%extra_names)
            if uv%extra_names[l].eq.atype then
              let icol uv%extra_columns[l]
              let uvd[iaxis] uvs[icol]
              if (atype.eq."para_angle") then
                let uvd[iaxis] 180*uvd[iaxis]/pi
                let atext "Parallactic Angle (^)"
              else if (atype.eq."doppler") then
                let uvd[iaxis] 299792.458*uvd[iaxis]
                let atext "Doppler (km/s)" 
              else
                let atext 'atype'
              endif
            endif
          next
        endif
      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]
        greg1\pencil 'itrack-1'
        if ((xtype.eq."u").and.(ytype.eq."v")) then
          greg1\limits uu -uu vv -vv
          greg1\points uvx uvy
          greg1\limits -uu uu -vv vv
          greg1\points uvx uvy
        else
          greg1\points 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
            greg1\errorbar y uvx uvy uve
          endif
        endif
      next itrack
    endif
    !
    if (show_uvfit) then
      greg1\pencil 1
      if (uvshow%fit.eq."CURVE") then
        defin integer nnn nn1
        let nnn size(uvff[1])
        let nn1 nnn-1
        define real suvff[nn1]
        let suvff uvff[2:nnn,1]
        if any(suvff.ge.uvff[1:nn1,1]) then
          greg1\points uvff[1] uvff[2]   ! Non strictly monotonic variable
        else
          greg1\curve  uvff[1] uvff[2]  ! Monotonic one...
        endif
        on error
      else
        greg1\points uvff[1] uvff[2]
      endif
      greg1\pencil 0
    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
        greg1\pencil 0
        if (xtype.eq."u").and.(ytype.eq."v") then
          let uu max(-user_xmin,user_xmax)
          let vv -user_ymin
          greg1\limits uu -uu vv -vv
          greg1\points uvd[1] uvd[2]
          greg1\limits -uu uu -vv vv
          greg1\points uvd[1] uvd[2]
        else
          greg1\points 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
      greg1\pencil 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
        greg1\limits uu -uu vv -vv
        greg1\points uvx uvy
        greg1\limits -uu uu -vv vv
        greg1\points uvx uvy
      else
        greg1\points uvx uvy
      endif
      greg1\pencil 0 /col 0
    endif
    !
    ! S.Guilloteau: 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...
      greg1\pencil 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
        greg1\limits uu -uu vv -vv
        greg1\points uvx uvy
        greg1\limits -uu uu -vv vv
        greg1\points uvx uvy
      else
        greg1\points uvx uvy
      endif
      greg1\pencil 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 size(uvs,2)
    let ne nd-1
    let uvs[ne] uvd[1]
    let uvs[nd] uvd[2]
    !
    ! end S.Guilloteau
    !
    !
    if (uvshow%zero) then
      greg1\pencil 1
      greg1\draw relo 'user_xmin' 0 /user
      greg1\draw line 'user_xmax' 0 /user /clip
      greg1\draw relo 0 'user_ymin' /user
      greg1\draw line 0 'user_ymax' /user /clip
    endif
    !
    greg1\pencil 0
    if (mark.ne."none") then
      let xmark &1%veloff+(ichan-&1%convert[1,1])*&1%velres
      if (mark.eq."frequency") then
        let imark nint((-(xmark-&1%veloff)*&1%restfre/299792.458+&1%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'
      greg1\draw text 0.1 -0.1 'cmark' 3 /box 7
    endif
    !
    greg1\box n n
    !
  next ichan
  !
  delete /symbol alimits
  !
  @ window_xy 'nx_box' 'ny_box' 1 1
  greg1\box
  greg1\label 'xtext' /x
  greg1\label 'ytext' /y
  !
  greg1\set expand 1.
  !
end procedure uvshow-sub
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure uvshow-field
  !
  ! This is intended to show things field-per-field
  ! There is not yet any simple mechanism in IMAGER to
  ! do so  -   We would need to extract the corresponding
  ! field from the whole data set and plot it.
  !
  ! That may be done through e.g; UV_EXTRACT /FIELD
  ! if we create such an option - FIELDS%N gives
  ! the number of fields in fact.
  !
  if .not.exist(fields%n) then
    SAY "No  FIELD%N value"
    return
  endif
  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,fields%n))
  endif
  if .not.exist(uvshow%nfields) define integer uvshow%nfields /global
  let uvshow%nfields fields%n
  define logical loop
  let loop yes
  for /while loop
    ! Here extract the corresponding Field
    uv_extract /field 'uvshow%ifield'
    uv_sort time ! Sort & Transpose
    @ uvshow-sub &1 &2 &3
    message I show "Showing field number "'uvshow%ifield'
    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,uvshow%nfields)+1
    else if (cursor_code.eq."P") then
      let uvshow%ifield mod(uvshow%ifield-2,uvshow%nfields)+1
    endif
    read uv ! Return back to original dataset 
  next
  !
end procedure uvshow-field
!
begin procedure uvshow-plane
  !
  ! This is intended to show things one Plane at a time
  ! with one box per field. There is no simple
  ! way to do that.
  !
  ! That may be done through e.g; UV_EXTRACT /FIELD
  ! if we create such an option - FIELDS%N gives
  ! the number of fields in fact.
  !
  SAY "PLANE mode not implemented"
  return base
  on error "return base"
  !
  let nxy fields%n
  define logical loop
  let loop yes
  for /while loop
    ! Here extract the corresponding Field
    uv_extract /field 'uvshow%ifield'
    uv_sort time ! Sort & Transpose
    @ uvshow-sub &1 &2 &3
    message I show "Showing field number "'uvshow%ifield'
    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,uvshow%nfields)+1
    else if (cursor_code.eq."P") then
      let uvshow%ifield mod(uvshow%ifield-2,uvshow%nfields)+1
    endif
    read uv ! Return back to original dataset 
  next
  !
end procedure uvshow-plane
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
on error "return base"
!
@ window_tools.greg
@ window_init
defin logical in_mosaic
let in_mosaic mosaic
if exist(fields%n) then
  if fields%n.gt.1 let in_mosaic yes
endif
!
if (in_mosaic) then
  SAY "&2+"
  if ("&2+".eq."PLANE+") then
    let first &3
    let last &3
    @ uvshow-sub "UV" &4 &5 !! &3
  else if ("&2+".eq."FIELD+") then     
    if .not.exist(uvshow%ifield) define integer uvshow%ifield /global
    let uvshow%ifield &3 
    PAUSE
    @ uvshow-field "UV" &4 &5
  else
    @ uvshow-sub "UV" &2 &3
  endif
else
  @ uvshow-sub "UV" &2 &3
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
