!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ p-old-pv
!
! &1: Kind of PV diagram (xv, vx, yv or vy). Default is xv.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Define recovery behavior
on error return base
!
! Clear plot
if ("&2none".eq."none") then
   let true%directory "<greg"
   gtvl\clear
else
   let true%directory "&2"
   change directory <greg
   create directory 'true%directory'
   change directory 'true%directory'
   let true%directory "<greg<"'true%directory'
endif
gtvl\clear alpha
!
! Check NAME and TYPE variables
@ l-old-name.cube
!
! Define useful variables
define double xmark
define integer iplane paxis qaxis
define character ofilename*512 nfilename*512 old_name*512 old_type*12
define character new_type*12 trans_type*12 pv_kind*12 pv_type*12 cmark*12[2]
!
! Check input parameter
if ("&1none".eq."none") then
   let pv_type "xv"
else
   let pv_type "&1" /lower
endif
!
! Check frequency axis
let ofilename 'name'"."'type'
if .not.file(ofilename) then
   message e pv "Inexistent file: "'ofilename'
   return base
endif
define header bbb 'ofilename' read
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."vx") then
      let    pv_kind "vp"
      let   new_type "vxy"
      let trans_type "312"
      let paxis 1
      let qaxis 2
   else if (pv_type.eq."yv") then
      let    pv_kind "pv"
      let   new_type "yvx"
      let trans_type "231"
      let paxis 2
      let qaxis 1
   else if (pv_type.eq."vy") then
      let pv_kind "vp"
      ! Intermediate transposition
      let new_type "yxv"
      let nfilename 'cube%dir%tmp'"/"'true%name'"."'new_type'
      @ l-old-date.cube 'ofilename' 'nfilename'
      if (true%younger) then
         message w pv "Transposing "'ofilename'" to "'nfilename'" for display"
         vector\transpose 'ofilename' 'nfilename' 21
      endif
      let ofilename 'nfilename'
      ! Back to normal state
      let   new_type "vyx"
      let trans_type "312"
      let paxis 2
      let qaxis 1
   else
      message e pv "Type "'pv_type'" not supported"
   endif
   let nfilename 'cube%dir%tmp'"/"'true%name'"."'new_type'
   @ l-old-date.cube 'ofilename' 'nfilename'
   if (true%younger) then
      message w pv "Transposing "'ofilename'" to "'nfilename'" for display"
      vector\transpose 'ofilename' 'nfilename' 'trans_type'
   endif
else
   message e pv "Cube "'name'"."'type'" must be LMV ordered"
   return base
endif
delete /var bbb
!
! Load transposed image
let old_name 'name'
let old_type 'type'
let name 'cube%dir%tmp'"/"'true%name'
let type 'new_type'
message w pv "Name set to "'name'
message w pv "Type set to "'type'
@ l-old-load.cube aaa1
let name 'old_name'
let type 'old_type'
message w pv "Name reset to "'name'
message w pv "Type reset to "'type'
!
! Define subcube to be plotted
@ l-old-vrange.cube aaa1 range true%vrange ! Signed velocity range
@ l-old-prange.cube aaa1 center size       ! Signed space ranges
@ l-old-sampling.cube aaa1                 ! Unsigned sampling (default is Nyquist)
!
! Define plot layout
@ l-old-layout.cube aaa1 "1d" 'true%prange%p[1,qaxis]' 'true%prange%p[2,qaxis]' 'true%sampling%p[qaxis]'
@ l-old-maxbox.cube "1d"
!
! Define size of plotted diagram
greg2\rgdata aaa1 /variable
if (pv_kind.eq."vp") then
   greg1\limits true%vrange%v[1] true%vrange%v[2] true%prange%r[1,paxis] true%prange%r[2,paxis]
else
   greg1\limits true%prange%r[1,paxis] true%prange%r[2,paxis] true%vrange%v[1] true%vrange%v[2]
endif
!
! Define default marking scheme
if (mark.eq."*") then
   let true%mark "offset"
else
   let true%mark 'mark' /lower
endif
!
! Plot header and wedge. It must come after definition of true%first[1], true%last[1] and true%step[1]
@ l-old-box-setup.cube aaa1 image ! Define box setup (position, labels, text of legends)
@ l-old-level.cube aaa1 ! Contour levels
@ l-old-scale.cube aaa1 ! Intensity scale
@ l-old-header.cube aaa1
if (do%header) then
   @ l-old-wedge.cube
endif
!
! Define box characteristics
set\panel /default /aspect 1.5 /fixed trc /inter 0 /position plot /nxy 'true%nbox'
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 on window
for ibox 1 to true%nbox
   ! Define plane corresponding to window 'ibox'
   let iplane true%last[1]-true%step[1]*(ibox-1)
   ! Define current box position and create corresponding directory
   ! or directly go to this directory
   gtvl\change directory 'true%directory'
   set\panel 'ibox'
   gtvl\create directory p'ibox'
   gtvl\change directory p'ibox'
   ! Load plane 'iplane'.
   if (aaa1%ndim.le.2) then
      greg2\rgdata aaa1 /variable
   else if (aaa1%ndim.eq.3) then
      greg2\rgdata aaa1[iplane] /variable
   else
      greg2\rgdata aaa1[iplane,1] /variable
   endif
   ! Plot plane 'iplane'.
   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
      greg2\rgmap quiet /abs 1.0
   endif
   if (do%cross) then
      if (pv_kind.eq."pv") then
         greg1\draw relo user_xmin vsys /user
         greg1\draw line user_xmax vsys /user
         greg1\draw relo 0 user_ymin /user
         greg1\draw line 0 user_ymax /user
      else
         greg1\draw relo user_xmin 0 /user
         greg1\draw line user_xmax 0 /user
         greg1\draw relo vsys user_ymin /user
         greg1\draw line vsys user_ymax /user
      endif
   endif
next
!
! Draw box, mark
define double factor
if (do%scoord.eq."*") then
   let factor 1.0/sec
else if (do%scoord.eq."deg") then
   let factor 180/pi
else if (do%scoord.eq."min") then
   let factor 30/pi
else if (do%scoord.eq."sec") then
   let factor 1.0/sec
endif
for ibox 1 to true%nbox
   ! Define plane corresponding to window 'ibox'
   let iplane true%last[1]-true%step[1]*(ibox-1)
   ! Go to directory.
   gtvl\change directory 'true%directory'<p'ibox'
   ! 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 (aaa1%convert[2,3]+(iplane-aaa1%convert[1,3])*aaa1%convert[3,3])*factor
      if (true%mark.eq."plane") then
         let cmark[1] 'iplane'
      else if (true%mark.eq."offset") then
         let cmark[1] '0.01*nint(100*xmark)'
      else if (true%mark.eq."both") then
         let cmark[1] '0.01*nint(100*xmark)'
         let cmark[2] 'iplane'
      else
         message e pv "Unknown mark type: "'mark'
         return base
      endif
      greg1\draw text 0.2 -0.1 'cmark[1]' 3 /box 7
      if (true%mark.eq."both") then
         greg1\draw text -0.2 -0.1 'cmark[2]' 1 /box 9
      endif
   endif
   ! Plot box without labels
   @ l-old-box.cube no aaa1
next
!
! Plot x and y axis labels
gtvl\change directory 'true%directory'
@ l-old-legend.cube aaa1
!
! Reset
greg1\set expand 1.0
!
! Go to root directory
gtvl\change directory 'true%directory'
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
