[GRASS-SVN] r67710 - grass/trunk/lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jan 30 19:54:13 PST 2016


Author: annakrat
Date: 2016-01-30 19:54:13 -0800 (Sat, 30 Jan 2016)
New Revision: 67710

Modified:
   grass/trunk/lib/python/script/array.py
Log:
pythonlib/array: fix reading raster on Windows, make read method deprecated

Modified: grass/trunk/lib/python/script/array.py
===================================================================
--- grass/trunk/lib/python/script/array.py	2016-01-31 00:38:42 UTC (rev 67709)
+++ grass/trunk/lib/python/script/array.py	2016-01-31 03:54:13 UTC (rev 67710)
@@ -32,13 +32,11 @@
 ... map2d_1.write(mapname="map2d_1", overwrite=True)
 0
 >>>
->>> # We create a new array and read map2d_1 to modify it
-... map2d_2 = garray.array()
+>>> # We create a new array from raster map2d_1 to modify it
+... map2d_2 = garray.array(mapname="map2d_1")
 >>> # Don't do map2d_2 = map2d_1 % 3
 ... # because: this will overwrite the internal temporary filename
-... map2d_2.read("map2d_1")
-0
->>> map2d_2 %= 3
+... map2d_2 %= 3
 >>> # Show the result
 ... print(map2d_2)
 [[ 0.  1.  2.  0.  1.  2.]
@@ -80,13 +78,11 @@
 ... # with name map3d_1
 ... map3d_1.write(mapname="map3d_1", overwrite=True)
 0
->>> # We create a new 3D array and read map3d_1 to modify it
-... map3d_2 = garray.array3d()
+>>> # We create a new 3D array from 3D raster map3d_1 to modify it
+... map3d_2 = garray.array3d(mapname="map3d_1")
 >>> # Don't do map3d_2 = map3d_1 % 3
 ... # because: this will overwrite the internal temporary filename
-... map3d_2.read("map3d_1")
-0
->>> map3d_2 %= 3
+... map3d_2 %= 3
 >>> # Show the result
 ... print(map3d_2)
 [[[ 0.  1.  2.  0.  1.  2.]
@@ -114,7 +110,9 @@
 
 .. sectionauthor:: Glynn Clements
 """
+
 from __future__ import absolute_import
+import sys
 
 import numpy
 
@@ -135,7 +133,7 @@
 ###############################################################################
 
 class array(numpy.memmap):
-    def __new__(cls, dtype=numpy.double):
+    def __new__(cls, mapname=None, null=None, dtype=numpy.double):
         """Define new numpy array
 
         :param cls:
@@ -147,12 +145,35 @@
         shape = (r, c)
 
         tempfile = _tempfile()
+        if mapname:
+            kind = numpy.dtype(dtype).kind
+            size = numpy.dtype(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)
+
+            gcore.run_command(
+                'r.out.bin',
+                flags=flags,
+                input=mapname,
+                output=tempfile.filename,
+                bytes=size,
+                null=null,
+                quiet=True,
+                overwrite=True)
+
         self = numpy.memmap.__new__(
             cls,
             filename=tempfile.filename,
             dtype=dtype,
-            mode='w+',
+            mode='r+',
             shape=shape)
 
         self.tempfile = tempfile
@@ -167,7 +188,14 @@
 
         :return: 0 on success
         :return: non-zero code on failure
+
+        .. deprecated:: 7.1
+        Instead reading the map after creating the array,
+        pass the map name in the array constructor.
         """
+        if sys.platform == 'win32':
+            gcore.warning(_("grass.script.array.read is deprecated and does not"
+                            " work on MS Windows, pass raster name in the constructor"))
         kind = self.dtype.kind
         size = self.dtype.itemsize
 
@@ -252,7 +280,7 @@
 
 
 class array3d(numpy.memmap):
-    def __new__(cls, dtype=numpy.double):
+    def __new__(cls, mapname=None, null=None, dtype=numpy.double):
         """Define new 3d numpy array
 
         :param cls:
@@ -265,12 +293,35 @@
         shape = (d, r, c)
 
         tempfile = _tempfile()
+        if mapname:
+            kind = numpy.dtype(dtype).kind
+            size = numpy.dtype(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)
+
+            gcore.run_command(
+                'r3.out.bin',
+                flags=flags,
+                input=mapname,
+                output=tempfile.filename,
+                bytes=size,
+                null=null,
+                quiet=True,
+                overwrite=True)
+
         self = numpy.memmap.__new__(
             cls,
             filename=tempfile.filename,
             dtype=dtype,
-            mode='w+',
+            mode='r+',
             shape=shape)
 
         self.tempfile = tempfile
@@ -286,7 +337,14 @@
 
         :return: 0 on success
         :return: non-zero code on failure
+
+        .. deprecated:: 7.1
+        Instead reading the map after creating the array,
+        pass the map name in the array constructor.
         """
+        if sys.platform == 'win32':
+            gcore.warning(_("grass.script.array3d.read is deprecated and does not"
+                            " work on MS Windows, pass 3D raster name in the constructor"))
         kind = self.dtype.kind
         size = self.dtype.itemsize
 



More information about the grass-commit mailing list