Source code for grass.temporal.univar_statistics

"""
Univariate statistic function for space time datasets

Usage:

.. code-block:: python

    import grass.temporal as tgis

    tgis.print_gridded_dataset_univar_statistics(
        type, input, output, where, extended, no_header, fs, rast_region
    )

..

(C) 2012-2024 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.

:authors: Soeren Gebbert
"""

from multiprocessing import Pool
from subprocess import PIPE
import json

import grass.script as gs
from grass.pygrass.modules import Module

from .core import SQLDatabaseInterfaceConnection, get_current_mapset
from .factory import dataset_factory
from .open_stds import open_old_stds

###############################################################################


[docs] def compute_univar_stats( registered_map_info, stats_module, fs, rast_region: bool = False, output_format: str | None = None, ): """Compute univariate statistics for a map of a space time raster or raster3d dataset :param registered_map_info: dict or db row with tgis info for a registered map :param stats_module: Pre-configured PyGRASS Module to compute univariate statistics with :param fs: Field separator :param rast_region: If set True ignore the current region settings and use the raster map regions for univar statistical calculation. Only available for strds. :param output_format: Output format specification """ id = registered_map_info["id"] start = registered_map_info["start_time"] end = registered_map_info["end_time"] semantic_label = ( "" if stats_module.name == "r3.univar" or not registered_map_info["semantic_label"] else registered_map_info["semantic_label"] ) stats_module.inputs.map = id if rast_region and (stats_module.inputs.zones or stats_module.name == "r3.univar"): stats_module.env = gs.region_env(raster=id) stats_module.run() univar_stats = stats_module.outputs.stdout if not univar_stats: gs.warning( _("Unable to get statistics for raster map <%s>") % id if stats_module.name == "r.univar" else _("Unable to get statistics for 3d raster map <%s>") % id ) return None if output_format == "json": try: stats = json.loads(univar_stats) except ValueError: gs.warning(_("Unable to parse JSON output for map <%s>") % id) return None if isinstance(stats, dict): stats = [stats] processed_stats = [] for s in stats: new_s = { "id": id, "semantic_label": semantic_label if stats_module.name == "r.univar" and semantic_label else None, "start": str(start) if start else None, "end": str(end) if end else None, } new_s.update(s) processed_stats.append(new_s) return processed_stats lines = univar_stats.strip().split("\n") if len(lines) < 2: return "" prefix = ( f"{id}{fs}{semantic_label}{fs}{start}{fs}{end}" if stats_module.name == "r.univar" else f"{id}{fs}{start}{fs}{end}" ) output_rows = [] for line in lines[1:]: if line.strip(): formatted_line = line.replace(",", fs) output_rows.append(f"{prefix}{fs}{formatted_line}") return "\n".join(output_rows)
###############################################################################