modules.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. Interface to GRASS GIS modules
  2. ==============================
  3. In "modules", GRASS GIS modules are represented as objects. These objects
  4. are generated based on the XML module description that is used also for
  5. the generation of the graphical user interface (GUI). ::
  6. >>> from pygrass.modules import Module
  7. >>> slope_aspect = Module("r.slope.aspect", elevation='elevation',
  8. ... slope='slp', aspect='asp',
  9. ... format='percent', overwrite=True)
  10. It is possible to create a run-able module object and run it later (this
  11. is also needed for registering GRASS GIS commands with more than one dot
  12. in their name, e.g. r.slope.aspect):
  13. >>> slope_aspect = Module("r.slope.aspect", elevation='elevation',
  14. ... slope='slp', aspect='asp',
  15. ... format='percent', overwrite=True, run_=False)
  16. Then we can run the module with: ::
  17. >>> slope_aspect()
  18. or using the run method: ::
  19. >>> slope_aspect.run()
  20. It is possible to initialize a module, and give the parameters later: ::
  21. >>> slope_aspect = Module("r.slope.aspect")
  22. >>> slope_aspect(elevation='elevation', slope='slp', aspect='asp',
  23. ... format='percent', overwrite=True)
  24. Create the module object input step by step and run later: ::
  25. >>> slope_aspect = Module("r.slope.aspect")
  26. >>> slope_aspect.inputs['elevation']
  27. Parameter <elevation> (required:yes, type:raster, multiple:no)
  28. >>> slope_aspect.inputs["elevation"].value = "elevation"
  29. >>> slope_aspect.inputs["format"]
  30. Parameter <format> (required:no, type:string, multiple:no)
  31. >>> print slope_aspect.inputs["format"].__doc__
  32. format: 'degrees', optional, string
  33. Format for reporting the slope
  34. Values: 'degrees', 'percent'
  35. >>> slope_aspect.inputs["format"].value = 'percents'
  36. Traceback (most recent call last):
  37. ...
  38. ValueError: The Parameter <format>, must be one of: ['degrees', 'percent']
  39. >>> slope_aspect.inputs["format"].value = 'percent'
  40. >>> slope_aspect.flags = "g"
  41. Traceback (most recent call last):
  42. ...
  43. ValueError: Flag not valid, valid flag are: ['a']
  44. >>> slope_aspect.flags = "a"
  45. >>> slope_aspect.flags_dict['overwrite']
  46. Flag <overwrite> (Allow output files to overwrite existing files)
  47. >>> slope_aspect.flags_dict['overwrite'].value = True
  48. >>> slope_aspect()
  49. It is possible to access the module descriptions (name, one line description,
  50. keywords, label) with:
  51. >>> slope_aspect.name
  52. 'r.slope.aspect'
  53. >>> slope_aspect.description
  54. 'Aspect is calculated counterclockwise from east.'
  55. >>> slope_aspect.keywords
  56. 'raster, terrain'
  57. >>> slope_aspect.label
  58. 'Generates raster maps of slope, aspect, curvatures and partial derivatives from a elevation raster map.'
  59. and get the module documentation with: ::
  60. >>> print slope_aspect.__doc__
  61. r.slope.aspect(elevation=elevation, slope=None, aspect=None
  62. format=percent, prec=None, pcurv=None
  63. tcurv=None, dx=None, dy=None
  64. dxx=None, dyy=None, dxy=None
  65. zfactor=None, min_slp_allowed=None)
  66. <BLANKLINE>
  67. Parameters
  68. ----------
  69. <BLANKLINE>
  70. <BLANKLINE>
  71. elevation: required, string
  72. Name of input elevation raster map
  73. slope: optional, string
  74. Name for output slope raster map
  75. aspect: optional, string
  76. Name for output aspect raster map
  77. format: 'degrees', optional, string
  78. Format for reporting the slope
  79. Values: 'degrees', 'percent'
  80. prec: 'float', optional, string
  81. Type of output aspect and slope maps
  82. Values: 'default', 'double', 'float', 'int'
  83. pcurv: optional, string
  84. Name for output profile curvature raster map
  85. tcurv: optional, string
  86. Name for output tangential curvature raster map
  87. dx: optional, string
  88. Name for output first order partial derivative dx (E-W slope) raster map
  89. dy: optional, string
  90. Name for output first order partial derivative dy (N-S slope) raster map
  91. dxx: optional, string
  92. Name for output second order partial derivative dxx raster map
  93. dyy: optional, string
  94. Name for output second order partial derivative dyy raster map
  95. dxy: optional, string
  96. Name for output second order partial derivative dxy raster map
  97. zfactor: 1.0, optional, float
  98. Multiplicative factor to convert elevation units to meters
  99. min_slp_allowed: optional, float
  100. Minimum slope val. (in percent) for which aspect is computed
  101. <BLANKLINE>
  102. Flags
  103. ------
  104. <BLANKLINE>
  105. a: None
  106. Do not align the current region to the elevation layer
  107. overwrite: None
  108. Allow output files to overwrite existing files
  109. verbose: None
  110. Verbose module output
  111. quiet: None
  112. Quiet module output
  113. For each input and output parameter it is possible to obtain specific
  114. information. To see all module inputs, just type: ::
  115. >>> slope_aspect.inputs #doctest: +NORMALIZE_WHITESPACE
  116. TypeDict([('elevation', Parameter <elevation> (required:yes, type:raster, multiple:no)), ('format', Parameter <format> (required:no, type:string, multiple:no)), ('prec', Parameter <prec> (required:no, type:string, multiple:no)), ('zfactor', Parameter <zfactor> (required:no, type:float, multiple:no)), ('min_slp_allowed', Parameter <min_slp_allowed> (required:no, type:float, multiple:no))])
  117. To get information for each parameter: ::
  118. >>> slope_aspect.inputs["elevation"].description
  119. 'Name of input elevation raster map'
  120. >>> slope_aspect.inputs["elevation"].type
  121. 'raster'
  122. >>> slope_aspect.inputs["elevation"].typedesc
  123. 'string'
  124. >>> slope_aspect.inputs["elevation"].multiple
  125. False
  126. >>> slope_aspect.inputs["elevation"].required
  127. True
  128. Or get a small documentation for each parameter with:
  129. >>> print slope_aspect.inputs["elevation"].__doc__
  130. elevation: required, string
  131. Name of input elevation raster map
  132. User or developer can check which parameters have been set, with: ::
  133. if slope_aspect.outputs['aspect'].value == None:
  134. print "Aspect is not computed"
  135. After we set the parameters and run the module, the execution of the module
  136. instantiate a popen attribute to the class. The `Popen`_ class allow user
  137. to kill/wait/ the process. ::
  138. >>> slope_aspect = Module('r.slope.aspect')
  139. >>> slope_aspect(elevation='elevation', slope='slp', aspect='asp', overwrite=True, finish_=False)
  140. >>> slope_aspect.popen.wait() # *.kill(), *.terminate()
  141. 0
  142. >>> out, err = slope_aspect.popen.communicate()
  143. >>> print err #doctest: +NORMALIZE_WHITESPACE
  144. 100%
  145. Aspect raster map <asp> complete
  146. Slope raster map <slp> complete
  147. <BLANKLINE>
  148. On the above example we use a new parameter `finish_`, if is set to True, the
  149. run method, automatically store the stdout and stderr to stdout and stderr
  150. attributes of the class: ::
  151. >>> slope_aspect = Module('r.slope.aspect')
  152. >>> slope_aspect(elevation='elevation', slope='slp', aspect='asp', overwrite=True, finish_=True)
  153. >>> print slope_aspect.stderr #doctest: +NORMALIZE_WHITESPACE
  154. 100%
  155. Aspect raster map <asp> complete
  156. Slope raster map <slp> complete
  157. <BLANKLINE>
  158. Another example of use: ::
  159. >>> info = Module("r.info", map="elevation", flags="r", finish_=True)
  160. >>> from pygrass.modules import stdout2dict
  161. >>> stdout2dict(info.stdout)
  162. {'max': '156.3299', 'min': '55.57879'}
  163. >>> info = Module("r.info", map="elevation", flags="r", finish_=False)
  164. >>> category = Module("r.category", map="elevation",
  165. ... stdin_=info.popen.stdout, finish_=True)
  166. .. _Popen: http://docs.python.org/library/subprocess.html#Popen