[GRASS-SVN] r59150 - in grass/trunk/raster/r.li: r.li.dominance r.li.edgedensity r.li.pielou r.li.renyi r.li.richness r.li.shannon r.li.simpson

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 28 13:57:35 PST 2014


Author: mmetz
Date: 2014-02-28 13:57:34 -0800 (Fri, 28 Feb 2014)
New Revision: 59150

Modified:
   grass/trunk/raster/r.li/r.li.dominance/dominance.c
   grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c
   grass/trunk/raster/r.li/r.li.pielou/pielou.c
   grass/trunk/raster/r.li/r.li.renyi/renyi.c
   grass/trunk/raster/r.li/r.li.richness/r.li.richness.html
   grass/trunk/raster/r.li/r.li.richness/richness.c
   grass/trunk/raster/r.li/r.li.shannon/shannon.c
   grass/trunk/raster/r.li/r.li.simpson/simpson.c
Log:
r.li: fix more bugs

Modified: grass/trunk/raster/r.li/r.li.dominance/dominance.c
===================================================================
--- grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -15,14 +15,14 @@
  *
  *****************************************************************************/
 
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-
 #include <stdlib.h>
 #include <fcntl.h>
 #include <math.h>
 
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
 #include "../r.li.daemon/daemon.h"
 #include "../r.li.daemon/avlDefs.h"
 #include "../r.li.daemon/avl.h"
@@ -94,8 +94,9 @@
 	}
     }
 
-    if (ris != RLI_OK)
+    if (ris != RLI_OK) {
 	return RLI_ERRORE;
+    }
 
     *result = indice;
 
@@ -139,6 +140,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_c_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -148,7 +150,6 @@
 	}
 
 	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-	Rast_set_c_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -177,7 +178,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -211,7 +211,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.c = precCell;
@@ -252,7 +252,7 @@
     if (area > 0) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
@@ -326,6 +326,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_d_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -335,7 +336,6 @@
 	}
 
 	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_d_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -364,7 +364,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -398,7 +397,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.dc = precCell;
@@ -439,7 +438,7 @@
     if (area > 0) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
@@ -513,6 +512,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_f_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -522,7 +522,6 @@
 	}
 
 	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_f_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -551,7 +550,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -585,7 +583,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.fc = precCell;
@@ -626,7 +624,7 @@
     if (area > 0) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {

Modified: grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c
===================================================================
--- grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -9,14 +9,14 @@
  *       
  */
 
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-
 #include <stdlib.h>
 #include <fcntl.h>		/* for O_RDONLY usage */
 #include <math.h>
 
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
 #include "../r.li.daemon/avlDefs.h"
 #include "../r.li.daemon/avl.h"
 #include "../r.li.daemon/daemon.h"
@@ -72,7 +72,6 @@
 
     return calculateIndex(conf->answer, edgedensity, par, raster->answer,
 			  output->answer);
-
 }
 
 int edgedensity(int fd, char **valore, struct area_entry *ad, double *result)

Modified: grass/trunk/raster/r.li/r.li.pielou/pielou.c
===================================================================
--- grass/trunk/raster/r.li/r.li.pielou/pielou.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.pielou/pielou.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -15,14 +15,14 @@
  *       
  ***************************************************************************/
 
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-
 #include <stdlib.h>
 #include <fcntl.h>
 #include <math.h>
 
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
 #include "../r.li.daemon/daemon.h"
 #include "../r.li.daemon/avlDefs.h"
 #include "../r.li.daemon/avl.h"
@@ -65,6 +65,7 @@
 			  output->answer);
 }
 
+
 int pielou(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
@@ -73,17 +74,17 @@
     switch (ad->data_type) {
     case CELL_TYPE:
 	{
-	    calculate(fd, ad, &indice);
+	    ris = calculate(fd, ad, &indice);
 	    break;
 	}
     case DCELL_TYPE:
 	{
-	    calculateD(fd, ad, &indice);
+	    ris = calculateD(fd, ad, &indice);
 	    break;
 	}
     case FCELL_TYPE:
 	{
-	    calculateF(fd, ad, &indice);
+	    ris = calculateF(fd, ad, &indice);
 	    break;
 	}
     default:
@@ -139,6 +140,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_c_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -148,7 +150,6 @@
 	}
 
 	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-	Rast_set_c_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -177,7 +178,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -211,7 +211,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.c = precCell;
@@ -249,10 +249,10 @@
 	}
     }
 
