begin procedure sub_rgdata 
  if (&1%ndim.le.2) then
    greg2\rgdata &1 /variable
  else if (&1%ndim.eq.3) then
    greg2\rgdata &1[&2] /variable
  else 
    greg2\rgdata &1[&2,&3] /variable
  end if
end procedure
!
begin procedure sub_panel
  change dir <greg
  !
  define integer a_box i_box j_box ix iy
  if (exist(mapcur)) delete /var mapcur
  define structure mapcur /glo
  define double mapcur%freq /glo
  define integer mapcur%plane /glo
  define real mapcur%velo /glo
  !
  for /while yes
    draw relo
    if ((cursor_code.eq."Q").or.(cursor_code.eq."E")) then
      gtvl\change dir <greg
      delete /var mapcur
      return  ! Exit the loop and the procedure
    endif
    @ find_window_number 'phy_curs[1]' 'phy_curs[2]' mapcur%plane
    !
    ! Where am I ?
    let a_box mapcur%plane
    let i_box int((a_box-1)/nx_box)+1
    let j_box a_box-(i_box-1)*nx_box
    let mapcur%plane (mapcur%plane-1)*popup%step+popup%first
    gtvl\gtv search <greg<P'j_box'-'i_box'
    if (.not.gtv%exist) next
    gtvl\change dir <greg<P'j_box'-'i_box'
    greg1\draw relo * *
    !
    ! Now find Related pixel
    let mapcur%velo  (mapcur%plane-&1%convert[1,3])*&1%velres+&1%veloff
    let mapcur%freq  (mapcur%plane-&1%convert[1,3])*&1%freqres+&1%restfre
    let ix (use_curs[1]-&1%convert[2,1])/&1%convert[3,1]+&1%convert[1,1]
    let iy (use_curs[2]-&1%convert[2,2])/&1%convert[3,2]+&1%convert[1,2]
    !
    ! Show it
    @ side
    @ p_side_display &1 'ix' 'iy' mapcur &2
    gtvl\change dir <greg
  next
end procedure sub_panel  
!
!------------------------------------------------------------------------
! p_lmv_beam.ima
!
! Procedure to plot contour maps of dirty beams, even for Mosaics.
!
! S.Guilloteau 07-Nov-2014 (20 years and 9 months after the original)
!     Revised 17-Jun-2019
!     Revised 13-Oct-2019   Side display
!
! @ p_lmv_beam Variable CODE KEY Plane [Fchan [Lchan]]
!	  Variable	Image variable to be displayed
!   CODE      1 allow extra plots, 0 do not
!   KEY       PLANE or FIELDS
!   Plane     Plane or Field number
!   Fchan     First channel   (default First, 0==>1)
!  	Lchan	   	Last channel    (default Last, 0==> Variable%dim[3])
!------------------------------------------------------------------------
!
if (.not.show_side) then
  !
  ! Erase the "Side" graphic window
  gtvl\gtv search <vside
  if (gtv%exist) then
    gtvl\change directory <vside
    gtvl\clear directory
  endif
  gtvl\change dir <greg
end if  
!
gtvl\clear directory 
!
define integer KeyDim Odim
let KeyDim 3
if (&1%ndim.eq.4) then
  if (&1%dim[3]*&1%dim[4].gt.1) then 
    if (pro%narg.lt.4) then
      if (&1%dim[4].eq.1) then
        let KeyDim 4
      else if (&1%dim[3]) then
        let KeyDim 3
      else
        message E "SHOW &1" "Missing PLANE or FIELD key and number"
        return base
      endif
    else
      if ("&3+".eq."PLANE+") then
        if (&1%unit4.eq."FIELD") then
          let KeyDim min(4,&1%ndim)
        endif
      else if ("&3+".eq."FIELD+") then
        if (&1%unit4.ne."FIELD") then
          let KeyDim min(4,&1%ndim)
        endif
      else
        message E "SHOW &1" "Invalid Keyword &3"
        return base
      endif  
      let Odim 7-KeyDim  
      if (&4.gt.&1%dim[ODim]) then
        message E "SHOW &1" "&3 out of range "'&1%dim[ODim]'
        return base
      endif 
    endif
  endif
endif
!
!! on error "return"  ! "return base" gives better results
!
def character save_spacing*16[2]
!
let save_spacing 'spacing[1]' 'spacing[2]'
let spacing -1 USER
!let save_scale scale
greg2\levels -0.40 -0.30 -0.20 -0.15 -0.10 -0.05 -0.02 0.02 0.05 0.1 0.15 0.20 0.30 0.40 0.50
!
@ window_tools.greg
!
!
define integer next_arg arg_beam arg_cover
def real x2 dx y2
def double xmark ratio old_aspect
def integer m n n1 n2 imark nmap nadd code_mode
def character cmark*12
def logical my_do_clean my_do_dirty my_do_coverage
!
@ header_position
!
@ p_scale &1
@ p_level &1
if do_wedge then
  @ p_wedge 
endif
@ p_header &1 'KeyDim'
!
define logical doflux
let doflux no
if (&1%major.ne.0) then
  define character u_unit*12
  let u_unit '&1%unit' /upper
  let doflux u_unit.eq."JY/BEAM"
  !exa u_unit
  !exa &1%unit
  delete /var u_unit
  if (doflux) then
    let view%jyperk &1%major*&1%minor/4./log(2.0)
    let view%jyperk 2.0*1.38e3*pi*view%jyperk|(2.99792458e8|&1%restfre*1e-6)^2
  endif
