main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************************************************
  2. *
  3. * MODULE: i.eb.netrad
  4. * AUTHOR(S): Yann Chemin - yann.chemin@gmail.com
  5. * PURPOSE: Calculates the instantaneous net radiation at
  6. * as seen in Bastiaanssen (1995) using time of
  7. * satellite overpass.
  8. *
  9. * COPYRIGHT: (C) 2006-2011 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 r_net(double bbalb, double ndvi, double tempk, double dtair,
  23. double e0, double tsw, double doy, double utc,
  24. double sunzangle);
  25. int main(int argc, char *argv[])
  26. {
  27. struct Cell_head cellhd; /*region+header info */
  28. char *mapset; /*mapset name */
  29. int nrows, ncols;
  30. int row, col;
  31. struct GModule *module;
  32. struct Option *input1, *input2, *input3, *input4, *input5;
  33. struct Option *input6, *input7, *input8, *input9, *output1;
  34. struct Flag *flag1;
  35. struct History history; /*metadata */
  36. struct Colors colors; /*Color rules */
  37. char *name; /*input raster name */
  38. char *result; /*output raster name */
  39. int infd_albedo, infd_ndvi, infd_tempk, infd_time, infd_dtair;
  40. int infd_emissivity, infd_tsw, infd_doy, infd_sunzangle;
  41. int outfd;
  42. char *albedo, *ndvi, *tempk, *time, *dtair, *emissivity;
  43. char *tsw, *doy, *sunzangle;
  44. int i = 0, j = 0;
  45. void *inrast_albedo, *inrast_ndvi, *inrast_tempk, *inrast_rnet;
  46. void *inrast_time, *inrast_dtair, *inrast_emissivity, *inrast_tsw;
  47. void *inrast_doy, *inrast_sunzangle;
  48. DCELL * outrast;
  49. CELL val1,val2; /*For color range*/
  50. G_gisinit(argv[0]);
  51. module = G_define_module();
  52. G_add_keyword(_("imagery"));
  53. G_add_keyword(_("energy balance"));
  54. G_add_keyword(_("net radiation"));
  55. G_add_keyword(_("SEBAL"));
  56. module->description =
  57. _("Net radiation approximation (Bastiaanssen, 1995).");
  58. /* Define the different options */
  59. input1 = G_define_standard_option(G_OPT_R_INPUT);
  60. input1->key = "albedo";
  61. input1->description = _("Name of albedo raster map [0.0;1.0]");
  62. input2 = G_define_standard_option(G_OPT_R_INPUT);
  63. input2->key = "ndvi";
  64. input2->description = _("Name of NDVI raster map [-1.0;+1.0]");
  65. input3 = G_define_standard_option(G_OPT_R_INPUT);
  66. input3->key = "temperature";
  67. input3->description =
  68. _("Name of surface temperature raster map [K]");
  69. input4 = G_define_standard_option(G_OPT_R_INPUT);
  70. input4->key = "localutctime";
  71. input4->description =
  72. _("Name of time of satellite overpass raster map [local time in UTC]");
  73. input5 = G_define_standard_option(G_OPT_R_INPUT);
  74. input5->key = "temperaturedifference2m";
  75. input5->description =
  76. _("Name of the difference map of temperature from surface skin to about 2 m height [K]");
  77. input6 = G_define_standard_option(G_OPT_R_INPUT);
  78. input6->key = "emissivity";
  79. input6->description = _("Name of the emissivity map [-]");
  80. input7 = G_define_standard_option(G_OPT_R_INPUT);
  81. input7->key = "transmissivity_singleway";
  82. input7->description =
  83. _("Name of the single-way atmospheric transmissivitymap [-]");
  84. input8 = G_define_standard_option(G_OPT_R_INPUT);
  85. input8->key = "dayofyear";
  86. input8->description = _("Name of the Day Of Year (DOY) map [-]");
  87. input9 = G_define_standard_option(G_OPT_R_INPUT);
  88. input9->key = "sunzenithangle";
  89. input9->description = _("Name of the sun zenith angle map [degrees]");
  90. output1 = G_define_standard_option(G_OPT_R_OUTPUT);
  91. output1->description = _("Name of the output net radiation layer");
  92. /********************/
  93. if (G_parser(argc, argv))
  94. exit(EXIT_FAILURE);
  95. albedo = input1->answer;
  96. ndvi = input2->answer;
  97. tempk = input3->answer;
  98. time = input4->answer;
  99. dtair = input5->answer;
  100. emissivity = input6->answer;
  101. tsw = input7->answer;
  102. doy = input8->answer;
  103. sunzangle = input9->answer;
  104. result = output1->answer;
  105. /* Open access to image files and allocate row access memory */
  106. infd_albedo = Rast_open_old(albedo, "");
  107. inrast_albedo = Rast_allocate_d_buf();
  108. infd_ndvi = Rast_open_old(ndvi, "");
  109. inrast_ndvi = Rast_allocate_d_buf();
  110. infd_tempk = Rast_open_old(tempk, "");
  111. inrast_tempk = Rast_allocate_d_buf();
  112. infd_dtair = Rast_open_old(dtair, "");
  113. inrast_dtair = Rast_allocate_d_buf();
  114. infd_time = Rast_open_old(time, "");
  115. inrast_time = Rast_allocate_d_buf();
  116. infd_emissivity = Rast_open_old(emissivity, "");
  117. inrast_emissivity = Rast_allocate_d_buf();
  118. infd_tsw = Rast_open_old(tsw, "");
  119. inrast_tsw = Rast_allocate_d_buf();
  120. infd_doy = Rast_open_old(doy, "");
  121. inrast_doy = Rast_allocate_d_buf();
  122. infd_sunzangle = Rast_open_old(sunzangle, "");
  123. inrast_sunzangle = Rast_allocate_d_buf();
  124. nrows = Rast_window_rows();
  125. ncols = Rast_window_cols();
  126. outfd = Rast_open_new(result, DCELL_TYPE);
  127. outrast = Rast_allocate_d_buf();
  128. /* Process pixels */
  129. for (row = 0; row < nrows; row++)
  130. {
  131. DCELL d;
  132. DCELL d_albedo;
  133. DCELL d_ndvi;
  134. DCELL d_tempk;
  135. DCELL d_dtair;
  136. DCELL d_time;
  137. DCELL d_emissivity;
  138. DCELL d_tsw;
  139. DCELL d_doy;
  140. DCELL d_sunzangle;
  141. /* Display row process percentage */
  142. G_percent(row, nrows, 2);
  143. /* Load rows for each input image */
  144. Rast_get_d_row(infd_albedo, inrast_albedo, row);
  145. Rast_get_d_row(infd_ndvi, inrast_ndvi, row);
  146. Rast_get_d_row(infd_tempk, inrast_tempk, row);
  147. Rast_get_d_row(infd_dtair, inrast_dtair, row);
  148. Rast_get_d_row(infd_time, inrast_time, row);
  149. Rast_get_d_row(infd_emissivity, inrast_emissivity, row);
  150. Rast_get_d_row(infd_tsw, inrast_tsw, row);
  151. Rast_get_d_row(infd_doy, inrast_doy, row);
  152. Rast_get_d_row(infd_sunzangle, inrast_sunzangle, row);
  153. /*process the data */
  154. for (col = 0; col < ncols; col++)
  155. {
  156. d_albedo = (double)((DCELL *) inrast_albedo)[col];
  157. d_ndvi = (double)((DCELL *) inrast_ndvi)[col];
  158. d_tempk = (double)((DCELL *) inrast_tempk)[col];
  159. d_dtair = (double)((DCELL *) inrast_dtair)[col];
  160. d_time = (double)((DCELL *) inrast_time)[col];
  161. d_emissivity = (double)((DCELL *) inrast_emissivity)[col];
  162. d_tsw = (double)((DCELL *) inrast_tsw)[col];
  163. d_doy = (double)((DCELL *) inrast_doy)[col];
  164. d_sunzangle = (double)((DCELL *) inrast_sunzangle)[col];
  165. /* process NULL Values */
  166. if (Rast_is_d_null_value(&d_albedo) ||
  167. Rast_is_d_null_value(&d_ndvi) ||
  168. Rast_is_d_null_value(&d_tempk) ||
  169. Rast_is_d_null_value(&d_dtair) ||
  170. Rast_is_d_null_value(&d_time) ||
  171. Rast_is_d_null_value(&d_emissivity) ||
  172. Rast_is_d_null_value(&d_tsw) ||
  173. Rast_is_d_null_value(&d_doy) ||
  174. Rast_is_d_null_value(&d_sunzangle)) {
  175. Rast_set_d_null_value(&outrast[col], 1);
  176. }
  177. else {
  178. /************************************/
  179. /* calculate the net radiation */
  180. d = r_net(d_albedo, d_ndvi, d_tempk, d_dtair, d_emissivity, d_tsw, d_doy, d_time, d_sunzangle);
  181. outrast[col] = d;
  182. }
  183. }
  184. Rast_put_d_row(outfd, outrast);
  185. }
  186. G_free(inrast_albedo);
  187. G_free(inrast_ndvi);
  188. G_free(inrast_tempk);
  189. G_free(inrast_dtair);
  190. G_free(inrast_time);
  191. G_free(inrast_emissivity);
  192. G_free(inrast_tsw);
  193. G_free(inrast_doy);
  194. G_free(inrast_sunzangle);
  195. Rast_close(infd_albedo);
  196. Rast_close(infd_ndvi);
  197. Rast_close(infd_tempk);
  198. Rast_close(infd_dtair);
  199. Rast_close(infd_time);
  200. Rast_close(infd_emissivity);
  201. Rast_close(infd_tsw);
  202. Rast_close(infd_doy);
  203. Rast_close(infd_sunzangle);
  204. G_free(outrast);
  205. Rast_close(outfd);
  206. /* Colors in grey shade */
  207. Rast_init_colors(&colors);
  208. val1=0;
  209. val2=900;
  210. Rast_add_c_color_rule(&val1, 0, 0, 0, &val2, 255, 255, 255, &colors);
  211. /* Metadata */
  212. Rast_short_history(result, "raster", &history);
  213. Rast_command_history(&history);
  214. Rast_write_history(result, &history);
  215. exit(EXIT_SUCCESS);
  216. }