[GRASS-SVN] r41097 - grass-addons/grass7/raster/r.univar.zonal
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Feb 18 10:56:48 EST 2010
Author: mmetz
Date: 2010-02-18 10:56:48 -0500 (Thu, 18 Feb 2010)
New Revision: 41097
Modified:
grass-addons/grass7/raster/r.univar.zonal/Makefile
grass-addons/grass7/raster/r.univar.zonal/globals.h
grass-addons/grass7/raster/r.univar.zonal/r.univar_main.c
grass-addons/grass7/raster/r.univar.zonal/r3.univar_main.c
grass-addons/grass7/raster/r.univar.zonal/stats.c
Log:
r.univar.zonal for grass7
Modified: grass-addons/grass7/raster/r.univar.zonal/Makefile
===================================================================
--- grass-addons/grass7/raster/r.univar.zonal/Makefile 2010-02-18 15:50:11 UTC (rev 41096)
+++ grass-addons/grass7/raster/r.univar.zonal/Makefile 2010-02-18 15:56:48 UTC (rev 41097)
@@ -1,23 +1,18 @@
MODULE_TOPDIR = ../..
-LIBES = $(G3DLIB) $(GISLIB)
-DEPENDENCIES = $(G3DDEP) $(GISDEP)
+LIBES2 = $(RASTERLIB) $(GISLIB) $(MATHLIB)
+LIBES3 = $(G3DLIB) $(GISLIB) $(MATHLIB)
+DEPENDENCIES = $(G3DDEP) $(GISDEP) $(RASTERDEP)
-#needed for htmlmulti
PROGRAMS = r.univar.zonal r3.univar.zonal
+r_univar_zonal_OBJS = r.univar_main.o sort.o stats.o
+r3_univar_zonal_OBJS = r3.univar_main.o sort.o stats.o
+
include $(MODULE_TOPDIR)/include/Make/Multi.make
-R3UNIVAR = $(BIN)/r3.univar.zonal$(EXE)
-RUNIVAR = $(BIN)/r.univar.zonal$(EXE)
+default: multi
-default: $(RUNIVAR) $(R3UNIVAR)
- $(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)
-
+$(BIN)/r.univar.zonal$(EXE): LIBES = $(LIBES2)
+$(BIN)/r3.univar.zonal$(EXE): LIBES = $(LIBES3)
Modified: grass-addons/grass7/raster/r.univar.zonal/globals.h
===================================================================
--- grass-addons/grass7/raster/r.univar.zonal/globals.h 2010-02-18 15:50:11 UTC (rev 41096)
+++ grass-addons/grass7/raster/r.univar.zonal/globals.h 2010-02-18 15:56:48 UTC (rev 41097)
@@ -1,10 +1,11 @@
/*
* Calculates univariate statistics from the non-null cells
*
- * Copyright (C) 2004-2007 by the GRASS Development Team
+ * Copyright (C) 2004-2010 by the GRASS Development Team
* Author(s): Soeren Gebbert
* Based on r.univar from Hamish Bowman, University of Otago, New Zealand
* and Martin Landa
+ * zonal loop by Markus Metz
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
@@ -20,6 +21,7 @@
#include <math.h>
#include <grass/gis.h>
#include <grass/G3d.h>
+#include <grass/raster.h>
#include <grass/glocale.h>
/*- Parameters and global variables -----------------------------------------*/
@@ -30,7 +32,7 @@
double min;
double max;
unsigned int n_perc;
- int *perc;
+ double *perc;
double sum_abs;
int n;
int size;
@@ -57,13 +59,8 @@
struct Flag *shell_style, *extended, *table;
} param_type;
-#ifdef MAIN
-param_type param;
-zone_type zone_info;
-#else
extern param_type param;
extern zone_type zone_info;
-#endif
/* fn prototypes */
void heapsort_double(double *data, int n);
Modified: grass-addons/grass7/raster/r.univar.zonal/r.univar_main.c
===================================================================
--- grass-addons/grass7/raster/r.univar.zonal/r.univar_main.c 2010-02-18 15:50:11 UTC (rev 41096)
+++ grass-addons/grass7/raster/r.univar.zonal/r.univar_main.c 2010-02-18 15:56:48 UTC (rev 41097)
@@ -16,11 +16,10 @@
#include <assert.h>
#include <string.h>
-#define MAIN
#include "globals.h"
-/* local proto */
-void set_params();
+param_type param;
+zone_type zone_info;
/* ************************************************************************* */
/* Set up the arguments we are expecting ********************************** */
@@ -42,7 +41,7 @@
param.percentile = G_define_option();
param.percentile->key = "percentile";
- param.percentile->type = TYPE_INTEGER;
+ param.percentile->type = TYPE_DOUBLE;
param.percentile->required = NO;
param.percentile->multiple = YES;
param.percentile->options = "0-100";
@@ -85,13 +84,14 @@
char **p, *z;
int fd, fdz, cell_type, min, max;
struct Range zone_range;
- char *mapset, *name;
+ const char *mapset, *name;
G_gisinit(argv[0]);
module = G_define_module();
- module->keywords = _("raster, statistics");
+ G_add_keyword(_("raster"));
+ G_add_keyword(_("statistics"));
module->description =
_("Calculates univariate statistics from the non-null cells of a raster map.");
@@ -123,19 +123,18 @@
/* open zoning raster */
if ((z = param.zonefile->answer)) {
- mapset = G_find_cell2(z, "");
+ mapset = G_find_raster2(z, "");
fdz = open_raster(z);
- cell_type = G_get_raster_map_type(fdz);
+ cell_type = Rast_get_map_type(fdz);
if (cell_type != CELL_TYPE)
G_fatal_error("Zoning raster must be of type CELL");
- if (G_read_range(z, mapset, &zone_range) == -1)
+ if (Rast_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)))
+ Rast_get_range_min_max(&zone_range, &min, &max);
+ if (Rast_read_cats(z, mapset, &(zone_info.cats)))
G_warning("no category support for zoning raster");
zone_info.min = min;
@@ -159,7 +158,7 @@
if (map_type != -1) {
/* NB: map_type must match when doing extended stats */
- int this_type = G_get_raster_map_type(fd);
+ int this_type = Rast_get_map_type(fd);
assert(this_type > -1);
if (map_type < -1) {
@@ -176,12 +175,12 @@
process_raster(stats, fd, fdz, ®ion);
/* close input raster */
- G_close_cell(fd);
+ Rast_close(fd);
}
/* close zoning raster */
if (z)
- G_close_cell(fdz);
+ Rast_close(fdz);
/* create the output */
if (param.table->answer)
@@ -197,19 +196,16 @@
static int open_raster(const char *infile)
{
- char *mapset;
+ const char *mapset;
int fd;
- mapset = G_find_cell2(infile, "");
+ mapset = G_find_raster2(infile, "");
if (mapset == NULL) {
G_fatal_error(_("Raster map <%s> not found"), infile);
}
- fd = G_open_cell_old(infile, mapset);
- if (fd < 0)
- G_fatal_error(_("Unable to open raster map <%s>"), infile);
+ fd = Rast_open_old(infile, mapset);
- /* . */
return fd;
}
@@ -228,7 +224,7 @@
stats = create_univar_stat_struct(map_type, i);
for (i = 0; i < n_zones; i++) {
for (j = 0; j < stats[i].n_perc; j++) {
- sscanf(param.percentile->answers[j], "%i", &(stats[i].perc[j]));
+ sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
}
}
@@ -243,27 +239,25 @@
const int rows = region->rows;
const int cols = region->cols;
- const RASTER_MAP_TYPE map_type = G_get_raster_map_type(fd);
- const size_t value_sz = G_raster_size(map_type);
+ const RASTER_MAP_TYPE map_type = Rast_get_map_type(fd);
+ const size_t value_sz = Rast_cell_size(map_type);
unsigned int row;
void *raster_row;
CELL *zoneraster_row;
int n_zones = zone_info.n_zones;
- raster_row = G_allocate_raster_buf(map_type);
+ raster_row = Rast_allocate_buf(map_type);
if (n_zones)
- zoneraster_row = G_allocate_c_raster_buf();
+ zoneraster_row = Rast_allocate_c_buf();
for (row = 0; row < rows; row++) {
void *ptr;
CELL *zptr;
unsigned int col;
- if (G_get_raster_row(fd, raster_row, row, map_type) < 0)
- G_fatal_error(_("Reading row %d"), row);
+ Rast_get_row(fd, raster_row, row, map_type);
if (n_zones) {
- if (G_get_c_raster_row(fdz, zoneraster_row, row) < 0)
- G_fatal_error(_("Reading row %d"), row);
+ Rast_get_c_row(fdz, zoneraster_row, row);
zptr = zoneraster_row;
}
@@ -275,7 +269,7 @@
if (n_zones) {
/* skip NULL cells in zone map */
- if (G_is_c_null_value(zptr)) {
+ if (Rast_is_c_null_value(zptr)) {
ptr = G_incr_void_ptr(ptr, value_sz);
zptr++;
continue;
@@ -287,7 +281,7 @@
stats[zone].size++;
/* can't do stats with NULL cells in input map */
- if (G_is_null_value(ptr, map_type)) {
+ if (Rast_is_null_value(ptr, map_type)) {
ptr = G_incr_void_ptr(ptr, value_sz);
if (n_zones)
zptr++;
Modified: grass-addons/grass7/raster/r.univar.zonal/r3.univar_main.c
===================================================================
--- grass-addons/grass7/raster/r.univar.zonal/r3.univar_main.c 2010-02-18 15:50:11 UTC (rev 41096)
+++ grass-addons/grass7/raster/r.univar.zonal/r3.univar_main.c 2010-02-18 15:56:48 UTC (rev 41097)
@@ -16,12 +16,11 @@
*/
#include <string.h>
-#define MAIN
#include "globals.h"
#include "grass/G3d.h"
-/* local proto */
-void set_params();
+param_type param;
+zone_type zone_info;
/* ************************************************************************* */
/* Set up the arguments we are expecting ********************************** */
@@ -43,7 +42,7 @@
param.percentile = G_define_option();
param.percentile->key = "percentile";
- param.percentile->type = TYPE_INTEGER;
+ param.percentile->type = TYPE_DOUBLE;
param.percentile->required = NO;
param.percentile->multiple = YES;
param.percentile->options = "0-100";
@@ -82,7 +81,7 @@
univar_stat *stats;
char *infile, *zonemap;
- void *map, *zmap;
+ void *map, *zmap = NULL;
G3D_Region region;
unsigned int i;
unsigned int rows, cols, depths;
@@ -97,7 +96,8 @@
G_gisinit(argv[0]);
module = G_define_module();
- module->keywords = _("raster3d, statistics");
+ G_add_keyword(_("raster3d"));
+ G_add_keyword(_("statistics"));
module->description =
_("Calculates univariate statistics from the non-null 3d cells of a raster3d map.");
@@ -134,7 +134,7 @@
zone_info.n_zones = 0;
/* open 3D zoning raster with default region */
- if ((zonemap = param.zonefile->answer)) {
+ if ((zonemap = param.zonefile->answer) != NULL) {
if (NULL == (mapset = G_find_grid3(zonemap, "")))
G3d_fatalError(_("Requested g3d map <%s> not found"), zonemap);
@@ -156,7 +156,7 @@
if (G3d_readRange(zonemap, mapset, &zone_range) == -1)
G_fatal_error("Can not read range for zoning raster");
G3d_range_min_max(zmap, &dmin, &dmax);
- if (G_read_raster_cats(zonemap, mapset, &(zone_info.cats)))
+ if (Rast_read_cats(zonemap, mapset, &(zone_info.cats)))
G_warning("no category support for zoning raster");
/* properly round dmin and dmax */
@@ -193,11 +193,13 @@
while (param.percentile->answers[i])
i++;
stats = create_univar_stat_struct(map_type, 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++) {
+ unsigned int j;
+ for (j = 0; j < stats[i].n_perc; j++) {
+ sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
+ }
}
- stats->n = 0;
for (z = 0; z < depths; z++) { /*From the bottom to the top */
if (!(param.shell_style->answer))
G_percent(z, depths - 1, 10);
Modified: grass-addons/grass7/raster/r.univar.zonal/stats.c
===================================================================
--- grass-addons/grass7/raster/r.univar.zonal/stats.c 2010-02-18 15:50:11 UTC (rev 41096)
+++ grass-addons/grass7/raster/r.univar.zonal/stats.c 2010-02-18 15:56:48 UTC (rev 41097)
@@ -34,7 +34,7 @@
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));
+ stats[i].perc = (double *)G_malloc(n_perc * sizeof(double));
else
stats[i].perc = NULL;
stats[i].sum_abs = 0.0;
@@ -135,8 +135,11 @@
sprintf(sum_str, "%.10f", stats[z].sum);
G_trim_decimal(sum_str);
- if (zone_info.n_zones)
- fprintf(stdout, "\nzone %d %s\n\n", z + zone_info.min, G_get_cat(z + zone_info.min, &(zone_info.cats)));
+ if (zone_info.n_zones) {
+ int z_cat = z + zone_info.min;
+
+ fprintf(stdout, "\nzone %d %s\n\n", z_cat, Rast_get_c_cat(&z_cat, &(zone_info.cats)));
+ }
if (!param.shell_style->answer) {
fprintf(stdout, "total null and non-null cells: %d\n", stats[z].size);
@@ -240,7 +243,10 @@
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],
+ char buf[24];
+ sprintf(buf, "%.15g", stats[z].perc[i]);
+ G_strchg(buf, '.', '_');
+ fprintf(stdout, "percentile_%s=%g\n", buf,
quartile_perc[i]);
}
}
@@ -255,18 +261,26 @@
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],
+ if (stats[z].perc[i] == (int)stats[z].perc[i]) {
+ /* percentile is an exact integer */
+ if ((int)stats[z].perc[i] % 10 == 1 && (int)stats[z].perc[i] != 11)
+ fprintf(stdout, "%dst percentile: %g\n", (int)stats[z].perc[i],
+ quartile_perc[i]);
+ else if ((int)stats[z].perc[i] % 10 == 2 && (int)stats[z].perc[i] != 12)
+ fprintf(stdout, "%dnd percentile: %g\n", (int)stats[z].perc[i],
+ quartile_perc[i]);
+ else if ((int)stats[z].perc[i] % 10 == 3 && (int)stats[z].perc[i] != 13)
+ fprintf(stdout, "%drd percentile: %g\n", (int)stats[z].perc[i],
+ quartile_perc[i]);
+ else
+ fprintf(stdout, "%dth percentile: %g\n", (int)stats[z].perc[i],
+ quartile_perc[i]);
+ }
+ else {
+ /* percentile is not an exact integer */
+ fprintf(stdout, "%.15g 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);
@@ -313,7 +327,18 @@
fprintf(stdout, "%smedian", zone_info.sep);
fprintf(stdout, "%sthird_quart", zone_info.sep);
for (i = 0; i < stats[0].n_perc; i++) {
- fprintf(stdout, "%sperc_%d", zone_info.sep, stats[0].perc[i]);
+
+ if (stats[0].perc[i] == (int)stats[0].perc[i]) {
+ /* percentile is an exact integer */
+ fprintf(stdout, "%sperc_%d", zone_info.sep, (int)stats[0].perc[i]);
+ }
+ else {
+ /* percentile is not an exact integer */
+ char buf[24];
+ sprintf(buf, "%.15g", stats[0].perc[i]);
+ G_strchg(buf, '.', '_');
+ fprintf(stdout, "%sperc_%s", zone_info.sep, buf);
+ }
}
}
fprintf(stdout, "\n");
@@ -344,10 +369,11 @@
var_coef = (stdev / mean) * 100.; /* perhaps stdev/fabs(mean) ? */
if (zone_info.n_zones) {
+ int z_cat = z + zone_info.min;
/* zone number */
fprintf(stdout, "%d%s", z + zone_info.min, zone_info.sep);
/* zone label */
- fprintf(stdout,"%s%s", G_get_cat(z + zone_info.min, &(zone_info.cats)), zone_info.sep);
+ fprintf(stdout,"%s%s", Rast_get_c_cat(&z_cat, &(zone_info.cats)), zone_info.sep);
}
/* total cells */
More information about the grass-commit
mailing list