univar_statistics.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. def print_gridded_dataset_univar_statistics(type, input, where, extended, header, fs):
  18. """!Print univariate statistics for a space time raster or raster3d dataset
  19. @param type Must be "strds" or "str3ds"
  20. @param input The name of the space time dataset
  21. @param where A temporal database where statement
  22. @param extended If True compute extended statistics
  23. @param header If True print column names as header
  24. @param fs Field separator
  25. """
  26. # We need a database interface
  27. dbif = sql_database_interface_connection()
  28. dbif.connect()
  29. mapset = core.gisenv()["MAPSET"]
  30. if input.find("@") >= 0:
  31. id = input
  32. else:
  33. id = input + "@" + mapset
  34. sp = dataset_factory(type, id)
  35. if sp.is_in_db(dbif) == False:
  36. dbif.close()
  37. core.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  38. sp.select(dbif)
  39. rows = sp.get_registered_maps("id,start_time,end_time", where, "start_time", dbif)
  40. if not rows:
  41. dbif.close()
  42. core.fatal(_("Space time %s dataset <%s> is empty") % (sp.get_new_map_instance(None).get_type(), out_id))
  43. if header == True:
  44. print "id" + fs + "start" + fs + "end" + fs + "mean" + fs + "min" + fs + "max" + fs,
  45. print "mean_of_abs" + fs + "stddev" + fs + "variance" + fs,
  46. if extended == True:
  47. print "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells" + fs,
  48. print "first_quartile" + fs + "median" + fs + "third_quartile" + fs + "percentile_90"
  49. else:
  50. print "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  51. for row in rows:
  52. id = row["id"]
  53. start = row["start_time"]
  54. end = row["end_time"]
  55. flag="g"
  56. if extended == True:
  57. flag += "e"
  58. if type == "strds":
  59. stats = core.parse_command("r.univar", map=id, flags=flag)
  60. elif type == "str3ds":
  61. stats = core.parse_command("r3.univar", map=id, flags=flag)
  62. print str(id) + fs + str(start) + fs + str(end),
  63. print fs + str(stats["mean"]) + fs + str(stats["min"]) + fs + str(stats["max"]) + fs + str(stats["mean_of_abs"]),
  64. print fs + str(stats["stddev"]) + fs + str(stats["variance"]) + fs + str(stats["coeff_var"]) + fs + str(stats["sum"]),
  65. if extended == True:
  66. print fs + str(stats["null_cells"]) + fs + str(stats["cells"]) + fs,
  67. print str(stats["first_quartile"]) + fs + str(stats["median"]) + fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  68. else:
  69. print fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  70. dbif.close()
  71. if __name__ == "__main__":
  72. options, flags = core.parser()
  73. main()