[GRASS-SVN] r59092 - in grass/trunk/raster/r.li: r.li.cwed r.li.dominance r.li.edgedensity r.li.mpa r.li.mps r.li.padcv r.li.padrange r.li.padsd r.li.patchdensity r.li.patchnum r.li.pielou r.li.renyi r.li.richness r.li.shannon r.li.shape r.li.simpson

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Feb 18 13:02:56 PST 2014


Author: mmetz
Date: 2014-02-18 13:02:56 -0800 (Tue, 18 Feb 2014)
New Revision: 59092

Modified:
   grass/trunk/raster/r.li/r.li.cwed/cwed.c
   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.mpa/mpa.c
   grass/trunk/raster/r.li/r.li.mps/mps.c
   grass/trunk/raster/r.li/r.li.padcv/padcv.c
   grass/trunk/raster/r.li/r.li.padrange/padrange.c
   grass/trunk/raster/r.li/r.li.padsd/padsd.c
   grass/trunk/raster/r.li/r.li.patchdensity/main.c
   grass/trunk/raster/r.li/r.li.patchnum/main.c
   grass/trunk/raster/r.li/r.li.pielou/pielou.c
   grass/trunk/raster/r.li/r.li.renyi/r.li.renyi.html
   grass/trunk/raster/r.li/r.li.renyi/renyi.c
   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.shape/main.c
   grass/trunk/raster/r.li/r.li.simpson/simpson.c
Log:
r.li: use function prototype for index calculation

Modified: grass/trunk/raster/r.li/r.li.cwed/cwed.c
===================================================================
--- grass/trunk/raster/r.li/r.li.cwed/cwed.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.cwed/cwed.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -26,6 +26,7 @@
 #define _PRES 1
 #define _ERR -1
 
+rli_func contrastWeightedEdgeDensity;
 int calculate(int fd, struct area_entry *ad, Coppie * cc, long totCoppie,
 	      double *result);
 int calculateD(int fd, struct area_entry *ad, Coppie * cc, long totCoppie,

Modified: grass/trunk/raster/r.li/r.li.dominance/dominance.c
===================================================================
--- grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -29,6 +29,7 @@
 
 /* template is shannon */
 
+rli_func dominance;
 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);

Modified: grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c
===================================================================
--- grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -21,6 +21,7 @@
 #include "../r.li.daemon/avl.h"
 #include "../r.li.daemon/daemon.h"
 
+rli_func edgedensity;
 int calculate(int fd, struct area_entry *ad, char **valore, double *result);
 int calculateD(int fd, struct area_entry *ad, char **valore, double *result);
 int calculateF(int fd, struct area_entry *ad, char **valore, double *result);

Modified: grass/trunk/raster/r.li/r.li.mpa/mpa.c
===================================================================
--- grass/trunk/raster/r.li/r.li.mpa/mpa.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.mpa/mpa.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -22,7 +22,7 @@
 #include "../r.li.daemon/defs.h"
 #include "../r.li.daemon/daemon.h"
 
-
+rli_func meanPixelAttribute;
 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);

Modified: grass/trunk/raster/r.li/r.li.mps/mps.c
===================================================================
--- grass/trunk/raster/r.li/r.li.mps/mps.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.mps/mps.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -35,6 +35,7 @@
     generic_cell type;
 };
 
+rli_func meanPatchSize;
 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);

Modified: grass/trunk/raster/r.li/r.li.padcv/padcv.c
===================================================================
--- grass/trunk/raster/r.li/r.li.padcv/padcv.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.padcv/padcv.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -36,6 +36,7 @@
     generic_cell type;
 };
 
+rli_func patchAreaDistributionCV;
 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);

Modified: grass/trunk/raster/r.li/r.li.padrange/padrange.c
===================================================================
--- grass/trunk/raster/r.li/r.li.padrange/padrange.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.padrange/padrange.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -36,6 +36,7 @@
     generic_cell type;
 };
 
+rli_func patchAreaDistributionRANGE;
 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);

