main.c 12 KB

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