[GRASS-SVN] r62686 - in grass-addons/grass7/raster: r.stream.basins r.stream.channel r.stream.distance r.stream.order r.stream.segment r.stream.snap r.stream.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 9 06:51:02 PST 2014


Author: mmetz
Date: 2014-11-09 06:51:02 -0800 (Sun, 09 Nov 2014)
New Revision: 62686

Modified:
   grass-addons/grass7/raster/r.stream.basins/basins_fill.c
   grass-addons/grass7/raster/r.stream.basins/basins_inputs.c
   grass-addons/grass7/raster/r.stream.basins/io.c
   grass-addons/grass7/raster/r.stream.basins/io.h
   grass-addons/grass7/raster/r.stream.basins/main.c
   grass-addons/grass7/raster/r.stream.channel/io.c
   grass-addons/grass7/raster/r.stream.channel/io.h
   grass-addons/grass7/raster/r.stream.channel/local_proto.h
   grass-addons/grass7/raster/r.stream.channel/local_vars.h
   grass-addons/grass7/raster/r.stream.channel/main.c
   grass-addons/grass7/raster/r.stream.channel/stream_topology.c
   grass-addons/grass7/raster/r.stream.channel/stream_write.c
   grass-addons/grass7/raster/r.stream.distance/distance_calc.c
   grass-addons/grass7/raster/r.stream.distance/io.c
   grass-addons/grass7/raster/r.stream.distance/io.h
   grass-addons/grass7/raster/r.stream.distance/main.c
   grass-addons/grass7/raster/r.stream.order/io.c
   grass-addons/grass7/raster/r.stream.order/io.h
   grass-addons/grass7/raster/r.stream.order/main.c
   grass-addons/grass7/raster/r.stream.order/stream_init.c
   grass-addons/grass7/raster/r.stream.order/stream_raster_close.c
   grass-addons/grass7/raster/r.stream.order/stream_topology.c
   grass-addons/grass7/raster/r.stream.segment/io.c
   grass-addons/grass7/raster/r.stream.segment/io.h
   grass-addons/grass7/raster/r.stream.segment/local_vars.h
   grass-addons/grass7/raster/r.stream.segment/main.c
   grass-addons/grass7/raster/r.stream.segment/stream_segment.c
   grass-addons/grass7/raster/r.stream.segment/stream_topology.c
   grass-addons/grass7/raster/r.stream.snap/io.c
   grass-addons/grass7/raster/r.stream.snap/io.h
   grass-addons/grass7/raster/r.stream.snap/main.c
   grass-addons/grass7/raster/r.stream.snap/snap.c
   grass-addons/grass7/raster/r.stream.stats/io.c
   grass-addons/grass7/raster/r.stream.stats/io.h
   grass-addons/grass7/raster/r.stream.stats/main.c
   grass-addons/grass7/raster/r.stream.stats/stats_calculate.c
   grass-addons/grass7/raster/r.stream.stats/stats_prepare.c
   grass-addons/grass7/raster/r.stream.stats/stats_print.c
Log:
r.stream.* addons: fix NULL handling and memory usage

Modified: grass-addons/grass7/raster/r.stream.basins/basins_fill.c
===================================================================
--- grass-addons/grass7/raster/r.stream.basins/basins_fill.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.basins/basins_fill.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -1,3 +1,5 @@
+#include "local_proto.h"
+
 /* 
    Link: a channel between junction
    Outlet: is final cell of every segment
@@ -18,7 +20,6 @@
 /* removal point: head + 1 */
 /* head == tail if last point removed or if only one free slot left */
 
-#include "local_proto.h"
 static int tail, head, fifo_count;
 
 int fifo_insert(POINT point)
@@ -32,6 +33,7 @@
 	tail = 0;
     }
     fifo_count++;
+
     return 0;
 }
 
@@ -94,6 +96,7 @@
     int next_r, next_c;
     int r, c, val, i, j;
     POINT n_cell;
+    int dirs_cell, basins_cell;
 
     tail = 0;
     head = -1;
@@ -115,8 +118,11 @@
 		continue;
 	    j = DIAG(i);
 
+	    dirs_cell = dirs[next_r][next_c];
+	    basins_cell = basins[next_r][next_c];
+
 	    /* contributing cell, not yet assigned to a basin */
-	    if (dirs[next_r][next_c] == j && basins[next_r][next_c] == 0) {
+	    if (dirs_cell == j && basins_cell == 0) {
 		basins[next_r][next_c] = val;
 		n_cell.r = next_r;
 		n_cell.c = next_c;
@@ -128,6 +134,7 @@
 	r = n_cell.r;
 	c = n_cell.c;
     }				/* end while */
+
     return 0;
 }
 
@@ -158,10 +165,10 @@
 		continue;
 	    j = DIAG(i);
 
-	    /* contributing cell, not yet assigned to a basin */
 	    Segment_get(basins, &basins_cell, next_r, next_c);
 	    Segment_get(dirs, &dirs_cell, next_r, next_c);
 
+	    /* contributing cell, not yet assigned to a basin */
 	    if (dirs_cell == j && basins_cell == 0) {
 		Segment_put(basins, &val, next_r, next_c);
 		n_cell.r = next_r;
@@ -173,7 +180,6 @@
 	n_cell = fifo_return_del();
 	r = n_cell.r;
 	c = n_cell.c;
-
     }				/* end while */
 
     return 0;

Modified: grass-addons/grass7/raster/r.stream.basins/basins_inputs.c
===================================================================
--- grass-addons/grass7/raster/r.stream.basins/basins_inputs.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.basins/basins_inputs.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -1,12 +1,10 @@
 #include <grass/gis.h>
 #include <grass/vector.h>
 #include <grass/glocale.h>
-
 #include "local_proto.h"
 
 int process_coors(char **answers)
 {
-
     int n, outlets_num;
     double X, Y;
     struct Cell_head window;
@@ -97,8 +95,9 @@
 {
     int i, cat;
     int r, c, d;		/* d: direction */
-    int outlets_num = 0;
-    int next_stream = -1, cur_stream;
+    int outlets_num;
+    int next_stream, cur_stream;
+    int streams_cell, dirs_cell;
     int out_max = ncols + nrows;
 
     categories = NULL;
@@ -117,34 +116,37 @@
 
     G_message("Finding nodes...");
     outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
+    outlets_num = 0;
 
-    for (r = 0; r < nrows; ++r)
-	for (c = 0; c < ncols; ++c)
-	    if (streams[r][c] > 0) {
+    for (r = 0; r < nrows; ++r) {
+	G_percent(r, nrows, 4);
+	for (c = 0; c < ncols; ++c) {
+	    streams_cell = streams[r][c];
+	    if (streams_cell > 0) {
 		if (outlets_num > 6 * (out_max - 1))
 		    G_fatal_error(_("Stream and direction maps probably do not match"));
 
-		if (outlets_num > (out_max - 1))
+		if (outlets_num > (out_max - 1)) {
 		    outlets =
 			(OUTLET *) G_realloc(outlets,
 					     out_max * 6 * sizeof(OUTLET));
+		}
 
-		d = abs(dirs[r][c]);	/* r.watershed */
+		dirs_cell = dirs[r][c];
+		d = abs(dirs_cell);	/* r.watershed */
 
-		if (NOT_IN_REGION(d))
+		if (NOT_IN_REGION(d) || d == 0)
 		    next_stream = -1;	/* border */
-		else
-		    next_stream = (streams[NR(d)][NC(d)] > 0) ?
-			streams[NR(d)][NC(d)] : -1;
+		else {
+		    next_stream = streams[NR(d)][NC(d)];
+		    if (next_stream < 1)
+			next_stream = -1;
+		}
 
-		if (d == 0)
-		    next_stream = -1;
+		cur_stream = streams_cell;
 
-		cur_stream = streams[r][c];
-
 		if (lasts) {
 		    if (next_stream < 0) {	/* is outlet! */
-
 			if (categories)
 			    if (categories[cur_stream] == -1)	/* but not in list */
 				continue;
@@ -157,9 +159,7 @@
 		    }
 		}
 		else {		/* not lasts */
-
 		    if (cur_stream != next_stream) {	/* is node or outlet! */
-
 			if (categories)
 			    if (categories[cur_stream] == -1)	/* but not in list */
 				continue;
@@ -172,6 +172,9 @@
 		    }
 		}		/* end if else lasts */
 	    }			/* end if streams */
+	}
+    }
+    G_percent(r, nrows, 4);
 
     return outlets_num;
 }
@@ -183,8 +186,8 @@
     int i, cat;
     int r, c, d;		/* d: direction */
     int outlets_num;
-    int next_stream = -1, cur_stream;
-    int streams_cell, dirs_cell, streams_next_cell;
+    int next_stream, cur_stream;
+    int streams_cell, dirs_cell;
     int out_max = ncols + nrows;
 
     categories = NULL;
@@ -204,7 +207,8 @@
     outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
     outlets_num = 0;
 
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
+	G_percent(r, nrows, 4);
 	for (c = 0; c < ncols; ++c) {
 
 	    Segment_get(streams, &streams_cell, r, c);
@@ -220,21 +224,17 @@
 		Segment_get(dirs, &dirs_cell, r, c);
 		d = abs(dirs_cell);	/* abs */
 
-		if (NOT_IN_REGION(d))
+		if (NOT_IN_REGION(d) || d == 0)
 		    next_stream = -1;	/* border */
 		else {
-		    Segment_get(streams, &streams_next_cell, NR(d), NC(d));
-		    next_stream =
-			(streams_next_cell > 0) ? streams_next_cell : -1;
+		    Segment_get(streams, &next_stream, NR(d), NC(d));
+		    if (next_stream < 1)
+			next_stream = -1;
 		}
 
-		if (d == 0)
-		    next_stream = -1;
-
 		cur_stream = streams_cell;
 
 		if (lasts) {
-
 		    if (next_stream < 0) {	/* is outlet! */
 			if (categories)
 			    if (categories[cur_stream] == -1)	/* but not in list */
@@ -248,7 +248,6 @@
 		    }
 		}
 		else {		/* not lasts */
-
 		    if (cur_stream != next_stream) {	/* is outlet or node! */
 			if (categories)
 			    if (categories[cur_stream] == -1)	/* but not in list */
@@ -263,5 +262,8 @@
 		}		/* end if else lasts */
 	    }			/* end if streams */
 	}			/* end for */
+    }
+    G_percent(r, nrows, 4);
+
     return outlets_num;
 }