Modified: grass/trunk/raster/r.li/r.li.padsd/padsd.c
===================================================================
--- grass/trunk/raster/r.li/r.li.padsd/padsd.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.padsd/padsd.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -36,6 +36,7 @@
     generic_cell type;
 };
 
+rli_func patchAreaDistributionSD;
 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);

Modified: grass/trunk/raster/r.li/r.li.patchdensity/main.c
===================================================================
--- grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -34,6 +34,7 @@
     generic_cell type;
 };
 
+rli_func patch_density;
 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);

Modified: grass/trunk/raster/r.li/r.li.patchnum/main.c
===================================================================
--- grass/trunk/raster/r.li/r.li.patchnum/main.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.patchnum/main.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -34,6 +34,7 @@
     generic_cell type;
 };
 
+rli_func patch_number;
 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);

Modified: grass/trunk/raster/r.li/r.li.pielou/pielou.c
===================================================================
--- grass/trunk/raster/r.li/r.li.pielou/pielou.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.pielou/pielou.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -29,6 +29,7 @@
 
 /* template is shannon */
 
+rli_func pielou;
 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);

Modified: grass/trunk/raster/r.li/r.li.renyi/r.li.renyi.html
===================================================================
--- grass/trunk/raster/r.li/r.li.renyi/r.li.renyi.html	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.renyi/r.li.renyi.html	2014-02-18 21:02:56 UTC (rev 59092)
@@ -1,7 +1,7 @@
 <h2>DESCRIPTION</h2>
 
 <em>r.li.renyi</em> calculates the "Renyi's diversity index" as:<br>
-<img src="r_li_renyi.png alt="r.li.renyi formula"> <br>
+<img src="r_li_renyi.png" alt="r.li.renyi formula"> <br>
 with:
 <ul>
   <li><b>H</b>: Renyi entropy</li>

Modified: grass/trunk/raster/r.li/r.li.renyi/renyi.c
===================================================================
--- grass/trunk/raster/r.li/r.li.renyi/renyi.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.renyi/renyi.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -28,6 +28,7 @@
 
 /* template is shannon */
 
+rli_func renyi;
 int calculate(int fd, struct area_entry *ad, char **par, double *result);
 int calculateD(int fd, struct area_entry *ad, char **par, double *result);
 int calculateF(int fd, struct area_entry *ad, char **par, double *result);
@@ -67,7 +68,7 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-    if (atoi(alpha->answer) == 1) {
+    if (atof(alpha->answer) == 1) {
 	G_fatal_error
 	    ("If alpha = 1 Renyi index is not defined. (Ricotta et al., 2003, Environ. Model. Softw.)");
 	exit(RLI_ERRORE);
@@ -275,7 +276,7 @@
 	double alpha;
 	double pi;
 	double t;
-	double sum;
+	double sum, sum2;
 
 	alpha = atof(par[0]);
 
@@ -292,13 +293,25 @@
 
 	/* calculate renyi index */
 	sum = 0;
+	sum2 = 0;
 	for (i = 0; i < m; i++) {
 	    t = array[i]->tot;
 	    pi = t / area;
 	    sum += pow(pi, alpha);
+	    sum2 += pi;
 	}
 	G_free(array);
-	
+
+	/* correction for numerical instability */
+	if (sum2 != 1)
+	    sum += 1 - sum2;
+
+	if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
+	    G_warning("Renyi index calculation reached numerical instability. "
+	              "This can happen with alpha close to 1. The result will be set to zero.");
+	    sum = 1;
+	}
+
 	*result = (1 / (1 - alpha)) * log(sum);
     }
     else
@@ -464,7 +477,7 @@
 	double alpha;
 	double pi;
 	double t;
-	double sum;
+	double sum, sum2;
 
 	alpha = atof(par[0]);
 
@@ -481,13 +494,25 @@
 
 	/* calculate renyi index */
 	sum = 0;
+	sum2 = 0;
 	for (i = 0; i < m; i++) {
 	    t = array[i]->tot;
 	    pi = t / area;
 	    sum += pow(pi, alpha);
+	    sum += pi;
 	}
 	G_free(array);
