main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * r.in.xyz
  3. *
  4. * Calculates univariate statistics from the non-null cells of a GRASS raster map
  5. *
  6. * Copyright 2006-2012 by M. Hamish Bowman, and The GRASS Development Team
  7. * Author: M. Hamish Bowman, University of Otago, Dunedin, New Zealand
  8. *
  9. * Extended 2007 by Volker Wichmann to support the aggregate functions
  10. * median, percentile, skewness and trimmed mean
  11. *
  12. * This program is free software licensed under the GPL (>=v2).
  13. * Read the COPYING file that comes with GRASS for details.
  14. *
  15. * This program is intended as a replacement for the GRASS 5 s.cellstats module.
  16. */
  17. #include <grass/config.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <sys/types.h>
  23. #include <grass/gis.h>
  24. #include <grass/raster.h>
  25. #include <grass/glocale.h>
  26. #include "local_proto.h"
  27. struct node
  28. {
  29. int next;
  30. double z;
  31. };
  32. #define SIZE_INCREMENT 10
  33. int num_nodes = 0;
  34. int max_nodes = 0;
  35. struct node *nodes;
  36. int new_node(void)
  37. {
  38. int n = num_nodes++;
  39. if (num_nodes >= max_nodes) {
  40. max_nodes += SIZE_INCREMENT;
  41. nodes = G_realloc(nodes, (size_t)max_nodes * sizeof(struct node));
  42. }
  43. return n;
  44. }
  45. /* add node to sorted, single linked list
  46. * returns id if head has to be saved to index array, otherwise -1 */
  47. int add_node(int head, double z)
  48. {
  49. int node_id, last_id, newnode_id, head_id;
  50. head_id = head;
  51. node_id = head_id;
  52. last_id = head_id;
  53. while (node_id != -1 && nodes[node_id].z < z) {
  54. last_id = node_id;
  55. node_id = nodes[node_id].next;
  56. }
  57. /* end of list, simply append */
  58. if (node_id == -1) {
  59. newnode_id = new_node();
  60. nodes[newnode_id].next = -1;
  61. nodes[newnode_id].z = z;
  62. nodes[last_id].next = newnode_id;
  63. return -1;
  64. }
  65. else if (node_id == head_id) { /* pole position, insert as head */
  66. newnode_id = new_node();
  67. nodes[newnode_id].next = head_id;
  68. head_id = newnode_id;
  69. nodes[newnode_id].z = z;
  70. return (head_id);
  71. }
  72. else { /* somewhere in the middle, insert */
  73. newnode_id = new_node();
  74. nodes[newnode_id].z = z;
  75. nodes[newnode_id].next = node_id;
  76. nodes[last_id].next = newnode_id;
  77. return -1;
  78. }
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. FILE *in_fp;
  83. int out_fd;
  84. char *infile, *outmap;
  85. int xcol, ycol, zcol, vcol, max_col, percent;
  86. int method = -1;
  87. int bin_n, bin_min, bin_max, bin_sum, bin_sumsq, bin_index;
  88. double zrange_min, zrange_max, vrange_min, vrange_max, d_tmp;
  89. char *fs; /* field delim */
  90. off_t filesize;
  91. int linesize;
  92. unsigned long estimated_lines;
  93. int from_stdin;
  94. int can_seek;
  95. RASTER_MAP_TYPE rtype;
  96. struct History history;
  97. char title[64];
  98. void *n_array, *min_array, *max_array, *sum_array, *sumsq_array,
  99. *index_array;
  100. void *raster_row, *ptr;
  101. struct Cell_head region;
  102. int rows, cols; /* scan box size */
  103. int row, col; /* counters */
  104. int pass, npasses;
  105. unsigned long line;
  106. char buff[BUFFSIZE];
  107. double x, y, z;
  108. char **tokens;
  109. int ntokens; /* number of tokens */
  110. double pass_north, pass_south;
  111. int arr_row, arr_col;
  112. unsigned long count, count_total;
  113. double min = 0.0 / 0.0; /* init as nan */
  114. double max = 0.0 / 0.0; /* init as nan */
  115. double zscale = 1.0;
  116. double vscale = 1.0;
  117. size_t offset, n_offset;
  118. int n = 0;
  119. double sum = 0.;
  120. double sumsq = 0.;
  121. double variance, mean, skew, sumdev;
  122. int pth = 0;
  123. double trim = 0.0;
  124. int j, k;
  125. int head_id, node_id;
  126. int r_low, r_up;
  127. struct GModule *module;
  128. struct Option *input_opt, *output_opt, *delim_opt, *percent_opt,
  129. *type_opt;
  130. struct Option *method_opt, *xcol_opt, *ycol_opt, *zcol_opt, *zrange_opt,
  131. *zscale_opt, *vcol_opt, *vrange_opt, *vscale_opt;
  132. struct Option *trim_opt, *pth_opt;
  133. struct Flag *scan_flag, *shell_style, *skipline;
  134. G_gisinit(argv[0]);
  135. module = G_define_module();
  136. G_add_keyword(_("raster"));
  137. G_add_keyword(_("import"));
  138. G_add_keyword(_("conversion"));
  139. G_add_keyword("ASCII");
  140. G_add_keyword(_("LIDAR"));
  141. module->description =
  142. _("Creates a raster map from an assemblage of many coordinates using univariate statistics.");
  143. input_opt = G_define_standard_option(G_OPT_F_INPUT);
  144. input_opt->description =
  145. _("ASCII file containing input data (or \"-\" to read from stdin)");
  146. output_opt = G_define_standard_option(G_OPT_R_OUTPUT);
  147. method_opt = G_define_option();
  148. method_opt->key = "method";
  149. method_opt->type = TYPE_STRING;
  150. method_opt->required = NO;
  151. method_opt->description = _("Statistic to use for raster values");
  152. method_opt->options =
  153. "n,min,max,range,sum,mean,stddev,variance,coeff_var,median,percentile,skewness,trimmean";
  154. method_opt->answer = "mean";
  155. method_opt->guisection = _("Statistic");
  156. type_opt = G_define_option();
  157. type_opt->key = "type";
  158. type_opt->type = TYPE_STRING;
  159. type_opt->required = NO;
  160. type_opt->options = "CELL,FCELL,DCELL";
  161. type_opt->answer = "FCELL";
  162. type_opt->description = _("Storage type for resultant raster map");
  163. delim_opt = G_define_standard_option(G_OPT_F_SEP);
  164. delim_opt->guisection = _("Input");
  165. xcol_opt = G_define_option();
  166. xcol_opt->key = "x";
  167. xcol_opt->type = TYPE_INTEGER;
  168. xcol_opt->required = NO;
  169. xcol_opt->answer = "1";
  170. xcol_opt->description =
  171. _("Column number of x coordinates in input file (first column is 1)");
  172. xcol_opt->guisection = _("Input");
  173. ycol_opt = G_define_option();
  174. ycol_opt->key = "y";
  175. ycol_opt->type = TYPE_INTEGER;
  176. ycol_opt->required = NO;
  177. ycol_opt->answer = "2";
  178. ycol_opt->description = _("Column number of y coordinates in input file");
  179. ycol_opt->guisection = _("Input");
  180. zcol_opt = G_define_option();
  181. zcol_opt->key = "z";
  182. zcol_opt->type = TYPE_INTEGER;
  183. zcol_opt->required = NO;
  184. zcol_opt->answer = "3";
  185. zcol_opt->label = _("Column number of data values in input file");
  186. zcol_opt->description =
  187. _("If a separate value column is given, this option refers to the "
  188. "z-coordinate column to be filtered by the zrange option");
  189. zcol_opt->guisection = _("Input");
  190. zrange_opt = G_define_option();
  191. zrange_opt->key = "zrange";
  192. zrange_opt->type = TYPE_DOUBLE;
  193. zrange_opt->required = NO;
  194. zrange_opt->key_desc = "min,max";
  195. zrange_opt->description = _("Filter range for z data (min,max)");
  196. zrange_opt->guisection = _("Advanced Input");
  197. zscale_opt = G_define_option();
  198. zscale_opt->key = "zscale";
  199. zscale_opt->type = TYPE_DOUBLE;
  200. zscale_opt->required = NO;
  201. zscale_opt->answer = "1.0";
  202. zscale_opt->description = _("Scale to apply to z data");
  203. zscale_opt->guisection = _("Advanced Input");
  204. vcol_opt = G_define_option();
  205. vcol_opt->key = "value_column";
  206. vcol_opt->type = TYPE_INTEGER;
  207. vcol_opt->required = NO;
  208. vcol_opt->answer = "0";
  209. vcol_opt->label = _("Alternate column number of data values in input file");
  210. vcol_opt->description = _("If not given (or set to 0) the z-column data is used");
  211. vcol_opt->guisection = _("Advanced Input");
  212. vrange_opt = G_define_option();
  213. vrange_opt->key = "vrange";
  214. vrange_opt->type = TYPE_DOUBLE;
  215. vrange_opt->required = NO;
  216. vrange_opt->key_desc = "min,max";
  217. vrange_opt->description = _("Filter range for alternate value column data (min,max)");
  218. vrange_opt->guisection = _("Advanced Input");
  219. vscale_opt = G_define_option();
  220. vscale_opt->key = "vscale";
  221. vscale_opt->type = TYPE_DOUBLE;
  222. vscale_opt->required = NO;
  223. vscale_opt->answer = "1.0";
  224. vscale_opt->description = _("Scale to apply to alternate value column data");
  225. vscale_opt->guisection = _("Advanced Input");
  226. percent_opt = G_define_option();
  227. percent_opt->key = "percent";
  228. percent_opt->type = TYPE_INTEGER;
  229. percent_opt->required = NO;
  230. percent_opt->answer = "100";
  231. percent_opt->options = "1-100";
  232. percent_opt->description = _("Percent of map to keep in memory");
  233. /* I would prefer to call the following "percentile", but that has too
  234. * much namespace overlap with the "percent" option above */
  235. pth_opt = G_define_option();
  236. pth_opt->key = "pth";
  237. pth_opt->type = TYPE_INTEGER;
  238. pth_opt->required = NO;
  239. pth_opt->options = "1-100";
  240. pth_opt->description = _("pth percentile of the values");
  241. pth_opt->guisection = _("Statistic");
  242. trim_opt = G_define_option();
  243. trim_opt->key = "trim";
  244. trim_opt->type = TYPE_DOUBLE;
  245. trim_opt->required = NO;
  246. trim_opt->options = "0-50";
  247. trim_opt->description =
  248. _("Discard <trim> percent of the smallest and <trim> percent of the largest observations");
  249. trim_opt->guisection = _("Statistic");
  250. scan_flag = G_define_flag();
  251. scan_flag->key = 's';
  252. scan_flag->description = _("Scan data file for extent then exit");
  253. shell_style = G_define_flag();
  254. shell_style->key = 'g';
  255. shell_style->description =
  256. _("In scan mode, print using shell script style");
  257. skipline = G_define_flag();
  258. skipline->key = 'i';
  259. skipline->description = _("Ignore broken lines");
  260. if (G_parser(argc, argv))
  261. exit(EXIT_FAILURE);
  262. /* parse input values */
  263. infile = input_opt->answer;
  264. outmap = output_opt->answer;
  265. if (shell_style->answer && !scan_flag->answer) {
  266. scan_flag->answer = 1; /* pointer not int, so set = shell_style->answer ? */
  267. }
  268. fs = G_option_to_separator(delim_opt);
  269. xcol = atoi(xcol_opt->answer);
  270. ycol = atoi(ycol_opt->answer);
  271. zcol = atoi(zcol_opt->answer);
  272. vcol = atoi(vcol_opt->answer);
  273. if ((xcol < 0) || (ycol < 0) || (zcol < 0) || (vcol < 0))
  274. G_fatal_error(_("Please specify a reasonable column number."));
  275. max_col = (xcol > ycol) ? xcol : ycol;
  276. max_col = (zcol > max_col) ? zcol : max_col;
  277. if(vcol)
  278. max_col = (vcol > max_col) ? vcol : max_col;
  279. percent = atoi(percent_opt->answer);
  280. zscale = atof(zscale_opt->answer);
  281. vscale = atof(vscale_opt->answer);
  282. /* parse zrange and vrange */
  283. if (zrange_opt->answer != NULL) {
  284. if (zrange_opt->answers[0] == NULL)
  285. G_fatal_error(_("Invalid zrange"));
  286. sscanf(zrange_opt->answers[0], "%lf", &zrange_min);
  287. sscanf(zrange_opt->answers[1], "%lf", &zrange_max);
  288. if (zrange_min > zrange_max) {
  289. d_tmp = zrange_max;
  290. zrange_max = zrange_min;
  291. zrange_min = d_tmp;
  292. }
  293. }
  294. if (vrange_opt->answer != NULL) {
  295. if (vrange_opt->answers[0] == NULL)
  296. G_fatal_error(_("Invalid vrange"));
  297. sscanf(vrange_opt->answers[0], "%lf", &vrange_min);
  298. sscanf(vrange_opt->answers[1], "%lf", &vrange_max);
  299. if (vrange_min > vrange_max) {
  300. d_tmp = vrange_max;
  301. vrange_max = vrange_min;
  302. vrange_min = d_tmp;
  303. }
  304. }
  305. /* figure out what maps we need in memory */
  306. /* n n
  307. min min
  308. max max
  309. range min max max - min
  310. sum sum
  311. mean sum n sum/n
  312. stddev sum sumsq n sqrt((sumsq - sum*sum/n)/n)
  313. variance sum sumsq n (sumsq - sum*sum/n)/n
  314. coeff_var sum sumsq n sqrt((sumsq - sum*sum/n)/n) / (sum/n)
  315. median n array index to linked list
  316. percentile n array index to linked list
  317. skewness n array index to linked list
  318. trimmean n array index to linked list
  319. */
  320. bin_n = FALSE;
  321. bin_min = FALSE;
  322. bin_max = FALSE;
  323. bin_sum = FALSE;
  324. bin_sumsq = FALSE;
  325. bin_index = FALSE;
  326. if (strcmp(method_opt->answer, "n") == 0) {
  327. method = METHOD_N;
  328. bin_n = TRUE;
  329. }
  330. if (strcmp(method_opt->answer, "min") == 0) {
  331. method = METHOD_MIN;
  332. bin_min = TRUE;
  333. }
  334. if (strcmp(method_opt->answer, "max") == 0) {
  335. method = METHOD_MAX;
  336. bin_max = TRUE;
  337. }
  338. if (strcmp(method_opt->answer, "range") == 0) {
  339. method = METHOD_RANGE;
  340. bin_min = TRUE;
  341. bin_max = TRUE;
  342. }
  343. if (strcmp(method_opt->answer, "sum") == 0) {
  344. method = METHOD_SUM;
  345. bin_sum = TRUE;
  346. }
  347. if (strcmp(method_opt->answer, "mean") == 0) {
  348. method = METHOD_MEAN;
  349. bin_sum = TRUE;
  350. bin_n = TRUE;
  351. }
  352. if (strcmp(method_opt->answer, "stddev") == 0) {
  353. method = METHOD_STDDEV;
  354. bin_sum = TRUE;
  355. bin_sumsq = TRUE;
  356. bin_n = TRUE;
  357. }
  358. if (strcmp(method_opt->answer, "variance") == 0) {
  359. method = METHOD_VARIANCE;
  360. bin_sum = TRUE;
  361. bin_sumsq = TRUE;
  362. bin_n = TRUE;
  363. }
  364. if (strcmp(method_opt->answer, "coeff_var") == 0) {
  365. method = METHOD_COEFF_VAR;
  366. bin_sum = TRUE;
  367. bin_sumsq = TRUE;
  368. bin_n = TRUE;
  369. }
  370. if (strcmp(method_opt->answer, "median") == 0) {
  371. method = METHOD_MEDIAN;
  372. bin_index = TRUE;
  373. }
  374. if (strcmp(method_opt->answer, "percentile") == 0) {
  375. if (pth_opt->answer != NULL)
  376. pth = atoi(pth_opt->answer);
  377. else
  378. G_fatal_error(_("Unable to calculate percentile without the pth option specified!"));
  379. method = METHOD_PERCENTILE;
  380. bin_index = TRUE;
  381. }
  382. if (strcmp(method_opt->answer, "skewness") == 0) {
  383. method = METHOD_SKEWNESS;
  384. bin_index = TRUE;
  385. }
  386. if (strcmp(method_opt->answer, "trimmean") == 0) {
  387. if (trim_opt->answer != NULL)
  388. trim = atof(trim_opt->answer) / 100.0;
  389. else
  390. G_fatal_error(_("Unable to calculate trimmed mean without the trim option specified!"));
  391. method = METHOD_TRIMMEAN;
  392. bin_index = TRUE;
  393. }
  394. if (strcmp("CELL", type_opt->answer) == 0)
  395. rtype = CELL_TYPE;
  396. else if (strcmp("DCELL", type_opt->answer) == 0)
  397. rtype = DCELL_TYPE;
  398. else
  399. rtype = FCELL_TYPE;
  400. if (method == METHOD_N)
  401. rtype = CELL_TYPE;
  402. G_get_window(&region);
  403. rows = (int)(region.rows * (percent / 100.0));
  404. cols = region.cols;
  405. G_debug(2, "region.n=%f region.s=%f region.ns_res=%f", region.north,
  406. region.south, region.ns_res);
  407. G_debug(2, "region.rows=%d [box_rows=%d] region.cols=%d", region.rows,
  408. rows, region.cols);
  409. npasses = (int)ceil(1.0 * region.rows / rows);
  410. if (!scan_flag->answer) {
  411. /* check if rows * (cols + 1) go into a size_t */
  412. if (sizeof(size_t) < 8) {
  413. double dsize = rows * (cols + 1);
  414. if (dsize != (size_t)rows * (cols + 1))
  415. G_fatal_error(_("Unable to process the hole map at once. "
  416. "Please set the %s option to some value lower than 100."),
  417. percent_opt->key);
  418. }
  419. /* allocate memory (test for enough before we start) */
  420. if (bin_n)
  421. n_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(CELL_TYPE));
  422. if (bin_min)
  423. min_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  424. if (bin_max)
  425. max_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  426. if (bin_sum)
  427. sum_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  428. if (bin_sumsq)
  429. sumsq_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  430. if (bin_index)
  431. index_array =
  432. G_calloc((size_t)rows * (cols + 1), Rast_cell_size(CELL_TYPE));
  433. /* and then free it again */
  434. if (bin_n)
  435. G_free(n_array);
  436. if (bin_min)
  437. G_free(min_array);
  438. if (bin_max)
  439. G_free(max_array);
  440. if (bin_sum)
  441. G_free(sum_array);
  442. if (bin_sumsq)
  443. G_free(sumsq_array);
  444. if (bin_index)
  445. G_free(index_array);
  446. /** end memory test **/
  447. }
  448. /* open input file */
  449. if (strcmp("-", infile) == 0) {
  450. from_stdin = TRUE;
  451. in_fp = stdin;
  452. infile = G_store("stdin"); /* filename for history metadata */
  453. }
  454. else {
  455. if ((in_fp = fopen(infile, "r")) == NULL)
  456. G_fatal_error(_("Unable to open input file <%s>"), infile);
  457. }
  458. can_seek = fseek(in_fp, 0L, SEEK_SET) == 0;
  459. /* can't rewind() non-files */
  460. if (!can_seek && npasses != 1) {
  461. G_warning(_("If input is not from a file it is only possible to perform a single pass."));
  462. npasses = 1;
  463. }
  464. if (scan_flag->answer) {
  465. if (zrange_opt->answer || vrange_opt->answer)
  466. G_warning(_("Range filters will not be taken into account during scan"));
  467. scan_bounds(in_fp, xcol, ycol, zcol, vcol, fs, shell_style->answer,
  468. skipline->answer, zscale, vscale);
  469. if (!from_stdin)
  470. fclose(in_fp);
  471. exit(EXIT_SUCCESS);
  472. }
  473. /* open output map */
  474. out_fd = Rast_open_new(outmap, rtype);
  475. if (can_seek) {
  476. /* guess at number of lines in the file without actually reading it all in */
  477. for (line = 0; line < 10; line++) { /* arbitrarily use 10th line for guess */
  478. if (0 == G_getl2(buff, BUFFSIZE - 1, in_fp))
  479. break;
  480. linesize = strlen(buff) + 1;
  481. }
  482. G_fseek(in_fp, 0L, SEEK_END);
  483. filesize = G_ftell(in_fp);
  484. rewind(in_fp);
  485. if (linesize < 6) /* min possible: "0,0,0\n" */
  486. linesize = 6;
  487. estimated_lines = filesize / linesize;
  488. G_debug(2, "estimated number of lines in file: %ld", estimated_lines);
  489. }
  490. else
  491. estimated_lines = -1;
  492. /* allocate memory for a single row of output data */
  493. raster_row = Rast_allocate_buf(rtype);
  494. G_message(_("Reading input data..."));
  495. count_total = 0;
  496. /* main binning loop(s) */
  497. for (pass = 1; pass <= npasses; pass++) {
  498. if (npasses > 1)
  499. G_message(_("Pass #%d (of %d) ..."), pass, npasses);
  500. if (can_seek)
  501. rewind(in_fp);
  502. /* figure out segmentation */
  503. pass_north = region.north - (pass - 1) * rows * region.ns_res;
  504. if (pass == npasses)
  505. rows = region.rows - (pass - 1) * rows;
  506. pass_south = pass_north - rows * region.ns_res;
  507. G_debug(2, "pass=%d/%d pass_n=%f pass_s=%f rows=%d",
  508. pass, npasses, pass_north, pass_south, rows);
  509. if (bin_n) {
  510. G_debug(2, "allocating n_array");
  511. n_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(CELL_TYPE));
  512. blank_array(n_array, rows, cols, CELL_TYPE, 0);
  513. }
  514. if (bin_min) {
  515. G_debug(2, "allocating min_array");
  516. min_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  517. blank_array(min_array, rows, cols, rtype, -1); /* fill with NULLs */
  518. }
  519. if (bin_max) {
  520. G_debug(2, "allocating max_array");
  521. max_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  522. blank_array(max_array, rows, cols, rtype, -1); /* fill with NULLs */
  523. }
  524. if (bin_sum) {
  525. G_debug(2, "allocating sum_array");
  526. sum_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  527. blank_array(sum_array, rows, cols, rtype, 0);
  528. }
  529. if (bin_sumsq) {
  530. G_debug(2, "allocating sumsq_array");
  531. sumsq_array = G_calloc((size_t)rows * (cols + 1), Rast_cell_size(rtype));
  532. blank_array(sumsq_array, rows, cols, rtype, 0);
  533. }
  534. if (bin_index) {
  535. G_debug(2, "allocating index_array");
  536. index_array =
  537. G_calloc((size_t)rows * (cols + 1), Rast_cell_size(CELL_TYPE));
  538. blank_array(index_array, rows, cols, CELL_TYPE, -1); /* fill with NULLs */
  539. }
  540. line = 0;
  541. count = 0;
  542. G_percent_reset();
  543. while (0 != G_getl2(buff, BUFFSIZE - 1, in_fp)) {
  544. line++;
  545. if (line % 10000 == 0) { /* mod for speed */
  546. if (!can_seek)
  547. G_clicker();
  548. else if (line < estimated_lines)
  549. G_percent(line, estimated_lines, 3);
  550. }
  551. if ((buff[0] == '#') || (buff[0] == '\0')) {
  552. continue; /* line is a comment or blank */
  553. }
  554. G_chop(buff); /* remove leading and trailing whitespace from the string. unneded?? */
  555. tokens = G_tokenize(buff, fs);
  556. ntokens = G_number_of_tokens(tokens);
  557. if ((ntokens < 3) || (max_col > ntokens)) {
  558. if (skipline->answer) {
  559. G_warning(_("Not enough data columns. "
  560. "Incorrect delimiter or column number? "
  561. "Found the following character(s) in row %lu:\n[%s]"),
  562. line, buff);
  563. G_warning(_("Line ignored as requested"));
  564. continue; /* line is garbage */
  565. }
  566. else {
  567. G_fatal_error(_("Not enough data columns. "
  568. "Incorrect delimiter or column number? "
  569. "Found the following character(s) in row %lu:\n[%s]"),
  570. line, buff);
  571. }
  572. }
  573. /* too slow?
  574. if ( G_projection() == PROJECTION_LL ) {
  575. G_scan_easting( tokens[xcol-1], &x, region.proj);
  576. G_scan_northing( tokens[ycol-1], &y, region.proj);
  577. }
  578. else {
  579. */
  580. if (1 != sscanf(tokens[ycol - 1], "%lf", &y))
  581. G_fatal_error(_("Bad y-coordinate line %lu column %d. <%s>"),
  582. line, ycol, tokens[ycol - 1]);
  583. if (y <= pass_south || y > pass_north) {
  584. G_free_tokens(tokens);
  585. continue;
  586. }
  587. if (1 != sscanf(tokens[xcol - 1], "%lf", &x))
  588. G_fatal_error(_("Bad x-coordinate line %lu column %d. <%s>"),
  589. line, xcol, tokens[xcol - 1]);
  590. if (x < region.west || x > region.east) {
  591. G_free_tokens(tokens);
  592. continue;
  593. }
  594. if (1 != sscanf(tokens[zcol - 1], "%lf", &z))
  595. G_fatal_error(_("Bad z-coordinate line %lu column %d. <%s>"),
  596. line, zcol, tokens[zcol - 1]);
  597. z = z * zscale;
  598. if (zrange_opt->answer) {
  599. if (z < zrange_min || z > zrange_max) {
  600. G_free_tokens(tokens);
  601. continue;
  602. }
  603. }
  604. if(vcol) {
  605. if (1 != sscanf(tokens[vcol - 1], "%lf", &z))
  606. G_fatal_error(_("Bad data value line %lu column %d. <%s>"),
  607. line, vcol, tokens[vcol - 1]);
  608. /* we're past the zrange check, so pass over control of the variable */
  609. z = z * vscale;
  610. if (vrange_opt->answer) {
  611. if (z < vrange_min || z > vrange_max) {
  612. G_free_tokens(tokens);
  613. continue;
  614. }
  615. }
  616. }
  617. count++;
  618. /* G_debug(5, "x: %f, y: %f, z: %f", x, y, z); */
  619. G_free_tokens(tokens);
  620. /* find the bin in the current array box */
  621. arr_row = (int)((pass_north - y) / region.ns_res);
  622. arr_col = (int)((x - region.west) / region.ew_res);
  623. /* G_debug(5, "arr_row: %d arr_col: %d", arr_row, arr_col); */
  624. /* The range should be [0,cols-1]. We use (int) to round down,
  625. but if the point exactly on eastern edge arr_col will be /just/
  626. on the max edge .0000000 and end up on the next row.
  627. We could make above bounds check "if(x>=region.east) continue;"
  628. But instead we go to all sorts of trouble so that not one single
  629. data point is lost. GE is too small to catch them all.
  630. We don't try to make y happy as percent segmenting will make some
  631. points happen twice that way; so instead we use the y<= test above.
  632. */
  633. if (arr_col >= cols) {
  634. if (((x - region.west) / region.ew_res) - cols <
  635. 10 * GRASS_EPSILON)
  636. arr_col--;
  637. else { /* oh well, we tried. */
  638. G_debug(3,
  639. "skipping extraneous data point [%.3f], column %d of %d",
  640. x, arr_col, cols);
  641. continue;
  642. }
  643. }
  644. if (bin_n)
  645. update_n(n_array, cols, arr_row, arr_col);
  646. if (bin_min)
  647. update_min(min_array, cols, arr_row, arr_col, rtype, z);
  648. if (bin_max)
  649. update_max(max_array, cols, arr_row, arr_col, rtype, z);
  650. if (bin_sum)
  651. update_sum(sum_array, cols, arr_row, arr_col, rtype, z);
  652. if (bin_sumsq)
  653. update_sumsq(sumsq_array, cols, arr_row, arr_col, rtype, z);
  654. if (bin_index) {
  655. ptr = index_array;
  656. ptr =
  657. G_incr_void_ptr(ptr,
  658. ((arr_row * cols) +
  659. arr_col) * Rast_cell_size(CELL_TYPE));
  660. if (Rast_is_null_value(ptr, CELL_TYPE)) { /* first node */
  661. head_id = new_node();
  662. nodes[head_id].next = -1;
  663. nodes[head_id].z = z;
  664. Rast_set_c_value(ptr, head_id, CELL_TYPE); /* store index to head */
  665. }
  666. else { /* head is already there */
  667. head_id = Rast_get_c_value(ptr, CELL_TYPE); /* get index to head */
  668. head_id = add_node(head_id, z);
  669. if (head_id != -1)
  670. Rast_set_c_value(ptr, head_id, CELL_TYPE); /* store index to head */
  671. }
  672. }
  673. } /* while !EOF */
  674. G_percent(1, 1, 1); /* flush */
  675. G_debug(2, "pass %d finished, %lu coordinates in box", pass, count);
  676. count_total += count;
  677. /* calc stats and output */
  678. G_message(_("Writing to output raster map..."));
  679. for (row = 0; row < rows; row++) {
  680. G_percent(row, rows, 5);
  681. switch (method) {
  682. case METHOD_N: /* n is a straight copy */
  683. Rast_raster_cpy(raster_row,
  684. n_array +
  685. (row * cols * Rast_cell_size(CELL_TYPE)), cols,
  686. CELL_TYPE);
  687. break;
  688. case METHOD_MIN:
  689. Rast_raster_cpy(raster_row,
  690. min_array + (row * cols * Rast_cell_size(rtype)),
  691. cols, rtype);
  692. break;
  693. case METHOD_MAX:
  694. Rast_raster_cpy(raster_row,
  695. max_array + (row * cols * Rast_cell_size(rtype)),
  696. cols, rtype);
  697. break;
  698. case METHOD_SUM:
  699. Rast_raster_cpy(raster_row,
  700. sum_array + (row * cols * Rast_cell_size(rtype)),
  701. cols, rtype);
  702. break;
  703. case METHOD_RANGE: /* (max-min) */
  704. ptr = raster_row;
  705. for (col = 0; col < cols; col++) {
  706. offset = (row * cols + col) * Rast_cell_size(rtype);
  707. min = Rast_get_d_value(min_array + offset, rtype);
  708. max = Rast_get_d_value(max_array + offset, rtype);
  709. Rast_set_d_value(ptr, max - min, rtype);
  710. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  711. }
  712. break;
  713. case METHOD_MEAN: /* (sum / n) */
  714. ptr = raster_row;
  715. for (col = 0; col < cols; col++) {
  716. offset = (row * cols + col) * Rast_cell_size(rtype);
  717. n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
  718. n = Rast_get_c_value(n_array + n_offset, CELL_TYPE);
  719. sum = Rast_get_d_value(sum_array + offset, rtype);
  720. if (n == 0)
  721. Rast_set_null_value(ptr, 1, rtype);
  722. else
  723. Rast_set_d_value(ptr, (sum / n), rtype);
  724. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  725. }
  726. break;
  727. case METHOD_STDDEV: /* sqrt(variance) */
  728. case METHOD_VARIANCE: /* (sumsq - sum*sum/n)/n */
  729. case METHOD_COEFF_VAR: /* 100 * stdev / mean */
  730. ptr = raster_row;
  731. for (col = 0; col < cols; col++) {
  732. offset = (row * cols + col) * Rast_cell_size(rtype);
  733. n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
  734. n = Rast_get_c_value(n_array + n_offset, CELL_TYPE);
  735. sum = Rast_get_d_value(sum_array + offset, rtype);
  736. sumsq = Rast_get_d_value(sumsq_array + offset, rtype);
  737. if (n == 0)
  738. Rast_set_null_value(ptr, 1, rtype);
  739. else {
  740. variance = (sumsq - sum * sum / n) / n;
  741. if (variance < GRASS_EPSILON)
  742. variance = 0.0;
  743. if (method == METHOD_STDDEV)
  744. Rast_set_d_value(ptr, sqrt(variance), rtype);
  745. else if (method == METHOD_VARIANCE)
  746. Rast_set_d_value(ptr, variance, rtype);
  747. else if (method == METHOD_COEFF_VAR)
  748. Rast_set_d_value(ptr,
  749. 100 * sqrt(variance) / (sum /
  750. n),
  751. rtype);
  752. }
  753. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  754. }
  755. break;
  756. case METHOD_MEDIAN: /* median, if only one point in cell we will use that */
  757. ptr = raster_row;
  758. for (col = 0; col < cols; col++) {
  759. n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
  760. if (Rast_is_null_value(index_array + n_offset, CELL_TYPE)) /* no points in cell */
  761. Rast_set_null_value(ptr, 1, rtype);
  762. else { /* one or more points in cell */
  763. head_id =
  764. Rast_get_c_value(index_array + n_offset,
  765. CELL_TYPE);
  766. node_id = head_id;
  767. n = 0;
  768. while (node_id != -1) { /* count number of points in cell */
  769. n++;
  770. node_id = nodes[node_id].next;
  771. }
  772. if (n == 1) /* only one point, use that */
  773. Rast_set_d_value(ptr, nodes[head_id].z,
  774. rtype);
  775. else if (n % 2 != 0) { /* odd number of points: median_i = (n + 1) / 2 */
  776. n = (n + 1) / 2;
  777. node_id = head_id;
  778. for (j = 1; j < n; j++) /* get "median element" */
  779. node_id = nodes[node_id].next;
  780. Rast_set_d_value(ptr, nodes[node_id].z,
  781. rtype);
  782. }
  783. else { /* even number of points: median = (val_below + val_above) / 2 */
  784. z = (n + 1) / 2.0;
  785. n = floor(z);
  786. node_id = head_id;
  787. for (j = 1; j < n; j++) /* get element "below" */
  788. node_id = nodes[node_id].next;
  789. z = (nodes[node_id].z +
  790. nodes[nodes[node_id].next].z) / 2;
  791. Rast_set_d_value(ptr, z, rtype);
  792. }
  793. }
  794. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  795. }
  796. break;
  797. case METHOD_PERCENTILE: /* rank = (pth*(n+1))/100; interpolate linearly */
  798. ptr = raster_row;
  799. for (col = 0; col < cols; col++) {
  800. n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
  801. if (Rast_is_null_value(index_array + n_offset, CELL_TYPE)) /* no points in cell */
  802. Rast_set_null_value(ptr, 1, rtype);
  803. else {
  804. head_id =
  805. Rast_get_c_value(index_array + n_offset,
  806. CELL_TYPE);
  807. node_id = head_id;
  808. n = 0;
  809. while (node_id != -1) { /* count number of points in cell */
  810. n++;
  811. node_id = nodes[node_id].next;
  812. }
  813. z = (pth * (n + 1)) / 100.0;
  814. r_low = floor(z); /* lower rank */
  815. if (r_low < 1)
  816. r_low = 1;
  817. else if (r_low > n)
  818. r_low = n;
  819. r_up = ceil(z); /* upper rank */
  820. if (r_up > n)
  821. r_up = n;
  822. node_id = head_id;
  823. for (j = 1; j < r_low; j++) /* search lower value */
  824. node_id = nodes[node_id].next;
  825. z = nodes[node_id].z; /* save lower value */
  826. node_id = head_id;
  827. for (j = 1; j < r_up; j++) /* search upper value */
  828. node_id = nodes[node_id].next;
  829. z = (z + nodes[node_id].z) / 2;
  830. Rast_set_d_value(ptr, z, rtype);
  831. }
  832. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  833. }
  834. break;
  835. case METHOD_SKEWNESS: /* skewness = sum(xi-mean)^3/(N-1)*s^3 */
  836. ptr = raster_row;
  837. for (col = 0; col < cols; col++) {
  838. n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
  839. if (Rast_is_null_value(index_array + n_offset, CELL_TYPE)) /* no points in cell */
  840. Rast_set_null_value(ptr, 1, rtype);
  841. else {
  842. head_id =
  843. Rast_get_c_value(index_array + n_offset,
  844. CELL_TYPE);
  845. node_id = head_id;
  846. n = 0; /* count */
  847. sum = 0.0; /* sum */
  848. sumsq = 0.0; /* sum of squares */
  849. sumdev = 0.0; /* sum of (xi - mean)^3 */
  850. skew = 0.0; /* skewness */
  851. while (node_id != -1) {
  852. z = nodes[node_id].z;
  853. n++;
  854. sum += z;
  855. sumsq += (z * z);
  856. node_id = nodes[node_id].next;
  857. }
  858. if (n > 1) { /* if n == 1, skew is "0.0" */
  859. mean = sum / n;
  860. node_id = head_id;
  861. while (node_id != -1) {
  862. z = nodes[node_id].z;
  863. sumdev += pow((z - mean), 3);
  864. node_id = nodes[node_id].next;
  865. }
  866. variance = (sumsq - sum * sum / n) / n;
  867. if (variance < GRASS_EPSILON)
  868. skew = 0.0;
  869. else
  870. skew =
  871. sumdev / ((n - 1) *
  872. pow(sqrt(variance), 3));
  873. }
  874. Rast_set_d_value(ptr, skew, rtype);
  875. }
  876. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  877. }
  878. break;
  879. case METHOD_TRIMMEAN:
  880. ptr = raster_row;
  881. for (col = 0; col < cols; col++) {
  882. n_offset = (row * cols + col) * Rast_cell_size(CELL_TYPE);
  883. if (Rast_is_null_value(index_array + n_offset, CELL_TYPE)) /* no points in cell */
  884. Rast_set_null_value(ptr, 1, rtype);
  885. else {
  886. head_id =
  887. Rast_get_c_value(index_array + n_offset,
  888. CELL_TYPE);
  889. node_id = head_id;
  890. n = 0;
  891. while (node_id != -1) { /* count number of points in cell */
  892. n++;
  893. node_id = nodes[node_id].next;
  894. }
  895. if (1 == n)
  896. mean = nodes[head_id].z;
  897. else {
  898. k = floor(trim * n + 0.5); /* number of ranks to discard on each tail */
  899. if (k > 0 && (n - 2 * k) > 0) { /* enough elements to discard */
  900. node_id = head_id;
  901. for (j = 0; j < k; j++) /* move to first rank to consider */
  902. node_id = nodes[node_id].next;
  903. j = k + 1;
  904. k = n - k;
  905. n = 0;
  906. sum = 0.0;
  907. while (j <= k) { /* get values in interval */
  908. n++;
  909. sum += nodes[node_id].z;
  910. node_id = nodes[node_id].next;
  911. j++;
  912. }
  913. }
  914. else {
  915. node_id = head_id;
  916. n = 0;
  917. sum = 0.0;
  918. while (node_id != -1) {
  919. n++;
  920. sum += nodes[node_id].z;
  921. node_id = nodes[node_id].next;
  922. }
  923. }
  924. mean = sum / n;
  925. }
  926. Rast_set_d_value(ptr, mean, rtype);
  927. }
  928. ptr = G_incr_void_ptr(ptr, Rast_cell_size(rtype));
  929. }
  930. break;
  931. default:
  932. G_fatal_error("?");
  933. }
  934. /* write out line of raster data */
  935. Rast_put_row(out_fd, raster_row, rtype);
  936. }
  937. /* free memory */
  938. if (bin_n)
  939. G_free(n_array);
  940. if (bin_min)
  941. G_free(min_array);
  942. if (bin_max)
  943. G_free(max_array);
  944. if (bin_sum)
  945. G_free(sum_array);
  946. if (bin_sumsq)
  947. G_free(sumsq_array);
  948. if (bin_index) {
  949. G_free(index_array);
  950. G_free(nodes);
  951. num_nodes = 0;
  952. max_nodes = 0;
  953. nodes = NULL;
  954. }
  955. } /* passes loop */
  956. G_percent(1, 1, 1); /* flush */
  957. G_free(raster_row);
  958. /* close input file */
  959. if (!from_stdin)
  960. fclose(in_fp);
  961. /* close raster file & write history */
  962. Rast_close(out_fd);
  963. sprintf(title, "Raw x,y,z data binned into a raster grid by cell %s",
  964. method_opt->answer);
  965. Rast_put_cell_title(outmap, title);
  966. Rast_short_history(outmap, "raster", &history);
  967. Rast_command_history(&history);
  968. Rast_set_history(&history, HIST_DATSRC_1, infile);
  969. Rast_write_history(outmap, &history);
  970. sprintf(buff, _("%lu points found in region."), count_total);
  971. G_done_msg(buff);
  972. G_debug(1, "Processed %lu lines.", line);
  973. exit(EXIT_SUCCESS);
  974. }
  975. int scan_bounds(FILE * fp, int xcol, int ycol, int zcol, int vcol, char *fs,
  976. int shell_style, int skipline, double zscale, double vscale)
  977. {
  978. unsigned long line;
  979. int first, max_col;
  980. char buff[BUFFSIZE];
  981. double min_x, max_x, min_y, max_y, min_z, max_z, min_v, max_v;
  982. char **tokens;
  983. int ntokens; /* number of tokens */
  984. double x, y, z, v;
  985. max_col = (xcol > ycol) ? xcol : ycol;
  986. max_col = (zcol > max_col) ? zcol : max_col;
  987. if(vcol)
  988. max_col = (vcol > max_col) ? vcol : max_col;
  989. line = 0;
  990. first = TRUE;
  991. G_verbose_message(_("Scanning data ..."));
  992. while (0 != G_getl2(buff, BUFFSIZE - 1, fp)) {
  993. line++;
  994. if ((buff[0] == '#') || (buff[0] == '\0')) {
  995. continue; /* line is a comment or blank */
  996. }
  997. G_chop(buff); /* remove leading and trailing whitespace. unneded?? */
  998. tokens = G_tokenize(buff, fs);
  999. ntokens = G_number_of_tokens(tokens);
  1000. if ((ntokens < 3) || (max_col > ntokens)) {
  1001. if (skipline) {
  1002. G_warning(_("Not enough data columns. "
  1003. "Incorrect delimiter or column number? "
  1004. "Found the following character(s) in row %lu:\n[%s]"),
  1005. line, buff);
  1006. G_warning(_("Line ignored as requested"));
  1007. continue; /* line is garbage */
  1008. }
  1009. else {
  1010. G_fatal_error(_("Not enough data columns. "
  1011. "Incorrect delimiter or column number? "
  1012. "Found the following character(s) in row %lu:\n[%s]"),
  1013. line, buff);
  1014. }
  1015. }
  1016. /* too slow?
  1017. if ( G_projection() == PROJECTION_LL ) {
  1018. G_scan_easting( tokens[xcol-1], &x, region.proj);
  1019. G_scan_northing( tokens[ycol-1], &y, region.proj);
  1020. }
  1021. else {
  1022. */
  1023. if (1 != sscanf(tokens[xcol - 1], "%lf", &x))
  1024. G_fatal_error(_("Bad x-coordinate line %lu column %d. <%s>"), line,
  1025. xcol, tokens[xcol - 1]);
  1026. if (first) {
  1027. min_x = x;
  1028. max_x = x;
  1029. }
  1030. else {
  1031. if (x < min_x)
  1032. min_x = x;
  1033. if (x > max_x)
  1034. max_x = x;
  1035. }
  1036. if (1 != sscanf(tokens[ycol - 1], "%lf", &y))
  1037. G_fatal_error(_("Bad y-coordinate line %lu column %d. <%s>"), line,
  1038. ycol, tokens[ycol - 1]);
  1039. if (first) {
  1040. min_y = y;
  1041. max_y = y;
  1042. }
  1043. else {
  1044. if (y < min_y)
  1045. min_y = y;
  1046. if (y > max_y)
  1047. max_y = y;
  1048. }
  1049. if (1 != sscanf(tokens[zcol - 1], "%lf", &z))
  1050. G_fatal_error(_("Bad z-coordinate line %lu column %d. <%s>"), line,
  1051. zcol, tokens[zcol - 1]);
  1052. if (first) {
  1053. min_z = z;
  1054. max_z = z;
  1055. if(!vcol)
  1056. first = FALSE;
  1057. }
  1058. else {
  1059. if (z < min_z)
  1060. min_z = z;
  1061. if (z > max_z)
  1062. max_z = z;
  1063. }
  1064. if(vcol) {
  1065. if (1 != sscanf(tokens[vcol - 1], "%lf", &v))
  1066. G_fatal_error(_("Bad data value line %lu column %d. <%s>"), line,
  1067. vcol, tokens[vcol - 1]);
  1068. if (first) {
  1069. min_v = v;
  1070. max_v = v;
  1071. first = FALSE;
  1072. }
  1073. else {
  1074. if (v < min_v)
  1075. min_v = v;
  1076. if (v > max_v)
  1077. max_v = v;
  1078. }
  1079. }
  1080. G_free_tokens(tokens);
  1081. }
  1082. if (!shell_style) {
  1083. fprintf(stderr, _("Range: min max\n"));
  1084. fprintf(stdout, "x: %11.15g %11.15g\n", min_x, max_x);
  1085. fprintf(stdout, "y: %11.15g %11.15g\n", min_y, max_y);
  1086. fprintf(stdout, "z: %11.15g %11.15g\n", min_z * zscale, max_z * zscale);
  1087. if(vcol)
  1088. fprintf(stdout, "v: %11.15g %11.15g\n", min_v * vscale, max_v * vscale);
  1089. }
  1090. else {
  1091. fprintf(stdout, "n=%.15g s=%.15g e=%.15g w=%.15g b=%.15g t=%.15g",
  1092. max_y, min_y, max_x, min_x, min_z * zscale, max_z * zscale);
  1093. if(vcol)
  1094. fprintf(stdout, " min=%.15g max=%.15g\n", min_v * vscale,
  1095. max_v * vscale);
  1096. else
  1097. fprintf(stdout, "\n");
  1098. }
  1099. G_debug(1, "Processed %lu lines.", line);
  1100. G_debug(1, "region template: g.region n=%.15g s=%.15g e=%.15g w=%.15g",
  1101. max_y, min_y, max_x, min_x);
  1102. return 0;
  1103. }