!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure 3view-help
  say " "
  say "Authors: J.Pety & S.Guilloteau (2003)"
  say "Actions:"
  say "   Press E key: EXIT loop"
  say "   Press H key: HELP display"
  say "   Press Q key: QUIT loop"
  say "   Press V key: Velocity movie"
  say "   Press X key: X-axis movie"
  say "   Press Y key: Y-axis movie"
  say " "
end procedure 3view-help
!
begin procedure vmovie
  !
  define integer old_iv
  let old_iv iv
  let do_movie yes
  gtvl\change directory
  gtvl\change directory vy
  define integer ivfirst ivlast
  let ivfirst max((user_xmin-&1%convert[2,3])/&1%convert[3,3]+&1%convert[1,3],1)
  let ivlast  min((user_xmax-&1%convert[2,3])/&1%convert[3,3]+&1%convert[1,3],&1%dim[3])
  for i ivfirst to ivlast
    let iv i
    @ re_3view &1
!    sic wait 1
  next
  let do_movie no
  let iv old_iv
  @ re_3view &1
  !
end procedure vmovie
!
begin procedure xmovie
  !
  define integer old_ix
  let old_ix ix
  let do_movie yes
  gtvl\change directory
  gtvl\change directory xy
  define integer ixfirst ixlast
  let ixfirst max((user_xmin-&1%convert[2,1])/&1%convert[3,1]+&1%convert[1,1],1)
  let ixlast  min((user_xmax-&1%convert[2,1])/&1%convert[3,1]+&1%convert[1,1],&1%dim[1])
  for i ixfirst to ixlast
    let ix i
    @ re_3view &1
!    sic wait 1
  next
  let do_movie no
  let ix old_ix
  @ re_3view &1
  !
end procedure xmovie
!
begin procedure ymovie
  !
  define integer old_iy
  let old_iy iy
  let do_movie yes
  gtvl\change directory
  gtvl\change directory xy
  define integer iyfirst iylast
  let iyfirst max((user_ymin-&1%convert[2,2])/&1%convert[3,2]+&1%convert[1,2],1)
  let iylast  min((user_ymax-&1%convert[2,2])/&1%convert[3,2]+&1%convert[1,2],&1%dim[1])
  for i iyfirst to iylast
    let iy i
    @ re_3view &1
!    sic wait 1
  next
  let do_movie no
  let iy old_iy
  @ re_3view &1
  !
end procedure ymovie
!
! p_3view.graphic
!
! Called by
!
! GO 3VIEW
!
if (name.eq." ") then
  let name
endif
if (type.eq." ") then
  let type
endif
!
if (.not.exist(first_3view)) then
  define integer ix iy iv /global
  define logical first_3view do_movie /global
  let first_3view yes
  let do_movie no
endif
if (.not.exist(a__date)) then
  define integer a__date /global
  define char a__name*256 a__type*16 /global
endif
if (.not.exist(boxes)) then
  define real boxes[4,4] /global
  define char cbox*8[5] /global
endif
if (.not.exist(vlsr)) then
  define real vlsr /global
endif
let cbox "xy" "vy" "xv" "spectrum" "outside"
if (exist(ulimit)) then
  delete /var ulimit
endif
define double ulimit[2,3] /global
!
@ p_load.greg aaa1
@ p_scale.greg aaa1
@ p_level.greg aaa1
greg2\rgdata aaa1 /var
if size[1].eq.0 then
  greg1\limits /rg
  let ulimit[1] user_xmin user_xmax
  let ulimit[2] user_ymin user_ymax
else if (size[1].gt.0) then
  greg1\limits center[1]+size[1]|2 center[1]-size[1]|2 center[2]-size[1]|2 center[2]+size[1]|2 seconds
  let ulimit[1] '(center[1]+size[1]|2)*sec' '(center[1]-size[1]|2)*sec'
  let ulimit[2] '(center[2]-size[1]|2)*sec' '(center[2]+size[1]|2)*sec'
