begin procedure show-help
  say " "
!  say "Author: S.Guilloteau (2022)"
  say "Position-independent actions:"
  say "   Press E key: EXIT loop"
  say "   Press H key: HELP display"
  say "      Press Z key: ZOOM defined spatial region"
  say "      Press B key: BACK to full size"
  say "      Press C key: Adjust color LUT"
  say " "
end procedure show-help

begin procedure get_mzoom
  !
  ! Spatial zoom definition
  !
  define integer trc[2] blc[2] 
  define double xl[4]
  gtvl\change directory <greg<P1-1    ! Of course, only 1 panel
  define logical local_loop
  let local_loop yes
  for /while local_loop
    say "Select top, right corner"
    greg1\draw relo
    let trc pix_curs
    let xl[3:4] use_curs
    say "Select bottom, left corner"
    greg1\draw relo
    let blc pix_curs
    let xl[1:2] use_curs
    if ((blc[1].ne.trc[1]).and.(blc[2].ne.trc[2])) then
      let local_loop no
    else
      say "Area of zoom rectangle must be > 0.0."
      RETURN
    endif
  next
  define character c*14
  let box_limits " "
  for i 1 3 2 4
    let c xl[i] /forma 1PG13.6
    let box_limits 'box_limits'" "'c'
  next
  exa box_limits
  !
end procedure get_mzoom
!
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
  gtvl\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
  define logical doside
  !
  let view%go yes
  for /while view%go
    let mapcur%plane 0
    greg1\draw relo
    if ((cursor_code.eq."Q").or.(cursor_code.eq."E")) then
      gtvl\change dir <greg
      delet /var mapcur
      let view%go NO
      return  ! Exit the loop and the procedure
    else if (cursor_code.eq."C") then
      let color[3] /Prompt "Enter color smoothing "
      @ p_color
      break 
    else if (nwindowx_int.eq.1).and.(nwindowy_int.eq.1) then
      ! More actions possible with just 1 Window...
      ! e.g. Zoom, Back, Polygon definition etc...
      if (cursor_code.eq."Z") then
        ! Zoom   
        @ get_mzoom &1
        let view%go yes
        destroy dir <greg<p1-1
        return
      else if (cursor_code.eq."B") then
        ! Back
        let box_limits " "
        let view%go yes
        destroy dir <greg<p1-1
        return
      else if (cursor_code.eq."H") then
        @ show-help
        let doside no
      else if (cursor_code.eq."*") then  ! Right mouse button
        say "Define polygon"
        greg2\polygon
        let doside no
      else if (cursor_code.eq."P") then  ! Right mouse button
        if exist(poly%nxy) then
          greg2\polygon /plot
        endif
        let doside no
      else if (cursor_code.eq."F") then  ! Right mouse button
        @ get_flux &1   ! Created by @ define
        let doside no
      else
        @ find_window_number 'phy_curs[1]' 'phy_curs[2]' mapcur%plane
        let i_box 1
        let j_box 1
        let doside yes
      endif
    else
      ! Several panels, just display values
      @ 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'
      let doside = gtv%exist
    endif    
    !
    if (mapcur%plane.eq.0) next
    if (.not.doside) 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_map.ima
!
! Procedure to plot multiple contour maps of spectral line images
! overlaid on the bitmap. Similar to p_lmv.greg, but with some
! specificities due to IMAGER program
!
! 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_map Variable CODE [Beam] [Coverage] [Fchan [Lchan]]
!	  Variable	Image variable to be displayed
!   CODE      1 allow extra plots, 0 do not
!   [Beam]    Optional name of dirty beam variable
!   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
end if
!
gtvl\change dir <greg
gtvl\clear directory
!
on error "return"  ! "return base" gives better results
!
@ window_tools.greg
!
!
define integer next_arg arg_beam 
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_axes &1
@ p_scale &1
@ p_level &1
if do_wedge @ p_wedge
@ p_header &1
!
!
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
!
let x2 box_xmax
let y2 box_ymax
!
let nadd 0
next_arg = 2
let code_mode &2
!
if (code_mode.ne.0) then
  !
  ! Allow extra panels (used by SHOW, but not by VIEW)
  let my_do_clean  do_nice.and.(&1%major.ne.0)  !! beam_type.eq."CLEAN"
  let my_do_dirty  do_dirty.and.exist(BEAM)     !! beam_type.eq."DIRTY"
  let my_do_coverage do_coverage.and.exist(UV)  !! extra.eq."COVERAGE"
  if (my_do_dirty) then
    let nadd nadd+1
    let next_arg next_arg+1
    let arg_beam next_arg
  end if
  if (my_do_coverage) then
    let nadd nadd+1
    let next_arg next_arg+1
  end if
else
  let my_do_coverage no
  let my_do_dirty no
  let my_do_clean no
