main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /***************************************************************************
  2. *
  3. * MODULE: r3.in.ascii
  4. *
  5. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova, Bill Brown,
  6. * Lubos Mitas, Jaro Hofierka, Soeren Gebbert
  7. *
  8. * PURPOSE: Convert a 3D ASCII raster text file into a (binary) 3D raster map layer
  9. *
  10. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster3d.h>
  22. #include <grass/glocale.h>
  23. #define ROW_ORDER_NORTH_TO_SOUTH 1
  24. #define ROW_ORDER_SOUTH_TO_NORTH 2
  25. #define DEPTH_ORDER_BOTTOM_TO_TOP 3
  26. #define DEPTH_ORDER_TOP_TO_BOTTOM 4
  27. /*- prototypes --------------------------------------------------------------*/
  28. static void fatalError(char *errorMsg); /*Simple Error message */
  29. static void setParams(); /*Fill the paramType structure */
  30. /*Puts the userdefined parameters into easier handable variables */
  31. static void getParams(char **input, char **output, int *convertNull,
  32. char *nullValue);
  33. /*reads a g3d ascii-file headerfile-string */
  34. static void readHeaderString(FILE * fp, char *valueString, double *value);
  35. static FILE *openAscii(char *asciiFile, RASTER3D_Region * region); /*open the g3d ascii file */
  36. /*This function does all the work, it reads the values from the g3d ascii-file and put
  37. it into an g3d-map */
  38. static void asciiToG3d(FILE * fp, RASTER3D_Region * region, int convertNull,
  39. char *nullValue);
  40. /*---------------------------------------------------------------------------*/
  41. /* global variables */
  42. void *map = NULL;
  43. int rowOrder;
  44. int depthOrder;
  45. extern void *Rast3d_open_new_param();
  46. /*---------------------------------------------------------------------------*/
  47. void fatalError(char *errorMsg)
  48. {
  49. if (map != NULL) {
  50. /* should unopen map here! */
  51. Rast3d_close(map);
  52. }
  53. Rast3d_fatal_error("%s", errorMsg);
  54. }
  55. /*---------------------------------------------------------------------------*/
  56. typedef struct {
  57. struct Option *input, *output, *nv;
  58. } paramType;
  59. static paramType param;
  60. static void setParams()
  61. {
  62. param.input = G_define_standard_option(G_OPT_F_INPUT);
  63. param.input->required = YES;
  64. param.input->description = _("Name of input file to be imported");
  65. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  66. param.nv = G_define_standard_option(G_OPT_M_NULL_VALUE);
  67. param.nv->answer = "*";
  68. param.nv->description = /* TODO: '*' or 'none' in the msg ?? */
  69. _("String representing NULL value data cell (use 'none' if no such value)");
  70. }
  71. /*---------------------------------------------------------------------------*/
  72. void
  73. getParams(char **input, char **output, int *convertNull, char *nullValue)
  74. {
  75. *input = param.input->answer;
  76. *output = param.output->answer;
  77. *convertNull = (strcmp(param.nv->answer, "none") != 0);
  78. if (*convertNull)
  79. if (sscanf(param.nv->answer, "%s", nullValue) != 1)
  80. fatalError("getParams: NULL-value value invalid");
  81. G_debug(3, "getParams: input: %s, output: %s", *input, *output);
  82. }
  83. /*---------------------------------------------------------------------------*/
  84. void readHeaderString(FILE * fp, char *valueString, double *value)
  85. {
  86. static char format[100];
  87. char line_buff[1024];
  88. /* to avoid buffer overflows we use G_snprintf */
  89. G_snprintf(format, 100, "%s %%lf", valueString);
  90. G_getl2(line_buff, 1024, fp);
  91. if (sscanf(line_buff, format, value) != 1) {
  92. G_debug(3, "bad value for [%s]", valueString);
  93. fatalError("readHeaderString: header value invalid");
  94. }
  95. }
  96. /*---------------------------------------------------------------------------*/
  97. FILE *openAscii(char *asciiFile, RASTER3D_Region * region)
  98. {
  99. FILE *fp;
  100. double tmp;
  101. char buff[1024];
  102. char line_buff[1024];
  103. G_debug(3, "openAscii: opens the ascii file and reads the header");
  104. fp = fopen(asciiFile, "r");
  105. if (fp == NULL) {
  106. perror(asciiFile);
  107. G_usage();
  108. exit(EXIT_FAILURE);
  109. }
  110. /* Initialize the default order */
  111. rowOrder = ROW_ORDER_NORTH_TO_SOUTH;
  112. depthOrder = DEPTH_ORDER_BOTTOM_TO_TOP;
  113. /* Read the first line and check for grass version */
  114. G_getl2(line_buff, 1024, fp);
  115. /* First check for new ascii format*/
  116. if (sscanf(line_buff, "version: %s", buff) == 1) {
  117. G_message("Found version information: %s\n", buff);
  118. if (G_strcasecmp(buff, "grass7") == 0) {
  119. /* Parse the row and depth order */
  120. G_getl2(line_buff, 1024, fp);
  121. if (sscanf(line_buff, "order: %s", buff) != 1)
  122. fatalError("Unable to parse the row and depth order");
  123. if (G_strcasecmp(buff, "nsbt") == 0) {
  124. rowOrder = ROW_ORDER_NORTH_TO_SOUTH;
  125. depthOrder = DEPTH_ORDER_BOTTOM_TO_TOP;
  126. G_message("Found north -> south, bottom -> top order (nsbt)");
  127. }
  128. if (G_strcasecmp(buff, "snbt") == 0) {
  129. rowOrder = ROW_ORDER_SOUTH_TO_NORTH;
  130. depthOrder = DEPTH_ORDER_BOTTOM_TO_TOP;
  131. G_message("Found south -> north, bottom -> top order (snbt)");
  132. }
  133. if (G_strcasecmp(buff, "nstb") == 0) {
  134. rowOrder = ROW_ORDER_NORTH_TO_SOUTH;
  135. depthOrder = DEPTH_ORDER_TOP_TO_BOTTOM;
  136. G_message("Found north -> south, top -> bottom order (nstb)");
  137. }
  138. if (G_strcasecmp(buff, "sntb") == 0) {
  139. rowOrder = ROW_ORDER_SOUTH_TO_NORTH;
  140. depthOrder = DEPTH_ORDER_TOP_TO_BOTTOM;
  141. G_message("Found south -> north, top -> bottom order (sntb)");
  142. }
  143. } else {
  144. G_fatal_error(_("Unsupported GRASS version %s"), buff);
  145. }
  146. } else {
  147. /* Rewind the stream if no grass version info found */
  148. rewind(fp);
  149. }
  150. Rast3d_get_window(region);
  151. readHeaderString(fp, "north:", &(region->north));
  152. readHeaderString(fp, "south:", &(region->south));
  153. readHeaderString(fp, "east:", &(region->east));
  154. readHeaderString(fp, "west:", &(region->west));
  155. readHeaderString(fp, "top:", &(region->top));
  156. readHeaderString(fp, "bottom:", &(region->bottom));
  157. readHeaderString(fp, "rows:", &tmp);
  158. region->rows = (int) tmp;
  159. readHeaderString(fp, "cols:", &tmp);
  160. region->cols = (int) tmp;
  161. readHeaderString(fp, "levels:", &tmp);
  162. region->depths = (int) tmp;
  163. return fp;
  164. }
  165. /*---------------------------------------------------------------------------*/
  166. #undef MAX
  167. #define MAX(a,b) (a > b ? a : b)
  168. void
  169. asciiToG3d(FILE * fp, RASTER3D_Region * region, int convertNull, char *nullValue)
  170. {
  171. int x, y, z;
  172. int col, row, depth;
  173. double value;
  174. char buff[256];
  175. int tileX, tileY, tileZ;
  176. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  177. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  178. Rast3d_autolock_on(map);
  179. Rast3d_unlock_all(map);
  180. G_message(_("Loading data ... (%dx%dx%d)"), region->cols, region->rows,
  181. region->depths);
  182. G_debug(3,
  183. "asciiToG3d: writing the 3D raster map, with rows %i cols %i depths %i",
  184. region->rows, region->cols, region->depths);
  185. for (z = 0; z < region->depths; z++) {
  186. G_percent(z, region->depths, 1);
  187. if ((z % tileZ) == 0)
  188. Rast3d_unlock_all(map);
  189. for (y = 0; y < region->rows; y++) /* go south to north */
  190. for (x = 0; x < region->cols; x++) {
  191. /* From west to east */
  192. col = x;
  193. /* The default is to read rows from north to south */
  194. row = y;
  195. /* From bottom to the top */
  196. depth = z;
  197. /* Read rows as from south to north */
  198. if (rowOrder == ROW_ORDER_SOUTH_TO_NORTH)
  199. row = region->rows - y - 1;
  200. /* Read XY layer from top to bottom */
  201. if (depthOrder == DEPTH_ORDER_TOP_TO_BOTTOM)
  202. depth = region->depths - z - 1;
  203. if (fscanf(fp, "%s", buff) != 1) {
  204. if (feof(fp))
  205. G_warning(_("End of file reached while still loading data."));
  206. G_debug(3,
  207. "missing data at col=%d row=%d depth=%d last_value=[%.4f]",
  208. x + 1, y + 1, z + 1, value);
  209. fatalError("asciiToG3d: read failed");
  210. }
  211. /* Check for null value */
  212. if (convertNull && strncmp(buff, nullValue, strlen(nullValue)) == 0) {
  213. Rast3d_set_null_value(&value, 1, DCELL_TYPE);
  214. } else {
  215. if (sscanf(buff, "%lf", &value) != 1) {
  216. G_warning(_("Invalid value detected"));
  217. G_debug(1, "invalid value at col=%d row=%d depth=%d last_value=[%s]",
  218. x + 1, y + 1, z + 1, buff);
  219. fatalError("asciiToG3d: read failed");
  220. }
  221. }
  222. /* Write the data */
  223. Rast3d_put_double(map, col, row, depth, value);
  224. }
  225. }
  226. if (fscanf(fp, "%lf", &value) == 1) {
  227. G_warning(_("Data exists in input file after fully importing "
  228. "expected data. [%.4f ...]"), value);
  229. }
  230. if (!Rast3d_flush_all_tiles(map))
  231. fatalError("asciiTog3d: error flushing tiles");
  232. Rast3d_autolock_off(map);
  233. Rast3d_unlock_all(map);
  234. G_percent(1, 1, 1);
  235. }
  236. /*---------------------------------------------------------------------------*/
  237. int main(int argc, char *argv[])
  238. {
  239. char *input, *output;
  240. int convertNull;
  241. char nullValue[256];
  242. int useTypeDefault, type, useCompressionDefault, doCompression;
  243. int usePrecisionDefault, precision, useDimensionDefault, tileX, tileY,
  244. tileZ;
  245. RASTER3D_Region region;
  246. FILE *fp;
  247. struct GModule *module;
  248. struct History history;
  249. map = NULL;
  250. G_gisinit(argv[0]);
  251. module = G_define_module();
  252. G_add_keyword(_("raster3d"));
  253. G_add_keyword(_("import"));
  254. G_add_keyword(_("voxel"));
  255. G_add_keyword(_("conversion"));
  256. G_add_keyword("ASCII");
  257. module->description =
  258. _("Converts a 3D ASCII raster text file into a (binary) 3D raster map.");
  259. setParams();
  260. Rast3d_set_standard3d_input_params();
  261. if (G_parser(argc, argv))
  262. exit(EXIT_FAILURE);
  263. getParams(&input, &output, &convertNull, nullValue);
  264. if (!Rast3d_get_standard3d_params(&useTypeDefault, &type,
  265. &useCompressionDefault, &doCompression,
  266. &usePrecisionDefault, &precision,
  267. &useDimensionDefault, &tileX, &tileY,
  268. &tileZ))
  269. fatalError("Error getting standard parameters");
  270. Rast3d_init_defaults();
  271. fp = openAscii(input, &region);
  272. /*Open the new RASTER3D map */
  273. map = Rast3d_open_new_param(output, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_XY,
  274. &region,
  275. type, doCompression, precision, tileX, tileY,
  276. tileZ);
  277. if (map == NULL)
  278. fatalError(_("Unable to open 3D raster map"));
  279. /*Create the new RASTER3D Map */
  280. asciiToG3d(fp, &region, convertNull, nullValue);
  281. if (!Rast3d_close(map))
  282. fatalError(_("Unable to close 3D raster map"));
  283. /* write input name to map history */
  284. Rast3d_read_history(output, G_mapset(), &history);
  285. Rast_command_history(&history);
  286. Rast_set_history(&history, HIST_DATSRC_1, input);
  287. Rast3d_write_history(output, &history);
  288. map = NULL;
  289. if (fclose(fp))
  290. fatalError(_("Unable to close ASCII file"));
  291. return EXIT_SUCCESS;
  292. }