main.c 3.5 KB

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