~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to display/d.histogram/get_stats.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <string.h>
4
 
#include <stdarg.h>
5
4
#include <unistd.h>
6
5
#include <grass/gis.h>
 
6
#include <grass/raster.h>
 
7
#include <grass/spawn.h>
7
8
#include "options.h"
8
9
#include "dhist.h"
9
10
 
10
 
static char *mk_command(const char *fmt, int nargs, ...)
 
11
static void run_stats(const char *mapname, const char *tempfile)
11
12
{
12
 
    /* asprintf() would solve this problem better */
13
 
    size_t len = strlen(fmt) + 1;
14
 
    char *cmd;
15
 
    va_list ap;
16
 
 
17
 
    va_start(ap, nargs);
18
 
 
19
 
    while (nargs--) {
20
 
        cmd = va_arg(ap, char *);
21
 
 
22
 
        len += strlen(cmd);
 
13
    char buf[32];
 
14
    const char *argv[12];
 
15
    int argc = 0;
 
16
 
 
17
    argv[argc++] = "r.stats";
 
18
 
 
19
    argv[argc++] = "-r";
 
20
    if (cat_ranges)
 
21
        argv[argc++] = "-C";
 
22
 
 
23
    argv[argc++] = type == COUNT
 
24
        ? "-c"
 
25
        : "-a";
 
26
 
 
27
    argv[argc++] = mapname;
 
28
 
 
29
    if (!cat_ranges) {
 
30
        sprintf(buf, "nsteps=%d", nsteps);
 
31
        argv[argc++] = buf;
23
32
    }
24
33
 
25
 
    va_end(ap);
26
 
 
27
 
    cmd = G_malloc(len);
28
 
 
29
 
    va_start(ap, nargs);
30
 
    vsprintf(cmd, fmt, ap);
31
 
 
32
 
    va_end(ap);
33
 
 
34
 
    return cmd;
 
34
    argv[argc++] = SF_REDIRECT_FILE;
 
35
    argv[argc++] = SF_STDOUT;
 
36
    argv[argc++] = SF_MODE_OUT;
 
37
    argv[argc++] = tempfile;
 
38
 
 
39
    argv[argc++] = NULL;
 
40
 
 
41
    if (G_vspawn_ex(argv[0], argv) != 0)
 
42
        G_fatal_error("error running r.stats");
35
43
}
36
44
 
37
 
int get_stats(char *mapname, char *mapset, struct stat_list *dist_stats,        /* linked list of stats */
38
 
              int quiet)
 
45
int get_stats(const char *mapname, struct stat_list *dist_stats)        /* linked list of stats */
39
46
{
40
47
    char buf[1024];             /* input buffer for reading stats */
41
48
    int done = 0;
42
49
    char *tempfile;             /* temp file name */
43
 
    char *fullname;
44
 
    char *cmd;
45
50
    FILE *fd;                   /* temp file pointer */
46
51
 
47
52
    long int cat;               /* a category value */
51
56
 
52
57
    /* write stats to a temp file */
53
58
    tempfile = G_tempfile();
54
 
    fullname = G_fully_qualified_name(mapname, mapset);
55
 
    is_fp = G_raster_map_is_fp(mapname, mapset);
 
59
    is_fp = Rast_map_is_fp(mapname, "");
56
60
    if (is_fp) {
57
61
        if (cat_ranges) {
58
 
            if (G_read_raster_cats(mapname, mapset, &cats) < 0)
 
62
            if (Rast_read_cats(mapname, "", &cats) < 0)
59
63
                G_fatal_error("Can't read category file");
60
 
            if (G_number_of_raster_cats(&cats) <= 0) {
 
64
            if (Rast_number_of_cats(&cats) <= 0) {
61
65
                G_warning("There are no labeled cats, using nsteps argument");
62
66
                cat_ranges = 0;
63
67
            }
64
68
        }
65
 
        if (G_read_fp_range(map_name, mapset, &fp_range) <= 0)
 
69
        if (Rast_read_fp_range(map_name, "", &fp_range) <= 0)
66
70
            G_fatal_error("Can't read frange file");
67
71
    }
68
 
    if (cat_ranges) {
69
 
        cmd = mk_command("r.stats -Cr%s%s \"%s\" > \"%s\"\n", 4,
70
 
                         type == COUNT ? "c" : "a", quiet ? "q" : "",
71
 
                         fullname, tempfile);
72
 
    }
73
 
    else {
74
 
        sprintf(buf, "%d", nsteps);
75
 
        cmd = mk_command("r.stats -r%s%s \"%s\" nsteps=%s > \"%s\"\n", 5,
76
 
                         type == COUNT ? "c" : "a", quiet ? "q" : "",
77
 
                         fullname, buf, tempfile);
78
 
    }
79
72
 
80
 
    if (system(cmd))
81
 
        G_fatal_error("%s: ERROR running r.stats", G_program_name());
 
73
    run_stats(mapname, tempfile);
82
74
 
83
75
    /* open temp file and read the stats into a linked list */
84
76
    fd = fopen(tempfile, "r");