Modified: grass-addons/grass7/raster/r.stream.basins/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.basins/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.basins/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -79,7 +79,7 @@
           G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
                           "Run 'g.region rast=%s' to set proper region resolution."),
                         input_map_name, input_map_name);
-    
+
     /* checking if input map is of required type */
     if (check_data_type != map->data_type)
 	G_debug(1,
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -239,7 +299,7 @@
    * free memory allocated for map, set pointer to null;
    */
     int r;
-    
+
     for (r = 0; r < map->nrows; ++r)
 	G_free((map->map)[r]);
     G_free(map->map);
@@ -311,7 +371,7 @@
     close(fd);
     if (0 > (fd = open(filename, 2))) {
 	unlink(filename);
-	G_fatal_error(_("Unable re-open file '%s'"), filename);
+	G_fatal_error(_("Unable to re-open file '%s'"), filename);
     }
 
     if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -365,7 +427,7 @@
 	    this_window.ns_res != cellhd.ns_res)
           G_fatal_error(_("Region resolution and raster map <%s> resolution differs. "
                           "Run 'g.region rast=%s' to set proper region resolution."),
-			  input_map_name, input_map_name);
+                        input_map_name, input_map_name);
 
     if (check_data_type != seg->data_type)
 	G_debug(1,
@@ -401,7 +463,7 @@
 
     /* end opening and checking */
 
-    G_message(_("Reading map <%s>"), input_map_name);
+    G_message(_("Reading raster map <%s>..."), input_map_name);
     input_buffer = Rast_allocate_buf(input_data_type);
 
     target_buffer = Rast_allocate_buf(seg->data_type);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,8 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to null at: %d %d"), r,
-			  c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.basins/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.basins/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.basins/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.basins/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.basins/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.basins/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -40,12 +40,15 @@
     int segmentation, zerofill, lasts, cats;
     int i, outlets_num = 0;
     int max_number_of_streams;
+    struct Range range;
+    CELL min, max;
+    struct Colors colors;
 
     G_gisinit(argv[0]);
 
     module = G_define_module();
     module->label = _("Delineates basins according stream network.");
-    module->description = _("Input can be stream network, vector point map  with outlets or outlet coordinates.");
+    module->description = _("Input can be stream network, vector point map with outlets or outlet coordinates.");
     G_add_keyword(_("raster"));
     G_add_keyword(_("hydrology"));
     G_add_keyword(_("stream network"));
@@ -141,13 +144,12 @@
 	MAP map_dirs, map_streams, map_basins;
 	CELL **streams = NULL, **dirs, **basins;
 
-	G_message("All in RAM calculation...");
+	G_message(_("All in RAM calculation..."));
 
 	ram_create_map(&map_dirs, CELL_TYPE);
-	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	dirs = (CELL **) map_dirs.map;
 
-
 	switch (b_test) {
 	case 1:
 	    G_message(_("Calculating basins using coordinates..."));
@@ -157,7 +159,7 @@
 	case 2:
 	    G_message(_("Calculating basins using streams..."));
 	    ram_create_map(&map_streams, CELL_TYPE);
-	    ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	    ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	    streams = (CELL **) map_streams.map;
 	    max_number_of_streams = (int)map_streams.max + 1;
 	    outlets_num = ram_process_streams(in_stm_cat_opt->answers,
@@ -173,13 +175,18 @@
 	}
 
 	ram_create_map(&map_basins, CELL_TYPE);
+	ram_reset_map(&map_basins, 0);
 	basins = (CELL **) map_basins.map;
 	ram_add_outlets(basins, outlets_num);
 	fifo_max = 4 * (nrows + ncols);
 	fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
 
-	for (i = 0; i < outlets_num; ++i)
+	G_message(_("Delineating basins for %d outlets..."), outlets_num);
+	for (i = 0; i < outlets_num; ++i) {
+	    G_percent(i, outlets_num, 4);
 	    ram_fill_basins(outlets[i], basins, dirs);
+	}
+	G_percent(i, outlets_num, 4);
 
 	G_free(fifo_points);
 	ram_write_map(&map_basins, opt_basins->answer, CELL_TYPE, zerofill,
@@ -188,23 +195,28 @@
 	ram_release_map(&map_basins);
 
     }				/* end ram */
-
     /* SEGMENT VERSION */
-
-    if (segmentation) {
+    else {
 	SEG map_dirs, map_streams, map_basins;
 	SEGMENT *streams = NULL, *dirs, *basins;
-	int number_of_segs, num_maps;
+	int number_of_segs;
+	double seg_size;
 
         G_message(_("Memory swap calculation (may take some time)..."));
 
-	num_maps = b_test == 2 ? 3 : 2;
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	number_of_segs = number_of_segs < 32 ? (int)(32 / 0.12) :
-					       (int)(number_of_segs * 4.0 / num_maps);
+	number_of_segs = atoi(opt_swapsize->answer);
+	if (number_of_segs < 3)
+	    number_of_segs = 3;
 
+	/* segment size in MB */
+	seg_size = sizeof(CELL) * 2.0 * SROWS * SCOLS / (1 << 20); 
+
+	number_of_segs = (int)(number_of_segs / seg_size);
+	if (number_of_segs < 10)
+	    number_of_segs = 10;
+
 	seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	dirs = &map_dirs.seg;
 
 	switch (b_test) {
@@ -217,7 +229,7 @@
 	    G_message(_("Calculating basins using streams..."));
 	    seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs,
 			   CELL_TYPE);
-	    seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	    seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	    streams = &map_streams.seg;
 	    max_number_of_streams = (int)map_streams.max + 1;
 	    outlets_num = seg_process_streams(in_stm_cat_opt->answers,
@@ -227,19 +239,24 @@
 	    break;
 
 	case 4:
-	    G_message(_("Calculate basins using vector point map..."));
+	    G_message(_("Calculating basins using vector point map..."));
 	    outlets_num = process_vector(in_point_opt->answer);
 	    break;
 	}
 
 	seg_create_map(&map_basins, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+	seg_reset_map(&map_basins, 0);
 	basins = &map_basins.seg;
 	seg_add_outlets(basins, outlets_num);
 	fifo_max = 4 * (nrows + ncols);
 	fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
 
-	for (i = 0; i < outlets_num; ++i)
+	G_message(_("Delineating basins for %d outlets..."), outlets_num);
+	for (i = 0; i < outlets_num; ++i) {
+	    G_percent(i, outlets_num, 4);
 	    seg_fill_basins(outlets[i], basins, dirs);
+	}
+	G_percent(i, outlets_num, 4);
 	G_free(fifo_points);
 	seg_write_map(&map_basins, opt_basins->answer, CELL_TYPE, zerofill,
 		      0);
@@ -247,5 +264,10 @@
 	seg_release_map(&map_basins);
     }
 
+    Rast_read_range(opt_basins->answer, G_mapset(), &range);
+    Rast_get_range_min_max(&range, &min, &max);
+    Rast_make_random_colors(&colors, min, max);
+    Rast_write_colors(opt_basins->answer, G_mapset(), &colors);
+
     exit(EXIT_SUCCESS);
 }

Modified: grass-addons/grass7/raster/r.stream.channel/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -235,9 +295,9 @@
 
 int ram_release_map(MAP *map)
 {
-    /* 
-     * free memory allocated for map, set pointer to null;
-     */
+  /* 
+   * free memory allocated for map, set pointer to null;
+   */
     int r;
 
     for (r = 0; r < map->nrows; ++r)
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,7 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to NULL at: %d %d"), r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.channel/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.channel/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/local_proto.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/local_proto.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -9,7 +9,6 @@
 int ram_number_of_streams(CELL **streams, CELL **dirs);
 int ram_find_contributing_cell(int r, int c, CELL **dirs, FCELL **elevation);
 
-int ram_set_null_output(DCELL **output);
 int ram_calculate_identifiers(CELL **identifier, int number_of_streams,
 			      int downstream);
 int ram_calculate_difference(DCELL **output, int number_of_streams,
@@ -36,7 +35,6 @@
 int seg_find_contributing_cell(int r, int c, SEGMENT *dirs,
 			       SEGMENT *elevation);
 
-int seg_set_null_output(SEGMENT *output);
 int seg_calculate_identifiers(SEGMENT *identifier, int number_of_streams,
 			      int downstream);
 int seg_calculate_difference(SEGMENT *output, int number_of_streams,

Modified: grass-addons/grass7/raster/r.stream.channel/local_vars.h
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/local_vars.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/local_vars.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -12,7 +12,7 @@
 	int stream_num;
 	int number_of_cells;
 	int order;
-	unsigned long int * points;
+	long int * points;
 	float * elevation;
 	double * distance;
 	unsigned int init_r;

Modified: grass-addons/grass7/raster/r.stream.channel/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -150,15 +150,19 @@
 	CELL **streams, **dirs, **identifier = NULL;
 	FCELL **elevation;
 	DCELL **output;
+	DCELL nullval;
 
 	G_message(_("All in RAM calculation - direction <%s>..."),
 		  method_name[downstream]);
+
+	Rast_set_d_null_value(&nullval, 1);
+
 	ram_create_map(&map_streams, CELL_TYPE);
-	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_dirs, CELL_TYPE);
-	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_elevation, FCELL_TYPE);
-	ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 
 	streams = (CELL **) map_streams.map;
 	dirs = (CELL **) map_dirs.map;
@@ -168,11 +172,13 @@
 	ram_build_streamlines(streams, dirs, elevation, number_of_streams);
 	ram_release_map(&map_streams);
 	ram_release_map(&map_dirs);
+	ram_release_map(&map_elevation);
 	ram_create_map(&map_output, DCELL_TYPE);
 	output = (DCELL **) map_output.map;	/* one output for all maps */
 
 	if (out_identifier_opt->answer) {
 	    ram_create_map(&map_identifier, CELL_TYPE);
+	    ram_reset_map(&map_identifier, 0);
 	    identifier = (CELL **) map_identifier.map;
 	    ram_calculate_identifiers(identifier, number_of_streams,
 				      downstream);
@@ -182,7 +188,7 @@
 	}
 
 	if (out_difference_opt->answer) {
-	    ram_set_null_output(output);
+	    ram_reset_map(&map_output, nullval);
 	    if (local)
 		ram_calculate_difference(output, number_of_streams,
 					 downstream);
@@ -193,7 +199,7 @@
 	}
 
 	if (out_distance_opt->answer) {
-	    ram_set_null_output(output);
+	    ram_reset_map(&map_output, nullval);
 	    if (local && !cells)
 		ram_calculate_local_distance(output, number_of_streams,
 					     downstream);
@@ -206,7 +212,7 @@
 	}
 
 	if (out_gradient_opt->answer) {
-	    ram_set_null_output(output);
+	    ram_reset_map(&map_output, nullval);
 	    if (local)
 		ram_calculate_local_gradient(output, number_of_streams,
 					     downstream);
@@ -217,7 +223,7 @@
 	}
 
 	if (out_curvature_opt->answer) {
-	    ram_set_null_output(output);
+	    ram_reset_map(&map_output, nullval);
 	    ram_calculate_curvature(output, number_of_streams, downstream);
 	    ram_write_map(&map_output, out_curvature_opt->answer, DCELL_TYPE,
 			  0, 0);
@@ -225,25 +231,35 @@
 
 	ram_release_map(&map_output);
     }
-
-
-    if (segmentation) {
+    else {
 	SEG map_dirs, map_streams, map_elevation, map_output, map_identifier;
 	SEGMENT *streams, *dirs, *elevation, *output, *identifier;
+	DCELL nullval;
+	double seg_size;
 
 	G_message(_("Calculating segments in direction <%s> (may take some time)..."),
 		  method_name[downstream]);
 
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
+	Rast_set_d_null_value(&nullval, 1);
 
+	number_of_segs = atoi(opt_swapsize->answer);
+	if (number_of_segs < 3)
+	    number_of_segs = 3;
+
+	/* segment size in MB */
+	seg_size = (sizeof(CELL) * 2.0 + sizeof(FCELL)) * SROWS * SCOLS / (1 << 20); 
+
+	number_of_segs = (int)(number_of_segs / seg_size);
+	if (number_of_segs < 10)
+	    number_of_segs = 10;
+
 	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
 		       FCELL_TYPE);
-	seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 
 	streams = &map_streams.seg;
 	dirs = &map_dirs.seg;
@@ -253,12 +269,14 @@
 	seg_build_streamlines(streams, dirs, elevation, number_of_streams);
 	seg_release_map(&map_streams);
 	seg_release_map(&map_dirs);
+	seg_release_map(&map_elevation);
 	seg_create_map(&map_output, SROWS, SCOLS, number_of_segs, DCELL_TYPE);
 	output = &map_output.seg;	/* one output for all maps */
 
 	if (out_identifier_opt->answer) {
 	    seg_create_map(&map_identifier, SROWS, SCOLS, number_of_segs,
 			   CELL_TYPE);
+	    seg_reset_map(&map_identifier, 0);
 	    identifier = &map_identifier.seg;
 	    seg_calculate_identifiers(identifier, number_of_streams,
 				      downstream);
@@ -268,7 +286,7 @@
 	}
 
 	if (out_difference_opt->answer) {
-	    seg_set_null_output(output);
+	    seg_reset_map(&map_output, nullval);
 	    if (local)
 		seg_calculate_difference(output, number_of_streams,
 					 downstream);
@@ -279,7 +297,7 @@
 	}
 
 	if (out_distance_opt->answer) {
-	    seg_set_null_output(output);
+	    seg_reset_map(&map_output, nullval);
 	    if (local && !cells)
 		seg_calculate_local_distance(output, number_of_streams,
 					     downstream);
@@ -292,7 +310,7 @@
 	}
 
 	if (out_gradient_opt->answer) {
-	    seg_set_null_output(output);
+	    seg_reset_map(&map_output, nullval);
 	    if (local)
 		seg_calculate_local_gradient(output, number_of_streams,
 					     downstream);
@@ -303,7 +321,7 @@
 	}
 
 	if (out_curvature_opt->answer) {
-	    seg_set_null_output(output);
+	    seg_reset_map(&map_output, nullval);
 	    seg_calculate_curvature(output, number_of_streams, downstream);
 	    seg_write_map(&map_output, out_curvature_opt->answer, DCELL_TYPE,
 			  0, 0);
@@ -312,5 +330,6 @@
 	seg_release_map(&map_output);
     }
     free_attributes(number_of_streams);
+
     exit(EXIT_SUCCESS);
 }

Modified: grass-addons/grass7/raster/r.stream.channel/stream_topology.c
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/stream_topology.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/stream_topology.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -34,7 +34,7 @@
 	    trib_num++;
     }
 
-    if (trib_num > 1)
+    if (trib_num > 1) {
 	for (i = 1; i < 9; ++i) {
 	    if (NOT_IN_REGION(i))
 		continue;
@@ -47,6 +47,7 @@
 		dirs[next_r][next_c] == j)
 		trib_num--;
 	}
+    }
 
     if (trib_num > 5)
 	G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match"));
@@ -63,11 +64,15 @@
     int r, c;
     int stream_num = 0;
 
-    for (r = 0; r < nrows; ++r)
-	for (c = 0; c < ncols; ++c)
-	    if (streams[r][c] > 0)
+    for (r = 0; r < nrows; ++r) {
+	for (c = 0; c < ncols; ++c) {
+	    if (streams[r][c] > 0) {
 		if (ram_trib_nums(r, c, streams, dirs) != 1)
 		    stream_num++;
+	    }
+	}
+    }
+
     return stream_num;
 }
 
@@ -90,9 +95,9 @@
     G_message(_("Finding inits..."));
     SA = stream_attributes;
 
-    for (r = 0; r < nrows; ++r)
-	for (c = 0; c < ncols; ++c)
-	    if (streams[r][c])
+    for (r = 0; r < nrows; ++r) {
+	for (c = 0; c < ncols; ++c) {
+	    if (streams[r][c] > 0) {
 		if (ram_trib_nums(r, c, streams, dirs) != 1) {	/* adding inits */
 		    if (stream_num > number_of_streams)
 			G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match"));
@@ -101,6 +106,9 @@
 		    SA[stream_num].init_r = r;
 		    SA[stream_num++].init_c = c;
 		}
+	    }
+	}
+    }
 
     for (i = 1; i < stream_num; ++i) {
 
@@ -123,8 +131,8 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 
-	SA[i].points = (unsigned long int *)
-	    G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+	SA[i].points = (long int *)
+	    G_malloc((SA[i].number_of_cells) * sizeof(long int));
 	SA[i].elevation = (float *)
 	    G_malloc((SA[i].number_of_cells) * sizeof(float));
 	SA[i].distance = (double *)
@@ -137,16 +145,20 @@
 	prev_c = NC(contrib_cell);
 
 	/* add one point contributing to init to calculate parameters */
-	/* what to do if there is no contributing points? */
+	/* what to do if there are no contributing points? */
 	SA[i].points[0] = (contrib_cell == 0) ? -1 : INDEX(prev_r, prev_c);
-	SA[i].elevation[0] = (contrib_cell == 0) ? -99999 :
-	    elevation[prev_r][prev_c];
+	SA[i].elevation[0] = -99999;
+	if (contrib_cell != 0 && !Rast_is_f_null_value(&elevation[prev_r][prev_c]))
+	    SA[i].elevation[0] = elevation[prev_r][prev_c];
 	d = (contrib_cell == 0) ? dirs[r][c] : dirs[prev_r][prev_c];
 	SA[i].distance[0] = (contrib_cell == 0) ? get_distance(r, c, d) :
 	    get_distance(prev_r, prev_c, d);
 
 	SA[i].points[1] = INDEX(r, c);
-	SA[i].elevation[1] = elevation[r][c];
+
+	SA[i].elevation[1] = -99999;
+	if (!Rast_is_f_null_value(&elevation[r][c]))
+	    SA[i].elevation[1] = elevation[r][c];
 	d = abs(dirs[r][c]);
 	SA[i].distance[1] = get_distance(r, c, d);
 
@@ -157,6 +169,9 @@
 	    if (NOT_IN_REGION(d) || d == 0) {
 		SA[i].points[cell_num] = -1;
 		SA[i].distance[cell_num] = SA[i].distance[cell_num - 1];
+		/* what if SA[i].elevation[cell_num - 1] == -99999 ||
+		 *         SA[i].elevation[cell_num - 2] == -99999 
+		 * ? */
 		SA[i].elevation[cell_num] =
 		    2 * SA[i].elevation[cell_num - 1] -
 		    SA[i].elevation[cell_num - 2];
@@ -166,6 +181,8 @@
 	    c = NC(d);
 	    SA[i].points[cell_num] = INDEX(r, c);
 	    SA[i].elevation[cell_num] = elevation[r][c];
+	    if (Rast_is_f_null_value(&SA[i].elevation[cell_num]))
+		SA[i].elevation[cell_num] = -99999;
 	    next_d = (abs(dirs[r][c]) == 0) ? d : abs(dirs[r][c]);
 	    SA[i].distance[cell_num] = get_distance(r, c, next_d);
 	    cell_num++;
@@ -173,6 +190,9 @@
 		G_fatal_error(_("To many points in stream line"));
 	} while (streams[r][c] == SA[i].order);
 
+	/* what if SA[i].elevation[1] == -99999 ||
+	 *         SA[i].elevation[2] == -99999 
+	 * ? */
 	if (SA[i].elevation[0] == -99999)
 	    SA[i].elevation[0] = 2 * SA[i].elevation[1] - SA[i].elevation[2];
     }
@@ -203,7 +223,7 @@
 	    trib_num++;
     }
 
