GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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/gis.h>
6 #include <grass/raster3d.h>
7 
8 /*!
9  * \brief
10  *
11  * Writes the cell-values of <em>map</em> in ascii format to file
12  * <em>fname</em>. The values are organized by horizontal slices.
13  *
14  * \param map
15  * \param fname
16  * \return void
17  */
18 void Rast3d_write_ascii(void *map, const char *fname)
19 {
20  FILE *fp;
21  DCELL d1 = 0;
22  DCELL *d1p;
23  FCELL *f1p;
24  int x, y, z;
25  int rows, cols, depths, typeIntern;
26 
27  Rast3d_get_coords_map(map, &rows, &cols, &depths);
28  typeIntern = Rast3d_tile_type_map(map);
29 
30  d1p = &d1;
31  f1p = (FCELL *)&d1;
32 
33  if (fname == NULL)
34  fp = stdout;
35  else if ((fp = fopen(fname, "w")) == NULL)
36  Rast3d_fatal_error("Rast3d_write_ascii: 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  Rast3d_get_value_region(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 
53  if (fp != stdout)
54  fclose(fp);
55 }
#define NULL
Definition: ccmath.h:32
int Rast3d_tile_type_map(RASTER3D_Map *)
Returns the type in which tiles of map are stored in memory.
Definition: headerinfo.c:150
void Rast3d_get_coords_map(RASTER3D_Map *, int *, int *, int *)
Returns the size of the region of map in cells.
Definition: headerinfo.c:17
void Rast3d_get_value_region(RASTER3D_Map *, int, int, int, void *, int)
Returns in *value the cell-value of the cell with region-coordinate (x, y, z). The value returned is ...
Definition: getvalue.c:249
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
float FCELL
Definition: gis.h:636
double DCELL
Definition: gis.h:635
#define FCELL_TYPE
Definition: raster.h:12
void Rast3d_write_ascii(void *map, const char *fname)
Writes the cell-values of map in ascii format to file fname. The values are organized by horizontal s...
Definition: writeascii.c:18
#define x