main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /****************************************************************************
  2. *
  3. * MODULE: r.usler
  4. * AUTHOR(S): Natalia Medvedeva - natmead@gmail.com
  5. * Yann Chemin - yann.chemin@gmail.com
  6. * PURPOSE: Calculates USLE R factor
  7. * Rainfall Erosion index according to four methods
  8. *
  9. * COPYRIGHT: (C) 2002-2008, 2010 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/gis.h>
  20. #include <grass/raster.h>
  21. #include <grass/glocale.h>
  22. double elswaify_1985(double annual_pmm);
  23. double morgan_1974(double annual_pmm);
  24. double foster_1981(double annual_pmm);
  25. double roose_1975(double annual_pmm);
  26. int main(int argc, char *argv[])
  27. {
  28. int nrows, ncols;
  29. int row, col;
  30. char *nameflag; /*Switch for particular method */
  31. struct GModule *module;
  32. struct Option *input1, *input2, *output;
  33. struct History history; /*metadata */
  34. char *desc;
  35. /************************************/
  36. char *result; /*output raster name */
  37. int infd_annual_pmm;
  38. int outfd;
  39. char *annual_pmm;
  40. void *inrast_annual_pmm;
  41. DCELL * outrast;
  42. /************************************/
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("raster"));
  46. G_add_keyword(_("hydrology"));
  47. G_add_keyword(_("rainfall"));
  48. G_add_keyword(_("soil"));
  49. G_add_keyword(_("erosion"));
  50. module->description = _("Computes USLE R factor, Rainfall erosivity index.");
  51. input2 = G_define_standard_option(G_OPT_R_INPUT);
  52. input2->description = _("Name of annual precipitation raster map [mm/year]");
  53. output = G_define_standard_option(G_OPT_R_OUTPUT);
  54. output->description = _("Name for output USLE R raster map [MJ.mm/ha.hr.year]");
  55. /* Define the different options */
  56. input1 = G_define_option();
  57. input1->key = "method";
  58. input1->type = TYPE_STRING;
  59. input1->required = YES;
  60. input1->description = _("Name of USLE R equation");
  61. input1->options = "roose, morgan, foster, elswaify";
  62. desc = NULL;
  63. G_asprintf(&desc,
  64. "roose;%s;morgan;%s;foster;%s;elswaify;%s",
  65. _("Roosle (1975)"),
  66. _("Morgan (1974)"),
  67. _("Foster (1981)"),
  68. _("El-Swaify (1985)"));
  69. input1->descriptions = desc;
  70. input1->answer = "morgan";
  71. /********************/
  72. if (G_parser(argc, argv))
  73. exit(EXIT_FAILURE);
  74. nameflag = input1->answer;
  75. annual_pmm = input2->answer;
  76. result = output->answer;
  77. /***************************************************/
  78. infd_annual_pmm = Rast_open_old(annual_pmm, "");
  79. inrast_annual_pmm = Rast_allocate_d_buf();
  80. /***************************************************/
  81. nrows = Rast_window_rows();
  82. ncols = Rast_window_cols();
  83. outrast = Rast_allocate_d_buf();
  84. /* Create New raster files */
  85. outfd = Rast_open_new(result, DCELL_TYPE);
  86. /* Process pixels */
  87. for (row = 0; row < nrows; row++)
  88. {
  89. DCELL d;
  90. DCELL d_annual_pmm;
  91. G_percent(row, nrows, 2);
  92. /* read input map */
  93. Rast_get_d_row(infd_annual_pmm, inrast_annual_pmm, row);
  94. /*process the data */
  95. for (col = 0; col < ncols; col++)
  96. {
  97. d_annual_pmm = ((DCELL *) inrast_annual_pmm)[col];
  98. if (Rast_is_d_null_value(&d_annual_pmm))
  99. Rast_set_d_null_value(&outrast[col], 1);
  100. else
  101. {
  102. /*calculate morgan */
  103. if (!strcmp(nameflag, "morgan"))
  104. d = morgan_1974(d_annual_pmm);
  105. /*calculate roose */
  106. if (!strcmp(nameflag, "roose"))
  107. d = roose_1975(d_annual_pmm);
  108. /*calculate foster */
  109. if (!strcmp(nameflag, "foster"))
  110. d = foster_1981(d_annual_pmm);
  111. /*calculate elswaify */
  112. if (!strcmp(nameflag, "elswaify"))
  113. d = elswaify_1985(d_annual_pmm);
  114. outrast[col] = d ;
  115. }
  116. }
  117. Rast_put_d_row(outfd, outrast);
  118. }
  119. G_free(inrast_annual_pmm);
  120. Rast_close(infd_annual_pmm);
  121. G_free(outrast);
  122. Rast_close(outfd);
  123. Rast_short_history(result, "raster", &history);
  124. Rast_command_history(&history);
  125. Rast_write_history(result, &history);
  126. exit(EXIT_SUCCESS);
  127. }