main.c 12 KB

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