else
  define real lim[4]
  symb alim 'box_limits'
  let lim 'alim'
  let lim lim*sec
  let ulimit[1] lim[1] lim[2]
  let ulimit[2] lim[3] lim[4]
  dele /var lim
endif
!
let ulimit[3]   (0.5-aaa1%convert[1,3])*aaa1%convert[3,3]+aaa1%convert[2,3] -
  (aaa1%dim[3]+0.5-aaa1%convert[1,3])*aaa1%convert[3,3]+aaa1%convert[2,3]
if (ulimit[1,3].gt.ulimit[2,3]) then
   let u ulimit[1,3] /new real
   let ulimit[1,3] ulimit[2,3]
   let ulimit[2,3] u
   dele /var u
endif
!
if (exist(range)) then
  if (range[1].ne.0.or.range[2].ne.0) then
   let ulimit[1,3] max(ulimit[1,3],range[1])
   let ulimit[2,3] min(ulimit[2,3],range[2])
  endif
endif
!
if (.not.first_3view) then
  @ re_3view aaa1
  @ loop_3view aaa1
  return
else
  let ix '0.5*aaa1%dim[1]'
  let iy '0.5*aaa1%dim[2]'
  let iv '0.5*aaa1%dim[3]'
endif
!
! Reset full image
!
begin procedure 3view_sub_box
  define real views[4]  thebox[4]
  define integer ibox
  let boxes[1] 4. 14. 1 11
  let boxes[2] 16. 26. 1 11
  let boxes[3] 4. 14. 11 21
  let boxes[4] 16. 26.  11 21
  let views 0.05 0.95 0.075 0.975
  !
  let ibox &1
  let thebox[1] boxes[1,ibox]+views[1]*(boxes[2,ibox]-boxes[1,ibox])
  let thebox[2] boxes[1,ibox]+views[2]*(boxes[2,ibox]-boxes[1,ibox])
  let thebox[3] boxes[3,ibox]+views[3]*(boxes[4,ibox]-boxes[3,ibox])
  let thebox[4] boxes[3,ibox]+views[4]*(boxes[4,ibox]-boxes[3,ibox])
  greg1\set box thebox[1] thebox[2] thebox[3] thebox[4]
end procedure 3view_sub_box
!
begin procedure crosses
  !
  define real xpos ypos vpos
  let xpos ((ix-&1%convert[1,1])*&1%convert[3,1]+&1%convert[2,1])
  let ypos ((iy-&1%convert[1,2])*&1%convert[3,2]+&1%convert[2,2])
  let vpos ((iv-&1%convert[1,3])*&1%convert[3,3]+&1%convert[2,3])
  !
  gtvl\change directory
  gtvl\change directory xy
  greg1\draw relo 1*sec 0 /user
  greg1\draw line -1*sec 0 /user
  greg1\draw relo 0 1*sec /user
  greg1\draw line 0 -1*sec /user
  greg1\draw relo xpos user_ymin /user
  greg1\draw line xpos user_ymax /user
  greg1\draw relo user_xmin ypos /user
  greg1\draw line user_xmax ypos /user
  !
  gtvl\change directory
  gtvl\change directory vy
  greg1\draw relo vpos user_ymin /user
  greg1\draw line vpos user_ymax /user
  if (vlsr.ne.0.0) then
     greg1\draw relo vlsr -1*sec /user
     greg1\draw line vlsr +1*sec /user
     greg1\draw relo vlsr-0.05*(user_xmax-user_xmin) 0 /user
     greg1\draw line vlsr+0.05*(user_xmax-user_xmin) 0 /user
  endif
  gtvl\change directory
  gtvl\change directory xv
  greg1\draw relo user_xmin vpos /user
  greg1\draw line user_xmax vpos /user
  if (vlsr.ne.0.0) then
     greg1\draw relo -1*sec vlsr /user
     greg1\draw line +1*sec vlsr /user
     greg1\draw relo  0 vlsr-0.05*(user_ymax-user_ymin) /user
     greg1\draw line  0 vlsr+0.05*(user_ymax-user_ymin) /user
  endif
  !