-	
+
+	/* correction for numerical instability */
+	if (sum2 != 1)
+	    sum += 1 - sum2;
+
+	if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
+	    G_warning("Renyi index calculation reached numerical instability. "
+	              "This can happen with alpha close to 1. The result will be set to zero.");
+	    sum = 1;
+	}
+
 	*result = (1 / (1 - alpha)) * log(sum);
     }
     else
@@ -653,7 +678,7 @@
 	double alpha;
 	double pi;
 	double t;
-	double sum;
+	double sum, sum2;
 
 	alpha = atof(par[0]);
 
@@ -670,13 +695,25 @@
 
 	/* calculate renyi index */
 	sum = 0;
+	sum2 = 0;
 	for (i = 0; i < m; i++) {
 	    t = array[i]->tot;
 	    pi = t / area;
 	    sum += pow(pi, alpha);
+	    sum2 += pi;
 	}
 	G_free(array);
-	
+
+	/* correction for numerical instability */
+	if (sum2 != 1)
+	    sum += 1 - sum2;
+
+	if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
+	    G_warning("Renyi index calculation reached numerical instability. "
+	              "This can happen with alpha close to 1. The result will be set to zero.");
+	    sum = 1;
+	}
+
 	*result = (1 / (1 - alpha)) * log(sum);
     }
     else

Modified: grass/trunk/raster/r.li/r.li.richness/richness.c
===================================================================
--- grass/trunk/raster/r.li/r.li.richness/richness.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.richness/richness.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -23,9 +23,10 @@
 #include "../r.li.daemon/avl.h"
 #include "../r.li.daemon/daemon.h"
 
-double calculate(int fd, struct area_entry *ad, double *result);
-double calculateD(int fd, struct area_entry *ad, double *result);
-double calculateF(int fd, struct area_entry *ad, double *result);
+rli_func dominance;
+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);
 
 int main(int argc, char *argv[])
 {
@@ -63,10 +64,7 @@
 {
     int ris = RLI_OK;
     double indice = 0;
-    struct Cell_head hd;
 
-    Rast_get_cellhd(ad->raster, "", &hd);
-
     switch (ad->data_type) {
     case CELL_TYPE:
 	{
@@ -102,7 +100,7 @@
 
 
 
-double calculate(int fd, struct area_entry *ad, double *result)
+int calculate(int fd, struct area_entry *ad, double *result)
 {
     CELL *buf;
     CELL corrCell;
@@ -241,7 +239,7 @@
 }
 
 
-double calculateD(int fd, struct area_entry *ad, double *result)
+int calculateD(int fd, struct area_entry *ad, double *result)
 {
     DCELL *buf;
     DCELL corrCell;
@@ -382,7 +380,7 @@
 
 
 
-double calculateF(int fd, struct area_entry *ad, double *result)
+int calculateF(int fd, struct area_entry *ad, double *result)
 {
     FCELL *buf;
     FCELL corrCell;

Modified: grass/trunk/raster/r.li/r.li.shannon/shannon.c
===================================================================
--- grass/trunk/raster/r.li/r.li.shannon/shannon.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.shannon/shannon.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -29,6 +29,7 @@
 
 /* template for dominance, renyi, pielou, simpson */
 
+rli_func shannon;
 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);

Modified: grass/trunk/raster/r.li/r.li.shape/main.c
===================================================================
--- grass/trunk/raster/r.li/r.li.shape/main.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.shape/main.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -23,6 +23,8 @@
 #include <grass/glocale.h>
 #include "../r.li.daemon/daemon.h"
 
+rli_func shape_index;
+
 int main(int argc, char *argv[])
 {
     struct Option *raster, *conf, *output;

Modified: grass/trunk/raster/r.li/r.li.simpson/simpson.c
===================================================================
--- grass/trunk/raster/r.li/r.li.simpson/simpson.c	2014-02-18 21:01:41 UTC (rev 59091)
+++ grass/trunk/raster/r.li/r.li.simpson/simpson.c	2014-02-18 21:02:56 UTC (rev 59092)
@@ -29,6 +29,7 @@
 
 /* template is shannon */
 
+rli_func simpson;
 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);



More information about the grass-commit mailing list