main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /****************************************************************************
  2. *
  3. * MODULE: r.in.ascii
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>, Hamish Bowman <hamish_b yahoo.com>,
  6. * Roberto Flor <flor itc.it>, Roger Miller <rgrmill rt66 com>,
  7. * Brad Douglas <rez touchofmadness.com>, Huidae Cho <grass4u gmail.com>,
  8. * Glynn Clements <glynn gclements.plus.com>
  9. * Jachym Cepicky <jachym les-ejk.cz>, Jan-Oliver Wagner <jan intevation.de>,
  10. * Justin Hickey <jhickey hpcc.nectec.or.th>
  11. * PURPOSE: Import ASCII or SURFER files
  12. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include <grass/glocale.h>
  25. #include "local_proto.h"
  26. FILE *Tmp_fd = NULL;
  27. char *Tmp_file = NULL;
  28. const float GS_BLANK = 1.70141E+038;
  29. static int file_cpy(FILE *, FILE *);
  30. int main(int argc, char *argv[])
  31. {
  32. char *input;
  33. char *output;
  34. char *title;
  35. char *temp;
  36. FILE *fd, *ft;
  37. int cf, direction, sz;
  38. struct Cell_head cellhd;
  39. struct History history;
  40. void *rast, *rast_ptr;
  41. int row, col;
  42. int nrows, ncols;
  43. double x;
  44. char y[128];
  45. struct GModule *module;
  46. struct
  47. {
  48. struct Option *input, *output, *title, *mult, *nv, *type;
  49. } parm;
  50. struct
  51. {
  52. struct Flag *s;
  53. } flag;
  54. char *null_val_str;
  55. DCELL mult;
  56. RASTER_MAP_TYPE data_type;
  57. double atof();
  58. G_gisinit(argv[0]);
  59. module = G_define_module();
  60. G_add_keyword(_("raster"));
  61. G_add_keyword(_("import"));
  62. G_add_keyword(_("conversion"));
  63. G_add_keyword("ASCII");
  64. module->description =
  65. _("Converts a GRASS ASCII raster file to binary raster map.");
  66. parm.input = G_define_standard_option(G_OPT_F_INPUT);
  67. parm.input->label =
  68. _("Name of input file to be imported");
  69. parm.input->description = _("-' for standard input");
  70. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  71. parm.type = G_define_option();
  72. parm.type->key = "type";
  73. parm.type->type = TYPE_STRING;
  74. parm.type->required = NO;
  75. parm.type->options = "CELL,FCELL,DCELL";
  76. parm.type->label = _("Storage type for resultant raster map");
  77. parm.type->description = _("Default: CELL for integer values, DCELL for floating-point values");
  78. parm.title = G_define_option();
  79. parm.title->key = "title";
  80. parm.title->key_desc = "phrase";
  81. parm.title->type = TYPE_STRING;
  82. parm.title->required = NO;
  83. parm.title->description = _("Title for resultant raster map");
  84. parm.mult = G_define_option();
  85. parm.mult->key = "mult";
  86. parm.mult->type = TYPE_DOUBLE;
  87. parm.mult->description = _("Default: read from header");
  88. parm.mult->required = NO;
  89. parm.mult->label = _("Multiplier for ASCII data");
  90. parm.nv = G_define_option();
  91. parm.nv->key = "nv";
  92. parm.nv->type = TYPE_STRING;
  93. parm.nv->required = NO;
  94. parm.nv->multiple = NO;
  95. parm.nv->description = _("Default: read from header");
  96. parm.nv->label = _("String representing NULL value data cell");
  97. parm.nv->guisection = _("NULL data");
  98. flag.s = G_define_flag();
  99. flag.s->key = 's';
  100. flag.s->description =
  101. _("SURFER (Golden Software) ASCII file will be imported");
  102. if (G_parser(argc, argv))
  103. exit(EXIT_FAILURE);
  104. input = parm.input->answer;
  105. output = parm.output->answer;
  106. temp = G_tempfile();
  107. ft = fopen(temp, "w+");
  108. if (ft == NULL)
  109. G_fatal_error(_("Unable to open temporary file <%s>"), temp);
  110. if ((title = parm.title->answer))
  111. G_strip(title);
  112. if (!parm.mult->answer)
  113. Rast_set_d_null_value(&mult, 1);
  114. else if ((sscanf(parm.mult->answer, "%lf", &mult)) != 1)
  115. G_fatal_error(_("Wrong entry for multiplier: %s"), parm.mult->answer);
  116. null_val_str = parm.nv->answer;
  117. data_type = -1;
  118. if (parm.type->answer) {
  119. switch(parm.type->answer[0]) {
  120. case 'C':
  121. data_type = CELL_TYPE;
  122. break;
  123. case 'F':
  124. data_type = FCELL_TYPE;
  125. break;
  126. case 'D':
  127. data_type = DCELL_TYPE;
  128. break;
  129. }
  130. }
  131. if (strcmp(input, "-") == 0) {
  132. Tmp_file = G_tempfile();
  133. if (NULL == (Tmp_fd = fopen(Tmp_file, "w+")))
  134. G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file);
  135. unlink(Tmp_file);
  136. if (0 > file_cpy(stdin, Tmp_fd))
  137. G_fatal_error(_("Unable to read input from stdin"));
  138. fd = Tmp_fd;
  139. }
  140. else
  141. fd = fopen(input, "r");
  142. if (fd == NULL) {
  143. G_fatal_error(_("Unable to read input from <%s>"), input);
  144. }
  145. direction = 1;
  146. sz = 0;
  147. if (flag.s->answer) {
  148. sz = getgrdhead(fd, &cellhd);
  149. /* for Surfer files, the data type is always FCELL_TYPE,
  150. the multiplier and the null_val_str are never used */
  151. data_type = FCELL_TYPE;
  152. mult = 1.;
  153. null_val_str = "";
  154. /* rows in surfer files are ordered from bottom to top,
  155. opposite of normal GRASS ordering */
  156. direction = -1;
  157. }
  158. else
  159. sz = gethead(fd, &cellhd, &data_type, &mult, &null_val_str);
  160. if (!sz)
  161. G_fatal_error(_("Can't get cell header"));
  162. nrows = cellhd.rows;
  163. ncols = cellhd.cols;
  164. Rast_set_window(&cellhd);
  165. if (nrows != Rast_window_rows())
  166. G_fatal_error(_("OOPS: rows changed from %d to %d"), nrows,
  167. Rast_window_rows());
  168. if (ncols != Rast_window_cols())
  169. G_fatal_error(_("OOPS: cols changed from %d to %d"), ncols,
  170. Rast_window_cols());
  171. rast_ptr = Rast_allocate_buf(data_type);
  172. rast = rast_ptr;
  173. cf = Rast_open_new(output, data_type);
  174. for (row = 0; row < nrows; row++) {
  175. G_percent(row, nrows, 2);
  176. for (col = 0; col < ncols; col++) {
  177. if (fscanf(fd, "%s", y) != 1) {
  178. Rast_unopen(cf);
  179. G_fatal_error(_("Data conversion failed at row %d, col %d"),
  180. row + 1, col + 1);
  181. }
  182. if (strcmp(y, null_val_str)) {
  183. x = atof(y);
  184. if ((float)x == GS_BLANK) {
  185. Rast_set_null_value(rast_ptr, 1, data_type);
  186. }
  187. else {
  188. Rast_set_d_value(rast_ptr,
  189. (DCELL) (x * mult), data_type);
  190. }
  191. }
  192. else {
  193. Rast_set_null_value(rast_ptr, 1, data_type);
  194. }
  195. rast_ptr = G_incr_void_ptr(rast_ptr, Rast_cell_size(data_type));
  196. }
  197. fwrite(rast, Rast_cell_size(data_type), ncols, ft);
  198. rast_ptr = rast;
  199. }
  200. G_percent(nrows, nrows, 2);
  201. G_debug(1, "Creating support files for %s", output);
  202. sz = 0;
  203. if (direction < 0) {
  204. sz = -ncols * Rast_cell_size(data_type);
  205. G_fseek(ft, sz, SEEK_END);
  206. sz *= 2;
  207. }
  208. else {
  209. G_fseek(ft, 0L, SEEK_SET);
  210. }
  211. for (row = 0; row < nrows; row += 1) {
  212. fread(rast, Rast_cell_size(data_type), ncols, ft);
  213. Rast_put_row(cf, rast, data_type);
  214. G_fseek(ft, sz, SEEK_CUR);
  215. }
  216. fclose(ft);
  217. unlink(temp);
  218. Rast_close(cf);
  219. if (title)
  220. Rast_put_cell_title(output, title);
  221. Rast_short_history(output, "raster", &history);
  222. Rast_command_history(&history);
  223. Rast_write_history(output, &history);
  224. G_done_msg(" ");
  225. exit(EXIT_SUCCESS);
  226. }
  227. static int file_cpy(FILE * from, FILE * to)
  228. {
  229. char buf[BUFSIZ];
  230. size_t size;
  231. int written = 0;
  232. while (1) {
  233. size = fread(buf, 1, BUFSIZ, from);
  234. if (!size) {
  235. if (written) {
  236. fflush(to);
  237. G_fseek(to, 0L, SEEK_SET);
  238. }
  239. return (0);
  240. }
  241. if (!fwrite(buf, 1, size, to)) {
  242. G_warning(_("Unable to write to file"));
  243. return (-1);
  244. }
  245. written = 1;
  246. }
  247. /* NOTREACHED */
  248. return -1;
  249. }