end procedure crosses
!
begin procedure show_plot
  if (do_bit) then
     greg2\plot /scal lin &1%min &1%max
  endif
  if do_grey then
     greg2\rgmap quiet /grey /abs
     greg2\rgmap quiet /abs
  else if do_contour then
     greg2\rgmap quiet /abs
  endif
end procedure show_plot
!
begin procedure re_3view
  !
  ! RE_3VIEW is called by GO 3VIEW after the first time instead of P_VIEW
  !
  gtvl\change directory <greg
  if (.not.do_movie) then
    gtvl\clear directory
  endif
  !
  define integer nx ny nv ibox
  let nx &1%dim[1]
  let ny &1%dim[2]
  let nv &1%dim[3]
  define structure xy
  define structure vy
  define structure xv
  define real xy%data[nx,ny] yv[ny,nv] vy%data[nv,ny] xv%data[nx,nv] 
  define double xy%convert[3,2] vy%convert[3,2] xv%convert[3,2]
  define double numb xspectre[nv] yspectre[nv]
  !
  ! Plot the current plane
  gtvl\change directory <greg
  let ibox 1
  if (.not.do_movie) then
    gtvl\create directory 'cbox[ibox]'
  endif
  gtvl\change directory 'cbox[ibox]'
  @ 3view_sub_box 'ibox'
  let xy%data aaa1[iv]
  let xy%convert[1] &1%convert[1]
  let xy%convert[2] &1%convert[2]
  greg1\limits ulimit[1,1] ulimit[2,1] ulimit[1,2] ulimit[2,2]
  !
  !
  greg2\rgdata xy /var
  @ show_plot aaa1
  !
  greg1\box /unit sec ! /abs
  let numb 0.001*nint(1000*((iv-&1%convert[1,3])*&1%convert[3,3]+&1%convert[2,3]))
  greg1\draw text 1 -1 "V\\dLSR\\u "'numb' 6 0 /CHAR 7
  greg1\label "R.A. Offset (`)" /X
  greg1\label "Dec. Offset (`)" /Y
  !
  !
  ! Plot the YV cut
  gtvl\change directory <greg
  let ibox 2
  if (.not.do_movie) then
     gtvl\create directory 'cbox[ibox]'
  endif
  gtvl\change directory 'cbox[ibox]'
  @ 3view_sub_box 'ibox'
  !
  let yv aaa1[ix,,]  ! Automatic transposition
  greg1\limits ulimit[1,3] ulimit[2,3] ulimit[1,2] ulimit[2,2]
  !
  vector\transpose yv vy%data 21
  let vy%convert[1] &1%convert[3]
  let vy%convert[2] &1%convert[2]
  !
  greg2\rgdata vy /var
  @ show_plot &1
  !
  greg1\box n n n
  greg1\limits ulimit[1,3] ulimit[2,3] ulimit[1,2]/sec ulimit[2,2]/sec
  greg1\axis xl
  greg1\axis xu
  greg1\axis yl /label N
  greg1\axis yr /label O
  greg1\limits ulimit[1,3] ulimit[2,3] ulimit[1,2] ulimit[2,2]
  greg1\label "Velocity (km/s)" /X
  !
  let numb 0.001*nint(1000*((ix-&1%convert[1,1])*&1%convert[3,1]+&1%convert[2,1])/sec)
  greg1\draw text 1 -1 "\gdRA "'numb'"`" 6 0 /CHAR 7
  if (do_bit) then
    greg2\wedge left
    greg1\draw text -3.5 0 "Flux (Jy/beam)" 5 90 /char 4
  endif
  !
  ! Plot the XV cut
  gtvl\change directory <greg
  let ibox 3
  if (.not.do_movie) then
     gtvl\create directory 'cbox[ibox]'
  endif
  gtvl\change directory 'cbox[ibox]'
  @ 3view_sub_box 'ibox'
  !
  let XV%data &1[,iy,]
  let xv%convert[1] &1%convert[1]
  let xv%convert[2] &1%convert[3]
  greg1\limits ulimit[1,1] ulimit[2,1] ulimit[1,3] ulimit[2,3]
  !
  greg2\rgdata xv /var
  @ show_plot &1
  !
  greg1\limits ulimit[1,1]/sec ulimit[2,1]/sec ulimit[1,3] ulimit[2,3]
  greg1\box n o
  greg1\axis xl
  greg1\limits ulimit[1,1] ulimit[2,1] ulimit[1,3] ulimit[2,3]
  greg1\label "Velocity (km/s)" /Y
  let numb 0.001*nint(1000*((iy-&1%convert[1,2])*&1%convert[3,2]+&1%convert[2,2])/sec)
  greg1\draw text 1 -1 "\gdDec "'numb'"`" 6 0 /CHAR 7
  !
  ! Display the current spectrum
  gtvl\change dir <greg
  let ibox 4
  if (.not.do_movie) then
     gtvl\create directory 'cbox[ibox]'
  endif
  gtvl\change directory 'cbox[ibox]'
  @ 3view_sub_box 'ibox'
  !
  let xspectre[is] (is-&1%convert[1,3])*&1%convert[3,3]+&1%convert[2,3]
  let yspectre &1[ix,iy,]
  greg1\limits /var xspectre yspectre
  box
  !! greg1\label "Velocity (km/s)" /X
  greg1\draw text -3.5 0 "Flux (Jy/beam)" 5 90 /char 4
  histo xspectre yspectre
  !
  !
  @ crosses &1
  !
  gtvl\change dir <greg
  !
