Debian Codegraph @

raster3d/r3.out.ascii/main.c

raw
1 /****************************************************************************
2 *
3 * MODULE: r3.out.ascii
4 *
5 * AUTHOR(S): Original author Mark Astley, Bill Brown
6 * USA CERL started 4/4/96
7 * Soeren Gebbert
8 *
9 * PURPOSE: Converts a 3D raster map layer into an ASCII text file
10 *
11 * COPYRIGHT: (C) 2005 - 2012 by the GRASS Development Team
12 *
13 * This program is free software under the GNU General Public
14 * License (>=v2). Read the file COPYING that comes with GRASS
15 * for details.
16 *
17 *****************************************************************************/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <grass/gis.h>
22 #include <grass/raster3d.h>
23 #include <grass/glocale.h>
24
25 /* structs */
26 typedef struct {
27 struct Option *input, *output, *decimals, *null_val;
28 struct Flag *header;
29 struct Flag *row;
30 struct Flag *depth;
31 struct Flag *grass6;
32 struct Flag *mask;
33 } paramType;
34
35 /* protos */
36 static void fatalError(char *errorMsg);
37 static void setParams(void);
38 static void getParams(char **input, char **output, int *decim);
39 static void writeHeaderString(FILE *fp, char *valueString, double value);
40 static void writeHeaderString2(FILE *fp, char *valueString, int value);
41 static void writeHeaderString3(FILE *fp, char *valueString, const char *value);
42 static FILE *openAscii(char *asciiFile, RASTER3D_Region region);
43 static void G3dToascii(FILE *fp, RASTER3D_Region region, int decim);
44
45 /* globals */
46 void *map = NULL;
47 paramType param;
48
49 /*---------------------------------------------------------------------------*/
50
51 /* Simple error handling routine, will eventually replace this with
52 * RASTER3D_fatalError.
53 */
54 void fatalError(char *errorMsg)
55 {
56 if (map != NULL) {
57 /* should unopen map here! */
58 if (!Rast3d_close(map))
59 Rast3d_fatal_error("%s (%s)", errorMsg,
60 _("Unable to close 3D raster map"));
61 }
62
63 Rast3d_fatal_error("%s", errorMsg);
64 }
65
66 /*---------------------------------------------------------------------------*/
67
68 /* Convenient way to set up the arguments we are expecting
69 */
70 void setParams(void)
71 {
72 param.input = G_define_option();
73 param.input->key = "input";
74 param.input->type = TYPE_STRING;
75 param.input->required = YES;
76 param.input->gisprompt = "old,grid3,3d-raster";
77 param.input->multiple = NO;
78 param.input->description = _("3D raster map to be converted to ASCII");
79
80 param.output = G_define_standard_option(G_OPT_F_OUTPUT);
81 param.output->required = NO;
82 param.output->description = _("Name for ASCII output file");
83
84 param.decimals = G_define_option();
85 param.decimals->key = "precision";
86 param.decimals->type = TYPE_INTEGER;
87 param.decimals->required = NO;
88 param.decimals->multiple = NO;
89 param.decimals->answer = "8";
90 param.decimals->options = "0-20";
91 param.decimals->description = _("Number of decimal places for floats");
92
93 param.null_val = G_define_standard_option(G_OPT_M_NULL_VALUE);
94 param.null_val->answer = "*";
95
96 param.header = G_define_flag();
97 param.header->key = 'h';
98 param.header->description = _("Suppress printing of header information");
99
100 param.row = G_define_flag();
101 param.row->key = 'r';
102 param.row->description =
103 _("Switch the row order in output from north->south to south->north");
104
105 param.depth = G_define_flag();
106 param.depth->key = 'd';
107 param.depth->description =
108 _("Switch the depth order in output from bottom->top to top->bottom");
109
110 param.grass6 = G_define_flag();
111 param.grass6->key = 'c';
112 param.grass6->description =
113 _("Print grass6 compatible format. Flags -d and -r are ignored.");
114
115 param.mask = G_define_flag();
116 param.mask->key = 'm';
117 param.mask->description =
118 _("Use 3D raster mask (if exists) with input map");
119 }
120
121 /*---------------------------------------------------------------------------*/
122
123 /* Set up the input and output file names from the user's responses
124 */
125 void getParams(char **input, char **output, int *decim)
126 {
127 *input = param.input->answer;
128 *output = param.output->answer;
129 sscanf(param.decimals->answer, "%d", decim);
130 }
131
132 /*---------------------------------------------------------------------------*/
133
134 /* This function is used to write parts of the header for the output
135 * ASCII file.
136 */
137 void writeHeaderString(FILE *fp, char *valueString, double value)
138 {
139 static char format[100];
140
141 snprintf(format, 100, "%s %%lf\n", valueString);
142 if (fprintf(fp, format, value) < 0)
143 fatalError("writeHeaderString: header value invalid");
144 }
145
146 /*---------------------------------------------------------------------------*/
147 void writeHeaderString2(FILE *fp, char *valueString, int value)
148 {
149 static char format[100];
150
151 snprintf(format, 100, "%s %%d\n", valueString);
152 if (fprintf(fp, format, value) < 0)
153 fatalError("writeHeaderString: header value invalid");
154 }
155
156 /*---------------------------------------------------------------------------*/
157 void writeHeaderString3(FILE *fp, char *valueString, const char *value)
158 {
159 static char format[100];
160
161 snprintf(format, 100, "%s %%s\n", valueString);
162 if (fprintf(fp, format, value) < 0)
163 fatalError("writeHeaderString: header value invalid");
164 }
165
166 /*---------------------------------------------------------------------------*/
167
168 /* Opens the output ascii file and writes the header.
169 * Returns the file handle for the output file.
170 */
171 FILE *openAscii(char *asciiFile, RASTER3D_Region region)
172 {
173 FILE *fp;
174
175 if (asciiFile) {
176 fp = fopen(asciiFile, "w");
177 if (fp == NULL) {
178 perror(asciiFile);
179 G_usage();
180 exit(EXIT_FAILURE);
181 }
182 }
183 else
184 fp = stdout;
185
186 if (!param.header->answer) {
187
188 /* Do not print the new header in grass compatibility mode */
189 if (!param.grass6->answer) {
190 /* Write the version information */
191 writeHeaderString3(fp, "version:", "grass7");
192 /* Write the row and depth order information */
193 if (!param.depth->answer && !param.row->answer)
194 writeHeaderString3(fp, "order:", "nsbt");
195 else if (param.depth->answer && !param.row->answer)
196 writeHeaderString3(fp, "order:", "nstb");
197 else if (!param.depth->answer && param.row->answer)
198 writeHeaderString3(fp, "order:", "snbt");
199 else if (param.depth->answer && param.row->answer)
200 writeHeaderString3(fp, "order:", "sntb");
201 }
202
203 writeHeaderString(fp, "north:", region.north);
204 writeHeaderString(fp, "south:", region.south);
205 writeHeaderString(fp, "east:", region.east);
206 writeHeaderString(fp, "west:", region.west);
207 writeHeaderString(fp, "top:", region.top);
208 writeHeaderString(fp, "bottom:", region.bottom);
209 writeHeaderString2(fp, "rows:", region.rows);
210 writeHeaderString2(fp, "cols:", region.cols);
211 writeHeaderString2(fp, "levels:", region.depths);
212 }
213
214 return fp;
215 }
216
217 /*---------------------------------------------------------------------------*/
218 /* This function does all the work. Basically, we just output the
219 * source G3d file one layer at a time.
220 *//* * */
221
222 void G3dToascii(FILE *fp, RASTER3D_Region region, int decim)
223 {
224 DCELL dvalue;
225 FCELL fvalue;
226 int x, y, z;
227 int rows, cols, depths, typeIntern;
228 int col, row, depth;
229
230 rows = region.rows;
231 cols = region.cols;
232 depths = region.depths;
233
234 typeIntern = Rast3d_tile_type_map(map);
235
236 for (z = 0; z < depths; z++) {
237 G_percent(z, depths, 1);
238 for (y = 0; y < rows; y++) { /* g3d rows count from south to north */
239 for (x = 0; x < cols; x++) {
240
241 /* From west to east */
242 col = x;
243 /* The default is to write rows from north to south
244 to be r.in.ascii compatible
245 */
246 row = y;
247 /* From bottom to the top */
248 depth = z;
249
250 /* Write rows from south to north */
251 if (param.row->answer)
252 row = rows - y - 1;
253
254 /* write XY layer from top to bottom */
255 if (param.depth->answer)
256 depth = depths - z - 1;
257
258 /* Get the data and resample if necessary */
259
260 if (typeIntern == FCELL_TYPE) {
261
262 Rast3d_get_value(map, col, row, depth, &fvalue, FCELL_TYPE);
263
264 if (Rast3d_is_null_value_num(&fvalue, FCELL_TYPE))
265 fprintf(fp, "%s ", param.null_val->answer);
266 else
267 fprintf(fp, "%.*f ", decim, fvalue);
268 }
269 else {
270
271 Rast3d_get_value(map, col, row, depth, &dvalue, DCELL_TYPE);
272
273 if (Rast3d_is_null_value_num(&dvalue, DCELL_TYPE))
274 fprintf(fp, "%s ", param.null_val->answer);
275 else
276 fprintf(fp, "%.*lf ", decim, dvalue);
277 }
278 }
279 fprintf(fp, "\n");
280 }
281 }
282 G_percent(1, 1, 1);
283 G_percent_reset();
284 }
285
286 /*---------------------------------------------------------------------------*/
287
288 /* Main function: open the input and output files, then call
289 * G3dtoascii.
290 */
291 int main(int argc, char *argv[])
292 {
293 char *input, *output;
294 int decim;
295 RASTER3D_Region region;
296 FILE *fp;
297 int changemask = 0;
298 struct GModule *module;
299
300 /* Initialize GRASS */
301 G_gisinit(argv[0]);
302 module = G_define_module();
303 G_add_keyword(_("raster3d"));
304 G_add_keyword(_("export"));
305 G_add_keyword(_("output"));
306 G_add_keyword(_("voxel"));
307 G_add_keyword(_("conversion"));
308 G_add_keyword("ASCII");
309 module->description =
310 _("Converts a 3D raster map layer into a ASCII text file.");
311
312 /* Get parameters from user */
313 setParams();
314
315 /* Have GRASS get inputs */
316 if (G_parser(argc, argv))
317 exit(EXIT_FAILURE);
318
319 /* Parse input parameters */
320 getParams(&input, &output, &decim);
321
322 if (param.grass6->answer) {
323 param.depth->answer = 0;
324 param.row->answer = 0;
325 }
326
327 if (NULL == G_find_raster3d(input, ""))
328 Rast3d_fatal_error(_("3D raster map <%s> not found"), input);
329
330 /* Initiate the default settings */
331 Rast3d_init_defaults();
332
333 /* Figure out the current region settings */
334 Rast3d_get_window(&region);
335
336 /* Open the map and use XY cache mode */
337 map = Rast3d_open_cell_old(input, G_find_raster3d(input, ""), &region,
338 RASTER3D_TILE_SAME_AS_FILE,
339 RASTER3D_USE_CACHE_DEFAULT);
340
341 if (map == NULL)
342 Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), input);
343
344 /* Open the output ascii file */
345 fp = openAscii(output, region);
346
347 /*if requested set the Mask on */
348 if (param.mask->answer) {
349 if (Rast3d_mask_file_exists()) {
350 changemask = 0;
351 if (Rast3d_mask_is_off(map)) {
352 Rast3d_mask_on(map);
353 changemask = 1;
354 }
355 }
356 }
357
358 /* Now barf out the contents of the map in ascii form */
359 G3dToascii(fp, region, decim);
360
361 /*We set the Mask off, if it was off bevor */
362 if (param.mask->answer) {
363 if (Rast3d_mask_file_exists())
364 if (Rast3d_mask_is_on(map) && changemask)
365 Rast3d_mask_off(map);
366 }
367
368 /* Close files and exit */
369 if (!Rast3d_close(map))
370 fatalError(_("Unable to close 3D raster map"));
371
372 if (output)
373 if (fclose(fp))
374 fatalError(_("Unable to close new ASCII file"));
375
376 return 0;
377 }
378
379 /*---------------------------------------------------------------------------*/