main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_standard_option(G_OPT_R_INPUT);
  61. parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
  62. parm.output->required = NO;
  63. parm.output->description =
  64. _("Name for output ASCII grid map (use out=- for stdout)");
  65. parm.dp = G_define_option();
  66. parm.dp->key = "precision";
  67. parm.dp->type = TYPE_INTEGER;
  68. parm.dp->required = NO;
  69. parm.dp->description =
  70. _("Number of significant digits (floating point only)");
  71. parm.width = G_define_option();
  72. parm.width->key = "width";
  73. parm.width->type = TYPE_INTEGER;
  74. parm.width->required = NO;
  75. parm.width->description =
  76. _("Number of values printed before wrapping a line (only SURFER or MODFLOW format)");
  77. parm.null = G_define_standard_option(G_OPT_M_NULL_VALUE);
  78. parm.null->answer = "*";
  79. parm.null->description =
  80. _("String to represent null cell (GRASS grid only)");
  81. flag.noheader = G_define_flag();
  82. flag.noheader->key = 'h';
  83. flag.noheader->description = _("Suppress printing of header information");
  84. flag.surfer = G_define_flag();
  85. flag.surfer->key = 's';
  86. flag.surfer->description = _("Write SURFER (Golden Software) ASCII grid");
  87. flag.modflow = G_define_flag();
  88. flag.modflow->key = 'm';
  89. flag.modflow->description = _("Write MODFLOW (USGS) ASCII array");
  90. flag.int_out = G_define_flag();
  91. flag.int_out->key = 'i';
  92. flag.int_out->description = _("Force output of integer values");
  93. if (G_parser(argc, argv))
  94. exit(EXIT_FAILURE);
  95. if (parm.dp->answer) {
  96. if (sscanf(parm.dp->answer, "%d", &dp) != 1)
  97. G_fatal_error(_("Failed to interpret dp as an integer"));
  98. if (dp > 20 || dp < 0)
  99. G_fatal_error(_("dp has to be from 0 to 20"));
  100. }
  101. width = 10;
  102. if (parm.width->answer) {
  103. if (sscanf(parm.width->answer, "%d", &width) != 1)
  104. G_fatal_error(_("Failed to interpret width as an integer"));
  105. }
  106. null_str = parm.null->answer;
  107. if (flag.surfer->answer && flag.noheader->answer)
  108. G_fatal_error(_("Both -s and -h doesn't make sense"));
  109. if (flag.surfer->answer && flag.modflow->answer)
  110. G_fatal_error(_("Use -M or -s, not both"));
  111. name = parm.map->answer;
  112. /* open raster map */
  113. fd = Rast_open_old(name, "");
  114. map_type = Rast_get_map_type(fd);
  115. if (!flag.int_out->answer)
  116. out_type = map_type;
  117. else
  118. out_type = CELL_TYPE;
  119. if (!parm.dp->answer) {
  120. dp = 6;
  121. if (out_type == DCELL_TYPE)
  122. dp = 16;
  123. }
  124. nrows = Rast_window_rows();
  125. ncols = Rast_window_cols();
  126. /* open ascii file for writing or use stdout */
  127. if (parm.output->answer && strcmp("-", parm.output->answer) != 0) {
  128. if (NULL == (fp = fopen(parm.output->answer, "w")))
  129. G_fatal_error(_("Unable to open file <%s>"), parm.output->answer);
  130. }
  131. else
  132. fp = stdout;
  133. /* process the requested output format */
  134. if (flag.surfer->answer) {
  135. if (!flag.noheader->answer) {
  136. if (writeGSheader(fp, name))
  137. G_fatal_error(_("Unable to read fp range for <%s>"), name);
  138. }
  139. rc = write_GSGRID(fd, fp, nrows, ncols, out_type, dp, surfer_null_str,
  140. width);
  141. }
  142. else if (flag.modflow->answer) {
  143. if (!flag.noheader->answer)
  144. writeMFheader(fp, dp, width, out_type);
  145. rc = write_MODFLOW(fd, fp, nrows, ncols, out_type, dp, width);
  146. }
  147. else {
  148. if (!flag.noheader->answer)
  149. writeGRASSheader(fp);
  150. rc = write_GRASS(fd, fp, nrows, ncols, out_type, dp, null_str);
  151. }
  152. if (rc) {
  153. G_fatal_error(_("Read failed at row %d"), rc);
  154. }
  155. /* tidy up and go away */
  156. Rast_close(fd);
  157. fclose(fp);
  158. exit(EXIT_SUCCESS);
  159. }