[GRASS-SVN] r53838 - grass/trunk/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 15 06:00:38 PST 2012


Author: huhabla
Date: 2012-11-15 06:00:37 -0800 (Thu, 15 Nov 2012)
New Revision: 53838

Modified:
   grass/trunk/lib/python/array.py
Log:
PEP8 compliance.
Added 3D array using r3.out.bin and r3.in.bin.


Modified: grass/trunk/lib/python/array.py
===================================================================
--- grass/trunk/lib/python/array.py	2012-11-15 13:27:01 UTC (rev 53837)
+++ grass/trunk/lib/python/array.py	2012-11-15 14:00:37 UTC (rev 53838)
@@ -1,6 +1,6 @@
 """!@package grass.script.array
 
- at brief GRASS Python scripting module (rasters with numpy)
+ at brief GRASS Python scripting module (2D and 3D raster with numpy)
 
 Functions to use GRASS rasters with NumPy.
 
@@ -30,36 +30,37 @@
 
 import core as grass
 
+
 class array(numpy.memmap):
-    def __new__(cls, dtype = numpy.double):
+    def __new__(cls, dtype=numpy.double):
         """!Define new numpy array
 
         @param cls
         @param dtype data type (default: 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
-    
+        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)
+        numpy.memmap._close(self)
+        if isinstance(self, array):
+            grass.try_remove(self.filename)
 
-    def read(self, mapname, null = None):
+    def read(self, mapname, null=None):
         """!Read raster map into array
 
         @param mapname name of raster map to be read
@@ -68,29 +69,29 @@
         @return 0 on success
         @return non-zero code on failure
         """
-	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':
+            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):
         """!Write array into raster map
 
         @param mapname name for raster map
@@ -101,40 +102,145 @@
         @return 0 on success
         @return non-zero code on failure
         """
-	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'])
-    
+        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'])
+
+
+class array3d(numpy.memmap):
+    def __new__(cls, dtype=numpy.double):
+        """!Define new 3d numpy array
+
+        @param cls
+        @param dtype data type (default: numpy.double)
+        """
+        reg = grass.region(True)
+        r = reg['rows3']
+        c = reg['cols3']
+        d = reg['depths']
+        shape = (d, 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):
+        """!Read 3D raster map into array
+
+        @param mapname name of 3D raster map to be read
+        @param null null value
+
+        @return 0 on success
+        @return non-zero code on failure
+        """
+        kind = self.dtype.kind
+        size = self.dtype.itemsize
+
+        if kind == 'f':
+            flags = None # default is double
+        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(
+            'r3.out.bin',
+            flags=flags,
+            input=mapname,
+            output=self.filename,
+            bytes=size,
+            null=null,
+            quiet=True)
+
+    def write(self, mapname, null=None, overwrite=None):
+        """!Write array into 3D raster map
+
+        @param mapname name for 3D raster map
+        @param null null value
+        @param overwrite True for overwriting existing raster maps
+
+        @return 0 on success
+        @return non-zero code on failure
+        """
+        kind = self.dtype.kind
+        size = self.dtype.itemsize
+
+        if kind == 'f':
+            if size != 4 and size != 8:
+                raise ValueError(_('Invalid FP size <%d>') % size)
+        elif kind in 'biu':
+            if size not in [1, 2, 4, 8]:
+                raise ValueError(_('Invalid integer size <%d>') % size)
+            flags = 'i'
+        else:
+            raise ValueError(_('Invalid kind <%s>') % kind)
+
+        reg = grass.region(True)
+
+        return grass.run_command(
+            'r3.in.bin',
+            flags=flags,
+            input=self.filename,
+            output=mapname,
+            bytes=size,
+            null=null,
+            overwrite=overwrite,
+            verbose=True,
+            north=reg['n'],
+            south=reg['s'],
+            top=reg['t'],
+            bottom=reg['b'],
+            east=reg['e'],
+            west=reg['w'],
+            depths=reg['depths'],
+            rows=reg['rows3'],
+            cols=reg['cols3'])



More information about the grass-commit mailing list