main.c 13 KB

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