-    if (trib_num > 1)
+    if (trib_num > 1) {
 	for (i = 1; i < 9; ++i) {
 	    if (NOT_IN_REGION(i))
 		continue;
@@ -216,6 +236,7 @@
 	    if (streams_next_cell == streams_cell && dirs_next_cell == j)
 		trib_num--;
 	}
+    }
 
     if (trib_num > 5)
 	G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
@@ -231,14 +252,16 @@
     int stream_num = 0;
     int streams_cell;
 
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 	    Segment_get(streams, &streams_cell, r, c);
-	    if (streams_cell > 0)
+	    if (streams_cell > 0) {
 		if (seg_trib_nums(r, c, streams, dirs) != 1)
 		    stream_num++;
+	    }
 	}
-    G_message("%d", stream_num);
+    }
+
     return stream_num;
 }
 
@@ -256,18 +279,17 @@
 
     stream_num = 1;
 
-
     stream_attributes =
 	(STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
     G_message("Finding inits...");
     SA = stream_attributes;
 
     /* finding inits */
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 	    Segment_get(streams, &streams_cell, r, c);
 
-	    if (streams_cell)
+	    if (streams_cell > 0) {
 		if (seg_trib_nums(r, c, streams, dirs) != 1) {	/* adding inits */
 		    if (stream_num > number_of_streams)
 			G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match"));
@@ -276,7 +298,9 @@
 		    SA[stream_num].init_r = r;
 		    SA[stream_num++].init_c = c;
 		}
+	    }
 	}
