GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
array.py
Go to the documentation of this file.
1 """!@package grass.script.array
2 
3 @brief GRASS Python scripting module (rasters with numpy)
4 
5 Functions to use GRASS rasters with NumPy.
6 
7 Usage:
8 
9 @code
10  map = 'elevation'
11  x = garray.array()
12  x.read(map)
13  # calculate something on array
14  x[...] = x / 50.
15  x.write(map + ".new")
16 @endcode
17 
18 (C) 2010-2011 by Glynn Clements and the GRASS Development Team
19 This program is free software under the GNU General Public
20 License (>=v2). Read the file COPYING that comes with GRASS
21 for details.
22 
23 @author Glynn Clements
24 """
25 
26 import os
27 import numpy
28 
29 import core as grass
30 
31 # i18N
32 import gettext
33 gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
34 
35 class array(numpy.memmap):
36  def __new__(cls, dtype = numpy.double):
37  """!Define new numpy array
38 
39  @param cls
40  @param dtype data type (default: numpy.double)
41  """
42  reg = grass.region()
43  r = reg['rows']
44  c = reg['cols']
45  shape = (r, c)
46 
47  filename = grass.tempfile()
48 
49  self = numpy.memmap.__new__(
50  cls,
51  filename = filename,
52  dtype = dtype,
53  mode = 'w+',
54  shape = shape)
55 
56  self.filename = filename
57  return self
58 
59  def _close(self):
60  numpy.memmap._close(self)
61  if isinstance(self, array):
62  grass.try_remove(self.filename)
63 
64  def read(self, mapname, null = None):
65  """!Read raster map into array
66 
67  @param mapname name of raster map to be read
68  @param null null value
69 
70  @return 0 on success
71  @return non-zero code on failure
72  """
73  kind = self.dtype.kind
74  size = self.dtype.itemsize
75 
76  if kind == 'f':
77  flags = 'f'
78  elif kind in 'biu':
79  flags = 'i'
80  else:
81  raise ValueError(_('Invalid kind <%s>') % kind)
82 
83  if size not in [1,2,4,8]:
84  raise ValueError(_('Invalid size <%d>') % size)
85 
86  return grass.run_command(
87  'r.out.bin',
88  flags = flags,
89  input = mapname,
90  output = self.filename,
91  bytes = size,
92  null = null,
93  quiet = True)
94 
95  def write(self, mapname, title = None, null = None, overwrite = None):
96  """!Write array into raster map
97 
98  @param mapname name for raster map
99  @param title title for raster map
100  @param null null value
101  @param overwrite True for overwritting existing raster maps
102 
103  @return 0 on success
104  @return non-zero code on failure
105  """
106  kind = self.dtype.kind
107  size = self.dtype.itemsize
108 
109  if kind == 'f':
110  if size == 4:
111  flags = 'f'
112  elif size == 8:
113  flags = 'd'
114  else:
115  raise ValueError(_('Invalid FP size <%d>') % size)
116  size = None
117  elif kind in 'biu':
118  if size not in [1,2,4]:
119  raise ValueError(_('Invalid integer size <%d>') % size)
120  flags = None
121  else:
122  raise ValueError(_('Invalid kind <%s>') % kind)
123 
124  reg = grass.region()
125 
126  return grass.run_command(
127  'r.in.bin',
128  flags = flags,
129  input = self.filename,
130  output = mapname,
131  title = title,
132  bytes = size,
133  anull = null,
134  overwrite = overwrite,
135  verbose = True,
136  north = reg['n'],
137  south = reg['s'],
138  east = reg['e'],
139  west = reg['w'],
140  rows = reg['rows'],
141  cols = reg['cols'])
142 
def __new__
Define new numpy array.
Definition: array.py:36
def read
Read raster map into array.
Definition: array.py:64
def write
Write array into raster map.
Definition: array.py:95