!------------------------------------------------------------------------
! p_pos.ima
! Procedure to plot multiple contour maps of spectral line images,
! in whichever order. Does not reset user limits. Uses several
! tricks for box size.
!
! Global variables defined in DEFINE.IMA
! Calls
! p_LOAD      (to load image)
! p_HEADER      (to plot header information)
! p_LEVEL         (to compute automatic contour levels)
! p_SCALE       (to compute automatic scale)
!
! S.Guilloteau 10-Jan-1994
!   Updated to Handle SIC Image variables  10-Nov-2017
!------------------------------------------------------------------------
! ON ERROR RETURN
!
if .not.exist(ActiveVariable) define char ActiveVariable*32 /global
@ p_name
@ window_tools
!
gtvl\destroy all
gtvl\change directory <greg
def real x2 dx y2 x1 y1
def double ratio
def integer m n n1 n2 nmap kdir k
def logical do_clean do_dirty do_coverage
let do_clean beam_type.eq."CLEAN"
let do_dirty beam_type.eq."DIRTY"
let do_coverage extra.eq."COVERAGE"
!
!!! symbol aaaref aaa
!
@ header_position
if exist(bbb) delete /var bbb
@ p_load      bbb ActiveVariable
!
! Check frequency axis
if ('ActiveVariable'%f_axis.eq.3) then
  message w pos "Transposing for display"
  if exist(aaa) delete /var aaa
  define integer m1 m2 m3
  let m1 'ActiveVariable'%dim[1]
  let m2 'ActiveVariable'%dim[2]
  let m3 'ActiveVariable'%dim[3]
  define image aaa[m3,m1,m2] * real /global
  vector\transpose 'ActiveVariable' aaa 312
  dele /var m1 m2 m3
  if (ActiveVariable.eq."bbb") delet /var bbb
  let ActiveVariable aaa
endif
!
@ p_scale      'ActiveVariable'
@ p_level      'ActiveVariable'
@ p_header      'ActiveVariable'
!
!    No limits /rg
if (aaa%ndim.le.2) then
  greg2\rgdata 'ActiveVariable' /variable
else if (aaa%ndim.eq.3) then
  greg2\rgdata 'ActiveVariable'[1] /variable
else
  greg2\rgdata 'ActiveVariable'[1,1] /variable
endif
if (box_limits.eq." ") then
  greg1\show angle
  EXA U_ANGLE
  greg1\limits /rgdata
  greg1\show angle
else
  symbol alimits 'box_limits'
  greg1\limits 'alimits' /rgdata
endif
!
if (aspect.eq.0) then
  let ratio 1.0
  let aspect 1.0 ! Needed
else
  let ratio aspect
endif
let x2 box_xmax
let y2 box_ymax
let x1 box_xmin
let y1 box_ymin
!
if first.eq.0 then
  let n1 1
else
  let n1 first
endif
if last.eq.0 then
  let n2 aaa%dim[3]
else
  let n2 last
endif
if (step.eq.0) then
  let nmap (n2-n1+1)
  let step 1
else
  let nmap (n2-n1+1)|step
endif
if ratio.gt.1.d0 then           ! M > N
  let m int(sqrt(nmap*ratio+1))
  let n nint(m|ratio)
  if (m*n.lt.nmap) then
    let m = m+1
    if (m*n.lt.nmap) then
      let n n+1
    endif
  endif
else
  let n int(sqrt(nmap|ratio+1))
  let m nint(n*ratio)
  if (m*n.lt.nmap) then
    let n = n+1
    if (m*n.lt.nmap) then
      let m m+1
    endif
  endif
endif
!
! Now plot
greg1\set expand min(6.0|(m+n),1.)
!
@ plot_position
@ find_nx_ny 'nmap'
!
on error
let kdir 0
let k n1
!
let do_tree yes /new log
!
for j 1 to m
  for i 1 to n
    !
    let kdir kdir+1
    if (do_tree) then
      gtvl\change directory <greg
      gtvl\create directory p'kdir'
      gtvl\change directory p'kdir'
    endif
    !
    @ window_xy 'nx_box' 'ny_box' 'kdir'
    !
    if (aaa%ndim.le.2) then
      greg2\rgdata 'ActiveVariable' /variable
    else if (aaa%ndim.eq.3) then
      greg2\rgdata 'ActiveVariable'[k] /variable
    else
      greg2\rgdata 'ActiveVariable'[k,1] /variable
    endif
    !
    ! Plot the RGDATA array...
    if (do_bit) then
      greg2\plot /scaling linear true_scale[1] true_scale[2]
    endif
    if (do_grey) then
      greg2\rgmap quiet /abs 1.0 /grey
    endif
    if (do_contour) then
      greg2\rgmap quiet /abs 1.0
    endif
    if (box_xmin.lt.1.001*x1).and.(box_ymin.lt.1.001*y1) then
      @ p_box      tick 'ActiveVariable'
    else
      @ p_box      no 'ActiveVariable'
    endif
    ! Mark if needed
    if (mark.ne."none") then
      @ p_mark      'ActiveVariable' 'k'
    endif
    !
    IF (K.GE.'ActiveVariable'%DIM[3]) BREAK ! Safety check
    let k k+step
    !
  next
next
!
! Remember the Channel selection
if .not.exist(popup) then
  define structure popup /global
  define integer popup%first popup%last popup%step /global
endif
let popup%first n1
let popup%last min(k,'ActiveVariable'%dim[3])
let popup%step step
!
gtvl\change directory <greg
!
@ p_header_box 'ActiveVariable'
gtvl\change directory <greg
