Debian Codegraph @

imagery/i.eb.soilheatflux/main.c

raw
1 /****************************************************************************
2 *
3 * MODULE: i.eb.soilheatflux
4 * AUTHOR(S): Yann Chemin - [email protected]
5 * PURPOSE: Calculates an approximation of soil heat flux
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
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <grass/gis.h>
21 #include <grass/raster.h>
22 #include <grass/glocale.h>
23
24 double g_0(double bbalb, double ndvi, double tempk, double rnet, double time,
25 int roerink);
26
27 int main(int argc, char *argv[])
28 {
29 int nrows, ncols;
30 int row, col;
31 int roerink = 0; /*Roerink Flag for HAPEX-Sahel conditions */
32 struct GModule *module;
33 struct Option *input1, *input2, *input3, *input4, *input5, *output1;
34 struct Flag *flag1;
35 struct History history; /*metadata */
36 struct Colors colors; /*metadata */
37 char *result; /*output raster name */
38 int infd_albedo, infd_ndvi, infd_tempk, infd_rnet, infd_time;
39 int outfd;
40 char *albedo, *ndvi, *tempk, *rnet, *time;
41 void *inrast_albedo, *inrast_ndvi, *inrast_tempk, *inrast_rnet,
42 *inrast_time;
43 DCELL *outrast;
44 CELL val1, val2;
45
46 G_gisinit(argv[0]);
47
48 module = G_define_module();
49 G_add_keyword(_("imagery"));
50 G_add_keyword(_("energy balance"));
51 G_add_keyword(_("soil heat flux"));
52 G_add_keyword(_("SEBAL"));
53 module->description =
54 _("Soil heat flux approximation (Bastiaanssen, 1995).");
55
56 /* Define the different options */
57 input1 = G_define_standard_option(G_OPT_R_INPUT);
58 input1->key = "albedo";
59 input1->description = _("Name of albedo raster map [0.0;1.0]");
60
61 input2 = G_define_standard_option(G_OPT_R_INPUT);
62 input2->key = "ndvi";
63 input2->description = _("Name of NDVI raster map [-1.0;+1.0]");
64
65 input3 = G_define_standard_option(G_OPT_R_INPUT);
66 input3->key = "temperature";
67 input3->description = _("Name of Surface temperature raster map [K]");
68
69 input4 = G_define_standard_option(G_OPT_R_INPUT);
70 input4->key = "netradiation";
71 input4->description = _("Name of Net Radiation raster map [W/m2]");
72
73 input5 = G_define_standard_option(G_OPT_R_INPUT);
74 input5->key = "localutctime";
75 input5->description =
76 _("Name of time of satellite overpass raster map [local time in UTC]");
77
78 output1 = G_define_standard_option(G_OPT_R_OUTPUT);
79
80 flag1 = G_define_flag();
81 flag1->key = 'r';
82 flag1->description = _("HAPEX-Sahel empirical correction (Roerink, 1995)");
83
84 if (G_parser(argc, argv))
85 exit(EXIT_FAILURE);
86
87 albedo = input1->answer;
88 ndvi = input2->answer;
89 tempk = input3->answer;
90 rnet = input4->answer;
91 time = input5->answer;
92 result = output1->answer;
93 roerink = flag1->answer;
94
95 infd_albedo = Rast_open_old(albedo, "");
96 inrast_albedo = Rast_allocate_d_buf();
97
98 infd_ndvi = Rast_open_old(ndvi, "");
99 inrast_ndvi = Rast_allocate_d_buf();
100
101 infd_tempk = Rast_open_old(tempk, "");
102 inrast_tempk = Rast_allocate_d_buf();
103
104 infd_rnet = Rast_open_old(rnet, "");
105 inrast_rnet = Rast_allocate_d_buf();
106
107 infd_time = Rast_open_old(time, "");
108 inrast_time = Rast_allocate_d_buf();
109
110 nrows = Rast_window_rows();
111 ncols = Rast_window_cols();
112 outrast = Rast_allocate_d_buf();
113
114 /* Create New raster files */
115 outfd = Rast_open_new(result, DCELL_TYPE);
116
117 /* Process pixels */
118 for (row = 0; row < nrows; row++) {
119 DCELL d;
120 DCELL d_albedo;
121 DCELL d_ndvi;
122 DCELL d_tempk;
123 DCELL d_rnet;
124 DCELL d_time;
125
126 G_percent(row, nrows, 2);
127 /* read input maps */
128 Rast_get_d_row(infd_albedo, inrast_albedo, row);
129 Rast_get_d_row(infd_ndvi, inrast_ndvi, row);
130 Rast_get_d_row(infd_tempk, inrast_tempk, row);
131 Rast_get_d_row(infd_rnet, inrast_rnet, row);
132 Rast_get_d_row(infd_time, inrast_time, row);
133 /*process the data */
134 for (col = 0; col < ncols; col++) {
135 d_albedo = ((DCELL *)inrast_albedo)[col];
136 d_ndvi = ((DCELL *)inrast_ndvi)[col];
137 d_tempk = ((DCELL *)inrast_tempk)[col];
138 d_rnet = ((DCELL *)inrast_rnet)[col];
139 d_time = ((DCELL *)inrast_time)[col];
140 if (Rast_is_d_null_value(&d_albedo) ||
141 Rast_is_d_null_value(&d_ndvi) ||
142 Rast_is_d_null_value(&d_tempk) ||
143 Rast_is_d_null_value(&d_rnet) ||
144 Rast_is_d_null_value(&d_time)) {
145 Rast_set_d_null_value(&outrast[col], 1);
146 }
147 else {
148 /* calculate soil heat flux */
149 d = g_0(d_albedo, d_ndvi, d_tempk, d_rnet, d_time, roerink);
150 outrast[col] = d;
151 }
152 }
153 Rast_put_d_row(outfd, outrast);
154 }
155 G_free(inrast_albedo);
156 G_free(inrast_ndvi);
157 G_free(inrast_tempk);
158 G_free(inrast_rnet);
159 G_free(inrast_time);
160 Rast_close(infd_albedo);
161 Rast_close(infd_ndvi);
162 Rast_close(infd_tempk);
163 Rast_close(infd_rnet);
164 Rast_close(infd_time);
165 G_free(outrast);
166 Rast_close(outfd);
167
168 /* Colors in grey shade */
169 Rast_init_colors(&colors);
170 val1 = 0;
171 val2 = 200;
172 Rast_add_c_color_rule(&val1, 0, 0, 0, &val2, 255, 255, 255, &colors);
173 Rast_short_history(result, "raster", &history);
174 Rast_command_history(&history);
175 Rast_write_history(result, &history);
176
177 exit(EXIT_SUCCESS);
178 }