GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
writeascii.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <grass/G3d.h>
6 
18 void G3d_writeAscii(void *map, const char *fname)
19 {
20  FILE *fp;
21  double d1 = 0;
22  double *d1p;
23  float *f1p;
24  int x, y, z;
25  int rows, cols, depths, typeIntern;
26 
27  G3d_getCoordsMap(map, &rows, &cols, &depths);
28  typeIntern = G3d_tileTypeMap(map);
29 
30  d1p = &d1;
31  f1p = (float *)&d1;
32 
33  if (fname == NULL)
34  fp = stdout;
35  else if ((fp = fopen(fname, "w")) == NULL)
36  G3d_fatalError("G3d_writeAscii: can't open file to write\n");
37 
38  for (z = 0; z < depths; z++)
39  for (y = 0; y < rows; y++) {
40  fprintf(fp, "z y x %d %d (%d - %d)\n", z, y, 0, cols - 1);
41  for (x = 0; x < cols; x++) {
42  G3d_getValueRegion(map, x, y, z, d1p, typeIntern);
43 
44  if (typeIntern == FCELL_TYPE)
45  fprintf(fp, "%.18f ", *f1p);
46  else
47  fprintf(fp, "%.50f ", d1);
48  }
49  fprintf(fp, "\n");
50  }
51 
52  if (fp != stdout)
53  fclose(fp);
54 }