main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_standard_option(G_OPT_R_TYPE);
  72. parm.type->required = NO;
  73. parm.type->description = _("Default: CELL for integer values, DCELL for floating-point values");
  74. parm.title = G_define_option();
  75. parm.title->key = "title";
  76. parm.title->key_desc = "phrase";
  77. parm.title->type = TYPE_STRING;
  78. parm.title->required = NO;
  79. parm.title->description = _("Title for resultant raster map");
  80. parm.mult = G_define_option();
  81. parm.mult->key = "multiplier";
  82. parm.mult->type = TYPE_DOUBLE;
  83. parm.mult->description = _("Default: read from header");
  84. parm.mult->required = NO;
  85. parm.mult->label = _("Multiplier for ASCII data");
  86. parm.nv = G_define_standard_option(G_OPT_M_NULL_VALUE);
  87. parm.nv->description = _("Default: read from header");
  88. parm.nv->label = _("String representing NULL value data cell");
  89. parm.nv->guisection = _("NULL data");
  90. flag.s = G_define_flag();
  91. flag.s->key = 's';
  92. flag.s->description =
  93. _("SURFER (Golden Software) ASCII file will be imported");
  94. if (G_parser(argc, argv))
  95. exit(EXIT_FAILURE);
  96. input = parm.input->answer;
  97. output = parm.output->answer;
  98. temp = G_tempfile();
  99. ft = fopen(temp, "w+");
  100. if (ft == NULL)
  101. G_fatal_error(_("Unable to open temporary file <%s>"), temp);
  102. if ((title = parm.title->answer))
  103. G_strip(title);
  104. if (!parm.mult->answer)
  105. Rast_set_d_null_value(&mult, 1);
  106. else if ((sscanf(parm.mult->answer, "%lf", &mult)) != 1)
  107. G_fatal_error(_("Wrong entry for multiplier: %s"), parm.mult->answer);
  108. null_val_str = parm.nv->answer;
  109. data_type = -1;
  110. if (parm.type->answer) {
  111. switch(parm.type->answer[0]) {
  112. case 'C':
  113. data_type = CELL_TYPE;
  114. break;
  115. case 'F':
  116. data_type = FCELL_TYPE;
  117. break;
  118. case 'D':
  119. data_type = DCELL_TYPE;
  120. break;
  121. }
  122. }
  123. if (strcmp(input, "-") == 0) {
  124. Tmp_file = G_tempfile();
  125. if (NULL == (Tmp_fd = fopen(Tmp_file, "w+")))
  126. G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file);
  127. unlink(Tmp_file);
  128. if (0 > file_cpy(stdin, Tmp_fd))
  129. G_fatal_error(_("Unable to read input from stdin"));
  130. fd = Tmp_fd;
  131. }
  132. else
  133. fd = fopen(input, "r");
  134. if (fd == NULL) {
  135. G_fatal_error(_("Unable to read input from <%s>"), input);
  136. }
  137. direction = 1;
  138. sz = 0;
  139. if (flag.s->answer) {
  140. sz = getgrdhead(fd, &cellhd);
  141. /* for Surfer files, the data type is always FCELL_TYPE,
  142. the multiplier and the null_val_str are never used */
  143. data_type = FCELL_TYPE;
  144. mult = 1.;
  145. null_val_str = "";
  146. /* rows in surfer files are ordered from bottom to top,
  147. opposite of normal GRASS ordering */
  148. direction = -1;
  149. }
  150. else
  151. sz = gethead(fd, &cellhd, &data_type, &mult, &null_val_str);
  152. if (!sz)
  153. G_fatal_error(_("Can't get cell header"));
  154. nrows = cellhd.rows;
  155. ncols = cellhd.cols;
  156. Rast_set_window(&cellhd);
  157. if (nrows != Rast_window_rows())
  158. G_fatal_error(_("OOPS: rows changed from %d to %d"), nrows,
  159. Rast_window_rows());
  160. if (ncols != Rast_window_cols())
  161. G_fatal_error(_("OOPS: cols changed from %d to %d"), ncols,
  162. Rast_window_cols());
  163. rast_ptr = Rast_allocate_buf(data_type);
  164. rast = rast_ptr;
  165. cf = Rast_open_new(output, data_type);
  166. for (row = 0; row < nrows; row++) {
  167. G_percent(row, nrows, 2);
  168. for (col = 0; col < ncols; col++) {
  169. if (fscanf(fd, "%s", y) != 1) {
  170. Rast_unopen(cf);
  171. G_fatal_error(_("Data conversion failed at row %d, col %d"),
  172. row + 1, col + 1);
  173. }
  174. if (strcmp(y, null_val_str)) {
  175. x = atof(y);
  176. if ((float)x == GS_BLANK) {
  177. Rast_set_null_value(rast_ptr, 1, data_type);
  178. }
  179. else {
  180. Rast_set_d_value(rast_ptr,
  181. (DCELL) (x * mult), data_type);
  182. }
  183. }
  184. else {
  185. Rast_set_null_value(rast_ptr, 1, data_type);
  186. }
  187. rast_ptr = G_incr_void_ptr(rast_ptr, Rast_cell_size(data_type));
  188. }
  189. fwrite(rast, Rast_cell_size(data_type), ncols, ft);
  190. rast_ptr = rast;
  191. }
  192. G_percent(nrows, nrows, 2);
  193. G_debug(1, "Creating support files for %s", output);
  194. sz = 0;
  195. if (direction < 0) {
  196. sz = -ncols * Rast_cell_size(data_type);
  197. G_fseek(ft, sz, SEEK_END);
  198. sz *= 2;
  199. }
  200. else {
  201. G_fseek(ft, 0L, SEEK_SET);
  202. }
  203. for (row = 0; row < nrows; row += 1) {
  204. fread(rast, Rast_cell_size(data_type), ncols, ft);
  205. Rast_put_row(cf, rast, data_type);
  206. G_fseek(ft, sz, SEEK_CUR);
  207. }
  208. fclose(ft);
  209. unlink(temp);
  210. Rast_close(cf);
  211. if (title)
  212. Rast_put_cell_title(output, title);
  213. Rast_short_history(output, "raster", &history);
  214. Rast_command_history(&history);
  215. Rast_write_history(output, &history);
  216. G_done_msg(" ");
  217. exit(EXIT_SUCCESS);
  218. }
  219. static int file_cpy(FILE * from, FILE * to)
  220. {
  221. char buf[BUFSIZ];
  222. size_t size;
  223. int written = 0;
  224. while (1) {
  225. size = fread(buf, 1, BUFSIZ, from);
  226. if (!size) {
  227. if (written) {
  228. fflush(to);
  229. G_fseek(to, 0L, SEEK_SET);
  230. }
  231. return (0);
  232. }
  233. if (!fwrite(buf, 1, size, to)) {
  234. G_warning(_("Unable to write to file"));
  235. return (-1);
  236. }
  237. written = 1;
  238. }
  239. /* NOTREACHED */
  240. return -1;
  241. }