!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ p-old-over
!
! &1: gtvl directory name for more complex plots
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Define recovery behavior
on error return base
!
! Clear plot
if ("&1none".eq."none") then
   let true%directory "<greg"
   gtvl\clear
else
   let true%directory "&1"
   gtvl\change directory <greg
   gtvl\create directory 'true%directory'
   gtvl\change directory 'true%directory'
   let true%directory "<greg<"'true%directory'
endif
!gtvl\clear alpha
!
! Define useful variables
define logical do_cross
define double xmark
define integer nadd nbox imark ibox
define character cmark*12
!
! Load reference image
symbol aaaref aaa'nreference'
go\oldload 'names[nreference]' /in 'aaaref' /syntax hgdf
!
! Define reference image shortcuts
if .not.exist(aaaref_dim) then
   define integer aaaref_dim[7] /global
endif
if .not.exist(aaaref_convert) then
   define double aaaref_convert[3,7] /global
endif
let aaaref_dim     'aaaref'%dim
let aaaref_convert 'aaaref'%convert
!
! Define subcube to be plotted
@ l-old-prange.cube 'aaaref' center size
@ l-old-vrange.cube 'aaaref' range true%vrange
!
! Define plot layout
@ l-old-layout.cube 'aaaref' "1d" true%vrange%p[1] true%vrange%p[2] 1
@ l-old-maxbox.cube "1d"
!
! Define contour levels
let spacing    'spacings[nreference]'
@ l-old-level.cube 'aaaref'
!
! Define box setup (position, labels, text of legends)
@ l-old-box-setup.cube 'aaaref' image
!
! Define default marking scheme
if (mark.eq."*") then
   let true%mark "velocity"
else
   let true%mark 'mark' /lower
endif
!
! Plot headers only now because we need above information
@ l-old-header.cube 'aaaref'
greg2\rgdata 'aaaref' /variable
!
! Do we make a cross?
if (('aaaref'%x_axis.ne.1).or.('aaaref'%y_axis.ne.2)) then
   let do_cross no
else
   let do_cross yes
endif
!
! Set plot limits
greg1\limits true%prange%s[1,1] true%prange%s[2,1] true%prange%s[1,2] true%prange%s[2,2] seconds
!!$ apparently greg1\limits and JP disagree on the interpretation of the header
!!$say 'true%prange%s[1,1]' 'true%prange%s[2,1]' 'true%prange%s[1,2]' 'true%prange%s[2,2]'
!!$say 'true%prange%r[1,1]' 'true%prange%r[2,1]' 'true%prange%r[1,2]' 'true%prange%r[2,2]'
!!$say 'user_xmin' 'user_xmax' 'user_ymin' 'user_ymax'
!!$greg1\limits /rgdata
!!$say 'user_xmin' 'user_xmax' 'user_ymin' 'user_ymax'
!!$pause
!
! Define red and blue-shifted channels when possible
if ((do%vsys).and.('aaaref'%f_axis.ne.0)) then
   define integer ivsys vsys_pens['aaaref_dim[3]',2]
   define integer black[2] blue[2] red[2]
   greg1\pencil 10 /col 0 /dash 3
   greg1\pencil 11 /col 1 /dash 3
   greg1\pencil 13 /col 3 /dash 3
   greg1\pencil 0
   let black 0 10
   if (aaaref_convert[3,3].gt.0) then
      let red  1 11
      let blue 3 13
   else
      let red  3 13
      let blue 1 11
   endif
   let ivsys aaaref_convert[1,3]+(vsys-aaaref_convert[2,3])/aaaref_convert[3,3]
   if (ivsys.le.1) then
      let vsys_pens[ivsys,] red
      if (ivsys.eq.1) then
         let vsys_pens[1,1:2] black[1:2]
      endif
   else if (ivsys.ge.aaaref_dim[3]) then
      let vsys_pens[ivsys,] blue
      if (ivsys.eq.aaaref_dim[3]) then
         let vsys_pens['aaaref_dim[3]',1:2] black[1:2]
      endif
   else
      let vsys_pens[ivsys:'aaaref_dim[3]',1] red[1]
      let vsys_pens[ivsys:'aaaref_dim[3]',2] red[2]
      let vsys_pens[1:ivsys,1]               blue[1]
      let vsys_pens[1:ivsys,2]               blue[2]
      let vsys_pens[ivsys,1]                 black[1]
      let vsys_pens[ivsys,2]                 black[2]
   endif
   delete /var black blue red
endif
!
! Draw wedge corresponding to background image when asked
if (do%wedge.and.do%header) then
   let spacing 'spacings[nreference]'
   go\oldload 'names[1]' /in aaa1 /syntax hgdf
   @ l-old-level.cube aaa1
   @ l-old-scale.cube aaa1
   @ l-old-wedge.cube
endif
!
! Define number of additional boxes
let nadd 0
if (do%weight) then
   let nadd nadd+1
endif
if (do%dirty) then
   let nadd nadd+1
endif
if (do%coverage) then
   let nadd nadd+1