-    if (area > 0) {
+    if (area > 0 && m > 1) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
@@ -326,6 +326,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_d_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -335,7 +336,6 @@
 	}
 
 	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_d_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -364,7 +364,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -398,7 +397,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.dc = precCell;
@@ -436,10 +435,10 @@
 	}
     }
 
-    if (area > 0) {
+    if (area > 0 && m > 1) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
@@ -513,6 +512,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_f_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -522,7 +522,6 @@
 	}
 
 	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_f_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -551,7 +550,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -585,7 +583,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.fc = precCell;
@@ -623,10 +621,10 @@
 	}
     }
 
-    if (area > 0) {
+    if (area > 0 && m > 1) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {

Modified: grass/trunk/raster/r.li/r.li.renyi/renyi.c
===================================================================
--- grass/trunk/raster/r.li/r.li.renyi/renyi.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.renyi/renyi.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -20,6 +20,7 @@
 #include <math.h>
 
 #include <grass/gis.h>
+#include <grass/raster.h>
 #include <grass/glocale.h>
 
 #include "../r.li.daemon/daemon.h"
@@ -85,6 +86,7 @@
 			  output->answer);
 }
 
+
 int renyi(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
@@ -114,11 +116,11 @@
 	    G_fatal_error("data type unknown");
 	    return RLI_ERRORE;
 	}
-
     }
 
-    if (ris != RLI_OK)
+    if (ris != RLI_OK) {
 	return RLI_ERRORE;
+    }
 
     *result = indice;
 
@@ -162,6 +164,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_c_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -171,7 +174,6 @@
 	}
 
 	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-	Rast_set_c_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -200,7 +202,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -234,7 +235,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.c = precCell;
@@ -363,6 +364,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_d_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -372,7 +374,6 @@
 	}
 
 	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_d_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -401,7 +402,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -435,7 +435,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.dc = precCell;
@@ -564,6 +564,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_f_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -573,7 +574,6 @@
 	}
 
 	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_f_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -602,7 +602,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -636,7 +635,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.fc = precCell;

Modified: grass/trunk/raster/r.li/r.li.richness/r.li.richness.html
===================================================================
--- grass/trunk/raster/r.li/r.li.richness/r.li.richness.html	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.richness/r.li.richness.html	2014-02-28 21:57:34 UTC (rev 59150)
@@ -19,7 +19,7 @@
 <p>
 <!-- TODO: verify next: -->
 If the input raster map contains only NULL values then <em>r.li.richness</em>
-returns 0.<br>
+returns NULL.<br>
 
 
 <h2>EXAMPLES</h2>

Modified: grass/trunk/raster/r.li/r.li.richness/richness.c
===================================================================
--- grass/trunk/raster/r.li/r.li.richness/richness.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.richness/richness.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -10,20 +10,19 @@
  *
  */
 
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-
 #include <stdlib.h>
 #include <fcntl.h>
 #include <math.h>
 
-#include "../r.li.daemon/defs.h"
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
+#include "../r.li.daemon/daemon.h"
 #include "../r.li.daemon/avlDefs.h"
 #include "../r.li.daemon/avl.h"
-#include "../r.li.daemon/daemon.h"
 
-rli_func dominance;
+rli_func richness;
 int calculate(int fd, struct area_entry *ad, double *result);
 int calculateD(int fd, struct area_entry *ad, double *result);
 int calculateF(int fd, struct area_entry *ad, double *result);
@@ -36,10 +35,10 @@
     G_gisinit(argv[0]);
     module = G_define_module();
     module->description =
-	_("Calculates dominance's diversity index on a raster map");
+	_("Calculates richness index on a raster map");
     G_add_keyword(_("raster"));
     G_add_keyword(_("landscape structure analysis"));