+    }
 
     /* building streamline */
     for (i = 1; i < stream_num; ++i) {
@@ -319,12 +343,13 @@
 	prev_c = NC(contrib_cell);
 
 	/* add one point contributing to init to calculate parameters */
-	/* what to do if there is no contributing points? */
+	/* what to do if there are no contributing points? */
 	SA[i].points[0] = (contrib_cell == 0) ? -1 : INDEX(prev_r, prev_c);
 
 	Segment_get(elevation, &elevation_prev_cell, prev_r, prev_c);
-	SA[i].elevation[0] = (contrib_cell == 0) ? -99999 :
-	    elevation_prev_cell;
+	SA[i].elevation[0] = -99999;
+	if (contrib_cell != 0 && !Rast_is_f_null_value(&elevation_prev_cell))
+	    SA[i].elevation[0] = elevation_prev_cell;
 
 	if (contrib_cell == 0)
 	    Segment_get(dirs, &d, r, c);
@@ -335,6 +360,9 @@
 
 	SA[i].points[1] = INDEX(r, c);
 	Segment_get(elevation, &(SA[i].elevation[1]), r, c);
+	if (Rast_is_f_null_value(&SA[i].elevation[1]))
+	    SA[i].elevation[1] = -99999;
+
 	Segment_get(dirs, &d, r, c);
 	SA[i].distance[1] = get_distance(r, c, d);
 
@@ -346,6 +374,9 @@
 	    if (NOT_IN_REGION(d) || d == 0) {
 		SA[i].points[cell_num] = -1;
 		SA[i].distance[cell_num] = SA[i].distance[cell_num - 1];
+		/* what if SA[i].elevation[cell_num - 1] == -99999 ||
+		 *         SA[i].elevation[cell_num - 2] == -99999 
+		 * ? */
 		SA[i].elevation[cell_num] =
 		    2 * SA[i].elevation[cell_num - 1] -
 		    SA[i].elevation[cell_num - 2];
@@ -355,6 +386,8 @@
 	    c = NC(d);
 	    SA[i].points[cell_num] = INDEX(r, c);
 	    Segment_get(elevation, &(SA[i].elevation[cell_num]), r, c);
+	    if (Rast_is_f_null_value(&SA[i].elevation[cell_num]))
+		SA[i].elevation[cell_num] = -99999;
 	    Segment_get(dirs, &next_d, r, c);
 	    next_d = (abs(next_d) == 0) ? d : abs(next_d);
 	    SA[i].distance[cell_num] = get_distance(r, c, next_d);
@@ -364,6 +397,9 @@
 		G_fatal_error(_("To much points in stream line..."));
 	} while (streams_cell == SA[i].order);
 
+	/* what if SA[i].elevation[1] == -99999 ||
+	 *         SA[i].elevation[2] == -99999 
+	 * ? */
 	if (SA[i].elevation[0] == -99999)
 	    SA[i].elevation[0] = 2 * SA[i].elevation[1] - SA[i].elevation[2];
     }
@@ -383,6 +419,7 @@
 	next_r = NR(i);
 	next_c = NC(i);
 	if (dirs[next_r][next_c] == DIAG(i) &&
+	    !Rast_is_f_null_value(&elevation[next_r][next_c]) &&
 	    elevation[next_r][next_c] < elev_min) {
 	    elev_min = elevation[next_r][next_c];
 	    j = i;
@@ -397,9 +434,9 @@
 {
     int i, j = 0;
     int next_r, next_c;
-    float elev_min = 9999;
+    FCELL elev_min = 9999;
     int dirs_next_cell;
-    float elevation_next_cell;
+    FCELL elevation_next_cell;
 
     for (i = 1; i < 9; ++i) {
 	if (NOT_IN_REGION(i))
@@ -408,7 +445,9 @@
 	next_c = NC(i);
 	Segment_get(dirs, &dirs_next_cell, next_r, next_c);
 	Segment_get(elevation, &elevation_next_cell, next_r, next_c);
-	if (dirs_next_cell == DIAG(i) && elevation_next_cell < elev_min) {
+	if (dirs_next_cell == DIAG(i) &&
+	    !Rast_is_f_null_value(&elevation_next_cell) &&
+	    elevation_next_cell < elev_min) {
 	    elev_min = elevation_next_cell;
 	    j = i;
 	}

Modified: grass-addons/grass7/raster/r.stream.channel/stream_write.c
===================================================================
--- grass-addons/grass7/raster/r.stream.channel/stream_write.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.channel/stream_write.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -1,29 +1,5 @@
 #include "local_proto.h"
-int ram_set_null_output(DCELL **output)
-{
-    int r;
 
-    for (r = 0; r < nrows; ++r)
-	Rast_set_d_null_value(output[r], ncols);
-
-    return 0;
-}
-
-int seg_set_null_output(SEGMENT *output)
-{
-    int r, c;
-    double output_cell;
-
-    for (r = 0; r < nrows; ++r) {
-	for (c = 0; c < ncols; ++c) {
-	    Rast_set_d_null_value(&output_cell, 1);
-	    Segment_put(output, &output_cell, r, c);
-	}
-    }
-
-    return 0;
-}
-
 int ram_calculate_identifiers(CELL **identifier, int number_of_streams,
 			      int downstream)
 {
@@ -79,20 +55,22 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 	cum_length = 0;
-	if (!downstream)
+	if (!downstream) {
 	    for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
 		cum_length += SA[i].distance[j];
 		r = (int)SA[i].points[j] / ncols;
 		c = (int)SA[i].points[j] % ncols;
 		output[r][c] = cum_length;
 	    }
-	else
+	}
+	else {
 	    for (j = SA[i].number_of_cells - 2; j > 0; --j) {
 		cum_length += SA[i].distance[j];
 		r = (int)SA[i].points[j] / ncols;
 		c = (int)SA[i].points[j] % ncols;
 		output[r][c] = cum_length;
 	    }
+	}
     }
 
     return 0;
@@ -110,20 +88,22 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 	cum_length = 0;
-	if (!downstream)
+	if (!downstream) {
 	    for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
 		cum_length += SA[i].distance[j];
 		r = (int)SA[i].points[j] / ncols;
 		c = (int)SA[i].points[j] % ncols;
 		Segment_put(output, &cum_length, r, c);
 	    }
-	else
+	}
+	else {
 	    for (j = SA[i].number_of_cells - 2; j > 0; --j) {
 		cum_length += SA[i].distance[j];
 		r = (int)SA[i].points[j] / ncols;
 		c = (int)SA[i].points[j] % ncols;
 		Segment_put(output, &cum_length, r, c);
 	    }
+	}
     }
 
     return 0;
@@ -187,6 +167,7 @@
     for (i = 1; i < number_of_streams; ++i) {
 
 	for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+	    /* what if any elevation is -99999 ? */
 	    result = downstream ?
 		SA[i].elevation[j - 1] - SA[i].elevation[j] :
 		SA[i].elevation[j] - SA[i].elevation[j + 1];
@@ -212,6 +193,7 @@
     for (i = 1; i < number_of_streams; ++i) {
 
 	for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+	    /* what if any elevation is -99999 ? */
 	    output_cell = downstream ?
 		SA[i].elevation[j - 1] - SA[i].elevation[j] :
 		SA[i].elevation[j] - SA[i].elevation[j + 1];
@@ -234,6 +216,7 @@
     SA = stream_attributes;
 
     for (i = 1; i < number_of_streams; ++i) {
+	/* what if any elevation is -99999 ? */
 	if (!downstream) {
 	    init = SA[i].elevation[1];
 	    for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
@@ -267,6 +250,7 @@
     SA = stream_attributes;
 
     for (i = 1; i < number_of_streams; ++i) {
+	/* what if any elevation is -99999 ? */
 	if (!downstream) {
 	    init = SA[i].elevation[1];
 	    for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
@@ -303,6 +287,7 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 	cum_length = 0;
+	/* what if any elevation is -99999 ? */
 	if (!downstream) {
 	    init = SA[i].elevation[0];
 	    for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
@@ -340,6 +325,7 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 	cum_length = 0;
+	/* what if any elevation is -99999 ? */
 	if (!downstream) {
 	    init = SA[i].elevation[1];
 	    for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
@@ -381,6 +367,7 @@
 	for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
 	    r = (int)SA[i].points[j] / ncols;
 	    c = (int)SA[i].points[j] % ncols;
+	    /* what if any elevation is -99999 ? */
 	    elev_diff =
 		(SA[i].elevation[j] - SA[i].elevation[j + 1]) <
 		0 ? 0 : (SA[i].elevation[j] - SA[i].elevation[j + 1]);
@@ -407,6 +394,7 @@
 	for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
 	    r = (int)SA[i].points[j] / ncols;
 	    c = (int)SA[i].points[j] % ncols;
+	    /* what if any elevation is -99999 ? */
 	    elev_diff =
 		(SA[i].elevation[j] - SA[i].elevation[j + 1]) <
 		0 ? 0 : (SA[i].elevation[j] - SA[i].elevation[j + 1]);
@@ -478,6 +466,7 @@
 	for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
 	    r = (int)SA[i].points[j] / ncols;
 	    c = (int)SA[i].points[j] % ncols;
+	    /* what if any elevation is -99999 ? */
 	    first_derivative =
 		(SA[i].elevation[j - 1] -
 		 SA[i].elevation[j + 1]) / (SA[i].distance[j - 1] +
@@ -512,6 +501,7 @@
 	for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
 	    r = (int)SA[i].points[j] / ncols;
 	    c = (int)SA[i].points[j] % ncols;
+	    /* what if any elevation is -99999 ? */
 	    first_derivative =
 		(SA[i].elevation[j - 1] -
 		 SA[i].elevation[j + 1]) / (SA[i].distance[j - 1] +

Modified: grass-addons/grass7/raster/r.stream.distance/distance_calc.c
===================================================================
--- grass-addons/grass7/raster/r.stream.distance/distance_calc.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.distance/distance_calc.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -105,6 +105,7 @@
 		}		/* end stream mode */
 
 		if (elevation) {
+		    /* TODO: check for NULL value */
 		    elevation[next_r][next_c] =
 			elevation[next_r][next_c] - target_elev;
 		    n_cell.target_elev = target_elev;
@@ -212,6 +213,7 @@
 		}		/* end stream mode */
 
 		if (elevation) {
+		    /* TODO: check for NULL value */
 		    Segment_get(elevation, &elevation_cell, next_r, next_c);
 		    elevation_cell -= target_elev;
 		    Segment_put(elevation, &elevation_cell, next_r, next_c);
@@ -232,6 +234,7 @@
 	target_elev = n_cell.target_elev;
 
     }				/* end while */
+
     return 0;
 }
 
@@ -354,12 +357,13 @@
 
     if (elevation) {
 	elevation_data_size = Rast_cell_size(DCELL_TYPE);
-	for (r = 0; r < nrows; ++r)
+	for (r = 0; r < nrows; ++r) {
 	    memcpy(tmp_elevation[r], elevation[r],
 		   ncols * elevation_data_size);
+	}
     }
 
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 
 	    for (i = 1; i < 9; ++i) {
@@ -379,11 +383,12 @@
 	    else if (dirs[r][c] > 0)
 		distance[r][c] = -1;
 	}
+    }
 
     d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
 
     k = 0;
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 
 	    if (distance[r][c] == 1) {
@@ -401,13 +406,14 @@
 		d_inits[k].c = c;
 		d_inits[k].cur_dist = 0;
 
-
+		/* TODO: check for NULL value */
 		if (elevation)
 		    d_inits[k].target_elev = tmp_elevation[r][c];
 
 		k++;
 	    }
 	}
+    }
 
     counter = n_inits = k;
     /* return 0; */
@@ -444,6 +450,7 @@
 	    if (done) {
 		distance[next_r][next_c] = cur_dist;
 		if (elevation) {
+		    /* TODO: check for NULL value */
 		    elevation[next_r][next_c] =
 			target_elev - tmp_elevation[next_r][next_c];
 		}
@@ -454,6 +461,7 @@
 		d_inits[k].c = next_c;
 		d_inits[k].cur_dist = cur_dist;
 
+		/* TODO: check for NULL value */
 		if (elevation)
 		    d_inits[k].target_elev = target_elev;
 		k++;
@@ -462,6 +470,7 @@
 	n_inits = k;
     }
     G_percent((counter - n_inits), counter, 10);
+
     return 0;
 }
 
@@ -493,14 +502,15 @@
 
     if (elevation) {
         /* elevation_data_size = Rast_cell_size(DCELL_TYPE); */
-	for (r = 0; r < nrows; ++r)
+	for (r = 0; r < nrows; ++r) {
 	    for (c = 0; c < ncols; ++c) {
 		Segment_get(elevation, &elevation_cell, r, c);
 		Segment_put(tmp_elevation, &elevation_cell, r, c);
 	    }
+	}
     }
 
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 
 	    Segment_get(distance, &distance_cell, r, c);
@@ -531,11 +541,12 @@
 	    }
 
 	}
+    }
 
     d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
 
     k = 0;
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 
 	    Segment_get(distance, &distance_cell, r, c);
@@ -561,6 +572,7 @@
 		k++;
 	    }
 	}
