!------------------------------------------------------------------------
! p_popup.ima
!
! Procedure to "popup"  a given panel of a multi-plane plot
! of a data cube.  This is more than a simple zoom, since
! the box is drawn around the popped up plane.
!
! Global variables defined in DEFINE.IMA
! Calls
!     q_mark.greg   ( To indicate which plane is loaded)
!
! S.Guilloteau Sep-2017
!------------------------------------------------------------------------
!
! Verify we can POPUP anything at all
if (.not.exist(POPUP)) then
  MESSAGE W POPUP "No map-only panels displayed"
  return
endif
!
define logical done_init
done_init = exist(myplane_number)
gtvl\gtv SEARCH <POPUP
if (.not.gtv%exist) then
  gtvl\create directory <POPUP
  gtvl\change dir <POPUP
  greg1\set box 4 22 2.7 20.7
  gtvl\change dir <GREG
endif
!
! Redefine it each time because of AAAREF symbolics
begin procedure pop_one_plane
  let myplane_number 0
  if pro%narg.gt.0 then
    if ("&1".ne."^") let myplane_number &1
  endif
  if (myplane_number.le.0) then
    @ find_window_number 'phy_curs[1]' 'phy_curs[2]' myplane_number
  endif
  let myplane_number (myplane_number-1)*popup%step+popup%first
  if myplane_number.gt.max(popup%last,popup%first) return
  if myplane_number.lt.min(popup%last,popup%first) return
  gtvl\change directory <POPUP
  gtvl\clear directory <POPUP
  @ plot_one_plane 'ActiveVariable' myplane_number
  gtvl\change directory <GREG
end procedure
!
if (done_init) then
  if popup%reset @ popup_limits
  if pro%narg.gt.0 then
    @ pop_one_plane &1
  else
    greg1\look "@ pop_one_plane"
  endif
  return
endif
!
define integer myplane_number /global
!
begin procedure popup_limits
  gtvl\clear directory <POPUP
  gtvl\change directory <POPUP
  !
  greg2\rgdata 'ActiveVariable' /variable
  !
  if ('ActiveVariable'%f_axis.eq.3) then
    ! MAP
    if size[1].eq.0 then
      greg1\limits /rgdata
    else if (size[1].gt.0) then
      greg1\limits size[1]/2 -size[1]/2 -size[1]/2 size[1]/2 seconds
    end if
  else
    ! POS or equivalent
    if (box_limits.eq." ") then
      greg1\limits /rgdata
    else
      symbol alimits 'box_limits'
      greg1\limits 'alimits' /rgdata
    endif
    !
  endif
  define log do_ratio
  define real ratio
  if exist(aspec_ratio) then
    let do_ratio aspect_ratio
  else
    let do_ratio no
  endif
  if aspect.eq.0 let do_ratio yes
  if (do_ratio) then
    let ratio abs((user_xmax-user_xmin)/(user_ymax-user_ymin))
  else
    let ratio aspect
  end if
  greg1\set box 4 22 2.7 20.7
  !
  define real dx
  let dx (box_xmax-box_xmin)/ratio
  let dx min((box_ymax-box_ymin),dx)
  greg1\set box box_xmin box_xmin+dx box_ymax-dx box_ymax
  gtvl\change dir <GREG
  !
  let popup%reset no
end procedure popup_limits
!
begin procedure plot_one_plane
  def double xmark
  def character cmark*12
  def logical my_do_clean
  def integer k imark
  !
  let k &2
  let my_do_clean  do_nice  !! beam_type.eq."CLEAN"
  !
  if (&1%ndim.le.2) then
    greg2\rgdata &1 /variable
  else if (&1%ndim.eq.3) then
    greg2\rgdata &1[k] /variable
  else
    greg2\rgdata &1[k,1] /variable
  end if
  ! Plot the RGDATA array...
  if (do_bit) then
    greg2\plot /scaling linear true_scale[1] true_scale[2] ! &1%min &1%max
  end if
  !
  ! Now the graphic
  if (do_grey) greg2\rgmap quiet /abs 1.0 /grey
  if (do_contour) greg2\rgmap quiet /abs 1.0
  !
  if (do_nice) @ p_beam &1
  !
  ! The box
  @ p_box  label &1
  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
  ! The mark
  @ p_mark &1 'k'
  ! The wedge
  if (do_bit) wedge right
  !
end procedure plot_one_plane
!
@ popup_limits
if pro%narg.ne.0 then
  @ pop_one_plane &1
else
  greg1\look "@ pop_one_plane "
endif