-    G_add_keyword(_("dominance index"));
+    G_add_keyword(_("diversity index"));
 
     /* define options */
 
@@ -55,12 +54,11 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-    return calculateIndex(conf->answer, dominance, NULL, raster->answer,
+    return calculateIndex(conf->answer, richness, NULL, raster->answer,
 			  output->answer);
-
 }
 
-int dominance(int fd, char **par, struct area_entry *ad, double *result)
+int richness(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
     double indice = 0;
@@ -86,20 +84,18 @@
 	    G_fatal_error("data type unknown");
 	    return RLI_ERRORE;
 	}
-
     }
 
-    if (ris != RLI_OK)
+    if (ris != RLI_OK) {
 	return RLI_ERRORE;
+    }
 
     *result = indice;
 
     return RLI_OK;
-
 }
 
 
-
 int calculate(int fd, struct area_entry *ad, double *result)
 {
     CELL *buf;
@@ -128,6 +124,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_c_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -137,7 +134,7 @@
 	}
 
 	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-	Rast_set_c_null_value(&precCell, 1);
+
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
 
@@ -145,53 +142,49 @@
 		Rast_set_c_null_value(&corrCell, 1);
 	    }
 
-	    if (!(Rast_is_null_value(&corrCell, uc.t))) {
+	    if (!(Rast_is_null_value(&precCell, uc.t)) &&
+		 corrCell != precCell) {
 
-		if (!(Rast_is_null_value(&precCell, uc.t)) &&
-		     corrCell != precCell) {
-
+		if (albero == NULL) {
+		    uc.val.c = precCell;
+		    albero = avl_make(uc, (long)1);
 		    if (albero == NULL) {
-			uc.val.c = precCell;
-			albero = avl_make(uc, (long)1);
-			if (albero == NULL) {
-			    G_fatal_error("avl_make error");
+			G_fatal_error("avl_make error");
+			return RLI_ERRORE;
+		    }
+		    m++;
+		}
+		else {
+		    uc.val.c = precCell;
+		    ris = avl_add(&albero, uc, (long)1);
+		    switch (ris) {
+		    case AVL_ERR:
+			{
+			    G_fatal_error("avl_add error");
 			    return RLI_ERRORE;
 			}
-			else
+		    case AVL_ADD:
+			{
 			    m++;
-		    }
-		    else {
-			uc.val.c = precCell;
-			ris = avl_add(&albero, uc, (long)1);
-			switch (ris) {
-			case AVL_ERR:
-			    {
-				G_fatal_error("avl_add error");
-				return RLI_ERRORE;
-			    }
-			case AVL_ADD:
-			    {
-				m++;
-				break;
-			    }
-			case AVL_PRES:
-			    {
-				break;
-			    }
-			default:
-			    {
-				G_fatal_error("avl_make unknown error");
-				return RLI_ERRORE;
-			    }
+			    break;
 			}
+		    case AVL_PRES:
+			{
+			    break;
+			}
+		    default:
+			{
+			    G_fatal_error("avl_make unknown error");
+			    return RLI_ERRORE;
+			}
 		    }
-		}		/* endif not equal cells */
-	    }
+		}
+	    }		/* endif not equal cells */
 	    precCell = corrCell;
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (!(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.c = precCell;
@@ -229,11 +222,16 @@
 	}
     }
 
-    *result = m;
+    if (m)
+	*result = m;
+    else
+	Rast_set_d_null_value(result, 1);
 
     avl_destroy(albero);
-    if (masked)
+    if (masked) {
+	close(mask_fd);
 	G_free(mask_buf);
+    }
 
     return RLI_OK;
 }
@@ -261,12 +259,13 @@
 	    return RLI_ERRORE;
 	mask_buf = G_malloc(ad->cl * sizeof(int));
 	if (mask_buf == NULL) {
-	    G_fatal_error("malloc mask_buffailed");
+	    G_fatal_error("malloc mask_buf failed");
 	    return RLI_ERRORE;
 	}
 	masked = TRUE;
     }
 
+    Rast_set_d_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -276,62 +275,57 @@
 	}
 
 	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_d_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each dcell in the row */
 	    corrCell = buf[i + ad->x];
 
