| 1 |
/***************************************************************************** |
| 2 |
* |
| 3 |
* MODULE: i.evapo.mh |
| 4 |
* AUTHOR: Yann Chemin [email protected] |
| 5 |
* |
| 6 |
* PURPOSE: To estimate the reference evapotranspiration by means |
| 7 |
* of Modified Hargreaves method (2001). |
| 8 |
* Also has a switch for original Hargreaves (1985), |
| 9 |
* and for Hargreaves-Samani (1985). |
| 10 |
* |
| 11 |
* COPYRIGHT: (C) 2007-2011 by the GRASS Development Team |
| 12 |
* |
| 13 |
* This program is free software under the GNU General Public |
| 14 |
* Licence (>=2). Read the file COPYING that comes with GRASS |
| 15 |
* for details. |
| 16 |
* |
| 17 |
***************************************************************************/ |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <math.h> |
| 23 |
#include <grass/gis.h> |
| 24 |
#include <grass/raster.h> |
| 25 |
#include <grass/glocale.h> |
| 26 |
|
| 27 |
double mh_original(double ra, double tavg, double tmax, double tmin, double p); |
| 28 |
double mh_eto(double ra, double tavg, double tmax, double tmin, double p); |
| 29 |
double mh_samani(double ra, double tavg, double tmax, double tmin); |
| 30 |
|
| 31 |
int main(int argc, char *argv[]) |
| 32 |
{ |
| 33 |
/* buffer for input-output rasters */ |
| 34 |
void *inrast_TEMPKAVG, *inrast_TEMPKMIN, *inrast_TEMPKMAX, *inrast_RNET, |
| 35 |
*inrast_P; |
| 36 |
DCELL *outrast; |
| 37 |
|
| 38 |
/* pointers to input-output raster files */ |
| 39 |
int infd_TEMPKAVG, infd_TEMPKMIN, infd_TEMPKMAX, infd_RNET, infd_P; |
| 40 |
int outfd; |
| 41 |
|
| 42 |
/* names of input-output raster files */ |
| 43 |
char *RNET, *TEMPKAVG, *TEMPKMIN, *TEMPKMAX, *P; |
| 44 |
char *ETa; |
| 45 |
|
| 46 |
/* input-output cell values */ |
| 47 |
DCELL d_tempkavg, d_tempkmin, d_tempkmax, d_rnet, d_p; |
| 48 |
DCELL d_daily_et; |
| 49 |
|
| 50 |
/* region information and handler */ |
| 51 |
struct Cell_head cellhd; |
| 52 |
int nrows, ncols; |
| 53 |
int row, col; |
| 54 |
|
| 55 |
/* parser structures definition */ |
| 56 |
struct GModule *module; |
| 57 |
struct Option *input_RNET, *input_TEMPKAVG, *input_TEMPKMIN; |
| 58 |
struct Option *input_TEMPKMAX, *input_P; |
| 59 |
struct Option *output; |
| 60 |
struct Flag *zero, *original, *samani; |
| 61 |
struct Colors color; |
| 62 |
struct History history; |
| 63 |
|
| 64 |
/* Initialize the GIS calls */ |
| 65 |
G_gisinit(argv[0]); |
| 66 |
|
| 67 |
module = G_define_module(); |
| 68 |
G_add_keyword(_("imagery")); |
| 69 |
G_add_keyword(_("evapotranspiration")); |
| 70 |
module->description = |
| 71 |
_("Computes evapotranspiration calculation " |
| 72 |
"modified or original Hargreaves formulation, 2001."); |
| 73 |
|
| 74 |
/* Define different options */ |
| 75 |
input_RNET = G_define_standard_option(G_OPT_R_INPUT); |
| 76 |
input_RNET->key = "netradiation_diurnal"; |
| 77 |
input_RNET->description = |
| 78 |
_("Name of input diurnal net radiation raster map [W/m2/d]"); |
| 79 |
|
| 80 |
input_TEMPKAVG = G_define_standard_option(G_OPT_R_INPUT); |
| 81 |
input_TEMPKAVG->key = "average_temperature"; |
| 82 |
input_TEMPKAVG->description = |
| 83 |
_("Name of input average air temperature raster map [C]"); |
| 84 |
|
| 85 |
input_TEMPKMIN = G_define_standard_option(G_OPT_R_INPUT); |
| 86 |
input_TEMPKMIN->key = "minimum_temperature"; |
| 87 |
input_TEMPKMIN->description = |
| 88 |
_("Name of input minimum air temperature raster map [C]"); |
| 89 |
|
| 90 |
input_TEMPKMAX = G_define_standard_option(G_OPT_R_INPUT); |
| 91 |
input_TEMPKMAX->key = "maximum_temperature"; |
| 92 |
input_TEMPKMAX->description = |
| 93 |
_("Name of input maximum air temperature raster map [C]"); |
| 94 |
|
| 95 |
input_P = G_define_standard_option(G_OPT_R_INPUT); |
| 96 |
input_P->required = NO; |
| 97 |
input_P->key = "precipitation"; |
| 98 |
input_P->label = _("Name of precipitation raster map [mm/month]"); |
| 99 |
input_P->description = _("Disabled for original Hargreaves (1985)"); |
| 100 |
|
| 101 |
output = G_define_standard_option(G_OPT_R_OUTPUT); |
| 102 |
output->description = _("Name for output raster map [mm/d]"); |
| 103 |
|
| 104 |
/* Define the different flags */ |
| 105 |
zero = G_define_flag(); |
| 106 |
zero->key = 'z'; |
| 107 |
zero->description = _("Set negative ETa to zero"); |
| 108 |
|
| 109 |
original = G_define_flag(); |
| 110 |
original->key = 'h'; |
| 111 |
original->description = _("Use original Hargreaves (1985)"); |
| 112 |
|
| 113 |
samani = G_define_flag(); |
| 114 |
samani->key = 's'; |
| 115 |
samani->description = _("Use Hargreaves-Samani (1985)"); |
| 116 |
|
| 117 |
if (G_parser(argc, argv)) |
| 118 |
exit(EXIT_FAILURE); |
| 119 |
|
| 120 |
/* get entered parameters */ |
| 121 |
RNET = input_RNET->answer; |
| 122 |
TEMPKAVG = input_TEMPKAVG->answer; |
| 123 |
TEMPKMIN = input_TEMPKMIN->answer; |
| 124 |
TEMPKMAX = input_TEMPKMAX->answer; |
| 125 |
P = input_P->answer; |
| 126 |
|
| 127 |
ETa = output->answer; |
| 128 |
|
| 129 |
/* open pointers to input raster files */ |
| 130 |
infd_RNET = Rast_open_old(RNET, ""); |
| 131 |
infd_TEMPKAVG = Rast_open_old(TEMPKAVG, ""); |
| 132 |
infd_TEMPKMIN = Rast_open_old(TEMPKMIN, ""); |
| 133 |
infd_TEMPKMAX = Rast_open_old(TEMPKMAX, ""); |
| 134 |
if (!original->answer) { |
| 135 |
infd_P = Rast_open_old(P, ""); |
| 136 |
} |
| 137 |
/* read headers of raster files */ |
| 138 |
Rast_get_cellhd(RNET, "", &cellhd); |
| 139 |
Rast_get_cellhd(TEMPKAVG, "", &cellhd); |
| 140 |
Rast_get_cellhd(TEMPKMIN, "", &cellhd); |
| 141 |
Rast_get_cellhd(TEMPKMAX, "", &cellhd); |
| 142 |
if (!original->answer) { |
| 143 |
Rast_get_cellhd(P, "", &cellhd); |
| 144 |
} |
| 145 |
/* Allocate input buffer */ |
| 146 |
inrast_RNET = Rast_allocate_d_buf(); |
| 147 |
inrast_TEMPKAVG = Rast_allocate_d_buf(); |
| 148 |
inrast_TEMPKMIN = Rast_allocate_d_buf(); |
| 149 |
inrast_TEMPKMAX = Rast_allocate_d_buf(); |
| 150 |
if (!original->answer) { |
| 151 |
inrast_P = Rast_allocate_d_buf(); |
| 152 |
} |
| 153 |
/* get rows and columns number of the current region */ |
| 154 |
nrows = Rast_window_rows(); |
| 155 |
ncols = Rast_window_cols(); |
| 156 |
|
| 157 |
/* allocate output buffer */ |
| 158 |
outrast = Rast_allocate_d_buf(); |
| 159 |
|
| 160 |
/* open pointers to output raster files */ |
| 161 |
outfd = Rast_open_new(ETa, DCELL_TYPE); |
| 162 |
|
| 163 |
/* start the loop through cells */ |
| 164 |
for (row = 0; row < nrows; row++) { |
| 165 |
G_percent(row, nrows, 2); |
| 166 |
/* read input raster row into line buffer */ |
| 167 |
Rast_get_d_row(infd_RNET, inrast_RNET, row); |
| 168 |
Rast_get_d_row(infd_TEMPKAVG, inrast_TEMPKAVG, row); |
| 169 |
Rast_get_d_row(infd_TEMPKMIN, inrast_TEMPKMIN, row); |
| 170 |
Rast_get_d_row(infd_TEMPKMAX, inrast_TEMPKMAX, row); |
| 171 |
if (!original->answer) { |
| 172 |
Rast_get_d_row(infd_P, inrast_P, row); |
| 173 |
} |
| 174 |
for (col = 0; col < ncols; col++) { |
| 175 |
/* read current cell from line buffer */ |
| 176 |
d_rnet = ((DCELL *)inrast_RNET)[col]; |
| 177 |
d_tempkavg = ((DCELL *)inrast_TEMPKAVG)[col]; |
| 178 |
d_tempkmin = ((DCELL *)inrast_TEMPKMIN)[col]; |
| 179 |
d_tempkmax = ((DCELL *)inrast_TEMPKMAX)[col]; |
| 180 |
if (!original->answer) { |
| 181 |
d_p = ((DCELL *)inrast_P)[col]; |
| 182 |
} |
| 183 |
if (Rast_is_d_null_value(&d_rnet) || |
| 184 |
Rast_is_d_null_value(&d_tempkavg) || |
| 185 |
Rast_is_d_null_value(&d_tempkmin) || |
| 186 |
Rast_is_d_null_value(&d_tempkmax) || |
| 187 |
Rast_is_d_null_value(&d_p)) { |
| 188 |
Rast_set_d_null_value(&outrast[col], 1); |
| 189 |
} |
| 190 |
else { |
| 191 |
if (original->answer) { |
| 192 |
d_daily_et = mh_original(d_rnet, d_tempkavg, d_tempkmax, |
| 193 |
d_tempkmin, d_p); |
| 194 |
} |
| 195 |
else if (samani->answer) { |
| 196 |
d_daily_et = |
| 197 |
mh_samani(d_rnet, d_tempkavg, d_tempkmax, d_tempkmin); |
| 198 |
} |
| 199 |
else { |
| 200 |
d_daily_et = |
| 201 |
mh_eto(d_rnet, d_tempkavg, d_tempkmax, d_tempkmin, d_p); |
| 202 |
} |
| 203 |
if (zero->answer && d_daily_et < 0) |
| 204 |
d_daily_et = 0.0; |
| 205 |
/* write calculated ETP to output line buffer */ |
| 206 |
outrast[col] = d_daily_et; |
| 207 |
} |
| 208 |
} |
| 209 |
/* write output line buffer to output raster file */ |
| 210 |
Rast_put_d_row(outfd, outrast); |
| 211 |
} |
| 212 |
/* free buffers and close input maps */ |
| 213 |
|
| 214 |
G_free(inrast_RNET); |
| 215 |
G_free(inrast_TEMPKAVG); |
| 216 |
G_free(inrast_TEMPKMIN); |
| 217 |
G_free(inrast_TEMPKMAX); |
| 218 |
if (!original->answer) { |
| 219 |
G_free(inrast_P); |
| 220 |
} |
| 221 |
Rast_close(infd_RNET); |
| 222 |
Rast_close(infd_TEMPKAVG); |
| 223 |
Rast_close(infd_TEMPKMIN); |
| 224 |
Rast_close(infd_TEMPKMAX); |
| 225 |
if (!original->answer) { |
| 226 |
Rast_close(infd_P); |
| 227 |
} |
| 228 |
/* generate color table between -20 and 20 */ |
| 229 |
Rast_make_rainbow_colors(&color, -20, 20); |
| 230 |
Rast_write_colors(ETa, G_mapset(), &color); |
| 231 |
|
| 232 |
Rast_short_history(ETa, "raster", &history); |
| 233 |
Rast_command_history(&history); |
| 234 |
Rast_write_history(ETa, &history); |
| 235 |
|
| 236 |
/* free buffers and close output map */ |
| 237 |
G_free(outrast); |
| 238 |
Rast_close(outfd); |
| 239 |
|
| 240 |
return (EXIT_SUCCESS); |
| 241 |
} |