endif
!
if (&1%dim[3].gt.1).and.any(range.ne.0) then
  define double r1 r2
  ! Range is specified in 3rd-axis unit
  let r1 (range[1]-&1%convert[2,3])/&1%convert[3,3]+&1%convert[1,3]
  let r2 (range[2]-&1%convert[2,3])/&1%convert[3,3]+&1%convert[1,3]
  !
  let n1 min(r1,r2)
  let n2 max(r1,r2)
  let n1 max(1,n1)
  let n2 min(&1%dim[3],n2)
  if (n1.gt.n2) then
    let n1 min(r1,r2)
    let n2 max(r1,r2)
    say "Channel range out of scope "'n1'"-"'n2'
    return all
  endif
else  
  ! n1 - N2 from First & Last
  let n1 first
  if (pro%narg.gt.next_arg) then
    let next_arg next_arg+1
    let n1 'pro%arg[next_arg]'
  end if
  if (n1.le.0) then
    let n1 1
  end if
  let n2 last
  if (pro%narg.gt.next_arg) then
    let next_arg next_arg+1
    let n2 'pro%arg[next_arg]'
  end if
  if (n2.eq.0) then
    let n2 &1%dim[3]
  else
    let n2 min(n2,&1%dim[3])
  end if
  if (n2.lt.n1) then
    say "Channel range out of scope "'n1'"-"'n2'
    return all
  endif  
endif
!
let nmap n2-n1+1
!
! NEW
define log verify
if (.not.exist(view%go)) define logical view%go /global
define integer k
let view%go YES
defin integer counter 
delete /variable ny_box nx_box
define integer ny_box nx_box /global
!
for /while view%GO
  @ p_limits &1
  !
  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
  !
  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 
  !
  let ny_box m
  let nx_box n
  let mark 'mark' /lower
  !
  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
  sic verif off
  !
  let counter 0 
  for j 1 to m
    for i 1 to n
      gtvl\change directory <greg
      let counter counter+1 !NEW
      @ find_window_position 'counter' !
      let k k+1
      if ((k.gt.&1%dim[3]).or.(k.gt.n2)) then
        if my_do_dirty then
          gtvl\create directory p'i'-'j'
          gtvl\change directory p'i'-'j'
          greg2\levels -0.5 to -0.1 by 0.1 -0.05 0.05 0.1 to 0.5 by 0.1
          @ sub_rgdata BEAM 
          if (do_bit) then
            greg2\plot 
          end if
          if (do_contour) then
            greg2\rgmap quiet
            greg1\box n n /abs
          end if
          greg1\draw text -0.2 0.2 "Dirty Beam" 7 /box 3
          let my_do_dirty = .false.
        else if my_do_coverage then
          gtvl\create directory p'i'-'j'
          gtvl\change directory p'i'-'j'
          define real uvt_u uvt_v /like uv[1,]
          define real max_p max_n umax vmax uvmax
          let uvt_u uv[1,]
          let uvt_v uv[2,]
          compu max_p max uvt_u
          compu max_n min uvt_u
          let umax max(-max_n,max_p)
          compu max_p max uvt_v
          compu max_n min uvt_v
          let vmax max(-max_n,max_p)
          let uvmax 1.05*max(umax,vmax)
          !
          greg1\limits -uvmax uvmax -uvmax uvmax
          greg1\box p n
          greg1\axis yr /label o
          greg1\points uvt_u uvt_v
          greg1\limits uvmax -uvmax uvmax -uvmax
          greg1\points uvt_u uvt_v
          greg1\draw text -0.2 0.2 "UV Coverage" 7 /box 3
          let my_do_coverage = .false.
          delete /variable uvt_u uvt_v
        else
          greg1\set expand 1.0
          break ! Abort loop
        end if
      else
        gtvl\create directory p'i'-'j'
        gtvl\change directory p'i'-'j'
        ! Plot the RGDATA array...
        @ sub_rgdata &1 'k' &5
        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 &1
        !
        ! 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) then
               @ p_hide 7 0.3 0.15
            end if
          end if
          @ p_mark &1 'k'
        end if
        !
        if (do_fields) @ p_over_fields
      end if
    next
  next
  !
  symbol aaaref &1
  greg1\set expand 1.0
  gtvl\change dir <greg
  !
  ! Set the legend
  greg1\set expand 1.0
  @ p_legend X &1
  @ p_legend Y &1
  !
  ! Remember the Channel selection
  @ d_popup 'n1' 'n2' 1
  !
  ! Reset color scale if needed
  if (show_color.ne.0)  @ p_color 'show_color'
  !
  ! Side panel to explore values
  if (show_side) then
    ! @ side
    @ sub_panel &1 'doflux'
  else
    let view%go NO 
  endif
  !
next    ! If view%go is true....
! Restore aspect ratio
let aspect old_aspect
!
!
if .not.exist(ActiveVariable) define char*32 ActiveVariable /global
let ActiveVariable &1
if verify sic verify on
!
!
