univar_statistics.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. """
  2. Univariate statistic function for space time datasets
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. tgis.print_gridded_dataset_univar_statistics(type, input, output, where, extended, no_header, fs, rast_region)
  7. ..
  8. (C) 2012-2013 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. :authors: Soeren Gebbert
  13. """
  14. from open_stds import *
  15. import grass.script as gscript
  16. ###############################################################################
  17. def print_gridded_dataset_univar_statistics(type, input, output, where, extended,
  18. no_header=False, fs="|",
  19. rast_region=False):
  20. """Print univariate statistics for a space time raster or raster3d dataset
  21. :param type: Must be "strds" or "str3ds"
  22. :param input: The name of the space time dataset
  23. :param output: Name of the optional output file, if None stdout is used
  24. :param where: A temporal database where statement
  25. :param extended: If True compute extended statistics
  26. :param no_header: Supress the printing of column names
  27. :param fs: Field separator
  28. :param rast_region: If set True ignore the current region settings
  29. and use the raster map regions for univar statistical calculation.
  30. Only available for strds.
  31. """
  32. # We need a database interface
  33. dbif = SQLDatabaseInterfaceConnection()
  34. dbif.connect()
  35. sp = open_old_stds(input, type, dbif)
  36. if output is not None:
  37. out_file = open(output, "w")
  38. rows = sp.get_registered_maps(
  39. "id,start_time,end_time", where, "start_time", dbif)
  40. if not rows:
  41. dbif.close()
  42. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  43. 'sp': sp.get_new_map_instance(None).get_type(),
  44. 'i': sp.get_id()})
  45. if no_header is False:
  46. string = ""
  47. string += "id" + fs + "start" + fs + "end" + fs + "mean" + fs
  48. string += "min" + fs + "max" + fs
  49. string += "mean_of_abs" + fs + "stddev" + fs + "variance" + fs
  50. string += "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  51. if extended is True:
  52. string += fs + "first_quartile" + fs + "median" + fs
  53. string += "third_quartile" + fs + "percentile_90"
  54. if output is None:
  55. print string
  56. else:
  57. out_file.write(string + "\n")
  58. for row in rows:
  59. string = ""
  60. id = row["id"]
  61. start = row["start_time"]
  62. end = row["end_time"]
  63. flag = "g"
  64. if extended is True:
  65. flag += "e"
  66. if type == "strds" and rast_region is True:
  67. flag += "r"
  68. if type == "strds":
  69. stats = gscript.parse_command("r.univar", map=id, flags=flag)
  70. elif type == "str3ds":
  71. stats = gscript.parse_command("r3.univar", map=id, flags=flag)
  72. if not stats:
  73. if type == "strds":
  74. gscript.warning(_("Unable to get statistics for raster map "
  75. "<%s>") % id)
  76. elif type == "str3ds":
  77. gscript.warning(_("Unable to get statistics for 3d raster map"
  78. " <%s>") % id)
  79. continue
  80. string += str(id) + fs + str(start) + fs + str(end)
  81. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  82. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  83. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  84. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  85. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  86. if extended is True:
  87. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  88. string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  89. if output is None:
  90. print string
  91. else:
  92. out_file.write(string + "\n")
  93. dbif.close()
  94. if output is not None:
  95. out_file.close()
  96. ###############################################################################
  97. def print_vector_dataset_univar_statistics(input, output, twhere, layer, type, column,
  98. where, extended, no_header=False,
  99. fs="|"):
  100. """Print univariate statistics for a space time vector dataset
  101. :param input: The name of the space time dataset
  102. :param output: Name of the optional output file, if None stdout is used
  103. :param twhere: A temporal database where statement
  104. :param layer: The layer number used in case no layer is present
  105. in the temporal dataset
  106. :param type: options: point,line,boundary,centroid,area
  107. :param column: The name of the attribute column
  108. :param where: A temporal database where statement
  109. :param extended: If True compute extended statistics
  110. :param no_header: Supress the printing of column names
  111. :param fs: Field separator
  112. """
  113. # We need a database interface
  114. dbif = SQLDatabaseInterfaceConnection()
  115. dbif.connect()
  116. if output is not None:
  117. out_file = open(output, "w")
  118. mapset = get_current_mapset()
  119. if input.find("@") >= 0:
  120. id = input
  121. else:
  122. id = input + "@" + mapset
  123. sp = dataset_factory("stvds", id)
  124. if sp.is_in_db(dbif) is False:
  125. dbif.close()
  126. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> not found") % {
  127. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  128. sp.select(dbif)
  129. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  130. twhere, "start_time", dbif)
  131. if not rows:
  132. dbif.close()
  133. gscript.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  134. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  135. string = ""
  136. if no_header is False:
  137. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  138. fs + "nmissing" + fs + "nnull" + fs
  139. string += "min" + fs + "max" + fs + "range"
  140. if type == "point" or type == "centroid":
  141. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  142. fs + "population_variance" + fs
  143. string += "population_coeff_variation" + fs + \
  144. "sample_stddev" + fs + "sample_variance" + fs
  145. string += "kurtosis" + fs + "skewness"
  146. if extended is True:
  147. string += fs + "first_quartile" + fs + "median" + fs + \
  148. "third_quartile" + fs + "percentile_90"
  149. if output is None:
  150. print string
  151. else:
  152. out_file.write(string + "\n")
  153. for row in rows:
  154. id = row["name"] + "@" + row["mapset"]
  155. start = row["start_time"]
  156. end = row["end_time"]
  157. mylayer = row["layer"]
  158. flags = "g"
  159. if extended is True:
  160. flags += "e"
  161. if not mylayer:
  162. mylayer = layer
  163. stats = gscript.parse_command("v.univar", map=id, where=where,
  164. column=column, layer=mylayer,
  165. type=type, flags=flags)
  166. string = ""
  167. if not stats:
  168. gscript.warning(_("Unable to get statistics for vector map <%s>")
  169. % id)
  170. continue
  171. string += str(id) + fs + str(start) + fs + str(end)
  172. string += fs + str(stats["n"]) + fs + str(stats[
  173. "nmissing"]) + fs + str(stats["nnull"])
  174. if "min" in stats:
  175. string += fs + str(stats["min"]) + fs + str(
  176. stats["max"]) + fs + str(stats["range"])
  177. else:
  178. string += fs + fs + fs
  179. if type == "point" or type == "centroid":
  180. if "mean" in stats:
  181. string += fs + str(stats["mean"]) + fs + \
  182. str(stats["mean_abs"]) + fs + \
  183. str(stats["population_stddev"]) + fs + \
  184. str(stats["population_variance"])
  185. string += fs + str(stats["population_coeff_variation"]) + \
  186. fs + str(stats["sample_stddev"]) + fs + \
  187. str(stats["sample_variance"])
  188. string += fs + str(stats["kurtosis"]) + fs + \
  189. str(stats["skewness"])
  190. else:
  191. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  192. if extended is True:
  193. if "first_quartile" in stats:
  194. string += fs + str(stats["first_quartile"]) + fs + \
  195. str(stats["median"]) + fs + \
  196. str(stats["third_quartile"]) + fs + \
  197. str(stats["percentile_90"])
  198. else:
  199. string += fs + fs + fs + fs
  200. if output is None:
  201. print string
  202. else:
  203. out_file.write(string + "\n")
  204. dbif.close()
  205. if output is not None:
  206. out_file.close()