endif
!
! Compute total number of box
let nbox true%nbox+nadd
!
! Define box characteristics
define real myaspect
if (do%aspect.eq.0) then
   let myaspect abs((user_xmax-user_xmin)/(user_ymax-user_ymin))
   if (myaspect.lt.0.1.or.myaspect.gt.10) then
      message w go "box aspect ratio > 10: ignored"
      let myaspect 1.0
   endif
else
   let myaspect do%aspect
endif
if ("&1none".eq."none") then
   set\panel /default /aspect 'myaspect' /fixed trc /inter 0 0 /position plot /nxy 'nbox'
else
   set\panel /default /aspect 'myaspect' /fixed trc /inter 0 0 /position cubeset%panel%true%min[1] cubeset%panel%true%max[1] cubeset%panel%true%min[2] cubeset%panel%true%max[2] /nxy 'nbox'
endif
let true%nxybox cubeset%panel%do%nxy
!
if (do%expand.eq.0) then
   ! Automatic definition character size
   let true%expand min(6.0/(true%nxybox[1]+true%nxybox[2]),1.)
else
   ! User defined character size
   let true%expand 'do%expand'
endif
greg1\set expand 'true%expand'
!
! Loop over images to be overplotted
for iimage 1 to nimages
   ! Load image
   if (iimage.ne.nreference) then
      go\oldload 'names[iimage]'  /in aaa'iimage' /syntax hgdf
   endif
   ! Resample velocity axis when needed
   if ((iimage.ne.nreference).and.(aaa'iimage'%dim[3].gt.1)) then
      cube\resample 'names[iimage]' /like 'names[nreference]'
      ! VVV Here the image to be reloaded has forcibly a vlm type due
      ! to how cube\resample works
      go\oldload /in aaa'iimage' /syntax hgdf
   endif
   ! Compute levels and scaling
   let scale scales[iimage]
   let spacing 'spacings[iimage]'
   @ l-old-level.cube aaa'iimage'
   ! Plot planes
   for iplane true%first[1] to true%last[1] by true%step[1]
      ! Define current box position and create corresponding directory
      ! or directly go to this directory
      if (iimage.eq.1) then
         gtvl\change directory 'true%directory'
	 if (true%step[1].gt.0) then
            let ibox iplane-true%first[1]+1
	 else
	    let ibox true%first[1]-iplane+1
	 endif
	 set\panel 'ibox'
         gtvl\create directory p'iplane'
         gtvl\change directory p'iplane'
      else
         gtvl\change directory 'true%directory'<p'iplane'
      endif
      ! Load plane 'iplane'
      if (aaa'iimage'%ndim.le.2) then
         greg2\rgdata aaa'iimage' /variable
      else if (aaa'iimage'%ndim.eq.3) then
         if (aaa'iimage'%dim[3].eq.1) then
            greg2\rgdata aaa'iimage'[1] /variable
         else
            greg2\rgdata aaa'iimage'[iplane] /variable
         endif
      else
         greg2\rgdata aaa'iimage'[iplane,1] /variable
      endif
      ! Plot plane 'iplane'
      if (iimage.eq.1) then
	 ! VVV This is a temporary workaround to avoid the flurry of
	 ! warnings regarding the presence of NaNs when plotting
	 sic message sic s-w
         if (do%bit) then
            greg2\plot /scaling 'true%scaling' true%scale[1] true%scale[2]
         endif
         if (do%grey) then
            greg2\rgmap quiet /abs 1.0 /grey
         endif
         if (do%contour) then
            if ((do%vsys).and.('aaaref'%f_axis.ne.0)) then
               greg2\rgmap quiet /abs 1.0 /pens 'vsys_pens[iplane,1]' 'vsys_pens[iplane,2]'
            else
               greg2\rgmap quiet /abs 1.0 /pens 'pens[1,1]' 'pens[2,1]'! /keep "poly/test-"'iplane'
            endif
         endif
	 sic message sic s+w
         greg1\pencil 0
         ! Plot a cross at the phase center when asked
         ! Plot it here, this way it will be under the contours of others images
         if (do_cross.and.do%cross) 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
      else
!!$         pen 'pens[1,iimage]' /weight 5
!!$         pen 'pens[2,iimage]' /weight 5
!!$         greg1\pencil 7 /weight 2
!!$         greg2\rgmap quiet /abs 1.0 /pens 7
         greg2\rgmap quiet /abs 1.0 /pens 'pens[1,iimage]' 'pens[2,iimage]'
         greg1\pencil 0
      endif
   next iplane