+    }
 
     counter = n_inits = k;
 
@@ -601,6 +613,7 @@
 	    if (done) {
 		Segment_put(distance, &cur_dist, next_r, next_c);
 		if (elevation) {
+		    /* TODO: check for NULL value */
 		    Segment_get(tmp_elevation, &tmp_elevation_cell, next_r,
 				next_c);
 		    tmp_elevation_cell = target_elev - tmp_elevation_cell;
@@ -616,6 +629,7 @@
 		d_inits[k].c = next_c;
 		d_inits[k].cur_dist = cur_dist;
 
+		/* TODO: check for NULL value */
 		if (elevation)
 		    d_inits[k].target_elev = target_elev;
 		k++;
@@ -624,5 +638,6 @@
 	n_inits = k;
     }
     G_percent(1, 1, 1);
+
     return 0;
 }

Modified: grass-addons/grass7/raster/r.stream.distance/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.distance/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.distance/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -235,9 +295,9 @@
 
 int ram_release_map(MAP *map)
 {
-    /* 
-     * free memory allocated for map, set pointer to null;
-     */
+  /* 
+   * free memory allocated for map, set pointer to null;
+   */
     int r;
 
     for (r = 0; r < map->nrows; ++r)
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,7 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to NULL at: %d %d"), r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.distance/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.distance/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.distance/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.distance/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.distance/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.distance/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -123,7 +123,8 @@
     if (out_diff_opt->answer && !in_elev_opt->answer)
 	G_fatal_error(_("Output elevation difference raster map requires "
                         "input elevation raster map to be specified"));
-    
+
+    method = -1;
     if (!strcmp(in_method_opt->answer, "upstream"))
 	method = UPSTREAM;
     else if (!strcmp(in_method_opt->answer, "downstream"))
@@ -142,19 +143,23 @@
     fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
 
     if (!segmentation) {
-	G_message(_("All in RAM calculation - method <%s>..."),
-		  method_name[method]);
 	MAP map_dirs, map_streams, map_distance, map_elevation,
 	    map_tmp_elevation;
 	CELL **streams, **dirs;
 	DCELL **distance;
 	DCELL **elevation = NULL;
 	DCELL **tmp_elevation = NULL;
+	DCELL nullval;
 
+	G_message(_("All in RAM calculation - method <%s>..."),
+		  method_name[method]);
+
+	Rast_set_d_null_value(&nullval, 1);
+
 	ram_create_map(&map_streams, CELL_TYPE);
-	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_dirs, CELL_TYPE);
-	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_distance, DCELL_TYPE);
 
 	streams = (CELL **) map_streams.map;
@@ -169,11 +174,10 @@
 
 	if (in_elev_opt->answer) {
 	    ram_create_map(&map_elevation, DCELL_TYPE);
-	    ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	    ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 	    elevation = (DCELL **) map_elevation.map;
 	}			/* map elevation will be replaced by elevation difference map */
 
-
 	if (method == DOWNSTREAM) {
 	    G_message(_("Calculate downstream parameters..."));
 	    for (j = 0; j < outlets_num; ++j) {
@@ -182,20 +186,23 @@
 					 outlets[j], outs);
 	    }
 	    G_percent(j, outlets_num, 1);