endif
!
greg2\rgdata &1 /variable
!
greg1\limits /rgdata
if ((size[1].ne.0).or.(size[2].ne.0)) then
  if (size[1].eq.0) then
    greg1\limits = = (center[2]-size[2]/2) (center[2]+size[2]/2) seconds
  else if (size[2].eq.0) then
    greg1\limits (center[1]+size[1]/2) (center[1]-size[1]/2) = = seconds
  else
    greg1\limits (center[1]+size[1]/2) (center[1]-size[1]/2) (center[2]-size[2]/2) (center[2]+size[2]/2) seconds
  endif
endif
!
if (aspect_ratio).or.(aspect.eq.0) then
  let ratio abs((user_xmax-user_xmin)/(user_ymax-user_ymin))
else
  let ratio aspect
end if
!
if (ratio.lt.0.1.or.ratio.gt.10) then
  message W SHOW "box aspect ratio > 10: ignored"
  let ratio 1.0
endif
let x2 box_xmax
let y2 box_ymax
!
let nadd 0
next_arg = 2
let code_mode &2  ! Actually unused in this case
!
let n1 first
if (pro%narg.gt.4) then
  let n1 &5
end if
if (n1.eq.0) then
  let n1 1
end if
let n2 last
if (pro%narg.gt.5) then
  let n2 &6 
end if
if (n2.eq.0) then
  let n2 &1%dim[KeyDim]
end if
!
let nmap n2-n1+1
!
if ratio.gt.1.d0 then           ! M > N
  let m int(sqrt((nmap+nadd)*ratio+1))
  let n nint(m/ratio)
  if (m*n.lt.(nmap+nadd)) then
    let m = m+1
    if (m*n.lt.(nmap+nadd)) then
      let n n+1
    end if
  end if
else
  let n int(sqrt((nmap+nadd)/ratio+1))
  let m nint(n*ratio)
  if (m*n.lt.(nmap+nadd)) then
    let n = n+1
    if (m*n.lt.(nmap+nadd)) then
      let m m+1
    end if
  end if
end if
greg1\set expand min(6.0/(m+n),1.)
let dx (x2-box_xmin)/n/ratio
let dx min((y2-box_ymin)/m,dx)
let k n1-1 /new integer
!
delete /variable ny_box nx_box 
define integer ny_box nx_box /global
let ny_box m
let nx_box n
let mark 'mark' /lower
!
! NEW
let old_aspect aspect
let aspect_ratio yes ! Respect aspect ratio
let aspect ratio     ! New value of 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
@ window_xy 'nx_box' 'ny_box' 1 1  ! Box position
!
let verify sic%verify /new log
sic verif off
!
let counter 0 /new integer
for j 1 to m
  for i 1 to n
    gtvl\change directory <greg
    gtvl\create directory p'i'-'j'
    gtvl\change directory p'i'-'j'
    let counter counter+1 !NEW
    @ find_window_position 'counter' !
    let k k+1
    if ((k.gt.&1%dim[KeyDim]).or.(k.gt.n2)) then
      ! Remove the empty directory and exit
      gtvl\change directory <greg
      greg1\set expand 1.0
      gtvl\destroy directory p'i'-'j'
      break ! 
    else
      ! Plot the RGDATA array...
      if (KeyDim.eq.3) then
        @ sub_rgdata &1 'k' &4 
      else
        @ sub_rgdata &1 &4 'k' 
      endif
      if (do_bit) then
        greg2\plot /scaling linear true_scale[1] true_scale[2] ! &1%min &1%max
      end if
      if (do_grey) then
        greg2\rgmap quiet /abs 1.0 /grey
      end if
      ! Now the graphic
      if (do_contour) then
        greg2\rgmap quiet /abs 1.0
      end if
      !
      if (my_do_clean) @ p_beam
      !
      ! The box
      if i.eq.1.and.(k+n.gt.n2) then
        @ p_box label &1 
      else
        @ p_box no &1 
      end if
      if (cross.ne.0) then
        greg1\draw relo 0 cross*sec /user
        greg1\draw line 0 -cross*sec /user
        greg1\draw relo cross*sec 0 /user
        greg1\draw line -cross*sec 0 /user
      end if
      !
      if (mark.ne."none") then
        if (exist(do_mask)) then
          if (do_mask) @ p_hide 7 0.3 0.15
        end if 
        if (KeyDim.eq.3) then   
          @ p_mark &1 'k'
        else
          greg1\draw text 0.2 -0.1 'k' 3 /box 7
        endif
      end if 
      !
    end if
  next
next
!
symbol aaaref &1
greg1\set expand 1.0
gtvl\change dir <greg
!
! Set the legend
set expand 1.0
@ p_legend X &1 
@ p_legend Y &1 
!
let spacing 'save_spacing[1]' 'save_spacing[2]'
!
! Remember the Channel selection
@ d_popup 'n1' 'n2' 1
!
! Side panel to explore values
if (show_side) then
  ! @ side
  @ sub_panel &1 'doflux'
endif
! Restore aspect ratio
let aspect old_aspect
!
!
if .not.exist(ActiveVariable) define char*32 ActiveVariable /global
let ActiveVariable &1
if verify sic verify on

