;+ ; NAME: plot2ps ; ; ; ; PURPOSE: ; Takes a plotting command (a procedure) and sends the result ; to a postscript file of the user's choosing. The device is ; set back to the original plotting device when the routine ; exits. Note that if the plot command fails, no postscript ; file is created and an error message is generated by ; default. (This can be overridden.) ; ; ; ; CATEGORY: ; Plotting ; ; ; CALLING SEQUENCE: ; plot2ps, filename, command[, vars, xsize=xsize, ; ysize=ysize, /ignoreerror, /noclobber, extra keywords for ; z-buffer device call] ; ; ; ; INPUTS: ; filename: ; Filename of resulting postscript (string) ; ; command: ; The exact command to be execute to make the plot, ; with any variables defined at the main level ; referrenced as "var.variable_name". See below. (String) ; ; ; ; OPTIONAL INPUTS: ; var: ; Structure containing any variables used which are defined ; at the main level. (Rather than defined or read-in in the ; plotting procedure.) Any such variables are then called as ; "var.variable_name" in the command string. ; ; ; KEYWORD PARAMETERS: ; ; /ignoreerror: ; If an error occurs in plotting, produce the postscript ; file anyway. ; ; /noclobber: ; If the file, filename, already exists, do NOT overwrite ; it. Instead, append _1, _2, ... to the end of the ; file's base name. (But before the extension.) ; Note that by default, plot2ps WILL clobber, or overwrite, ; a pre-existing file! ; ; Extra keywords: ; Any additional keywords to be used by the ; postscript device call. This can include the size of ; the postscript file, the orientation (landscape versus ; portrait), or the format (regular versus encapsulated) ; ; OUTPUTS: ; None ; ; ; OPTIONAL OUTPUTS: ; None ; ; ; COMMON BLOCKS: ; Definitely none ; ; ; SIDE EFFECTS: ; ; ; ; RESTRICTIONS: ; ; ; ; PROCEDURE: ; ; ; ; EXAMPLE: ; IDL> x = findgen(101)/10.0 ; Create x-array ; IDL> y = x^2 ; Create y-array ; IDL> plot, x, y ; Plots to screen (presumably) ; IDL> plot2ps, 'test.ps', 'plot, var.x, var.y', {x:x, y:y} ; ; Alternately, we can create the structure before calling plot2ps: ; IDL> var = {x:x, y:y} ; Create the structure ; IDL> plot, var.x, var.y ; Test the plotting ; IDL> plot2ps, 'test.ps', 'plot, var.x, var.y', var ; MODIFICATION HISTORY: ; ; Created: 30 June 2005 by John Weiss ; ;- pro plot2ps, filename, command, var, ignoreerror=ignoreerror, noclobber=noclobber, _EXTRA=_EXTRA if(n_params() LT 2) then begin print, "Syntax: plot2ps, filename, command[, struct with variables used, keywords for ps device]" return endif ; If this is set to use noclobber, see if the file exits. If it does, ; use a modified name if(keyword_set(noclobber)) then begin ; The extension: extname = stregex(filename, "\.([A-Za-z]*)$", /extract) basename = file_basename(filename, extname) ; Get the basename (no path, no ; extension) of the file dirname = file_dirname(filename, /mark_directory) ; Name of the directory num = 0 while(file_test(filename)) do begin num = num + 1 ; The new candidate name filename = dirname + basename+"_" + $ strcompress(string(num, format="(I)"), /remove_all) + $ extname endwhile endif original_device = !d.name set_plot, 'ps' device, filename=filename,$ _Extra=_Extra tvlct, r,g,b, /get success=execute(command) if(not success) then begin print, "Plot command did not execute successfully in plot2ps" set_plot, original_device return endif device, /close set_plot, original_device end