; $Id: psinit.pro,v 1.4 2002/09/16 20:43:56 jmspark Exp $
;-------------------------------------------------------------
;+
; NAME:
;       PSINIT
; PURPOSE:
;       Redirect plots and images to postscript printer.
; CATEGORY:
; CALLING SEQUENCE:
;       psinit [,num]
; INPUTS:
;       num = Postscript printer number.           in
; KEYWORD PARAMETERS:
;       Keywords:
;         /FULL to use full page in portrait mode (def=top half).
;         /LANDSCAPE to do plot in landscape mode.
;         FILE=f redirect plot to a specified file.  The default
;           is idl.ps in the local directory.
;         MARGIN = [left, right, bottom, top] sets margins (inches).
;           May give as few values as needed.  For left margin only
;           may just use MAR=xxx, otherwise give an array.
;         WINDOW = [xmin, xmx, ymin, ymx] sets window (inches).
;           Must give all 4 values.
;         /CENTIMETERS  interpreted margin or window values in cm.
;         /DOUBLE to do plot in double thickness.
;         /VECTOR to use vector fonts instead of postscript fonts.
;         /ORIGIN plots a mark at the page origin.
;         /QUIET turns off psinit messages.
; 	  /VMS looks for system variable: IDL_PSPRINTERS to get printer info
; 	    default is to use: 'lp -onb' Unix printer with banner suppression
;         /LIST_PRINTERS lists available printers and their numbers.
;         COMMENT=txt  comment to print with the time stamp.
; OUTPUTS:
; COMMON BLOCKS:
;       ps_com,dname,xthick,ythick,pthick,pfont,num,comm,pfil
; NOTES:
;       Notes:
;         Default is portrait mode, top half of page.
;         Related routines:
;         psterm - terminate postscript output and make plots.
;         pspaper - plot norm. coord. system for curr. page setup.
; MODIFICATION HISTORY:
;       R. Sterner, 2 Aug, 1989.
;       R. Sterner, 22 May, 1990 --- converted to VMS.
;       R. Sterner, 12 Oct, 1990 --- WINDOW keyword.
;       R. Sterner, 20 Dec, 1990 --- added COMMENT keyword.
;       R. Sterner,  6 Dec, 1991 --- added FILE= keyword.
;	P. Schleger, Oct. 4, 1993 --- Added the VMS option, so that default assume unix
;				lp command on a local ps printer
;
; Copyright (C) 1989, Johns Hopkins University/Applied Physics Laboratory
; This software may be used, copied, or redistributed as long as it is not
; sold and this copyright notice is reproduced on each copy made.  This
; routine is provided as is without any express or implied warranties
; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
;-
;-------------------------------------------------------------

	pro psinit, pnum, landscape=v, double=dbl, full=fl, help=hlp, $
	            vector=d3, origin=ori, margin=mar, centimeters=cm, $
		    quiet=qt, list_printers=list, window=window, $
	            comment=comment, file=outfile, vms=vms $
	            	, printer = printer

	common ps_com,dname,xthick,ythick,pthick,pfont,num,comm,pfile

	if keyword_set(hlp) then begin
	  print,' Redirect plots and images to postscript printer.'
	  print,' psinit [,num]
	  print,'   num = Postscript printer number.           in'
	  print,' Keywords:'
 	  print,'   /FULL to use full page in portrait mode (def=top half).'
	  print,'   /LANDSCAPE to do plot in landscape mode.'
	  print,'   FILE=f redirect plot to a specified file.  The default'
	  print,'     is idl.ps in the local directory.'
	  print,'   MARGIN = [left, right, bottom, top] sets margins (inches).'
	  print,'     May give as few values as needed.  For left margin only'
	  print,'     may just use MAR=xxx, otherwise give an array.'
	  print,'   WINDOW = [xmin, xmx, ymin, ymx] sets window (inches).'
	  print,'     Must give all 4 values.'
	  print,'   /CENTIMETERS  interpreted margin or window values in cm.'
	  print,'   /DOUBLE to do plot in double thickness.'
 	  print,'   /VECTOR to use vector fonts instead of postscript fonts.'
 	  print,'   /ORIGIN plots a mark at the page origin.'
 	  print,'   /QUIET turns off psinit messages.'
 	  print,'   /VMS looks for system variable: IDL_PSPRINTERS to get printer info'
 	  print,'     default is to use: "lp -onb" Unix printer with banner suppression'
	  print,'   /LIST_PRINTERS lists available printers and their numbers. /VMS only'
	  print,'   COMMENT=txt  comment to print with the time stamp.'
	  print,' Notes:'
	  print,'   Default is portrait mode, top half of page.'
	  print,'   Related routines:'
	  print,'   psterm - terminate postscript output and make plots.'
	  print,'   pspaper - plot norm. coord. system for curr. page setup.'
	  return
	endif

	IF (keyword_set(VMS)) THEN BEGIN
		getsysnams, 'IDL_PSPRINTERS', npr, cmds, plist	; Get printers.

		if keyword_set(list) then begin
	  		print,' '
	  		print,'           Available Postscript printers:'
	  		print,' '
	  		for i = 0, n_elements(plist)-1 do print,i,'.  '+plist(i)
	  		print,' '
	  		return
		endif



		if n_elements(pnum) eq 0 then pnum = 0
		if (pnum lt 0) or (pnum ge npr) then begin
	  		print,' Error in psinit: printer number out of range.  '+$
	    		'must be 0 to '+strtrim(npr-1,2)
	  		return
		endif
		num = pnum
	END ELSE BEGIN
		pnum=0
		num = pnum
		cmds = ['lpr ','lpr -Pspins']
		plist = ['Terminal Room','SPINS printer']
	END

	if n_elements(comm) eq 0 then comm = ''
	if n_elements(comment) ne 0 then comm = comment

	dname = !d.name		; Save current setup.
	xthick = !x.thick
	ythick = !y.thick
	pthick = !p.thick
	pfont = !p.font


	qflg = not keyword_set(qt)

	;--------------  Enter PS mode  -------------------
	set_plot, 'ps'
	if keyword_set( printer ) then begin
		if not dialog_printersetup() then begin
			message, 'No printer selected. Using postscript output.', /continue
			printer = 0
		endif else set_plot, 'printer'
	endif

	if not keyword_set( printer ) then begin

	if qflg then print,' All plots are now redirected to the '+$
	  'postscript printer number ' + strtrim(num,2) + '.'
	if qflg then print,'   = ' + plist(num)
	if qflg then print,' To output plots and reset to screen do PSTERM.'

	;--------------  Handle optional filename  --------
	if n_elements(outfile) eq 0 then outfile = ''	; Use default = idl.ps.
	if outfile ne '' then begin			; Use given file.
	  device, filename=outfile			; Direct plots to file.
	  if qflg then print,' Output file is '+outfile
	endif
	pfile = outfile					; Save file name.

	endif


	;------  Set plot area using Window mode or Margin mode (def)  ------
	;-----  MARGIN MODE  ----------
	if n_elements(window) eq 0 then begin	; No WINDOW, assume MARGIN.
	  ;-----  Get margin values ready to use  ------
	  if n_elements(mar) eq 0 then mar=[0,0,0,0]	; opt. page margins.
	  if keyword_set(cm) then mar = mar/2.54	; Want margins in cm.
	  mar = [mar,0,0,0]				; Any # of values.
	  ;--- dx1,dy1 = offsets from origin.  dx2,dy2=total margins  -------
	  dx1 = mar(0) & dx2 = dx1+mar(1) & dy1 = mar(2) & dy2 = dy1+mar(3)
	  ;----  landscape mode  --------
	  if keyword_set(v) then begin		; Handle landscape mode.
	    device, /landscape, /inches, xoffset=.75+dy1, yoffset=10.25-dx1, $
	      xsize=9.5-dx2, ysize=7.0-dy2
	    if qflg then print,' Landscape mode.'
	  ;--- portrait mode  -------
	  endif else begin			; Portrait mode.
	  ;----  Default = top half portrait mode  ------
	    device, /portrait, /inches, xoffset=0.5+dx1, yoffset=5.25+dy1, $
	      xsize=7.5-dx2, ysize=5.25-dy2
	    if keyword_set(fl) then begin		; Handle top.
	  ;----  Full page portrait mode  -------
	      device, /portrait, /inches, xoffset=0.5+dx1, yoffset=0.5+dy1, $
	        xsize=7.5-dx2, ysize=10.-dy2
	      if qflg then print,' Portrait mode, full page.'
	    endif else if qflg then print,' Portrait mode, top half page.'
	  endelse
	;-------  WINDOW MODE  ---------
	endif else begin
	  ;-----  Get window values ready to use  ------
	  if n_elements(window) lt 4 then begin
	    print,' Error in psinit: If WINDOW keyword used it must have 4 '+$
	      'values.'
	    return
	  endif
	  if qflg then print,' Setting plot window.'
	  if keyword_set(cm) then window = window/2.54	  	; in cm.
	  ;--- dx1,dy1 = offsets from origin.  dx2,dy2=total margins  -------
	  dx1 = window(0) & dx2 = window(1)-dx1
	  dy1 = window(2) & dy2 = window(3)-dy1
	  ;----  landscape mode  --------
	  if keyword_set(v) then begin		; Handle landscape mode.
	    device, /landscape, /inches, xoffset=.75+dy1, yoffset=10.25-dx1, $
	      xsize=dx2, ysize=dy2
	    if qflg then print,' Landscape mode.'
	  ;--- portrait mode  -------
	  endif else begin			; Portrait mode.
	  ;----  Default = top half portrait mode  ------
	    device, /portrait, /inches, xoffset=0.5+dx1, yoffset=5.25+dy1, $
	      xsize=dx2, ysize=dy2
	    if keyword_set(fl) then begin		; Handle top.
	  ;----  Full page portrait mode  -------
	      device, /portrait, /inches, xoffset=0.5+dx1, yoffset=0.5+dy1, $
	        xsize=dx2, ysize=dy2
	      if qflg then print,' Portrait mode, full page.'
	    endif else if qflg then print,' Portrait mode, top half page.'
	  endelse
	endelse  ; End of set plot area
	;--------------------------------------------------------------------


	; Def 32 gray levels (max ALW)
	if not keyword_set( printer ) then begin
		device, bits_per_pixel = 5.
		!p.font = 0
		; Default font is times.
		device, /times
	endif else d3 = 1

	if keyword_set(dbl) then begin		; Handle double thickness.
	  !x.thick = 2
	  !y.thick = 2
	  !p.thick = 2
	  device, /bold
	  if qflg then print,' Double thickness mode.'
	endif

	if keyword_set(d3) then begin
	  !p.font = -1
	  if qflg then print,' Using vector font.'
	endif

	if keyword_set(ori) then begin
	  if qflg then print,' Marking origin.'
	  plots, [0,0], [0,0], /normal, psym=1
	endif

	return
	end