-	    if (masked && mask_buf[i] == 0) {
+	    if ((masked) && (mask_buf[i] == 0)) {
 		Rast_set_d_null_value(&corrCell, 1);
 	    }
 
-	    if (!(Rast_is_null_value(&corrCell, uc.t))) {
+	    if (!(Rast_is_null_value(&precCell, uc.t)) &&
+		 corrCell != precCell) {
 
-		if (!(Rast_is_null_value(&precCell, uc.t)) &&
-		     corrCell != precCell) {
-
+		if (albero == NULL) {
+		    uc.val.dc = precCell;
+		    albero = avl_make(uc, (long)1);
 		    if (albero == NULL) {
-			uc.val.dc = precCell;
-			albero = avl_make(uc, (long)1);
-			if (albero == NULL) {
-			    G_fatal_error("avl_make error");
+			G_fatal_error("avl_make error");
+			return RLI_ERRORE;
+		    }
+		    m++;
+		}
+		else {
+		    uc.val.dc = precCell;
+		    ris = avl_add(&albero, uc, (long)1);
+		    switch (ris) {
+		    case AVL_ERR:
+			{
+			    G_fatal_error("avl_add error");
 			    return RLI_ERRORE;
 			}
-			m++;
-		    }
-		    else {
-			uc.val.dc = precCell;
-			ris = avl_add(&albero, uc, (long)1);
-			switch (ris) {
-			case AVL_ERR:
-			    {
-				G_fatal_error("avl_add error");
-				return RLI_ERRORE;
-			    }
-			case AVL_ADD:
-			    {
-				m++;
-				break;
-			    }
-			case AVL_PRES:
-			    {
-				break;
-			    }
-			default:
-			    {
-				G_fatal_error("avl_make unknown error");
-				return RLI_ERRORE;
-			    }
+		    case AVL_ADD:
+			{
+			    m++;
+			    break;
 			}
+		    case AVL_PRES:
+			{
+			    break;
+			}
+		    default:
+			{
+			    G_fatal_error("avl_make unknown error");
+			    return RLI_ERRORE;
+			}
 		    }
-
-		}		/* endif not equal dcells */
-	    }
+		}
+	    }		/* endif not equal dcells */
 	    precCell = corrCell;
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (!(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.dc = precCell;
@@ -369,17 +363,21 @@
 	}
     }
 
-    *result = m;
+    if (m)
+	*result = m;
+    else
+	Rast_set_d_null_value(result, 1);
 
     avl_destroy(albero);
-    if (masked)
+    if (masked) {
+	close(mask_fd);
 	G_free(mask_buf);
+    }
 
     return RLI_OK;
 }
 
 
-
 int calculateF(int fd, struct area_entry *ad, double *result)
 {
     FCELL *buf;
@@ -408,6 +406,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_f_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -417,67 +416,59 @@
 	}
 
 	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_f_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each fcell in the row */
 
 	    corrCell = buf[i + ad->x];
 
-	    if (masked && mask_buf[i] == 0) {
+	    if ((masked) && (mask_buf[i] == 0)) {
 		Rast_set_f_null_value(&corrCell, 1);
 	    }
 
-	    if (!(Rast_is_null_value(&corrCell, uc.t))) {
+	    if (!(Rast_is_null_value(&precCell, uc.t)) &&
+		 corrCell != precCell) {
 
-		if (!(Rast_is_null_value(&precCell, uc.t)) &&
-		     corrCell != precCell) {
-
+		if (albero == NULL) {
+		    uc.val.fc = precCell;
+		    albero = avl_make(uc, (long)1);
 		    if (albero == NULL) {
-			uc.val.fc = precCell;
-			albero = avl_make(uc, (long)1);
-			if (albero == NULL) {
-			    G_fatal_error("avl_make error");
+			G_fatal_error("avl_make error");
+			return RLI_ERRORE;
+		    }
+		    m++;
+		}
+		else {
+		    uc.val.fc = precCell;
+		    ris = avl_add(&albero, uc, (long)1);
+		    switch (ris) {
+		    case AVL_ERR:
+			{
+			    G_fatal_error("avl_add error");
 			    return RLI_ERRORE;
 			}
-			m++;
-		    }
-		    else {
-			uc.val.fc = precCell;
-			ris = avl_add(&albero, uc, (long)1);
-			switch (ris) {
-			case AVL_ERR:
-			    {
-				G_fatal_error("avl_add error");
-				return RLI_ERRORE;
-			    }
-			case AVL_ADD:
-			    {
-				m++;
-				break;
-			    }
-			case AVL_PRES:
-			    {
-				break;
-			    }
-			default:
-			    {
-				G_fatal_error("avl_make unknown error");
-				return RLI_ERRORE;
-			    }
+		    case AVL_ADD:
+			{
+			    m++;
+			    break;
 			}
+		    case AVL_PRES:
+			{
+			    break;
+			}
+		    default:
+			{
+			    G_fatal_error("avl_make unknown error");
+			    return RLI_ERRORE;
+			}
 		    }
-
-		}		/* endif not equal fcells */
-		else {		/*equal fcells */
-
-		    ;
 		}
-	    }
+
+	    }		/* endif not equal fcells */
 	    precCell = corrCell;
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (!(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.fc = precCell;
@@ -515,11 +506,16 @@
 	}
     }
 
-    *result = m;
+    if (m)
+	*result = m;
+    else
+	Rast_set_d_null_value(result, 1);
 
     avl_destroy(albero);
-    if (masked)
+    if (masked) {
+	close(mask_fd);
 	G_free(mask_buf);
+    }
 
     return RLI_OK;
 }

Modified: grass/trunk/raster/r.li/r.li.shannon/shannon.c
===================================================================
--- grass/trunk/raster/r.li/r.li.shannon/shannon.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.shannon/shannon.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -15,14 +15,14 @@
  *
  *****************************************************************************/
 
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-
 #include <stdlib.h>
 #include <fcntl.h>
 #include <math.h>
 
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
 #include "../r.li.daemon/daemon.h"
 #include "../r.li.daemon/avlDefs.h"
 #include "../r.li.daemon/avl.h"
@@ -66,7 +66,7 @@
 }
 
 
-int shannon(int fd, char **par, struct area_entry * ad, double *result)
+int shannon(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
     double indice = 0;
@@ -74,17 +74,17 @@
     switch (ad->data_type) {
     case CELL_TYPE:
 	{
-	    calculate(fd, ad, &indice);
+	    ris = calculate(fd, ad, &indice);
 	    break;
 	}
     case DCELL_TYPE:
 	{
-	    calculateD(fd, ad, &indice);
+	    ris = calculateD(fd, ad, &indice);
 	    break;
 	}
     case FCELL_TYPE:
 	{
-	    calculateF(fd, ad, &indice);
+	    ris = calculateF(fd, ad, &indice);
 	    break;
 	}
     default:
@@ -140,6 +140,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_c_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -149,7 +150,6 @@
 	}
 
 	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-	Rast_set_c_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -178,7 +178,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -212,7 +211,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.c = precCell;
@@ -253,7 +252,7 @@
     if (area > 0) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
@@ -327,6 +326,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_d_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -336,7 +336,6 @@
 	}
 
 	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_d_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -365,7 +364,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -399,7 +397,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.dc = precCell;
@@ -440,7 +438,7 @@
     if (area > 0) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
@@ -514,6 +512,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_f_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -523,7 +522,6 @@
 	}
 
 	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_f_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -552,7 +550,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -586,7 +583,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.fc = precCell;
@@ -627,7 +624,7 @@
     if (area > 0) {
 	double t;
 	double shannon;
-	double perc, logarithm;;
+	double perc, logarithm;
 
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {

Modified: grass/trunk/raster/r.li/r.li.simpson/simpson.c
===================================================================
--- grass/trunk/raster/r.li/r.li.simpson/simpson.c	2014-02-28 21:55:28 UTC (rev 59149)
+++ grass/trunk/raster/r.li/r.li.simpson/simpson.c	2014-02-28 21:57:34 UTC (rev 59150)
@@ -65,6 +65,7 @@
 			  output->answer);
 }
 
+
 int simpson(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
@@ -93,8 +94,9 @@
 	}
     }
 
-    if (ris != RLI_OK)
+    if (ris != RLI_OK) {
 	return RLI_ERRORE;
+    }
 
     *result = indice;
 
@@ -118,10 +120,7 @@
     long zero = 0;
     long totCorr = 1;
 
-    double somma = 0;
-    double p = 0;
     long area = 0;
-    double t;
 
     avl_tree albero = NULL;
     AVL_table *array;
@@ -141,6 +140,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_c_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -150,7 +150,6 @@
 	}
 
 	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-	Rast_set_c_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -179,7 +178,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -213,7 +211,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.c = precCell;
@@ -252,6 +250,10 @@
     }
 
     if (area > 0) {
+	double t;
+	double p;
+	double simpson;
+
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -263,15 +265,16 @@
 	    return RLI_ERRORE;
 	}
 
