Package PyFoam :: Package ThirdParty :: Package Gnuplot :: Module gp_unix
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.ThirdParty.Gnuplot.gp_unix

  1  # $Id: gp_unix.py,v 2.6 2003/04/21 09:44:09 mhagger Exp $ 
  2   
  3  # Copyright (C) 1998-2003 Michael Haggerty <mhagger@alum.mit.edu> 
  4  # 
  5  # This file is licensed under the GNU Lesser General Public License 
  6  # (LGPL).  See LICENSE.txt for details. 
  7   
  8  """gp_unix -- an interface to gnuplot used for unix platforms. 
  9   
 10  This file implements a low-level interface to a gnuplot program for a 
 11  unix platform (actually it is used for any non-Windows, non-Mac 
 12  system).  This file should be imported through gp.py, which in turn 
 13  should be imported via 'import Gnuplot' rather than these low-level 
 14  interfaces. 
 15   
 16  """ 
 17   
 18  __cvs_version__ = '$Revision: 2.6 $' 
 19   
 20   
 21  # ############ Configuration variables: ################################ 
 22   
23 -class GnuplotOpts:
24 """The configuration options for gnuplot on generic platforms. 25 26 Store the options in a class to make them easy to import and 27 modify en masse. If you want to modify the options from the 28 command line or within a running program, do something like the 29 following:: 30 31 import Gnuplot 32 Gnuplot.GnuplotOpts.gnuplot_command = '/bin/mygnuplot' 33 34 """ 35 36 # Command to start up the gnuplot program. If your version of 37 # gnuplot is run otherwise, specify the correct command here. You 38 # could also specify a full path or append command-line options 39 # here if you wish. 40 gnuplot_command = 'gnuplot' 41 42 # Recent versions of gnuplot (at least for Xwindows) allow a 43 # `-persist' command-line option when starting up gnuplot. When 44 # this option is specified, graph windows remain on the screen 45 # even after you quit gnuplot (type `q' in the window to close 46 # it). This can be handy but unfortunately it is not supported by 47 # older versions of gnuplot. The following configuration variable 48 # specifies whether the user's version of gnuplot recognizes this 49 # option or not. You can set this variable to 1 (supports 50 # -persist) or 0 (doesn't support) yourself; if you leave it with 51 # the value None then the first time you create a Gnuplot object 52 # it will try to detect automatically whether your version accepts 53 # this option. 54 recognizes_persist = None # test automatically on first use 55 56 # What should be the default if the persist option is not 57 # specified explicitly? 58 prefer_persist = 0 59 60 # Recent versions of gnuplot allow you to specify a `binary' 61 # option to the splot command for grid data, which means that the 62 # data file is to be read in binary format. This option saves 63 # substantial time writing and reading the file, and can also save 64 # substantial disk space and therefore it is the default for that 65 # type of plot. But if you have an older version of gnuplot (or 66 # you prefer text format) you can disable the binary option in 67 # either of two ways: (a) set the following variable to 0; or (b) 68 # pass `binary=0' to the GridData constructor. (Note that the 69 # demo uses binary=0 to maximize portability.) 70 recognizes_binary_splot = 1 71 72 # Data can be passed to gnuplot through a temporary file or as 73 # inline data (i.e., the filename is set to '-' and the data is 74 # entered into the gnuplot interpreter followed by 'e'). If 75 # prefer_inline_data is true, then use the inline method as 76 # default whenever it is supported. This should be fast but will 77 # use more memory since currently the inline data is put into a 78 # big string when the PlotItem is created. 79 prefer_inline_data = 0 80 81 # Does Python implement the threading module and os.mkfifo on this 82 # operating system? If so, the _FIFOFileItem class will be 83 # defined in PlotItem.py. 84 support_fifo = 1 85 86 # Should FIFOs be used to send data to gnuplot by default? 87 prefer_fifo_data = 1 88 89 # After a hardcopy is produced, we have to set the terminal type 90 # back to `on screen' using gnuplot's `set terminal' command. The 91 # following is the usual setting for Xwindows. If it is wrong, 92 # change the following line to select the terminal type you prefer 93 # to use for on-screen work. 94 default_term = 'x11' 95 96 # Gnuplot can plot to a printer by using "set output '| ...'" 97 # where ... is the name of a program that sends its stdin to a 98 # printer, or by "set output 'printer_device', where 99 # 'printer_device is the name of a file-like interface to the 100 # printer. On my machine the appropriate program is `lpr', as set 101 # below. On your computer it may be something different (like 102 # `lp'); you can set that by changing the variable below. You can 103 # also add options to the print command if needed. 104 default_lpr = '| lpr' 105 106 # Enhanced postscript is an option to the postscript terminal 107 # driver that requests enhanced treatment of strings (for example, 108 # font changes, superscripts, and subscripts). Set to 1 to enable 109 # or 0 to disable. If you have a version of gnuplot earlier than 110 # 3.7, you should set this to None (*not* 0!) so that the option 111 # is not used at all. 112 prefer_enhanced_postscript = 1
113 114 # ############ End of configuration options ############################ 115 116 from os import popen 117 118
119 -def test_persist():
120 """Determine whether gnuplot recognizes the option '-persist'. 121 122 If the configuration variable 'recognizes_persist' is set (i.e., 123 to something other than 'None'), return that value. Otherwise, 124 try to determine whether the installed version of gnuplot 125 recognizes the -persist option. (If it doesn't, it should emit an 126 error message with '-persist' in the first line.) Then set 127 'recognizes_persist' accordingly for future reference. 128 129 """ 130 131 if GnuplotOpts.recognizes_persist is None: 132 import string 133 g = popen('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command, 'r') 134 response = g.readlines() 135 g.close() 136 GnuplotOpts.recognizes_persist = ( 137 (not response) or (string.find(response[0], '-persist') == -1)) 138 return GnuplotOpts.recognizes_persist
139 140
141 -class GnuplotProcess:
142 """Unsophisticated interface to a running gnuplot program. 143 144 This represents a running gnuplot program and the means to 145 communicate with it at a primitive level (i.e., pass it commands 146 or data). When the object is destroyed, the gnuplot program exits 147 (unless the 'persist' option was set). The communication is 148 one-way; gnuplot's text output just goes to stdout with no attempt 149 to check it for error messages. 150 151 Members: 152 153 'gnuplot' -- the pipe to the gnuplot command. 154 155 Methods: 156 157 '__init__' -- start up the program. 158 159 '__call__' -- pass an arbitrary string to the gnuplot program, 160 followed by a newline. 161 162 'write' -- pass an arbitrary string to the gnuplot program. 163 164 'flush' -- cause pending output to be written immediately. 165 166 """ 167
168 - def __init__(self, persist=None):
169 """Start a gnuplot process. 170 171 Create a 'GnuplotProcess' object. This starts a gnuplot 172 program and prepares to write commands to it. 173 174 Keyword arguments: 175 176 'persist=1' -- start gnuplot with the '-persist' option, 177 (which leaves the plot window on the screen even after 178 the gnuplot program ends, and creates a new plot window 179 each time the terminal type is set to 'x11'). This 180 option is not available on older versions of gnuplot. 181 182 """ 183 184 if persist is None: 185 persist = GnuplotOpts.prefer_persist 186 if persist: 187 if not test_persist(): 188 raise ('-persist does not seem to be supported ' 189 'by your version of gnuplot!') 190 self.gnuplot = popen('%s -persist' % GnuplotOpts.gnuplot_command, 191 'w') 192 else: 193 self.gnuplot = popen(GnuplotOpts.gnuplot_command, 'w') 194 195 # forward write and flush methods: 196 self.write = self.gnuplot.write 197 self.flush = self.gnuplot.flush
198
199 - def __call__(self, s):
200 """Send a command string to gnuplot, followed by newline.""" 201 202 self.write(s + '\n') 203 self.flush()
204