next iimage
!
! Draw box, mark
for iplane true%first[1] to true%last[1] by true%step[1]
   ! Go to directory
   gtvl\change directory 'true%directory'<p'iplane'
   ! Plot the clean beam in the bottom,left corner when asked
   if (do%nice) then
      @ l-old-beam.cube 'aaaref' ! brc
   endif
   ! Make a white mask in the top left corner when asked
   if (exist(do%mask)) then
      if (do%mask) then
         @ l-old-mask.cube 7 0.3 0.15
      endif
   endif
   ! Print the channel or frequency or velocity when asked
   if (true%mark.ne."none") then
      let xmark 'aaaref'%convert[2,3]+(iplane-'aaaref'%convert[1,3])*'aaaref'%convert[3,3]
      if ((true%mark.eq."channel").or.(true%mark.eq."plane")) then
         let xmark iplane
      else if (true%mark.eq."frequency") then
         let imark nint((-(xmark-'aaaref'%veloff)*'aaaref'%restfre/299792.458+'aaaref'%restfre)*1e3)
         let xmark 1d-6*imark
      else if (true%mark.eq."velocity") then
         let imark nint(xmark*100.)
         let xmark 1d-2*imark
      else
         message e over "Unknown mark type: "'mark'
         return base
      endif
      let cmark 'xmark'
      greg1\pencil 7
      greg1\draw text 0.2 -0.1 'cmark' 3 /box 7
      greg1\pencil 0
   endif
   ! Plot box without labels
   @ l-old-box.cube no 'aaaref'
next iplane
!
! Plot x and y axis labels
!
! *** JP: This is a problem for poly latter on... Unclear whether the next
! *** JP: line is even needed.
gtvl\change directory 'true%directory'
@ l-old-legend.cube 'aaaref'
greg1\pencil 7
@ l-old-box.cube no 'aaaref'
greg1\pencil 0
greg1\box n n n
!
! Plot weight image when asked
! let name 'names[nreference]'
if (do%weight) then
   gtvl\change directory 'true%directory'
   set\panel 'true%nbox+1'
   gtvl\create directory weight
   gtvl\change directory weight
   on error continue
   go\oldload 'names[nreference]' wei /in wei /syntax hgdf
   on error return base
   if (cubego%oldload%found) then
      greg2\rgdata wei /variable
      greg2\extrema quiet /blanking /compute
      if (do%bit) then
	 greg2\plot wei /scaling linear rgmin rgmax
      endif
      if (do%contour) then
	 greg2\levels 3.33 6.66
	 greg2\rgmap quiet /percent 10
	 greg2\wedge /scaling linear rgmin rgmax /level
      else
	 greg2\wedge /scaling linear rgmin rgmax
      endif
      @ l-old-box.cube no 'aaaref'
      greg1\draw text 0 -1.25 "Weight image" 5 /char 2
   endif
endif
!
! Plot dirty beam when asked
if (do%dirty) then
   gtvl\change directory 'true%directory'
   set\panel 'true%nbox+1'
   gtvl\create directory dirty
   gtvl\change directory dirty
   on error continue
   go\oldload 'names[nreference]' beam /in lob /syntax hgdf
   on error return base
   if (cubego%oldload%found) then
      greg2\levels -0.5 to -0.1 by 0.1 0.1 to 0.5 by 0.1
      greg2\rgdata lob /variable
      if (do%bit) then
	 greg2\plot lob /scaling linear -0.5 1.0
      endif
      if (do%contour) then
	 greg2\rgmap quiet
      endif
      @ l-old-box.cube no 'aaaref'
      greg1\draw text -0.2 0.2 "Dirty Beam" 7 /box 3
   endif
endif
!
! VVV The last plot the UV coverage one, has been commented for the
! moment, as there is no way to deal with them under the current
! file name logic.
! Plot uv coverage when asked
! if (do%coverage.and.file("'name'.uvt")) then
!    ! Check dates and transpose when needed
!    define character ofilename*512 nfilename*512
!    let ofilename 'name'".uvt"
!    let nfilename 'cube%dir%tmp'"/"'true%name'".tuv"
!    @ l-old-date.cube 'ofilename' 'nfilename'
!    if (true%younger) then
!       message w over "Transposing "'ofilename'" to "'nfilename'" for display"
!       vector\transpose 'ofilename' 'nfilename' 21
!    endif
!    ! Plot uv coverage
!    gtvl\change directory 'true%directory'
!    set\panel 'nx_cover' 1
!    gtvl\create directory coverage
!    gtvl\change directory coverage
!    define image uvt 'nfilename' read
!    define real uu vv
!    greg1\limits /var uvt[1] uvt[2]
!    let uu max(abs(user_xmin),abs(user_xmax))
!    let vv max(abs(user_ymin),abs(user_ymax))
!    greg1\limits uu -uu vv -vv
!    greg1\points uvt[1] uvt[2]
!    greg1\limits -uu uu -vv vv
!    greg1\points uvt[1] uvt[2]
!    greg1\box p n
!    greg1\axis yr /label o
!    greg1\draw text -0.2 0.2 "uv Coverage" 7 /box 3
!    delete /variable ofilename nfilename uvt uu vv
! endif
!
! Change to the root directory for a correct behavior of the HARDCOPY command
! but reload the last plane to allow the user to use facilities like poly
! and mean when they plot only one plane
gtvl\change directory 'true%directory'
set\panel 'true%nbox'
if (aaa1%ndim.le.2) then
   greg2\rgdata aaa1 /variable
else if (aaa1%ndim.eq.3) then
   if (aaa1%dim[3].eq.1) then
      greg2\rgdata aaa1[1] /variable
   else
      greg2\rgdata aaa1['true%last[1]'] /variable
   endif
else
   greg2\rgdata aaa1['true%last[1]',1] /variable
endif
!
! Reset
greg1\set expand 1.0
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
