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 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. #define MAX(a,b) (a > b ? a : b)
  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. /* protos */
  35. static void fatalError(char *errorMsg);
  36. static void setParams();
  37. static void getParams(char **input, char **output, int *decim);
  38. static void writeHeaderString(FILE * fp, char *valueString, double value);
  39. static void writeHeaderString2(FILE * fp, char *valueString, int value);
  40. static void writeHeaderString3(FILE * fp, char *valueString, const char* value);
  41. static FILE *openAscii(char *asciiFile, RASTER3D_Region region);
  42. static void G3dToascii(FILE * fp, RASTER3D_Region region, int decim);
  43. /* globals */
  44. void *map = NULL;
  45. paramType param;
  46. /*---------------------------------------------------------------------------*/
  47. /* Simple error handling routine, will eventually replace this with
  48. * RASTER3D_fatalError.
  49. */
  50. void fatalError(char *errorMsg)
  51. {
  52. if (map != NULL) {
  53. /* should unopen map here! */
  54. if (!Rast3d_close(map))
  55. fatalError(_("Unable to close 3D raster map"));
  56. }
  57. Rast3d_fatal_error(errorMsg);
  58. }
  59. /*---------------------------------------------------------------------------*/
  60. /* Convenient way to set up the arguments we are expecting
  61. */
  62. void setParams()
  63. {
  64. param.input = G_define_option();
  65. param.input->key = "input";
  66. param.input->type = TYPE_STRING;
  67. param.input->required = YES;
  68. param.input->gisprompt = "old,grid3,3d-raster";
  69. param.input->multiple = NO;
  70. param.input->description = _("3D raster map to be converted to ASCII");
  71. param.output = G_define_standard_option(G_OPT_F_OUTPUT);
  72. param.output->required = NO;
  73. param.output->description = _("Name for ASCII output file");
  74. param.decimals = G_define_option();
  75. param.decimals->key = "dp";
  76. param.decimals->type = TYPE_INTEGER;
  77. param.decimals->required = NO;
  78. param.decimals->multiple = NO;
  79. param.decimals->answer = "8";
  80. param.decimals->options = "0-20";
  81. param.decimals->description = _("Number of decimal places for floats");
  82. param.null_val = G_define_option();
  83. param.null_val->key = "null";
  84. param.null_val->type = TYPE_STRING;
  85. param.null_val->required = NO;
  86. param.null_val->description = _("Char string to represent no data cell");
  87. param.null_val->answer = "*";
  88. param.header = G_define_flag();
  89. param.header->key = 'h';
  90. param.header->description = _("Suppress printing of header information");
  91. param.row = G_define_flag();
  92. param.row->key = 'r';
  93. param.row->description = _("Switch the row order in output from north->south to south->north");
  94. param.depth = G_define_flag();
  95. param.depth->key = 'd';
  96. param.depth->description = _("Switch the depth order in output from bottom->top to top->bottom");
  97. param.grass6 = G_define_flag();
  98. param.grass6->key = 'c';
  99. param.grass6->description = _("Print grass6 compatible format. Flags -d and -r are ignored.");
  100. param.mask = G_define_flag();
  101. param.mask->key = 'm';
  102. param.mask->description = _("Use 3D raster mask (if exists) with input map");
  103. }
  104. /*---------------------------------------------------------------------------*/
  105. /* Set up the input and output file names from the user's responses
  106. */
  107. void getParams(char **input, char **output, int *decim)
  108. {
  109. *input = param.input->answer;
  110. *output = param.output->answer;
  111. sscanf(param.decimals->answer, "%d", decim);
  112. }
  113. /*---------------------------------------------------------------------------*/
  114. /* This function is used to write parts of the header for the output
  115. * ASCII file.
  116. */
  117. void writeHeaderString(FILE * fp, char *valueString, double value)
  118. {
  119. static char format[100];
  120. G_snprintf(format, 100, "%s %%lf\n", valueString);
  121. if (fprintf(fp, format, value) < 0)
  122. fatalError("writeHeaderString: header value invalid");
  123. }
  124. /*---------------------------------------------------------------------------*/
  125. void writeHeaderString2(FILE * fp, char *valueString, int value)
  126. {
  127. static char format[100];
  128. G_snprintf(format, 100, "%s %%d\n", valueString);
  129. if (fprintf(fp, format, value) < 0)
  130. fatalError("writeHeaderString: header value invalid");
  131. }
  132. /*---------------------------------------------------------------------------*/
  133. void writeHeaderString3(FILE * fp, char *valueString, const char* value)
  134. {
  135. static char format[100];
  136. G_snprintf(format, 100, "%s %%s\n", valueString);
  137. if (fprintf(fp, format, value) < 0)
  138. fatalError("writeHeaderString: header value invalid");
  139. }
  140. /*---------------------------------------------------------------------------*/
  141. /* Opens the output acsii file and writes the header.
  142. * Returns the file handle for the output file.
  143. */
  144. FILE *openAscii(char *asciiFile, RASTER3D_Region region)
  145. {
  146. FILE *fp;
  147. if (asciiFile) {
  148. fp = fopen(asciiFile, "w");
  149. if (fp == NULL) {
  150. perror(asciiFile);
  151. G_usage();
  152. exit(EXIT_FAILURE);
  153. }
  154. } else
  155. fp = stdout;
  156. if (!param.header->answer) {
  157. /* Do not print the new header in grass compatibility mode */
  158. if (!param.grass6->answer) {
  159. /* Write the version information */
  160. writeHeaderString3(fp, "version:", "grass7");
  161. /* Write the row and depth order information */
  162. if (!param.depth->answer && !param.row->answer)
  163. writeHeaderString3(fp, "order:", "nsbt");
  164. else if (param.depth->answer && !param.row->answer)
  165. writeHeaderString3(fp, "order:", "nstb");
  166. else if (!param.depth->answer && param.row->answer)
  167. writeHeaderString3(fp, "order:", "snbt");
  168. else if (param.depth->answer && param.row->answer)
  169. writeHeaderString3(fp, "order:", "sntb");
  170. }
  171. writeHeaderString(fp, "north:", region.north);
  172. writeHeaderString(fp, "south:", region.south);
  173. writeHeaderString(fp, "east:", region.east);
  174. writeHeaderString(fp, "west:", region.west);
  175. writeHeaderString(fp, "top:", region.top);
  176. writeHeaderString(fp, "bottom:", region.bottom);
  177. writeHeaderString2(fp, "rows:", region.rows);
  178. writeHeaderString2(fp, "cols:", region.cols);
  179. writeHeaderString2(fp, "levels:", region.depths);
  180. }
  181. return fp;
  182. }
  183. /*---------------------------------------------------------------------------*/
  184. /* This function does all the work. Basically, we just output the
  185. * source G3d file one layer at a time.
  186. *//* * */
  187. void G3dToascii(FILE * fp, RASTER3D_Region region, int decim)
  188. {
  189. DCELL dvalue;
  190. FCELL fvalue;
  191. int x, y, z;
  192. int rows, cols, depths, typeIntern;
  193. int col, row, depth;
  194. rows = region.rows;
  195. cols = region.cols;
  196. depths = region.depths;
  197. typeIntern = Rast3d_tile_type_map(map);
  198. for (z = 0; z < depths; z++) {
  199. G_percent(z, depths, 1);
  200. for (y = 0; y < rows; y++) { /* g3d rows count from south to north */
  201. for (x = 0; x < cols; x++) {
  202. /* From west to east */
  203. col = x;
  204. /* The default is to write rows from north to south
  205. to be r.in.ascii compatible
  206. */
  207. row = y;
  208. /* From bottom to the top */
  209. depth = z;
  210. /* Write rows from south to north */
  211. if (param.row->answer)
  212. row = rows - y - 1;
  213. /* write XY layer from top to bottom */
  214. if (param.depth->answer)
  215. depth = depths - z - 1;
  216. /* Get the data and resample if nessessary */
  217. if (typeIntern == FCELL_TYPE) {
  218. Rast3d_get_value(map, col, row, depth, &fvalue, FCELL_TYPE);
  219. if (Rast3d_is_null_value_num(&fvalue, FCELL_TYPE))
  220. fprintf(fp, "%s ", param.null_val->answer);
  221. else
  222. fprintf(fp, "%.*f ", decim, fvalue);
  223. } else {
  224. Rast3d_get_value(map, col, row, depth, &dvalue, DCELL_TYPE);
  225. if (Rast3d_is_null_value_num(&dvalue, DCELL_TYPE))
  226. fprintf(fp, "%s ", param.null_val->answer);
  227. else
  228. fprintf(fp, "%.*lf ", decim, dvalue);
  229. }
  230. }
  231. fprintf(fp, "\n");
  232. }
  233. }
  234. G_percent(1, 1, 1);
  235. G_percent_reset();
  236. }
  237. /*---------------------------------------------------------------------------*/
  238. /* Main function: open the input and output files, then call
  239. * G3dtoascii.
  240. */
  241. int main(int argc, char *argv[])
  242. {
  243. char *input, *output;
  244. int decim;
  245. RASTER3D_Region region;
  246. FILE *fp;
  247. int changemask = 0;
  248. struct GModule *module;
  249. /* Initialize GRASS */
  250. G_gisinit(argv[0]);
  251. module = G_define_module();
  252. G_add_keyword(_("raster3d"));
  253. G_add_keyword(_("voxel"));
  254. G_add_keyword(_("export"));
  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. /*---------------------------------------------------------------------------*/