[GRASS5] calculating averages on raster map patches?

Hi,

I have a floasting point EVI vegetation index map from MODIS and vector fields.
Now I need to calculate the average EVI value per field. To do so, I

- used v.to.rast to generate a raster fields map (each field one number)
- run r.average: segmentation fault because the underlying 'r.stats -anC '
  segfaults
- run r.statistics: doesn't support FCELL/DCELL

If you want to test with Spearfish:
g.region -dp
r.average -c base=fields cover=elevation.10m output=elev.by.farm
WARNING: r.stats: cats for elevation.10m are either missing or have no
         explicit labels. Using nsteps=255

         -> doesn't sound convincing either

On a 64bit box I get a segfault from r.stats (called by r.average) after this
message:

gdb `which r.stats`
(gdb) r -anC input='areecampione125m,modis_vi250m20010712.250m_16_days_EVI' fs=space
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402c02 in main (argc=1, argv=0x3535323d737065) at main.c:242
242 for (i = 0; name = option.cell->answers[i]; i++)

(gdb) bt full
#0 0x0000000000402c02 in main (argc=1, argv=0x3535323d737065) at main.c:242
        msg = "r.stats: cats for modis_vi250m20010712.250m_16_days_EVI are either missing or have no explicit label"
        fd = (int *) 0x5090a0
        i = 2
        name = 0xe8 <Address 0xe8 out of bounds>
        mapset = 0x1 <Address 0x1 out of bounds>
        verbose = 1
        raw_data = 1575156072
        with_coordinates = 0
        with_xy = 0
        with_percents = 0
        with_counts = 0
        with_areas = 1
        with_labels = 0
        fmt = '\0' <repeats 19 times>
        range = {min = 1, max = 40, first_time = 0}
        fp_range = {min = -0.247, max = 1, first_time = 0}
        q = {truncate_only = 0, round_only = 0, defaultDRuleSet = 4199435, defaultCRuleSet = 0, infiniteLeftSet = 0,
  infiniteRightSet = 0, cRangeSet = 1569807600, maxNofRules = 0, nofRules = 0, defaultDMin = 2.7108496074642367e-312,
  defaultDMax = 1.2174480074876701e-316, defaultCMin = 4199435, defaultCMax = 0, infiniteDLeft = 0,
  infiniteDRight = 1.1111882213708365e-312, infiniteCLeft = -1786587576, infiniteCRight = 42, dMin = -0.247, dMax = 1, cMin = 1,
  cMax = 255, table = 0x50cdc0, fp_lookup = {vals = 0x3436, rules = 0x0, nalloc = 0, active = 0,
    inf_dmin = 9.0363127466918322e-313, inf_dmax = 1.1111936913593466e-312, inf_min = -1073744856, inf_max = 127}}
        min = 1
        max = 255
        null_set = 1
        dmin = -0.247
        dmax = 1
        module = (struct GModule *) 0xe8
        flag = {A = 0x5089d0, a = 0x508a20, c = 0x508a70, p = 0x508ac0, l = 0x508b10, q = 0x508b60, n = 0x508bb0, N = 0x508c00,
  one = 0x2a95705380, x = 0x508ca0, g = 0x508c50, i = 0x508d90, r = 0x508d40, C = 0x508cf0}
        option = {cell = 0x3535323d737065, fs = 0x508730, nv = 0x5087c0, output = 0x508850, nsteps = 0x508900}

Any ideas? I just wonder about

/* open all cell files */
    for (i = 0; name = option.cell->answers[i]; i++)
                    ^^^

Does anyone know how to get the desired result?
Can't believe that such a standard application fails... :frowning:

Markus

Markus Neteler wrote:

On a 64bit box I get a segfault from r.stats (called by r.average) after this
message:

gdb `which r.stats`
(gdb) r -anC input='areecampione125m,modis_vi250m20010712.250m_16_days_EVI' fs=space
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402c02 in main (argc=1, argv=0x3535323d737065) at main.c:242
242 for (i = 0; name = option.cell->answers[i]; i++)

