[GRASS-SVN] r35971 - grass-addons/raster/r.univar2.zonal
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Feb 20 02:22:37 EST 2009
Author: mmetz
Date: 2009-02-20 02:22:34 -0500 (Fri, 20 Feb 2009)
New Revision: 35971
Modified:
grass-addons/raster/r.univar2.zonal/Makefile
grass-addons/raster/r.univar2.zonal/globals.h
grass-addons/raster/r.univar2.zonal/r.univar_main.c
grass-addons/raster/r.univar2.zonal/stats.c
Log:
zonal stats, trying proper svn usage
Modified: grass-addons/raster/r.univar2.zonal/Makefile
===================================================================
--- grass-addons/raster/r.univar2.zonal/Makefile 2009-02-20 07:15:04 UTC (rev 35970)
+++ grass-addons/raster/r.univar2.zonal/Makefile 2009-02-20 07:22:34 UTC (rev 35971)
@@ -5,19 +5,19 @@
DEPENDENCIES = $(G3DDEP) $(GISDEP)
#needed for htmlmulti
-PROGRAMS = r.univar r3.univar
+PROGRAMS = r.univar.zonal
include $(MODULE_TOPDIR)/include/Make/Multi.make
-R3UNIVAR = $(BIN)/r3.univar$(EXE)
-RUNIVAR = $(BIN)/r.univar$(EXE)
+#R3UNIVAR = $(BIN)/r3.univar.zonal$(EXE)
+RUNIVAR = $(BIN)/r.univar.zonal$(EXE)
-default: $(R3UNIVAR) $(RUNIVAR)
+default: $(RUNIVAR)
$(MAKE) htmlmulti
$(RUNIVAR): $(OBJDIR)/r.univar_main.o $(OBJDIR)/sort.o $(OBJDIR)/stats.o
$(CC) $(LDFLAGS) -o $@ $^ $(FMODE_OBJ) $(LIBES) $(XDRLIB) $(MATHLIB)
-$(R3UNIVAR): $(OBJDIR)/r3.univar_main.o $(OBJDIR)/sort.o $(OBJDIR)/stats.o
- $(CC) $(LDFLAGS) -o $@ $^ $(FMODE_OBJ) $(LIBES) $(XDRLIB) $(MATHLIB)
+#$(R3UNIVAR): $(OBJDIR)/r3.univar_main.o $(OBJDIR)/sort.o $(OBJDIR)/stats.o
+# $(CC) $(LDFLAGS) -o $@ $^ $(FMODE_OBJ) $(LIBES) $(XDRLIB) $(MATHLIB)
Modified: grass-addons/raster/r.univar2.zonal/globals.h
===================================================================
--- grass-addons/raster/r.univar2.zonal/globals.h 2009-02-20 07:15:04 UTC (rev 35970)
+++ grass-addons/raster/r.univar2.zonal/globals.h 2009-02-20 07:22:34 UTC (rev 35971)
@@ -38,19 +38,28 @@
FCELL *fcell_array;
CELL *cell_array;
int map_type;
+ void *nextp;
} univar_stat;
+typedef struct
+{
+ CELL min, max, n_zones;
+ struct Categories cats;
+} zone_type;
+
/* command line options are the same for raster and raster3d maps */
typedef struct
{
- struct Option *inputfile, *percentile;
+ struct Option *inputfile, *zonefile, *percentile, *output_file;
struct Flag *shell_style, *extended;
} param_type;
#ifdef MAIN
param_type param;
+zone_type zone_info;
#else
extern param_type param;
+extern zone_type zone_info;
#endif
/* fn prototypes */
Modified: grass-addons/raster/r.univar2.zonal/r.univar_main.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/r.univar_main.c 2009-02-20 07:15:04 UTC (rev 35970)
+++ grass-addons/raster/r.univar2.zonal/r.univar_main.c 2009-02-20 07:22:34 UTC (rev 35971)
@@ -27,8 +27,19 @@
/* ************************************************************************* */
void set_params()
{
- param.inputfile = G_define_standard_option(G_OPT_R_MAPS);
+ param.inputfile = G_define_standard_option(G_OPT_R_INPUT);
+ param.zonefile = G_define_standard_option(G_OPT_R_MAP);
+ param.zonefile->key = "zones";
+ param.zonefile->required = YES;
+ param.zonefile->description =
+ _("Raster map used for zoning, must be of type CELL");
+
+ param.output_file = G_define_standard_option(G_OPT_F_OUTPUT);
+ param.output_file->required = NO;
+ param.output_file->description =
+ _("Name for output file (if omitted or \"-\" output to stdout)");
+
param.percentile = G_define_option();
param.percentile->key = "percentile";
param.percentile->type = TYPE_INTEGER;
@@ -53,7 +64,7 @@
static int open_raster(const char *infile);
static univar_stat *univar_stat_with_percentiles(int map_type, int size);
-static void process_raster(univar_stat * stats, int fd,
+static void process_raster(univar_stat * stats, int fd, int fdz,
const struct Cell_head *region);
/* *************************************************************** */
@@ -62,11 +73,15 @@
int main(int argc, char *argv[])
{
unsigned int rows, cols; /* totals */
- int rasters;
+ /* int rasters; */
struct Cell_head region;
struct GModule *module;
univar_stat *stats;
+ char *p, *z;
+ int fd, fdz, cell_type, min, max;
+ struct Range zone_range;
+ char *mapset, *name;
G_gisinit(argv[0]);
@@ -82,50 +97,70 @@
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
+ name = param.output_file->answer;
+ if (name != NULL && strcmp(name, "-") != 0) {
+ if (NULL == freopen(name, "w", stdout)) {
+ G_fatal_error(_("Unable to open file <%s> for writing"), name);
+ }
+ }
+
G_get_window(®ion);
rows = region.rows;
cols = region.cols;
- /* count the rasters given */
- {
- const char **p;
+ zone_info.min = 0.0 / 0.0; /*set to nan as default */
+ zone_info.max = 0.0 / 0.0; /*set to nan as default */
+ zone_info.n_zones = 0;
- for (p = (const char **)param.inputfile->answers, rasters = 0;
- *p; p++, rasters++) ;
- }
+ /* open zoning raster */
+ z = param.zonefile->answer;
+ mapset = G_find_cell2(z, "");
- /* process them all */
- {
- size_t cells = rasters * cols * rows;
- int map_type = param.extended->answer ? -2 : -1;
- char **p;
+ fdz = open_raster(z);
+
+ cell_type = G_get_raster_map_type(fdz);
+ if (cell_type != CELL_TYPE)
+ G_fatal_error("Zoning raster must be of type CELL");
- stats = ((map_type == -1)
- ? create_univar_stat_struct(-1, cells, 0)
- : 0);
+ if (G_read_range(z, mapset, &zone_range) == -1)
+ G_fatal_error("Can not read range for zoning raster");
+ if (G_get_range_min_max(&zone_range, &min, &max))
+ G_fatal_error("Can not read range for zoning raster");
+ if (G_read_raster_cats(z, mapset, &(zone_info.cats)))
+ G_warning("no category support for zoning raster");
- for (p = param.inputfile->answers; *p; p++) {
- int fd = open_raster(*p);
+ zone_info.min = min;
+ zone_info.max = max;
+ zone_info.n_zones = max - min + 1;
- if (map_type != -1) {
- /* NB: map_type must match when doing extended stats */
- int this_type = G_get_raster_map_type(fd);
+ /* process input raster */
+ size_t cells = cols * rows;
+ int map_type = param.extended->answer ? -2 : -1;
- assert(this_type > -1);
- if (map_type < -1) {
- assert(stats == 0);
- map_type = this_type;
- stats = univar_stat_with_percentiles(map_type, cells);
- }
- else if (this_type != map_type) {
- G_fatal_error(_("Raster <%s> type mismatch"), *p);
- }
- }
+ stats = ((map_type == -1)
+ ? create_univar_stat_struct(-1, cells, 0)
+ : 0);
- process_raster(stats, fd, ®ion);
+ p = param.inputfile->answer;
+ fd = open_raster(p);
+
+ if (map_type != -1) {
+ /* NB: map_type must match when doing extended stats */
+ int this_type = G_get_raster_map_type(fd);
+
+ assert(this_type > -1);
+ if (map_type < -1) {
+ assert(stats == 0);
+ map_type = this_type;
+ stats = univar_stat_with_percentiles(map_type, cells);
}
+ else if (this_type != map_type) {
+ G_fatal_error(_("Raster <%s> type mismatch"), p);
+ }
}
+ process_raster(stats, fd, fdz, ®ion);
+
if (!(param.shell_style->answer))
G_percent(rows, rows, 2); /* finish it off */
@@ -159,14 +194,16 @@
static univar_stat *univar_stat_with_percentiles(int map_type, int size)
{
univar_stat *stats;
- int i;
+ int i, j;
i = 0;
while (param.percentile->answers[i])
i++;
stats = create_univar_stat_struct(map_type, size, i);
- for (i = 0; i < stats->n_perc; i++) {
- sscanf(param.percentile->answers[i], "%i", &stats->perc[i]);
+ for (i = 0; i < zone_info.n_zones; i++) {
+ for (j = 0; j < stats[i].n_perc; j++) {
+ sscanf(param.percentile->answers[j], "%i", &stats[i].perc[j]);
+ }
}
/* . */
@@ -174,45 +211,91 @@
}
static void
-process_raster(univar_stat * stats, int fd, const struct Cell_head *region)
+process_raster(univar_stat * stats, int fd, int fdz, const struct Cell_head *region)
{
/* use G_window_rows(), G_window_cols() here? */
const int rows = region->rows;
const int cols = region->cols;
- int first = (stats->n < 1);
+ const int n_zones = zone_info.n_zones;
+ int *first, i;
const RASTER_MAP_TYPE map_type = G_get_raster_map_type(fd);
- void *nextp
- = ((!param.extended->answer) ? 0
- : (map_type == DCELL_TYPE) ? (void *)stats->dcell_array
- : (map_type == FCELL_TYPE) ? (void *)stats->fcell_array
- : (void *)stats->cell_array);
const size_t value_sz = G_raster_size(map_type);
unsigned int row;
void *raster_row;
+ CELL *zoneraster_row;
+ switch (map_type) {
+ case CELL_TYPE:
+ for (i = 0; i < n_zones; i++) {
+ stats[i].nextp
+ = ((!param.extended->answer) ? 0 : (void *)stats[i].cell_array);
+ }
+ break;
+
+ case FCELL_TYPE:
+ for (i = 0; i < n_zones; i++) {
+ stats[i].nextp
+ = ((!param.extended->answer) ? 0 : (void *)stats[i].fcell_array);
+ }
+ break;
+
+ case DCELL_TYPE:
+ for (i = 0; i < n_zones; i++) {
+ stats[i].nextp
+ = ((!param.extended->answer) ? 0 : (void *)stats[i].dcell_array);
+ }
+ break;
+
+ default:
+ G_fatal_error("input raster has unknown type");
+ break;
+ }
+
+ first = (int *) G_malloc((n_zones - 1) * sizeof(int));
+ {
+ int i;
+ for (i = 0; i < n_zones; i++)
+ first[i] = (stats[i].n < 1);
+ }
+
+
raster_row = G_calloc(cols, value_sz);
+ zoneraster_row = G_allocate_c_raster_buf();
for (row = 0; row < rows; row++) {
void *ptr;
+ CELL *zptr;
unsigned int col;
+ int zone;
if (G_get_raster_row(fd, raster_row, row, map_type) < 0)
G_fatal_error(_("Reading row %d"), row);
+ if (G_get_c_raster_row(fdz, zoneraster_row, row) < 0)
+ G_fatal_error(_("Reading row %d"), row);
ptr = raster_row;
+ zptr = zoneraster_row;
for (col = 0; col < cols; col++) {
if (G_is_null_value(ptr, map_type)) {
ptr = G_incr_void_ptr(ptr, value_sz);
+ zptr++;
continue;
}
+ if (G_is_c_null_value(zptr)) {
+ ptr = G_incr_void_ptr(ptr, value_sz);
+ zptr++;
+ continue;
+ }
- if (nextp) {
+ zone = *zptr + zone_info.min;
+
+ if (stats[zone].nextp) {
/* put the value into stats->XXXcell_array */
- memcpy(nextp, ptr, value_sz);
- nextp = G_incr_void_ptr(nextp, value_sz);
+ memcpy(stats[zone].nextp, ptr, value_sz);
+ stats[zone].nextp = G_incr_void_ptr(stats[zone].nextp, value_sz);
}
{
@@ -220,25 +303,26 @@
: (map_type == FCELL_TYPE) ? *((FCELL *) ptr)
: *((CELL *) ptr));
- stats->sum += val;
- stats->sumsq += val * val;
- stats->sum_abs += fabs(val);
+ stats[zone].sum += val;
+ stats[zone].sumsq += val * val;
+ stats[zone].sum_abs += fabs(val);
- if (first) {
- stats->max = val;
- stats->min = val;
- first = FALSE;
+ if (first[zone]) {
+ stats[zone].max = val;
+ stats[zone].min = val;
+ first[zone] = FALSE;
}
else {
- if (val > stats->max)
- stats->max = val;
- if (val < stats->min)
- stats->min = val;
+ if (val > stats[zone].max)
+ stats[zone].max = val;
+ if (val < stats[zone].min)
+ stats[zone].min = val;
}
}
ptr = G_incr_void_ptr(ptr, value_sz);
- stats->n++;
+ zptr++;
+ stats[zone].n++;
}
if (!(param.shell_style->answer))
G_percent(row, rows, 2);
Modified: grass-addons/raster/r.univar2.zonal/stats.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/stats.c 2009-02-20 07:15:04 UTC (rev 35970)
+++ grass-addons/raster/r.univar2.zonal/stats.c 2009-02-20 07:22:34 UTC (rev 35971)
@@ -19,36 +19,40 @@
univar_stat *create_univar_stat_struct(int map_type, int size, int n_perc)
{
univar_stat *stats;
+ int i;
+ int n_zones = zone_info.n_zones;
- stats = (univar_stat *) G_calloc(1, sizeof(univar_stat));
+ stats = (univar_stat *) G_calloc(n_zones, sizeof(univar_stat));
- stats->sum = 0.0;
- stats->sumsq = 0.0;
- stats->min = 0.0 / 0.0; /*set to nan as default */
- stats->max = 0.0 / 0.0; /*set to nan as default */
- stats->n_perc = n_perc;
- if (n_perc > 0)
- stats->perc = (int *)G_malloc(n_perc * sizeof(int));
- else
- stats->perc = NULL;
- stats->sum_abs = 0.0;
- stats->n = 0;
- stats->size = size;
- stats->dcell_array = NULL;
- stats->fcell_array = NULL;
- stats->cell_array = NULL;
- stats->map_type = map_type;
+ for (i = 0; i < n_zones; i++) {
+ stats[i].sum = 0.0;
+ stats[i].sumsq = 0.0;
+ stats[i].min = 0.0 / 0.0; /*set to nan as default */
+ stats[i].max = 0.0 / 0.0; /*set to nan as default */
+ stats[i].n_perc = n_perc;
+ if (n_perc > 0)
+ stats[i].perc = (int *)G_malloc(n_perc * sizeof(int));
+ else
+ stats[i].perc = NULL;
+ stats[i].sum_abs = 0.0;
+ stats[i].n = 0;
+ stats[i].size = size;
+ stats[i].dcell_array = NULL;
+ stats[i].fcell_array = NULL;
+ stats[i].cell_array = NULL;
+ stats[i].map_type = map_type;
- /* alloacte memory for extended computation */
- if (param.extended->answer) {
- if (map_type == DCELL_TYPE)
- stats->dcell_array =
- (DCELL *) G_calloc(stats->size, sizeof(DCELL));
- if (map_type == FCELL_TYPE)
- stats->fcell_array =
- (FCELL *) G_calloc(stats->size, sizeof(FCELL));
- if (map_type == CELL_TYPE)
- stats->cell_array = (CELL *) G_calloc(stats->size, sizeof(CELL));
+ /* allocate memory for extended computation */
+ if (param.extended->answer) {
+ if (map_type == DCELL_TYPE)
+ stats[i].dcell_array =
+ (DCELL *) G_calloc(stats[i].size, sizeof(DCELL));
+ if (map_type == FCELL_TYPE)
+ stats[i].fcell_array =
+ (FCELL *) G_calloc(stats[i].size, sizeof(FCELL));
+ if (map_type == CELL_TYPE)
+ stats[i].cell_array = (CELL *) G_calloc(stats[i].size, sizeof(CELL));
+ }
}
return stats;
@@ -60,15 +64,19 @@
/* *************************************************************** */
void free_univar_stat_struct(univar_stat * stats)
{
- if (stats->perc)
- G_free(stats->perc);
- if (stats->dcell_array)
- G_free(stats->dcell_array);
- if (stats->fcell_array)
- G_free(stats->fcell_array);
- if (stats->cell_array)
- G_free(stats->cell_array);
+ int i;
+ for (i = 0; i < zone_info.n_zones; i++){
+ if (stats[i].perc)
+ G_free(stats[i].perc);
+ if (stats[i].dcell_array)
+ G_free(stats[i].dcell_array);
+ if (stats[i].fcell_array)
+ G_free(stats[i].fcell_array);
+ if (stats[i].cell_array)
+ G_free(stats[i].cell_array);
+ }
+
G_free(stats);
return;
@@ -80,164 +88,175 @@
/* *************************************************************** */
int print_stats(univar_stat * stats)
{
- char sum_str[100];
- double mean, variance, stdev, var_coef;
+ int z;
- /*for extendet 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;
+ for (z = 0; z < zone_info.n_zones; z++) {
+ char sum_str[100];
+ double mean, variance, stdev, var_coef;
+ /*for extendet 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;
- /* all these calculations get promoted to doubles, so any DIV0 becomes nan */
- mean = stats->sum / stats->n;
- variance = (stats->sumsq - stats->sum * stats->sum / stats->n) / stats->n;
- if (variance < GRASS_EPSILON)
- variance = 0.0;
- stdev = sqrt(variance);
- var_coef = (stdev / mean) * 100.; /* perhaps stdev/fabs(mean) ? */
+ /* stats collected for this zone? */
+ if (stats[z].n == 0)
+ continue;
- sprintf(sum_str, "%.10f", stats->sum);
- G_trim_decimal(sum_str);
- if (!param.shell_style->answer) {
- fprintf(stdout, "total null and non-null cells: %d\n", stats->size);
- fprintf(stdout, "total null cells: %d\n\n", stats->size - stats->n);
- fprintf(stdout, "Of the non-null cells:\n----------------------\n");
- }
+ /* 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;
+ if (variance < GRASS_EPSILON)
+ variance = 0.0;
+ stdev = sqrt(variance);
+ var_coef = (stdev / mean) * 100.; /* perhaps stdev/fabs(mean) ? */
- if (param.shell_style->answer) {
- fprintf(stdout, "n=%d\n", stats->n);
- fprintf(stdout, "null_cells=%d\n", stats->size - stats->n);
- fprintf(stdout, "min=%.15g\n", stats->min);
- fprintf(stdout, "max=%.15g\n", stats->max);
- fprintf(stdout, "range=%.15g\n", stats->max - stats->min);
- fprintf(stdout, "mean=%.15g\n", mean);
- fprintf(stdout, "mean_of_abs=%.15g\n", stats->sum_abs / stats->n);
- fprintf(stdout, "stddev=%.15g\n", stdev);
- fprintf(stdout, "variance=%.15g\n", variance);
- fprintf(stdout, "coeff_var=%.15g\n", var_coef);
- fprintf(stdout, "sum=%s\n", sum_str);
- }
- else {
- fprintf(stdout, "n: %d\n", stats->n);
- fprintf(stdout, "minimum: %g\n", stats->min);
- fprintf(stdout, "maximum: %g\n", stats->max);
- fprintf(stdout, "range: %g\n", stats->max - stats->min);
- fprintf(stdout, "mean: %g\n", mean);
- fprintf(stdout, "mean of absolute values: %g\n",
- stats->sum_abs / stats->n);
- fprintf(stdout, "standard deviation: %g\n", stdev);
- fprintf(stdout, "variance: %g\n", variance);
- fprintf(stdout, "variation coefficient: %g %%\n", var_coef);
- fprintf(stdout, "sum: %s\n", sum_str);
- }
+ sprintf(sum_str, "%.10f", stats[z].sum);
+ G_trim_decimal(sum_str);
+ fprintf(stdout, "\nzone %d %s\n\n", z - zone_info.min, G_get_c_raster_cat(&z + zone_info.min, &(zone_info.cats)));
- /* TODO: mode, skewness, kurtosis */
- if (param.extended->answer) {
- qpos_perc = (int *)G_calloc(stats->n_perc, sizeof(int));
- quartile_perc = (double *)G_calloc(stats->n_perc, sizeof(double));
- for (i = 0; i < stats->n_perc; i++) {
- qpos_perc[i] = (int)(stats->n * 1e-2 * stats->perc[i] - 0.5);
+ if (!param.shell_style->answer) {
+ fprintf(stdout, "total null and non-null cells: %d\n", stats[z].size);
+ fprintf(stdout, "total null cells: %d\n\n", stats[z].size - stats[z].n);
+ fprintf(stdout, "Of the non-null cells:\n----------------------\n");
}
- qpos_25 = (int)(stats->n * 0.25 - 0.5);
- qpos_75 = (int)(stats->n * 0.75 - 0.5);
- switch (stats->map_type) {
- case CELL_TYPE:
- heapsort_int(stats->cell_array, stats->n);
+ if (param.shell_style->answer) {
+ fprintf(stdout, "n=%d\n", stats[z].n);
+ fprintf(stdout, "null_cells=%d\n", stats[z].size - stats[z].n);
+ fprintf(stdout, "min=%.15g\n", stats[z].min);
+ fprintf(stdout, "max=%.15g\n", stats[z].max);
+ fprintf(stdout, "range=%.15g\n", stats[z].max - stats[z].min);
+ fprintf(stdout, "mean=%.15g\n", mean);
+ fprintf(stdout, "mean_of_abs=%.15g\n", stats[z].sum_abs / stats[z].n);
+ fprintf(stdout, "stddev=%.15g\n", stdev);
+ fprintf(stdout, "variance=%.15g\n", variance);
+ fprintf(stdout, "coeff_var=%.15g\n", var_coef);
+ fprintf(stdout, "sum=%s\n", sum_str);
+ }
+ else {
+ fprintf(stdout, "n: %d\n", stats[z].n);
+ fprintf(stdout, "minimum: %g\n", stats[z].min);
+ fprintf(stdout, "maximum: %g\n", stats[z].max);
+ fprintf(stdout, "range: %g\n", stats[z].max - stats[z].min);
+ fprintf(stdout, "mean: %g\n", mean);
+ fprintf(stdout, "mean of absolute values: %g\n",
+ stats[z].sum_abs / stats[z].n);
+ fprintf(stdout, "standard deviation: %g\n", stdev);
+ fprintf(stdout, "variance: %g\n", variance);
+ fprintf(stdout, "variation coefficient: %g %%\n", var_coef);
+ fprintf(stdout, "sum: %s\n", sum_str);
+ }
- quartile_25 = (double)stats->cell_array[qpos_25];
- if (stats->n % 2) /* odd */
- median = (double)stats->cell_array[(int)(stats->n / 2)];
- else /* even */
- median =
- (double)(stats->cell_array[stats->n / 2 - 1] +
- stats->cell_array[stats->n / 2]) / 2.0;
- quartile_75 = (double)stats->cell_array[qpos_75];
- for (i = 0; i < stats->n_perc; i++) {
- quartile_perc[i] = (double)stats->cell_array[qpos_perc[i]];
+
+ /* TODO: mode, skewness, kurtosis */
+ 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);
}
- 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->fcell_array, stats->n);
+ switch (stats[z].map_type) {
+ case CELL_TYPE:
+ heapsort_int(stats[z].cell_array, stats[z].n);
- quartile_25 = (double)stats->fcell_array[qpos_25];
- if (stats->n % 2) /* odd */
- median = (double)stats->fcell_array[(int)(stats->n / 2)];
- else /* even */
- median =
- (double)(stats->fcell_array[stats->n / 2 - 1] +
- stats->fcell_array[stats->n / 2]) / 2.0;
- quartile_75 = (double)stats->fcell_array[qpos_75];
- for (i = 0; i < stats->n_perc; i++) {
- quartile_perc[i] = (double)stats->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->dcell_array, stats->n);
+ case FCELL_TYPE:
+ heapsort_float(stats[z].fcell_array, stats[z].n);
- quartile_25 = stats->dcell_array[qpos_25];
- if (stats->n % 2) /* odd */
- median = stats->dcell_array[(int)(stats->n / 2)];
- else /* even */
- median =
- (stats->dcell_array[stats->n / 2 - 1] +
- stats->dcell_array[stats->n / 2]) / 2.0;
- quartile_75 = stats->dcell_array[qpos_75];
- for (i = 0; i < stats->n_perc; i++) {
- quartile_perc[i] = stats->dcell_array[qpos_perc[i]];
- }
- break;
+ 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;
- default:
- break;
- }
+ case DCELL_TYPE:
+ heapsort_double(stats[z].dcell_array, stats[z].n);
- if (param.shell_style->answer) {
- fprintf(stdout, "first_quartile=%g\n", quartile_25);
- fprintf(stdout, "median=%g\n", median);
- fprintf(stdout, "third_quartile=%g\n", quartile_75);
- for (i = 0; i < stats->n_perc; i++) {
- fprintf(stdout, "percentile_%d=%g\n", stats->perc[i],
- quartile_perc[i]);
+ 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;
}
- }
- else {
- fprintf(stdout, "1st quartile: %g\n", quartile_25);
- if (stats->n % 2)
- fprintf(stdout, "median (odd number of cells): %g\n", median);
- else
- fprintf(stdout, "median (even number of cells): %g\n",
- median);
- fprintf(stdout, "3rd quartile: %g\n", quartile_75);
-
- for (i = 0; i < stats->n_perc; i++) {
- if (stats->perc[i] % 10 == 1 && stats->perc[i] != 11)
- fprintf(stdout, "%dst percentile: %g\n", stats->perc[i],
+ if (param.shell_style->answer) {
+ fprintf(stdout, "first_quartile=%g\n", quartile_25);
+ fprintf(stdout, "median=%g\n", median);
+ fprintf(stdout, "third_quartile=%g\n", quartile_75);
+ for (i = 0; i < stats[z].n_perc; i++) {
+ fprintf(stdout, "percentile_%d=%g\n", stats[z].perc[i],
quartile_perc[i]);
- else if (stats->perc[i] % 10 == 2 && stats->perc[i] != 12)
- fprintf(stdout, "%dnd percentile: %g\n", stats->perc[i],
- quartile_perc[i]);
- else if (stats->perc[i] % 10 == 3 && stats->perc[i] != 13)
- fprintf(stdout, "%drd percentile: %g\n", stats->perc[i],
- quartile_perc[i]);
+ }
+ }
+ else {
+ fprintf(stdout, "1st quartile: %g\n", quartile_25);
+ if (stats[z].n % 2)
+ fprintf(stdout, "median (odd number of cells): %g\n", median);
else
- fprintf(stdout, "%dth percentile: %g\n", stats->perc[i],
- quartile_perc[i]);
+ fprintf(stdout, "median (even number of cells): %g\n",
+ median);
+ fprintf(stdout, "3rd quartile: %g\n", quartile_75);
+
+
+ for (i = 0; i < stats[z].n_perc; i++) {
+ if (stats[z].perc[i] % 10 == 1 && stats[z].perc[i] != 11)
+ fprintf(stdout, "%dst percentile: %g\n", stats[z].perc[i],
+ quartile_perc[i]);
+ else if (stats[z].perc[i] % 10 == 2 && stats[z].perc[i] != 12)
+ fprintf(stdout, "%dnd percentile: %g\n", stats[z].perc[i],
+ quartile_perc[i]);
+ else if (stats[z].perc[i] % 10 == 3 && stats[z].perc[i] != 13)
+ fprintf(stdout, "%drd percentile: %g\n", stats[z].perc[i],
+ quartile_perc[i]);
+ else
+ fprintf(stdout, "%dth percentile: %g\n", stats[z].perc[i],
+ quartile_perc[i]);
+ }
}
+ G_free((void *)quartile_perc);
+ G_free((void *)qpos_perc);
}
- G_free((void *)quartile_perc);
- G_free((void *)qpos_perc);
+
+ /* G_message() prints to stderr not stdout: disabled. this \n is printed above with zone */
+ /* if (!(param.shell_style->answer))
+ G_message("\n"); */
}
- if (!(param.shell_style->answer))
- G_message("\n");
-
return 1;
}
More information about the grass-commit
mailing list