!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ q-old-view
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
on error return base
!
if .not.exist(view%first_time) then
   if exist(view) then
      delete /var view
   endif
   define structure view /global
   define logical view%first_time /global
   !
   define structure view%poly /global
   !
   define structure view%comp /global
   define logical view%comp%spec view%comp%mean view%comp%area view%comp%extr /global
   !
   define integer view%iprint view%imap1 view%imap2 view%ispe1 view%ispe2 /global
   define integer view%i_chan view%i_spec view%i_area view%i_mean view%i_outs view%i_pane /global
   !
   define character view%panels*8[5] view%coord*3[2] view%scoord*3[3] view%vcoord*3[3] /global
   !
   define double view%scale[2] view%arange[2] view%bounds[4,4] /global
   !
   let view%first_time yes
   let view%panels "channel" "spectrum" "area" "mean" "outside"
   let view%i_chan 1
   let view%i_spec 2
   let view%i_area 3
   let view%i_mean 4
   let view%i_outs 5
   let view%i_pane 5
   !
   let view%coord  "abs" "rel"
   let view%scoord "*" "deg" "min" !"pix"
   let view%vcoord "vel" "fre" "pix"
   let view%imap1 2
   let view%imap2 1
   let view%ispe1 1
   let view%ispe2 1
   let view%iprint 0
   !
   let view%comp%spec yes
   let view%comp%mean yes
   let view%comp%area yes
   let view%comp%extr yes
   !
   define logical view%slice_mode view%thres_mode /global
   let view%slice_mode no
   let view%thres_mode no
else
   let view%iprint 0
endif
!
!!$if ("&1none".ne."none") then
!!$  let do%write &1
!!$endif
!!$if (do%write) then
!!$  define character oldname*256
!!$  let oldname 'name'
!!$  let name 'oldname'"-mod"
!!$  sic copy 'oldname'"."'type' 'name'"."'type'
!!$  @ l-old-load.cube aaa1 write
!!$  let name 'oldname'
!!$  @ l-old-load.cube b
!!$  delete /var oldname
!!$else
!!$endif
!
go\oldload 'name' /syntax hgdf /in aaa1
!
! Sanity checks
!!$define character dummy*32
!!$let dummy 'aaa1%unit'"none"
!!$if (dummy.eq."none") then
!!$   message i view "Please input data cube unit"
!!$   let aaa1%unit
!!$endif
!!$delete /var dummy
if (aaa1%ndim.ne.3) then
   message w view "Input image is not a 3D cube => Default to GO NICE"
   @ p-old-nice.cube
   return
endif
if (aaa1%dim[3].eq.1) then
   message w view "Input image has a degenerated 3rd axis => Default to GO NICE"
   @ p-old-nice.cube
   return
endif
if (aaa1%f_axis.ne.3) then
   message e view "3rd axis is not velocity"
   return base
endif
if (aaa1%convert[3,3].eq.0.0) then
   message e view "Velocity increment is equal to 0.0"
   return base
endif
!
if exist(aaa1_velocities) then
   delete /var aaa1_velocities
endif
define double aaa1_velocities['aaa1%dim[3]'] /global
let aaa1_velocities[i] (i-aaa1%convert[1,3])*aaa1%convert[3,3]+aaa1%convert[2,3]
!
if ((page_x.ne.30).or.(page_y.ne.21)) then
   greg1\set plot landscape
   message w view "Ensure a landscape plot page"