(gdb) bt full
#0 0x0000000000402c02 in main (argc=1, argv=0x3535323d737065) at main.c:242
        msg = "r.stats: cats for modis_vi250m20010712.250m_16_days_EVI are either missing or have no explicit label"
        fd = (int *) 0x5090a0
        i = 2
        name = 0xe8 <Address 0xe8 out of bounds>
        mapset = 0x1 <Address 0x1 out of bounds>
        verbose = 1
        raw_data = 1575156072
        with_coordinates = 0
        with_xy = 0
        with_percents = 0
        with_counts = 0
        with_areas = 1
        with_labels = 0
        fmt = '\0' <repeats 19 times>
        range = {min = 1, max = 40, first_time = 0}
        fp_range = {min = -0.247, max = 1, first_time = 0}
        q = {truncate_only = 0, round_only = 0, defaultDRuleSet = 4199435, defaultCRuleSet = 0, infiniteLeftSet = 0,
  infiniteRightSet = 0, cRangeSet = 1569807600, maxNofRules = 0, nofRules = 0, defaultDMin = 2.7108496074642367e-312,
  defaultDMax = 1.2174480074876701e-316, defaultCMin = 4199435, defaultCMax = 0, infiniteDLeft = 0,
  infiniteDRight = 1.1111882213708365e-312, infiniteCLeft = -1786587576, infiniteCRight = 42, dMin = -0.247, dMax = 1, cMin = 1,
  cMax = 255, table = 0x50cdc0, fp_lookup = {vals = 0x3436, rules = 0x0, nalloc = 0, active = 0,
    inf_dmin = 9.0363127466918322e-313, inf_dmax = 1.1111936913593466e-312, inf_min = -1073744856, inf_max = 127}}
        min = 1
        max = 255
        null_set = 1
        dmin = -0.247
        dmax = 1
        module = (struct GModule *) 0xe8
        flag = {A = 0x5089d0, a = 0x508a20, c = 0x508a70, p = 0x508ac0, l = 0x508b10, q = 0x508b60, n = 0x508bb0, N = 0x508c00,
  one = 0x2a95705380, x = 0x508ca0, g = 0x508c50, i = 0x508d90, r = 0x508d40, C = 0x508cf0}
        option = {cell = 0x3535323d737065, fs = 0x508730, nv = 0x5087c0, output = 0x508850, nsteps = 0x508900}

Any ideas?

Was this compiled without optimisation? If you enable optimisation
(e.g. -O2, which is added by default unless you manually specify
CFLAGS when configuring), output from gdb is often meaningless.

I just wonder about

/* open all cell files */
    for (i = 0; name = option.cell->answers[i]; i++)
                    ^^^

That looks fine to me. It's iterating over all answers until it gets a
null pointer, marking the end of the list.

--
Glynn Clements <glynn@gclements.plus.com>

On Tue, 2005-08-23 at 23:05 +0100, Glynn Clements wrote:

Was this compiled without optimisation? If you enable optimisation
(e.g. -O2, which is added by default unless you manually specify
CFLAGS when configuring), output from gdb is often meaningless.

I thought optimizing with -g or -ggdb is no longer an issue. I see no
documentation to the contrary.

...Nevermind. I just found gdb documentation to the contrary. Updating
C[XX]FLAGS.

--
Brad Douglas <rez@touchofmadness.com>

Brad,

when you are at it, would you be also able to try out v.to.rast with an option to compute the
direction of the lines (e.g. streams)?

It is in
http://skagit.meas.ncsu.edu/~helena/grasswork/grasscontrib/
v.to.rastdir.tar

Jaro has already updated it to GRASS6, so it just needs to be merged with any
changes that might have been done since april and try it out. It should convert
lines to a raster with cell values representing the aspect (or angle measured from north, I believe).
So for streams that are digitized downstream the angle would represent flow direction.
There are many applications for this, among them the angle can be used in r.sim.water and probably
also in r.flow to prescribe flow direction.

Thank you,

Helena

I updated r.carve to GRASS6 and it seems to work rather well.

Is it okay if I add it to CVS?

Brad Douglas <rez@touchofmadness.com>

On Tue, 2005-08-23 at 23:59 -0400, Helena Mitasova wrote:

Brad,