end procedure re_3view
!  
begin procedure loop_3view
 define integer ibox
 for /while .true.
  gtvl\change directory <greg
  greg1\draw relo
  if (cursor_code.eq."Q").or.(cursor_code.eq."E") then
     return
  else if (cursor_code.eq."E") then
     return all
  else if (cursor_code.eq."H") then
!     @ p3view-help
     @ 3view-help
  else if (cursor_code.eq."V") then
     @ vmovie &1
  else if (cursor_code.eq."X") then
     @ xmovie &1
  else if (cursor_code.eq."Y") then
     @ ymovie &1
  else
     let ibox 0
     for i 1 to 4
       if ((x_pen.ge.boxes[1,i]).and.(x_pen.le.boxes[2,i])) then
          if ((y_pen.ge.boxes[3,i]).and.(y_pen.le.boxes[4,i])) then
             let ibox i
          endif
       endif
     next
     !
     ! Get the coordinates according to which box
     !
     if (ibox.ge.1.and.ibox.le.3) then
       gtvl\change directory <greg<'cbox[ibox]'
       greg1\draw relo * *
       if (ibox.eq.1) then  ! Cuts in X and Y
        ix = (use_curs[1]-&1%convert[2,1])/&1%convert[3,1]+&1%convert[1,1]
        iy = (use_curs[2]-&1%convert[2,2])/&1%convert[3,2]+&1%convert[1,2]
       else if (ibox.eq.2) then  ! Cuts in V and Y
        iv = (use_curs[1]-&1%convert[2,3])/&1%convert[3,3]+&1%convert[1,3]
        iy = (use_curs[2]-&1%convert[2,2])/&1%convert[3,2]+&1%convert[1,2]
       else if (ibox.eq.3) then  ! Cuts in X and V
        ix = (use_curs[1]-&1%convert[2,1])/&1%convert[3,1]+&1%convert[1,1]
        iv = (use_curs[2]-&1%convert[2,3])/&1%convert[3,3]+&1%convert[1,3]
       end if
       gtvl\change directory <greg
       @ re_3view aaa1
     endif
  endif
 next
end procedure loop_3view
!
@ 3view-help
@ re_3view aaa1
let first_3view no
@ loop_3view aaa1
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
