temporal_raster3d_algebra.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """!@package grass.temporal
  2. Temporal raster algebra
  3. (C) 2013 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. :authors: Thomas Leppelt and Soeren Gebbert
  8. """
  9. from .temporal_raster_base_algebra import *
  10. ###############################################################################
  11. class TemporalRaster3DAlgebraParser(TemporalRasterBaseAlgebraParser):
  12. """The temporal raster algebra class"""
  13. def __init__(self, pid=None, run=False, debug=True, spatial=False,
  14. register_null=False, dry_run=False, nprocs=1):
  15. TemporalRasterBaseAlgebraParser.__init__(self, pid=pid, run=run, debug=debug,
  16. spatial=spatial, register_null=register_null,
  17. dry_run=dry_run, nprocs=nprocs)
  18. self.m_mapcalc = pymod.Module('r3.mapcalc')
  19. self.m_mremove = pymod.Module('g.remove')
  20. def parse(self, expression, basename = None, overwrite=False):
  21. # Check for space time dataset type definitions from temporal algebra
  22. l = TemporalRasterAlgebraLexer()
  23. l.build()
  24. l.lexer.input(expression)
  25. while True:
  26. tok = l.lexer.token()
  27. if not tok: break
  28. if tok.type == "STVDS" or tok.type == "STRDS" or tok.type == "STR3DS":
  29. raise SyntaxError("Syntax error near '%s'" %(tok.type))
  30. self.lexer = TemporalRasterAlgebraLexer()
  31. self.lexer.build()
  32. self.parser = yacc.yacc(module=self, debug=self.debug)
  33. self.overwrite = overwrite
  34. self.count = 0
  35. self.stdstype = "str3ds"
  36. self.maptype = "raster_3d"
  37. self.mapclass = Raster3DDataset
  38. self.basename = basename
  39. self.expression = expression
  40. self.parser.parse(expression)
  41. return self.process_chain_dict
  42. ######################### Temporal functions ##############################
  43. def p_statement_assign(self, t):
  44. # The expression should always return a list of maps.
  45. """
  46. statement : stds EQUALS expr
  47. """
  48. TemporalRasterBaseAlgebraParser.p_statement_assign(self, t)
  49. def p_ts_neighbor_operation(self, t):
  50. # Examples:
  51. # A[1,0,-1]
  52. # B[-2]
  53. # C[1,-2,1,3]
  54. """
  55. expr : stds L_SPAREN number COMMA number COMMA number R_SPAREN
  56. | stds L_SPAREN number R_SPAREN
  57. | stds L_SPAREN number COMMA number COMMA number COMMA number R_SPAREN
  58. """
  59. # Check input stds.
  60. maplist = self.check_stds(t[1])
  61. t_neighbor = 0
  62. row_neighbor = None
  63. col_neighbor = None
  64. depth_neighbor = None
  65. if len(t) == 5:
  66. t_neighbor = t[3]
  67. elif len(t) == 9:
  68. row_neighbor = t[3]
  69. col_neighbor = t[5]
  70. depth_neighbor = t[7]
  71. elif len(t) == 11:
  72. t_neighbor = t[9]
  73. row_neighbor = t[3]
  74. col_neighbor = t[5]
  75. depth_neighbor = t[7]
  76. if self.run:
  77. resultlist = []
  78. max_index = len(maplist)
  79. for map_i in maplist:
  80. # Get map index and temporal extent.
  81. map_index = maplist.index(map_i)
  82. new_index = map_index + t_neighbor
  83. if new_index < max_index and new_index >= 0:
  84. map_i_t_extent = map_i.get_temporal_extent()
  85. # Get neighboring map and set temporal extent.
  86. map_n = maplist[new_index]
  87. # Generate an intermediate map for the result map list.
  88. map_new = self.generate_new_map(map_n, bool_op = 'and', copy = True)
  89. map_new.set_temporal_extent(map_i_t_extent)
  90. # Create r.mapcalc expression string for the operation.
  91. if "cmd_list" in dir(map_new) and len(t) == 5:
  92. cmdstring = "%s" %(map_new.cmd_list)
  93. elif "cmd_list" not in dir(map_new) and len(t) == 5:
  94. cmdstring = "%s" %(map_n.get_id())
  95. elif "cmd_list" in dir(map_new) and len(t) in (9,11):
  96. cmdstring = "%s[%s,%s,%s]" %(map_new.cmd_list, row_neighbor, col_neighbor, depth_neighbor)
  97. elif "cmd_list" not in dir(map_new) and len(t) in (9,11):
  98. cmdstring = "%s[%s,%s,%s]" %(map_n.get_id(), row_neighbor, col_neighbor, depth_neighbor)
  99. # Set new command list for map.
  100. map_new.cmd_list = cmdstring
  101. # Append map with updated command list to result list.
  102. resultlist.append(map_new)
  103. t[0] = resultlist
  104. ###############################################################################
  105. if __name__ == "__main__":
  106. import doctest
  107. doctest.testmod()