GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
resout2d.c
Go to the documentation of this file.
1 
2 /*-
3  * Written by H. Mitasova, I. Kosinovsky, D. Gerdes Summer 1993
4  * University of Illinois
5  * US Army Construction Engineering Research Lab
6  * Copyright 1993, H. Mitasova (University of Illinois),
7  * I. Kosinovsky, (USA-CERL), and D.Gerdes (USA-CERL)
8  *
9  * modified by McCauley in August 1995
10  * modified by Mitasova in August 1995
11  *
12  */
13 
14 #define MULT 100000
15 
16 #include <stdio.h>
17 #include <math.h>
18 #include <grass/gis.h>
19 #include <grass/bitmap.h>
20 #include <grass/linkm.h>
21 
22 #include <grass/interpf.h>
23 
24 
25 /* output cell maps for elevation, aspect, slope and curvatures */
26 
27 int IL_resample_output_2d(struct interp_params *params, double zmin, double zmax, /* min,max input z-values */
28  double zminac, double zmaxac, /* min,max interpolated values */
29  double c1min, double c1max, double c2min, double c2max, double gmin, double gmax, double ertot, /* total interplating func. error */
30  char *input, /* input file name */
31  double *dnorm, struct Cell_head *outhd, /* Region with desired resolution */
32  struct Cell_head *winhd, /* Current region */
33  char *smooth, int n_points)
34 
35 /*
36  * Creates output files as well as history files and color tables for
37  * them.
38  */
39 {
40  FCELL *cell1; /* cell buffer */
41  int cf1 = 0, cf2 = 0, cf3 = 0, cf4 = 0, cf5 = 0, cf6 = 0; /* cell file descriptors */
42  int nrows, ncols; /* current region rows and columns */
43  int i; /* loop counter */
44  char *mapset;
45  float dat1, dat2;
46  struct Colors colors, colors2;
47  double value1, value2;
48  struct History hist, hist1, hist2, hist3, hist4, hist5;
49  struct _Color_Rule_ *rule;
50  char *maps, *type;
51  int cond1, cond2;
52 
53  cond2 = ((params->pcurv != NULL) ||
54  (params->tcurv != NULL) || (params->mcurv != NULL));
55  cond1 = ((params->slope != NULL) || (params->aspect != NULL) || cond2);
56 
57  /* change region to output cell file region */
58  fprintf(stderr,
59  "Temporarily changing the region to desired resolution...\n");
60  if (G_set_window(outhd) < 0) {
61  fprintf(stderr, "Cannot set region to output region!\n");
62  return -1;
63  }
64  mapset = G_mapset();
65 
66  cell1 = G_allocate_f_raster_buf();
67 
68  if (params->elev != NULL) {
69  cf1 = G_open_fp_cell_new(params->elev);
70  if (cf1 < 0) {
71  fprintf(stderr, "unable to create raster map %s\n", params->elev);
72  return -1;
73  }
74  }
75 
76  if (params->slope != NULL) {
77  cf2 = G_open_fp_cell_new(params->slope);
78  if (cf2 < 0) {
79  fprintf(stderr, "unable to create raster map %s\n",
80  params->slope);
81  return -1;
82  }
83  }
84 
85  if (params->aspect != NULL) {
86  cf3 = G_open_fp_cell_new(params->aspect);
87  if (cf3 < 0) {
88  fprintf(stderr, "unable to create raster map %s\n",
89  params->aspect);
90  return -1;
91  }
92  }
93 
94  if (params->pcurv != NULL) {
95  cf4 = G_open_fp_cell_new(params->pcurv);
96  if (cf4 < 0) {
97  fprintf(stderr, "unable to create raster map %s\n",
98  params->pcurv);
99  return -1;
100  }
101  }
102 
103  if (params->tcurv != NULL) {
104  cf5 = G_open_fp_cell_new(params->tcurv);
105  if (cf5 < 0) {
106  fprintf(stderr, "unable to create raster map %s\n",
107  params->tcurv);
108  return -1;
109  }
110  }
111 
112  if (params->mcurv != NULL) {
113  cf6 = G_open_fp_cell_new(params->mcurv);
114  if (cf6 < 0) {
115  fprintf(stderr, "unable to create raster map %s\n",
116  params->mcurv);
117  return -1;
118  }
119  }
120 
121  nrows = outhd->rows;
122  if (nrows != params->nsizr) {
123  fprintf(stderr, "first change your rows number(%d) to %d!\n",
124  nrows, params->nsizr);
125  return -1;
126  }
127 
128  ncols = outhd->cols;
129  if (ncols != params->nsizc) {
130  fprintf(stderr, "first change your rows number(%d) to %d!\n",
131  ncols, params->nsizc);
132  return -1;
133  }
134 
135  if (params->elev != NULL) {
136  fseek(params->Tmp_fd_z, 0L, 0); /* seek to the beginning */
137  for (i = 0; i < params->nsizr; i++) {
138  /* seek to the right row */
139  if (fseek(params->Tmp_fd_z, (long)
140  ((params->nsizr - 1 -
141  i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
142  fprintf(stderr, "cannot fseek to the right spot\n");
143  return -1;
144  }
145  fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_z);
146  if (G_put_f_raster_row(cf1, cell1) < 0) {
147  fprintf(stderr, "cannot write file\n");
148  return -1;
149  }
150  }
151  }
152 
153  if (params->slope != NULL) {
154  fseek(params->Tmp_fd_dx, 0L, 0); /* seek to the beginning */
155  for (i = 0; i < params->nsizr; i++) {
156  /* seek to the right row */
157  if (fseek(params->Tmp_fd_dx, (long)
158  ((params->nsizr - 1 -
159  i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
160  fprintf(stderr, "cannot fseek to the right spot\n");
161  return -1;
162  }
163  fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dx);
164  /*
165  * for (ii==0;ii<params->nsizc;ii++) { fprintf(stderr,"ii=%d ",ii);
166  * fprintf(stderr,"%f ",cell1[ii]); }
167  * fprintf(stderr,"params->nsizc=%d \n",params->nsizc);
168  */
169  if (G_put_f_raster_row(cf2, cell1) < 0) {
170  fprintf(stderr, "cannot write file\n");
171  return -1;
172  }
173  }
174  }
175 
176  if (params->aspect != NULL) {
177  fseek(params->Tmp_fd_dy, 0L, 0); /* seek to the beginning */
178  for (i = 0; i < params->nsizr; i++) {
179  /* seek to the right row */
180  if (fseek(params->Tmp_fd_dy, (long)
181  ((params->nsizr - 1 -
182  i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
183  fprintf(stderr, "cannot fseek to the right spot\n");
184  return -1;
185  }
186  fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dy);
187  if (G_put_f_raster_row(cf3, cell1) < 0) {
188  fprintf(stderr, "cannot write file\n");
189  return -1;
190  }
191  }
192  }
193 
194  if (params->pcurv != NULL) {
195  fseek(params->Tmp_fd_xx, 0L, 0); /* seek to the beginning */
196  for (i = 0; i < params->nsizr; i++) {
197  /* seek to the right row */
198  if (fseek(params->Tmp_fd_xx, (long)
199  ((params->nsizr - 1 -
200  i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
201  fprintf(stderr, "cannot fseek to the right spot\n");
202  return -1;
203  }
204  fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xx);
205  if (G_put_f_raster_row(cf4, cell1) < 0) {
206  fprintf(stderr, "cannot write file\n");
207  return -1;
208  }
209  }
210  }
211 
212  if (params->tcurv != NULL) {
213  fseek(params->Tmp_fd_yy, 0L, 0); /* seek to the beginning */
214  for (i = 0; i < params->nsizr; i++) {
215  /* seek to the right row */
216  if (fseek(params->Tmp_fd_yy, (long)
217  ((params->nsizr - 1 -
218  i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
219  fprintf(stderr, "cannot fseek to the right spot\n");
220  return -1;
221  }
222  fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_yy);
223  if (G_put_f_raster_row(cf5, cell1) < 0) {
224  fprintf(stderr, "cannot write file\n");
225  return -1;
226  }
227  }
228  }
229 
230  if (params->mcurv != NULL) {
231  fseek(params->Tmp_fd_xy, 0L, 0); /* seek to the beginning */
232  for (i = 0; i < params->nsizr; i++) {
233  /* seek to the right row */
234  if (fseek(params->Tmp_fd_xy, (long)
235  ((params->nsizr - 1 -
236  i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
237  fprintf(stderr, "cannot fseek to the right spot\n");
238  return -1;
239  }
240  fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xy);
241  if (G_put_f_raster_row(cf6, cell1) < 0) {
242  fprintf(stderr, "cannot write file\n");
243  return -1;
244  }
245  }
246  }
247 
248  if (cf1)
249  G_close_cell(cf1);
250  if (cf2)
251  G_close_cell(cf2);
252  if (cf3)
253  G_close_cell(cf3);
254  if (cf4)
255  G_close_cell(cf4);
256  if (cf5)
257  G_close_cell(cf5);
258  if (cf6)
259  G_close_cell(cf6);
260 
261  /* write colormaps and history for output cell files */
262  /* colortable for elevations */
263  maps = G_find_file("cell", input, "");
264 
265  if (params->elev != NULL) {
266  if (maps == NULL) {
267  fprintf(stderr, "file [%s] not found\n", input);
268  return -1;
269  }
270  G_init_colors(&colors2);
271  /*
272  * G_mark_colors_as_fp(&colors2);
273  */
274 
275  if (G_read_colors(input, maps, &colors) >= 0) {
276  if (colors.modular.rules) {
277  rule = colors.modular.rules;
278 
279  while (rule->next)
280  rule = rule->next;
281 
282  for (; rule; rule = rule->prev) {
283  value1 = rule->low.value * params->zmult;
284  value2 = rule->high.value * params->zmult;
285  G_add_modular_d_raster_color_rule(&value1, rule->low.red,
286  rule->low.grn,
287  rule->low.blu, &value2,
288  rule->high.red,
289  rule->high.grn,
290  rule->high.blu,
291  &colors2);
292  }
293  }
294 
295  if (colors.fixed.rules) {
296  rule = colors.fixed.rules;
297 
298  while (rule->next)
299  rule = rule->next;
300 
301  for (; rule; rule = rule->prev) {
302  value1 = rule->low.value * params->zmult;
303  value2 = rule->high.value * params->zmult;
304  G_add_d_raster_color_rule(&value1, rule->low.red,
305  rule->low.grn, rule->low.blu,
306  &value2, rule->high.red,
307  rule->high.grn, rule->high.blu,
308  &colors2);
309  }
310  }
311 
312  maps = NULL;
313  maps = G_find_file("cell", params->elev, "");
314  if (maps == NULL) {
315  fprintf(stderr, "file [%s] not found\n", params->elev);
316  return -1;
317  }
318 
319  if (G_write_colors(params->elev, maps, &colors2) < 0) {
320  fprintf(stderr, "Cannot write color table\n");
321  return -1;
322  }
323  G_quantize_fp_map_range(params->elev, mapset,
324  zminac - 0.5, zmaxac + 0.5,
325  (CELL) (zminac - 0.5),
326  (CELL) (zmaxac + 0.5));
327  }
328  else
329  fprintf(stderr,
330  "No color table for input file -- will not create color table\n");
331  }
332 
333  /* colortable for slopes */
334  if (cond1 & (!params->deriv)) {
335  G_init_colors(&colors);
336  G_add_color_rule(0, 255, 255, 255, 2, 255, 255, 0, &colors);
337  G_add_color_rule(2, 255, 255, 0, 5, 0, 255, 0, &colors);
338  G_add_color_rule(5, 0, 255, 0, 10, 0, 255, 255, &colors);
339  G_add_color_rule(10, 0, 255, 255, 15, 0, 0, 255, &colors);
340  G_add_color_rule(15, 0, 0, 255, 30, 255, 0, 255, &colors);
341  G_add_color_rule(30, 255, 0, 255, 50, 255, 0, 0, &colors);
342  G_add_color_rule(50, 255, 0, 0, 90, 0, 0, 0, &colors);
343 
344  if (params->slope != NULL) {
345  maps = NULL;
346  maps = G_find_file("cell", params->slope, "");
347  if (maps == NULL) {
348  fprintf(stderr, "file [%s] not found\n", params->slope);
349  return -1;
350  }
351  G_write_colors(params->slope, maps, &colors);
352  G_quantize_fp_map_range(params->slope, mapset, 0., 90., 0, 90);
353 
354  type = "raster";
355  G_short_history(params->slope, type, &hist1);
356  if (params->elev != NULL)
357  sprintf(hist1.edhist[0], "The elevation map is %s",
358  params->elev);
359 
360  sprintf(hist1.datsrc_1, "raster map %s", input);
361  hist1.edlinecnt = 1;
362 
363  G_write_history(params->slope, &hist1);
364  }
365 
366  /* colortable for aspect */
367  G_init_colors(&colors);
368  G_add_color_rule(0, 255, 255, 255, 0, 255, 255, 255, &colors);
369  G_add_color_rule(1, 255, 255, 0, 90, 0, 255, 0, &colors);
370  G_add_color_rule(90, 0, 255, 0, 180, 0, 255, 255, &colors);
371  G_add_color_rule(180, 0, 255, 255, 270, 255, 0, 0, &colors);
372  G_add_color_rule(270, 255, 0, 0, 360, 255, 255, 0, &colors);
373 
374  if (params->aspect != NULL) {
375  maps = NULL;
376  maps = G_find_file("cell", params->aspect, "");
377  if (maps == NULL) {
378  fprintf(stderr, "file [%s] not found\n", params->aspect);
379  return -1;
380  }
381  G_write_colors(params->aspect, maps, &colors);
382  G_quantize_fp_map_range(params->aspect, mapset, 0., 360., 0, 360);
383 
384  type = "raster";
385  G_short_history(params->aspect, type, &hist2);
386  if (params->elev != NULL)
387  sprintf(hist2.edhist[0], "The elevation map is %s",
388  params->elev);
389 
390  sprintf(hist2.datsrc_1, "raster map %s", input);
391  hist2.edlinecnt = 1;
392 
393  G_write_history(params->aspect, &hist2);
394  }
395 
396  /* colortable for curvatures */
397  if (cond2) {
398  G_init_colors(&colors);
399 
400  dat1 = (FCELL) amin1(c1min, c2min);
401  dat2 = (FCELL) - 0.01;
402 
403  G_add_f_raster_color_rule(&dat1, 50, 0, 155,
404  &dat2, 0, 0, 255, &colors);
405  dat1 = dat2;
406  dat2 = (FCELL) - 0.001;
407  G_add_f_raster_color_rule(&dat1, 0, 0, 255,
408  &dat2, 0, 127, 255, &colors);
409  dat1 = dat2;
410  dat2 = (FCELL) - 0.00001;
411  G_add_f_raster_color_rule(&dat1, 0, 127, 255,
412  &dat2, 0, 255, 255, &colors);
413  dat1 = dat2;
414  dat2 = (FCELL) 0.00;
415  G_add_f_raster_color_rule(&dat1, 0, 255, 255,
416  &dat2, 200, 255, 200, &colors);
417  dat1 = dat2;
418  dat2 = (FCELL) 0.00001;
419  G_add_f_raster_color_rule(&dat1, 200, 255, 200,
420  &dat2, 255, 255, 0, &colors);
421  dat1 = dat2;
422  dat2 = (FCELL) 0.001;
423  G_add_f_raster_color_rule(&dat1, 255, 255, 0,
424  &dat2, 255, 127, 0, &colors);
425  dat1 = dat2;
426  dat2 = (FCELL) 0.01;
427  G_add_f_raster_color_rule(&dat1, 255, 127, 0,
428  &dat2, 255, 0, 0, &colors);
429  dat1 = dat2;
430  dat2 = (FCELL) amax1(c1max, c2max);
431  G_add_f_raster_color_rule(&dat1, 255, 0, 0,
432  &dat2, 155, 0, 20, &colors);
433  maps = NULL;
434  if (params->pcurv != NULL) {
435  maps = G_find_file("cell", params->pcurv, "");
436  if (maps == NULL) {
437  fprintf(stderr, "file [%s] not found\n", params->pcurv);
438  return -1;
439  }
440  G_write_colors(params->pcurv, maps, &colors);
441 
442  fprintf(stderr, "color map written\n");
443 
444  G_quantize_fp_map_range(params->pcurv, mapset,
445  dat1, dat2,
446  (CELL) (dat1 * MULT),
447  (CELL) (dat2 * MULT));
448  type = "raster";
449  G_short_history(params->pcurv, type, &hist3);
450  if (params->elev != NULL)
451  sprintf(hist3.edhist[0], "The elevation map is %s",
452  params->elev);
453 
454  sprintf(hist3.datsrc_1, "raster map %s", input);
455  hist3.edlinecnt = 1;
456 
457  G_write_history(params->pcurv, &hist3);
458  }
459 
460  if (params->tcurv != NULL) {
461  maps = NULL;
462  maps = G_find_file("cell", params->tcurv, "");
463  if (maps == NULL) {
464  fprintf(stderr, "file [%s] not found\n", params->tcurv);
465  return -1;
466  }
467  G_write_colors(params->tcurv, maps, &colors);
468  G_quantize_fp_map_range(params->tcurv, mapset,
469  dat1, dat2, (CELL) (dat1 * MULT),
470  (CELL) (dat2 * MULT));
471 
472  type = "raster";
473  G_short_history(params->tcurv, type, &hist4);
474  if (params->elev != NULL)
475  sprintf(hist4.edhist[0], "The elevation map is %s",
476  params->elev);
477 
478  sprintf(hist4.datsrc_1, "raster map %s", input);
479  hist4.edlinecnt = 1;
480 
481  G_write_history(params->tcurv, &hist4);
482  }
483 
484  if (params->mcurv != NULL) {
485  maps = NULL;
486  maps = G_find_file("cell", params->mcurv, "");
487  if (maps == NULL) {
488  fprintf(stderr, "file [%s] not found\n", params->mcurv);
489  return -1;
490  }
491  G_write_colors(params->mcurv, maps, &colors);
492  G_quantize_fp_map_range(params->mcurv, mapset,
493  dat1, dat2,
494  (CELL) (dat1 * MULT),
495  (CELL) (dat2 * MULT));
496 
497  type = "raster";
498  G_short_history(params->mcurv, type, &hist5);
499  if (params->elev != NULL)
500  sprintf(hist5.edhist[0], "The elevation map is %s",
501  params->elev);
502 
503  sprintf(hist5.datsrc_1, "raster map %s", input);
504  hist5.edlinecnt = 1;
505 
506  G_write_history(params->mcurv, &hist5);
507  }
508  }
509  }
510 
511  if (params->elev != NULL) {
512  maps = G_find_file("cell", params->elev, "");
513  if (maps == NULL) {
514  fprintf(stderr, "file [%s] not found \n", params->elev);
515  return -1;
516  }
517  G_short_history(params->elev, "raster", &hist);
518 
519  if (smooth != NULL)
520  sprintf(hist.edhist[0], "tension=%f, smoothing=%s",
521  params->fi * 1000. / (*dnorm), smooth);
522  else
523  sprintf(hist.edhist[0], "tension=%f",
524  params->fi * 1000. / (*dnorm));
525  sprintf(hist.edhist[1], "dnorm=%f, zmult=%f", *dnorm, params->zmult);
526  sprintf(hist.edhist[2], "KMAX=%d, KMIN=%d, errtotal=%f", params->kmax,
527  params->kmin, sqrt(ertot / n_points));
528  sprintf(hist.edhist[3], "zmin_data=%f, zmax_data=%f", zmin, zmax);
529  sprintf(hist.edhist[4], "zmin_int=%f, zmax_int=%f", zminac, zmaxac);
530 
531  sprintf(hist.datsrc_1, "raster map %s", input);
532 
533  hist.edlinecnt = 5;
534 
535  G_write_history(params->elev, &hist);
536  }
537 
538  /* change region to initial region */
539  fprintf(stderr, "Changing the region back to initial...\n");
540  if (G_set_window(winhd) < 0) {
541  fprintf(stderr, "Cannot set region to back to initial region!\n");
542  return -1;
543  }
544 
545  return 1;
546 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
int G_write_colors(const char *name, const char *mapset, struct Colors *colors)
write map layer color table
Definition: color_write.c:74
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_short_history(const char *name, const char *type, struct History *hist)
initialize history structure
Definition: history.c:202
int G_open_fp_cell_new(const char *name)
Opens new fcell file in a database.
Definition: opencell.c:546
int G_add_d_raster_color_rule(const DCELL *val1, int r1, int g1, int b1, const DCELL *val2, int r2, int g2, int b2, struct Colors *colors)
Adds the floating-point rule (DCELL version)
Definition: color_rule.c:41
int G_close_cell(int fd)
close a raster map
Definition: closecell.c:71
FCELL * G_allocate_f_raster_buf(void)
Allocates memory for a raster map of type FCELL.
Definition: alloc_cell.c:111
char * mcurv
Definition: interpf.h:53
FILE * Tmp_fd_xy
Definition: interpf.h:60
int G_set_window(struct Cell_head *window)
Establishes &#39;window&#39; as the current working window.
Definition: set_window.c:49
FILE * Tmp_fd_yy
Definition: interpf.h:60
double amax1(double, double)
Definition: minmax.c:54
char * elev
Definition: interpf.h:53
char * pcurv
Definition: interpf.h:53
int G_read_colors(const char *name, const char *mapset, struct Colors *colors)
read map layer color table
Definition: color_read.c:62
FILE * Tmp_fd_xx
Definition: interpf.h:60
int G_write_history(const char *name, struct History *hist)
write raster history file
Definition: history.c:153
FILE * Tmp_fd_dx
Definition: interpf.h:60
int G_add_f_raster_color_rule(const FCELL *cat1, int r1, int g1, int b1, const FCELL *cat2, int r2, int g2, int b2, struct Colors *colors)
Adds the floating-point rule (FCELL version)
Definition: color_rule.c:65
char * aspect
Definition: interpf.h:53
double fi
Definition: interpf.h:49
double amin1(double, double)
Definition: minmax.c:67
char * tcurv
Definition: interpf.h:53
return NULL
Definition: dbfopen.c:1394
FILE * Tmp_fd_dy
Definition: interpf.h:60
int IL_resample_output_2d(struct interp_params *, double, double, double, double, double, double, double, double, double, double, double, char *, double *, struct Cell_head *, struct Cell_head *, char *, int)
Definition: resout2d.c:27
#define MULT
Definition: resout2d.c:14
FILE * Tmp_fd_z
Definition: interpf.h:60
int G_add_color_rule(CELL cat1, int r1, int g1, int b1, CELL cat2, int r2, int g2, int b2, struct Colors *colors)
Set colors rules.
Definition: color_rule.c:165
double zmult
Definition: interpf.h:40
char * G_find_file(const char *element, char *name, const char *mapset)
searches for a file from the mapset search list or in a specified mapset. returns the mapset name whe...
Definition: find_file.c:159
int G_add_modular_d_raster_color_rule(const DCELL *val1, int r1, int g1, int b1, const DCELL *val2, int r2, int g2, int b2, struct Colors *colors)
Add modular color rule (DCELL version)
Definition: color_rule.c:187
int G_quantize_fp_map_range(const char *name, const char *mapset, DCELL d_min, DCELL d_max, CELL min, CELL max)
Writes the f_quant file for the raster map name with one rule. The rule is generated using the floati...
Definition: quant_rw.c:159
G_init_colors(colors)
char * slope
Definition: interpf.h:53
int G_put_f_raster_row(int fd, const FCELL *buf)
Definition: gis/put_row.c:233