endif
!
let view%first_time yes
if (.not.view%first_time) then
   let view%comp%extr yes
   let view%comp%mean yes
   let view%comp%area yes
   @ view-help
   @ view-view
   @ view-loop
   return
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! General purpose procedures
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-panel
  gtvl\change directory <greg
  gtvl\create directory 'view%panels[&1]'
  gtvl\change directory 'view%panels[&1]'
  !
  set\panel /default
  set\panel /aspect 'abs((true%prange%s[2,1]-true%prange%s[1,1])/(true%prange%s[2,2]-true%prange%s[1,2]))'
  if (&1.eq.view%i_chan) then
     set\panel /position 0.17 0.72 0.56 0.995
  else if (&1.eq.view%i_spec) then
     set\panel /position  0.86 1.30 0.56 0.995 /aspect 1.0
  else if (&1.eq.view%i_area) then
     set\panel /position 0.17 0.72 0.075 0.51
  else if (&1.eq.view%i_mean) then
     set\panel /position 0.86 1.30 0.075 0.51 /aspect 1.0
  endif
  set\panel 1 /nxy 1 1
  !
  ! Panel boundaries must be updated as the image aspect may have changed
  let view%bounds[&1] 'box_xmin' 'box_xmax' 'box_ymin' 'box_ymax'
end procedure view-panel
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-view
  ! Does not anymore change window position on purpose to ease comparison
  ! of two sessions
  gtvl\change directory <greg
  gtvl\clear
  !
  ! 1. Analyze input parameters
  !
  define double loc_range[2]
  !
  ! Working subcube
  @ l-old-prange.cube aaa1 center size
  @ l-old-vrange.cube aaa1 range true%vrange
  @ l-old-corner.cube aaa1
  ! Velocity window
  let loc_range min(true%vrange%v[1],true%vrange%v[2]) max(true%vrange%v[1],true%vrange%v[2])
  if ((do%win[1].ne.0).or.(do%win[2].ne.0)) then
     for iedge 1 to 2
        let do%win[iedge] min(max(do%win[iedge],loc_range[1]),loc_range[2])
     next
  else
     let do%win range
  endif
  @ l-old-vrange.cube aaa1 do%win true%win
  @ l-old-layout.cube aaa1 "1d" true%win%p[1] true%win%p[2] 1
  !
  ! Selected channel
  define double chan
  if (do%chan.eq.do%chan) then
     let chan do%chan
  else
     ! NaN => Default to systemic velocity
     let chan aaa1%veloff
  endif   
  if ((chan.lt.loc_range[1]).or.(loc_range[2].lt.chan)) then
     let chan 0.5*(loc_range[1]+loc_range[2])
  endif
  @ l-old-channel.cube aaa1 'chan' V
  ! Selected position
  for iaxis 1 to 2
     let loc_range min(true%prange%s[1,iaxis],true%prange%s[2,iaxis]) max(true%prange%s[1,iaxis],true%prange%s[2,iaxis])
     if ((do%pos[iaxis].lt.loc_range[1]).or.(loc_range[2].lt.do%pos[iaxis])) then
        let do%pos[iaxis] 0.5*(loc_range[1]+loc_range[2])
     endif
  next iaxis
  @ l-old-position.cube aaa1 do%pos true%pos
  !
  ! 2. Compute when needed
  !
  ! area%map and area%mask
  if (view%comp%area) then
     @ c-old-area.cube "inter"
     let view%comp%area no
  endif
  ! Subcube and area%map extrema
  if (view%comp%extr) then
     sic\compute view%scale[1] min aaa1['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]','true%blc[3]':'true%trc[3]']
     sic\compute view%scale[2] max aaa1['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]','true%blc[3]':'true%trc[3]']
     sic\compute area%map%min min area%map['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]']
     sic\compute area%map%max max area%map['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]']
     let view%comp%extr no
  endif
  ! Mean spectrum
  if (view%comp%mean) then
     if (view%thres_mode) then
        ! Threshold mode => Remove polygon and define threshold mask first
        ! Remove polygon (there is no easy way...)
        greg2\polygon /reset
        ! Define mask
        if exist(mask) then
           delete /var mask
        endif
        define integer mask /like aaa1[1] /global
        if exist(area%mask) then
           ! Recover area mask
           let mask area%mask
        else
           ! Everybody is good by default
           let mask 0
        endif
        ! Mark as additional bad pixels the ones outside the view%arange
        let mask area%chantot /where (area%map.lt.view%arange[1].or.view%arange[2].lt.area%map)
        ! Compute using only mask
        @ c-old-mean-spectrum.cube /mask mask 'area%chantot' /action "inter"
     else
        ! Restore polygon
        if exist(view%poly%xy) then
           greg2\polygon view%poly%xy /var
        endif
        ! Compute using area%mask and polygon
        @ c-old-mean-spectrum.cube /mask area%mask 'area%chantot' /action "inter"
     endif
     let view%comp%mean no
  endif
  ! Single spectrum
  if (view%comp%spec) then
     @ c-old-get-spectrum.cube "inter"
     let view%comp%spec no
  endif
  !
  ! 3. Plot
  !
  ! 3.1 Integrated area
  !
  @ view-panel 'view%i_area'
  ! Image
  greg1\limits true%prange%r[1,1] true%prange%r[2,1] true%prange%r[1,2] true%prange%r[2,2]
  greg2\rgdata area%map /var /subset 'true%blc[1]' 'true%blc[2]' 'true%trc[1]' 'true%trc[2]'
  greg2\plot area%map /scal lin area%map%min area%map%max
  ! Contours + Wedge
  if (view%thres_mode) then
     ! Contours
     greg2\levels 'view%arange[1]' 'view%arange[2]'
     greg2\rgmap quiet /abs 1.0
     ! Wedge
     greg2\wedge right /levels
  else
     ! Polygon if possible
     if (exist(poly%nxy)) then
        if (poly%nxy.ge.3) then
           greg2\polygon /plot
        endif
     endif
     ! Wedge
     greg2\wedge right
  endif
  ! Box + legend
  greg1\draw text 4 0 "Area ("'area%map%unit'")" 5 90 /char 6
  let do%lab%yp "*" "*"
  let do%leg%xt "*"
  let do%leg%yt "*"
  let do%leg%yp 0
  let do%coord  'view%coord[view%imap1]'
  let do%scoord 'view%scoord[view%imap2]'
  @ l-old-box-setup.cube area%map image
  @ l-old-box.cube label
  !
  ! 3.2 Selected velocity channel
  !
  @ view-panel 'view%i_chan'
  greg1\limits true%prange%r[1,1] true%prange%r[2,1] true%prange%r[1,2] true%prange%r[2,2]
  greg2\plot aaa1[true%chan%p] /scal lin view%scale[1] view%scale[2]
  greg2\wedge right
  greg1\draw text 4 0 "Channel "'true%chan%p'" ("'aaa1%unit'")" 5 90 /char 6
  let do%leg%xt " "
  let do%leg%yt " "
  @ l-old-box-setup.cube aaa1 image
  @ l-old-box.cube label
  !
  ! 3.3 Mean spectrum
  !
  @ view-panel 'view%i_mean'
  ! Setup layout
  let do%fill yes
  let do%coord  'view%coord[view%ispe1]'
  let do%vcoord 'view%vcoord[view%ispe2]'
  let do%lab%yp "n" "o"
  let do%leg%xt "*"
  let do%leg%yt "*"
  let do%leg%yp 19
  let mark "none"
