[GRASS-SVN] r74213 - grass/trunk/raster/r.univar
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Mar 9 12:11:05 PST 2019
Author: mmetz
Date: 2019-03-09 12:11:05 -0800 (Sat, 09 Mar 2019)
New Revision: 74213
Modified:
grass/trunk/raster/r.univar/stats.c
Log:
r.univar: report stats with nan if a given zone has only NULL values
Modified: grass/trunk/raster/r.univar/stats.c
===================================================================
--- grass/trunk/raster/r.univar/stats.c 2019-03-09 20:07:30 UTC (rev 74212)
+++ grass/trunk/raster/r.univar/stats.c 2019-03-09 20:11:05 UTC (rev 74213)
@@ -113,16 +113,12 @@
char sum_str[100];
double mean, variance, stdev, var_coef;
- /* for extendet stats */
+ /* for extended stats */
double quartile_25 = 0.0, quartile_75 = 0.0, *quartile_perc;
double median = 0.0;
unsigned int i;
int qpos_25, qpos_75, *qpos_perc;
- /* stats collected for this zone? */
- if (stats[z].n == 0)
- continue;
-
/* all these calculations get promoted to doubles, so any DIV0 becomes nan */
mean = stats[z].sum / stats[z].n;
variance = (stats[z].sumsq - stats[z].sum * stats[z].sum / stats[z].n) / stats[z].n;
@@ -131,6 +127,8 @@
stdev = sqrt(variance);
var_coef = (stdev / mean) * 100.; /* perhaps stdev/fabs(mean) ? */
+ if (stats[z].n == 0)
+ stats[z].sum = stats[z].sum_abs = 0.0 / 0.0;
sprintf(sum_str, "%.15g", stats[z].sum);
G_trim_decimal(sum_str);
@@ -182,63 +180,71 @@
if (param.extended->answer) {
qpos_perc = (int *)G_calloc(stats[z].n_perc, sizeof(int));
quartile_perc = (double *)G_calloc(stats[z].n_perc, sizeof(double));
- for (i = 0; i < stats[z].n_perc; i++) {
- qpos_perc[i] = (int)(stats[z].n * 1e-2 * stats[z].perc[i] - 0.5);
+
+ if (stats[z].n == 0) {
+ quartile_25 = median = quartile_75 = 0.0 / 0.0;
+ for (i = 0; i < stats[z].n_perc; i++)
+ quartile_perc[i] = 0.0 / 0.0;
}
- qpos_25 = (int)(stats[z].n * 0.25 - 0.5);
- qpos_75 = (int)(stats[z].n * 0.75 - 0.5);
-
- switch (stats[z].map_type) {
- case CELL_TYPE:
- heapsort_int(stats[z].cell_array, stats[z].n);
-
- quartile_25 = (double)stats[z].cell_array[qpos_25];
- if (stats[z].n % 2) /* odd */
- median = (double)stats[z].cell_array[(int)(stats[z].n / 2)];
- else /* even */
- median =
- (double)(stats[z].cell_array[stats[z].n / 2 - 1] +
- stats[z].cell_array[stats[z].n / 2]) / 2.0;
- quartile_75 = (double)stats[z].cell_array[qpos_75];
+ else {
for (i = 0; i < stats[z].n_perc; i++) {
- quartile_perc[i] = (double)stats[z].cell_array[qpos_perc[i]];
+ qpos_perc[i] = (int)(stats[z].n * 1e-2 * stats[z].perc[i] - 0.5);
}
- break;
+ qpos_25 = (int)(stats[z].n * 0.25 - 0.5);
+ qpos_75 = (int)(stats[z].n * 0.75 - 0.5);
- case FCELL_TYPE:
- heapsort_float(stats[z].fcell_array, stats[z].n);
+ switch (stats[z].map_type) {
+ case CELL_TYPE:
+ heapsort_int(stats[z].cell_array, stats[z].n);
- quartile_25 = (double)stats[z].fcell_array[qpos_25];
- if (stats[z].n % 2) /* odd */
- median = (double)stats[z].fcell_array[(int)(stats[z].n / 2)];
- else /* even */
- median =
- (double)(stats[z].fcell_array[stats[z].n / 2 - 1] +
- stats[z].fcell_array[stats[z].n / 2]) / 2.0;
- quartile_75 = (double)stats[z].fcell_array[qpos_75];
- for (i = 0; i < stats[z].n_perc; i++) {
- quartile_perc[i] = (double)stats[z].fcell_array[qpos_perc[i]];
- }
- break;
+ quartile_25 = (double)stats[z].cell_array[qpos_25];
+ if (stats[z].n % 2) /* odd */
+ median = (double)stats[z].cell_array[(int)(stats[z].n / 2)];
+ else /* even */
+ median =
+ (double)(stats[z].cell_array[stats[z].n / 2 - 1] +
+ stats[z].cell_array[stats[z].n / 2]) / 2.0;
+ quartile_75 = (double)stats[z].cell_array[qpos_75];
+ for (i = 0; i < stats[z].n_perc; i++) {
+ quartile_perc[i] = (double)stats[z].cell_array[qpos_perc[i]];
+ }
+ break;
- case DCELL_TYPE:
- heapsort_double(stats[z].dcell_array, stats[z].n);
+ case FCELL_TYPE:
+ heapsort_float(stats[z].fcell_array, stats[z].n);
- quartile_25 = stats[z].dcell_array[qpos_25];
- if (stats[z].n % 2) /* odd */
- median = stats[z].dcell_array[(int)(stats[z].n / 2)];
- else /* even */
- median =
- (stats[z].dcell_array[stats[z].n / 2 - 1] +
- stats[z].dcell_array[stats[z].n / 2]) / 2.0;
- quartile_75 = stats[z].dcell_array[qpos_75];
- for (i = 0; i < stats[z].n_perc; i++) {
- quartile_perc[i] = stats[z].dcell_array[qpos_perc[i]];
+ quartile_25 = (double)stats[z].fcell_array[qpos_25];
+ if (stats[z].n % 2) /* odd */
+ median = (double)stats[z].fcell_array[(int)(stats[z].n / 2)];
+ else /* even */
+ median =
+ (double)(stats[z].fcell_array[stats[z].n / 2 - 1] +
+ stats[z].fcell_array[stats[z].n / 2]) / 2.0;
+ quartile_75 = (double)stats[z].fcell_array[qpos_75];
+ for (i = 0; i < stats[z].n_perc; i++) {
+ quartile_perc[i] = (double)stats[z].fcell_array[qpos_perc[i]];
+ }
+ break;
+
+ case DCELL_TYPE:
+ heapsort_double(stats[z].dcell_array, stats[z].n);
+
+ quartile_25 = stats[z].dcell_array[qpos_25];
+ if (stats[z].n % 2) /* odd */
+ median = stats[z].dcell_array[(int)(stats[z].n / 2)];
+ else /* even */
+ median =
+ (stats[z].dcell_array[stats[z].n / 2 - 1] +
+ stats[z].dcell_array[stats[z].n / 2]) / 2.0;
+ quartile_75 = stats[z].dcell_array[qpos_75];
+ for (i = 0; i < stats[z].n_perc; i++) {
+ quartile_perc[i] = stats[z].dcell_array[qpos_perc[i]];
+ }
+ break;
+
+ default:
+ break;
}
- break;
-
- default:
- break;
}
if (param.shell_style->answer) {
@@ -358,7 +364,7 @@
int qpos_25, qpos_75, *qpos_perc;
/* stats collected for this zone? */
- if (stats[z].n == 0)
+ if (stats[z].size == 0)
continue;
i = 0;
@@ -371,6 +377,9 @@
stdev = sqrt(variance);
var_coef = (stdev / mean) * 100.; /* perhaps stdev/fabs(mean) ? */
+ if (stats[z].n == 0)
+ stats[z].sum = stats[z].sum_abs = 0.0 / 0.0;
+
if (zone_info.n_zones) {
int z_cat = z + zone_info.min;
/* zone number */
@@ -412,63 +421,72 @@
if (param.extended->answer) {
qpos_perc = (int *)G_calloc(stats[z].n_perc, sizeof(int));
quartile_perc = (double *)G_calloc(stats[z].n_perc, sizeof(double));
- for (i = 0; i < stats[z].n_perc; i++) {
- qpos_perc[i] = (int)(stats[z].n * 1e-2 * stats[z].perc[i] - 0.5);
- }
- qpos_25 = (int)(stats[z].n * 0.25 - 0.5);
- qpos_75 = (int)(stats[z].n * 0.75 - 0.5);
- switch (stats[z].map_type) {
- case CELL_TYPE:
- heapsort_int(stats[z].cell_array, stats[z].n);
- quartile_25 = (double)stats[z].cell_array[qpos_25];
- if (stats[z].n % 2) /* odd */
- median = (double)stats[z].cell_array[(int)(stats[z].n / 2)];
- else /* even */
- median =
- (double)(stats[z].cell_array[stats[z].n / 2 - 1] +
- stats[z].cell_array[stats[z].n / 2]) / 2.0;
- quartile_75 = (double)stats[z].cell_array[qpos_75];
+ if (stats[z].n == 0) {
+ quartile_25 = median = quartile_75 = 0.0 / 0.0;
+ for (i = 0; i < stats[z].n_perc; i++)
+ quartile_perc[i] = 0.0 / 0.0;
+ }
+ else {
for (i = 0; i < stats[z].n_perc; i++) {
- quartile_perc[i] = (double)stats[z].cell_array[qpos_perc[i]];
+ qpos_perc[i] = (int)(stats[z].n * 1e-2 * stats[z].perc[i] - 0.5);
}
- break;
+ qpos_25 = (int)(stats[z].n * 0.25 - 0.5);
+ qpos_75 = (int)(stats[z].n * 0.75 - 0.5);
- case FCELL_TYPE:
- heapsort_float(stats[z].fcell_array, stats[z].n);
+ switch (stats[z].map_type) {
+ case CELL_TYPE:
+ heapsort_int(stats[z].cell_array, stats[z].n);
- quartile_25 = (double)stats[z].fcell_array[qpos_25];
- if (stats[z].n % 2) /* odd */
- median = (double)stats[z].fcell_array[(int)(stats[z].n / 2)];
- else /* even */
- median =
- (double)(stats[z].fcell_array[stats[z].n / 2 - 1] +
- stats[z].fcell_array[stats[z].n / 2]) / 2.0;
- quartile_75 = (double)stats[z].fcell_array[qpos_75];
- for (i = 0; i < stats[z].n_perc; i++) {
- quartile_perc[i] = (double)stats[z].fcell_array[qpos_perc[i]];
- }
- break;
+ quartile_25 = (double)stats[z].cell_array[qpos_25];
+ if (stats[z].n % 2) /* odd */
+ median = (double)stats[z].cell_array[(int)(stats[z].n / 2)];
+ else /* even */
+ median =
+ (double)(stats[z].cell_array[stats[z].n / 2 - 1] +
+ stats[z].cell_array[stats[z].n / 2]) / 2.0;
+ quartile_75 = (double)stats[z].cell_array[qpos_75];
+ for (i = 0; i < stats[z].n_perc; i++) {
+ quartile_perc[i] = (double)stats[z].cell_array[qpos_perc[i]];
+ }
+ break;
- case DCELL_TYPE:
- heapsort_double(stats[z].dcell_array, stats[z].n);
+ case FCELL_TYPE:
+ heapsort_float(stats[z].fcell_array, stats[z].n);
- quartile_25 = stats[z].dcell_array[qpos_25];
- if (stats[z].n % 2) /* odd */
- median = stats[z].dcell_array[(int)(stats[z].n / 2)];
- else /* even */
- median =
- (stats[z].dcell_array[stats[z].n / 2 - 1] +
- stats[z].dcell_array[stats[z].n / 2]) / 2.0;
- quartile_75 = stats[z].dcell_array[qpos_75];
- for (i = 0; i < stats[z].n_perc; i++) {
- quartile_perc[i] = stats[z].dcell_array[qpos_perc[i]];
+ quartile_25 = (double)stats[z].fcell_array[qpos_25];
+ if (stats[z].n % 2) /* odd */
+ median = (double)stats[z].fcell_array[(int)(stats[z].n / 2)];
+ else /* even */
+ median =
+ (double)(stats[z].fcell_array[stats[z].n / 2 - 1] +
+ stats[z].fcell_array[stats[z].n / 2]) / 2.0;
+ quartile_75 = (double)stats[z].fcell_array[qpos_75];
+ for (i = 0; i < stats[z].n_perc; i++) {
+ quartile_perc[i] = (double)stats[z].fcell_array[qpos_perc[i]];
+ }
+ break;
+
+ case DCELL_TYPE:
+ heapsort_double(stats[z].dcell_array, stats[z].n);
+
+ quartile_25 = stats[z].dcell_array[qpos_25];
+ if (stats[z].n % 2) /* odd */
+ median = stats[z].dcell_array[(int)(stats[z].n / 2)];
+ else /* even */
+ median =
+ (stats[z].dcell_array[stats[z].n / 2 - 1] +
+ stats[z].dcell_array[stats[z].n / 2]) / 2.0;
+ quartile_75 = stats[z].dcell_array[qpos_75];
+ for (i = 0; i < stats[z].n_perc; i++) {
+ quartile_perc[i] = stats[z].dcell_array[qpos_perc[i]];
+ }
+ break;
+
+ default:
+ break;
}
- break;
-
- default:
- break;
}
/* first quartile */
More information about the grass-commit
mailing list