sg_factor.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "Gwater.h"
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. CELL *ril_buf;
  6. int sg_factor(void)
  7. {
  8. int r, c;
  9. CELL low_elev, hih_elev;
  10. double height, length, S, sin_theta;
  11. G_message(_("SECTION 4: RUSLE LS and/or S factor determination."));
  12. if (ril_flag)
  13. ril_buf = Rast_allocate_c_buf();
  14. for (r = 0; r < nrows; r++) {
  15. G_percent(r, nrows, 3);
  16. if (ril_flag) {
  17. Rast_get_c_row(ril_fd, ril_buf, r);
  18. }
  19. for (c = 0; c < ncols; c++) {
  20. low_elev = alt[SEG_INDEX(alt_seg, r, c)];
  21. hih_elev = r_h[SEG_INDEX(r_h_seg, r, c)];
  22. length = s_l[SEG_INDEX(s_l_seg, r, c)];
  23. height = 1.0 * (hih_elev - low_elev) / ele_scale;
  24. if (length > max_length) {
  25. height *= max_length / length;
  26. length = max_length;
  27. }
  28. sin_theta = height / sqrt(height * height + length * length);
  29. if (height / length < .09)
  30. S = 10.8 * sin_theta + .03;
  31. else
  32. S = 16.8 * sin_theta - .50;
  33. if (sg_flag)
  34. s_g[SEG_INDEX(s_g_seg, r, c)] = S;
  35. if (ls_flag) {
  36. length *= METER_TO_FOOT;
  37. len_slp_equ(length, sin_theta, S, r, c);
  38. }
  39. }
  40. }
  41. G_percent(nrows, nrows, 1); /* finish it */
  42. if (ril_flag) {
  43. G_free(ril_buf);
  44. Rast_close(ril_fd);
  45. }
  46. return 0;
  47. }
  48. int len_slp_equ(double slope_length, double sin_theta, double S, int r, int c)
  49. {
  50. double ril, s_l_exp, /* m */
  51. rill_ratio, /* Beta */
  52. L;
  53. rill_ratio = (sin_theta / 0.0896) / (3.0 * pow(sin_theta, 0.8) + 0.56);
  54. if (ril_flag) {
  55. ril = ril_buf[c];
  56. }
  57. else if (ril_value >= 0.0) {
  58. ril = ril_value;
  59. }
  60. else
  61. ril = 0.0;
  62. /* rill_ratio equation from Steve Warren */
  63. rill_ratio *= .5 + .005 * ril + .0001 * ril * ril;
  64. s_l_exp = rill_ratio / (1 + rill_ratio);
  65. L = pow((slope_length / 72.6), s_l_exp);
  66. l_s[SEG_INDEX(l_s_seg, r, c)] = L * S;
  67. return 0;
  68. }