modules.rst 7.3 KB

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