univar_statistics.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.print_gridded_dataset_univar_statistics(type, input, where, extended, header, fs)
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from space_time_datasets_tools import *
  17. ###############################################################################
  18. def print_gridded_dataset_univar_statistics(type, input, where, extended, header, fs):
  19. """!Print univariate statistics for a space time raster or raster3d dataset
  20. param type Must be "strds" or "str3ds"
  21. @param input The name of the space time dataset
  22. @param where A temporal database where statement
  23. @param extended If True compute extended statistics
  24. @param header If True print column names as header
  25. @param fs Field separator
  26. """
  27. # We need a database interface
  28. dbif = sql_database_interface_connection()
  29. dbif.connect()
  30. mapset = core.gisenv()["MAPSET"]
  31. if input.find("@") >= 0:
  32. id = input
  33. else:
  34. id = input + "@" + mapset
  35. sp = dataset_factory(type, id)
  36. if sp.is_in_db(dbif) == False:
  37. dbif.close()
  38. core.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  39. sp.select(dbif)
  40. rows = sp.get_registered_maps("id,start_time,end_time", where, "start_time", dbif)
  41. if not rows:
  42. dbif.close()
  43. core.fatal(_("Space time %s dataset <%s> is empty") % (sp.get_new_map_instance(None).get_type(), out_id))
  44. if header == True:
  45. print "id" + fs + "start" + fs + "end" + fs + "mean" + fs + "min" + fs + "max" + fs,
  46. print "mean_of_abs" + fs + "stddev" + fs + "variance" + fs,
  47. if extended == True:
  48. print "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells" + fs,
  49. print "first_quartile" + fs + "median" + fs + "third_quartile" + fs + "percentile_90"
  50. else:
  51. print "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  52. for row in rows:
  53. id = row["id"]
  54. start = row["start_time"]
  55. end = row["end_time"]
  56. flag="g"
  57. if extended == True:
  58. flag += "e"
  59. if type == "strds":
  60. stats = core.parse_command("r.univar", map=id, flags=flag)
  61. elif type == "str3ds":
  62. stats = core.parse_command("r3.univar", map=id, flags=flag)
  63. print str(id) + fs + str(start) + fs + str(end),
  64. print fs + str(stats["mean"]) + fs + str(stats["min"]) + fs + str(stats["max"]) + fs + str(stats["mean_of_abs"]),
  65. print fs + str(stats["stddev"]) + fs + str(stats["variance"]) + fs + str(stats["coeff_var"]) + fs + str(stats["sum"]),
  66. if extended == True:
  67. print fs + str(stats["null_cells"]) + fs + str(stats["cells"]) + fs,
  68. print str(stats["first_quartile"]) + fs + str(stats["median"]) + fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  69. else:
  70. print fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  71. dbif.close()
  72. ###############################################################################
  73. def print_vector_dataset_univar_statistics(input, twhere, layer, type, column, where, extended, header, fs):
  74. """!Print univariate statistics for a space time vector dataset
  75. @param input The name of the space time dataset
  76. @param twhere A temporal database where statement
  77. @param layer The layer number used in case no layer is present in the temporal dataset
  78. @param type options: point,line,boundary,centroid,area
  79. @param column The name of the attribute column
  80. @param where A temporal database where statement
  81. @param extended If True compute extended statistics
  82. @param header If True print column names as header
  83. @param fs Field separator
  84. """
  85. # We need a database interface
  86. dbif = sql_database_interface_connection()
  87. dbif.connect()
  88. mapset = core.gisenv()["MAPSET"]
  89. if input.find("@") >= 0:
  90. id = input
  91. else:
  92. id = input + "@" + mapset
  93. sp = dataset_factory("stvds", id)
  94. if sp.is_in_db(dbif) == False:
  95. dbif.close()
  96. core.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  97. sp.select(dbif)
  98. rows = sp.get_registered_maps("id,start_time,end_time,layer", twhere, "start_time", dbif)
  99. if not rows:
  100. dbif.close()
  101. core.fatal(_("Space time %s dataset <%s> is empty") % (sp.get_new_map_instance(None).get_type(), out_id))
  102. string = ""
  103. if header == True:
  104. string += "id" + fs + "start" + fs + "end" + fs + "n" + fs + "nmissing" + fs + "nnull" + fs
  105. string += "min" + fs + "max" + fs + "range"
  106. if type == "point" or type == "centroid":
  107. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" + fs + "population_variance" + fs
  108. string += "population_coeff_variation" + fs + "sample_stddev" + fs + "sample_variance" + fs
  109. string += "kurtosis" + fs + "skewness"
  110. if extended == True:
  111. string+= fs + "first_quartile" + fs + "median" + fs + "third_quartile" + fs + "percentile_90"
  112. print string
  113. for row in rows:
  114. id = row["id"]
  115. start = row["start_time"]
  116. end = row["end_time"]
  117. mylayer = row["layer"]
  118. flags="g"
  119. if extended == True:
  120. flags += "e"
  121. if not mylayer:
  122. mylayer = layer
  123. stats = core.parse_command("v.univar", map=id, where=where, column=column, layer=mylayer, type=type, flags=flags)
  124. string = ""
  125. if stats:
  126. string += str(id) + fs + str(start) + fs + str(end)
  127. string += fs + str(stats["n"]) + fs + str(stats["nmissing"]) + fs + str(stats["nnull"])
  128. if stats.has_key("min"):
  129. string += fs + str(stats["min"]) + fs + str(stats["max"]) + fs + str(stats["range"])
  130. else:
  131. string += fs + fs + fs
  132. if type == "point" or type == "centroid":
  133. if stats.has_key("mean"):
  134. string += fs + str(stats["mean"]) + fs + str(stats["mean_abs"]) + fs + str(stats["population_stddev"]) + fs + str(stats["population_variance"])
  135. string += fs + str(stats["population_coeff_variation"]) + fs + str(stats["sample_stddev"]) + fs + str(stats["sample_variance"])
  136. string += fs + str(stats["kurtosis"]) + fs + str(stats["skewness"])
  137. else:
  138. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  139. if extended == True:
  140. if stats.has_key("first_quartile"):
  141. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"]) + fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  142. else:
  143. string += fs + fs + fs + fs
  144. print string
  145. dbif.close()