!  let scale 0
  if .not.exist(mean_spectrum%wmin) then
     define integer mean_spectrum%wmin mean_spectrum%wmax /global
  endif
  if (true%step[1].gt.0) then
     let mean_spectrum%wmin true%first[1]-true%blc[3]+1
     let mean_spectrum%wmax  true%last[1]-true%blc[3]+1
  else
     let mean_spectrum%wmin  true%last[1]-true%blc[3]+1
     let mean_spectrum%wmax true%first[1]-true%blc[3]+1
  endif
  ! Plot
  @ l-old-plot-spectrum.cube mean_spectrum
  !
  ! 3.4 Spectrum corresponding to selected pixel
  !
  @ view-panel 'view%i_spec'
  ! Setup layout
  let do%fill no
  let do%leg%xt " "
  let do%leg%yt "*"
  let mark "*"
!  let scale view%scale
  ! Plot
  @ l-old-plot-spectrum.cube single_spectrum
  ! Draw lines to indicate the selected pixel
  gtvl\change directory <greg<'view%panels[view%i_spec]'
  greg1\draw relo user_xmin user_ymin /user
  gtvl\change directory <greg<'view%panels[view%i_chan]'
  greg1\draw line 'true%pos%r[1]' 'true%pos%r[2]' /user
  gtvl\change directory <greg<'view%panels[view%i_spec]'
  greg1\draw line user_xmin user_ymax /user
  ! Draw a vertical line to indicate selected velocity channel
  gtvl\change directory <greg<'view%panels[view%i_spec]'
  greg1\pencil 1
  greg1\draw relo true%chan%v true%scale[1] /user
  greg1\draw line true%chan%v true%scale[2] /user
  greg1\pencil 0
  !
  @ l-old-coord
  !
  ! Reset
  gtvl\change directory <greg
  greg1\draw relo 0 0
  ! *** JP This is a mess. I should not reset to the default value here
  ! *** JP but to the value before I changed them for the purpose of q-old-view.cube...
  let do%lab%yp "*" "*"
  let do%leg%xt "*"
  let do%leg%yt "*"
  let do%leg%yp 0
  let mark "*"
