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

Source Code for Module PyFoam.ThirdParty.Gnuplot.utils

  1  #! /usr/bin/env python 
  2   
  3  # $Id: utils.py 297 2007-03-30 11:25:28Z mhagger $ 
  4   
  5  # Copyright (C) 1998-2003 Michael Haggerty <mhagger@alum.mit.edu> 
  6  # 
  7  # This file is licensed under the GNU Lesser General Public License 
  8  # (LGPL).  See LICENSE.txt for details. 
  9   
 10  """utils.py -- Utility functions used by Gnuplot. 
 11   
 12  This module contains utility functions used by Gnuplot.py which aren't 
 13  particularly gnuplot-related. 
 14   
 15  """ 
 16   
 17  import string 
 18  try: 
 19      import numpy 
 20  except ImportError: 
 21      import Numeric as numpy 
 22      numpy.float32=numpy.Float32 
 23      numpy.float_=numpy.Float 
 24       
25 -def float_array(m):
26 """Return the argument as a numpy array of type at least 'Float32'. 27 28 Leave 'Float64' unchanged, but upcast all other types to 29 'Float32'. Allow also for the possibility that the argument is a 30 python native type that can be converted to a numpy array using 31 'numpy.asarray()', but in that case don't worry about 32 downcasting to single-precision float. 33 34 """ 35 36 try: 37 # Try Float32 (this will refuse to downcast) 38 return numpy.asarray(m, numpy.float32) 39 except TypeError: 40 # That failure might have been because the input array was 41 # of a wider data type than float32; try to convert to the 42 # largest floating-point type available: 43 # NOTE TBD: I'm not sure float_ is the best data-type for this... 44 try: 45 return numpy.asarray(m, numpy.float_) 46 except TypeError: 47 # TBD: Need better handling of this error! 48 print "Fatal: array dimensions not equal!" 49 return None
50
51 -def write_array(f, set, 52 item_sep=' ', 53 nest_prefix='', nest_suffix='\n', nest_sep=''):
54 """Write an array of arbitrary dimension to a file. 55 56 A general recursive array writer. The last four parameters allow 57 a great deal of freedom in choosing the output format of the 58 array. The defaults for those parameters give output that is 59 gnuplot-readable. But using '(",", "{", "}", ",\n")' would output 60 an array in a format that Mathematica could read. 'item_sep' 61 should not contain '%' (or if it does, it should be escaped to 62 '%%') since it is put into a format string. 63 64 The default 2-d file organization:: 65 66 set[0,0] set[0,1] ... 67 set[1,0] set[1,1] ... 68 69 The 3-d format:: 70 71 set[0,0,0] set[0,0,1] ... 72 set[0,1,0] set[0,1,1] ... 73 74 set[1,0,0] set[1,0,1] ... 75 set[1,1,0] set[1,1,1] ... 76 77 """ 78 79 if len(set.shape) == 1: 80 (columns,) = set.shape 81 assert columns > 0 82 fmt = string.join(['%s'] * columns, item_sep) 83 f.write(nest_prefix) 84 f.write(fmt % tuple(set.tolist())) 85 f.write(nest_suffix) 86 elif len(set.shape) == 2: 87 # This case could be done with recursion, but `unroll' for 88 # efficiency. 89 (points, columns) = set.shape 90 assert points > 0 and columns > 0 91 fmt = string.join(['%s'] * columns, item_sep) 92 f.write(nest_prefix + nest_prefix) 93 f.write(fmt % tuple(set[0].tolist())) 94 f.write(nest_suffix) 95 for point in set[1:]: 96 f.write(nest_sep + nest_prefix) 97 f.write(fmt % tuple(point.tolist())) 98 f.write(nest_suffix) 99 f.write(nest_suffix) 100 else: 101 # Use recursion for three or more dimensions: 102 assert set.shape[0] > 0 103 f.write(nest_prefix) 104 write_array(f, set[0], 105 item_sep, nest_prefix, nest_suffix, nest_sep) 106 for subset in set[1:]: 107 f.write(nest_sep) 108 write_array(f, subset, 109 item_sep, nest_prefix, nest_suffix, nest_sep) 110 f.write(nest_suffix)
111