Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 27 additions & 46 deletions python/grass/temporal/univar_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def compute_univar_stats(
Only available for strds.
:param output_format: Output format specification
"""
string = ""
id = registered_map_info["id"]
start = registered_map_info["start_time"]
end = registered_map_info["end_time"]
Expand Down Expand Up @@ -106,44 +105,23 @@ def compute_univar_stats(

return processed_stats

eol = ""
lines = univar_stats.strip().split("\n")
if len(lines) < 2:
return ""

for idx, stats_kv in enumerate(univar_stats.split(";")):
stats = gs.utils.parse_key_val(stats_kv)
string += (
f"{id}{fs}{semantic_label}{fs}{start}{fs}{end}"
if stats_module.name == "r.univar"
else f"{id}{fs}{start}{fs}{end}"
)
if stats_module.inputs.zones:
if idx == 0:
zone = str(stats["zone"])
string = ""
continue
string += f"{fs}{zone}"
if "zone" in stats:
zone = str(stats["zone"])
eol = "\n"
else:
eol = ""
string += f"{fs}{stats['mean']}{fs}{stats['min']}"
string += f"{fs}{stats['max']}{fs}{stats['mean_of_abs']}"
string += f"{fs}{stats['stddev']}{fs}{stats['variance']}"
string += f"{fs}{stats['coeff_var']}{fs}{stats['sum']}"
string += f"{fs}{stats['null_cells']}{fs}{stats['n']}"
string += f"{fs}{stats['n']}"
if "median" in stats:
string += f"{fs}{stats['first_quartile']}{fs}{stats['median']}"
string += f"{fs}{stats['third_quartile']}"
if stats_module.inputs.percentile:
for perc in stats_module.inputs.percentile:
perc_value = stats[
"percentile_"
f"{str(perc).rstrip('0').rstrip('.').replace('.', '_')}"
]
string += f"{fs}{perc_value}"
string += eol
return string
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)


def print_gridded_dataset_univar_statistics(
Expand Down Expand Up @@ -235,20 +213,21 @@ def print_gridded_dataset_univar_statistics(
else ["id", "start", "end"]
)
if zones:
cols.append("zone")
cols.extend(["zone", "label"])
cols.extend(
[
"mean",
"non_null_cells",
"null_cells",
"min",
"max",
"range",
"mean",
"mean_of_abs",
"stddev",
"variance",
"coeff_var",
"sum",
"null_cells",
"cells",
"non_null_cells",
"sum_abs",
]
)
if extended is True:
Expand All @@ -261,6 +240,9 @@ def print_gridded_dataset_univar_statistics(
for perc in percentile
]
)
else:
cols.extend(["percentile_90"])

if no_header is False and format != "json":
string = fs.join(cols)
if output is None:
Expand All @@ -270,8 +252,6 @@ def print_gridded_dataset_univar_statistics(

# Define flags
flag = ""
if format != "json":
flag = "g"
if extended is True:
flag += "e"
if type == "strds" and rast_region is True and not zones:
Expand All @@ -284,7 +264,8 @@ def print_gridded_dataset_univar_statistics(
zones=zones,
percentile=percentile,
stdout_=PIPE,
format="json" if format == "json" else None,
format="json" if format == "json" else "csv",
quiet=True,
run_=False,
)

Expand Down
14 changes: 6 additions & 8 deletions temporal/t.rast.univar/t.rast.univar.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ Obtain the univariate statistics for the raster space time dataset
"tempmean_monthly" (precision reduced to 2 decimals in this example):

```sh
t.rast.univar -e tempmean_monthly
id|start|end|mean|min|max|mean_of_abs|stddev|variance|coeff_var|sum|null_cells|cells|first_quartile|median|third_quartile|percentile_90
2009_01_tempmean@climate_2009_2012|2009-01-01 00:00:00|2009-02-01 00:00:00|3.90|-3.38|7.43|3.95|1.79|3.20|45.91|1977967.31|503233|1010600|2.80|3.92|5.21|6.23
2009_02_tempmean@climate_2009_2012|2009-02-01 00:00:00|2009-03-01 00:00:00|5.91|-1.82|8.01|5.92|1.63|2.65|27.53|2999555.60|503233|1010600|5.44|6.26|7.07|7.48
...
2012_11_tempmean@climate_2009_2012|2012-11-01 00:00:00|2012-12-01 00:00:00|8.03|1.79|10.91|8.03|1.32|1.73|16.41|4072472.77|503233|1010600|7.49|8.13|8.96|9.48
2012_12_tempmean@climate_2009_2012|2012-12-01 00:00:00|2013-01-01 00:00:00|8.71|1.76|11.98|8.71|1.72|2.95|19.74|4418403.77|503233|1010600|7.84|8.95|9.99|10.67
t.rast.univar -e input=nc_lst_daily
id|semantic_label|start|end|non_null_cells|null_cells|min|max|range|mean|mean_of_abs|stddev|variance|coeff_var|sum|sum_abs|first_quartile|median|third_quartile|percentile_90
aqua_lst_day20020705@modis2002lst||2002-07-05 00:00:00|2002-07-06 00:00:00|448428|390977|25.730000000000018|45.77000000000004|20.04000000000002|36.24265068193782|36.24265068193782|2.6171016196159838|6.849220887396605|7.221054670044495|16252219.360000012|16252219.360000012|34.49000000000001|36.07000000000005|37.83000000000004|40.01000000000005
aqua_lst_day20020706@modis2002lst||2002-07-06 00:00:00|2002-07-07 00:00:00|246658|592747|25.630000000000052|34.81|9.17999999999995|28.219656852808374|28.219656852808374|1.6350578714604542|2.6734142430247916|5.794038814819025|6960604.120000008|6960604.120000008|27.110000000000014|27.970000000000027|28.970000000000027|30.450000000000045
aqua_lst_day20020707@modis2002lst||2002-07-07 00:00:00|2002-07-08 00:00:00|714454|124951|25.630000000000052|44.670000000000016|19.039999999999964|32.642155464172674|32.642155464172674|3.1874224286049917|10.159661738374142|9.764742503305083|23321318.54000002|23321318.54000002|30.350000000000023|32.450000000000045|34.77000000000004|36.950000000000045
```

You can use format options as well to output the statistics in
Expand Down Expand Up @@ -141,7 +139,7 @@ t.rast.univar -e input=nc_lst_daily format=json

## SEE ALSO

*[t.create](t.create.md), [t.info](t.info.md) [t.create](r.univar.md),*
*[t.create](t.create.md), [t.info](t.info.md) [r.univar](r.univar.md),*

## AUTHOR

Expand Down
Loading
Loading