!------------------------------------------------------------------------
! P_VELOCITY.GRAPHIC
!
! General Procedure to plot multiple contour maps of spectral line images
!	Options:
!	DO_CONTOUR	do/not draw contours
!	DO_BIT		do/not plot bitmap
!	DO_NICE		do/not make a nice truncation for beam
!	DO_MASK		do/not mask some part for text
!
! Global variables defined in DEFINE.GRAPHIC
!
! Calls
!	P_LOAD.GREG    (to load images)
!       P_HEADER.GREG  (to plot header information)
!       P_LEVELS.GREG  (to compute automatic contour levels)
!       P_SCALE.GREG   (to compute scaling for bitmaps)
!       P_BEAM.GREG    (to plot the clean beam, if any)
! S.Guilloteau 15-Oct-1992
!------------------------------------------------------------------------
gtvl\destroy all
def real x2 dx y2 aamin aamax
def double xmark ratio
def integer m n n1 n2 imark nmap nadd
def character ctext*10[3] cmark*32[3] amark*10 save_type*12
def logical do_clean do_dirty
let do_clean beam_type.eq."CLEAN"
let do_dirty beam_type.eq."DIRTY"
!
let save_type 'type'
on error return
let type mean
let spacing area_spacing
sic\@ p_load.greg aaa        ! Load image & compute extrema if needed
!
sic\@ p_scale.greg aaa       ! Compute scaling
sic\@ p_level.greg aaa       ! Compute levels
sic\@ p_header.greg aaa      ! Draw header
sic\if do_color then
  sic\@ p_color.greg      ! Setup nice colour table
endif
!
define real hbox_x[2] hbox_y[2]
let hbox_x box_xmax page_x-0.1
let hbox_y box_ymin page_y-0.1
!
greg2\rgdata aaa /variable
if (aaa%angle.ne.0) then
  symbol option "/UNIT SEC"
else
  symbol option "/ABSOLUTE"
endif
if size[1].eq.0 then
  greg1\limits /rg
else if (size[1].gt.0) then
  greg1\limits size[1]|2 -size[1]|2 -size[1]|2 size[1]|2 seconds
endif
if (aspect.eq.0) then
  let ratio abs((user_xmax-user_xmin)|(user_ymax-user_ymin))
else
  let ratio aspect
endif
let box_aspect ratio
let x2 box_xmax
let y2 box_ymax
let m 2
let n 2
greg1\set expand min(6.0|(m+n),1.)
let dx (x2-box_xmin)|n|ratio
let dx min((y2-box_ymin)|m,dx)
!
delete /variable mbox nbox ubox
define integer mbox nbox /global
define integer k
let k 0
let mbox m
let nbox n
define real ubox[5,nbox,mbox] /global
on error break
for j 1 to m
  for i 1 to n
    if (do_tree) then
      gtvl\change directory <greg
      gtvl\create directory p'i'-'j'
      gtvl\change directory p'i'-'j'
    endif
                                !
    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.eq.4) then
      let ubox[5,i,j] 0
      if do_clean then
        greg1\box n n 'option'
        greg2\ellipse 0.5*aaa%major 0.5*aaa%minor 90-180*aaa%pa|pi /box dx|2 dx|2
        greg1\draw text -0.2 0.2 "Clean Beam" 7 /box 3
        let do_clean = .false.
      else if do_dirty then
                                ! Plot the beam
        define image lob 'name'.beam read
        greg2\levels -0.5 to -0.1 by 0.1 0.1 to 0.5 by 0.1
        greg2\rgdata lob /var
        if (do_bit) then
          greg2\plot lob /scaling linear -0.5 1.0
        endif
        if (do_contour) then
          greg2\rgmap quiet
        endif
        greg1\box n n 'option'
        greg1\draw text -0.2 0.2 "Dirty Beam" 7 /box 3
        let do_dirty = .false.
        delete /var lob
      else if (extra.eq."FLUX") then
        greg1\set box box_xmin+1 box_xmax box_ymin+1 box_ymax
        greg1\column x 1 y 2 /table 'name'.flux
        greg1\limits
        greg1\box
        greg1\histogram
        greg1\label "V\\dlsr (km/s)" /x
        greg1\set expand 1.0
      endif
    else
      if (k.eq.1) then
        let amark "Intensity "
      else if (k.eq.2) then
        let spacing velo_spacing
        let type velo
        sic\@ p_load.greg aaa
        sic\@ p_scale.greg aaa               ! Compute scaling
        sic\@ p_level.greg aaa               ! Compute levels
        let amark "Velocity "
      else
        let spacing width_spacing
        let type width
        sic\@ p_load.greg aaa
        sic\@ p_scale.greg aaa               ! Compute scaling
        sic\@ p_level.greg aaa               ! Compute levels
        let amark "Width "
      endif
      let cmark[k] 'level_min'" to "'level_max'" by "'true_spacing'" "'aaa%unit'
      let ctext[k] 'amark'
      let ubox[5,i,j] 1.0*k
      if (aaa%ndim.le.2) then
        greg2\rgdata aaa /variable
      else if (aaa%ndim.eq.3) then
        greg2\rgdata aaa[k] /variable
      else
        greg2\rgdata aaa[k,1] /variable
      endif
      if (do_bit) then          ! Plot the RGDATA array...
        greg2\plot /scaling linear true_scale[1] true_scale[2]
      endif
      if (do_grey) then
        greg2\rgmap quiet /abs 1.0 /grey
      endif
                                ! Now the greg
      if (do_contour) then
        greg2\rgmap quiet /abs 1.0
      endif
      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
      endif
      if (do_nice) then
        @ p_beam.greg aaa
      endif
      if i.eq.1.and.(j.eq.m) then               !  K+N.GT.N2) THEN
        greg1\box 'option'
      else
        greg1\box n n 'option'
      endif
      if (exist(do_mask)) then
        if (do_mask) then
          @ p_mask.greg 7 0.4 0.20
        endif
      endif
      greg1\set coord box
      greg1\draw text 0.4 -0.3 'amark' 3 /box 7
    endif
  next
next
greg1\set expand 0.8
gtvl\change directory <greg
greg1\set box hbox_x[1] hbox_x[2] hbox_y[1] hbox_y[2]
for i 1 to 3
  greg1\draw text .5 -8-2*i "\\i"'ctext[i]'" levels" 6 /box 7
  greg1\draw text .5 -8.8-2*i 'cmark[i]' 6 /box 7
next
greg1\set expand 1.0
!
let type 'save_type'

