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      # assume this is pypy and retry 
 22      import numpypy 
 23      import numpy 
 24   
 25  from PyFoam.ThirdParty.six import print_ 
 26   
27 -def float_array(m):
28 """Return the argument as a numpy array of type at least 'Float32'. 29 30 Leave 'Float64' unchanged, but upcast all other types to 31 'Float32'. Allow also for the possibility that the argument is a 32 python native type that can be converted to a numpy array using 33 'numpy.asarray()', but in that case don't worry about 34 downcasting to single-precision float. 35 36 """ 37 38 try: 39 # Try Float32 (this will refuse to downcast) 40 return numpy.asarray(m, numpy.float32) 41 except TypeError: 42 # That failure might have been because the input array was 43 # of a wider data type than float32; try to convert to the 44 # largest floating-point type available: 45 # NOTE TBD: I'm not sure float_ is the best data-type for this... 46 try: 47 return numpy.asarray(m, numpy.float_) 48 except TypeError: 49 # TBD: Need better handling of this error! 50 print_("Fatal: array dimensions not equal!") 51 return None
52
53 -def write_array(f, set, 54 item_sep=' ', 55 nest_prefix='', nest_suffix='\n', nest_sep=''):
56 """Write an array of arbitrary dimension to a file. 57 58 A general recursive array writer. The last four parameters allow 59 a great deal of freedom in choosing the output format of the 60 array. The defaults for those parameters give output that is 61 gnuplot-readable. But using '(",", "{", "}", ",\n")' would output 62 an array in a format that Mathematica could read. 'item_sep' 63 should not contain '%' (or if it does, it should be escaped to 64 '%%') since it is put into a format string. 65 66 The default 2-d file organization:: 67 68 set[0,0] set[0,1] ... 69 set[1,0] set[1,1] ... 70 71 The 3-d format:: 72 73 set[0,0,0] set[0,0,1] ... 74 set[0,1,0] set[0,1,1] ... 75 76 set[1,0,0] set[1,0,1] ... 77 set[1,1,0] set[1,1,1] ... 78 79 """ 80 81 if len(set.shape) == 1: 82 (columns,) = set.shape 83 assert columns > 0 84 fmt = item_sep.join(['%s'] * columns) 85 f.write(nest_prefix) 86 f.write(fmt % tuple(set.tolist())) 87 f.write(nest_suffix) 88 elif len(set.shape) == 2: 89 # This case could be done with recursion, but `unroll' for 90 # efficiency. 91 (points, columns) = set.shape 92 assert points > 0 and columns > 0 93 fmt = item_sep.join(['%s'] * columns) 94 f.write(nest_prefix + nest_prefix) 95 f.write(fmt % tuple(set[0].tolist())) 96 f.write(nest_suffix) 97 for point in set[1:]: 98 f.write(nest_sep + nest_prefix) 99 f.write(fmt % tuple(point.tolist())) 100 f.write(nest_suffix) 101 f.write(nest_suffix) 102 else: 103 # Use recursion for three or more dimensions: 104 assert set.shape[0] > 0 105 f.write(nest_prefix) 106 write_array(f, set[0], 107 item_sep, nest_prefix, nest_suffix, nest_sep) 108 for subset in set[1:]: 109 f.write(nest_sep) 110 write_array(f, subset, 111 item_sep, nest_prefix, nest_suffix, nest_sep) 112 f.write(nest_suffix)
113 114 # Should work with Python3 and Python2 115