;-------------------------------------------------------------
;+
; NAME:
;        CLOSE_DEVICE
;
; PURPOSE:
;        CLOSE_DEVICE closes the PostScript device and spawns
;        a print job to the printer specified by the user or
;        it closes a graphics window.
;
; CATEGORY:
;        Input/Output
;
; CALLING SEQUENCE:
;        CLOSE_DEVICE, OLD_DEVICE, [keywords]
;
; INPUTS:
;        OLD_DEVICE (str) -> Content of !D.NAME before call to OPEN_DEVICE
;             If omitted, 'X' will be used as a default
;
; KEYWORD PARAMETERS:
;        PRINTER    (str) -> name of the printer to send output to
;             Default is 'none', i.e. the postscript file will only be closed
;             and can thenbe manually printed e.g. using the Unix lpr command.
;
;        FILENAME   (str) -> name of the PostScript file
;             Default is 'idl.ps'. NOTE: If a FILENAME was given with 
;             OPEN_DEVICE, the same name must be supplied here if the output 
;             shall be sent to the printer!
;
;        WINDOW     -> window number to be closed (or -1 if current)
;
;        /TIMESTAMP  -> add a label with filename and system time to the plot
;
; OUTPUTS:
;        If postscript device is active, a *.ps file will be created and/or 
;        sent to the printer.
;
; SUBROUTINES:
;
; REQUIREMENTS:
;
; NOTES: 
;        The print command (lpr -P) is specific to Unix systems.  Users
;        who are running other operating systems will have to modify this.
;
;        The WINDOW keyword is only evaluated if the current device supports 
;        windows [!D.FLAGS AND 256) GT 0]. If you only want to close a 
;        postscript file and don't fuss around with your screen windows
;        then simply don't set this keyword.
;
;        The OLD_DEVICE parameter can be misused to set the output device 
;        to anything ! Therefore, if you are working on a Unix system it's
;        probably safest to not use it and stick with the 'X' default.
; 
;
; EXAMPLE:
;        CLOSE_DEVICE, OLD_DEVICE, PRINTER='pro', FILENAME='myplot.ps'  
;            If current device name is PS then 'myplot.ps' will be closed
;            and spooled to printer 'pro'
;  
;        CLOSE_DEVICE, OLD_DEVICE, WIN=-1
;            If current device name is PS then the postscript file will
;            be closed. If the current device is a screen device (that 
;            supports windows), then the active window will be deleted.
; 
;        CLOSE_DEVICE
;            restores the plotting device to 'X'
;
; MODIFICATION HISTORY:
;        bmy, 18 Aug 1997: VERSION 1.00
;        bmy, 19 Aug 1997: VERSION 1.01
;        mgs, 20 Aug 1997: VERSION 1.02
;
;-
; Copyright (C) 1997, Bob Yantosca, Harvard University
; This software is provided as is without any warranty
; whatsoever. It may be freely used, copied or distributed
; for non-commercial purposes. This copyright notice must be
; kept with any copy of this software. If this software shall
; be used commercially or sold as part of a larger package,
; please contact the author to arrange payment.
; Bugs and comments should be directed to bmy@io.harvard.edu
; with subject "IDL routine close_device"
;-------------------------------------------------------------


pro close_device, OLD_DEVICE, PRINTER=PRINTER, FILENAME=FILENAME, $
              WINDOW=WINDOW, TIMESTAMP=TIMESTAMP

      on_error, 2

      if (n_params() le 0) then OLD_DEVICE = 'X' 

      if (not keyword_set(PRINTER))  then PRINTER  = 'none' 
      if (not keyword_set(FILENAME)) then FILENAME = 'idl.ps'

      if (!d.name eq 'PS') then begin   ; if postscript device active

; add timestamp if desired
          if(keyword_set(TIMESTAMP)) then begin  ; draw timestamp if desired
               timelabel = systime(0)
               xyouts,0.98,0.007,filename+'  '+timelabel,color=1,   $
                      align=1,/norm,charsize=0.4
          endif



	    device, /close      ; close postscript file

          if (PRINTER ne 'none') then begin   ; spawn postscript file to printer
  	        TRIM_PRINTER = strtrim(PRINTER,2)
              print, 'Sending output to printer ' + TRIM_PRINTER
              spawn, 'lpr -P ' + TRIM_PRINTER + ' ' + FILENAME
          endif

      endif else begin     ; no postscript device active

          ; check if device supports windows and if a window shall be closed
          if(n_elements(window) gt 0) then begin
              if(window lt 0) then window = !D.WINDOW 
              if( (!D.FLAGS AND 256) GT 0 AND window ge 0) then $
                  wdelete,window
          endif

      endelse

      set_plot, OLD_DEVICE

return
end



