[GRASS-SVN] r40556 - grass/trunk/lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Jan 19 10:48:31 EST 2010
Author: glynn
Date: 2010-01-19 10:48:30 -0500 (Tue, 19 Jan 2010)
New Revision: 40556
Added:
grass/trunk/lib/python/array.py
Modified:
grass/trunk/lib/python/Makefile
Log:
Add grass.script.array: read/write GRASS rasters to/from NumPy arrays
Create .pyc files
Modified: grass/trunk/lib/python/Makefile
===================================================================
--- grass/trunk/lib/python/Makefile 2010-01-19 15:43:36 UTC (rev 40555)
+++ grass/trunk/lib/python/Makefile 2010-01-19 15:48:30 UTC (rev 40556)
@@ -1,17 +1,19 @@
MODULE_TOPDIR = ../..
include $(MODULE_TOPDIR)/include/Make/Other.make
+include $(MODULE_TOPDIR)/include/Make/Python.make
include $(MODULE_TOPDIR)/include/Make/Doxygen.make
PYDIR = $(ETC)/python
GDIR = $(PYDIR)/grass
DSTDIR = $(GDIR)/script
-MODULES = core db raster vector
+MODULES = core db raster vector array
PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
+PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
-default: $(PYFILES) $(GDIR)/__init__.py
+default: $(PYFILES) $(PYCFILES) $(GDIR)/__init__.py $(GDIR)/__init__.pyc
$(PYDIR):
$(MKDIR) $@
Added: grass/trunk/lib/python/array.py
===================================================================
--- grass/trunk/lib/python/array.py (rev 0)
+++ grass/trunk/lib/python/array.py 2010-01-19 15:48:30 UTC (rev 40556)
@@ -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,
+ size = 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'])
More information about the grass-commit
mailing list