-	/* calculate index summary */
+	/* calculate simpson */
+	simpson = 0;
 	for (i = 0; i < m; i++) {
 	    t = (double)(array[i]->tot);
 	    p = t / area;
-	    somma = somma + (p * p);
+	    simpson += (p * p);
 	}
 	G_free(array);
 
-	*result = 1 - somma;
+	*result = 1 - simpson;
     }
     else
 	Rast_set_d_null_value(result, 1);
@@ -302,10 +305,7 @@
     long zero = 0;
     long totCorr = 1;
 
-    double somma = 0;
-    double p = 0;
     long area = 0;
-    double t;
 
     avl_tree albero = NULL;
     AVL_table *array;
@@ -325,6 +325,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_d_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -334,7 +335,6 @@
 	}
 
 	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_d_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -363,7 +363,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -397,7 +396,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.dc = precCell;
@@ -436,6 +435,10 @@
     }
 
     if (area > 0) {
+	double t;
+	double p;
+	double simpson;
+
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -447,15 +450,16 @@
 	    return RLI_ERRORE;
 	}
 
-	/* calculate index summary */
+	/* calculate simpson */
+	simpson = 0;
 	for (i = 0; i < m; i++) {
 	    t = (double)(array[i]->tot);
 	    p = t / area;
-	    somma = somma + (p * p);
+	    simpson += (p * p);
 	}
 	G_free(array);
 
