;+
; 
;             NAME : sp_plot_pos
; 
;          PURPOSE : To return positions for multiple graphs in normal
;                    coordinates. To be used with !p.position. Better
;                    than !p.multi in that one can exclude x and y
;                    labels for the "interior" plots.
;
; CALLING SEQUENCE : pos = sp_plot_pos(nx,ny)
; 
;           INPUTS : The number of plots in the x and y directions. 
; 
;  OPTIONAL INPUTS : spacing = [dx,dy,dx2,dy2] where dx and dy are the
;                    left and bottom margins and dx2 and dy2 are the
;                    right and top margins in multiples of the typical
;                    character dimensions in each direction (i.e., the
;                    sizes returned from get_charsize using normal
;                    coordinates). The default is [8,6,3,3].
; 
;         KEYWORDS : test     - displays a simple graph at each location
;                    xtitles  - make room for x labels on every plot
;                    ytitles  - make room for y labels on every plot
;                    nolabels - no x or y labels on any plots
; 
;          OUTPUTS : Positions for the plots in normal coodinates. TO
;                    be used with !p.position. Plots are arranged from
;                    left to right, top to bottom.
;
; OPTIONAL OUTPUTS : h - the height of each plot
;                    w - the width of each plot
;
;    COMMON BLOCKS : none
; 
;          EXAMPLE : pos = sp_plot_pos(3,3)
;                    !p.position = pos(0,0,*) ;; position for upper left
;
;    SPECIAL CALLS : get_charsize
;
;          HISTORY : Written by HPW many moons ago . . .
;
;                  : HPW 18-AUG-1998 Revised to use get_charsize to
;                    determine the x and y margins for the plot.
;
;                  : HPW 17-AUG-1999 Revised to produce equal margins
;                    for plots with no labels.
; 
;-

;; **********************************************************************

function old_get_charsize,x=x,y=y,data=data,device=device

if !d.name ne 'PS' and !d.window eq -1 then window,xs=640,ys=512

dy = !d.y_ch_size & dx = !d.x_ch_size 

if keyword_set(device) then begin
  if keyword_set(y) then return,dy 
  if keyword_set(x) then return,dx  
endif else begin

  if keyword_set(data) then begin
    d = convert_coord([0,dx],[0,dy],/device,/data)
  endif else begin
    d = convert_coord([0,dx],[0,dy],/device,/to_normal)
  endelse
  dx = d(0,1)-d(0,0) & dy = d(1,1)-d(1,0)
  
  if keyword_set(y) then return,dy 
  if keyword_set(x) then return,dx
endelse

return,[dx,dy]
end

function get_charsize,x=x,y=y,data=data,device=device

  if !d.name eq 'Z' then begin
    dx = !d.x_ch_size/float(!d.x_size)
    dy = !d.y_ch_size/float(!d.y_size)
    if keyword_set(y) then return,dy 
    if keyword_set(x) then return,dx
    return,[dx,dy]
  endif

  dy = !d.y_ch_size & dx = !d.x_ch_size 

  if keyword_set(device) then begin
    if keyword_set(y) then return,dy 
    if keyword_set(x) then return,dx  
  endif else begin

    if keyword_set(data) then begin
      d = convert_coord([0,dx],[0,dy],/device,/data)
    endif else begin
      d = convert_coord([0,dx],[0,dy],/device,/to_normal)
    endelse
    dx = d(0,1)-d(0,0) & dy = d(1,1)-d(1,0)
    
    if keyword_set(y) then return,dy 
    if keyword_set(x) then return,dx
  endelse

return,[dx,dy]
end

;; **********************************************************************

function sp_plot_pos,nx,ny,spacing=spacing,test=test,height=h,width=w,$
                     ytitles=ytitles,xtitles=xtitles,nolabels=nolabels,$
                     footer=footer,header=header,noreform=noreform,$
                     aspect=aspect,device_size=device_size

if keyword_set(ps) or keyword_set(eps) then dps = 1 else dps = 0

xch = get_charsize(/x)
ych = get_charsize(/y)

if keyword_set(footer) then foot = ych*footer else foot = 0
if keyword_set(header) then head = ych*header else head = 0

if keyword_set(nolabels) then begin

  if not(keyword_set(spacing)) then spacing = 1.0

  ds = spacing(0)*!d.x_ch_size 
  xs = !d.x_size
  ys = !d.y_size
  w  = (xs-(nx+1)*ds)/float(nx)
  h  = (ys-(ny+1)*ds)/float(ny)

  bots  = foot + ds + findgen(ny)*(h+ds)
  tops  = bots + h
  left  = ds + findgen(nx)*(w+ds)
  right = left + w

endif else begin

  if keyword_set(spacing) then begin
    dx  = spacing(0)*xch
    dy  = spacing(1)*ych
    dx2 = spacing(2)*xch
    dy2 = spacing(3)*ych
  endif else begin
    if dps then spacing = [8,6,2,1] else spacing = [8,6,2,2]
    if not(keyword_set(xtitles)) then spacing(3) = 2
    
    dx  = spacing(0)*xch
    dy  = spacing(1)*ych
    dx2 = spacing(2)*xch
    dy2 = spacing(3)*ych
  endelse   
  
  dx  = double(dx)
  dy  = double(dy)
  dx2 = double(dx2)
  dy2 = double(dy2)
  
  if keyword_set(ytitles) then nyt = nx else nyt = 1
  if keyword_set(ytitles) then yt  = 1 else yt = 0
  
  if keyword_set(xtitles) then nxt = ny else nxt = 1
  if keyword_set(xtitles) then xt  = 1 else xt = 0

  w = (1-nyt*dx-nx*dx2)/float(nx)
  h = (1-nxt*dy-ny*dy2-head-foot)/float(ny)

  bots  = foot + dy + findgen(ny)*(h+dy2+xt*dy)
  tops  = bots + h
  left  = dx + findgen(nx)*(w+dx2+yt*dx)
  right = left + w

endelse

pos = fltarr(nx,ny,4)
cnt = 0
for j=ny-1,0,-1 do begin
  for i=0,nx-1 do begin

    if keyword_set(nolabels) then begin
      xn1 = convert_coord([left(i),bots(j)],/device,/to_normal)
      xn2 = convert_coord([right(i),tops(j)],/device,/to_normal)
      pp = [xn1(0),xn1(1),xn2(0),xn2(1)]
    endif else begin
      pp = [left(i),bots(j),right(i),tops(j)]
    endelse
  
    pos(i,ny-1-j,*) = pp
    cnt = cnt + 1
    
  endfor
endfor

if not(keyword_set(noreform)) then pos = reform(pos)

if keyword_set(test) then begin
  erase
  x = findgen(100)*!pi*2/99.0
  y = sin(x)
  
  for i=0,nx*ny-1 do begin
    ii = i mod nx
    jj = i / nx

    if keyword_set(ytitles) then ytitle = 'Sin(theta)' else begin
      if ii eq 0 then ytitle = 'Sin(theta)' else ytitle = ''
    endelse

    if keyword_set(xtitles) then xtitle = 'theta' else begin
      if jj eq 1 then xtitle = 'theta' else xtitle = ''
    endelse

    !p.position = pos[ii,jj,*]
    
    plot,x,y,xstyle=1,ystyle=1,xtickname=replicate(' ',10),$
        ytickname=replicate(' ',10),/noerase,$
        xtitle=xtitle,ytitle=ytitle

  endfor
endif

!p.position = -1



return,pos
end 
