file_io.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "global.h"
  6. void read_cells(void)
  7. {
  8. int fd, i, j;
  9. RASTER_MAP_TYPE data_type;
  10. CELL *ccell = NULL;
  11. FCELL *fcell = NULL;
  12. struct Cell_head inhead;
  13. char buf_wrns[32], buf_wrew[32], buf_mrns[32], buf_mrew[32];
  14. fd = Rast_open_old(input, "");
  15. data_type = Rast_get_map_type(fd);
  16. Rast_get_cellhd(input, "", &inhead);
  17. if (data_type == CELL_TYPE)
  18. ccell = (CELL *) G_malloc(sizeof(CELL) * window.cols);
  19. else if (data_type == FCELL_TYPE)
  20. fcell = (FCELL *) G_malloc(sizeof(FCELL) * window.cols);
  21. cell = (DCELL **) G_malloc(sizeof(DCELL *) * window.rows);
  22. atb = (DCELL **) G_malloc(sizeof(DCELL *) * window.rows);
  23. a = (DCELL **) G_malloc(sizeof(DCELL *) * window.rows);
  24. if (window.ew_res < inhead.ew_res || window.ns_res < inhead.ns_res) {
  25. G_format_resolution(window.ew_res, buf_wrew, G_projection());
  26. G_format_resolution(window.ns_res, buf_wrns, G_projection());
  27. G_format_resolution(inhead.ew_res, buf_mrew, G_projection());
  28. G_format_resolution(inhead.ns_res, buf_mrns, G_projection());
  29. G_fatal_error(_("The current region resolution [%s x %s] is finer "
  30. "than the input map's resolution [%s x %s]. "
  31. "The current region resolution must be identical "
  32. "to, or coarser than, the input map's resolution."),
  33. buf_wrew, buf_wrns, buf_mrew, buf_mrns);
  34. }
  35. G_message(_("Reading elevation map..."));
  36. for (i = 0; i < window.rows; i++) {
  37. G_percent(i, window.rows, 2);
  38. cell[i] = (DCELL *) G_malloc(sizeof(DCELL) * window.cols);
  39. atb[i] = (DCELL *) G_malloc(sizeof(DCELL) * window.cols);
  40. a[i] = (DCELL *) G_malloc(sizeof(DCELL) * window.cols);
  41. if (data_type == CELL_TYPE) {
  42. Rast_get_c_row(fd, ccell, i);
  43. for (j = 0; j < window.cols; j++) {
  44. if (Rast_is_c_null_value(&ccell[j]))
  45. Rast_set_d_null_value(&cell[i][j], 1);
  46. else
  47. cell[i][j] = (DCELL) ccell[j];
  48. }
  49. }
  50. else if (data_type == FCELL_TYPE) {
  51. Rast_get_f_row(fd, fcell, i);
  52. for (j = 0; j < window.cols; j++) {
  53. if (Rast_is_f_null_value(&fcell[j]))
  54. Rast_set_d_null_value(&cell[i][j], 1);
  55. else
  56. cell[i][j] = (DCELL) fcell[j];
  57. }
  58. }
  59. else
  60. Rast_get_d_row(fd, cell[i], i);
  61. }
  62. if (data_type == CELL_TYPE)
  63. G_free(ccell);
  64. else if (data_type == FCELL_TYPE)
  65. G_free(fcell);
  66. G_percent(i, window.rows, 2);
  67. Rast_close(fd);
  68. }
  69. void write_cells(void)
  70. {
  71. int fd, i;
  72. struct History history;
  73. fd = Rast_open_new(output, DCELL_TYPE);
  74. G_message(_("Writing topographic index map..."));
  75. for (i = 0; i < window.rows; i++) {
  76. G_percent(i, window.rows, 2);
  77. Rast_put_d_row(fd, atb[i]);
  78. }
  79. G_percent(i, window.rows, 2);
  80. Rast_close(fd);
  81. Rast_short_history(output, "raster", &history);
  82. Rast_set_history(&history, HIST_DATSRC_1, input);
  83. Rast_write_history(output, &history);
  84. }