-
 	}
 	else if (method == UPSTREAM) {
 
-	    if (out_diff_opt->answer) {
+	    if (elevation) {
 		ram_create_map(&map_tmp_elevation, DCELL_TYPE);
 		tmp_elevation = (DCELL **) map_tmp_elevation.map;
 	    }
 
 	    for (j = 0; j < outlets_num; ++j)
 		ram_fill_basins(outlets[j], distance, dirs);
+
 	    ram_calculate_upstream(distance, dirs, elevation, tmp_elevation,
 				   near);
 
+	    if (elevation) {
+		ram_release_map(&map_tmp_elevation);
+	    }
 	}
 	else {
 	    G_fatal_error(_("Unrecognised method of processing"));
@@ -216,29 +223,41 @@
 
 	if (in_elev_opt->answer)
 	    ram_release_map(&map_elevation);
-	if (in_elev_opt->answer && method == UPSTREAM)
-	    ram_release_map(&map_tmp_elevation);
     }
-
-    if (segmentation) {
-	G_message(_("Calculating segments in direction <%s> (may take some time)..."),
-		  method_name[method]);
+    else {
 	SEG map_dirs, map_streams, map_distance, map_elevation,
 	    map_tmp_elevation;
 	SEGMENT *streams, *dirs, *distance;
 	SEGMENT *elevation = NULL;
 	SEGMENT *tmp_elevation = NULL;
+	DCELL nullval;
+	double seg_size;
 
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	if (method == DOWNSTREAM)
-	    number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
-	else			/* two elevation maps */
-	    number_of_segs = number_of_segs < 32 ? (int)(32 / 0.24) : number_of_segs / 0.24;
+	G_message(_("Calculating segments in direction <%s> (may take some time)..."),
+		  method_name[method]);
 
+	Rast_set_d_null_value(&nullval, 1);
+
+	number_of_segs = atoi(opt_swapsize->answer);
+	if (number_of_segs < 3)
+	    number_of_segs = 3;
+
+	/* segment size in MB */
+	if (method == UPSTREAM && in_elev_opt->answer) {
+	    seg_size = (sizeof(CELL) * 2.0 + sizeof(DCELL) * 2.0) * SROWS * SCOLS / (1 << 20);
+	}
+	else {
+	    seg_size = (sizeof(CELL) * 2.0 + sizeof(DCELL) * 1.0) * SROWS * SCOLS / (1 << 20);
+	}
+
+	number_of_segs = (int)(number_of_segs / seg_size);
+	if (number_of_segs < 10)
+	    number_of_segs = 10;
+
 	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_distance, SROWS, SCOLS, number_of_segs,
 		       DCELL_TYPE);
 
@@ -255,7 +274,7 @@
 	if (in_elev_opt->answer) {
 	    seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
 			   DCELL_TYPE);
-	    seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	    seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 	    elevation = &map_elevation.seg;
 	}			/* map elevation will be replaced by elevation difference map */
 
@@ -271,7 +290,7 @@
 	}
 	else if (method == UPSTREAM) {
 
-	    if (out_diff_opt->answer) {
+	    if (elevation) {
 		seg_create_map(&map_tmp_elevation, SROWS, SCOLS,
 			       number_of_segs, DCELL_TYPE);
 		tmp_elevation = &map_tmp_elevation.seg;
@@ -279,9 +298,12 @@
 
 	    for (j = 0; j < outlets_num; ++j)
 		seg_fill_basins(outlets[j], distance, dirs);
+
 	    seg_calculate_upstream(distance, dirs, elevation, tmp_elevation,
 				   near);
-
+	    if (elevation) {
+		seg_release_map(&map_tmp_elevation);
+	    }
 	}
 	else {
 	    G_fatal_error(_("Unrecognised method of processing"));
@@ -302,10 +324,9 @@
 
 	if (in_elev_opt->answer)
 	    seg_release_map(&map_elevation);
-	if (in_elev_opt->answer && method == UPSTREAM)
-	    seg_release_map(&map_tmp_elevation);
     }
 
     G_free(fifo_points);
+
     exit(EXIT_SUCCESS);
 }

Modified: grass-addons/grass7/raster/r.stream.order/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.order/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.order/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -235,9 +295,9 @@
 
 int ram_release_map(MAP *map)
 {
-    /* 
-     * free memory allocated for map, set pointer to null;
-     */
+  /* 
+   * free memory allocated for map, set pointer to null;
+   */
     int r;
 
     for (r = 0; r < map->nrows; ++r)
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,7 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to NULL at: %d %d"), r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.order/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.order/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.order/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.order/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.order/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.order/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -35,8 +35,8 @@
 	{"accumulation", NO, _("Name of input accumulation raster map")}
     };
 
-    /* add new basic rdering here and in local_vars.h declaration 
-     * it will be added to output without addational programing.
+    /* add new basic ordering here and in local_vars.h declaration 
+     * it will be added to output without additional programing.
      * Ordering functions are to be added in stream_order.c file.
      * Addational operations may shall be done in common part sections
      * derivative orders (like Scheideggers/Shreve) shall be added only 
@@ -57,7 +57,6 @@
     struct Flag *flag_zerofill, *flag_accum, *flag_segmentation;
 
     int output_num = 0;
-    /* int num_seg; */		/* number of segments */ 
     int segmentation, zerofill;
     int i;			/* iteration vars */
     int number_of_segs;
@@ -132,9 +131,6 @@
     use_accum = (flag_accum->answer != 0);
     use_vector = (opt_vector->answer != NULL);
 
-    number_of_segs = (int)atof(opt_swapsize->answer);
-    number_of_segs = number_of_segs < 32 ? (int)(32 / 0.12) : number_of_segs / 0.12;
-
     if (use_vector)
 	if (!opt_input[o_elev]->answer || !opt_input[o_accum]->answer)
 	    G_fatal_error(_("To calculate vector map both accumulation and elevation raster maps are required"));
@@ -167,14 +163,15 @@
 
     /* ALL IN RAM VERSION */
     if (!segmentation) {
-        G_message(_("All in RAM calculation..."));
 	MAP map_streams, map_dirs;
 	CELL **streams, **dirs;
 
+        G_message(_("All in RAM calculation..."));
+
 	ram_create_map(&map_streams, CELL_TYPE);
-	ram_read_map(&map_streams, in_streams, 1, CELL_TYPE);
+	ram_read_map(&map_streams, in_streams, 1, CELL_TYPE, 0);
 	ram_create_map(&map_dirs, CELL_TYPE);
-	ram_read_map(&map_dirs, in_dirs, 1, CELL_TYPE);
+	ram_read_map(&map_dirs, in_dirs, 1, CELL_TYPE, 0);
 	stream_init((int)map_streams.min, (int)map_streams.max);
 	number_of_streams = (int)(map_streams.max + 1);
 	streams = (CELL **) map_streams.map;
@@ -218,18 +215,29 @@
 	ram_release_map(&map_dirs);
 	/* end ram section */
     }
-
     /* SEGMENTATION VERSION */
-    if (segmentation) {
-        G_message(_("Memory swap calculation (may take some time)..."));
-
+    else {
 	SEG map_streams, map_dirs;
 	SEGMENT *streams, *dirs;
+	double seg_size;
 
+        G_message(_("Memory swap calculation (may take some time)..."));
+
+	number_of_segs = atoi(opt_swapsize->answer);
+	if (number_of_segs < 3)
+	    number_of_segs = 3;
+
+	/* segment size in MB */
+	seg_size = sizeof(CELL) * 2.0 * SROWS * SCOLS / (1 << 20); 
+
+	number_of_segs = (int)(number_of_segs / seg_size);
+	if (number_of_segs < 10)
+	    number_of_segs = 10;
+
 	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_streams, 1, CELL_TYPE);
+	seg_read_map(&map_streams, in_streams, 1, CELL_TYPE, 0);
 	seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_dirs, in_dirs, 1, CELL_TYPE);
+	seg_read_map(&map_dirs, in_dirs, 1, CELL_TYPE, 0);
 	stream_init((int)map_streams.min, (int)map_streams.max);
 	number_of_streams = (int)(map_streams.max + 1);
 	streams = &map_streams.seg;
@@ -274,7 +282,6 @@
 	/* end segmentation section */
     }
 
-
     /* free */
     G_free(stream_attributes);
     G_free(init_streams);
@@ -282,5 +289,4 @@
     G_free(init_cells);
 
     exit(EXIT_SUCCESS);
-
 }

Modified: grass-addons/grass7/raster/r.stream.order/stream_init.c
===================================================================
--- grass-addons/grass7/raster/r.stream.order/stream_init.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.order/stream_init.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -92,6 +92,7 @@
     input_data_type = Rast_map_type(input_map_name, mapset);
     input_data_size = Rast_cell_size(input_data_type);
     input_buffer = Rast_allocate_buf(input_data_type);
+    input_ptr = input_buffer;
 
     for (i = 0; i < (number_of_streams); ++i) {
 	if (pointers_to_stream[i]->stream == -1)
@@ -106,9 +107,9 @@
 	    c = (int)pointers_to_stream[i]->init % ncols;
 	}
 
-	if (r > cur_row) {
+	if (r != cur_row) {
 	    Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	    input_ptr = input_buffer;
+	    cur_row = r;
 	}
 	switch (what) {
 	case 0:		/* inits for elev */
@@ -130,5 +131,6 @@
     }
     G_free(input_buffer);
     G_free(pointers_to_stream);
+
     return 0;
 }

Modified: grass-addons/grass7/raster/r.stream.order/stream_raster_close.c
===================================================================
--- grass-addons/grass7/raster/r.stream.order/stream_raster_close.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.order/stream_raster_close.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -38,7 +38,8 @@
 		Rast_set_c_null_value(output_buffer, ncols);
 
 	    for (c = 0; c < ncols; ++c)
-		if (streams_buffer[c])
+		if (!Rast_is_c_null_value(&streams_buffer[c]) && 
+		    streams_buffer[c] > 0)
 		    output_buffer[c] = all_orders[i][streams_buffer[c]];
 
 	    Rast_put_c_row(output_fd[i], output_buffer);
@@ -106,7 +107,8 @@
 		Rast_set_c_null_value(output_buffer, ncols);
 
 	    for (c = 0; c < ncols; ++c)
-		if (!Rast_is_c_null_value(&streams_buffer[c]))
+		if (!Rast_is_c_null_value(&streams_buffer[c]) && 
+		    streams_buffer[c] > 0)
 		    output_buffer[c] = all_orders[i][streams_buffer[c]];
 
 	    Rast_put_c_row(output_fd[i], output_buffer);

Modified: grass-addons/grass7/raster/r.stream.order/stream_topology.c
===================================================================
--- grass-addons/grass7/raster/r.stream.order/stream_topology.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.order/stream_topology.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -48,7 +48,7 @@
 
     for (r = 0; r < nrows; ++r)
 	for (c = 0; c < ncols; ++c)
