!------------------------------------------------------------------------
! P_LMV_MAP.GREG
!
! 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 MAPPING program
!
! S.Guilloteau 07-Nov-2014 (20 years and 9 months after the original)
!
! @ 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])
!------------------------------------------------------------------------
@ window_tools.greg
!
on error "return"  ! "return base" gives better results
!
define integer next_arg arg_beam arg_cover
gtvl\destroy all
def real x2 dx y2
def double xmark ratio
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
!
@ p_scale.greg &1
@ p_level.greg &1
if do_bit then
  @ p_wedge.greg 
endif
@ p_header.greg &1
!
greg2\rgdata &1 /variable
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
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
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  !! beam_type.eq."CLEAN"
  let my_do_dirty  do_dirty !! beam_type.eq."DIRTY"
  let my_do_coverage do_coverage !! 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
    let arg_cover next_arg
  end if
else
  let my_do_coverage no
  let my_do_dirty no
  let my_do_clean no
endif
!
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.eq.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]
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 mbox nbox ubox
define integer mbox nbox /global
let mbox m
let nbox n
define real ubox[5,nbox,mbox] /global
let mark 'mark' /lower
!
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 ubox[1,i,j] x2+(i-n-1)*dx*ratio
    let ubox[2,i,j] x2+(i-n)*dx*ratio
    let ubox[3,i,j] y2-j*dx
    let ubox[4,i,j] y2+(1-j)*dx
    greg1\set box x2+(i-n-1)*dx*ratio x2+(i-n)*dx*ratio y2-j*dx y2+(1-j)*dx
    let k k+1
    if ((k.gt.&1%dim[3]).or.(k.gt.n2)) then
      let ubox[5,i,j] 0
      if my_do_dirty then
        greg2\levels -0.5 to -0.1 by 0.1 0.1 to 0.5 by 0.1
        greg2\rgdata 'pro%arg[arg_beam]' /var
        if (do_bit) then
          greg2\plot 'pro%arg[arg_beam]' 
        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
        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
        gtvl\change dir <greg
        return
      end if
    else
      let ubox[5,i,j] 1.0*k
      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
      if (do_grey) then
        greg2\rgmap quiet /abs 1.0 /grey
      end if
                                ! Now the graphic
      let xmark &1%convert[2,3]+(k-&1%convert[1,3])*&1%convert[3,3]
      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 1.0*k
      else if (mark.ne."none") then
        let mark "velocity"
        let imark nint(xmark*100.)
        let xmark 1d-2*imark
      end if
      if (do_contour) then
        greg2\rgmap quiet /abs 1.0
      end if
      !
      if (my_do_clean) then
        @ p_beam.greg &1
      endif
      !
      ! The box
      if i.eq.1.and.(k+n.gt.n2) then
        if (&1%angle.ne.0) then
          greg1\box /unit sec
        else
          greg1\box /abs
        end if
      else
        if (&1%angle.ne.0) then
          greg1\box n n /unit sec
        else
          greg1\box n n /abs
        end if
      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
      let cmark 'xmark'
      greg1\draw text 0.1 -0.1 'cmark' 3 /box 7
    end if
  next
next
greg1\set expand 1.0
define real last_box[4]
let last_box[1] box_xmin
let last_box[2] box_xmax
let last_box[3] box_ymin
let last_box[4] box_ymax
gtvl\change dir <greg
greg1\set box last_box[1] last_box[2] last_box[3] last_box[4]
!
