19
19
#include <stdlib.h>
20
20
#include <string.h>
21
21
#include <grass/gis.h>
22
#include <grass/raster.h>
22
23
#include <grass/glocale.h>
24
25
#include "local_proto.h"
26
int full_open_old(Gfile * gf, char *fname)
30
snprintf(gf->name, 127, "%s", fname);
32
gf->mapset = G_find_cell2(gf->name, "");
33
if (gf->mapset == NULL)
34
G_fatal_error(_("Raster map <%s> not found"), gf->name);
36
gf->fd = G_open_cell_old(gf->name, gf->mapset);
38
G_fatal_error(_("Unable to open raster map <%s@%s>"), gf->name,
41
gf->type = G_raster_map_type(gf->name, gf->mapset);
46
int full_open_new(Gfile * gf, char *fname, RASTER_MAP_TYPE ftype)
50
snprintf(gf->name, 127, "%s", fname);
51
if (G_legal_filename(gf->name) < 0)
52
G_fatal_error(_("<%s> is an illegal name"), gf->name);
55
gf->mapset = G_mapset();
56
gf->fd = G_open_raster_new(gf->name, gf->type);
58
G_fatal_error(_("Unable to create raster map <%s>"), gf->name);
63
27
int main(int argc, char *argv[])
65
29
struct History history;
66
30
struct GModule *module;
67
31
struct Cell_head hd_band, hd_dem, window;
69
char bufname[128]; /* TODO: use GNAME_MAX? */
72
34
struct Option *base, *output, *input, *zeni, *azim, *metho;
35
struct Flag *ilum, *scl;
75
37
Gfile dem, out, band;
76
38
double zenith, azimuth;
77
39
int method = COSINE;
79
42
/* initialize GIS environment */
80
43
G_gisinit(argv[0]);
82
45
/* initialize module */
83
46
module = G_define_module();
47
G_add_keyword(_("imagery"));
48
G_add_keyword(_("terrain"));
49
G_add_keyword(_("topographic correction"));
84
50
module->description = _("Computes topographic correction of reflectance.");
86
_("imagery, terrain, topographic correction");
88
52
/* It defines the different parameters */
90
54
input = G_define_standard_option(G_OPT_R_INPUTS);
132
100
G_fatal_error(_("Solar azimuth is necessary to calculate illumination terrain model"));
134
102
if (!ilum->answer && input->answer == NULL)
135
G_fatal_error(_("Reflectance maps are necessary to make topographic correction"));
104
(_("Reflectance maps are necessary to make topographic correction"));
137
106
zenith = atof(zeni->answer);
107
out.type = DCELL_TYPE;
108
do_scale = scl->answer;
139
110
/* Evaluate only cos_i raster file */
140
111
/* i.topo.corr -i out=cosi.on07 base=SRTM_v2 zenith=33.3631 azimuth=59.8897 */
141
112
if (ilum->answer) {
113
Rast_get_window(&window);
142
114
azimuth = atof(azim->answer);
143
115
/* Warning: make buffers and output after set window */
144
full_open_old(&dem, base->answer);
116
strcpy(dem.name, base->answer);
145
117
/* Set window to DEM file */
146
G_get_window(&window);
147
G_get_cellhd(dem.name, dem.mapset, &hd_dem);
148
G_align_window(&window, &hd_dem);
149
G_set_window(&window);
118
Rast_get_window(&window);
119
Rast_get_cellhd(dem.name, "", &hd_dem);
120
Rast_align_window(&window, &hd_dem);
121
dem.fd = Rast_open_old(dem.name, "");
122
dem.type = Rast_get_map_type(dem.fd);
150
123
/* Open and buffer of the output file */
151
full_open_new(&out, output->answer, DCELL_TYPE);
152
out.rast = G_allocate_raster_buf(out.type);
124
strcpy(out.name, output->answer);
125
out.fd = Rast_open_new(output->answer, DCELL_TYPE);
126
out.rast = Rast_allocate_buf(out.type);
153
127
/* Open and buffer of the elevation file */
154
if (dem.type == CELL_TYPE) {
155
dem.rast = G_allocate_raster_buf(CELL_TYPE);
156
eval_c_cosi(&out, &dem, zenith, azimuth);
158
else if (dem.type == FCELL_TYPE) {
159
dem.rast = G_allocate_raster_buf(FCELL_TYPE);
160
eval_f_cosi(&out, &dem, zenith, azimuth);
162
else if (dem.type == DCELL_TYPE) {
163
dem.rast = G_allocate_raster_buf(DCELL_TYPE);
164
eval_d_cosi(&out, &dem, zenith, azimuth);
167
G_fatal_error(_("Elevation raster map of unknown type"));
128
dem.rast = Rast_allocate_buf(dem.type);
129
eval_cosi(&out, &dem, zenith, azimuth);
169
130
/* Close files, buffers, and write history */
170
131
G_free(dem.rast);
171
G_close_cell(dem.fd);
172
133
G_free(out.rast);
173
G_close_cell(out.fd);
174
G_short_history(out.name, "raster", &history);
175
G_command_history(&history);
176
G_write_history(out.name, &history);
135
Rast_short_history(out.name, "raster", &history);
136
Rast_command_history(&history);
137
Rast_write_history(out.name, &history);
178
139
/* Evaluate topographic correction for all bands */
179
140
/* i.topo.corr input=on07.toar.1 out=tcor base=cosi.on07 zenith=33.3631 method=c-factor */
197
158
G_fatal_error(_("Invalid method: %s"), metho->answer);
199
full_open_old(&dem, base->answer);
160
dem.fd = Rast_open_old(base->answer, "");
161
dem.type = Rast_get_map_type(dem.fd);
200
163
if (dem.type == CELL_TYPE)
201
164
G_fatal_error(_("Illumination model is of CELL type"));
203
166
for (i = 0; input->answers[i] != NULL; i++) {
204
G_message("Band %s: ", input->answers[i]);
167
G_message(_("Band %s: "), input->answers[i]);
205
168
/* Abre fichero de bandas y el de salida */
206
full_open_old(&band, input->answers[i]);
169
strcpy(band.name, input->answers[i]);
170
Rast_get_cellhd(band.name, "", &hd_band);
171
Rast_set_window(&hd_band); /* Antes de out_open y allocate para mismo size */
172
band.fd = Rast_open_old(band.name, "");
173
band.type = Rast_get_map_type(band.fd);
207
174
if (band.type != DCELL_TYPE) {
208
G_warning(_("Reflectance of raster map <%s> is not of DCELL type - ignored"),
175
G_warning(_("Reflectance of <%s> is not of DCELL type - ignored."),
209
176
input->answers[i]);
210
G_close_cell(band.fd);
213
G_get_cellhd(band.name, band.mapset, &hd_band);
214
G_set_window(&hd_band); /* Antes de out_open y allocate para mismo tama�o */
216
snprintf(bufname, 127, "%s.%s", output->answer,
181
dem.fd = Rast_open_old(base->answer, "");
182
snprintf(out.name, GNAME_MAX - 1, "%s.%s", output->answer,
217
183
input->answers[i]);
218
full_open_new(&out, bufname, DCELL_TYPE);
219
out.rast = G_allocate_raster_buf(out.type);
220
band.rast = G_allocate_raster_buf(band.type);
221
dem.rast = G_allocate_raster_buf(dem.type);
184
out.fd = Rast_open_new(out.name, DCELL_TYPE);
185
out.rast = Rast_allocate_buf(out.type);
186
band.rast = Rast_allocate_buf(band.type);
187
dem.rast = Rast_allocate_buf(dem.type);
223
eval_tcor(method, &out, &dem, &band, zenith);
189
eval_tcor(method, &out, &dem, &band, zenith, do_scale);
225
191
G_free(dem.rast);
226
193
G_free(band.rast);
227
G_close_cell(band.fd);
228
195
G_free(out.rast);
229
G_close_cell(out.fd);
230
G_short_history(out.name, "raster", &history);
231
G_command_history(&history);
232
G_write_history(out.name, &history);
236
/* TODO: better avoid system() */
237
sprintf(command, "r.colors map=%s color=grey", out.name);
240
/* new but not functional:
197
Rast_short_history(out.name, "raster", &history);
198
Rast_command_history(&history);
199
Rast_write_history(out.name, &history);
242
202
struct FPRange range;
244
204
struct Colors grey;
245
G_read_fp_range(out.name, G_mapset(), &range);
246
G_get_fp_range_min_max(&range, &min, &max);
247
G_make_grey_scale_colors(&grey, min, max);
248
G_write_colors(out.name, G_mapset(), &grey);
208
if (Rast_read_colors(band.name, "", &grey) >= 0)
213
Rast_read_fp_range(out.name, G_mapset(), &range);
214
Rast_get_fp_range_min_max(&range, &min, &max);
215
Rast_make_grey_scale_colors(&grey, min, max);
217
Rast_write_colors(out.name, G_mapset(), &grey);
253
G_close_cell(dem.fd);
256
222
exit(EXIT_SUCCESS);