-	    if (streams[r][c]) {
+	    if (streams[r][c] > 0) {
 		trib_num = ram_number_of_tribs(r, c, streams, dirs);
 		trib = 0;
 		d = abs(dirs[r][c]);	/* r.watershed! */
@@ -261,7 +261,7 @@
 	    Segment_get(streams, &streams_cell, r, c);
 	    Segment_get(dirs, &dirs_cell, r, c);
 
-	    if (streams_cell) {
+	    if (streams_cell > 0) {
 		trib_num = seg_number_of_tribs(r, c, streams, dirs);
 		trib = 0;
 

Modified: grass-addons/grass7/raster/r.stream.segment/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.segment/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.segment/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -235,9 +295,9 @@
 
 int ram_release_map(MAP *map)
 {
-    /* 
-     * free memory allocated for map, set pointer to null;
-     */
+  /* 
+   * free memory allocated for map, set pointer to null;
+   */
     int r;
 
     for (r = 0; r < map->nrows; ++r)
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,7 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to NULL at: %d %d"), r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.segment/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.segment/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.segment/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.segment/local_vars.h
===================================================================
--- grass-addons/grass7/raster/r.stream.segment/local_vars.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.segment/local_vars.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -15,7 +15,7 @@
 	int next_stream;
 	int number_of_cells;
 	int order;
-	unsigned long int * points;
+	long int * points;
 	float * elevation;
 	double * distance;
 	unsigned long int init;

Modified: grass-addons/grass7/raster/r.stream.segment/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.segment/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.segment/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -139,20 +139,22 @@
     Rast_get_window(&window);
     G_begin_distance_calculations();
 
-
-
     if (!segmentation) {
 	MAP map_dirs, map_streams, map_elevation, map_unique_streams;
 	CELL **streams, **dirs, **unique_streams = NULL;
 	FCELL **elevation;
+	DCELL nullval;
 
 	G_message(_("All in RAM calculation..."));
+
+	Rast_set_d_null_value(&nullval, 1);
+
 	ram_create_map(&map_streams, CELL_TYPE);
-	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_dirs, CELL_TYPE);
-	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_elevation, FCELL_TYPE);
-	ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 
 	streams = (CELL **) map_streams.map;
 	dirs = (CELL **) map_dirs.map;
@@ -176,25 +178,36 @@
 	ram_release_map(&map_dirs);
 	ram_release_map(&map_elevation);
     }
-
-
-    if (segmentation) {
+    else {
 	SEG map_dirs, map_streams, map_elevation, map_unique_streams;
 	SEGMENT *streams, *dirs, *unique_streams = NULL;
 	SEGMENT *elevation;
+	DCELL nullval;
 	int number_of_segs;
+	double seg_size;
 
         G_message(_("Memory swap calculation (may take some time)..."));
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
 
+	Rast_set_d_null_value(&nullval, 1);
+
+	number_of_segs = atoi(opt_swapsize->answer);
+	if (number_of_segs < 3)
+	    number_of_segs = 3;
+
+	/* segment size in MB */
+	seg_size = (sizeof(CELL) * 2.0 + sizeof(FCELL)) * SROWS * SCOLS / (1 << 20); 
+
+	number_of_segs = (int)(number_of_segs / seg_size);
+	if (number_of_segs < 10)
+	    number_of_segs = 10;
+
 	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
 		       FCELL_TYPE);
-	seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 
 	streams = &map_streams.seg;
 	dirs = &map_dirs.seg;
@@ -286,6 +299,6 @@
     create_sector_vector(out_sector_opt->answer, number_of_streams, radians);
 
     free_attributes(number_of_streams);
-    G_message("Done");
+
     exit(EXIT_SUCCESS);
 }

Modified: grass-addons/grass7/raster/r.stream.segment/stream_segment.c
===================================================================
--- grass-addons/grass7/raster/r.stream.segment/stream_segment.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.segment/stream_segment.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 		   double seg_treshold)
 {
     DIRCELLS *streamline;
-    unsigned long int *P;	/* alias for points */
+    long int *P;	/* alias for points */
 
     int i, prev_i = 0;
     int number_of_cells;
@@ -223,7 +223,7 @@
     int cell_up, cell_down;
     int r, c, r_up, c_up, r_down, c_down;
     STREAM *SA = stream_attributes;
-    unsigned long int *P = cur_stream->points;
+    long int *P = cur_stream->points;
     int next_stream = cur_stream->next_stream;
     unsigned int outlet = cur_stream->outlet;
     int last_cell = cur_stream->number_of_cells - 1;

Modified: grass-addons/grass7/raster/r.stream.segment/stream_topology.c
===================================================================
--- grass-addons/grass7/raster/r.stream.segment/stream_topology.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.segment/stream_topology.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -203,8 +203,8 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 
-	SA[i].points = (unsigned long int *)
-	    G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+	SA[i].points = (long int *)
+	    G_malloc((SA[i].number_of_cells) * sizeof(long int));
 	SA[i].elevation = (float *)
 	    G_malloc((SA[i].number_of_cells) * sizeof(float));
 	SA[i].distance = (double *)
@@ -325,8 +325,8 @@
 
     for (i = 1; i < number_of_streams; ++i) {
 
-	SA[i].points = (unsigned long int *)
-	    G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+	SA[i].points = (long int *)
+	    G_malloc((SA[i].number_of_cells) * sizeof(long int));
 	SA[i].elevation = (float *)
 	    G_malloc((SA[i].number_of_cells) * sizeof(float));
 	SA[i].distance = (double *)

Modified: grass-addons/grass7/raster/r.stream.snap/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.snap/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.snap/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -235,9 +295,9 @@
 
 int ram_release_map(MAP *map)
 {
-    /* 
-     * free memory allocated for map, set pointer to null;
-     */
+  /* 
+   * free memory allocated for map, set pointer to null;
+   */
     int r;
 
     for (r = 0; r < map->nrows; ++r)
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,7 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to NULL at: %d %d"), r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.snap/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.snap/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.snap/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.snap/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.snap/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.snap/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -36,7 +36,9 @@
     int i;
     SEG map_streams, map_accum;
     SEGMENT *streams = NULL, *accum = NULL;
+    DCELL nullval;
     int number_of_segs;
+    double seg_size;
     int number_of_points;
     int radius;
     double accum_treshold;
@@ -94,9 +96,6 @@
     if (G_parser(argc, argv))	/* parser */
 	exit(EXIT_FAILURE);
 
-    number_of_segs = (int)atof(opt_swapsize->answer);
-    number_of_segs = number_of_segs < 32 ? (int)(32 / 0.12) : number_of_segs / 0.12;
-
     radius = atoi(opt_distance_treshold->answer);
     accum_treshold = atof(opt_accum_treshold->answer);
 
@@ -113,18 +112,36 @@
     if (!in_accum_opt->answer)
 	accum_treshold = -1;
 
+    Rast_set_d_null_value(&nullval, 1);
 
     /* SEGMENT VERSION ONLY */
 
+    number_of_segs = atoi(opt_swapsize->answer);
+    if (number_of_segs < 3)
+	number_of_segs = 3;
+
+    /* segment size in MB */
+    seg_size = 0;
+    if (in_stream_opt->answer)
+	seg_size = sizeof(CELL);
+    if (in_accum_opt->answer)
+	seg_size += sizeof(DCELL);
+    
+    seg_size = seg_size * SROWS * SCOLS / (1 << 20); 
+
+    number_of_segs = (int)(number_of_segs / seg_size);
+    if (number_of_segs < 10)
+	number_of_segs = 10;
+
     if (in_stream_opt->answer) {
 	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_stream_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_streams, in_stream_opt->answer, 1, CELL_TYPE, 0);
 	streams = &map_streams.seg;
     }
 
     if (in_accum_opt->answer) {
 	seg_create_map(&map_accum, SROWS, SCOLS, number_of_segs, DCELL_TYPE);
-	seg_read_map(&map_accum, in_accum_opt->answer, 0, -1);
+	seg_read_map(&map_accum, in_accum_opt->answer, 0, -1, nullval);
 	accum = &map_accum.seg;
     }
 

Modified: grass-addons/grass7/raster/r.stream.snap/snap.c
===================================================================
--- grass-addons/grass7/raster/r.stream.snap/snap.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.snap/snap.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -104,8 +104,9 @@
 	    }
 
         /* TODO: this should be fixed by someone who knows the code */
-	if (sumaccum > 0) 
+	if (sumaccum > 0) {
 	    /* accum_treshold=(sumaccum/naccum+maxaccum)/2 */ ;
+	}
 	accum_treshold = sumaccum / naccum;
 
 	for (i = -radius; i <= radius; ++i)

Modified: grass-addons/grass7/raster/r.stream.stats/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream.stats/io.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.stats/io.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -44,7 +44,7 @@
 }
 
 int ram_read_map(MAP * map, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
     /*
      * Function read external map and put it in MAP structure (created with create_map)
@@ -125,9 +125,9 @@
 	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
 
-	for (c = 0; c < map->ncols; ++c)
+	for (c = 0; c < map->ncols; ++c) {
 	    if (!Rast_is_null_value
-		(input_pointer + c * input_data_size, input_data_type))
+		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (map->data_type) {
 		case CELL_TYPE:
 		    ((CELL **) map->map)[r][c] =
@@ -148,23 +148,82 @@
 		    G_fatal_error(_("Wrong internal data type"));
 		    break;
 		}
+	    }
+	    else {
+		switch (map->data_type) {
+		case CELL_TYPE:
+		    ((CELL **) map->map)[r][c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL **) map->map)[r][c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL **) map->map)[r][c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
     }				/*end for r */
+    G_percent(r, map->nrows, 2);
 
+    Rast_close(input_map_fd);
     G_free(input_buffer);
-    G_percent(r, map->nrows, 2);
-    Rast_close(input_map_fd);
+
     return 0;
-}				/* end create floating point map */
+}
 
-int ram_reset_map(MAP * map, int value)
+int ram_reset_map(MAP * map, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
-    int r;
+    int r, c;
 
-    for (r = 0; r < map->nrows; ++r)
-	memset((map->map)[r], value, map->ncols * map->data_size);
+    switch (map->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((CELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((FCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+	    
+	    for (r = 0; r < map->nrows; ++r) {
+		for (c = 0; c < map->ncols; ++c) {
+		    ((DCELL **) map->map)[r][c] = v;
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -218,7 +277,8 @@
 			Rast_set_d_null_value(row + c * (map->data_size), 1);
 		break;
 	    default:
-		G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 
@@ -235,9 +295,9 @@
 
 int ram_release_map(MAP *map)
 {
-    /* 
-     * free memory allocated for map, set pointer to null;
-     */
+  /* 
+   * free memory allocated for map, set pointer to null;
+   */
     int r;
 
     for (r = 0; r < map->nrows; ++r)
@@ -325,7 +385,7 @@
 }
 
 int seg_read_map(SEG * seg, char *input_map_name, int check_res,
-		 RASTER_MAP_TYPE check_data_type)
+		 RASTER_MAP_TYPE check_data_type, DCELL nullval)
 {
 
     /*
@@ -353,8 +413,10 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"),
 		      input_map_name);
-    seg->mapset = mapset;
 
+    seg->map_name = G_store(input_map_name);
+    seg->mapset = G_store(mapset);
+
     /* checking if region and input are the same */
     G_get_window(&this_window);
     Rast_get_cellhd(input_map_name, mapset, &cellhd);
@@ -410,9 +472,8 @@
 	G_percent(r, seg->nrows, 2);
 	Rast_get_row(input_fd, input_buffer, r, input_data_type);
 	input_pointer = input_buffer;
-	memset(target_buffer, 0, seg->ncols * seg->data_size);
 
-	for (c = 0; c < seg->ncols; ++c)
+	for (c = 0; c < seg->ncols; ++c) {
 	    if (!Rast_is_null_value
 		(input_pointer + c * input_data_size, input_data_type)) {
 		switch (seg->data_type) {
@@ -436,6 +497,26 @@
 		    break;
 		}
 	    }
+	    else {
+		switch (seg->data_type) {
+		case CELL_TYPE:
+		    ((CELL *) target_buffer)[c] =
+			Rast_get_c_value(&nullval, DCELL_TYPE);
+		    break;
+		case FCELL_TYPE:
+		    ((FCELL *) target_buffer)[c] =
+			Rast_get_f_value(&nullval, DCELL_TYPE);
+		    break;
+		case DCELL_TYPE:
+		    ((DCELL *) target_buffer)[c] =
+			Rast_get_d_value(&nullval, DCELL_TYPE);
+		    break;
+		default:
+		    G_fatal_error(_("Wrong internal data type"));
+		    break;
+		}
+	    }
+	}
 
 	if (0 > Segment_put_row(&(seg->seg), target_buffer, r)) {
 	    G_free(input_buffer);
@@ -445,29 +526,61 @@
 			  r, input_map_name);
 	}
     }				/* end for row */
+    G_percent(r, seg->nrows, 2);
 
-    G_percent(r, seg->nrows, 2);
     Rast_close(input_fd);
     G_free(input_buffer);
     G_free(target_buffer);
 
-    seg->map_name = G_store(input_map_name);
-    seg->mapset = G_store(mapset);
-
     return 0;
 }
 
-int seg_reset_map(SEG * seg, int value)
+int seg_reset_map(SEG * seg, DCELL value)
 {
     /*
      * set all cells in the map to value
      */
     int r, c;
 
-    for (r = 0; r < seg->nrows; ++r)
-	for (c = 0; c < seg->ncols; ++c)
-	    Segment_put(&(seg->seg), &value, r, c);
+    switch (seg->data_type) {
+    case CELL_TYPE:
+	{
+	    CELL v = Rast_get_c_value(&value, DCELL_TYPE);
 
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case FCELL_TYPE:
+	{
+	    FCELL v = Rast_get_f_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    case DCELL_TYPE:
+	{
+	    DCELL v = Rast_get_d_value(&value, DCELL_TYPE);
+
+	    for (r = 0; r < seg->nrows; ++r) {
+		for (c = 0; c < seg->ncols; ++c) {
+		    Segment_put(&(seg->seg), &v, r, c);
+		}
+	    }
+	}
+	break;
+    default:
+	G_fatal_error(_("Wrong internal data type"));
+	break;
+    }
+
     return 0;
 }
 
@@ -528,7 +641,8 @@
 			Rast_set_d_null_value(row + c * (seg->data_size), 1);
 		break;
 	    default:
-		G_warning(_("Unable to convert to NULL at: %d %d"), r, c);
+		G_fatal_error(_("Wrong internal data type"));
+		break;
 	    }
 	}
 	Rast_put_row(output_fd, output_buffer, output_data_type);

Modified: grass-addons/grass7/raster/r.stream.stats/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream.stats/io.h	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.stats/io.h	2014-11-09 14:51:02 UTC (rev 62686)
@@ -17,8 +17,8 @@
 #define INDEX(r,c) ((r) * ncols + (c))
 #define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
 
-#define SROWS 256
-#define SCOLS 256
+#define SROWS 128
+#define SCOLS 128
 
 typedef struct {
 	void **map; /* matrix of data */
@@ -44,15 +44,15 @@
 
 /* all in ram functions */
 int ram_create_map(MAP *, RASTER_MAP_TYPE);
-int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
-int ram_reset_map(MAP *, int);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE, DCELL);
+int ram_reset_map(MAP *, DCELL);
 int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
 int ram_release_map(MAP *);
 int ram_destory_map(MAP *);
 
 /* memory swap functions */
 int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG *, int);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE, DCELL);
+int seg_reset_map (SEG *, DCELL);
 int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
 int seg_release_map(SEG *);

Modified: grass-addons/grass7/raster/r.stream.stats/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream.stats/main.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.stats/main.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -113,15 +113,18 @@
 	MAP map_dirs, map_streams, map_elevation;
 	CELL **streams, **dirs;
 	FCELL **elevation;
+	DCELL nullval;
 
 	G_message(_("All in RAM calculation..."));
 
+	Rast_set_d_null_value(&nullval, 1);
+
 	ram_create_map(&map_streams, CELL_TYPE);
-	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_dirs, CELL_TYPE);
-	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	ram_create_map(&map_elevation, FCELL_TYPE);
-	ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 
 	streams = (CELL **) map_streams.map;
 	dirs = (CELL **) map_dirs.map;
@@ -151,19 +154,31 @@
     if (segmentation) {
 	SEG map_dirs, map_streams, map_elevation;
 	SEGMENT *streams, *dirs, *elevation;
+	DCELL nullval;
+	double seg_size;
 
         G_message(_("Memory swap calculation (may take some time)..."));
 
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
+	Rast_set_d_null_value(&nullval, 1);
 
+	number_of_segs = atoi(opt_swapsize->answer);
+	if (number_of_segs < 3)
+	    number_of_segs = 3;
+
+	/* segment size in MB */
+	seg_size = (sizeof(CELL) * 2.0 + sizeof(FCELL)) * SROWS * SCOLS / (1 << 20); 
+
+	number_of_segs = (int)(number_of_segs / seg_size);
+	if (number_of_segs < 10)
+	    number_of_segs = 10;
+
 	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+	seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE, 0);
 	seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
 		       FCELL_TYPE);
-	seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+	seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1, nullval);
 
 	streams = &map_streams.seg;
 	dirs = &map_dirs.seg;

Modified: grass-addons/grass7/raster/r.stream.stats/stats_calculate.c
===================================================================
--- grass-addons/grass7/raster/r.stream.stats/stats_calculate.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.stats/stats_calculate.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -1,7 +1,7 @@
 #include "local_proto.h"
+
 double stats_linear_reg(int max_order, double *statistic)
 {
-
     int i;
     double avg_x = 0, avg_y = 0;
     double sum_x_square = 0;
@@ -24,6 +24,7 @@
 
 
     result = (avg_x_y - avg_x * avg_y) / (avg_x_square - avg_x * avg_x);
+
     return result;
 }
 

Modified: grass-addons/grass7/raster/r.stream.stats/stats_prepare.c
===================================================================
--- grass-addons/grass7/raster/r.stream.stats/stats_prepare.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.stats/stats_prepare.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -12,6 +12,7 @@
 	tail = 0;
     }
     fifo_count++;
