main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007, 2010 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <ctype.h>
  23. #include <time.h>
  24. #include <sys/types.h>
  25. #ifdef HAVE_STATVFS_H
  26. #include <sys/statvfs.h>
  27. #endif
  28. extern "C" {
  29. #include <grass/gis.h>
  30. #include <grass/raster.h>
  31. #include <grass/glocale.h>
  32. }
  33. #include "option.h"
  34. #include "common.h" /* declares the globals */
  35. #include "fill.h"
  36. #include "flow.h"
  37. #include "nodata.h"
  38. #include "grass2str.h"
  39. #include "water.h"
  40. #include "sortutils.h"
  41. /* globals: in common.H
  42. extern statsRecorder *stats;
  43. extern userOptions* opt;
  44. extern struct Cell_head region;
  45. */
  46. /* #define JUMP2FLOW */
  47. /* define it only if you want to skip the flow direction computation
  48. and jump directly to computing flow accumulation; the flowstream
  49. must exist in /STREAM_DIR/flowStream */
  50. /* ---------------------------------------------------------------------- */
  51. void
  52. parse_args(int argc, char *argv[]) {
  53. /* input elevation grid */
  54. struct Option *input_elev;
  55. input_elev = G_define_standard_option(G_OPT_R_ELEV);
  56. /* output filled elevation grid */
  57. struct Option *output_elev;
  58. output_elev = G_define_standard_option(G_OPT_R_OUTPUT);
  59. output_elev->key = "filled";
  60. output_elev->description= _("Name for output filled (flooded) elevation raster map");
  61. output_elev->required = NO;
  62. output_elev->guisection = _("Outputs");
  63. /* output direction grid */
  64. struct Option *output_dir;
  65. output_dir = G_define_standard_option(G_OPT_R_OUTPUT);
  66. output_dir->key = "direction";
  67. output_dir->description= _("Name for output flow direction raster map");
  68. output_dir->required = NO;
  69. output_dir->guisection = _("Outputs");
  70. /* output sinkwatershed grid */
  71. struct Option *output_watershed;
  72. output_watershed = G_define_standard_option(G_OPT_R_OUTPUT);
  73. output_watershed->key = "swatershed";
  74. output_watershed->description= _("Name for output sink-watershed raster map");
  75. output_watershed->required = NO;
  76. output_watershed->guisection = _("Outputs");
  77. /* output flow accumulation grid */
  78. struct Option *output_accu;
  79. output_accu = G_define_standard_option(G_OPT_R_OUTPUT);
  80. output_accu->key = "accumulation";
  81. output_accu->description= _("Name for output flow accumulation raster map");
  82. output_accu->required = NO;
  83. output_accu->guisection = _("Outputs");
  84. #ifdef OUTPUT_TCI
  85. struct Option *output_tci;
  86. output_tci = G_define_standard_option(G_OPT_R_OUTPUT);
  87. output_tci->key = "tci";
  88. output_tci->description=
  89. _("Name for output topographic convergence index (tci) raster map");
  90. output_tci->required = NO;
  91. output_tci->guisection = _("Outputs");
  92. #endif
  93. /* MFD/SFD flag */
  94. struct Flag *sfd_flag;
  95. sfd_flag = G_define_flag() ;
  96. sfd_flag->key = 's';
  97. sfd_flag->label= _("SFD (D8) flow (default is MFD)");
  98. sfd_flag->description =
  99. _("SFD: single flow direction, MFD: multiple flow direction");
  100. /* D8CUT value*/
  101. struct Option *d8cut;
  102. d8cut = G_define_option();
  103. d8cut->key = "d8cut";
  104. d8cut->type = TYPE_DOUBLE;
  105. d8cut->required = NO;
  106. d8cut->label = _("Routing using SFD (D8) direction");
  107. d8cut->description =
  108. _("If flow accumulation is larger than this value it is routed using "
  109. "SFD (D8) direction (meaningful only for MFD flow). "
  110. "If no answer is given it defaults to infinity.");
  111. /* raster cache memory */
  112. struct Option *mem;
  113. mem = G_define_standard_option(G_OPT_MEMORYMB);
  114. /* temporary STREAM path */
  115. struct Option *streamdir;
  116. streamdir = G_define_option() ;
  117. streamdir->key = "directory";
  118. streamdir->type = TYPE_STRING;
  119. streamdir->required = NO;
  120. /* streamdir->answer = ""; */
  121. streamdir->description=
  122. _("Directory to hold temporary files (they can be large)");
  123. /* stats file */
  124. struct Option *stats_opt;
  125. stats_opt = G_define_option() ;
  126. stats_opt->key = "stats";
  127. stats_opt->type = TYPE_STRING;
  128. stats_opt->required = NO;
  129. stats_opt->description= _("Name for output file containing runtime statistics");
  130. stats_opt->guisection = _("Outputs");
  131. G_option_requires(input_elev, output_elev, output_dir, output_watershed,
  132. output_accu, output_tci, NULL);
  133. if (G_parser(argc, argv)) {
  134. exit (EXIT_FAILURE);
  135. }
  136. /* ************************* */
  137. assert(opt);
  138. opt->elev_grid = input_elev->answer;
  139. opt->filled_grid = output_elev->answer;
  140. opt->dir_grid = output_dir->answer;
  141. opt->watershed_grid = output_watershed->answer;
  142. opt->flowaccu_grid = output_accu->answer;
  143. #ifdef OUTPUT_TCI
  144. opt->tci_grid = output_tci->answer;
  145. #endif
  146. opt->d8 = sfd_flag->answer;
  147. if (!d8cut->answer) {
  148. opt->d8cut = MAX_ACCU;
  149. } else {
  150. opt->d8cut = atof(d8cut->answer);
  151. }
  152. opt->mem = atoi(mem->answer);
  153. if (!streamdir->answer) {
  154. const char *tmpdir = G_tempfile();
  155. if (G_mkdir(tmpdir) == -1)
  156. G_fatal_error(_("Unable to create temp dir"));
  157. opt->streamdir = G_store(tmpdir);
  158. }
  159. else
  160. opt->streamdir = streamdir->answer;
  161. opt->stats = stats_opt->answer;
  162. opt->verbose = G_verbose() == G_verbose_max();
  163. /* somebody should delete the options */
  164. }
  165. /* ---------------------------------------------------------------------- */
  166. /* check compatibility of map header and region header */
  167. void check_header(char* cellname) {
  168. const char *mapset;
  169. mapset = G_find_raster(cellname, "");
  170. if (mapset == NULL) {
  171. G_fatal_error(_("Raster map <%s> not found"), cellname);
  172. }
  173. /* read cell header */
  174. struct Cell_head cell_hd;
  175. Rast_get_cellhd (cellname, mapset, &cell_hd);
  176. /* check compatibility with module region */
  177. if (!((region->ew_res == cell_hd.ew_res)
  178. && (region->ns_res == cell_hd.ns_res))) {
  179. G_fatal_error(_("Raster map <%s> resolution differs from current region"),
  180. cellname);
  181. } else {
  182. G_verbose_message(_("Header of raster map <%s> compatible with region header"),
  183. cellname);
  184. }
  185. /* check type of input elevation raster and check if precision is lost */
  186. RASTER_MAP_TYPE data_type;
  187. data_type = Rast_map_type(opt->elev_grid, mapset);
  188. #ifdef ELEV_SHORT
  189. G_verbose_message(_("Elevation stored as SHORT (%dB)"),
  190. sizeof(elevation_type));
  191. if (data_type == FCELL_TYPE) {
  192. G_warning(_("Raster map <%s> is of type FCELL_TYPE "
  193. "-- precision may be lost"), opt->elev_grid);
  194. }
  195. if (data_type == DCELL_TYPE) {
  196. G_warning(_("Raster map <%s> is of type DCELL_TYPE "
  197. "-- precision may be lost"), opt->elev_grid);
  198. }
  199. #endif
  200. #ifdef ELEV_FLOAT
  201. G_verbose_message(_("Elevation stored as FLOAT (%ldB)"),
  202. sizeof(elevation_type));
  203. if (data_type == CELL_TYPE) {
  204. G_warning(_("Raster map <%s> is of type CELL_TYPE "
  205. "-- you should use r.terraflow.short"), opt->elev_grid);
  206. }
  207. if (data_type == DCELL_TYPE) {
  208. G_warning(_("Raster map <%s> is of type DCELL_TYPE "
  209. "-- precision may be lost"), opt->elev_grid);
  210. }
  211. #endif
  212. }
  213. /* ---------------------------------------------------------------------- */
  214. void check_args() {
  215. /* check if filled elevation grid name is valid */
  216. if (opt->filled_grid && G_legal_filename (opt->filled_grid) < 0) {
  217. G_fatal_error(_("<%s> is an illegal file name"), opt->filled_grid);
  218. }
  219. /* check if output grid names are valid */
  220. if (opt->dir_grid && G_legal_filename (opt->dir_grid) < 0) {
  221. G_fatal_error(_("<%s> is an illegal file name"), opt->dir_grid);
  222. }
  223. if (opt->flowaccu_grid && G_legal_filename (opt->flowaccu_grid) < 0) {
  224. G_fatal_error(_("<%s> is an illegal file name"), opt->flowaccu_grid);
  225. }
  226. if (opt->watershed_grid && G_legal_filename (opt->watershed_grid) < 0) {
  227. G_fatal_error(_("<%s> is an illegal file name"), opt->watershed_grid);
  228. }
  229. #ifdef OUTPUT_TCI
  230. if (opt->tci_grid && G_legal_filename (opt->tci_grid) < 0) {
  231. G_fatal_error(_("<%s> is an illegal file name"), opt->tci_grid);
  232. }
  233. #endif
  234. /* check compatibility with region */
  235. check_header(opt->elev_grid);
  236. /* what else ? */
  237. }
  238. /* ---------------------------------------------------------------------- */
  239. void record_args(int argc, char **argv) {
  240. time_t t = time(NULL);
  241. char buf[BUFSIZ];
  242. if(t == (time_t)-1) {
  243. perror("time");
  244. exit(1);
  245. }
  246. #ifdef __MINGW32__
  247. strcpy(buf, ctime(&t));
  248. #else
  249. ctime_r(&t, buf);
  250. buf[24] = '\0';
  251. #endif
  252. stats->timestamp(buf);
  253. *stats << "Command Line: " << endl;
  254. for(int i=0; i<argc; i++) {
  255. *stats << argv[i] << " ";
  256. }
  257. *stats << endl;
  258. *stats << "input elevation grid: " << opt->elev_grid << "\n";
  259. if (opt->filled_grid)
  260. *stats << "output (flooded) elevations grid: " << opt->filled_grid << "\n";
  261. if (opt->dir_grid)
  262. *stats << "output directions grid: " << opt->dir_grid << "\n";
  263. if (opt->watershed_grid)
  264. *stats << "output sinkwatershed grid: " << opt->watershed_grid << "\n";
  265. if (opt->flowaccu_grid)
  266. *stats << "output accumulation grid: " << opt->flowaccu_grid << "\n";
  267. #ifdef OUTPUT_TCI
  268. if (opt->tci_grid)
  269. *stats << "output tci grid: " << opt->tci_grid << "\n";
  270. #endif
  271. if (opt->d8) {
  272. stats ->comment("SFD (D8) flow direction");
  273. } else {
  274. stats->comment("MFD flow direction");
  275. }
  276. sprintf(buf, "D8CUT=%f", opt->d8cut);
  277. stats->comment(buf);
  278. size_t mm_size = (size_t) opt->mem << 20; /* (in bytes) */
  279. char tmp[100];
  280. formatNumber(tmp, mm_size);
  281. sprintf(buf, "Memory size: %s bytes", tmp);
  282. stats->comment(buf);
  283. }
  284. /* ---------------------------------------------------------------------- */
  285. void
  286. setFlowAccuColorTable(char* cellname) {
  287. struct Colors colors;
  288. const char *mapset;
  289. struct Range r;
  290. mapset = G_find_raster(cellname, "");
  291. if (mapset == NULL) {
  292. G_fatal_error (_("Raster map <%s> not found"), cellname);
  293. }
  294. if (Rast_read_range(cellname, mapset, &r) == -1) {
  295. G_fatal_error(_("cannot read range"));
  296. }
  297. /*G_debug(1, "%s range is: min=%d, max=%d\n", cellname, r.min, r.max);*/
  298. int v[6];
  299. v[0] = r.min;
  300. v[1] = 5;
  301. v[2] = 30;
  302. v[3] = 100;
  303. v[4] = 1000;
  304. v[5] = r.max;
  305. Rast_init_colors(&colors);
  306. Rast_add_c_color_rule(&v[0], 255,255,255, &v[1], 255,255,0, &colors);
  307. Rast_add_c_color_rule(&v[1], 255,255,0, &v[2], 0,255,255, &colors);
  308. Rast_add_c_color_rule(&v[2], 0,255,255, &v[3], 0,127,255, &colors);
  309. Rast_add_c_color_rule(&v[3], 0,127,255, &v[4], 0,0,255, &colors);
  310. Rast_add_c_color_rule(&v[4], 0,0,255, &v[5], 0,0,0, &colors);
  311. Rast_write_colors(cellname, mapset, &colors);
  312. Rast_free_colors(&colors);
  313. }
  314. /* ---------------------------------------------------------------------- */
  315. void
  316. setSinkWatershedColorTable(char* cellname) {
  317. struct Colors colors;
  318. const char *mapset;
  319. struct Range r;
  320. mapset = G_find_raster(cellname, "");
  321. if (mapset == NULL) {
  322. G_fatal_error (_("Raster map <%s> not found"), cellname);
  323. }
  324. if (Rast_read_range(cellname, mapset, &r) == -1) {
  325. G_fatal_error(_("cannot read range"));
  326. }
  327. Rast_init_colors(&colors);
  328. Rast_make_random_colors(&colors, 1, r.max);
  329. Rast_write_colors(cellname, mapset, &colors);
  330. Rast_free_colors(&colors);
  331. }
  332. /* print the largest interim file that will be generated during
  333. r.terraflow */
  334. void
  335. printMaxSortSize(long nodata_count) {
  336. char buf[BUFSIZ];
  337. off_t fillmaxsize = (off_t)nrows*ncols*sizeof(waterWindowType);
  338. off_t flowmaxsize = ((off_t)nrows*ncols - nodata_count)*sizeof(sweepItem);
  339. off_t maxneed = (fillmaxsize > flowmaxsize) ? fillmaxsize: flowmaxsize;
  340. maxneed = 2*maxneed; /* need 2*N to sort */
  341. G_debug(1, "total elements=%" PRI_OFF_T ", nodata elements=%ld",
  342. (off_t)nrows * ncols, nodata_count);
  343. G_debug(1, "largest temporary files: ");
  344. G_debug(1, "\t\t FILL: %s [%" PRI_OFF_T " elements, %ldB each]",
  345. formatNumber(buf, fillmaxsize),
  346. (off_t)nrows * ncols, sizeof(waterWindowType));
  347. G_debug(1, "\t\t FLOW: %s [%" PRI_OFF_T " elements, %ldB each]",
  348. formatNumber(buf, flowmaxsize),
  349. (off_t)nrows * ncols - nodata_count, sizeof(sweepItem));
  350. G_debug(1, "Will need at least %s space available in %s",
  351. formatNumber(buf, maxneed), /* need 2*N to sort */
  352. getenv(STREAM_TMPDIR));
  353. #ifdef HAVE_STATVFS_H
  354. G_debug(1, "Checking current space in %s: ", getenv(STREAM_TMPDIR));
  355. struct statvfs statbuf;
  356. statvfs(getenv(STREAM_TMPDIR), &statbuf);
  357. float avail = statbuf.f_bsize*statbuf.f_bavail;
  358. G_debug(1, "available %ld blocks x %ldB = %.0fB",
  359. (long)statbuf.f_bavail, statbuf.f_bsize, avail);
  360. if (avail > maxneed) {
  361. G_debug(1, ". OK.");
  362. } else {
  363. G_fatal_error("Not enough space available");
  364. }
  365. #endif
  366. }
  367. /* ---------------------------------------------------------------------- */
  368. int
  369. main(int argc, char *argv[]) {
  370. struct GModule *module;
  371. Rtimer rtTotal;
  372. char buf[BUFSIZ];
  373. /* initialize GIS library */
  374. G_gisinit(argv[0]);
  375. module = G_define_module();
  376. module->description = _("Performs flow computation for massive grids.");
  377. G_add_keyword(_("raster"));
  378. G_add_keyword(_("hydrology"));
  379. G_add_keyword(_("flow"));
  380. G_add_keyword(_("accumulation"));
  381. G_add_keyword(_("sink"));
  382. /* read user options; fill in global <opt> */
  383. opt = (userOptions*)malloc(sizeof(userOptions));
  384. assert(opt);
  385. region = (struct Cell_head*)malloc(sizeof(struct Cell_head));
  386. assert(region);
  387. parse_args(argc, argv);
  388. /* get the current region and dimensions */
  389. G_get_set_window(region);
  390. check_args();
  391. int nr = Rast_window_rows();
  392. int nc = Rast_window_cols();
  393. if ((nr > dimension_type_max) || (nc > dimension_type_max)) {
  394. G_fatal_error(_("[nrows=%d, ncols=%d] dimension_type overflow -- "
  395. "change dimension_type and recompile"), nr, nc);
  396. } else {
  397. nrows = (dimension_type)nr;
  398. ncols = (dimension_type)nc;
  399. }
  400. G_verbose_message( _("Region size is %d x %d"), nrows, ncols);
  401. /* check STREAM path (the place where intermediate STREAMs are placed) */
  402. sprintf(buf, "%s=%s",STREAM_TMPDIR, opt->streamdir);
  403. /* don't pass an automatic variable; putenv() isn't guaranteed to make a copy */
  404. putenv(G_store(buf));
  405. if (getenv(STREAM_TMPDIR) == NULL) {
  406. G_fatal_error(_("%s not set"), STREAM_TMPDIR);
  407. } else {
  408. G_verbose_message(_("STREAM temporary files in <%s>. "
  409. "THESE INTERMEDIATE STREAMS WILL NOT BE DELETED "
  410. "IN CASE OF ABNORMAL TERMINATION OF THE PROGRAM. "
  411. "TO SAVE SPACE PLEASE DELETE THESE FILES MANUALLY!"),
  412. getenv(STREAM_TMPDIR));
  413. }
  414. if (opt->stats) {
  415. /* open the stats file */
  416. stats = new statsRecorder(opt->stats);
  417. record_args(argc, argv);
  418. {
  419. char buf[BUFSIZ];
  420. off_t grid_size = (off_t) nrows * ncols;
  421. *stats << "region size = " << formatNumber(buf, grid_size) << " elts "
  422. << "(" << nrows << " rows x " << ncols << " cols)\n";
  423. stats->flush();
  424. }
  425. }
  426. /* set up STREAM memory manager */
  427. size_t mm_size = (size_t) opt->mem << 20; /* opt->mem is in MB */
  428. MM_manager.set_memory_limit(mm_size);
  429. if (opt->verbose) {
  430. MM_manager.warn_memory_limit();
  431. } else {
  432. MM_manager.ignore_memory_limit();
  433. }
  434. if (opt->verbose)
  435. MM_manager.print_limit_mode();
  436. /* initialize nodata */
  437. nodataType::init();
  438. if (stats)
  439. *stats << "internal nodata value: " << nodataType::ELEVATION_NODATA << endl;
  440. /* start timing -- after parse_args, which are interactive */
  441. rt_start(rtTotal);
  442. #ifndef JUMP2FLOW
  443. /* read elevation into a stream */
  444. AMI_STREAM<elevation_type> *elstr=NULL;
  445. long nodata_count;
  446. elstr = cell2stream<elevation_type>(opt->elev_grid, elevation_type_max,
  447. &nodata_count);
  448. /* print the largest interim file that will be generated */
  449. printMaxSortSize(nodata_count);
  450. /* -------------------------------------------------- */
  451. /* compute flow direction and filled elevation (and watersheds) */
  452. AMI_STREAM<direction_type> *dirstr=NULL;
  453. AMI_STREAM<elevation_type> *filledstr=NULL;
  454. AMI_STREAM<waterWindowBaseType> *flowStream=NULL;
  455. AMI_STREAM<labelElevType> *labeledWater = NULL;
  456. flowStream=computeFlowDirections(elstr, filledstr, dirstr, labeledWater);
  457. delete elstr;
  458. /* write streams to GRASS raster maps */
  459. if (opt->dir_grid)
  460. stream2_CELL(dirstr, nrows, ncols, opt->dir_grid);
  461. delete dirstr;
  462. if (opt->filled_grid) {
  463. #ifdef ELEV_SHORT
  464. stream2_CELL(filledstr, nrows, ncols, opt->filled_grid);
  465. #else
  466. stream2_CELL(filledstr, nrows, ncols, opt->filled_grid,true);
  467. #endif
  468. }
  469. delete filledstr;
  470. if (opt->watershed_grid) {
  471. stream2_CELL(labeledWater, nrows, ncols, labelElevTypePrintLabel(),
  472. opt->watershed_grid);
  473. setSinkWatershedColorTable(opt->watershed_grid);
  474. }
  475. delete labeledWater;
  476. #else
  477. AMI_STREAM<waterWindowBaseType> *flowStream;
  478. char path[GPATH_MAX];
  479. sprintf(path, "%s/flowStream", streamdir->answer);
  480. flowStream = new AMI_STREAM<waterWindowBaseType>(path);
  481. G_verbose_message(_("flowStream opened: len=%lld\n", flowStream->stream_len());
  482. G_verbose_message(_("jumping to flow accumulation computation\n");
  483. #endif
  484. /* -------------------------------------------------- */
  485. /* compute flow accumulation (and tci) */
  486. AMI_STREAM<sweepOutput> *outstr=NULL;
  487. computeFlowAccumulation(flowStream, outstr);
  488. /* delete flowStream -- deleted inside */
  489. /* write output stream to GRASS raster maps */
  490. #ifdef OUTPUT_TCI
  491. if (opt->flowaccu_grid && opt->tci_grid)
  492. stream2_FCELL(outstr, nrows, ncols, printAccumulation(), printTci(),
  493. opt->flowaccu_grid, opt->tci_grid);
  494. else if (opt->tci_grid)
  495. stream2_FCELL(outstr, nrows, ncols, printTci(),
  496. opt->tci_grid);
  497. else if (opt->flowaccu_grid)
  498. stream2_FCELL(outstr, nrows, ncols, printAccumulation(),
  499. opt->flowaccu_grid);
  500. #else
  501. if (opt->flowaccu_grid)
  502. stream2_FCELL(outstr, nrows, ncols, printAccumulation(),
  503. opt->flowaccu_grid);
  504. #endif
  505. if (opt->flowaccu_grid)
  506. setFlowAccuColorTable(opt->flowaccu_grid);
  507. delete outstr;
  508. rt_stop(rtTotal);
  509. if (stats) {
  510. stats->recordTime("Total running time: ", rtTotal);
  511. stats->timestamp("end");
  512. }
  513. G_done_msg(" ");
  514. /* free the globals */
  515. free(region);
  516. free(opt);
  517. if (stats)
  518. delete stats;
  519. return 0;
  520. }