main.c 7.4 KB

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