+
     return 0;
 }
 
@@ -37,8 +38,8 @@
     outlets = (POINT *) G_malloc((out_max) * sizeof(POINT));
     outlets_num = 0;
 
-    for (r = 0; r < nrows; ++r)
-	for (c = 0; c < ncols; ++c)
+    for (r = 0; r < nrows; ++r) {
+	for (c = 0; c < ncols; ++c) {
 	    if (streams[r][c] > 0) {
 		if (outlets_num > (out_max - 1)) {
 		    out_max *= 2;
@@ -71,7 +72,8 @@
 
 		}
 	    }			/* end if streams */
-
+	}
+    }
     stat_streams = (STREAM *) G_malloc((outlets_num) * sizeof(STREAM));
 
     for (i = 0; i < outlets_num; ++i) {
@@ -91,11 +93,10 @@
     }
 
     G_free(outlets);
+
     return 0;
 }
 
-/////
-
 int seg_init_streams(SEGMENT *streams, SEGMENT *dirs, SEGMENT *elevation)
 {
     int d, i;		/* d: direction, i: iteration */
@@ -108,7 +109,7 @@
     outlets = (POINT *) G_malloc((out_max) * sizeof(POINT));
     outlets_num = 0;
 
-    for (r = 0; r < nrows; ++r)
+    for (r = 0; r < nrows; ++r) {
 	for (c = 0; c < ncols; ++c) {
 	    Segment_get(streams, &streams_cell, r, c);
 	    if (streams_cell > 0) {
@@ -147,6 +148,7 @@
 		}
 	    }			/* end if streams */
 	}
+    }
 
     stat_streams = (STREAM *) G_malloc((outlets_num) * sizeof(STREAM));
 
@@ -169,13 +171,12 @@
     }
 
     G_free(outlets);
+
     return 0;
 }
 
-
 int ram_calculate_streams(CELL **streams, CELL **dirs, FCELL **elevation)
 {
-
     int i, j, s, d;		/* s - streams index */
     int done = 1;
     int r, c;
@@ -233,7 +234,9 @@
 			G_distance(next_easting, next_northing, cur_easting,
 				   cur_northing);
 		    diff_elev = elevation[next_r][next_c] - elevation[r][c];
-		    diff_elev = (diff_elev < 0) ? 0. : diff_elev;	/* water cannot flow up */
+		    /* water cannot flow up
+		     * but DEMs are not perfect */
+		    diff_elev = (diff_elev < 0) ? 0. : diff_elev;
 
 		    stat_streams[s].length += cur_length;
 		    stat_streams[s].slope += (diff_elev / cur_length);
@@ -246,13 +249,10 @@
 	    }			/* end for i */
 	}			/* end while */
     }				/* end for s */
+
     return 0;
 }
 
-
-////
-
-
 int seg_calculate_streams(SEGMENT *streams, SEGMENT *dirs,
 			  SEGMENT *elevation)
 {
@@ -324,7 +324,9 @@
 				next_c);
 		    Segment_get(elevation, &elevation_cell, r, c);
 		    diff_elev = elevation_next_cell - elevation_cell;
-		    diff_elev = (diff_elev < 0) ? 0. : diff_elev;	/* water cannot flow up */
+		    /* water cannot flow up
+		     * but DEMs are not perfect */
+		    diff_elev = (diff_elev < 0) ? 0. : diff_elev;
 
 		    stat_streams[s].length += cur_length;
 		    stat_streams[s].slope += (diff_elev / cur_length);
@@ -337,6 +339,7 @@
 	    }			/* end for i */
 	}			/* end while */
     }				/* end for s */
+
     return 0;
 }
 
@@ -374,10 +377,10 @@
 	r = n_cell.r;
 	c = n_cell.c;
     }				/* end while */
+
     return area;
 }
 
-
 int ram_calculate_basins(CELL **dirs)
 {
     int i;
@@ -398,6 +401,7 @@
     }
 
     G_free(fifo_points);
+
     return 0;
 }
 
@@ -438,6 +442,7 @@
 	r = n_cell.r;
 	c = n_cell.c;
     }				/* end while */
+
     return area;
 }
 
@@ -460,5 +465,6 @@
     }
 
     G_free(fifo_points);
+
     return 0;
 }

Modified: grass-addons/grass7/raster/r.stream.stats/stats_print.c
===================================================================
--- grass-addons/grass7/raster/r.stream.stats/stats_print.c	2014-11-09 14:47:35 UTC (rev 62685)
+++ grass-addons/grass7/raster/r.stream.stats/stats_print.c	2014-11-09 14:51:02 UTC (rev 62686)
@@ -2,7 +2,6 @@
 
 int print_stats(int order_max)
 {
-
     int i;
 
     fflush(stdout);
@@ -94,6 +93,7 @@
 		ord_stats[i].stream_frequency * 1000000);
     }
     fflush(stdout);
+
     return 0;
 }
 
@@ -119,12 +119,12 @@
     fprintf(stdout, "Gradient ratio: %2.4f \n",
 	    stats_total.reg_gradient_ratio);
     fflush(stdout);
+
     return 0;
 }
 
 int print_stats_orders(int order_max)
 {
-
     int i;
 
     fflush(stdout);
@@ -145,5 +145,6 @@
 		ord_stats[i].sum_area / 1000000);
     }
     fflush(stdout);
+
     return 0;
 }



More information about the grass-commit mailing list