array.py 3.0 KB

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