;-------------------------------------------------------------
;+
; NAME:
;        OPEN_DEVICE
;
; PURPOSE:
;        If hard copy is to be generated, OPEN_DEVICE opens the 
;        PostScript device.  Otherwise OPEN_DEVICE opens an Xwindow. 
;
; CATEGORY:
;        Input/Output
;
; CALLING SEQUENCE:
;        OPEN_DEVICE, OLD_DEVICE, [,keywords]
;
; INPUTS:
;
; KEYWORD PARAMETERS:
;        PS        (int)  -> will send PostScript file to printer
;
;        COLOR     (int)  -> will enable PostScript color mode
;
;        LANDSCAPE (int)  -> will enable PostScript landscape mode
;
;        PORTRAIT  (int)  -> will enable PostScript portrait mode
;
;        FILENAME  (str)  -> The name to be given the PostScript file.
;                  Default: idl.ps
;
;        WINPARAM  (int)  -> An integer vector with 3 elements:
;                            WINPARAM(0) = window number  
;                            WINPARAM(1) = X dimension of window in pixels
;                            WINPARAM(2) = Y dimension of window in pixels
;         
;        _EXTRA -> additional keywords that are passed to the call to
;                  the DEVICE routine
;
; OUTPUTS:
;        OLD_DEVICE (str) -> stores the previous value of !D.NAME
;
; SUBROUTINES:
;
; REQUIREMENTS:
;
; NOTES:
;        If PS=0 then  
;            Open Xwindow WINPARAM(0), which is WINPARAM(1) pixels wide
;            in the X-direction, and WINPARAM(2) pixels wide in the
;            Y-direction. 
;
;        If PS=1 then 
;           depending on /PORTRAIT or /LANDSCAPE and /COLOR
;           postscript is enabled in either portrait or landscape
;           mode as color or b/w plot
;
;        The key parameter which determines whether to open a postscript
;        file or a screen window is PS. Therefore, e.g. in a widget 
;        application, you can pass a standard set of parameters for both,
;        postscript and screen, to this routine, and determine the device
;        to be chosen by a button state or checkbox which is passed into PS.
;              
;
; EXAMPLE:
;        OPEN_DEVICE, WINPARAM=[0,800,800]  
;            opens a screen window of size 800x800 pixels
;
;        OPEN_DEVICE, OLD_DEVICE, /LANDSCAPE, FILENAME='myplot.ps'
;            opens a postscript file named myplot.ps in b/w and landscape
;            orientation
;
;        OPEN_DEVICE, OLDDEV, PS=PS, /PORTRAIT, /COLOR, WIN=2
;            depending on the value of PS either a color postscript file 
;            named idl.ps is opened or screen window number 2 in default
;            size.
;
;
; MODIFICATION HISTORY:
;        bmy  15 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 open_device"
;-------------------------------------------------------------


pro open_device, OLD_DEVICE,                              $
                 PS=PS, COLOR=COLOR, FILENAME=FILENAME,   $  
                 LANDSCAPE=LANDSCAPE, PORTRAIT=PORTRAIT,  $ 
                 WINPARAM=WINPARAM, _EXTRA=E

      on_error, 2   ; return to caller
	
      OLD_DEVICE = !D.NAME    ; retrieve current device


      if (not keyword_set(FILENAME)) then FILENAME = 'idl.ps'
      if (not keyword_set(COLOR))    then COLOR    = 0
      if (not keyword_set(PORTRAIT)) then LANDSCAPE = 1 ; default

      if (keyword_set(PS)) then begin
          set_plot, 'PS'

          if (keyword_set(LANDSCAPE)) then begin       ;Landscape mode
              device, /landscape, color=COLOR, $
                  bits=8, filename=FILENAME, _EXTRA=e

          endif else begin    ; Portrait mode
              device, color=COLOR, bits=8, /portrait,   $
                  /inches, xoffset=0.25, yoffset=0.25,   $
                  xsize=8.0, ysize=10, filename=FILENAME, _EXTRA=e
          endelse


      endif else begin      ; no postscript desired
                            ; only action if winparam given

          if (n_elements(WINPARAM) gt 0) then begin      ;Open Xwindow
; if winparam is 3 element vector then open window of desired size
;  else open window with standard size 
              if(n_elements(winparam) eq 3) then $
                   window, WINPARAM(0), xsize=WINPARAM(1), ysize=WINPARAM(2) $
              else  $
                   window, winparam(0)
          endif 

      endelse


return	
end


