[GRASS-SVN] r43919 - grass/branches/releasebranch_6_4/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 14 13:12:50 EDT 2010


Author: martinl
Date: 2010-10-14 10:12:50 -0700 (Thu, 14 Oct 2010)
New Revision: 43919

Added:
   grass/branches/releasebranch_6_4/lib/python/array.py
Log:
backport array.py module from devbr6


Added: grass/branches/releasebranch_6_4/lib/python/array.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/array.py	                        (rev 0)
+++ grass/branches/releasebranch_6_4/lib/python/array.py	2010-10-14 17:12:50 UTC (rev 43919)
@@ -0,0 +1,112 @@
+"""!@package grass.script.array
+
+ at brief GRASS Python scripting module
+
+Functions to use GRASS rasters with NumPy.
+
+Usage:
+
+ at code
+from grass.script import array as garray
+...
+ at endcode
+
+(C) 2010 by Glynn Clements and the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Glynn Clements
+
+"""
+
+import os
+import numpy
+
+import core as grass
+
+class array(numpy.memmap):
+    def __new__(cls, dtype = numpy.double):
+	reg = grass.region()
+	r = reg['rows']
+	c = reg['cols']
+	shape = (r, c)
+
+	filename = grass.tempfile()
+
+	self = numpy.memmap.__new__(
+	    cls,
+	    filename = filename,
+	    dtype = dtype,
+	    mode = 'w+',
+	    shape = shape)
+
+	self.filename = filename
+	return self
+
+    def _close(self):
+	numpy.memmap._close(self)
+	if isinstance(self, array):
+	    grass.try_remove(self.filename)
+
+    def read(self, mapname, null = None):
+	kind = self.dtype.kind
+	size = self.dtype.itemsize
+
+	if kind == 'f':
+	    flags = 'f'
+	elif kind in 'biu':
+	    flags = 'i'
+	else:
+	    raise ValueError('invalid kind <%s>' % kind)
+
+	if size not in [1,2,4,8]:
+	    raise ValueError('invalid size <%d>' % size)
+
+	return grass.run_command(
+	    'r.out.bin',
+	    flags = flags,
+	    input = mapname,
+	    output = self.filename,
+	    bytes = size,
+	    null = null,
+	    quiet = True)
+	
+
+    def write(self, mapname, title = None, null = None, overwrite = None):
+	kind = self.dtype.kind
+	size = self.dtype.itemsize
+
+	if kind == 'f':
+	    if size == 4:
+		flags = 'f'
+	    elif size == 8:
+		flags = 'd'
+	    else:
+		raise ValueError('invalid FP size <%d>' % size)
+	    size = None
+	elif kind in 'biu':
+	    if size not in [1,2,4]:
+		raise ValueError('invalid integer size <%d>' % size)
+	    flags = None
+	else:
+	    raise ValueError('invalid kind <%s>' % kind)
+
+	reg = grass.region()
+
+	return grass.run_command(
+	    'r.in.bin',
+	    flags = flags,
+	    input = self.filename,
+	    output = mapname,
+	    title = title,
+	    bytes = size,
+	    anull = null,
+	    overwrite = overwrite,
+	    verbose = True,
+	    north = reg['n'],
+	    south = reg['s'],
+	    east  = reg['e'],
+	    west  = reg['w'],
+	    rows  = reg['rows'],
+	    cols  = reg['cols'])


Property changes on: grass/branches/releasebranch_6_4/lib/python/array.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list