;+
; 
;             NAME : sp_setup_ps
; 
;          PURPOSE : To set up the postscript device.
;
; CALLING SEQUENCE : sp_setup_ps,ps=ps,eps=eps,[/landscape],[/portrait],
;                    [height=height],[width=width],[/color],[bits=bits],
;                    [/preview],[font_size=font_size],[filename=filename],
;                    [/bottom],[/noext],[/close]
; 
;           INPUTS : none
; 
;  OPTIONAL INPUTS : height    = figure height in inches
;                    width     = figure width in inches
;                    bits      = the color depth in bits per pixel
;                    font_size = the font size in pt
;                    filename  = output file name 
;                    -- check code for defaults -- 
;
;                    default font is helvetica with isolatin1 encoding. 
; 
;         KEYWORDS : ps        - turns on ps output
;                    eps       - turns on eps output
;                    close     - closes the device
;                    landscape - 
;                    portrait  - default
;                    color     -    
;                    preview   - includes preview in eps or ps file (?)
;                    noext     - don't add .ps or .eps extension 
;                    bottom    - put figure at bottom of page, not middle
;                    courier   - use courier font, not helvetica
; 
;          OUTPUTS : none
;
; OPTIONAL OUTPUTS : none
;
;    COMMON BLOCKS : none
; 
;          EXAMPLE : to plot a simple graph 5 inches by 5 inches
;                    centered on a page to a ps file named myPSfile.ps
;                    you would do something like
;                    IDL> sp_setup_ps,/ps,w=5,h=5,filename="myPSfile"
;                    IDL> plot,x,y
;                    IDL> sp_setup_ps,/close
;
;    SPECIAL CALLS : none
;
;          WRITTEN : HPW Many Moons ago.
;
;         MODIFIED :
; 
;-

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

pro sp_setup_ps,close=close,$
             ps=ps,$
             eps=eps,$
             landscape=landscape,$
             portrait=portrait,$
             height=height,$
             width=width,$
             color=color,$
             bits=bits,$
             preview=preview,$
             font_size=font_size,$
             filename=filename,$
             bottom=bottom,$
             noext=noext,$
             page_length=page_length,$
             page_width=page_width,$
             xoffset=xoffset,yoffset=yoffset,$
             courier=courier,cmyk=cmyk

if not(keyword_set(ps)) and not(keyword_set(eps)) then return

;; ----------------------------------------------------------------------
;; Close PS device
;; ----------------------------------------------------------------------

if strlowcase(!version.os_family) eq 'windows' then dev = 'WIN'
if strlowcase(!version.os_family) eq 'unix' then dev = 'X'

if keyword_set(close) then begin
    device,/close
    set_plot,dev
    !p.font = -1
    return
endif

;; ----------------------------------------------------------------------
;; Check keywords
;; ----------------------------------------------------------------------

if keyword_set(landscape)    then orientation = "landscape"
if keyword_set(portrait)     then orientation = "portrait"
if keyword_set(color)        then c  = 1 else c = 0
if keyword_set(bits)         then b  = bits else b = 8
if keyword_set(preview)      then p  = 2 else p = 0
if keyword_set(font_size)    then fs = font_size else fs = 12

if keyword_set(page_width)  then pw = page_width else pw = 8.5
if keyword_set(page_length) then pl = page_length else pl = 11.0

;; filename
if keyword_set(eps)      then ext = ".eps" else ext = ".ps"
if keyword_set(filename) then fn = filename else fn = "idl" + ext
if keyword_set(filename) and not(keyword_set(noext)) then fn = fn + ext

;; ----------------------------------------------------------------------
;; set orientation 
;; set offsets so that figure is centered on the page
;; ----------------------------------------------------------------------

if n_elements(orientation) eq 0 then orientation = "portrait"
if (orientation eq "landscape") then begin
  if keyword_set(height) then fh = height else fh = 7.0
  if keyword_set(width)  then fw = width  else fw = 9.5
  yo = pl - (pl-fw)/2.0
  xo = (pw-fh)/2.0
  land = 1
  port = 0
endif else begin
  if keyword_set(height) then fh = height else fh = 5.0
  if keyword_set(width)  then fw = width  else fw = 7.0
  yo = (pl - fh)/2.0
  xo = (pw - fw)/2.0
  if keyword_set(bottom) then yo=1.0
  land = 0
  port = 1
endelse

if keyword_set(xoffset) then xo = xoffset
if keyword_set(yoffset) then yo = yoffset

;; ----------------------------------------------------------------------
;; Set PS device
;; ----------------------------------------------------------------------

set_plot,'ps',/interpolate
!p.font=0

;; set parameters common to all configurations
device,/inches,xoffset=xo,yoffset=yo,xsize=fw,ysize=fh,$ ;; position
    landscape=land,portrait=port,$			 ;; orientation
    /isolatin1,font_size=fs,$		                 ;; font
    filename=fn

if keyword_set(courier) then begin
  device,/courier
endif else begin
  device,/helvetica,/tt_font
endelse

;; color 
if c then device,/color,bits=b,cmyk=cmyk

;; color depth
if keyword_set(bits) then device,bits=b

;; encapsulation
if ext eq ".eps" then device,/encapsulated,preview=p else device,encapsulated=0

return
end
