writeascii.c 1.1 KB

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