end procedure view-view
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-help
  say " "
  say "Position-independent actions:"
  say "   Press ? key: Display help"
  say "   Press E key: Exit loop"
  say "   Press H key: Hardcopy in ha subdirectory"
  say "   Press M key: Movie"
  say "   Press X key: Extract on disk current zoomed region"
  say "Position-dependent actions:"
  say "   Cursor on images:"
  say "      Left  click: Display spectrum at pointed position"
  say "      Right click: Define a polygon"
  say "      Press D key: COORDINATES toggled from absolute to relative and back"
  say "      Press C key: COORDINATES cycled between degrees, minutes, and seconds"
  say "      Press S key: SLICE definition (velocity-position) CURRENTLY NOT AVAILABLE"
  say "      Press Z key: ZOOM defined spatial region"
  say "      Press B key: BACK to full field of view"
  say "   Cursor on spectra:"
  say "      Left click in Selected   Spectrum: Display selected velocity channel"
  say "      Left click in Integrated Spectrum: Define velocity range"
  say "      Press D key: COORDINATES toggled from absolute to relative and back"
  say "      Press C key: COORDINATES cycled between velocity, frequency and channel"
  say "      Press Z key: ZOOM defined velocity region"
  say "      Press B key: BACK to full velocity range"
  say "   Cursor outside plots:"
  say "      Press D key: COORDINATES toggled from absolute to relative and back (all plots)"
  say "      Press B key: BACK to full velocity range AND full field of view"
  say " "
end procedure view-help
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-loop
  define logical loop
  let loop yes
  for /while loop
     gtvl\change directory <greg
     greg1\draw relo
     let view%i_pane view%i_outs
     for ipane 1 to 4
        if ((x_pen.ge.view%bounds[1,ipane]).and.(x_pen.le.view%bounds[2,ipane])) then
           if ((y_pen.ge.view%bounds[3,ipane]).and.(y_pen.le.view%bounds[4,ipane])) then
              let view%i_pane ipane
           endif
        endif
     next ipane
     !
     ! First global behavior, then position-dependant behavior
     !