when you are at it, would you be also able to try out v.to.rast with an
option to compute the direction of the lines (e.g. streams)?

Committed to CVS.

I ran it against 'streams' vector in the spearfish dataset and the
results looks reasonable upon inspection.

--
Brad Douglas <rez@touchofmadness.com>

On Tue, Aug 23, 2005 at 11:05:45PM +0100, Glynn Clements wrote:

Markus Neteler wrote:

> On a 64bit box I get a segfault from r.stats (called by r.average) after this
> message:
>

...

> Any ideas?

Was this compiled without optimisation? If you enable optimisation
(e.g. -O2, which is added by default unless you manually specify
CFLAGS when configuring), output from gdb is often meaningless.

Sorry, disabled now. BTW: It's hard to tell configure to disable the
optimization:

./conf_grass61_64bit.sh
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
checking whether the C compiler (gcc -g -Wall -Werror-implicit-function-declaration -fno-common -mcpu=nocona -mtune=nocona -m64 -minline-all-stringops ) works... yes
checking whether the C compiler (gcc -g -Wall -Werror-implicit-function-declaration -fno-common -mcpu=nocona -mtune=nocona -m64 -minline-all-stringops ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
...

grep OPTI include/Make/Platform.make
CFLAGS_OPTIMIZE = -O2
LDFLAGS_OPTIMIZE =

It still get's in.
Anyway.

Manually resolved, now recompiled from scratch.

(gdb) r -anC input='areecampione125m,modis_vi250m20010712.250m_16_days_EVI' fs=space
Starting program: /hardmnt/eden0/ssi/software/cvsgrass61/dist.x86_64-unknown-linux-gnu/bin/r.stats -anC input='areecampione125m,modis_vi250m20010712.250m_16_days_EVI' fs=space
Detaching after fork from child process 6288.
Detaching after fork from child process 6289.
WARNING: r.stats: cats for modis_vi250m20010712.250m_16_days_EVI are either
         missing or have no explicit labels. Using nsteps=255

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402b7f in main (argc=4, argv=0x7fbffff498) at main.c:242
242 for (i = 0; name = option.cell->answers[i]; i++)
(gdb) bt full
#0 0x0000000000402b7f in main (argc=4, argv=0x7fbffff498) at main.c:242
        to_screen = 0x404c28 " output to screen "
        fd = (int *) 0x5090a0
        i = 2
        name = 0x508ec0 "modis_vi250m20010712.250m_16_days_EVI"
        mapset = 0x509300 "modisNDVI_v004"
        verbose = 1
        raw_data = 0
        with_coordinates = 0
        with_xy = 0
        with_percents = 0
        with_counts = 0
        with_areas = 1
        with_labels = 0
        fmt = "\030�\177\000\000\000Y\221]4\000\000\000\000\000\000"
        dp = -1
        range = {min = 1, max = 40, first_time = 0}
        fp_range = {min = -0.247, max = 1, first_time = 0}
        q = {truncate_only = 0, round_only = 0, defaultDRuleSet = 0, defaultCRuleSet = 0, infiniteLeftSet = 0,
  infiniteRightSet = 0, cRangeSet = 1572922248, maxNofRules = 0, nofRules = 0, defaultDMin = 3.4584595208887258e-323,
  defaultDMax = 9.0363178007857629e-313, defaultCMin = -1786485176, defaultCMax = 42, infiniteDLeft = 2.7108496073851862e-312,
  infiniteDRight = 2.0729848267200353e-317, infiniteCLeft = 24641422, infiniteCRight = 0, dMin = -0.247, dMax = 1, cMin = 1,
  cMax = 255, table = 0x50e770, fp_lookup = {vals = 0x7fbffff3b0, rules = 0x177ff8e, nalloc = 4199404, active = 0, inf_dmin = 0,
    inf_dmax = 1.1111882213708365e-312, inf_min = -1786485176, inf_max = 42}}
       min = 1
        max = 255
        null_set = 1
        dmin = -0.247
        dmax = 1
        module = (struct GModule *) 0x2a9571d4b0
        flag = {A = 0x5089d0, a = 0x508a20, c = 0x508a70, p = 0x508ac0, l = 0x508b10, q = 0x508b60, n = 0x508bb0, N = 0x508c00,
  one = 0x2a9571d3e0, x = 0x508ca0, g = 0x508c50, i = 0x508d90, r = 0x508d40, C = 0x508cf0}
        option = {cell = 0x3535323d737065, fs = 0x508730, nv = 0x5087c0, output = 0x508850, nsteps = 0x508900}

This looks similar (but hopefully more reasonable) to the first report.
http://grass.itc.it/pipermail/grass5/2005-August/019354.html

Markus

Markus Neteler wrote:

> > On a 64bit box I get a segfault from r.stats (called by r.average) after this
> > message:
> >
...
> > Any ideas?
>
> Was this compiled without optimisation? If you enable optimisation
> (e.g. -O2, which is added by default unless you manually specify
> CFLAGS when configuring), output from gdb is often meaningless.

Sorry, disabled now. BTW: It's hard to tell configure to disable the
optimization:

Unless the behaviour of AC_PROG_CC has changed recently, you can force
the initial CFLAGS setting by setting the environment variable CFLAGS
before running configure, e.g.:

  CFLAGS=-g ./configure

From the autoconf documentation:

- Variable: CFLAGS
     Debugging and optimization options for the C compiler. If it is
     not set in the environment when `configure' runs, the default
     value is set when you call `AC_PROG_CC' (or empty if you don't).
     `configure' uses this variable when compiling programs to test for
     C features.

- Macro: AC_PROG_CC ([COMPILER-SEARCH-LIST])

  [snip]

     If using the GNU C compiler, set shell variable `GCC' to `yes'.
     If output variable `CFLAGS' was not already set, set it to `-g
     -O2' for the GNU C compiler (`-O2' on systems where GCC does not
     accept `-g'), or `-g' for other compilers.

