GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
resout2d.c
Go to the documentation of this file.
1/*-
2 * Written by H. Mitasova, I. Kosinovsky, D. Gerdes Summer 1993
3 * University of Illinois
4 * US Army Construction Engineering Research Lab
5 * SPDX-FileCopyrightText: 1993 H. Mitasova (University of Illinois)
6 * SPDX-FileCopyrightText: 1993 I. Kosinovsky (USA-CERL)
7 * SPDX-FileCopyrightText: 1993 D.Gerdes (USA-CERL)
8 * SPDX-FileCopyrightText: GRASS Development Team
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 *
11 * modified by McCauley in August 1995
12 * modified by Mitasova in August 1995
13 *
14 */
15
16#define MULT 100000
17
18#include <stdio.h>
19#include <math.h>
20#include <errno.h>
21
22#include <grass/gis.h>
23#include <grass/raster.h>
24#include <grass/bitmap.h>
25#include <grass/linkm.h>
26#include <grass/interpf.h>
27#include <grass/glocale.h>
28
29/* output cell maps for elevation, aspect, slope and curvatures */
30
31static void do_history(const char *name, const char *input,
32 const struct interp_params *params)
33{
34 struct History hist;
35
36 Rast_short_history(name, "raster", &hist);
37 if (params->elev)
38 Rast_append_format_history(&hist, "The elevation map is %s",
39 params->elev);
40
41 Rast_format_history(&hist, HIST_DATSRC_1, "raster map %s", input);
42
44
45 Rast_free_history(&hist);
46}
47
49 struct interp_params *params, double zmin,
50 double zmax, /* min,max input z-values */
51 double zminac, double zmaxac, /* min,max interpolated values */
52 double c1min, double c1max, double c2min, double c2max,
53 double gmin G_UNUSED, double gmax G_UNUSED,
54 double ertot, /* total interplating func. error */
55 char *input, /* input file name */
56 double *dnorm, struct Cell_head *outhd, /* Region with desired resolution */
57 struct Cell_head *winhd, /* Current region */
58 char *smooth, int n_points)
59/*
60 * Creates output files as well as history files and color tables for
61 * them.
62 */
63{
64 FCELL *cell1; /* cell buffer */
65 int cf1 = 0, cf2 = 0, cf3 = 0, cf4 = 0, cf5 = 0,
66 cf6 = 0; /* cell file descriptors */
67 int nrows, ncols; /* current region rows and columns */
68 int i; /* loop counter */
69 const char *mapset;
70 float dat1, dat2;
71 struct Colors colors, colors2;
72 double value1, value2;
73 struct History hist;
74 struct _Color_Rule_ *rule;
75 const char *maps;
76 int cond1, cond2;
77 CELL val1, val2;
78
79 cond2 = ((params->pcurv != NULL) || (params->tcurv != NULL) ||
80 (params->mcurv != NULL));
81 cond1 = ((params->slope != NULL) || (params->aspect != NULL) || cond2);
82
83 /* change region to output cell file region */
85 _("Temporarily changing the region to desired resolution..."));
87 mapset = G_mapset();
88
90
91 if (params->elev)
92 cf1 = Rast_open_fp_new(params->elev);
93
94 if (params->slope)
95 cf2 = Rast_open_fp_new(params->slope);
96
97 if (params->aspect)
98 cf3 = Rast_open_fp_new(params->aspect);
99
100 if (params->pcurv)
101 cf4 = Rast_open_fp_new(params->pcurv);
102
103 if (params->tcurv)
104 cf5 = Rast_open_fp_new(params->tcurv);
105
106 if (params->mcurv)
107 cf6 = Rast_open_fp_new(params->mcurv);
108
109 nrows = outhd->rows;
110 if (nrows != params->nsizr) {
111 G_warning(_("First change your rows number(%d) to %d"), nrows,
112 params->nsizr);
113 return -1;
114 }
115
116 ncols = outhd->cols;
117 if (ncols != params->nsizc) {
118 G_warning(_("First change your columns number(%d) to %d"), ncols,
119 params->nsizr);
120 return -1;
121 }
122
123 if (params->elev != NULL) {
124 G_fseek(params->Tmp_fd_z, 0L, 0); /* seek to the beginning */
125 for (i = 0; i < params->nsizr; i++) {
126 /* seek to the right row */
127 G_fseek(params->Tmp_fd_z,
128 (off_t)(params->nsizr - 1 - i) * params->nsizc *
129 sizeof(FCELL),
130 0);
131 if (fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_z) !=
132 (size_t)params->nsizc)
133 G_fatal_error(_("RST library temporary file reading error: %s"),
134 strerror(errno));
136 }
137 }
138
139 if (params->slope != NULL) {
140 G_fseek(params->Tmp_fd_dx, 0L, 0); /* seek to the beginning */
141 for (i = 0; i < params->nsizr; i++) {
142 /* seek to the right row */
143 G_fseek(params->Tmp_fd_dx,
144 (off_t)(params->nsizr - 1 - i) * params->nsizc *
145 sizeof(FCELL),
146 0);
147 if (fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dx) !=
148 (size_t)params->nsizc)
149 G_fatal_error(_("RST library temporary file reading error: %s"),
150 strerror(errno));
152 }
153 }
154
155 if (params->aspect != NULL) {
156 G_fseek(params->Tmp_fd_dy, 0L, 0); /* seek to the beginning */
157 for (i = 0; i < params->nsizr; i++) {
158 /* seek to the right row */
159 G_fseek(params->Tmp_fd_dy,
160 (off_t)(params->nsizr - 1 - i) * params->nsizc *
161 sizeof(FCELL),
162 0);
163 if (fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dy) !=
164 (size_t)params->nsizc)
165 G_fatal_error(_("RST library temporary file reading error: %s"),
166 strerror(errno));
168 }
169 }
170
171 if (params->pcurv != NULL) {
172 G_fseek(params->Tmp_fd_xx, 0L, 0); /* seek to the beginning */
173 for (i = 0; i < params->nsizr; i++) {
174 /* seek to the right row */
175 G_fseek(params->Tmp_fd_xx,
176 (off_t)(params->nsizr - 1 - i) * params->nsizc *
177 sizeof(FCELL),
178 0);
179 if (fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xx) !=
180 (size_t)params->nsizc)
181 G_fatal_error(_("RST library temporary file reading error: %s"),
182 strerror(errno));
184 }
185 }
186
187 if (params->tcurv != NULL) {
188 G_fseek(params->Tmp_fd_yy, 0L, 0); /* seek to the beginning */
189 for (i = 0; i < params->nsizr; i++) {
190 /* seek to the right row */
191 G_fseek(params->Tmp_fd_yy,
192 (off_t)(params->nsizr - 1 - i) * params->nsizc *
193 sizeof(FCELL),
194 0);
195 if (fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_yy) !=
196 (size_t)params->nsizc)
197 G_fatal_error(_("RST library temporary file reading error: %s"),
198 strerror(errno));
200 }
201 }
202
203 if (params->mcurv != NULL) {
204 G_fseek(params->Tmp_fd_xy, 0L, 0); /* seek to the beginning */
205 for (i = 0; i < params->nsizr; i++) {
206 /* seek to the right row */
207 G_fseek(params->Tmp_fd_xy,
208 (off_t)(params->nsizr - 1 - i) * params->nsizc *
209 sizeof(FCELL),
210 0);
211 if (fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xy) !=
212 (size_t)params->nsizc)
213 G_fatal_error(_("RST library temporary file reading error: %s"),
214 strerror(errno));
216 }
217 }
218
219 if (cf1)
221 if (cf2)
223 if (cf3)
225 if (cf4)
227 if (cf5)
229 if (cf6)
231
232 /* write colormaps and history for output cell files */
233 /* colortable for elevations */
234 maps = G_find_file("cell", input, "");
235
236 if (params->elev != NULL) {
237 if (maps == NULL) {
238 G_warning(_("Raster map <%s> not found"), input);
239 return -1;
240 }
242 /*
243 * Rast_mark_colors_as_fp(&colors2);
244 */
245
246 if (Rast_read_colors(input, maps, &colors) >= 0) {
247 if (colors.modular.rules) {
248 rule = colors.modular.rules;
249
250 while (rule->next)
251 rule = rule->next;
252
253 for (; rule; rule = rule->prev) {
254 value1 = rule->low.value * params->zmult;
255 value2 = rule->high.value * params->zmult;
257 &value1, rule->low.red, rule->low.grn, rule->low.blu,
258 &value2, rule->high.red, rule->high.grn, rule->high.blu,
259 &colors2);
260 }
261 }
262
263 if (colors.fixed.rules) {
264 rule = colors.fixed.rules;
265
266 while (rule->next)
267 rule = rule->next;
268
269 for (; rule; rule = rule->prev) {
270 value1 = rule->low.value * params->zmult;
271 value2 = rule->high.value * params->zmult;
272 Rast_add_d_color_rule(&value1, rule->low.red, rule->low.grn,
273 rule->low.blu, &value2,
274 rule->high.red, rule->high.grn,
275 rule->high.blu, &colors2);
276 }
277 }
278
279 maps = NULL;
280 maps = G_find_file("cell", params->elev, "");
281 if (maps == NULL) {
282 G_warning(_("Raster map <%s> not found"), params->elev);
283 return -1;
284 }
285
287 Rast_quantize_fp_map_range(params->elev, mapset, zminac - 0.5,
288 zmaxac + 0.5, (CELL)(zminac - 0.5),
289 (CELL)(zmaxac + 0.5));
290 }
291 else
292 G_warning(_("No color table for input raster map -- will not "
293 "create color table"));
294 }
295
296 /* colortable for slopes */
297 if (cond1 & (!params->deriv)) {
298 Rast_init_colors(&colors);
299 val1 = 0;
300 val2 = 2;
301 Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0,
302 &colors);
303 val1 = 2;
304 val2 = 5;
305 Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
306 val1 = 5;
307 val2 = 10;
308 Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
309 val1 = 10;
310 val2 = 15;
311 Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
312 val1 = 15;
313 val2 = 30;
314 Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
315 val1 = 30;
316 val2 = 50;
317 Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
318 val1 = 50;
319 val2 = 90;
320 Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
321
322 if (params->slope != NULL) {
323 maps = NULL;
324 maps = G_find_file("cell", params->slope, "");
325 if (maps == NULL) {
326 G_warning(_("Raster map <%s> not found"), params->slope);
327 return -1;
328 }
329 Rast_write_colors(params->slope, maps, &colors);
330 Rast_quantize_fp_map_range(params->slope, mapset, 0., 90., 0, 90);
331
332 do_history(params->slope, input, params);
333 }
334
335 /* colortable for aspect */
336 Rast_init_colors(&colors);
337 val1 = 0;
338 val2 = 0;
339 Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 255,
340 &colors);
341 val1 = 1;
342 val2 = 90;
343 Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
344 val1 = 90;
345 val2 = 180;
346 Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
347 val1 = 180;
348 val2 = 270;
349 Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 255, 0, 0, &colors);
350 val1 = 270;
351 val2 = 360;
352 Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 255, 255, 0, &colors);
353
354 if (params->aspect != NULL) {
355 maps = NULL;
356 maps = G_find_file("cell", params->aspect, "");
357 if (maps == NULL) {
358 G_warning(_("Raster map <%s> not found"), params->aspect);
359 return -1;
360 }
361 Rast_write_colors(params->aspect, maps, &colors);
362 Rast_quantize_fp_map_range(params->aspect, mapset, 0., 360., 0,
363 360);
364
365 do_history(params->aspect, input, params);
366 }
367
368 /* colortable for curvatures */
369 if (cond2) {
370 Rast_init_colors(&colors);
371
373 dat2 = (FCELL)-0.01;
374
375 Rast_add_f_color_rule(&dat1, 50, 0, 155, &dat2, 0, 0, 255, &colors);
376 dat1 = dat2;
377 dat2 = (FCELL)-0.001;
378 Rast_add_f_color_rule(&dat1, 0, 0, 255, &dat2, 0, 127, 255,
379 &colors);
380 dat1 = dat2;
381 dat2 = (FCELL)-0.00001;
382 Rast_add_f_color_rule(&dat1, 0, 127, 255, &dat2, 0, 255, 255,
383 &colors);
384 dat1 = dat2;
385 dat2 = (FCELL)0.00;
386 Rast_add_f_color_rule(&dat1, 0, 255, 255, &dat2, 200, 255, 200,
387 &colors);
388 dat1 = dat2;
389 dat2 = (FCELL)0.00001;
390 Rast_add_f_color_rule(&dat1, 200, 255, 200, &dat2, 255, 255, 0,
391 &colors);
392 dat1 = dat2;
393 dat2 = (FCELL)0.001;
394 Rast_add_f_color_rule(&dat1, 255, 255, 0, &dat2, 255, 127, 0,
395 &colors);
396 dat1 = dat2;
397 dat2 = (FCELL)0.01;
398 Rast_add_f_color_rule(&dat1, 255, 127, 0, &dat2, 255, 0, 0,
399 &colors);
400 dat1 = dat2;
402 Rast_add_f_color_rule(&dat1, 255, 0, 0, &dat2, 155, 0, 20, &colors);
403 maps = NULL;
404 if (params->pcurv != NULL) {
405 maps = G_find_file("cell", params->pcurv, "");
406 if (maps == NULL) {
407 G_warning(_("Raster map <%s> not found"), params->pcurv);
408 return -1;
409 }
410 Rast_write_colors(params->pcurv, maps, &colors);
411
412 fprintf(stderr, "color map written\n");
413
414 Rast_quantize_fp_map_range(params->pcurv, mapset, dat1, dat2,
415 (CELL)(dat1 * MULT),
416 (CELL)(dat2 * MULT));
417 do_history(params->pcurv, input, params);
418 }
419
420 if (params->tcurv != NULL) {
421 maps = NULL;
422 maps = G_find_file("cell", params->tcurv, "");
423 if (maps == NULL) {
424 G_warning(_("Raster map <%s> not found"), params->tcurv);
425 return -1;
426 }
427 Rast_write_colors(params->tcurv, maps, &colors);
428 Rast_quantize_fp_map_range(params->tcurv, mapset, dat1, dat2,
429 (CELL)(dat1 * MULT),
430 (CELL)(dat2 * MULT));
431
432 do_history(params->tcurv, input, params);
433 }
434
435 if (params->mcurv != NULL) {
436 maps = NULL;
437 maps = G_find_file("cell", params->mcurv, "");
438 if (maps == NULL) {
439 G_warning(_("Raster map <%s> not found"), params->mcurv);
440 return -1;
441 }
442 Rast_write_colors(params->mcurv, maps, &colors);
443 Rast_quantize_fp_map_range(params->mcurv, mapset, dat1, dat2,
444 (CELL)(dat1 * MULT),
445 (CELL)(dat2 * MULT));
446
447 do_history(params->mcurv, input, params);
448 }
449 }
450 }
451
452 if (params->elev != NULL) {
453 if (!G_find_file2("cell", params->elev, "")) {
454 G_warning(_("Raster map <%s> not found"), params->elev);
455 return -1;
456 }
457
458 Rast_short_history(params->elev, "raster", &hist);
459
460 if (smooth != NULL)
461 Rast_append_format_history(&hist, "tension=%f, smoothing=%s",
462 params->fi * 1000. / (*dnorm), smooth);
463 else
464 Rast_append_format_history(&hist, "tension=%f",
465 params->fi * 1000. / (*dnorm));
466
467 Rast_append_format_history(&hist, "dnorm=%f, zmult=%f", *dnorm,
468 params->zmult);
469 Rast_append_format_history(&hist, "KMAX=%d, KMIN=%d, errtotal=%f",
470 params->kmax, params->kmin,
471 sqrt(ertot / n_points));
472 Rast_append_format_history(&hist, "zmin_data=%f, zmax_data=%f", zmin,
473 zmax);
474 Rast_append_format_history(&hist, "zmin_int=%f, zmax_int=%f", zminac,
475 zmaxac);
476
477 Rast_format_history(&hist, HIST_DATSRC_1, "raster map %s", input);
478
479 Rast_write_history(params->elev, &hist);
480
481 Rast_free_history(&hist);
482 }
483
484 /* change region to initial region */
485 G_verbose_message(_("Changing the region back to initial..."));
487
488 return 1;
489}
#define NULL
Definition ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
const char * G_find_file(const char *, char *, const char *)
Searches for a file from the mapset search list or in a specified mapset.
Definition find_file.c:182
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition gis/seek.c:48
void void G_verbose_message(const char *,...) __attribute__((format(printf
const char * G_find_file2(const char *, const char *, const char *)
Searches for a file from the mapset search list or in a specified mapset. (look but don't touch)
Definition find_file.c:230
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
void Rast_set_output_window(struct Cell_head *)
Establishes 'window' as the current working window for output.
void Rast_add_d_color_rule(const DCELL *, int, int, int, const DCELL *, int, int, int, struct Colors *)
Adds the floating-point color rule (DCELL version)
Definition color_rule.c:36
int Rast_read_colors(const char *, const char *, struct Colors *)
Read color table of raster map.
void Rast_close(int)
Close a raster map.
void Rast_quantize_fp_map_range(const char *, const char *, DCELL, DCELL, CELL, CELL)
Write quant rules (f_quant) for floating-point raster map.
Definition quant_rw.c:121
void Rast_format_history(struct History *, int, const char *,...)
FCELL * Rast_allocate_f_output_buf(void)
Definition alloc_cell.c:188
void Rast_add_f_color_rule(const FCELL *, int, int, int, const FCELL *, int, int, int, struct Colors *)
Adds the floating-point color rule (FCELL version)
Definition color_rule.c:55
void Rast_add_c_color_rule(const CELL *, int, int, int, const CELL *, int, int, int, struct Colors *)
Adds the integer color rule (CELL version)
Definition color_rule.c:74
void Rast_put_f_row(int, const FCELL *)
Writes the next row for fcell file (FCELL version)
void Rast_write_history(const char *, struct History *)
Write raster history file.
void Rast_append_format_history(struct History *, const char *,...)
Append a formatted string to a History structure.
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition color_init.c:23
void Rast_short_history(const char *, const char *, struct History *)
Initialize history structure.
int Rast_add_modular_d_color_rule(const DCELL *, int, int, int, const DCELL *, int, int, int, struct Colors *)
Add modular floating-point color rule (DCELL version)
Definition color_rule.c:122
void Rast_write_colors(const char *, const char *, struct Colors *)
Write map layer color table.
int Rast_open_fp_new(const char *)
Opens new fcell file in a database.
void Rast_free_history(struct History *)
float FCELL
Definition gis.h:633
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
int CELL
Definition gis.h:631
#define _(str)
Definition glocale.h:10
double amin1(double, double)
Definition minmax.c:68
double amax1(double, double)
Definition minmax.c:55
const char * name
Definition named_colr.c:6
@ HIST_DATSRC_1
Description of original data source (two lines)
Definition raster.h:162
int IL_resample_output_2d(struct interp_params *params, double zmin, double zmax, double zminac, double zmaxac, double c1min, double c1max, double c2min, double c2max, double gmin, double gmax, double ertot, char *input, double *dnorm, struct Cell_head *outhd, struct Cell_head *winhd, char *smooth, int n_points)
Definition resout2d.c:48
#define MULT
Definition resout2d.c:16
2D/3D raster map header (used also for region)
Definition gis.h:443
Definition gis.h:689
struct _Color_Info_ fixed
Definition gis.h:702
struct _Color_Info_ modular
Definition gis.h:703
Raster history info (metadata)
Definition raster.h:172
struct _Color_Rule_ * prev
Definition gis.h:662
struct _Color_Rule_ * next
Definition gis.h:661
struct _Color_Value_ low high
Definition gis.h:660
double zmult
Definition interpf.h:72
FILE * Tmp_fd_xx
Definition interpf.h:123
FILE * Tmp_fd_xy
Definition interpf.h:123
char * pcurv
Definition interpf.h:107
FILE * Tmp_fd_yy
Definition interpf.h:123
double fi
Definition interpf.h:97
FILE * Tmp_fd_dx
Definition interpf.h:123
FILE * Tmp_fd_z
Definition interpf.h:123
char * tcurv
Definition interpf.h:107
char * mcurv
Definition interpf.h:107
FILE * Tmp_fd_dy
Definition interpf.h:123
char * aspect
Definition interpf.h:107
char * elev
Definition interpf.h:107
char * slope
Definition interpf.h:107