[GRASS-SVN] r40648 - in grass/trunk: include lib/gis lib/imagery
raster/r.coin raster/r.mode raster/r.rescale
raster/r.rescale.eq raster/r.statistics
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Jan 25 13:28:39 EST 2010
Author: glynn
Date: 2010-01-25 13:28:36 -0500 (Mon, 25 Jan 2010)
New Revision: 40648
Removed:
grass/trunk/lib/imagery/ls_groups.c
grass/trunk/raster/r.statistics/o_distrib.c
Modified:
grass/trunk/include/gis.h
grass/trunk/include/gisdefs.h
grass/trunk/include/imagedefs.h
grass/trunk/include/spawn.h
grass/trunk/lib/gis/error.c
grass/trunk/lib/gis/list.c
grass/trunk/lib/gis/parser_wps.c
grass/trunk/lib/gis/popen.c
grass/trunk/lib/gis/spawn.c
grass/trunk/raster/r.coin/make_coin.c
grass/trunk/raster/r.mode/main.c
grass/trunk/raster/r.rescale.eq/main.c
grass/trunk/raster/r.rescale/main.c
grass/trunk/raster/r.statistics/main.c
grass/trunk/raster/r.statistics/method.h
grass/trunk/raster/r.statistics/o_adev.c
grass/trunk/raster/r.statistics/o_average.c
grass/trunk/raster/r.statistics/o_divr.c
grass/trunk/raster/r.statistics/o_kurt.c
grass/trunk/raster/r.statistics/o_max.c
grass/trunk/raster/r.statistics/o_median.c
grass/trunk/raster/r.statistics/o_min.c
grass/trunk/raster/r.statistics/o_mode.c
grass/trunk/raster/r.statistics/o_sdev.c
grass/trunk/raster/r.statistics/o_skew.c
grass/trunk/raster/r.statistics/o_sum.c
grass/trunk/raster/r.statistics/o_var.c
grass/trunk/raster/r.statistics/r.statistics.html
grass/trunk/raster/r.statistics/run_cmd.c
Log:
Eliminate use of [G_]popen()
Add G_wait()
Add G_{open,close}_pager ($GRASS_PAGER interface)
Add G_{open,close}_mail (mailing error messages to user)
Add G_popen_{read,write,close} (non-shell popen/pclose replacement)
Modified: grass/trunk/include/gis.h
===================================================================
--- grass/trunk/include/gis.h 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/include/gis.h 2010-01-25 18:28:36 UTC (rev 40648)
@@ -364,6 +364,11 @@
int value;
};
+struct Popen {
+ FILE *fp;
+ int pid;
+};
+
/*============================== Prototypes ================================*/
/* Since there are so many prototypes for the gis library they are stored */
Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/include/gisdefs.h 2010-01-25 18:28:36 UTC (rev 40648)
@@ -444,6 +444,12 @@
/* overwrite.c */
int G_check_overwrite(int argc, char **argv);
+/* pager.c */
+FILE *G_open_pager(struct Popen *);
+void G_close_pager(struct Popen *);
+FILE *G_open_mail(struct Popen *);
+void G_close_mail(struct Popen *);
+
/* parser.c */
void G_disable_interactive(void);
struct GModule *G_define_module(void);
@@ -472,6 +478,11 @@
void G_set_percent_routine(int (*) (int));
void G_unset_percent_routine(void);
+/* popen.c */
+FILE *G_popen_write(struct Popen *, const char *, const char **);
+FILE *G_popen_read(struct Popen *, const char *, const char **);
+void G_popen_close(struct Popen *);
+
/* plot.c */
void G_setup_plot(double, double, double, double, int (*)(int, int),
int (*)(int, int));
@@ -488,10 +499,6 @@
/* pole_in_poly.c */
int G_pole_in_polygon(const double *, const double *, int);
-/* popen.c */
-FILE *G_popen(const char *, const char *);
-int G_pclose(FILE *);
-
/* progrm_nme.c */
const char *G_program_name(void);
void G_set_program_name(const char *);
Modified: grass/trunk/include/imagedefs.h
===================================================================
--- grass/trunk/include/imagedefs.h 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/include/imagedefs.h 2010-01-25 18:28:36 UTC (rev 40648)
@@ -61,10 +61,6 @@
/* loc_info.c */
int I_location_info(char *, const char *);
-/* ls_groups.c */
-int I_list_groups(int);
-int I_list_subgroups(const char *, int);
-
/* points.c */
int I_new_control_point(struct Control_Points *, double, double, double,
double, int);
Modified: grass/trunk/include/spawn.h
===================================================================
--- grass/trunk/include/spawn.h 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/include/spawn.h 2010-01-25 18:28:36 UTC (rev 40648)
@@ -44,5 +44,6 @@
extern int G_spawn(const char *command, ...);
extern int G_vspawn_ex(const char *command, const char **args);
extern int G_spawn_ex(const char *command, ...);
+extern int G_wait(int i_pid);
#endif
Modified: grass/trunk/lib/gis/error.c
===================================================================
--- grass/trunk/lib/gis/error.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/lib/gis/error.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -54,7 +54,7 @@
static int print_word(FILE *, char **, int *, const int);
static void print_sentence(FILE *, const int, const char *);
static void print_error(const char *, const int);
-static int mail_msg(const char *, int);
+static void mail_msg(const char *, int);
static int write_error(const char *, int, time_t, const char *);
static void log_error(const char *, int);
@@ -388,23 +388,15 @@
}
/* Mail a message */
-static int mail_msg(const char *msg, int fatal)
+static void mail_msg(const char *msg, int fatal)
{
- FILE *mail;
- char command[64];
- const char *user;
+ struct Popen mail;
+ FILE *fp = G_open_mail(&mail);
- user = G_whoami();
- if (user == 0 || *user == 0)
- return 1;
+ if (fp)
+ fprintf(fp, "GIS %s: %s\n", fatal ? "ERROR" : "WARNING", msg);
- sprintf(command, "mail '%s'", G_whoami());
- if ((mail = G_popen(command, "w"))) {
- fprintf(mail, "GIS %s: %s\n", fatal ? "ERROR" : "WARNING", msg);
- G_pclose(mail);
- }
-
- return 0;
+ G_close_mail(&mail);
}
/* Print one word, new line if necessary */
Modified: grass/trunk/lib/gis/list.c
===================================================================
--- grass/trunk/lib/gis/list.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/lib/gis/list.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -23,11 +23,10 @@
#include <grass/gis.h>
#include <grass/glocale.h>
-static volatile int broken_pipe;
static int list_element(FILE *, const char *, const char *, const char *,
int (*)(const char *, const char *, const char *));
-static void sigpipe_catch(int);
+
/**
\brief General purpose list function.
@@ -55,41 +54,19 @@
const char *mapset,
int (*lister) (const char *, const char *, const char *))
{
+ struct Popen pager;
int n;
FILE *more;
int count;
-#ifdef SIGPIPE
- RETSIGTYPE (*sigpipe)(int);
-#endif
-
- /* must catch broken pipe in case "more" quits */
- broken_pipe = 0;
-#ifdef SIGPIPE
- sigpipe = signal(SIGPIPE, sigpipe_catch);
-#endif
-
count = 0;
if (desc == 0 || *desc == 0)
desc = element;
- /* G_popen() does not work with MinGW? */
-#ifndef __MINGW32__
/*
* G_popen() the more command to page the output
*/
- if (isatty(1)) {
-#ifdef __MINGW32__
- more = G_popen("%GRASS_PAGER%", "w");
-#else
- more = G_popen("$GRASS_PAGER", "w");
-#endif
- if (!more)
- more = stdout;
- }
- else
-#endif
- more = stdout;
+ more = G_open_pager(&pager);
fprintf(more, "----------------------------------------------\n");
/*
@@ -98,39 +75,27 @@
* otherwise just list the specified mapset
*/
if (mapset == 0 || *mapset == 0)
- for (n = 0; !broken_pipe && (mapset = G__mapset_name(n)); n++)
+ for (n = 0; (mapset = G__mapset_name(n)); n++)
count += list_element(more, element, desc, mapset, lister);
else
count += list_element(more, element, desc, mapset, lister);
- if (!broken_pipe) {
- if (count == 0) {
- if (mapset == 0 || *mapset == 0)
- fprintf(more, _("no %s files available in current mapset\n"),
- desc);
- else
- fprintf(more, _("no %s files available in mapset <%s>\n"),
- desc, mapset);
- }
+ if (count == 0) {
+ if (mapset == 0 || *mapset == 0)
+ fprintf(more, _("no %s files available in current mapset\n"),
+ desc);
+ else
+ fprintf(more, _("no %s files available in mapset <%s>\n"),
+ desc, mapset);
fprintf(more, "----------------------------------------------\n");
}
/*
* close the more
*/
- if (more != stdout)
- G_pclose(more);
-#ifdef SIGPIPE
- signal(SIGPIPE, sigpipe);
-#endif
+ G_close_pager(&pager);
}
-static void sigpipe_catch(int n)
-{
- broken_pipe = 1;
- signal(n, sigpipe_catch);
-}
-
static int list_element(
FILE *out, const char *element, const char *desc, const char *mapset,
int (*lister)(const char *, const char *, const char *))
Modified: grass/trunk/lib/gis/parser_wps.c
===================================================================
--- grass/trunk/lib/gis/parser_wps.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/lib/gis/parser_wps.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -745,4 +745,5 @@
fprintf(stdout,"\t\t\t\t\t</Supported>\n");
fprintf(stdout,"\t\t\t\t</BoundingBoxData>\n");
fprintf(stdout,"\t\t\t</Input>\n");
-}
\ No newline at end of file
+}
+
Modified: grass/trunk/lib/gis/popen.c
===================================================================
--- grass/trunk/lib/gis/popen.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/lib/gis/popen.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -1,21 +1,70 @@
-#include <unistd.h>
#include <stdio.h>
-#include <signal.h>
#include <stdlib.h>
-#include <sys/types.h>
+
+#include <unistd.h>
+
#include <grass/gis.h>
+#include <grass/spawn.h>
-#ifdef __MINGW32__
-#define popen(cmd,mode) _popen(cmd,mode)
-#define pclose(fp) _pclose(fp)
-#endif
+static FILE *do_popen(struct Popen *state, int wr,
+ const char *program, const char **args)
+{
+ const char *which = wr ? SF_STDIN : SF_STDOUT;
+ const char *dir = wr ? "w" : "r";
+ int pfd, cfd;
+ int pipe_fds[2];
+ const char *argv[2];
-FILE *G_popen(const char *cmd, const char *mode)
+ state->fp = NULL;
+ state->pid = -1;
+
+ if (pipe(pipe_fds) < 0)
+ return NULL;
+
+ cfd = pipe_fds[wr ? 0 : 1];
+ pfd = pipe_fds[wr ? 1 : 0];
+
+ if (!args) {
+ argv[0] = program;
+ argv[1] = NULL;
+ args = argv;
+ }
+
+ state->pid = G_spawn_ex(
+ program,
+ SF_ARGVEC, args,
+ SF_REDIRECT_DESCRIPTOR, which, cfd,
+ SF_CLOSE_DESCRIPTOR, pfd,
+ SF_BACKGROUND, NULL);
+
+ if (state->pid == -1) {
+ close(pipe_fds[0]);
+ close(pipe_fds[1]);
+ return NULL;
+ }
+
+ close(cfd);
+
+ state->fp = fdopen(pfd, dir);
+
+ return state->fp;
+}
+
+FILE *G_popen_write(struct Popen *state, const char *program, const char **args)
{
- return popen(cmd, mode);
+ return do_popen(state, 1, program, args);
}
-int G_pclose(FILE *ptr)
+FILE *G_popen_read(struct Popen *state, const char *program, const char **args)
{
- return pclose(ptr);
+ return do_popen(state, 0, program, args);
}
+
+void G_popen_close(struct Popen *state)
+{
+ if (state->fp)
+ fclose(state->fp);
+
+ if (state->pid != -1)
+ G_wait(state->pid);
+}
Modified: grass/trunk/lib/gis/spawn.c
===================================================================
--- grass/trunk/lib/gis/spawn.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/lib/gis/spawn.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -386,13 +386,18 @@
return -1;
}
+ CloseHandle(pi.hThread);
+
if (!background) {
WaitForSingleObject(pi.hProcess, INFINITE);
if (!GetExitCodeProcess(pi.hProcess, &exitcode))
return -1;
+ CloseHandle(pi.hProcess);
return (int) exitcode;
}
+ CloseHandle(pi.hProcess);
+
return pi.dwProcessId;
}
@@ -934,3 +939,42 @@
return status;
}
+int G_wait(int i_pid)
+{
+#ifdef __MINGW32__
+ DWORD rights = PROCESS_QUERY_INFORMATION | SYNCHRONIZE;
+ HANDLE hProcess = OpenProcess(rights, FALSE, (DWORD) i_pid);
+ DWORD exitcode;
+
+ if (!hProcess)
+ return -1;
+
+ WaitForSingleObject(hProcess, INFINITE);
+ if (!GetExitCodeProcess(hProcess, &exitcode))
+ exitcode = (DWORD) -1;
+
+ CloseHandle(hProcess);
+
+ return (int) exitcode;
+#else
+ pid_t pid = (pid_t) i_pid;
+ int status = -1;
+ pid_t n;
+
+ do
+ n = waitpid(pid, &status, 0);
+ while (n == (pid_t) - 1 && errno == EINTR);
+
+ if (n != pid)
+ return -1;
+ else {
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ else if (WIFSIGNALED(status))
+ return WTERMSIG(status);
+ else
+ return -0x100;
+ }
+#endif
+}
+
Deleted: grass/trunk/lib/imagery/ls_groups.c
===================================================================
--- grass/trunk/lib/imagery/ls_groups.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/lib/imagery/ls_groups.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -1,131 +0,0 @@
-
-/*************************************************************
-* I_list_groups (full)
-* I_list_subgroups (group, full)
-*************************************************************/
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <grass/imagery.h>
-#include <grass/spawn.h>
-
-static char *tempfile = NULL;
-
-
-int I_list_groups(int full)
-{
- char *element;
- int i;
-
- char buf[GPATH_MAX];
- char title[50];
- FILE *ls, *temp;
- struct Ref ref;
- int any;
-
- if (tempfile == NULL)
- tempfile = G_tempfile();
-
- element = "group";
- G__make_mapset_element(element);
-
- temp = fopen(tempfile, "w");
- if (temp == NULL)
- G_fatal_error("can't open any temp files");
- fprintf(temp, "Available groups\n");
- fprintf(temp, "---------------------------------\n");
-
- any = 0;
- strcpy(buf, "cd ");
- G__file_name(buf + strlen(buf), element, "", G_mapset());
- strcat(buf, ";ls");
- if (!full)
- strcat(buf, " -C");
- /* FIXME: use G__ls() */
- if ((ls = popen(buf, "r"))) {
- while (G_getl2(buf, sizeof(buf), ls)) {
- any = 1;
- fprintf(temp, "%s", buf);
- if (full) {
- I_get_group_title(buf, title, sizeof(title));
- if (*title)
- fprintf(temp, " (%s)", title);
- fprintf(temp, "\n");
- I_get_group_ref(buf, &ref);
- for (i = 0; i < ref.nfiles; i++)
- fprintf(temp, "\t%s in %s\n", ref.file[i].name,
- ref.file[i].mapset);
- if (ref.nfiles <= 0)
- fprintf(temp, "\t** empty **\n");
- I_free_group_ref(&ref);
- }
- else
- fprintf(temp, "\n");
- }
- pclose(ls);
- }
- if (!any)
- fprintf(temp, "no group files available\n");
- fprintf(temp, "---------------------------------\n");
- fclose(temp);
- G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL);
- remove(tempfile);
-
- return 0;
-}
-
-int I_list_subgroups(const char *group, int full)
-{
- char element[GNAME_MAX + 15];
- int i;
-
- char buf[GPATH_MAX];
- FILE *ls, *temp;
- struct Ref ref;
- int any;
-
- if (tempfile == NULL)
- tempfile = G_tempfile();
-
- sprintf(element, "group/%s/subgroup", group);
- G__make_mapset_element(element);
-
- temp = fopen(tempfile, "w");
- if (temp == NULL)
- G_fatal_error("Unable to open any temporary file");
- fprintf(temp, "Available subgroups in group %s\n", group);
- fprintf(temp, "---------------------------------\n");
-
- any = 0;
- strcpy(buf, "cd ");
- G__file_name(buf + strlen(buf), element, "", G_mapset());
- strcat(buf, ";ls");
- if (!full)
- strcat(buf, " -C");
- /* FIXME: use G__ls() */
- if ((ls = popen(buf, "r"))) {
- while (G_getl2(buf, sizeof(buf), ls)) {
- any = 1;
- fprintf(temp, "%s\n", buf);
- if (full) {
- I_get_subgroup_ref(group, buf, &ref);
- for (i = 0; i < ref.nfiles; i++)
- fprintf(temp, "\t%s in %s\n", ref.file[i].name,
- ref.file[i].mapset);
- if (ref.nfiles <= 0)
- fprintf(temp, "\t** empty **\n");
- I_free_group_ref(&ref);
- }
- }
- pclose(ls);
- }
- if (!any)
- fprintf(temp, "no subgroup files available\n");
- fprintf(temp, "---------------------------------\n");
- fclose(temp);
- G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL);
- remove(tempfile);
-
- return 0;
-}
Modified: grass/trunk/raster/r.coin/make_coin.c
===================================================================
--- grass/trunk/raster/r.coin/make_coin.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.coin/make_coin.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -28,8 +28,8 @@
int make_coin(void)
{
- FILE *fd;
- FILE *statfd;
+ FILE *fp;
+ FILE *stat_fp;
struct stats
{
long cat1;
@@ -39,43 +39,48 @@
} stats;
int n, n1, n2;
int reversed;
+ char input[GNAME_MAX*2+8];
+ const char *args[5];
+ struct Popen child;
char buf[512];
int count;
G_message(_("Tabulating Coincidence between '%s' and '%s'"),
map1name, map2name);
- sprintf(buf, "r.stats -anrc fs=: input=\"");
- strcat(buf, map1name);
- strcat(buf, ",");
- strcat(buf, map2name);
- strcat(buf, "\"");
- statfd = fopen(statname, "w");
- if (statfd == NULL)
+ sprintf(input, "input=%s,%s", map1name, map2name);
+
+ args[0] = "r.stats";
+ args[1] = "-anrc";
+ args[2] = "fs=:";
+ args[3] = input;
+ args[4] = NULL;
+
+ stat_fp = fopen(statname, "w");
+ if (!stat_fp)
G_fatal_error(_("Unable to create any tempfiles"));
- fd = popen(buf, "r");
- if (fd == NULL)
+ fp = G_popen_read(&child, "r.stats", args);
+ if (!fp)
G_fatal_error(_("Unable to run r.stats"));
/* need to find the number of cats in each file */
count = 0;
- while (fgets(buf, sizeof buf, fd)) {
+ while (fgets(buf, sizeof buf, fp)) {
if (sscanf(buf, "%ld:%ld:%lf:%ld",
&stats.cat1, &stats.cat2, &stats.area, &stats.count) != 4)
- {
- pclose(fd);
G_fatal_error(_("Unexpected output from r.stats"));
- }
- fwrite(&stats, sizeof(stats), 1, statfd);
+
+ fwrite(&stats, sizeof(stats), 1, stat_fp);
count++;
}
- pclose(fd);
- fclose(statfd);
+ G_popen_close(&child);
- statfd = fopen(statname, "r");
- if (statfd == NULL)
+ fclose(stat_fp);
+
+ stat_fp = fopen(statname, "r");
+ if (!stat_fp)
G_fatal_error(_("Unable to open tempfile"));
/* build a sorted list of cats in both maps */
@@ -84,7 +89,7 @@
/* read the statsfile to get the cat lists */
count = 0;
- while (fread(&stats, sizeof(stats), 1, statfd)) {
+ while (fread(&stats, sizeof(stats), 1, stat_fp)) {
catlist1[count] = stats.cat1;
catlist2[count++] = stats.cat2;
}
@@ -145,8 +150,8 @@
break;
/* now read the statsfile and insert into the table */
- G_fseek(statfd, 0L, 0);
- while (fread(&stats, sizeof(stats), 1, statfd)) {
+ G_fseek(stat_fp, 0L, 0);
+ while (fread(&stats, sizeof(stats), 1, stat_fp)) {
long z;
if (reversed) {
@@ -172,7 +177,7 @@
table[n].count = stats.count;
table[n].area = stats.area;
}
- fclose(statfd);
+ fclose(stat_fp);
return 0;
}
Modified: grass/trunk/raster/r.mode/main.c
===================================================================
--- grass/trunk/raster/r.mode/main.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.mode/main.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -38,7 +38,10 @@
char *basemap;
char *covermap;
char *outmap;
- char command[1024];
+ char input[GNAME_MAX*2+8];
+ char output[GNAME_MAX+8];
+ const char *args[4];
+ struct Popen stats_child, reclass_child;
struct Categories cover_cats;
FILE *stats, *reclass;
int first;
@@ -86,16 +89,25 @@
G_fatal_error(_("%s: Unable to read category labels"), covermap);
}
- sprintf(command, "r.stats -an \"%s,%s\"", basemap, covermap);
+ sprintf(input, "input=%s,%s", basemap, covermap);
- /* printf(command); */
- stats = popen(command, "r");
+ args[0] = "r.stats";
+ args[1] = "-an";
+ args[2] = input;
+ args[3] = NULL;
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outmap);
+ stats = G_popen_read(&stats_child, "r.stats", args);
- /* printf(command); */
- reclass = popen(command, "w");
+ sprintf(input, "input=%s", basemap);
+ sprintf(output, "output=%s", outmap);
+ args[0] = "r.reclass";
+ args[1] = input;
+ args[2] = output;
+ args[3] = NULL;
+
+ reclass = G_popen_write(&reclass_child, "r.reclass", args);
+
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
if (first) {
@@ -120,8 +132,8 @@
}
write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, &cover_cats));
- pclose(stats);
- pclose(reclass);
+ G_popen_close(&reclass_child);
+ G_popen_close(&stats_child);
- exit(0);
+ return 0;
}
Modified: grass/trunk/raster/r.rescale/main.c
===================================================================
--- grass/trunk/raster/r.rescale/main.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.rescale/main.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -21,8 +21,12 @@
int main(int argc, char *argv[])
{
- char buf[GPATH_MAX];
- FILE *fd;
+ char input[GNAME_MAX+8];
+ char output[GNAME_MAX+8];
+ char title[GPATH_MAX];
+ const char *args[5];
+ struct Popen child;
+ FILE *fp;
long old_min, old_max;
long new_min, new_max;
long new_delta, old_delta;
@@ -112,17 +116,22 @@
G_message(_("Rescale %s[%ld,%ld] to %s[%ld,%ld]"),
old_name, old_min, old_max, new_name, new_min, new_max);
- sprintf(buf, "r.reclass input=\"%s\" output=\"%s\" title=\"",
- old_name, new_name);
+ sprintf(input, "input=%s", old_name);
+ sprintf(output, "output=%s", new_name);
+
if (parm.title->answer)
- strcat(buf, parm.title->answer);
- else {
- strcat(buf, "rescale of ");
- strcat(buf, old_name);
- }
- strcat(buf, "\"");
+ sprintf(title, "title=%s", parm.title->answer);
+ else
+ sprintf(title, "title=rescale of %s", old_name);
- fd = popen(buf, "w");
+ args[0] = "r.reclass";
+ args[1] = input;
+ args[2] = output;
+ args[3] = title;
+ args[4] = NULL;
+
+ fp = G_popen_write(&child, "r.reclass", args);
+
old_delta = old_max - old_min;
new_delta = new_max - new_min;
divisor = (float)new_delta / (float)old_delta;
@@ -132,20 +141,21 @@
for (cat = old_min; cat <= old_max; cat++) {
value = (int)(divisor * (cat - old_min) + new_min + .5);
if (value != prev) {
- fprintf(fd, "%ld thru %ld = %ld %ld", first, cat - 1, prev,
+ fprintf(fp, "%ld thru %ld = %ld %ld", first, cat - 1, prev,
first);
if (cat - 1 != first)
- fprintf(fd, " thru %ld", cat - 1);
- fprintf(fd, "\n");
+ fprintf(fp, " thru %ld", cat - 1);
+ fprintf(fp, "\n");
prev = value;
first = cat;
}
}
- fprintf(fd, "%ld thru %ld = %ld %ld", first, cat - 1, prev, first);
+ fprintf(fp, "%ld thru %ld = %ld %ld", first, cat - 1, prev, first);
if (cat - 1 != first)
- fprintf(fd, " thru %ld", cat - 1);
- fprintf(fd, "\n");
+ fprintf(fp, " thru %ld", cat - 1);
+ fprintf(fp, "\n");
- pclose(fd);
- exit(EXIT_SUCCESS);
+ G_popen_close(&child);
+
+ return EXIT_SUCCESS;
}
Modified: grass/trunk/raster/r.rescale.eq/main.c
===================================================================
--- grass/trunk/raster/r.rescale.eq/main.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.rescale.eq/main.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -21,12 +21,16 @@
#include "local_proto.h"
#include <grass/glocale.h>
-static FILE *fd;
+static FILE *fp;
static void reclass(CELL, CELL, CELL);
int main(int argc, char *argv[])
{
- char buf[GPATH_MAX];
+ char input[GNAME_MAX+8];
+ char output[GNAME_MAX+8];
+ char title[GPATH_MAX];
+ const char *args[5];
+ struct Popen child;
CELL old_min, old_max;
CELL new_min, new_max;
long cat;
@@ -117,29 +121,36 @@
G_message(_("Rescale %s[%d,%d] to %s[%d,%d]"),
old_name, old_min, old_max, new_name, new_min, new_max);
- sprintf(buf, "r.reclass input=\"%s\" output=\"%s\" title=\"",
- old_name, new_name);
+ sprintf(input, "input=%s", old_name);
+ sprintf(output, "output=%s", new_name);
+
if (parm.title->answer)
- strcat(buf, parm.title->answer);
- else {
- strcat(buf, "rescale of ");
- strcat(buf, old_name);
- }
- strcat(buf, "\"");
+ sprintf(title, "title=%s", parm.title->answer);
+ else
+ sprintf(title, "title=rescale of %s", old_name);
- fd = popen(buf, "w");
- Rast_cell_stats_histo_eq(&statf, (CELL) old_min, (CELL) old_max,
- (CELL) new_min, (CELL) new_max, 0, reclass);
- if (fd != stdout)
- pclose(fd);
- exit(EXIT_SUCCESS);
+ args[0] = "r.reclass";
+ args[1] = input;
+ args[2] = output;
+ args[3] = title;
+ args[4] = NULL;
+
+ fp = G_popen_write(&child, "r.reclass", args);
+
+ Rast_cell_stats_histo_eq(&statf,
+ old_min, old_max,
+ new_min, new_max,
+ 0, reclass);
+ G_popen_close(&child);
+
+ return EXIT_SUCCESS;
}
static void reclass(CELL cat1, CELL cat2, CELL value)
{
- fprintf(fd, "%ld thru %ld = %ld %ld",
+ fprintf(fp, "%ld thru %ld = %ld %ld",
(long)cat1, (long)cat2, (long)value, (long)cat1);
if (cat1 != cat2)
- fprintf(fd, " thru %ld", (long)cat2);
- fprintf(fd, "\n");
+ fprintf(fp, " thru %ld", (long)cat2);
+ fprintf(fp, "\n");
}
Modified: grass/trunk/raster/r.statistics/main.c
===================================================================
--- grass/trunk/raster/r.statistics/main.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/main.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -25,26 +25,21 @@
/* modify this table to add new methods */
struct menu menu[] = {
- {"diversity", DIV, "diversity of values in specified objects in %%"},
- {"distribution", DISTRIB,
- "distribution of values in specified objects in %%"},
- {"average", AVERAGE, "average of values in specified objects"},
- {"mode", MODE, "mode of values in specified objects"},
- {"median", MEDIAN, "median of values in specified objects"},
- {"avedev", ADEV, "Average deviation of values in specified objects"},
- {"stddev", SDEV, "Standard deviation of values in specified objects"},
- {"variance", VARIANC, "Variance of values in specified objects"},
- {"skewness", SKEWNES, "Skewnes of values in specified objects"},
- {"kurtosis", KURTOSI, "Kurtosis of values in specified objects"},
- {"min", MIN, "Minimum of values in specified objects"},
- {"max", MAX, "Maximum of values in specified objects"},
- {"sum", SUM, "Sum of values in specified objects"},
- {0, 0, 0}
+ {"diversity", o_divr, "Diversity of values in specified objects in %%"},
+ {"average", o_average, "Average of values in specified objects"},
+ {"mode", o_mode, "Mode of values in specified objects"},
+ {"median", o_median, "Median of values in specified objects"},
+ {"avedev", o_adev, "Average deviation of values in specified objects"},
+ {"stddev", o_sdev, "Standard deviation of values in specified objects"},
+ {"variance", o_var, "Variance of values in specified objects"},
+ {"skewness", o_skew, "Skewnes of values in specified objects"},
+ {"kurtosis", o_kurt, "Kurtosis of values in specified objects"},
+ {"min", o_min, "Minimum of values in specified objects"},
+ {"max", o_max, "Maximum of values in specified objects"},
+ {"sum", o_sum, "Sum of values in specified objects"},
+ {NULL, NULL, NULL}
};
-/* function prototypes */
-static int is_ok(char *, char *);
-
int main(int argc, char **argv)
{
int o_method;
@@ -82,9 +77,8 @@
method->options = methods;
outputmap = G_define_standard_option(G_OPT_R_OUTPUT);
- outputmap->description =
- _("Resultant raster map (not used with 'distribution')");
- outputmap->required = NO;
+ outputmap->description = _("Resultant raster map");
+ outputmap->required = YES;
flag_c = G_define_flag();
flag_c->key = 'c';
@@ -100,10 +94,9 @@
if (Rast_map_is_fp(covermap->answer, "") != 0)
G_fatal_error(_("This module currently only works for integer (CELL) maps"));
- if (Rast_read_cats(covermap->answer, "", &cats) < 0) {
+ if (Rast_read_cats(covermap->answer, "", &cats) < 0)
G_fatal_error(_("Unable to read category file of raster map <%s>"),
covermap->answer);
- }
for (o_method = 0; menu[o_method].name; o_method++)
if (strcmp(menu[o_method].name, method->answer) == 0)
@@ -117,88 +110,10 @@
exit(EXIT_FAILURE);
}
- switch (menu[o_method].val) {
- case DISTRIB:
- if (outputmap->answer != NULL)
- G_warning(_("Output map <%s> ignored"), outputmap->answer);
+ (*menu[o_method].func)(basemap->answer, covermap->answer,
+ outputmap->answer,
+ flag_c->answer, &cats);
- o_distrib(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer);
- break;
- case AVERAGE:
- is_ok(method->answer, outputmap->answer);
- o_average(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case MODE:
- is_ok(method->answer, outputmap->answer);
- o_mode(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case ADEV:
- is_ok(method->answer, outputmap->answer);
- o_adev(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case SDEV:
- is_ok(method->answer, outputmap->answer);
- o_sdev(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case VARIANC:
- is_ok(method->answer, outputmap->answer);
- o_var(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case SKEWNES:
- is_ok(method->answer, outputmap->answer);
- o_skew(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case KURTOSI:
- is_ok(method->answer, outputmap->answer);
- o_kurt(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case MEDIAN:
- is_ok(method->answer, outputmap->answer);
- o_median(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case MIN:
- is_ok(method->answer, outputmap->answer);
- o_min(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case MAX:
- is_ok(method->answer, outputmap->answer);
- o_max(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case SUM:
- is_ok(method->answer, outputmap->answer);
- o_sum(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
- case DIV:
- is_ok(method->answer, outputmap->answer);
- o_divr(basemap->answer, covermap->answer,
- outputmap->answer, flag_c->answer, &cats);
- break;
-
- default:
- G_fatal_error(_("Not yet implemented!"));
- }
-
return 0;
}
-
-static int is_ok(char *method, char *map)
-{
- if (map == NULL)
- G_fatal_error(_("An output raster map needs to be defined with method '%s'"),
- method);
-
- return 0;
-}
Modified: grass/trunk/raster/r.statistics/method.h
===================================================================
--- grass/trunk/raster/r.statistics/method.h 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/method.h 2010-01-25 18:28:36 UTC (rev 40648)
@@ -1,20 +1,7 @@
#include <stdio.h>
+#include <grass/gis.h>
#include <grass/raster.h>
-#define DISTRIB 0
-#define AVERAGE 1
-#define MODE 2
-#define MEDIAN 3
-#define ADEV 4 /* Average deviation */
-#define SDEV 5 /* Standard deviation */
-#define VARIANC 6 /* Variance */
-#define SKEWNES 7 /* Skewnes */
-#define KURTOSI 8 /* Kurtosis */
-#define MIN 9 /* Minimum */
-#define MAX 10 /* Maximum */
-#define SUM 11 /* Sum */
-#define DIV 12 /* Diversity */
-
struct stats
{
int nalloc;
@@ -26,56 +13,30 @@
struct menu
{
const char *name; /* method name */
- int val; /* number of function */
+ int (*func)(const char *, const char *, const char *, int, struct Categories *);
const char *text; /* menu display - full description */
};
extern struct menu menu[];
-/* run_cmd.c */
-
-extern void run_stats(const char *, const char *, const char *, const char *);
-extern int run_reclass(const char *, const char *, const char *);
-
-/* o_adev.c */
int o_adev(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_average.c */
int o_average(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_distrib.c */
-int o_distrib(const char *, const char *, const char *, int);
-
-/* o_kurt.c */
+int o_divr(const char *, const char *, const char *, int, struct Categories *);
int o_kurt(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_max.c */
int o_max(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_median.c */
int o_median(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_min.c */
int o_min(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_mode.c */
int o_mode(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_sdev.c */
int o_sdev(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_skew.c */
int o_skew(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_sum.c */
int o_sum(const char *, const char *, const char *, int, struct Categories *);
-
-/* o_var.c */
int o_var(const char *, const char *, const char *, int, struct Categories *);
-/* o_divr.c */
-int o_divr(const char *, const char *, const char *, int, struct Categories *);
+/* run_cmd.c */
+FILE *run_stats(struct Popen *, const char *, const char *, const char *);
+FILE *run_reclass(struct Popen *, const char *, const char *);
+
/* read_stats.c */
int read_stats(FILE *, long *, long *, double *);
Modified: grass/trunk/raster/r.statistics/o_adev.c
===================================================================
--- grass/trunk/raster/r.statistics/o_adev.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_adev.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -13,11 +13,10 @@
static int a_dev(double *, int, double *);
-int
-o_adev(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_adev(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first, mem, i, count;
long basecat, covercat, catb, catc;
@@ -27,14 +26,9 @@
mem = MEM * sizeof(double);
tab = (double *)G_malloc(mem);
- sprintf(command, "r.stats -cn input=\"%s,%s\" fs=space", basemap,
- covermap);
+ stats = run_stats(&stats_child, basemap, covermap, "-cn");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- stats = popen(command, "r");
-
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
if (first) {
@@ -76,9 +70,10 @@
fprintf(reclass, "%ld = %ld %f\n", catb, catb, adev);
}
- pclose(stats);
- pclose(reclass);
- /**/ return (0);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
+
+ return 0;
}
Modified: grass/trunk/raster/r.statistics/o_average.c
===================================================================
--- grass/trunk/raster/r.statistics/o_average.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_average.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -8,37 +8,25 @@
/* function prototypes */
static int out(FILE *, long, double, double);
-int
-o_average(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_average(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char *me = "o_average";
+ struct Popen stats_child, reclass_child;
+ FILE *stats, *reclass;
long catb, basecat, covercat;
double x, area, sum1, sum2;
- int stat;
- char *tempfile1, *tempfile2;
- FILE *fd1, *fd2;
- tempfile1 = G_tempfile();
- tempfile2 = G_tempfile();
+ stats = run_stats(&stats_child, basemap, covermap, "-an");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- run_stats(basemap, covermap, "-a", tempfile1);
+ out(reclass, 0L, 0.0, 1.0); /* force at least one reclass rule */
- fd1 = fopen(tempfile1, "r");
- fd2 = fopen(tempfile2, "w");
- if (fd1 == NULL || fd2 == NULL) {
- unlink(tempfile1);
- unlink(tempfile2);
- G_fatal_error(_("%s: unable to open temporary file"), me);
- }
- out(fd2, 0L, 0.0, 1.0); /* force at least one reclass rule */
-
catb = 0;
sum1 = 0.0;
sum2 = 0.0;
- while (fscanf(fd1, "%ld %ld %lf", &basecat, &covercat, &area) == 3) {
+ while (fscanf(stats, "%ld %ld %lf", &basecat, &covercat, &area) == 3) {
if (catb != basecat) {
- out(fd2, catb, sum1, sum2);
+ out(reclass, catb, sum1, sum2);
sum1 = 0.0;
sum2 = 0.0;
catb = basecat;
@@ -50,18 +38,17 @@
sum1 += x * area;
sum2 += area;
}
- out(fd2, basecat, sum1, sum2);
- fclose(fd1);
- fclose(fd2);
- stat = run_reclass(basemap, outputmap, tempfile2);
- unlink(tempfile1);
- unlink(tempfile2);
- return (stat);
+ out(reclass, basecat, sum1, sum2);
+
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
+
+ return 0;
}
-static int out(FILE * fd, long cat, double sum1, double sum2)
+static int out(FILE *fp, long cat, double sum1, double sum2)
{
char buf[80];
@@ -73,7 +60,7 @@
sprintf(buf, "%.10f", sum1 / sum2);
G_trim_decimal(buf);
}
- fprintf(fd, "%ld = %ld %s\n", cat, cat, buf);
+ fprintf(fp, "%ld = %ld %s\n", cat, cat, buf);
return 0;
}
Deleted: grass/trunk/raster/r.statistics/o_distrib.c
===================================================================
--- grass/trunk/raster/r.statistics/o_distrib.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_distrib.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -1,91 +0,0 @@
-#include <stdlib.h>
-#include <unistd.h>
-#include <grass/gis.h>
-#include <grass/glocale.h>
-#include "method.h"
-
-/* function prototypes */
-static int o_out(FILE *, long, long);
-
-
-int o_distrib(const char *basemap, const char *covermap, const char *outputmap, int usecats)
-{
- char *me = "o_distrib";
- long csum, area, catb, basecat, covercat;
- double sum, tot;
- long stat, cat, total_count;
- char *tempfile1, *tempfile2;
- FILE *fd1, *fd2;
-
- tempfile1 = G_tempfile();
- tempfile2 = G_tempfile();
-
- run_stats(basemap, covermap, "-c", tempfile1);
-
- fd1 = fopen(tempfile1, "r");
- fd2 = fopen(tempfile2, "w");
- if (fd1 == NULL || fd2 == NULL) {
- unlink(tempfile1);
- unlink(tempfile2);
- G_fatal_error(_("%s: unable to open temporary file"), me);
- }
- o_out(fd2, 0L, 0); /* force at least one reclass rule */
-
- catb = 0;
- csum = 0;
-
- G_debug(1, "***** Stage 1 - Calculating sums ****");
-
- while (fscanf(fd1, "%ld %ld %ld", &basecat, &covercat, &area) == 3) {
- if (catb != basecat) {
- o_out(fd2, catb, csum);
- csum = 0;
- catb = basecat;
- }
- csum += area;
- }
- o_out(fd2, catb, csum);
-
- rewind(fd1);
- freopen(tempfile2, "r", fd2);
-
- G_debug(1, "***** Stage 2 - Calculating percents of values in cover ****");
-
- catb = 0;
- tot = 0;
- total_count = 0;
-
- while (fscanf(fd1, "%ld %ld %ld", &basecat, &covercat, &area) == 3) {
- if (catb != basecat && basecat > 0) {
- if (fscanf(fd2, "%ld %ld", &cat, &total_count) != 2)
- return (1);
- catb = basecat;
- /* fprintf(stderr,"Total (must be 100): %lf\n",tot); */
- tot = 0;
- }
-
- if (basecat) {
- sum = (double)(100.0 * area) / total_count;
- fprintf(stdout, "%8ld %8ld %f\n", basecat, covercat, sum);
- /*tot+=sum;
- fprintf(stderr,"Area: %ld Tot: %ld totsum: %lf\n",area,total_count,tot); */
- }
- }
-
- fclose(fd1);
- fclose(fd2);
- unlink(tempfile1);
- unlink(tempfile2);
-
- return (stat);
-}
-
-
-static int o_out(FILE * fd, long cat, long sum)
-{
- if (sum == 0 || cat == 0)
- return -1;
- fprintf(fd, "%ld %ld\n", cat, sum);
-
- return 0;
-}
Modified: grass/trunk/raster/r.statistics/o_divr.c
===================================================================
--- grass/trunk/raster/r.statistics/o_divr.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_divr.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -4,34 +4,27 @@
#include <grass/raster.h>
#include "method.h"
-int
-o_divr(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_divr(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
- FILE *stats_fd, *reclass_fd;
+ struct Popen stats_child, reclass_child;
+ FILE *stats_fp, *reclass_fp;
int first;
long basecat, covercat, catb, catc;
double area;
+ stats_fp = run_stats(&stats_child, basemap, covermap, "-an");
+ reclass_fp = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.stats -an input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats_fd = popen(command, "r");
-
-
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass_fd = popen(command, "w");
-
first = 1;
- while (read_stats(stats_fd, &basecat, &covercat, &area)) {
+ while (read_stats(stats_fp, &basecat, &covercat, &area)) {
if (first) {
first = 0;
catb = basecat;
catc = 0;
}
if (basecat != catb) {
- write_reclass(reclass_fd, catb, catc, Rast_get_c_cat((CELL *) &catc, cats),
+ write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats),
usecats);
catb = basecat;
catc = 0;
@@ -39,10 +32,11 @@
catc++;
}
if (!first)
- write_reclass(reclass_fd, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
+ write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
- pclose(stats_fd);
- pclose(reclass_fd);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- exit(EXIT_SUCCESS);
+ return 0;
}
+
Modified: grass/trunk/raster/r.statistics/o_kurt.c
===================================================================
--- grass/trunk/raster/r.statistics/o_kurt.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_kurt.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -13,11 +13,10 @@
static int kurt(double *, int, double *);
-int
-o_kurt(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_kurt(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first, mem, i, count;
long basecat, covercat, catb, catc;
@@ -27,13 +26,9 @@
mem = MEM * sizeof(double);
tab = (double *)G_malloc(mem);
- sprintf(command, "r.stats -cn input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats = popen(command, "r");
+ stats = run_stats(&stats_child, basemap, covermap, "-cn");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
if (first) {
@@ -74,11 +69,10 @@
kurt(tab, count, &var);
fprintf(reclass, "%ld = %ld %f\n", catb, catb, var);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- pclose(stats);
- pclose(reclass);
-
- return (0);
+ return 0;
}
Modified: grass/trunk/raster/r.statistics/o_max.c
===================================================================
--- grass/trunk/raster/r.statistics/o_max.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_max.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -4,22 +4,17 @@
#include <grass/raster.h>
#include "method.h"
-int
-o_max(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_max(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first;
long basecat, covercat, catb, catc;
- sprintf(command, "r.stats -n input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats = popen(command, "r");
+ stats = run_stats(&stats_child, basemap, covermap, "-n");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
first = 1;
while (fscanf(stats, "%ld %ld", &basecat, &covercat) == 2) {
@@ -46,8 +41,9 @@
write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
- pclose(stats);
- pclose(reclass);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- return (0);
+ return 0;
}
+
Modified: grass/trunk/raster/r.statistics/o_median.c
===================================================================
--- grass/trunk/raster/r.statistics/o_median.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_median.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -8,28 +8,21 @@
static long median(struct stats *);
-int
-o_median(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_median(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
- FILE *stats_fd, *reclass_fd;
+ struct Popen stats_child, reclass_child;
+ FILE *stats_fp, *reclass_fp;
int first;
long basecat, covercat, catb, catc;
double area;
struct stats stats;
+ stats_fp = run_stats(&stats_child, basemap, covermap, "-an");
+ reclass_fp = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.stats -an input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats_fd = popen(command, "r");
-
-
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass_fd = popen(command, "w");
-
first = 1;
- while (read_stats(stats_fd, &basecat, &covercat, &area)) {
+ while (read_stats(stats_fp, &basecat, &covercat, &area)) {
if (first) {
stats.n = 0;
stats.nalloc = 16;
@@ -42,7 +35,7 @@
}
if (basecat != catb) {
catc = median(&stats);
- write_reclass(reclass_fd, catb, catc, Rast_get_c_cat((CELL *) &catc, cats),
+ write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats),
usecats);
catb = basecat;
stats.n = 0;
@@ -60,13 +53,13 @@
}
if (!first) {
catc = median(&stats);
- write_reclass(reclass_fd, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
+ write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
}
- pclose(stats_fd);
- pclose(reclass_fd);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- exit(EXIT_SUCCESS);
+ return 0;
}
Modified: grass/trunk/raster/r.statistics/o_min.c
===================================================================
--- grass/trunk/raster/r.statistics/o_min.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_min.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -4,22 +4,17 @@
#include <grass/raster.h>
#include "method.h"
-int
-o_min(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_min(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first;
long basecat, covercat, catb, catc;
- sprintf(command, "r.stats -n input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats = popen(command, "r");
+ stats = run_stats(&stats_child, basemap, covermap, "-n");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
first = 1;
while (fscanf(stats, "%ld %ld", &basecat, &covercat) == 2) {
@@ -46,8 +41,8 @@
write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
- pclose(stats);
- pclose(reclass);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- return (0);
+ return 0;
}
Modified: grass/trunk/raster/r.statistics/o_mode.c
===================================================================
--- grass/trunk/raster/r.statistics/o_mode.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_mode.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -8,19 +8,15 @@
o_mode(const char *basemap, const char *covermap, const char *outputmap, int usecats,
struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first;
long basecat, covercat, catb, catc;
double value, max;
- sprintf(command, "r.stats -an input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats = popen(command, "r");
+ stats = run_stats(&stats_child, basemap, covermap, "-an");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
@@ -51,8 +47,9 @@
write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats);
- pclose(stats);
- pclose(reclass);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- return (0);
+ return 0;
}
+
Modified: grass/trunk/raster/r.statistics/o_sdev.c
===================================================================
--- grass/trunk/raster/r.statistics/o_sdev.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_sdev.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -13,11 +13,10 @@
static int s_dev(double *, int, double *);
-int
-o_sdev(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_sdev(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first, mem, i, count;
long basecat, covercat, catb, catc;
@@ -28,15 +27,9 @@
mem = MEM * sizeof(double);
tab = (double *)G_malloc(mem);
- sprintf(command, "r.stats -cn input=\"%s,%s\" fs=space", basemap,
- covermap);
+ stats = run_stats(&stats_child, basemap, covermap, "-cn");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- stats = popen(command, "r");
-
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
-
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
if (first) {
@@ -78,10 +71,10 @@
fprintf(reclass, "%ld = %ld %f\n", catb, catb, sdev);
G_debug(5, "%ld = %ld %f\n", catb, catb, sdev);
- pclose(stats);
- pclose(reclass);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- return (0);
+ return 0;
}
Modified: grass/trunk/raster/r.statistics/o_skew.c
===================================================================
--- grass/trunk/raster/r.statistics/o_skew.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_skew.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -13,11 +13,10 @@
static int skew(double *, int, double *);
-int
-o_skew(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_skew(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first, mem, i, count;
long basecat, covercat, catb, catc;
@@ -28,14 +27,9 @@
mem = MEM * sizeof(double);
tab = (double *)G_malloc(mem);
- sprintf(command, "r.stats -cn input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats = popen(command, "r");
+ stats = run_stats(&stats_child, basemap, covermap, "-cn");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
-
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
if (first) {
@@ -76,13 +70,12 @@
skew(tab, count, &var);
fprintf(reclass, "%ld = %ld %f\n", catb, catb, var);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- pclose(stats);
- pclose(reclass);
- /**/ return (0);
+ return 0;
}
-
/***********************************************************************
*
* Given an array of data[1...n], this routine returns its skewness
Modified: grass/trunk/raster/r.statistics/o_sum.c
===================================================================
--- grass/trunk/raster/r.statistics/o_sum.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_sum.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -5,46 +5,29 @@
#include <grass/glocale.h>
#include "method.h"
-#define STATS "r.stats"
-#define RECLASS "r.reclass"
-
/* function prototypes */
static void sum_out(FILE *, long, double);
-
-int
-o_sum(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_sum(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char *me = "o_sum";
-
long catb, basecat, covercat;
double x, area, sum1;
int stat;
- char *tempfile1, *tempfile2;
- FILE *fd1, *fd2;
+ struct Popen stats_child, reclass_child;
+ FILE *stats, *reclass;
- tempfile1 = G_tempfile();
- tempfile2 = G_tempfile();
+ stats = run_stats(&stats_child, basemap, covermap, "-cn");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- run_stats(basemap, covermap, "-c", tempfile1);
+ sum_out(reclass, 0L, 0.0); /* force at least one reclass rule */
- fd1 = fopen(tempfile1, "r");
- fd2 = fopen(tempfile2, "w");
- if (fd1 == NULL || fd2 == NULL) {
- unlink(tempfile1);
- unlink(tempfile2);
- G_fatal_error(_("%s: unable to open temporary file"), me);
- }
- sum_out(fd2, 0L, 0.0); /* force at least one reclass rule */
-
catb = 0;
sum1 = 0.0;
-
- while (fscanf(fd1, "%ld %ld %lf", &basecat, &covercat, &area) == 3) {
+ while (fscanf(stats, "%ld %ld %lf", &basecat, &covercat, &area) == 3) {
if (catb != basecat) {
- sum_out(fd2, catb, sum1);
+ sum_out(reclass, catb, sum1);
sum1 = 0.0;
catb = basecat;
}
@@ -56,18 +39,15 @@
/* fprintf(stderr,"sum: %d\n",(int)sum1); */
}
- sum_out(fd2, basecat, sum1);
- fclose(fd1);
- fclose(fd2);
- stat = run_reclass(basemap, outputmap, tempfile2);
- unlink(tempfile1);
- unlink(tempfile2);
+ sum_out(reclass, basecat, sum1);
- return (stat);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
+
+ return stat;
}
-
-static void sum_out(FILE * fd, long cat, double sum1)
+static void sum_out(FILE *fp, long cat, double sum1)
{
char buf[64];
@@ -78,5 +58,6 @@
G_trim_decimal(buf);
}
- fprintf(fd, "%ld = %ld %s\n", cat, cat, buf);
+ fprintf(fp, "%ld = %ld %s\n", cat, cat, buf);
}
+
Modified: grass/trunk/raster/r.statistics/o_var.c
===================================================================
--- grass/trunk/raster/r.statistics/o_var.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/o_var.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -13,11 +13,10 @@
static int m_var(double *, int, double *);
-int
-o_var(const char *basemap, const char *covermap, const char *outputmap, int usecats,
- struct Categories *cats)
+int o_var(const char *basemap, const char *covermap, const char *outputmap,
+ int usecats, struct Categories *cats)
{
- char command[1024];
+ struct Popen stats_child, reclass_child;
FILE *stats, *reclass;
int first, mem, i, count;
long basecat, covercat, catb, catc;
@@ -28,14 +27,9 @@
mem = MEM * sizeof(double);
tab = (double *)G_malloc(mem);
- sprintf(command, "r.stats -cn input=\"%s,%s\" fs=space", basemap,
- covermap);
- stats = popen(command, "r");
+ stats = run_stats(&stats_child, basemap, covermap, "-cn");
+ reclass = run_reclass(&reclass_child, basemap, outputmap);
- sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", basemap, outputmap);
- reclass = popen(command, "w");
-
-
first = 1;
while (read_stats(stats, &basecat, &covercat, &value)) {
if (first) {
@@ -79,10 +73,10 @@
fprintf(reclass, "%ld = %ld %f\n", catb, catb, vari);
G_debug(5, "2. %ld = %ld %f", catb, catb, vari);
- pclose(stats);
- pclose(reclass);
+ G_popen_close(&stats_child);
+ G_popen_close(&reclass_child);
- return (0);
+ return 0;
}
Modified: grass/trunk/raster/r.statistics/r.statistics.html
===================================================================
--- grass/trunk/raster/r.statistics/r.statistics.html 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/r.statistics.html 2010-01-25 18:28:36 UTC (rev 40648)
@@ -13,7 +13,6 @@
The available methods are the following (english - german):
<ul>
-<li>distribution - Verteilung in Prozent</li>
<li>average - Durchschnitt</li>
<li>mode - Modalwert</li>
<li>median - Median</li>
@@ -36,39 +35,11 @@
stored.
<p>
-All calculations except "distribution" create an output layer. The output
+All calculations create an output layer. The output
layer is a reclassified version of the base layer with identical
category values, but modified category labels - the results of the calculations
are stored in the category labels of the output layer.
-<p>
-For distributions, the output is printed to the user interface (stdout).
-If an output file name was specified, it will be ignored. The result will
-be a text table with three columns.
-In the first column are the category values of the base layer (a), in the second
-column the associated value of the cover layers (b), and in the third column the
-percentage of area in that base layer category (a) that falls into that row's
-value in the cover layer(b). Example:
-
-<div class="code"><pre>
-1 124 23
-1 201 47
-1 273 30
-2 101 5
-2 152 16
-2 167 60
-2 187 19
- .
- .
- .
-</pre></div>
-
-So for the first line in the output above, we see that 23% of the cells of the
-base layer category 1 have a value of 124 in the cover layer.
-
-To transfer the values stored as category labels into cell values,
-<em>r.mapcalc</em> can be used ('@' operator).
-
<h2>EXAMPLES</h2>
Calculation of average elevation of each field in the Spearfish region:
Modified: grass/trunk/raster/r.statistics/run_cmd.c
===================================================================
--- grass/trunk/raster/r.statistics/run_cmd.c 2010-01-25 12:20:13 UTC (rev 40647)
+++ grass/trunk/raster/r.statistics/run_cmd.c 2010-01-25 18:28:36 UTC (rev 40648)
@@ -4,62 +4,49 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <grass/gis.h>
-#include <grass/spawn.h>
#include "method.h"
-void run_stats(const char *basemap, const char *covermap, const char *mode,
- const char *tempfile)
+FILE *run_stats(struct Popen *child,
+ const char *basemap, const char *covermap, const char *mode)
{
- char buf[6 + GNAME_MAX + 1 + GMAPSET_MAX + 1 + GNAME_MAX + 1 + GMAPSET_MAX + 1];
- const char *argv[10];
- int argc = 0;
+ char input[6 + GNAME_MAX + 1 + GMAPSET_MAX + 1 + GNAME_MAX + 1 + GMAPSET_MAX + 1];
+ const char *argv[5];
+ FILE *fp;
- argv[argc++] = "r.stats";
+ sprintf(input, "input=%s,%s", basemap, covermap);
- argv[argc++] = mode;
+ argv[0] = "r.stats";
+ argv[1] = mode;
+ argv[2] = input;
+ argv[3] = "fs=space";
+ argv[4] = NULL;
- argv[argc++] = "-n";
-
- sprintf(buf, "input=%s,%s", basemap, covermap);
- argv[argc++] = buf;
-
- argv[argc++] = "fs=space";
-
- argv[argc++] = SF_REDIRECT_FILE;
- argv[argc++] = SF_STDOUT;
- argv[argc++] = SF_MODE_OUT;
- argv[argc++] = tempfile;
-
- argv[argc++] = NULL;
-
- if (G_vspawn_ex(argv[0], argv) != 0) {
- remove(tempfile);
+ fp = G_popen_read(child, argv[0], argv);
+ if (!fp)
G_fatal_error("error running r.stats");
- }
+
+ return fp;
}
-int run_reclass(const char *basemap, const char *outputmap, const char *tempfile)
+FILE *run_reclass(struct Popen *child, const char *basemap, const char *outputmap)
{
- char buf1[6 + GNAME_MAX + 1 + GMAPSET_MAX + 1];
- char buf2[7 + GNAME_MAX + 1];
- const char *argv[8];
- int argc = 0;
+ char input[6 + GNAME_MAX + 1 + GMAPSET_MAX + 1];
+ char output[7 + GNAME_MAX + 1];
+ const char *argv[4];
+ FILE *fp;
- argv[argc++] = "r.reclass";
+ sprintf(input, "input=%s", basemap);
+ sprintf(output, "output=%s", outputmap);
- sprintf(buf1, "input=%s", basemap);
- argv[argc++] = buf1;
+ argv[0] = "r.reclass";
+ argv[1] = input;
+ argv[2] = output;
+ argv[3] = NULL;
- sprintf(buf2, "output=%s", outputmap);
- argv[argc++] = buf2;
+ fp = G_popen_write(child, argv[0], argv);
+ if (!fp)
+ G_fatal_error("error running r.stats");
- argv[argc++] = SF_REDIRECT_FILE;
- argv[argc++] = SF_STDIN;
- argv[argc++] = SF_MODE_IN;
- argv[argc++] = tempfile;
-
- argv[argc++] = NULL;
-
- return G_vspawn_ex(argv[0], argv);
+ return fp;
}
More information about the grass-commit
mailing list