!------------------------------------------------------------------------
! SPECTRE.GRAPHIC
! Procedure to plot spectra in box for a spectral line data cube.
! Global variables defined in DEFINE.GRAPHIC
! Calls
!	P_HEADER.GREG 	(to plot header information)
! 	P_LOAD.GREG	(to load image)
! and 	P_BOX.GREG	(to plot a box in the header with limits)
! S.Guilloteau 18-jun-1991
!----------------------------------------------------------------------
on error
if (name.eq." ") then
  say "Please enter map file name (without extension) "
  let name
endif
if (type.eq." ") then
  say "Please enter map file extension "
  let type
endif
gtvl\destroy all
def real x2 dx y2 dy xpixel[3] ypixel[3]
def double xmark ymark
def integer m n ix jy
def character cmark*24
!
define header ddd 'name'.'type' read
if (ddd%f_axis.eq.3) then
  message w spectre "Transposing "'name'.'type'" to "'name'".vxy for display"
  vector\transpose 'name'.'type' 'name'.vxy 312
  let type vxy
  message w spectre "Type set to vxy"
endif
dele /var ddd
!
@ p_load.greg spe
@ p_header.greg spe
on error
!
if (spe%f_axis.ne.1) then
  say "Warning!... First axis of Image/Cube is not the Frequency Axis"
  say "Continuing...(may be image header is inaccurate?)"
endif
let m spe%dim[1]
define real xspec[m]
let xspec[i] (i-spe%convert[1,1])*spe%convert[3,1]+spe%convert[2,1]
if (box_limits.eq." ") then
  greg1\limits (-spe%convert[1,1])*spe%convert[3,1]+spe%convert[2,1] (m+1-spe%convert[1,1])*spe%convert[3,1]+spe%convert[2,1] spe%min-0.05*(spe%max-spe%min) spe%max+0.05*(spe%max-spe%min)
else
  symbol alimits 'box_limits'
  greg1\limits 'alimits'
endif
let x2 box_xmax
let y2 box_ymax
!
let xpixel xpix
if (xpixel[1].eq.0) then
  if (spe%dim[2]*spe%dim[3].le.32*32) then
	let xpixel[1] 1
  else
      let xpixel[1] spe%dim[2]/4
  endif
endif
if (xpixel[2].eq.0) then
  if (spe%dim[2]*spe%dim[3].le.32*32) then
     let xpixel[2] spe%dim[2]
  else
     let xpixel[2] 3*spe%dim[2]/4
  endif
endif
if (xpixel[3].eq.0) then
  if (spe%major.ne.0) then
    let xpixel[3] int(sqrt(spe%major*spe%minor)/abs(spe%convert[3,2]))
  else
    let xpixel[3] 1
  endif
endif
let xpixel[1] max(1,xpixel[1])
let xpixel[2] min(xpixel[2],spe%dim[2])
let n = int((xpixel[2]-xpixel[1])|xpixel[3])+1
!
let ypixel ypix
if (ypixel[1].eq.0) then
  if (spe%dim[2]*spe%dim[3].le.32*32) then
	let ypixel[1] 1
  else
      let ypixel[1] spe%dim[3]/4
  endif
endif
if (ypixel[2].eq.0) then
  if (spe%dim[2]*spe%dim[3].le.32*32) then
	let ypixel[2] spe%dim[3]
  else
      let ypixel[2] 3*spe%dim[3]/4
  endif
endif
if (ypixel[3].eq.0) then
  if (spe%major.ne.0) then
    let ypixel[3] int(sqrt(spe%major*spe%minor)/abs(spe%convert[3,3]))
  else
    let ypixel[3] 1
  endif
endif
let ypixel[1] max(1,ypixel[1])
let ypixel[2] min(ypixel[2],spe%dim[3])
exa xpixel
exa ypixel
let m = int((ypixel[2]-ypixel[1])|ypixel[3])+1
!
greg1\set expand min(6.0|(m+n),1.)
let dx (x2-box_xmin)|n
let dy (y2-box_ymin)|m
for j 1 to m
  let jy ypixel[1]+(j-1)*ypixel[3]
  for i 1 to n
    let ix xpixel[1]+(i-1)*xpixel[3]
    greg1\set box x2+(i-n-1)*dx x2+(i-n)*dx y2+(j-m-1)*dy y2+(j-m)*dy
    greg1\box n n
    if (do_grey) then
      greg1\histogram xspec spe[ix,jy] /base 0 /fill 11
      if (do_contour) then
        greg1\histogram xspec spe[ix,jy]
      endif
    else
      greg1\histogram xspec spe[ix,jy]
    endif
    let xmark ((ix-spe%convert[1,2])*spe%convert[3,2]+spe%convert[2,2])|sec
    let ymark ((jy-spe%convert[1,3])*spe%convert[3,3]+spe%convert[2,3])|sec
    let xmark 0.1*nint(10*xmark)
    let ymark 0.1*nint(10*ymark)
    greg1\set coord box
    let cmark "("'xmark'","'ymark'")"
    greg1\draw text 0.1 -0.1 'cmark' 3 /box 7
  next
next
!
on error
greg1\set expand 0.6
if (page_x.gt.page_y) then
  let dx min((page_x-x2),0.15*page_y)
  let dy dx
  greg1\set box (page_x+x2)|2.-2*dx|5. (page_x+x2)|2.+3*dx|5. page_y*0.5-dy page_y*0.5
else
  let dx min((0.15*page_x),(page_y-y2))
  let dy dx
  greg1\set box page_x*0.95-dx page_x*0.95 page_y*0.95-dy page_y*0.95
endif
greg1\box
greg1\label 'spe%unit1' /x
greg1\label 'spe%unit' /y
greg1\set expand 1.0
!
