main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.out.ascii
  4. *
  5. * AUTHOR(S): Original author
  6. * Mark Astley, Bill Brown
  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/G3d.h>
  23. #include <grass/glocale.h>
  24. #define MAX(a,b) (a > b ? a : b)
  25. /* structs */
  26. typedef struct
  27. {
  28. struct Option *input, *output, *decimals, *null_val;
  29. struct Flag *header;
  30. struct Flag *mask;
  31. } paramType;
  32. /* protos */
  33. void fatalError(char *errorMsg);
  34. void setParams();
  35. void getParams(char **input, char **output, int *decim);
  36. void writeHeaderString(FILE * fp, char *valueString, double value);
  37. void writeHeaderString2(FILE * fp, char *valueString, int value);
  38. FILE *openAscii(char *asciiFile, G3D_Region region);
  39. void G3dToascii(FILE * fp, G3D_Region region, int decim);
  40. /* globals */
  41. void *map = NULL;
  42. paramType param;
  43. /*---------------------------------------------------------------------------*/
  44. /* Simple error handling routine, will eventually replace this with
  45. * G3D_fatalError.
  46. */
  47. void fatalError(char *errorMsg)
  48. {
  49. if (map != NULL) {
  50. /* should unopen map here! */
  51. if (!G3d_closeCell(map))
  52. fatalError(_("Error closing 3d raster map"));
  53. }
  54. G3d_fatalError(errorMsg);
  55. }
  56. /*---------------------------------------------------------------------------*/
  57. /* Convenient way to set up the arguments we are expecting
  58. */
  59. void setParams()
  60. {
  61. param.input = G_define_option();
  62. param.input->key = "input";
  63. param.input->type = TYPE_STRING;
  64. param.input->required = YES;
  65. param.input->gisprompt = "old,grid3,3d-raster";
  66. param.input->multiple = NO;
  67. param.input->description = _("3d raster map to be converted to ASCII");
  68. param.output = G_define_option();
  69. param.output->key = "output";
  70. param.output->type = TYPE_STRING;
  71. param.output->gisprompt = "new_file,file,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.mask = G_define_flag();
  92. param.mask->key = 'm';
  93. param.mask->description = _("Use G3D mask (if exists) with input map");
  94. }
  95. /*---------------------------------------------------------------------------*/
  96. /* Set up the input and output file names from the user's responses
  97. */
  98. void getParams(char **input, char **output, int *decim)
  99. {
  100. *input = param.input->answer;
  101. *output = param.output->answer;
  102. sscanf(param.decimals->answer, "%d", decim);
  103. }
  104. /*---------------------------------------------------------------------------*/
  105. /* This function is used to write parts of the header for the output
  106. * ASCII file.
  107. */
  108. void writeHeaderString(FILE * fp, char *valueString, double value)
  109. {
  110. static char format[100];
  111. sprintf(format, "%s %%lf\n", valueString);
  112. if (fprintf(fp, format, value) < 0)
  113. fatalError("writeHeaderString: header value invalid");
  114. }
  115. /*---------------------------------------------------------------------------*/
  116. void writeHeaderString2(FILE * fp, char *valueString, int value)
  117. {
  118. static char format[100];
  119. sprintf(format, "%s %%d\n", valueString);
  120. if (fprintf(fp, format, value) < 0)
  121. fatalError("writeHeaderString: header value invalid");
  122. }
  123. /*---------------------------------------------------------------------------*/
  124. /* Opens the output acsii file and writes the header.
  125. * Returns the file handle for the output file.
  126. */
  127. FILE *openAscii(char *asciiFile, G3D_Region region)
  128. {
  129. FILE *fp;
  130. if (asciiFile) {
  131. fp = fopen(asciiFile, "w");
  132. if (fp == NULL) {
  133. perror(asciiFile);
  134. G_usage();
  135. exit(EXIT_FAILURE);
  136. }
  137. }
  138. else
  139. fp = stdout;
  140. if (!param.header->answer) {
  141. writeHeaderString(fp, "north:", region.north);
  142. writeHeaderString(fp, "south:", region.south);
  143. writeHeaderString(fp, "east:", region.east);
  144. writeHeaderString(fp, "west:", region.west);
  145. writeHeaderString(fp, "top:", region.top);
  146. writeHeaderString(fp, "bottom:", region.bottom);
  147. writeHeaderString2(fp, "rows:", region.rows);
  148. writeHeaderString2(fp, "cols:", region.cols);
  149. writeHeaderString2(fp, "levels:", region.depths);
  150. }
  151. return fp;
  152. }
  153. /*---------------------------------------------------------------------------*/
  154. /* This function does all the work. Basically, we just output the
  155. * source G3d file one layer at a time.
  156. *//* * */
  157. void G3dToascii(FILE * fp, G3D_Region region, int decim)
  158. {
  159. double d1 = 0;
  160. double *d1p;
  161. float *f1p;
  162. int x, y, z;
  163. int rows, cols, depths, typeIntern;
  164. /*AV*/
  165. /* BEGIN OF ORIGINAL CODE */
  166. /*
  167. G3d_getCoordsMap (map, &rows, &cols, &depths);
  168. */
  169. /* END OF ORIGINAL CODE */
  170. /*AV*/
  171. /* BEGIN OF MY CODE */
  172. rows = region.rows;
  173. cols = region.cols;
  174. depths = region.depths;
  175. /* END OF MY CODE */
  176. typeIntern = G3d_tileTypeMap(map);
  177. d1p = &d1;
  178. f1p = (float *)&d1;
  179. for (z = 0; z < depths; z++)
  180. for (y = rows - 1; y >= 0; y--) { /* north to south */
  181. for (x = 0; x < cols; x++) {
  182. G3d_getValue(map, x, y, z, d1p, typeIntern);
  183. if (typeIntern == FCELL_TYPE) {
  184. if (G3d_isNullValueNum(f1p, FCELL_TYPE))
  185. fprintf(fp, "%s ", param.null_val->answer);
  186. else
  187. fprintf(fp, "%.*f ", decim, *f1p);
  188. }
  189. else {
  190. if (G3d_isNullValueNum(&d1, DCELL_TYPE))
  191. fprintf(fp, "%s ", param.null_val->answer);
  192. else
  193. fprintf(fp, "%.*lf ", decim, d1);
  194. }
  195. }
  196. fprintf(fp, "\n");
  197. }
  198. }
  199. /*---------------------------------------------------------------------------*/
  200. /* Main function: open the input and output files, then call
  201. * G3dtoascii.
  202. */
  203. int main(int argc, char *argv[])
  204. {
  205. char *input, *output;
  206. int decim;
  207. G3D_Region region;
  208. FILE *fp;
  209. int changemask = 0;
  210. struct GModule *module;
  211. /* Initialize GRASS */
  212. G_gisinit(argv[0]);
  213. module = G_define_module();
  214. G_add_keyword(_("raster3d"));
  215. G_add_keyword(_("voxel"));
  216. G_add_keyword(_("export"));
  217. module->description =
  218. _("Converts a 3D raster map layer into a ASCII text file.");
  219. /* Get parameters from user */
  220. setParams();
  221. /* Have GRASS get inputs */
  222. if (G_parser(argc, argv))
  223. exit(EXIT_FAILURE);
  224. /* Parse input parameters */
  225. getParams(&input, &output, &decim);
  226. if (NULL == G_find_grid3(input, ""))
  227. G3d_fatalError(_("Requested 3d raster map not found"));
  228. /* map = G3d_openCellOld(input, G_find_grid3(input, ""), G3D_DEFAULT_WINDOW,
  229. G3D_TILE_SAME_AS_FILE,
  230. G3D_NO_CACHE); */
  231. /* using cache mode due to bug */
  232. map = G3d_openCellOld(input, G_find_grid3(input, ""), G3D_DEFAULT_WINDOW,
  233. G3D_TILE_SAME_AS_FILE, G3D_USE_CACHE_DEFAULT);
  234. if (map == NULL)
  235. G3d_fatalError(_("Error opening 3d raster map"));
  236. /* Figure out the region from the map */
  237. /* G3d_getRegionStructMap(map, &region); */
  238. G3d_getWindow(&region);
  239. /* Open the output ascii file */
  240. fp = openAscii(output, region);
  241. /*if requested set the Mask on */
  242. if (param.mask->answer) {
  243. if (G3d_maskFileExists()) {
  244. changemask = 0;
  245. if (G3d_maskIsOff(map)) {
  246. G3d_maskOn(map);
  247. changemask = 1;
  248. }
  249. }
  250. }
  251. /* Now barf out the contents of the map in ascii form */
  252. G3dToascii(fp, region, decim);
  253. /*We set the Mask off, if it was off bevor */
  254. if (param.mask->answer) {
  255. if (G3d_maskFileExists())
  256. if (G3d_maskIsOn(map) && changemask)
  257. G3d_maskOff(map);
  258. }
  259. /* Close files and exit */
  260. if (!G3d_closeCell(map))
  261. fatalError(_("Error closing 3d raster map"));
  262. map = NULL;
  263. if (output)
  264. if (fclose(fp))
  265. fatalError(_("Error closing new ASCII file"));
  266. return 0;
  267. }
  268. /*---------------------------------------------------------------------------*/