main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. *
  3. ****************************************************************************
  4. *
  5. * MODULE: r.out.ascii
  6. * AUTHOR(S): Michael Shapiro
  7. * Markus Neteler: added SURFER support
  8. * Roger Miller added MODFLOW support and organization
  9. * PURPOSE: r.out.ascii: writes ASCII GRID file
  10. * COPYRIGHT: (C) 2000 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. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <string.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include "localproto.h"
  25. #include <grass/glocale.h>
  26. int main(int argc, char *argv[])
  27. {
  28. RASTER_MAP_TYPE out_type, map_type;
  29. char *name;
  30. char *null_str;
  31. char surfer_null_str[13] = { "1.70141e+038" };
  32. int fd;
  33. int nrows, ncols, dp, width;
  34. int rc;
  35. FILE *fp;
  36. struct GModule *module;
  37. struct
  38. {
  39. struct Option *map;
  40. struct Option *output;
  41. struct Option *dp;
  42. struct Option *width;
  43. struct Option *null;
  44. } parm;
  45. struct
  46. {
  47. struct Flag *noheader;
  48. struct Flag *surfer;
  49. struct Flag *modflow;
  50. struct Flag *int_out;
  51. } flag;
  52. G_gisinit(argv[0]);
  53. module = G_define_module();
  54. G_add_keyword(_("raster"));
  55. G_add_keyword(_("export"));
  56. G_add_keyword("ASCII");
  57. module->description =
  58. _("Converts a raster map layer into a GRASS ASCII text file.");
  59. /* Define the different options */
  60. parm.map = G_define_option();
  61. parm.map->key = "input";
  62. parm.map->type = TYPE_STRING;
  63. parm.map->required = YES;
  64. parm.map->gisprompt = "old,cell,raster";
  65. parm.map->description = _("Name of an existing raster map");
  66. parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
  67. parm.output->required = NO;
  68. parm.output->description =
  69. _("Name for output ASCII grid map (use out=- for stdout)");
  70. parm.dp = G_define_option();
  71. parm.dp->key = "dp";
  72. parm.dp->type = TYPE_INTEGER;
  73. parm.dp->required = NO;
  74. parm.dp->description =
  75. _("Number of significant digits (floating point only)");
  76. parm.width = G_define_option();
  77. parm.width->key = "width";
  78. parm.width->type = TYPE_INTEGER;
  79. parm.width->required = NO;
  80. parm.width->description =
  81. _("Number of values printed before wrapping a line (only SURFER or MODFLOW format)");
  82. parm.null = G_define_option();
  83. parm.null->key = "null";
  84. parm.null->type = TYPE_STRING;
  85. parm.null->required = NO;
  86. parm.null->answer = "*";
  87. parm.null->description =
  88. _("String to represent null cell (GRASS grid only)");
  89. flag.noheader = G_define_flag();
  90. flag.noheader->key = 'h';
  91. flag.noheader->description = _("Suppress printing of header information");
  92. flag.surfer = G_define_flag();
  93. flag.surfer->key = 's';
  94. flag.surfer->description = _("Write SURFER (Golden Software) ASCII grid");
  95. flag.modflow = G_define_flag();
  96. flag.modflow->key = 'm';
  97. flag.modflow->description = _("Write MODFLOW (USGS) ASCII array");
  98. flag.int_out = G_define_flag();
  99. flag.int_out->key = 'i';
  100. flag.int_out->description = _("Force output of integer values");
  101. if (G_parser(argc, argv))
  102. exit(EXIT_FAILURE);
  103. if (parm.dp->answer) {
  104. if (sscanf(parm.dp->answer, "%d", &dp) != 1)
  105. G_fatal_error(_("Failed to interpret dp as an integer"));
  106. if (dp > 20 || dp < 0)
  107. G_fatal_error(_("dp has to be from 0 to 20"));
  108. }
  109. width = 10;
  110. if (parm.width->answer) {
  111. if (sscanf(parm.width->answer, "%d", &width) != 1)
  112. G_fatal_error(_("Failed to interpret width as an integer"));
  113. }
  114. null_str = parm.null->answer;
  115. if (flag.surfer->answer && flag.noheader->answer)
  116. G_fatal_error(_("Both -s and -h doesn't make sense"));
  117. if (flag.surfer->answer && flag.modflow->answer)
  118. G_fatal_error(_("Use -M or -s, not both"));
  119. name = parm.map->answer;
  120. /* open raster map */
  121. fd = Rast_open_old(name, "");
  122. map_type = Rast_get_map_type(fd);
  123. if (!flag.int_out->answer)
  124. out_type = map_type;
  125. else
  126. out_type = CELL_TYPE;
  127. if (!parm.dp->answer) {
  128. dp = 6;
  129. if (out_type == DCELL_TYPE)
  130. dp = 16;
  131. }
  132. nrows = Rast_window_rows();
  133. ncols = Rast_window_cols();
  134. /* open ascii file for writing or use stdout */
  135. if (parm.output->answer && strcmp("-", parm.output->answer) != 0) {
  136. if (NULL == (fp = fopen(parm.output->answer, "w")))
  137. G_fatal_error(_("Unable to open file <%s>"), parm.output->answer);
  138. }
  139. else
  140. fp = stdout;
  141. /* process the requested output format */
  142. if (flag.surfer->answer) {
  143. if (!flag.noheader->answer) {
  144. if (writeGSheader(fp, name))
  145. G_fatal_error(_("Unable to read fp range for <%s>"), name);
  146. }
  147. rc = write_GSGRID(fd, fp, nrows, ncols, out_type, dp, surfer_null_str,
  148. width);
  149. }
  150. else if (flag.modflow->answer) {
  151. if (!flag.noheader->answer)
  152. writeMFheader(fp, dp, width, out_type);
  153. rc = write_MODFLOW(fd, fp, nrows, ncols, out_type, dp, width);
  154. }
  155. else {
  156. if (!flag.noheader->answer)
  157. writeGRASSheader(fp);
  158. rc = write_GRASS(fd, fp, nrows, ncols, out_type, dp, null_str);
  159. }
  160. if (rc) {
  161. G_fatal_error(_("Read failed at row %d"), rc);
  162. }
  163. /* tidy up and go away */
  164. Rast_close(fd);
  165. fclose(fp);
  166. exit(EXIT_SUCCESS);
  167. }