!     exa cursor_code
     if (cursor_code.eq."X") then
        @ view-extract
     else if (cursor_code.eq."?") then
        @ view-help
     else if (cursor_code.eq."M") then
        @ view-movie
     else if (cursor_code.eq."H") then
        @ view-hard
     else if (cursor_code.eq."E") then
        let loop no ! Exit the loop
     else if ((view%i_pane.eq.view%i_area).or.(view%i_pane.eq.view%i_chan)) then
        if (cursor_code.eq."D") then
           let view%imap1 mod(view%imap1,2)+1
           @ view-view
        else if (cursor_code.eq."C") then
           let view%imap2 mod(view%imap2,3)+1
           @ view-view
        else if (cursor_code.eq."Z") then
           @ view-zoom position
        else if (cursor_code.eq."B") then
           @ view-unzoom position
        else if (cursor_code.eq."S") then
	   message w view "Slices are currently not available"
           ! if (view%slice_mode) then
           !    let view%slice_mode no
           ! else
           !    let view%slice_mode yes
           ! endif
           ! @ view-view
        else if (cursor_code.eq."L") then
           if (view%thres_mode) then
              let view%thres_mode no
           else
              let view%thres_mode yes
           endif
           let view%comp%mean yes
           @ view-view
        else if (cursor_code.eq."*") then ! Right click
           if (view%slice_mode) then
              @ view-slice "*"
           else if (view%thres_mode) then
              @ view-mean "thre"
           else
              @ view-mean "poly"
           endif
        else if (cursor_code.eq."R") then ! Left click
           if (view%slice_mode) then
              @ view-slice "R"
           else
              @ view-spectrum
           endif
        else if (cursor_code.eq."&") then ! Middle click
           if (view%slice_mode) then
              @ view-slice "&"
           endif
        endif
     else if ((view%i_pane.eq.view%i_mean).or.(view%i_pane.eq.view%i_spec)) then
        if (cursor_code.eq."D") then
           let view%ispe1 mod(view%ispe1,2)+1
           @ view-view
        else if (cursor_code.eq."C") then
           let view%ispe2 mod(view%ispe2,3)+1
           @ view-view
        else if (cursor_code.eq."Z") then
           @ view-zoom velocity
        else if (cursor_code.eq."B") then
           @ view-unzoom velocity
        else if (cursor_code.eq."N") then
           @ view-movie n
        else if (cursor_code.eq."P") then
           @ view-movie p
        else if (cursor_code.eq."R") then
           if (view%i_pane.eq.view%i_mean) then
              @ view-area
           else
              @ view-channel
           endif
        endif
     else if (view%i_pane.eq.view%i_outs) then
        if (cursor_code.eq."B") then
           @ view-unzoom both
        else if (cursor_code.eq."D") then
           let view%ispe1 mod(view%ispe1,2)+1
           let view%imap1 mod(view%imap1,2)+1
           @ view-view
        endif
     else
        message w view "Should not be here: view%i_pane = "'view%i_pane'
     endif
  next
end procedure view-loop
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Specific action procedures
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-channel
  gtvl\change directory <greg<'view%panels[view%i_pane]'
  greg1\draw relo * *
  let do%chan use_curs[1]
  @ view-view
end procedure view-channel
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-mean
  gtvl\change directory <greg<'view%panels[view%i_pane]'
  greg1\draw relo * *
  if ("&1".eq."poly") then
     say "Define polygon"
     on error return
     greg2\polygon
     ! Save defined polygon
     if exist(view%poly%xy) then
        delete /var view%poly%xy
     endif
     define double view%poly%xy[poly%nxy,2] /global
     let view%poly%xy[1] poly%x
     let view%poly%xy[2] poly%y
  else if ("&1".eq."thre") then
     if (user_ymin.ne.user_ymax) then
        let view%arange[1] use_curs[2]
        say "Set upper limit"
        greg1\draw relo
        let view%arange[2] use_curs[2]
        let view%arange area%map%min+(area%map%max-area%map%min)*(view%arange-user_ymin)/(user_ymax-user_ymin)
        if (view%arange[1].gt.view%arange[2]) then
           define real dummy
           let dummy view%arange[2]
           let view%arange[2] view%arange[1]
           let view%arange[1] dummy
        endif
     else
        let view%arange area%map%min
     endif
  else
     return
  endif
  let view%comp%mean yes
  @ view-view
end procedure view-mean
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-area
  gtvl\change directory <greg<mean
  greg1\draw relo * *
  let do%win[1] use_curs[1]
  say "Set upper limit"
  greg1\draw relo
  let do%win[2] use_curs[1]
  let view%comp%extr yes
  let view%comp%area yes
  if (view%thres_mode) then
     let view%comp%spec yes
  endif
  @ view-view
