[GRASS-SVN] r42461 - grass-addons/raster/r.stream.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 3 07:34:35 EDT 2010


Author: mmetz
Date: 2010-06-03 07:34:35 -0400 (Thu, 03 Jun 2010)
New Revision: 42461

Modified:
   grass-addons/raster/r.stream.stats/global.h
   grass-addons/raster/r.stream.stats/io.c
   grass-addons/raster/r.stream.stats/main.c
   grass-addons/raster/r.stream.stats/print_stats.c
   grass-addons/raster/r.stream.stats/stats.c
Log:
format and clean up code

Modified: grass-addons/raster/r.stream.stats/global.h
===================================================================
--- grass-addons/raster/r.stream.stats/global.h	2010-06-03 11:33:50 UTC (rev 42460)
+++ grass-addons/raster/r.stream.stats/global.h	2010-06-03 11:34:35 UTC (rev 42461)
@@ -18,54 +18,54 @@
 #define SQRT2 1.414214
 #define POINT struct points	
 POINT {
-	int r, c;
-	int is_outlet;
-	};
+    int r, c;
+    int is_outlet;
+};
 	
 #define STREAM struct strs
 STREAM { 
-	int index;
-	int is_outlet;
-	int r, c; /* outlet */
-	float elev_diff;
-	float elev_spring, elev_outlet;
-	float slope; /* cumulative */
-	float gradient;
-	float length; /* cumulative */
-	int order;
-	double basin_area; /* basin order */
-	int cell_num;
-	};	
+    int index;
+    int is_outlet;
+    int r, c; /* outlet */
+    float elev_diff;
+    float elev_spring, elev_outlet;
+    float slope; /* cumulative */
+    float gradient;
+    float length; /* cumulative */
+    int order;
+    double basin_area; /* basin order */
+    int cell_num;
+};	
 	
 	
 #define STATS struct statistics
 STATS {
-	int order;
-	int stream_num;
-	float sum_length;
-	float avg_length;
-	float std_length;
-	float avg_slope;
-	float std_slope;
-	float avg_gradient;
-	float std_gradient;
-	double sum_area;
-	double avg_area;
-	double std_area;
-	float avg_elev_diff;
-	float std_elev_diff;
-	float bifur_ratio;
-	float std_bifur_ratio;
-	float length_ratio;
-	float std_length_ratio;
-	float area_ratio;
-	float std_area_ratio;
-	float slope_ratio;
-	float std_slope_ratio;
-	float gradient_ratio;
-	float std_gradient_ratio;
-	float stream_frequency;
-	float drainage_density;
+    int order;
+    int stream_num;
+    float sum_length;
+    float avg_length;
+    float std_length;
+    float avg_slope;
+    float std_slope;
+    float avg_gradient;
+    float std_gradient;
+    double sum_area;
+    double avg_area;
+    double std_area;
+    float avg_elev_diff;
+    float std_elev_diff;
+    float bifur_ratio;
+    float std_bifur_ratio;
+    float length_ratio;
+    float std_length_ratio;
+    float area_ratio;
+    float std_area_ratio;
+    float slope_ratio;
+    float std_slope_ratio;
+    float gradient_ratio;
+    float std_gradient_ratio;
+    float stream_frequency;
+    float drainage_density;
 };
 
 					/* functions.c */ 
@@ -78,6 +78,7 @@
 int init_streams (void);
 double fill_basin (int r,int c);
 int calculate_basins (void);
+int calculate_streams(void);
 int stats(void);
 int fifo_insert (POINT point);
 POINT fifo_return_del (void);

Modified: grass-addons/raster/r.stream.stats/io.c
===================================================================
--- grass-addons/raster/r.stream.stats/io.c	2010-06-03 11:33:50 UTC (rev 42460)
+++ grass-addons/raster/r.stream.stats/io.c	2010-06-03 11:34:35 UTC (rev 42461)
@@ -60,7 +60,6 @@
     streams = (CELL **) G_malloc(sizeof(CELL *) * nrows);
     elevation = (FCELL **) G_malloc(sizeof(FCELL *) * nrows);
 
-
     for (r = 0; r < nrows; ++r) {
 
 	/* dirs & streams */
@@ -116,7 +115,6 @@
 
 	}			/* end switch */
 
-
 	for (c = 0; c < ncols; ++c) {
 	    if (G_is_c_null_value(&r_dirs[c])) {
 		dirs[r][c] = 0;

Modified: grass-addons/raster/r.stream.stats/main.c
===================================================================
--- grass-addons/raster/r.stream.stats/main.c	2010-06-03 11:33:50 UTC (rev 42460)
+++ grass-addons/raster/r.stream.stats/main.c	2010-06-03 11:34:35 UTC (rev 42461)
@@ -31,7 +31,6 @@
  */
 int main(int argc, char *argv[])
 {
-
     struct GModule *module;	/* GRASS module for parsing arguments */
     struct Option *in_dir_opt, *in_stm_opt, *in_elev_opt;	/* options */
     struct Flag *out_hack;	/* flags */
@@ -67,7 +66,6 @@
     in_elev_opt->gisprompt = "old,cell,raster";
     in_elev_opt->description = "Name of elevation map";
 
-
     /* Define the different flags */
     /*
        out_hack = G_define_flag();

Modified: grass-addons/raster/r.stream.stats/print_stats.c
===================================================================
--- grass-addons/raster/r.stream.stats/print_stats.c	2010-06-03 11:33:50 UTC (rev 42460)
+++ grass-addons/raster/r.stream.stats/print_stats.c	2010-06-03 11:34:35 UTC (rev 42461)
@@ -6,7 +6,6 @@
 
 int print_stats(void)
 {
-
     int i;
 
     /* summary statistics */
@@ -88,4 +87,5 @@
 		ord_stats[i].drainage_density * 1000,
 		ord_stats[i].stream_frequency * 1000000);
     }
+    return 0;
 }

