!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ p_pv.greg
! J.Pety 2004
!
! Improvement: what is happening when using an input cube already transposed?
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
@ p_name.greg ! Check name and type variables
!
@ window_tools.greg
!
gtvl\destroy all
gtvl\clear alpha
!
!
!
define double qrange[2] xmark
define integer true_first true_last nbox imark iplane isampling paxis qaxis
define character cmark*12 old_type*12 new_type*12 trans_type*12 pv_kind*12
!
!
!
let old_type 'type'
!
! Check frequency axis
!
if exist(bbb) then
   delete /var bbb
endif
define header bbb 'name'.'type' read /global
if (bbb%f_axis.eq.3) then
   if (pv_type.eq."xv") then
      let    pv_kind "pv"
      let   new_type "xvy"
      let trans_type "132"
      let paxis 1
      let qaxis 2
   else if (pv_type.eq."vy") then
      let    pv_kind "vp"
      let   new_type "yxv"
      message w pv "Transposing "'name'.'type'" to "'name'"."'new_type'" for display"
      vector\transpose 'name'"."'type' 'name'"."'new_type' 21
      let type 'new_type'
      message w pv "Type set to "'new_type'
      let   new_type "vyx"
      let trans_type "312"
      let paxis 2
      let qaxis 1
   else
      message e pv "Type "'pv_type'" not supported"
   endif
   message w pv "Transposing "'name'.'type'" to "'name'"."'new_type'" for display"
   vector\transpose 'name'"."'type' 'name'"."'new_type' 'trans_type'
   let type 'new_type'
   message w pv "Type set to "'new_type'
else
   message e pv "Cube "'name'"."'type'" must be lmv ordered."
   return
endif
@ p_sampling.greg bbb
delete /var bbb
!
! Load reference image and plot header
!
@ header_position
@ p_load.greg aaa
@ p_scale.greg aaa
@ p_level.greg aaa
@ p_header.greg aaa
greg2\rgdata aaa /variable
!
!
!
let type 'old_type'
message w pv "Type reset to "'old_type'
!
! Definition of size of plotted diagram
!
greg1\limits /rgdata
if ((range[1].ne.0).or.(range[2].ne.0)) then
   if (pv_kind.eq."pv") then
      greg1\limits = = range[1] range[2]
   else
      greg1\limits range[1] range[2] = =
   endif
endif
if (size[paxis].ne.0) then
   define real min max
   let min (center[paxis]-size[paxis]/2)*sec
   let max (center[paxis]+size[paxis]/2)*sec
   if (pv_kind.eq."pv") then
      if (aaa%convert[3,1].gt.0) then
         greg1\limits min max = =
      else
         greg1\limits max min = =
      endif
   else
      if (aaa%convert[3,2].gt.0) then
         greg1\limits = = min max
      else
         greg1\limits = = max min
      endif
   endif
   delete /var min max
endif
!
! Definition of first and last plotted planes.
!
if (size[qaxis].ne.0) then
   let qrange 'center[qaxis]-size[qaxis]/2' 'center[qaxis]+size[qaxis]/2'
   let qrange qrange*sec
   if (aaa%convert[3,3].gt.0) then
      let true_first (qrange[1]-aaa%convert[2,3])/aaa%convert[3,3]+aaa%convert[1,3]
      let true_last  (qrange[2]-aaa%convert[2,3])/aaa%convert[3,3]+aaa%convert[1,3]
   else
      let true_first (qrange[2]-aaa%convert[2,3])/aaa%convert[3,3]+aaa%convert[1,3]
      let true_last  (qrange[1]-aaa%convert[2,3])/aaa%convert[3,3]+aaa%convert[1,3]
   endif
   let true_first max(true_first,1)
   let true_last  min(true_last,aaa%dim[3])
   if (size[qaxis].gt.0) then
      let isampling -inte_sampling[qaxis]
   else
      let isampling +inte_sampling[qaxis]
   endif
else
   let true_first 1
   let true_last  aaa%dim[3]
   let isampling  -inte_sampling[qaxis]
endif
let nbox abs((true_last-true_first)/inte_sampling[qaxis])+1
!
! Draw wedge corresponding to background image when asked
!
if (do_wedge) then
   @ p_scale.greg aaa
   @ p_level.greg aaa
   @ p_wedge.greg
endif
!
! Definition of box characteristics.
!
let aspect 1.0
let aspect_ratio yes ! Respect aspect ratio
let fixed_trc yes    ! Top right corner is fixed
let inter 0.0 0.0    ! No interspace between individual boxes (unit: % of box sizes)
@ plot_position      ! Definition of total available plotting place
@ find_nx_ny 'nbox'  ! Compute number of boxes in the x and y direction
@ find_size_margin 'nx_box' 'ny_box'   ! Compute individual box size and margin needed to keep aspect
greg1\set expand min(6.0/(nx_box+ny_box),1.) ! Define character size
!
! Plot planes.
!
for iwindow 1 to nbox
   !
   !
   !
   let iplane true_last+isampling*(iwindow-1)
   !
   ! Define current box position and create corresponding directory
   ! or directly go to this directory
   !
   gtvl\change directory <greg
   @ find_window_position 'iwindow'
   gtvl\create directory p'iwindow'
   gtvl\change directory p'iwindow'
   !
   ! Load plane 'iplane'.
   !
   if (aaa%ndim.le.2) then
      greg2\rgdata aaa /variable
   else if (aaa%ndim.eq.3) then
      greg2\rgdata aaa[iplane] /variable
   else
      greg2\rgdata aaa[iplane,1] /variable
   endif
   !
   ! Plot plane 'iplane'.
   !
   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 (do_cross) then
!!$      draw relo 0 cross*sec /user
!!$      draw line 0 -cross*sec /user
!!$      draw relo cross*sec 0 /user
!!$      draw line -cross*sec 0 /user
!!$   endif
   !
next
!
! Draw box, mark.
!
let mark 'mark' /lower
for iwindow 1 to nbox
   !
   !
   !
   let iplane true_last+isampling*(iwindow-1)
   !
   ! Go to directory.
   !
   gtvl\change directory <greg<p'iwindow'
   !
   ! Make a white mask in the top left corner when asked.
   !
   if (exist(do_mask)) then
      if (do_mask) then
         @ p_mask.greg 7 0.3 0.15
      endif
   endif
   !
   ! Print the channel or frequency or velocity when asked.
   !
   if (mark.ne."none") then
      let xmark (aaa%convert[2,3]+(iplane-aaa%convert[1,3])*aaa%convert[3,3])/sec
      if (mark.eq."channel") then
         let xmark iplane
      else
         let mark "position"
         let imark nint(xmark*100.)
         let xmark 1d-2*imark
      endif
      let cmark 'xmark'
      greg1\draw text 0.2 -0.1 'cmark' 3 /box 7
   endif
   !
   ! Plot box without labels.
   !
   @ p_box.greg no aaa
   !
next
!
! Plot x and y axis labels on the bottom, right corner.
! Try to be clever to avoid wrongly labelling the uv coverage!
!
gtvl\change directory <greg
if (do_coverage) then
   define integer nx_cover
   let nx_cover nbox-(ny_box-1)*nx_box
   if (nx_cover.ne.1) then
      @ window_xy 'nx_box' 'ny_box' 1 1
   else
      let nx_cover nx_box
      @ window_xy 'nx_box' 'ny_box' 1 2
   endif
else
   @ window_xy 'nx_box' 'ny_box' 1 1
endif
@ p_box.greg label aaa
!
gtvl\change directory <greg
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