-	*result = 1 - somma;
+	*result = 1 - simpson;
     }
     else
 	Rast_set_d_null_value(result, 1);
@@ -486,10 +490,7 @@
     long zero = 0;
     long totCorr = 1;
 
-    double somma = 0;
-    double p = 0;
     long area = 0;
-    double t;
 
     avl_tree albero = NULL;
     AVL_table *array;
@@ -509,6 +510,7 @@
 	masked = TRUE;
     }
 
+    Rast_set_f_null_value(&precCell, 1);
     for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
@@ -518,7 +520,6 @@
 	}
 
 	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-	Rast_set_f_null_value(&precCell, 1);
 
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    corrCell = buf[i + ad->x];
@@ -547,7 +548,6 @@
 			G_fatal_error("avl_make error");
 			return RLI_ERRORE;
 		    }
-
 		    m++;
 		}
 		else {
@@ -581,7 +581,7 @@
 	}
     }
 
-    /*last closing */
+    /* last closing */
     if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
 	if (albero == NULL) {
 	    uc.val.fc = precCell;
@@ -620,6 +620,10 @@
     }
 
     if (area > 0) {
+	double t;
+	double p;
+	double simpson;
+
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -631,15 +635,16 @@
 	    return RLI_ERRORE;
 	}
 
-	/* calculate index summary */
+	/* calculate simpson */
+	simpson = 0;
 	for (i = 0; i < m; i++) {
 	    t = (double)(array[i]->tot);
 	    p = t / area;
-	    somma = somma + (p * p);
+	    simpson += (p * p);
 	}
 	G_free(array);
 
-	*result = 1 - somma;
+	*result = 1 - simpson;
     }
     else
 	Rast_set_d_null_value(result, 1);



More information about the grass-commit mailing list