end procedure view-area
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-zoom
  ! Either position or velocity zoom
  define character kind*8
  let kind "&1" /lower
  !
  if (kind.eq."velocity") then
     ! Define zoom range
     gtvl\change directory <greg<'view%panels[view%i_pane]'
     say "Select lower limit"
     greg1\draw relo
     let range[1] use_curs[1]
     say "Set upper limit"
     greg1\draw relo
     let range[2] use_curs[1]
  else if (kind.eq."position") then
     ! Define zoom area
     gtvl\change directory <greg<'view%panels[view%i_pane]'
     define double blc[2] trc[2]
     say "Select top, left corner"
     greg1\draw relo
     let trc use_curs
     say "Select bottom, right corner"
     greg1\draw relo
     let blc use_curs
     let size   (trc-blc)/sec
     let center (trc+blc)/(2*sec)
     ! Visualize zoom area
     ! 1. Define space ranges associated to center and size
     @ l-old-prange.cube aaa1 center size
     ! 2. Define associated polygon
     define real xygons[4,2]
     let xygons[1,1:2] 'true%prange%r[2,1]' 'true%prange%r[2,2]'
     let xygons[2,1:2] 'true%prange%r[2,1]' 'true%prange%r[1,2]'
     let xygons[3,1:2] 'true%prange%r[1,1]' 'true%prange%r[1,2]'
     let xygons[4,1:2] 'true%prange%r[1,1]' 'true%prange%r[2,2]'
     ! 3. Load and plot
     gtvl\change directory <greg<channel
     greg2\polygon xygons /plot /var
     gtvl\change directory <greg<area
     greg2\polygon xygons /plot /var
     delete /var xygons
     sic wait 1
     ! Remove polygon
     if exist(view%poly%xy) then
        delete /var view%poly%xy
     endif
     define double view%poly%xy[1,2] /global
  endif
  let view%comp%spec yes
  let view%comp%mean yes
  let view%comp%extr yes
  @ view-view
end procedure view-zoom
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-unzoom
  ! 3 possibilities: unzoom velocity, position or both
  define character kind*8
  let kind "&1" /lower
  !
  if ((kind.eq."velocity").or.(kind.eq."both")) then
     let range 0
     let view%comp%area yes
  endif
  if ((kind.eq."position").or.(kind.eq."both")) then
     let size 0
     let center 0
  endif
  let view%comp%spec yes
  let view%comp%mean yes
  let view%comp%extr yes
  @ view-view
end procedure view-unzoom
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-spectrum
  gtvl\change directory <greg<'view%panels[view%i_pane]'
  greg1\draw relo * *
  let do%pos use_curs/sec
  let view%comp%spec yes
  @ view-view
end procedure view-spectrum
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-slice
  gtvl\change directory <greg<'view%panels[view%i_pane]'
  greg1\draw relo * *
  if ("&1".eq."R") then ! Left click
     let do%slice[1] use_curs/sec
     greg1\draw relo do%slice[1,1] do%slice[2,1] /user sec
     greg1\draw line do%slice[1,2] do%slice[2,2] /user sec
  else if ("&1".eq."*") then ! Right click
     let do%slice[2] use_curs/sec
     greg1\draw relo do%slice[1,1] do%slice[2,1] /user sec
     greg1\draw line do%slice[1,2] do%slice[2,2] /user sec
  else ! Middle click
     let do%slice[1] use_curs/sec
     greg1\draw relo do%slice[1,1] do%slice[2,1] /user sec
     greg1\draw line
     let do%slice[2] use_curs/sec
  endif
  !
  gtvl\gtv search <slice
  if (.not.gtv%exist) then
     gtvl\create dir <slice /size 20 20 /pixel 380 380
  endif
  gtvl\change dir <slice
  gtvl\clear dir
  gtvl\clear alpha
  !
  let do%lab%yp "*" "*"
  let do%leg%yp -3
  let do%leg%xt "*"
  let do%leg%yt "*"
  !
!  let scale view%scale
  @ c-old-slice.cube "inter"
  define double slice[2,2]
  let slice 0.01*nint(100*true%slice%s)
  greg1\draw text 0 1 "Slice from ("'slice[1,1]'"`,"'slice[2,1]'"`) to ("'slice[1,2]'"`,"'slice[2,2]'"`)" 5 0 /CHAR 8
  !
  gtvl\change dir <greg
