array.py 3.1 KB

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