Modified: grass-addons/raster/r.stream.stats/stats.c
===================================================================
--- grass-addons/raster/r.stream.stats/stats.c	2010-06-03 11:33:50 UTC (rev 42460)
+++ grass-addons/raster/r.stream.stats/stats.c	2010-06-03 11:34:35 UTC (rev 42461)
@@ -6,12 +6,11 @@
 
 int init_streams(void)
 {
-    int d, i, j;		/* d: direction, i: iteration */
+    int d, i;		/* d: direction, i: iteration */
     int r, c;
     int next_stream = -1, cur_stream;
     int out_max = ncols + nrows;
     POINT *outlets;
-
     int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
     int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
 
@@ -43,12 +42,12 @@
 		if (cur_stream != next_stream) {	/* is outlet or node! */
 		    outlets[outlets_num].r = r;
 		    outlets[outlets_num].c = c;
-				
-					if (next_stream==-1)
-				outlets[outlets_num].is_outlet=1;
-					else
-				outlets[outlets_num].is_outlet=0;	
-		    
+
+		    if (next_stream == -1)
+			outlets[outlets_num].is_outlet = 1;
+		    else
+			outlets[outlets_num].is_outlet = 0;
+
 		    outlets_num++;
 		}
 	    }			/* end if streams */
@@ -77,39 +76,34 @@
 
 int calculate_streams(void)
 {
-
     int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
     int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
-
     int i, j, s, d;		/* s - streams index */
     int done = 1;
     int r, c;
     float cur_northing, cur_easting;
     float next_northing, next_easting;
     float diff_elev, cur_length;
-    int cur_stream_order;
 
     G_begin_distance_calculations();
 
     for (s = 0; s < outlets_num; ++s) {
 	r = stat_streams[s].r;
 	c = stat_streams[s].c;
-	
-		cur_northing = window.north - (r + .5) * window.ns_res;
+
+	cur_northing = window.north - (r + .5) * window.ns_res;
 	cur_easting = window.west + (c + .5) * window.ew_res;
-	d= (dirs[r][c]==0) ? 2 : abs(dirs[r][c]);
-	
-			next_northing =
-	window.north - (r + nextr[d] + .5) * window.ns_res;
-	    next_easting =
-	window.west + (c + nextc[d] + .5) * window.ew_res;
+	d = (dirs[r][c] == 0) ? 2 : abs(dirs[r][c]);
 
+	next_northing = window.north - (r + nextr[d] + .5) * window.ns_res;
+	next_easting = window.west + (c + nextc[d] + .5) * window.ew_res;
 
-/* init length */
-			stat_streams[s].length=
-	G_distance(next_easting, next_northing, cur_easting,
-				   cur_northing);
-	
+
+	/* init length */
+	stat_streams[s].length =
+	    G_distance(next_easting, next_northing, cur_easting,
+		       cur_northing);
+
 	done = 1;
 
 	while (done) {
@@ -159,8 +153,9 @@
 int calculate_basins(void)
 {
     int i;
-    total_basins=0.;
 
+    total_basins = 0.;
+
     G_begin_cell_area_calculations();
     fifo_max = 4 * (nrows + ncols);
     fifo_outlet = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
@@ -168,10 +163,10 @@
     for (i = 0; i < outlets_num; ++i) {
 	stat_streams[i].basin_area =
 	    fill_basin(stat_streams[i].r, stat_streams[i].c);
-	
-		if (stat_streams[i].is_outlet)
-	total_basins += stat_streams[i].basin_area;
-	}
+
+	if (stat_streams[i].is_outlet)
+	    total_basins += stat_streams[i].basin_area;
+    }
     G_free(fifo_outlet);
     return 0;
 }
@@ -181,7 +176,6 @@
 {
     int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
     int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
-
     int i, j;
     double area;
     POINT n_cell;
@@ -224,7 +218,7 @@
 
 POINT fifo_return_del(void)
 {
-    if (head > fifo_max)
+    if (head >= fifo_max)
 	head = -1;
     return fifo_outlet[++head];
 }
@@ -376,24 +370,23 @@
 
 	ord_stats[i - 1].bifur_ratio =
 	    ord_stats[i - 1].stream_num / (float)ord_stats[i].stream_num;
-	
-	ord_stats[i-1].length_ratio =
-	    (i==1) ? 0 :
-	    ord_stats[i].avg_length / ord_stats[i-1].avg_length;
-	
+
+	ord_stats[i - 1].length_ratio =
+	    (i == 1) ? 0 :
+	    ord_stats[i].avg_length / ord_stats[i - 1].avg_length;
+
 	ord_stats[i].area_ratio =
-			(i==1) ? 0 :
-	    ord_stats[i].avg_area / ord_stats[i-1].avg_area;
-	
+	    (i == 1) ? 0 : ord_stats[i].avg_area / ord_stats[i - 1].avg_area;
+
 	ord_stats[i - 1].slope_ratio =
 	    ord_stats[i - 1].avg_slope / ord_stats[i].avg_slope;
-	
+
 	ord_stats[i - 1].gradient_ratio =
 	    ord_stats[i - 1].avg_gradient / ord_stats[i].avg_gradient;
-	
+
 	ord_stats[i].stream_frequency =
 	    ord_stats[i].stream_num / ord_stats[i].sum_area;
-	
+
 	ord_stats[i].drainage_density =
 	    ord_stats[i].sum_length / ord_stats[i].sum_area;
 
@@ -463,5 +456,5 @@
     stats_total.drainage_density =
 	stats_total.sum_length / stats_total.sum_area;
 
-return 0;
+    return 0;
 }



More information about the grass-commit mailing list