end procedure view-slice
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-movie
  define character action*1
  let action "&1" /lower
  define integer newchan
  if (action.eq."n") then
     let newchan true%chan%p+1
  else if (action.eq."p") then
     let newchan true%chan%p-1
  endif
  ! Loop on boundaries:
  if (newchan.gt.true%trc[3]) then
     let newchan true%blc[3]
  else if (newchan.lt.true%blc[3]) then
     let newchan true%trc[3]
  endif
  @ l-old-channel.cube aaa1 'newchan' C
  !
  gtvl\change directory <greg<spectrum
  gtvl\clear segment
  !
  gtvl\change directory <greg<channel
  gtvl\clear dir
  greg1\limits true%prange%r[1,1] true%prange%r[2,1] true%prange%r[1,2] true%prange%r[2,2]
  greg2\plot aaa1[true%chan%p] /scal lin view%scale[1] view%scale[2]
  greg2\wedge right
  greg1\draw text 4 0 "Channel "'true%chan%p'" ("'aaa1%unit'")" 5 90 /char 6
  let do%leg%xt " "
  let do%leg%yt " "
  let do%lab%yp "*" "*"
  let do%coord "rel" !*** for diffuse co... JP
  @ l-old-box-setup.cube aaa1 image
  @ l-old-box.cube label
  !
  gtvl\change directory <greg<spectrum
  greg1\pencil 1
  greg1\draw relo aaa1_velocities[true%chan%p] true%scale[1] /user
  greg1\draw line aaa1_velocities[true%chan%p] true%scale[2] /user
  greg1\pencil 0
  !
  @ l-old-coord  ! Because this procedure changes the current channel
  !
  gtvl\change directory <greg
  !
!!$  define integer ilast jlast
!!$  define real rwait
!!$  let rwait 0
!!$  let ilast true%blc[3]
!!$  let jlast ilast-true%blc[3]+1
!!$  for iplane true%blc[3] to true%trc[3]
!!$     change directory <greg<channel
!!$     greg2\plot aaa1[iplane]
!!$     change directory <greg<spectrum
!!$     greg1\pencil /col 8
!!$     greg1\draw relo velocities[ilast] user_ymin /user
!!$     greg1\draw line velocities[ilast] cspectre[jlast] /user
!!$     greg1\pencil /col 0
!!$     let ilast iplane
!!$     let jlast ilast-true%blc[3]+1
!!$     greg1\draw relo velocities[ilast] user_ymin /user
!!$     greg1\draw line velocities[ilast] cspectre[jlast] /user
!!$     if (rwait.ge.0.01) then
!!$        sic wait rwait
!!$     endif
!!$  next iplane
!!$  greg1\pencil /col 8
!!$  greg1\draw relo velocities[ilast] user_ymin /user
!!$  greg1\draw line velocities[ilast] cspectre[jlast] /user
!!$  greg1\pencil /col 0
!!$  !
!!$  ! Restore the defined channel
!!$  !
!!$  if (true%chan%p.ne.0) then
!!$     change directory <greg<channel
!!$     sic wait rwait
!!$     greg2\plot aaa1[true%chan%p]
!!$     if (map_rcoord) then
!!$        greg1\box /unit sec
!!$     else
!!$        greg1\box /abs
!!$     endif
!!$  endif
!!$  change directory <greg
end procedure view-movie
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-extract
  @ c-old-extract.cube aaa1
end procedure view-extract
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure view-hard
  if .not.file(ha) then
     sic mkdir ha
  endif
  let view%iprint view%iprint+1
  define character myname*256
  sic parse 'name' myname
  let myname "ha/"'myname'"-"'type'"-"'view%iprint'
  gtvl\hardcopy 'myname' /dev epdf
  say " "
  say "go view: PDF file available as "'myname'".pdf"
  say " "
end procedure view-hard
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
let view%first_time no
let view%comp%extr yes
let view%comp%mean yes
let view%comp%area yes
let view%comp%spec yes
if (do%starthelp) then
   @ view-help
endif
g\set expand 1.0
@ view-view
if (do%loop) then
   @ view-loop
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
