| 1 |
|
|---|
| 2 | /****************************************************************************
|
|---|
| 3 | *
|
|---|
| 4 | * MODULE: r.covar
|
|---|
| 5 | *
|
|---|
| 6 | * AUTHOR(S): Michael Shapiro - CERL
|
|---|
| 7 | *
|
|---|
| 8 | * PURPOSE: Outputs a covariance/correlation matrix for
|
|---|
| 9 | * user-specified raster map layer(s).
|
|---|
| 10 | *
|
|---|
| 11 | * COPYRIGHT: (C) 2006 by the GRASS Development Team
|
|---|
| 12 | *
|
|---|
| 13 | * This program is free software under the GNU General Public
|
|---|
| 14 | * License (>=v2). Read the file COPYING that comes with GRASS
|
|---|
| 15 | * for details.
|
|---|
| 16 | *
|
|---|
| 17 | ***************************************************************************/
|
|---|
| 18 |
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 | #include <string.h>
|
|---|
| 22 | #include <math.h>
|
|---|
| 23 | #include <grass/gis.h>
|
|---|
| 24 | #include <grass/raster.h>
|
|---|
| 25 | #include <grass/glocale.h>
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | int main(int argc, char *argv[])
|
|---|
| 29 | {
|
|---|
| 30 | int nrows, ncols;
|
|---|
| 31 | DCELL **dcell;
|
|---|
| 32 | char *name;
|
|---|
| 33 | double *sum, **sum2;
|
|---|
| 34 | double count;
|
|---|
| 35 | double ii, jj;
|
|---|
| 36 | int *fd;
|
|---|
| 37 | int nfiles;
|
|---|
| 38 | int i, j;
|
|---|
| 39 | int row, col;
|
|---|
| 40 | int correlation;
|
|---|
| 41 | struct GModule *module;
|
|---|
| 42 | struct Option *maps;
|
|---|
| 43 | struct
|
|---|
| 44 | {
|
|---|
| 45 | struct Flag *r;
|
|---|
| 46 | } flag;
|
|---|
| 47 |
|
|---|
| 48 | G_gisinit(argv[0]);
|
|---|
| 49 |
|
|---|
| 50 | module = G_define_module();
|
|---|
| 51 | G_add_keyword(_("raster"));
|
|---|
| 52 | G_add_keyword(_("statistics"));
|
|---|
| 53 | module->description =
|
|---|
| 54 | _("Outputs a covariance/correlation matrix "
|
|---|
| 55 | "for user-specified raster map layer(s).");
|
|---|
| 56 |
|
|---|
| 57 | maps = G_define_standard_option(G_OPT_R_MAPS);
|
|---|
| 58 |
|
|---|
| 59 | flag.r = G_define_flag();
|
|---|
| 60 | flag.r->key = 'r';
|
|---|
| 61 | flag.r->description = _("Print correlation matrix");
|
|---|
| 62 |
|
|---|
| 63 | if (G_parser(argc, argv))
|
|---|
| 64 | exit(EXIT_FAILURE);
|
|---|
| 65 |
|
|---|
| 66 | /* flags */
|
|---|
| 67 | correlation = flag.r->answer;
|
|---|
| 68 |
|
|---|
| 69 | /* count the number of raster maps */
|
|---|
| 70 | for (nfiles = 0; maps->answers[nfiles]; nfiles++) ;
|
|---|
| 71 |
|
|---|
| 72 | fd = (int *)G_malloc(nfiles * sizeof(int));
|
|---|
| 73 | dcell = (DCELL **) G_malloc(nfiles * sizeof(DCELL *));
|
|---|
| 74 | sum = (double *)G_calloc(nfiles, sizeof(double));
|
|---|
| 75 | sum2 = (double **)G_malloc(nfiles * sizeof(double *));
|
|---|
| 76 | for (i = 0; i < nfiles; i++) {
|
|---|
| 77 | sum2[i] = (double *)G_calloc(nfiles, sizeof(double));
|
|---|
| 78 | dcell[i] = Rast_allocate_d_buf();
|
|---|
| 79 | name = maps->answers[i];
|
|---|
| 80 | fd[i] = Rast_open_old(name, "");
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | nrows = Rast_window_rows();
|
|---|
| 84 | ncols = Rast_window_cols();
|
|---|
| 85 |
|
|---|
| 86 | G_message(_("%s: complete ... "), G_program_name());
|
|---|
| 87 | count = 0;
|
|---|
| 88 | for (row = 0; row < nrows; row++) {
|
|---|
| 89 | G_percent(row, nrows, 2);
|
|---|
| 90 | for (i = 0; i < nfiles; i++)
|
|---|
| 91 | Rast_get_d_row(fd[i], dcell[i], row);
|
|---|
| 92 |
|
|---|
| 93 | for (col = 0; col < ncols; col++) {
|
|---|
| 94 | /* ignore cells where any of the maps has null value */
|
|---|
| 95 | for (i = 0; i < nfiles; i++)
|
|---|
| 96 | if (Rast_is_d_null_value(&dcell[i][col]))
|
|---|
| 97 | break;
|
|---|
| 98 | if (i != nfiles)
|
|---|
| 99 | continue;
|
|---|
| 100 | count++;
|
|---|
| 101 | for (i = 0; i < nfiles; i++) {
|
|---|
| 102 | sum[i] += dcell[i][col];
|
|---|
| 103 | for (j = 0; j <= i; j++)
|
|---|
| 104 | sum2[j][i] += dcell[i][col] * dcell[j][col];
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | G_percent(row, nrows, 2);
|
|---|
| 109 | if (count <= 1.1)
|
|---|
| 110 | G_fatal_error(_("No non-null values"));
|
|---|
| 111 |
|
|---|
| 112 | fprintf(stdout, "N = %.0f\n", count);
|
|---|
| 113 |
|
|---|
| 114 | ii = jj = 1.0;
|
|---|
| 115 | for (i = 0; i < nfiles; i++) {
|
|---|
| 116 | if (correlation)
|
|---|
| 117 | ii = sqrt((sum2[i][i] - sum[i] * sum[i] / count) / (count - 1));
|
|---|
| 118 | for (j = 0; j <= i; j++) {
|
|---|
| 119 | if (correlation)
|
|---|
| 120 | jj = sqrt((sum2[j][j] - sum[j] * sum[j] / count) / (count -
|
|---|
| 121 | 1));
|
|---|
| 122 | fprintf(stdout, "%f ",
|
|---|
| 123 | (sum2[j][i] -
|
|---|
| 124 | sum[i] * sum[j] / count) / (ii * jj * (count - 1)));
|
|---|
| 125 | }
|
|---|
| 126 | for (j = i + 1; j < nfiles; j++) {
|
|---|
| 127 | if (correlation)
|
|---|
| 128 | jj = sqrt((sum2[j][j] - sum[j] * sum[j] / count) / (count -
|
|---|
| 129 | 1));
|
|---|
| 130 | fprintf(stdout, "%f ",
|
|---|
| 131 | (sum2[i][j] -
|
|---|
| 132 | sum[i] * sum[j] / count) / (ii * jj * (count - 1)));
|
|---|
| 133 | }
|
|---|
| 134 | fprintf(stdout, "\n");
|
|---|
| 135 | }
|
|---|
| 136 | exit(EXIT_SUCCESS);
|
|---|
| 137 | }
|
|---|