parse.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <grass/glocale.h>
  5. #include "global.h"
  6. int parse_command_line(int argc, char *argv[])
  7. {
  8. int i;
  9. char *desc;
  10. struct
  11. {
  12. struct Option *cell;
  13. struct Option *units;
  14. struct Option *pl; /* page length */
  15. struct Option *pw; /* page width */
  16. struct Option *outfile;
  17. struct Option *nv;
  18. struct Option *nsteps;
  19. struct Option *sort;
  20. } parms;
  21. struct
  22. {
  23. struct Flag *f;
  24. struct Flag *m;
  25. struct Flag *h;
  26. struct Flag *q;
  27. struct Flag *e;
  28. struct Flag *n;
  29. struct Flag *N;
  30. struct Flag *i; /* use quant rules for fp map,
  31. i.e. read it as int */
  32. struct Flag *C; /* report for fp ranges in Cats file
  33. (fp maps only) */
  34. } flags;
  35. parms.cell = G_define_standard_option(G_OPT_R_MAPS);
  36. parms.cell->description = _("Name of raster map(s) to report on");
  37. parms.units = G_define_option();
  38. parms.units->key = "units";
  39. parms.units->type = TYPE_STRING;
  40. parms.units->required = NO;
  41. parms.units->multiple = YES;
  42. parms.units->description = _("Units to report");
  43. desc = NULL;
  44. G_asprintf(&desc,
  45. "mi;%s;me;%s;k;%s;a;%s;h;%s;c;%s;p;%s",
  46. _("area in square miles"),
  47. _("area in square meters"),
  48. _("area in square kilometers"),
  49. _("area in acres"),
  50. _("area in hectares"),
  51. _("number of cells"),
  52. _("percent cover"));
  53. parms.units->descriptions = desc;
  54. parms.units->options = "mi,me,k,a,h,c,p";
  55. parms.units->guisection = _("Statistics");
  56. parms.outfile = G_define_standard_option(G_OPT_F_OUTPUT);
  57. parms.outfile->required = NO;
  58. parms.outfile->label =
  59. _("Name for output file to hold the report");
  60. parms.outfile->description =
  61. _("If no output file given report is printed to standard output");
  62. parms.nv = G_define_standard_option(G_OPT_M_NULL_VALUE);
  63. parms.nv->answer = "*";
  64. parms.nv->guisection = _("Formatting");
  65. parms.pl = G_define_option();
  66. parms.pl->key = "page_length";
  67. parms.pl->type = TYPE_INTEGER;
  68. parms.pl->required = NO;
  69. parms.pl->description = _("Page length");
  70. parms.pl->answer = DEFAULT_PAGE_LENGTH;
  71. parms.pl->guisection = _("Formatting");
  72. parms.pw = G_define_option();
  73. parms.pw->key = "page_width";
  74. parms.pw->type = TYPE_INTEGER;
  75. parms.pw->required = NO;
  76. parms.pw->description = _("Page width");
  77. parms.pw->answer = DEFAULT_PAGE_WIDTH;
  78. parms.pw->guisection = _("Formatting");
  79. parms.nsteps = G_define_option();
  80. parms.nsteps->key = "nsteps";
  81. parms.nsteps->type = TYPE_INTEGER;
  82. parms.nsteps->required = NO;
  83. parms.nsteps->multiple = NO;
  84. parms.nsteps->answer = "255";
  85. parms.nsteps->description =
  86. _("Number of floating-point subranges to collect stats from");
  87. parms.nsteps->guisection = _("Floating point");
  88. parms.sort = G_define_option();
  89. parms.sort->key = "sort";
  90. parms.sort->type = TYPE_STRING;
  91. parms.sort->required = NO;
  92. parms.sort->multiple = NO;
  93. parms.sort->label = _("Sort output statistics by cell counts");
  94. parms.sort->description = _("Default: sorted by categories or intervals");
  95. parms.sort->options = "asc,desc";
  96. G_asprintf((char **)&(parms.sort->descriptions),
  97. "asc;%s;desc;%s",
  98. _("Sort by cell counts in ascending order"),
  99. _("Sort by cell counts in descending order"));
  100. parms.sort->guisection = _("Formatting");
  101. flags.h = G_define_flag();
  102. flags.h->key = 'h';
  103. flags.h->description = _("Suppress page headers");
  104. flags.h->guisection = _("Formatting");
  105. flags.f = G_define_flag();
  106. flags.f->key = 'f';
  107. flags.f->description = _("Use formfeeds between pages");
  108. flags.f->guisection = _("Formatting");
  109. flags.e = G_define_flag();
  110. flags.e->key = 'e';
  111. flags.e->description = _("Scientific format");
  112. flags.e->guisection = _("Formatting");
  113. flags.n = G_define_flag();
  114. flags.n->key = 'n';
  115. flags.n->description = _("Do not report no data value");
  116. flags.n->guisection = _("No data");
  117. flags.N = G_define_flag();
  118. flags.N->key = 'a';
  119. flags.N->description = _("Do not report cells where all maps have no data");
  120. flags.N->guisection = _("No data");
  121. flags.C = G_define_flag();
  122. flags.C->key = 'c';
  123. flags.C->description = _("Report for cats floating-point ranges (floating-point maps only)");
  124. flags.C->guisection = _("Floating point");
  125. flags.i = G_define_flag();
  126. flags.i->key = 'i';
  127. flags.i->description =
  128. _("Read floating-point map as integer (use map's quant rules)");
  129. flags.i->guisection = _("Floating point");
  130. /* hidden feature.
  131. * if first arg is >file just run r.stats into this file and quit
  132. * if first arg is <file, run report from stats in file
  133. * (this feature is for the interactive version of this program -
  134. * to get more than one report without re-running r.stats)
  135. */
  136. stats_flag = EVERYTHING;
  137. if (argc > 1) {
  138. if (argv[1][0] == '<' || argv[1][0] == '>') {
  139. stats_file = argv[1] + 1;
  140. if (argv[1][0] == '<')
  141. stats_flag = REPORT_ONLY;
  142. else {
  143. unlink(stats_file);
  144. stats_flag = STATS_ONLY;
  145. }
  146. argc--;
  147. argv++;
  148. }
  149. }
  150. if (G_parser(argc, argv))
  151. exit(EXIT_FAILURE);
  152. use_formfeed = flags.f->answer;
  153. with_headers = !flags.h->answer;
  154. e_format = flags.e->answer;
  155. no_nulls = flags.n->answer;
  156. no_nulls_all = flags.N->answer;
  157. cat_ranges = flags.C->answer;
  158. as_int = flags.i->answer;
  159. for (i = 0; parms.cell->answers[i]; i++)
  160. parse_layer(parms.cell->answers[i]);
  161. if (parms.units->answers)
  162. for (i = 0; parms.units->answers[i]; i++)
  163. parse_units(parms.units->answers[i]);
  164. sscanf(parms.nsteps->answer, "%d", &nsteps);
  165. if (nsteps <= 0) {
  166. G_warning(_("nsteps has to be > 0; using nsteps=255"));
  167. nsteps = 255;
  168. }
  169. if (sscanf(parms.pl->answer, "%d", &page_length) != 1 ||
  170. page_length < 0) {
  171. G_fatal_error(_("Illegal page length"));
  172. }
  173. if (sscanf(parms.pw->answer, "%d", &page_width) != 1 ||
  174. page_width < 1) {
  175. G_fatal_error(_("Illegal page width"));
  176. }
  177. if (parms.outfile->answer) {
  178. if (freopen(parms.outfile->answer, "w", stdout) == NULL) {
  179. perror(parms.outfile->answer);
  180. exit(EXIT_FAILURE);
  181. }
  182. }
  183. no_data_str = parms.nv->answer;
  184. /* determine sorting method */
  185. do_sort = SORT_DEFAULT; /* sort by cats by default */
  186. if (parms.sort->answer) {
  187. switch(parms.sort->answer[0]) {
  188. case 'a':
  189. do_sort = SORT_ASC;
  190. break;
  191. case 'd':
  192. do_sort = SORT_DESC;
  193. break;
  194. default:
  195. G_debug(1, "Sorting by '%s' not supported", parms.sort->answer);
  196. break;
  197. }
  198. }
  199. return 0;
  200. }
  201. int parse_units(char *s)
  202. {
  203. int x;
  204. if (match(s, "miles", 2))
  205. x = SQ_MILES;
  206. else if (match(s, "meters", 2))
  207. x = SQ_METERS;
  208. else if (match(s, "kilometers", 1))
  209. x = SQ_KILOMETERS;
  210. else if (match(s, "acres", 1))
  211. x = ACRES;
  212. else if (match(s, "hectares", 1))
  213. x = HECTARES;
  214. else if (match(s, "cell_counts", 1))
  215. x = CELL_COUNTS;
  216. else if (match(s, "counts", 1))
  217. x = CELL_COUNTS;
  218. else if (match(s, "percent_cover", 1))
  219. x = PERCENT_COVER;
  220. else {
  221. G_usage();
  222. exit(EXIT_FAILURE);
  223. }
  224. if (nunits >= MAX_UNITS) {
  225. G_fatal_error(_("Only %d unit%s allowed"),
  226. MAX_UNITS, MAX_UNITS == 1 ? "" : "s");
  227. }
  228. unit[nunits].type = x;
  229. nunits++;
  230. return 0;
  231. }
  232. int parse_layer(char *s)
  233. {
  234. char name[GNAME_MAX];
  235. const char *mapset;
  236. struct FPRange fp_range;
  237. int n;
  238. strcpy(name, s);
  239. mapset = G_find_raster2(name, "");
  240. if (mapset == NULL)
  241. G_fatal_error(_("Raster map <%s> not found"), s);
  242. n = nlayers++;
  243. layers = (LAYER *) G_realloc(layers, nlayers * sizeof(LAYER));
  244. is_fp = (int *)G_realloc(is_fp, (nlayers + 1) * sizeof(int));
  245. DMAX = (DCELL *) G_realloc(DMAX, (nlayers + 1) * sizeof(DCELL));
  246. DMIN = (DCELL *) G_realloc(DMIN, (nlayers + 1) * sizeof(DCELL));
  247. if (!as_int)
  248. is_fp[n] = Rast_map_is_fp(name, mapset);
  249. else
  250. is_fp[n] = 0;
  251. if (is_fp[n]) {
  252. if (Rast_read_fp_range(name, mapset, &fp_range) < 0)
  253. G_fatal_error(_("Unable to read fp range for raster map <%s>"),
  254. name);
  255. Rast_get_fp_range_min_max(&fp_range, &DMIN[n], &DMAX[n]);
  256. }
  257. layers[n].name = G_store(name);
  258. layers[n].mapset = mapset;
  259. if (Rast_read_cats(name, mapset, &layers[n].labels) < 0)
  260. G_fatal_error(_("Unable to read category file of raster map <%s@%s>"),
  261. name, mapset);
  262. return 0;
  263. }
  264. int match(char *s, char *key, int min)
  265. {
  266. size_t len;
  267. len = strlen(s);
  268. if ((int) len < min)
  269. return 0;
  270. return strncmp(s, key, len) == 0;
  271. }