writeascii.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster3d.h>
  7. /*!
  8. * \brief
  9. *
  10. * Writes the cell-values of <em>map</em> in ascii format to file
  11. * <em>fname</em>. The values are organized by horizontal slices.
  12. *
  13. * \param map
  14. * \param fname
  15. * \return void
  16. */
  17. void Rast3d_write_ascii(void *map, const char *fname)
  18. {
  19. FILE *fp;
  20. DCELL d1 = 0;
  21. DCELL *d1p;
  22. FCELL *f1p;
  23. int x, y, z;
  24. int rows, cols, depths, typeIntern;
  25. Rast3d_get_coords_map(map, &rows, &cols, &depths);
  26. typeIntern = Rast3d_tile_type_map(map);
  27. d1p = &d1;
  28. f1p = (FCELL *) &d1;
  29. if (fname == NULL)
  30. fp = stdout;
  31. else if ((fp = fopen(fname, "w")) == NULL)
  32. Rast3d_fatal_error("Rast3d_write_ascii: can't open file to write\n");
  33. for (z = 0; z < depths; z++) {
  34. for (y = 0; y < rows; y++) {
  35. fprintf(fp, "z y x %d %d (%d - %d)\n", z, y, 0, cols - 1);
  36. for (x = 0; x < cols; x++) {
  37. Rast3d_get_value_region(map, x, y, z, d1p, typeIntern);
  38. if (typeIntern == FCELL_TYPE)
  39. fprintf(fp, "%.18f ", *f1p);
  40. else
  41. fprintf(fp, "%.50f ", d1);
  42. }
  43. fprintf(fp, "\n");
  44. }
  45. }
  46. if (fp != stdout)
  47. fclose(fp);
  48. }