file_io.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. void rdwr_gridatb(void)
  7. {
  8. char buf[1024];
  9. FILE *fp;
  10. int fd, i, j, retval;
  11. float idx;
  12. fp = fopen(file, "r");
  13. buf[0] = 0;
  14. fscanf(fp, "%[^\n]", buf);
  15. if (!buf[0])
  16. getc(fp);
  17. fscanf(fp, "%d %d %lf\n", &cellhd.cols, &cellhd.rows, &cellhd.ns_res);
  18. cellhd.ew_res = cellhd.ns_res;
  19. cellhd.south = 0;
  20. cellhd.north = cellhd.south + cellhd.ns_res * cellhd.rows;
  21. cellhd.west = 0;
  22. cellhd.east = cellhd.west + cellhd.ew_res * cellhd.cols;
  23. cellhd.format = -1;
  24. cellhd.compressed = 1;
  25. if (retval = adjcellhd(&cellhd)) {
  26. fclose(fp);
  27. switch (retval) {
  28. case 1:
  29. G_fatal_error(_("Setting window header failed"));
  30. break;
  31. case 2:
  32. G_fatal_error(_("Rows changed"));
  33. break;
  34. case 3:
  35. G_fatal_error(_("Cols changed"));
  36. break;
  37. }
  38. }
  39. fd = Rast_open_new(oname, FCELL_TYPE);
  40. cell = (FCELL *) G_malloc(sizeof(FCELL) * cellhd.cols);
  41. for (i = 0; i < cellhd.rows; i++) {
  42. G_percent(i, cellhd.rows, 2);
  43. for (j = 0; j < cellhd.cols; j++) {
  44. idx = 9999.0;
  45. fscanf(fp, "%f", &idx);
  46. if (idx >= 9999.0) {
  47. Rast_set_f_null_value(&(cell[j]), 1);
  48. }
  49. else {
  50. cell[j] = idx;
  51. }
  52. }
  53. Rast_put_f_row(fd, cell);
  54. }
  55. G_percent(i, cellhd.rows, 2);
  56. if(fp)
  57. fclose(fp);
  58. Rast_close(fd);
  59. Rast_put_cell_title(oname, buf);
  60. Rast_put_cellhd(oname, &cellhd);
  61. return;
  62. }