main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /****************************************************************************
  2. *
  3. * MODULE: i.emissivity
  4. * AUTHOR(S): Yann Chemin - yann.chemin@gmail.com
  5. * PURPOSE: Calculates the emissivity from NDVI (empirical)
  6. * as seen in Caselles and Colles (1997).
  7. *
  8. * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. *****************************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. double emissivity_generic(double ndvi);
  22. int main(int argc, char *argv[])
  23. {
  24. int nrows, ncols;
  25. int row, col;
  26. struct GModule *module;
  27. struct Option *input, *output;
  28. struct History history; /*metadata */
  29. /************************************/
  30. char *result1; /*output raster name */
  31. int infd, outfd; /*File Descriptors */
  32. char *ndvi;
  33. char *emissivity;
  34. void *inr;
  35. DCELL * outr;
  36. /************************************/
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("imagery"));
  40. G_add_keyword(_("emissivity"));
  41. G_add_keyword(_("land flux"));
  42. G_add_keyword(_("energy balance"));
  43. module->description =
  44. _("Computes emissivity from NDVI, generic method for sparse land.");
  45. /* Define the different options */
  46. input = G_define_standard_option(G_OPT_R_INPUT);
  47. input->description = _("Name of the NDVI map [-]");
  48. output = G_define_standard_option(G_OPT_R_OUTPUT);
  49. output->description = _("Name of the output emissivity layer");
  50. /********************/
  51. if (G_parser(argc, argv))
  52. exit(EXIT_FAILURE);
  53. ndvi = input->answer;
  54. result1 = output->answer;
  55. /***************************************************/
  56. infd = Rast_open_old(ndvi, "");
  57. inr = Rast_allocate_d_buf();
  58. /***************************************************/
  59. nrows = Rast_window_rows();
  60. ncols = Rast_window_cols();
  61. outr = Rast_allocate_d_buf();
  62. /* Create New raster files */
  63. outfd = Rast_open_new(result1, DCELL_TYPE);
  64. /* Process pixels */
  65. for (row = 0; row < nrows; row++)
  66. {
  67. DCELL d;
  68. DCELL d_ndvi;
  69. G_percent(row, nrows, 2);
  70. /* read input maps */
  71. Rast_get_row(infd,inr,row,DCELL_TYPE);
  72. /*process the data */
  73. for (col = 0; col < ncols; col++)
  74. {
  75. d_ndvi = ((DCELL *) inr)[col];
  76. if (Rast_is_d_null_value(&d_ndvi))
  77. Rast_set_d_null_value(&outr[col], 1);
  78. else {
  79. /****************************/
  80. /* calculate emissivity */
  81. d = emissivity_generic(d_ndvi);
  82. outr[col] = d;
  83. }
  84. }
  85. Rast_put_row(outfd, outr, DCELL_TYPE);
  86. }
  87. G_free(inr);
  88. Rast_close(infd);
  89. G_free(outr);
  90. Rast_close(outfd);
  91. Rast_short_history(result1, "raster", &history);
  92. Rast_command_history(&history);
  93. Rast_write_history(result1, &history);
  94. exit(EXIT_SUCCESS);
  95. }