Manually resolved, now recompiled from scratch.

(gdb) r -anC input='areecampione125m,modis_vi250m20010712.250m_16_days_EVI' fs=space
Starting program: /hardmnt/eden0/ssi/software/cvsgrass61/dist.x86_64-unknown-linux-gnu/bin/r.stats -anC input='areecampione125m,modis_vi250m20010712.250m_16_days_EVI' fs=space
Detaching after fork from child process 6288.
Detaching after fork from child process 6289.
WARNING: r.stats: cats for modis_vi250m20010712.250m_16_days_EVI are either
         missing or have no explicit labels. Using nsteps=255

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402b7f in main (argc=4, argv=0x7fbffff498) at main.c:242
242 for (i = 0; name = option.cell->answers[i]; i++)
(gdb) bt full
#0 0x0000000000402b7f in main (argc=4, argv=0x7fbffff498) at main.c:242

        i = 2

        option = {cell = 0x3535323d737065, fs = 0x508730, nv = 0x5087c0, output = 0x508850, nsteps = 0x508900}

This looks similar (but hopefully more reasonable) to the first
report.

That option.cell == 0x3535323d737065 looks quite wrong. The fact that
i == 2 suggests that it didn't start out wrong but has been
overwritten, apparently with ASCII:

  $ echo -ne 'eps=255\0' | od -t x8
  0000000 003535323d737065

Hmm.

  char msg[100];

  sprintf(msg, "%s: cats for %s are either missing or have no explicit labels. Using nsteps=%d", G_program_name(),name, nsteps);

Assuming:

  G_program_name() = "r.stats"
  name = "modis_vi250m20010712.250m_16_days_EVI"
  nsteps = 255

gives:

  msg = "r.stats: cats for modis_vi250m20010712.250m_16_days_EVI are either missing or have no explicit labels. Using nsteps=255"

The backtrace says:

        msg = "r.stats: cats for modis_vi250m20010712.250m_16_days_EVI are either missing or have no explicit label"

which is 100 bytes. The remainder ("s. Using nsteps=255" and the
terminating NUL) is beyond the array, and the last 8 bytes
("eps=255\0") are overwriting option.cell.

I suggest getting rid of all of the sprintf() calls and just using
G_fatal_error() (which has a 2000 byte buffer).

--
Glynn Clements <glynn@gclements.plus.com>