[GRASS-SVN] r44406 - in grass/branches/develbranch_6: . raster/r.li raster/r.li/r.li.daemon raster/r.li/r.li.pielou raster/r.li/r.li.renyi

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Nov 24 11:43:46 EST 2010


Author: neteler
Date: 2010-11-24 08:43:45 -0800 (Wed, 24 Nov 2010)
New Revision: 44406

Added:
   grass/branches/develbranch_6/raster/r.li/r.li.pielou/
   grass/branches/develbranch_6/raster/r.li/r.li.pielou/Makefile
   grass/branches/develbranch_6/raster/r.li/r.li.pielou/description.html
   grass/branches/develbranch_6/raster/r.li/r.li.pielou/pielou.c
   grass/branches/develbranch_6/raster/r.li/r.li.pielou/r_li_pielou.png
   grass/branches/develbranch_6/raster/r.li/r.li.renyi/
   grass/branches/develbranch_6/raster/r.li/r.li.renyi/Makefile
   grass/branches/develbranch_6/raster/r.li/r.li.renyi/description.html
   grass/branches/develbranch_6/raster/r.li/r.li.renyi/r_li_renyi.png
   grass/branches/develbranch_6/raster/r.li/r.li.renyi/renyi.c
Modified:
   grass/branches/develbranch_6/contributors_extra.csv
   grass/branches/develbranch_6/raster/r.li/Makefile
   grass/branches/develbranch_6/raster/r.li/description.html
   grass/branches/develbranch_6/raster/r.li/r.li.daemon/index.h
Log:
Luca Delucchi + Duccio Rocchini: two new indices

Modified: grass/branches/develbranch_6/contributors_extra.csv
===================================================================
--- grass/branches/develbranch_6/contributors_extra.csv	2010-11-24 16:19:56 UTC (rev 44405)
+++ grass/branches/develbranch_6/contributors_extra.csv	2010-11-24 16:43:45 UTC (rev 44406)
@@ -10,6 +10,7 @@
 Carl Anderson,<candrsn mindspring.com>,-
 Charles Ehlschlaeger,<cre111 wiu.edu>,-
 Christoph Simon,<ciccio kiosknet.com.br>,-
+Duccio Rocchini,<ducciorocchini gmail.com>,-
 Dylan Beaudette,<dylan.beaudette gmail.com>,yes
 Eric Mitchell,<emitchell altaira.com>,-
 Francesco Pirotti,<francesco.pirotti unipd.it>,-
@@ -20,6 +21,7 @@
 Lars Ahlzen,<lars ahlzen.com>,-
 Lorenzo Moretti,<lorenzo moretti bologna.enea.it>,-
 Lubos Mitas,<lmitas ncsa uiuc.edu>,-
+Luca Delucchi,<lucadeluge gmail.com>,-
 Malcolm Blue,<mblue nb sympatico.ca>,-
 Maria Brovelli,<maria.brovelli polimi.it>,-
 Michael Perdue,<michael_perdue yahoo.ca>,-

Modified: grass/branches/develbranch_6/raster/r.li/Makefile
===================================================================
--- grass/branches/develbranch_6/raster/r.li/Makefile	2010-11-24 16:19:56 UTC (rev 44405)
+++ grass/branches/develbranch_6/raster/r.li/Makefile	2010-11-24 16:43:45 UTC (rev 44406)
@@ -13,6 +13,8 @@
 	r.li.padsd \
 	r.li.patchdensity \
 	r.li.patchnum \
+	r.li.pielou \
+	r.li.renyi \
 	r.li.richness \
 	r.li.setup \
 	r.li.shannon \

Modified: grass/branches/develbranch_6/raster/r.li/description.html
===================================================================
--- grass/branches/develbranch_6/raster/r.li/description.html	2010-11-24 16:19:56 UTC (rev 44405)
+++ grass/branches/develbranch_6/raster/r.li/description.html	2010-11-24 16:43:45 UTC (rev 44406)
@@ -154,10 +154,12 @@
 
 <b>Diversity indices</b>:
 <ul>
-  <li> <a href="r.li.dominance.html">r.li.dominance</a>: Calculates dominance's diversity index on a raster map</li>
-  <li> <a href="r.li.richness.html">r.li.richness</a>: Calculates dominance's diversity index on a raster map</li>
-  <li> <a href="r.li.shannon.html">r.li.shannon</a>: Calculates Shannon's diversity index on a raster map</li>
-  <li> <a href="r.li.simpson.html">r.li.simpson</a>: Calculates Simpson's diversity index on a raster map</li>
+  <li> <a href="r.li.dominance.html">r.li.dominance</a>: Calculates dominance diversity index on a raster map</li>
+  <li> <a href="r.li.pielou.html">r.li.pielou</a>: Calculates Pielou eveness index on a raster map</li>
+  <li> <a href="r.li.renyi.html">r.li.renyi</a>: Calculates Renyi entropy on a raster map</li>
+  <li> <a href="r.li.richness.html">r.li.richness</a>: Calculates richness diversity index on a raster map</li>
+  <li> <a href="r.li.shannon.html">r.li.shannon</a>: Calculates Shannon diversity index on a raster map</li>
+  <li> <a href="r.li.simpson.html">r.li.simpson</a>: Calculates Simpson diversity index on a raster map</li>
 </ul>
 
 <h2>ADDING NEW INDICES</h2>

Modified: grass/branches/develbranch_6/raster/r.li/r.li.daemon/index.h
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.daemon/index.h	2010-11-24 16:19:56 UTC (rev 44405)
+++ grass/branches/develbranch_6/raster/r.li/r.li.daemon/index.h	2010-11-24 16:43:45 UTC (rev 44406)
@@ -5,6 +5,7 @@
  *  
  * \author Claudio Porta, Lucio Davide Spano, Serena Pallecchi students of Computer Science University of Pisa (Italy)
  *                      Commission from Faunalia Pontedera (PI) www.faunalia.it
+  *        Luca Delucchi and Duccio Rocchini, Fondazione Edmund Mach, Italy: r.li.pielou, r.li.renyi
  *
  *
  * This program is free software under the GPL (>=v2)
@@ -12,7 +13,6 @@
  * 
  * \version 1.1
  *
- * BUGS: please send bugs reports to spano at cli.di.unipi.it porta at cli.di.unipi.it pallecch at cli.di.unipi.it
  */
 
 
@@ -31,6 +31,10 @@
 int patch_number(int fd, char **par, area_des ad, double *result);
 int shape_index(int fd, char **par, area_des ad, double *result);
 int shannon(int fd, char **par, area_des ad, double *result);
+
+int pielou(int fd, char **par, area_des ad, double *result);
+int renyi(int fd, char **par, area_des ad, double *result);
+
 int simpson(int fd, char **par, area_des ad, double *result);
 int meanPatchSize(int fd, char **par, area_des ad, double *result);
 int meanPixelAttribute(int fd, char **par, area_des ad, double *result);

Added: grass/branches/develbranch_6/raster/r.li/r.li.pielou/Makefile
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.pielou/Makefile	                        (rev 0)
+++ grass/branches/develbranch_6/raster/r.li/r.li.pielou/Makefile	2010-11-24 16:43:45 UTC (rev 44406)
@@ -0,0 +1,18 @@
+MODULE_TOPDIR = ../../..
+
+PGM = r.li.pielou
+
+EXTRA_CLEAN_DIRS=doxygenhtml
+
+LIB_NAME      = grass_rli
+
+RLI_LIB = -l$(LIB_NAME)
+
+LIBES = $(GISLIB) $(RLI_LIB)
+
+DEPENDENCIES = $(GISDEP) $(LIB_NAME )
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
+


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.pielou/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass/branches/develbranch_6/raster/r.li/r.li.pielou/description.html
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.pielou/description.html	                        (rev 0)
+++ grass/branches/develbranch_6/raster/r.li/r.li.pielou/description.html	2010-11-24 16:43:45 UTC (rev 44406)
@@ -0,0 +1,51 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.li.pielou</em>  calculates the "pielou's diversity index" as: <br>
+<img src="r_li_pielou.png"><br>
+with:
+<ul>
+  <li><b>J</b>: Pielou eveness index</li>
+  <li><b>H'</b>: Shannon index</li>
+  <li><b>S</b>: number of classes</li>
+</ul>
+
+<h2>NOTES</h2>
+Do not use absolute path names for output files. They are raster if
+moving window disposition was selected, otherwise they are in
+<tt>~/.r.li/output/</tt> folder.
+<br>
+If the input raster contains NULL value cells, <em>r.li.pielou</em>
+returns -1 for these cells. <br>
+If you want to keep NULL values instead, run subsequently on the resulting map:
+<br>
+<div class="code"><pre>
+r.null setnull=-1 map=my_map
+</pre></div>
+
+<h2>EXAMPLES</h2>
+To calculate Pielou's diversity index on the map "my_map", using the
+previously created "my_conf" configuration file (<em>r.li.setup</em>)
+and saving results in "my_out" raster map, run:<br>
+<div class="code"><pre>
+r.li.pielou map=my_map conf=my_conf output=my_out
+</pre></div>
+
+
+<h2>REFERENCES</h2>
+McGarigal, K., and B. J. Marks. 1995. FRAGSTATS: spatial pattern
+analysis program for quantifying landscape structure. USDA For. Serv.
+Gen. Tech. Rep. PNW-351. (<a href="http://treesearch.fs.fed.us/pubs/3064">PDF</a>)
+
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="r.li.html">r.li</a>,
+<a href="r.li.setup.html">r.li.setup</a>
+</em>
+
+<h2>AUTHORS</h2>
+Luca Delucchi and Duccio Rocchini, Fondazione E. Mach (Italy), based on the <em>r.li.shannon</em> code
+developed by Serena Pallecchi student of Computer Science University of Pisa (Italy).<br>
+
+<p><i>Last changed: $Date$</i>
+


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.pielou/description.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass/branches/develbranch_6/raster/r.li/r.li.pielou/pielou.c
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.pielou/pielou.c	                        (rev 0)
+++ grass/branches/develbranch_6/raster/r.li/r.li.pielou/pielou.c	2010-11-24 16:43:45 UTC (rev 44406)
@@ -0,0 +1,802 @@
+
+/****************************************************************************
+ *
+ * MODULE:	r.li.pielou
+ *
+ * PURPOSE:	brief calculates pielou's diversity index
+ *
+ * AUTHOR(S):	Luca Delucchi, Fondazione Edmund Mach         
+ *		Serena Pallecchi student of Computer Science University of Pisa (Italy)
+ *            	
+ * COPYRIGHT:
+ *		This program is free software under the GPL (>=v2)
+ *		Read the COPYING file that comes with GRASS for details.
+ *       
+ ***************************************************************************/
+
+#include <stdlib.h>
+#include <fcntl.h>
+#include <math.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "../r.li.daemon/defs.h"
+#include "../r.li.daemon/avlDefs.h"
+#include "../r.li.daemon/avl.h"
+#include "../r.li.daemon/daemon.h"
+
+int calculate(int fd, area_des ad, double *result);
+
+int calculateD(int fd, area_des ad, double *result);
+
+int calculateF(int fd, area_des ad, double *result);
+
+/*This function is used to sort the values in the moving window */
+static int cmp(const void *pa, const void *pb)
+{
+    int *p1 = (int *)pa;
+    int *p2 = (int *)pb;
+
+    if (*p1 < *p2)
+	return -1;
+    if (*p1 > *p2)
+	return 1;
+    return 0;
+}
+
+int main(int argc, char *argv[])
+{
+    struct Option *raster, *conf, *output;
+
+    struct GModule *module;
+
+    G_gisinit(argv[0]);
+    module = G_define_module();
+    module->description =
+	_("Calculates Pielou's diversity index on a raster map");
+    module->keywords =
+	_("raster, landscape structure analysis, diversity index");
+
+    /* define options */
+
+    raster = G_define_standard_option(G_OPT_R_MAP);
+
+    conf = G_define_option();
+    conf->key = "conf";
+    conf->description = _("Configuration file");
+    conf->type = TYPE_STRING;
+    conf->gisprompt = "old_file,file,input";
+    conf->required = YES;
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    return calculateIndex(conf->answer, pielou, NULL, raster->answer,
+			  output->answer);
+
+}
+
+int pielou(int fd, char **par, area_des ad, double *result)
+{
+
+    char *mapset;
+    int ris = RLI_OK;
+    double indice = 0;
+    struct Cell_head hd;
+
+
+    mapset = G_find_cell(ad->raster, "");
+    if (G_get_cellhd(ad->raster, mapset, &hd) == -1)
+	return RLI_ERRORE;
+
+
+    switch (ad->data_type) {
+    case CELL_TYPE:
+	{
+	    calculate(fd, ad, &indice);
+	    break;
+	}
+    case DCELL_TYPE:
+	{
+	    calculateD(fd, ad, &indice);
+	    break;
+	}
+    case FCELL_TYPE:
+	{
+	    calculateF(fd, ad, &indice);
+	    break;
+	}
+    default:
+	{
+	    G_fatal_error("data type unknown");
+	    return RLI_ERRORE;
+	}
+    }
+
+    if (ris != RLI_OK) {
+	return RLI_ERRORE;
+    }
+
+    *result = indice;
+
+    return RLI_OK;
+
+}
+
+
+int calculate(int fd, area_des ad, double *result)
+{
+
+    CELL *buf;
+    CELL corrCell;
+    CELL precCell;
+    int i, j;
+    int mask_fd = -1, *mask_buf;
+    int ris = 0;
+    int masked = FALSE;
+    int a = 0;			/* a=0 if all cells are null */
+    long m = 0;
+    long tot = 0;
+    long zero = 0;
+    long totCorr = 0;
+    double indice = 0;
+    double somma = 0;
+    double percentuale = 0;
+    double area = 0;
+    double t;
+    double logaritmo;
+
+    /* add to compute the number of class */
+    long *array_copy;
+    int totNumClass = 1;
+    double logTotNumClass;
+
+    generic_cell cc;
+
+    generic_cell cc_passage;	/*add to compute the number of class */
+
+    avl_tree albero = NULL;
+
+    AVL_table *array;
+
+    cc.t = CELL_TYPE;
+
+    /* open mask if needed */
+    if (ad->mask == 1) {
+	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
+	    return RLI_ERRORE;
+	mask_buf = G_malloc(ad->cl * sizeof(int));
+	if (mask_buf == NULL) {
+	    G_fatal_error("malloc mask_buf failed");
+	    return RLI_ERRORE;
+	}
+	masked = TRUE;
+    }
+
+
+    G_set_c_null_value(&precCell, 1);
+
+
+    /*for each row */
+    for (j = 0; j < ad->rl; j++) {
+	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
+
+
+	if (masked) {
+	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
+		G_fatal_error("mask read failed");
+		return RLI_ERRORE;
+	    }
+	}
+
+
+	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
+	    area++;
+	    corrCell = buf[i + ad->x];
+
+	    if (masked && mask_buf[i + ad->x] == 0) {
+		G_set_c_null_value(&corrCell, 1);
+		area--;
+	    }
+
+	    if (!(G_is_null_value(&corrCell, cc.t))) {
+		a = 1;
+		if (G_is_null_value(&precCell, cc.t)) {
+		    precCell = corrCell;
+		}
+
+		if (corrCell != precCell) {
+		    if (albero == NULL) {
+			cc.val.c = precCell;
+			albero = avl_make(cc, totCorr);
+
+			if (albero == NULL) {
+			    G_fatal_error("avl_make error");
+			    return RLI_ERRORE;
+			}
+			else
+			    m++;
+		    }
+		    else {
+			cc.val.c = precCell;
+			ris = avl_add(&albero, cc, totCorr);
+			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_add unknown error");
+				return RLI_ERRORE;
+			    }
+			}
+		    }
+		    totCorr = 1;
+		}
+		else {
+		    totCorr++;
+		}
+		precCell = corrCell;
+	    }
+
+	}			/* end for */
+    }				/* end for */
+
+
+    /*last closing */
+    if (a != 0) {
+	if (albero == NULL) {
+	    cc.val.c = precCell;
+	    albero = avl_make(cc, totCorr);
+
+	    if (albero == NULL) {
+		G_fatal_error("avl_make error");
+		return RLI_ERRORE;
+	    }
+	    m++;
+	}
+	else {
+	    cc.val.c = precCell;
+	    ris = avl_add(&albero, cc, totCorr);
+	    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_add unknown error");
+		    return RLI_ERRORE;
+		}
+	    }
+	}
+    }
+
+    array = G_malloc(m * sizeof(AVL_tableRow));
+    if (array == NULL) {
+	G_fatal_error("malloc array failed");
+	return RLI_ERRORE;
+    }
+    tot = avl_to_array(albero, zero, array);
+
+    if (tot != m) {
+	G_warning("avl_to_array unaspected value. the result could be wrong");
+	return RLI_ERRORE;
+    }
+
+    array_copy = G_malloc(m * sizeof(AVL_tableRow));
+    for (i = 0; i < m; i++) {
+	cc_passage = array[i]->k;
+	array_copy[i] = cc_passage.val.c;
+    }
+
+    qsort(array_copy, m, sizeof(int), cmp);
+
+    for (i = 1; i < m; i++) {
+	if (array_copy[i - 1] != array_copy[i]) {
+	    totNumClass++;
+	}
+    }
+
+    /* calculate summary */
+    for (i = 0; i < m; i++) {
+	t = (double)array[i]->tot;
+	percentuale = (double)(t / area);
+	logaritmo = (double)log(percentuale);
+	somma = somma + (percentuale * logaritmo);
+    }
+
+    logTotNumClass = (double)log(totNumClass);
+    /*if a is 0, that is all cell are null, i put index=-1 */
+    if (a != 0) {
+	indice = ((-1) * somma) / logTotNumClass;
+    }
+    else {
+	indice = (double)(-1);
+    }
+
+    if (isnan(indice) || isinf(indice)) {
+	indice = -1;
+    }
+    G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
+	    somma, logTotNumClass, indice);
+
+    *result = indice;
+
+    G_free(array);
+    if (masked) {
+	G_free(mask_buf);
+    }
+
+    return RLI_OK;
+}
+
+
+
+int calculateD(int fd, area_des ad, double *result)
+{
+
+    DCELL *buf;
+    DCELL corrCell;
+    DCELL precCell;
+
+    int i, j;
+    int mask_fd = -1, *mask_buf;
+    int ris = 0;
+    int masked = FALSE;
+
+    int a = 0;			/* a=0 if all cells are null */
+    long m = 0;
+    long tot = 0;
+    long zero = 0;
+    long totCorr = 0;
+    double indice = 0;
+    double somma = 0;
+    double percentuale = 0;
+    double area = 0;
+    double t;
+    double logaritmo;
+
+    /*add to compute the number of class */
+    long *array_copy;
+    int totNumClass = 1;
+    double logTotNumClass;
+
+    avl_tree albero = NULL;
+    AVL_table *array;
+    generic_cell cc;
+    generic_cell cc_passage;	/*add to compute the number of class */
+    cc.t = DCELL_TYPE;
+
+    /* open mask if needed */
+    if (ad->mask == 1) {
+	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
+	    return RLI_ERRORE;
+	mask_buf = G_malloc(ad->cl * sizeof(int));
+	if (mask_buf == NULL) {
+	    G_fatal_error("malloc mask_buf failed");
+	    return RLI_ERRORE;
+	}
+	masked = TRUE;
+    }
+
+
+    G_set_d_null_value(&precCell, 1);
+
+    /*for each row */
+    for (j = 0; j < ad->rl; j++) {
+	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
+
+
+	if (masked) {
+	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
+		G_fatal_error("mask read failed");
+		return RLI_ERRORE;
+	    }
+	}
+
+
+	for (i = 0; i < ad->cl; i++) {	/* for each dcell in the row */
+	    area++;
+	    corrCell = buf[i + ad->x];
+
+	    if (masked && mask_buf[i + ad->x] == 0) {
+		G_set_d_null_value(&corrCell, 1);
+		area--;
+	    }
+
+	    if (!(G_is_null_value(&corrCell, DCELL_TYPE))) {
+		a = 1;
+		if (G_is_null_value(&precCell, DCELL_TYPE)) {
+		    precCell = corrCell;
+		}
+		if (corrCell != precCell) {
+		    if (albero == NULL) {
+			cc.val.dc = precCell;
+			albero = avl_make(cc, totCorr);
+			if (albero == NULL) {
+			    G_fatal_error("avl_make error");
+			    return RLI_ERRORE;
+			}
+			m++;
+		    }
+		    else {
+			cc.val.dc = precCell;
+			ris = avl_add(&albero, cc, totCorr);
+			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_add unknown error");
+				return RLI_ERRORE;
+			    }
+			}
+		    }
+		    totCorr = 1;
+		}
+		else {
+		    totCorr++;
+		}
+		precCell = corrCell;
+	    }
+
+	}			/*close for */
+    }				/*close for */
+
+    if (masked) {
+	G_free(mask_buf);
+    }
+
+    /*last closing */
+    if (a != 0) {
+	if (albero == NULL) {
+	    cc.val.dc = precCell;
+	    albero = avl_make(cc, totCorr);
+	    if (albero == NULL) {
+		G_fatal_error("avl_make error");
+		return RLI_ERRORE;
+	    }
+	    m++;
+	}
+	else {
+	    cc.val.fc = precCell;
+	    ris = avl_add(&albero, cc, totCorr);
+	    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_add unknown error");
+		    return RLI_ERRORE;
+		}
+	    }
+	}
+
+
+	array = G_malloc(m * sizeof(AVL_tableRow));
+	if (array == NULL) {
+	    G_fatal_error("malloc array failed");
+	    return RLI_ERRORE;
+	}
+	tot = avl_to_array(albero, zero, array);
+
+	if (tot != m) {
+	    G_warning
+		("avl_to_array unaspected value. the result could be wrong");
+	    return RLI_ERRORE;
+	}
+
+	array_copy = G_malloc(m * sizeof(AVL_tableRow));
+
+	for (i = 0; i < m; i++) {
+	    cc_passage = array[i]->k;
+	    array_copy[i] = cc_passage.val.c;
+	}
+
+	qsort(array_copy, m, sizeof(int), cmp);
+
+	for (i = 1; i < m; i++) {
+	    if (array_copy[i - 1] != array_copy[i]) {
+		totNumClass++;
+	    }
+	}
+
+
+	/* calculate summary */
+	for (i = 0; i < m; i++) {
+	    t = (double)array[i]->tot;
+	    percentuale = (double)(t / area);
+	    logaritmo = (double)log(percentuale);
+	    somma = somma + (percentuale * logaritmo);
+	}
+
+	G_free(array);
+
+	logTotNumClass = (double)log(totNumClass);
+
+	indice = (-1) * somma / logTotNumClass;
+    }
+    else {
+	/*if a is 0, that is all cell are null, i put index=-1 */
+	indice = ((double)(-1));
+    }
+
+    if (isnan(indice) || isinf(indice)) {
+	indice = -1;
+    }
+    G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
+	    somma, logTotNumClass, indice);
+
+    *result = indice;
+
+    return RLI_OK;
+}
+
+
+int calculateF(int fd, area_des ad, double *result)
+{
+
+    FCELL *buf;
+    FCELL corrCell;
+    FCELL precCell;
+
+    int i, j;
+    int mask_fd = -1, *mask_buf;
+    int ris = 0;
+    int masked = FALSE;
+    int a = 0;			/* a=0 if all cells are null */
+    long m = 0;
+    long tot = 0;
+    long zero = 0;
+    long totCorr = 0;
+    double indice = 0;
+    double somma = 0;
+    double percentuale = 0;
+    double area = 0;
+    double t;
+    double logaritmo;
+    long *array_copy;
+    int totNumClass = 1;
+    double logTotNumClass;
+
+    avl_tree albero = NULL;
+    AVL_table *array;
+    generic_cell cc;
+    generic_cell cc_passage;
+    cc.t = FCELL_TYPE;
+
+    /* open mask if needed */
+    if (ad->mask == 1) {
+	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
+	    return RLI_ERRORE;
+	mask_buf = G_malloc(ad->cl * sizeof(int));
+	if (mask_buf == NULL) {
+	    G_fatal_error("malloc mask_buf failed");
+	    return RLI_ERRORE;
+	}
+	masked = TRUE;
+    }
+
+
+    G_set_f_null_value(&precCell, 1);
+
+
+    /*for each row */
+    for (j = 0; j < ad->rl; j++) {
+	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
+
+
+	if (masked) {
+	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
+		G_fatal_error("mask read failed");
+		return RLI_ERRORE;
+	    }
+	}
+
+	for (i = 0; i < ad->cl; i++) {	/* for each fcell in the row */
+	    area++;
+	    corrCell = buf[i + ad->x];
+	    if (masked && mask_buf[i + ad->x] == 0) {
+		G_set_f_null_value(&corrCell, 1);
+		area--;
+	    }
+
+	    if (!(G_is_null_value(&corrCell, cc.t))) {
+		a = 1;
+		if (G_is_null_value(&precCell, cc.t)) {
+		    precCell = corrCell;
+		}
+		if (corrCell != precCell) {
+		    if (albero == NULL) {
+			cc.val.fc = precCell;
+			albero = avl_make(cc, totCorr);
+			if (albero == NULL) {
+			    G_fatal_error("avl_make error");
+			    return RLI_ERRORE;
+			}
+			m++;
+		    }
+		    else {
+			cc.val.fc = precCell;
+			ris = avl_add(&albero, cc, totCorr);
+			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_add unknown error");
+				return RLI_ERRORE;
+			    }
+			}
+		    }
+		    totCorr = 1;
+		}
+		else {
+		    totCorr++;
+		}
+
+		precCell = corrCell;
+	    }
+
+	}
+    }
+
+    /*last closing */
+    if (a != 0) {
+	if (albero == NULL) {
+	    cc.val.fc = precCell;
+	    albero = avl_make(cc, totCorr);
+	    if (albero == NULL) {
+		G_fatal_error("avl_make error");
+		return RLI_ERRORE;
+	    }
+	    m++;
+	}
+	else {
+	    cc.val.fc = precCell;
+	    ris = avl_add(&albero, cc, totCorr);
+	    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_add unknown error");
+		    return RLI_ERRORE;
+		}
+	    }
+	}
+    }
+
+    array = G_malloc(m * sizeof(AVL_tableRow));
+    if (array == NULL) {
+	G_fatal_error("malloc array failed");
+	return RLI_ERRORE;
+    }
+    tot = avl_to_array(albero, zero, array);
+
+    if (tot != m) {
+	G_warning("avl_to_array unaspected value. the result could be wrong");
+	return RLI_ERRORE;
+    }
+
+    array_copy = G_malloc(m * sizeof(AVL_tableRow));
+
+    for (i = 0; i < m; i++) {
+	cc_passage = array[i]->k;
+	array_copy[i] = cc_passage.val.c;
+    }
+
+    qsort(array_copy, m, sizeof(int), cmp);
+
+    for (i = 1; i < m; i++) {
+	if (array_copy[i - 1] != array_copy[i]) {
+	    totNumClass++;
+	}
+    }
+
+    /* calculate summary */
+    for (i = 0; i < m; i++) {
+	t = (double)array[i]->tot;
+	percentuale = (double)(t / area);
+	logaritmo = (double)log(percentuale);
+	somma = somma + (percentuale * logaritmo);
+    }
+
+    logTotNumClass = (double)log(totNumClass);
+
+
+    /*if a is 0, that is all cell are null, i put index=-1 */
+    if (a != 0) {
+	indice = ((-1) * somma) / logTotNumClass;
+    }
+    else {
+	indice = (double)(-1);
+    }
+
+    if (isnan(indice) || isinf(indice)) {
+	indice = -1;
+    }
+    G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
+	    somma, logTotNumClass, indice);
+
+    *result = indice;
+
+    G_free(array);
+    if (masked) {
+	G_free(mask_buf);
+    }
+    return RLI_OK;
+}


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.pielou/pielou.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/branches/develbranch_6/raster/r.li/r.li.pielou/r_li_pielou.png
===================================================================
(Binary files differ)


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.pielou/r_li_pielou.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass/branches/develbranch_6/raster/r.li/r.li.renyi/Makefile
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.renyi/Makefile	                        (rev 0)
+++ grass/branches/develbranch_6/raster/r.li/r.li.renyi/Makefile	2010-11-24 16:43:45 UTC (rev 44406)
@@ -0,0 +1,18 @@
+MODULE_TOPDIR = ../../..
+
+PGM = r.li.renyi
+
+EXTRA_CLEAN_DIRS=doxygenhtml
+
+LIB_NAME      = grass_rli
+
+RLI_LIB = -l$(LIB_NAME)
+
+LIBES = $(GISLIB) $(RLI_LIB)
+
+DEPENDENCIES = $(GISDEP) $(LIB_NAME )
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
+


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.renyi/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass/branches/develbranch_6/raster/r.li/r.li.renyi/description.html
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.renyi/description.html	                        (rev 0)
+++ grass/branches/develbranch_6/raster/r.li/r.li.renyi/description.html	2010-11-24 16:43:45 UTC (rev 44406)
@@ -0,0 +1,63 @@
+<h2>DESCRIPTION</h2>
+<em>r.li.renyi</em> 
+calculates the "renyi's diversity index" as:<br>
+<img src="r_li_renyi.png"> <br>
+with: <br>
+<ul>
+  <li><b>H</b>: Renyi entropy</li>
+  <li><b>alpha</b>: order of the generalized entropy</li>
+  <li><b>i</b>: ith class</li>
+  <li><b>S</b>: number of classes</li>
+  <li><b>p<small>i</small></b>: proportion of the landscape occupied by class i</li>
+</ul>
+
+<h2>NOTES</h2>
+Do not use absolute path names for output files. They are raster if
+moving window disposition was selected, otherwise they are in
+<tt>~/.r.li/output/</tt> folder.
+<br>
+If the input raster contains NULL value cells, <em>r.li.renyi</em>
+returns -1 for these cells. <br>
+If you want to keep NULL values instead, run subsequently on the resulting map:
+<br>
+<div class="code"><pre>
+r.null setnull=-1 map=my_map
+</pre></div>
+
+<h2>EXAMPLES</h2>
+To calculate Renyi's diversity index on map "my_map", using the
+previously created "my_conf" configuration file (<em>r.li.setup</em>)
+and saving results in "my_out" raster map, run:<br>
+<div class="code"><pre>
+r.li.renyi map=my_map conf=my_conf output=my_out alpha=0.6
+</pre></div>
+
+<h2>NOTES</h2>
+Do not use absolute path names for output files. They are raster if
+moving window disposition was selected, otherwise they are in
+~/.r.li/output folder. <br>
+
+If input raster is full of null values <em>r.li.renyi</em> returns -1.<br>
+If you want to have null values instead run <br>
+<div class="code"><pre>
+r.null setnull=-1 map=my_map
+</pre></div>
+after index calculation.
+
+<h2>REFERENCES</h2>
+McGarigal, K., and B. J. Marks. 1995. FRAGSTATS: spatial pattern
+analysis program for quantifying landscape structure. USDA For. Serv.
+Gen. Tech. Rep. PNW-351. (<a href="http://treesearch.fs.fed.us/pubs/3064">PDF</a>)
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="r.li.html">r.li</a>,
+<a href="r.li.setup.html">r.li.setup</a>
+</em>
+
+<h2>AUTHORS</h2>
+Luca Delucchi and Duccio Rocchini, Fondazione E. Mach (Italy), based on the <em>r.li.shannon</em> code
+developed by Serena Pallecchi student of Computer Science University of Pisa (Italy).<br>
+
+<p><i>Last changed: $Date$</i>
+


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.renyi/description.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass/branches/develbranch_6/raster/r.li/r.li.renyi/r_li_renyi.png
===================================================================
(Binary files differ)


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.renyi/r_li_renyi.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass/branches/develbranch_6/raster/r.li/r.li.renyi/renyi.c
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.renyi/renyi.c	                        (rev 0)
+++ grass/branches/develbranch_6/raster/r.li/r.li.renyi/renyi.c	2010-11-24 16:43:45 UTC (rev 44406)
@@ -0,0 +1,714 @@
+
+/****************************************************************************
+ *
+ * MODULE:	r.li.renyi
+ *
+ * PURPOSE:	brief calculates renyi's diversity index
+ *
+ * AUTHOR(S):	Luca Delucchi, Fondazione Edmund Mach         
+ *		Serena Pallecchi student of Computer Science University of Pisa (Italy)
+ *            	
+ * COPYRIGHT:
+ *		This program is free software under the GPL (>=v2)
+ *		Read the COPYING file that comes with GRASS for details.
+ *
+ ***************************************************************************/
+
+#include <stdlib.h>
+#include <fcntl.h>
+#include <math.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "../r.li.daemon/defs.h"
+#include "../r.li.daemon/avlDefs.h"
+#include "../r.li.daemon/avl.h"
+#include "../r.li.daemon/daemon.h"
+
+double calculate(area_des ad, int fd, char **par, double *result);
+
+double calculateD(area_des ad, int fd, char **par, double *result);
+
+double calculateF(area_des ad, int fd, char **par, double *result);
+
+int main(int argc, char *argv[])
+{
+    struct Option *raster, *conf, *output, *alpha;
+    struct GModule *module;
+    char **par = NULL;
+
+    G_gisinit(argv[0]);
+    module = G_define_module();
+    module->description =
+	_("Calculates Renyi's diversity index on a raster map");
+    module->keywords =
+	_("raster, landscape structure analysis, diversity index");
+
+    /* define options */
+
+    raster = G_define_standard_option(G_OPT_R_MAP);
+
+    conf = G_define_option();
+    conf->key = "conf";
+    conf->description = _("Configuration file");
+    conf->type = TYPE_STRING;
+    conf->gisprompt = "old_file,file,input";
+    conf->required = YES;
+
+    alpha = G_define_option();
+    alpha->key = "alpha";
+    alpha->description =
+	_("Alpha value is the order of the generalized entropy");
+    alpha->type = TYPE_STRING;
+    alpha->required = YES;
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    if (atoi(alpha->answer) == 1) {
+	G_fatal_error
+	    ("If alpha = 1 Renyi index is not defined. (Ricotta et al., 2003, Environ. Model. Softw.)");
+	exit(RLI_ERRORE);
+    }
+    else if (atof(alpha->answer) < 0.) {
+	G_fatal_error
+	    ("Alpha must be > 0 otherwise Renyi index is not defined. (Ricotta et al., 2003, Environ. Model. Softw.)");
+	exit(RLI_ERRORE);
+    }
+    else {
+	par = &alpha->answer;
+    }
+    return calculateIndex(conf->answer, renyi, par, raster->answer,
+			  output->answer);
+
+}
+
+int renyi(int fd, char **par, area_des ad, double *result)
+{
+    char *mapset;
+    int ris = RLI_OK;
+    double indice = 0;
+    struct Cell_head hd;
+
+    mapset = G_find_cell(ad->raster, "");
+    if (G_get_cellhd(ad->raster, mapset, &hd) == -1)
+	return RLI_ERRORE;
+
+    switch (ad->data_type) {
+    case CELL_TYPE:
+	{
+	    ris = calculate(ad, fd, par, &indice);
+	    break;
+	}
+    case DCELL_TYPE:
+	{
+	    ris = calculateD(ad, fd, par, &indice);
+	    break;
+	}
+    case FCELL_TYPE:
+	{
+	    ris = calculateF(ad, fd, par, &indice);
+	    break;
+	}
+    default:
+	{
+	    G_fatal_error("data type unknown");
+	    return RLI_ERRORE;
+	}
+
+    }
+
+    if (ris != RLI_OK)
+	return RLI_ERRORE;
+
+    *result = indice;
+
+    return RLI_OK;
+
+}
+
+
+
+double calculate(area_des ad, int fd, char **par, double *result)
+{
+    CELL *buf;
+    CELL corrCell;
+    CELL precCell;
+
+    int i, j;
+    int mask_fd = -1, *mask_buf;
+    int ris = 0;
+    int masked = FALSE;
+    int a = 0;			/* a=0 if all cells are null */
+    long m = 0;
+    long tot = 0;
+    long zero = 0;
+    long totCorr = 0;
+    double indice = 0;
+    double somma = 0;
+    double p = 0;
+    double area = 0;
+    double t;
+
+    avl_tree albero = NULL;
+    AVL_table *array;
+    generic_cell uc;
+    uc.t = CELL_TYPE;
+
+    /* open mask if needed */
+    if (ad->mask == 1) {
+	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
+	    return RLI_ERRORE;
+	mask_buf = G_malloc(ad->cl * sizeof(int));
+	if (mask_buf == NULL) {
+	    G_fatal_error("malloc mask_buf failed");
+	    return RLI_ERRORE;
+	}
+	masked = TRUE;
+    }
+
+    G_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) {
+		G_fatal_error("mask read failed");
+		return RLI_ERRORE;
+	    }
+	}
+
+	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
+	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
+	    area++;
+	    corrCell = buf[i + ad->x];
+
+	    if ((masked) && (mask_buf[i + ad->x] == 0)) {
+		G_set_c_null_value(&corrCell, 1);
+		area--;
+	    }
+
+	    if (!(G_is_null_value(&corrCell, uc.t))) {
+		a = 1;
+		if (G_is_null_value(&precCell, uc.t)) {
+		    precCell = corrCell;
+		}
+		if (corrCell != precCell) {
+		    if (albero == NULL) {
+			uc.val.c = precCell;
+			albero = avl_make(uc, totCorr);
+			if (albero == NULL) {
+			    G_fatal_error("avl_make error");
+			    return RLI_ERRORE;
+			}
+			else
+			    m++;
+		    }
+		    else {
+			uc.val.c = precCell;
+			ris = avl_add(&albero, uc, totCorr);
+			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;
+			    }
+			}
+		    }
+		    totCorr = 1;
+		}		/* endif not equal cells */
+		else {		/*equal cells */
+
+		    totCorr++;
+		}
+		precCell = corrCell;
+	    }
+
+	}
+    }
+
+    /*last closing */
+    if (a != 0) {
+	if (albero == NULL) {
+	    uc.val.c = precCell;
+	    albero = avl_make(uc, totCorr);
+	    if (albero == NULL) {
+		G_fatal_error("avl_make error");
+		return RLI_ERRORE;
+	    }
+	    m++;
+	}
+	else {
+	    uc.val.c = precCell;
+	    ris = avl_add(&albero, uc, totCorr);
+	    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_add unknown error");
+		    return RLI_ERRORE;
+		}
+	    }
+	}
+    }
+    array = G_malloc(m * sizeof(AVL_tableRow));
+    if (array == NULL) {
+	G_fatal_error("malloc array failed");
+	return RLI_ERRORE;
+    }
+    tot = avl_to_array(albero, zero, array);
+    if (tot != m) {
+	G_warning("avl_to_array unaspected value. the result could be wrong");
+	return RLI_ERRORE;
+    }
+
+    char *sval;
+
+    sval = par[0];
+    double alpha_double;
+
+    alpha_double = (double)atof(sval);
+    /* claculate index summary */
+    for (i = 0; i < m; i++) {
+	t = (double)(array[i]->tot);
+	p = t / area;
+	G_debug(1,"Valore p: %g, valore pow: %g",p,pow(p,alpha_double));
+	somma = somma + pow(p, alpha_double);
+    }
+
+    indice = (1 / (1 - alpha_double)) * log(somma);
+    if (isnan(indice) || isinf(indice)) {
+	indice = -1;
+    }
+    G_debug(1, "Valore somma: %g Valore indice: %g", somma, indice);
+
+    *result = indice;
+
+
+    G_free(array);
+    if (masked)
+	G_free(mask_buf);
+
+    return RLI_OK;
+}
+
+
+double calculateD(area_des ad, int fd, char **par, double *result)
+{
+    DCELL *buf;
+    DCELL corrCell;
+    DCELL precCell;
+
+    int i, j;
+    int mask_fd = -1, *mask_buf;
+    int ris = 0;
+    int masked = FALSE;
+    int a = 0;			/* a=0 if all cells are null */
+    long m = 0;
+    long tot = 0;
+    long zero = 0;
+    long totCorr = 0;
+    double indice = 0;
+    double somma = 0;
+    double p = 0;
+    double area = 0;
+    double t;
+
+    avl_tree albero = NULL;
+    AVL_table *array;
+    generic_cell uc;
+    uc.t = DCELL_TYPE;
+
+    /* open mask if needed */
+    if (ad->mask == 1) {
+	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
+	    return RLI_ERRORE;
+	mask_buf = G_malloc(ad->cl * sizeof(int));
+	if (mask_buf == NULL) {
+	    G_fatal_error("malloc mask_buffailed");
+	    return RLI_ERRORE;
+	}
+	masked = TRUE;
+    }
+
+    G_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) {
+		G_fatal_error("mask read failed");
+		return RLI_ERRORE;
+	    }
+	}
+
+	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
+
+	for (i = 0; i < ad->cl; i++) {	/* for each dcell in the row */
+	    area++;
+	    corrCell = buf[i + ad->x];
+
+	    if (masked && mask_buf[i + ad->x] == 0) {
+		G_set_d_null_value(&corrCell, 1);
+		area--;
+	    }
+
+	    if (!(G_is_null_value(&corrCell, uc.t))) {
+		a = 1;
+
+		if (G_is_null_value(&precCell, uc.t)) {
+		    precCell = corrCell;
+		}
+		if (corrCell != precCell) {
+		    if (albero == NULL) {
+			uc.val.dc = precCell;
+			albero = avl_make(uc, totCorr);
+			if (albero == NULL) {
+			    G_fatal_error("avl_make error");
+			    return RLI_ERRORE;
+			}
+			m++;
+		    }
+		    else {
+			uc.val.dc = precCell;
+			ris = avl_add(&albero, uc, totCorr);
+			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;
+			    }
+			}
+		    }
+		    totCorr = 1;
+		}		/* endif not equal dcells */
+		else {		/*equal dcells */
+
+		    totCorr++;
+		}
+		precCell = corrCell;
+	    }
+
+
+	}
+    }
+
+    /*last closing */
+    if (a != 0) {
+	if (albero == NULL) {
+	    uc.val.dc = precCell;
+	    albero = avl_make(uc, totCorr);
+	    if (albero == NULL) {
+		G_fatal_error("avl_make error");
+		return RLI_ERRORE;
+	    }
+	    m++;
+	}
+	else {
+	    uc.val.dc = precCell;
+	    ris = avl_add(&albero, uc, totCorr);
+	    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_add unknown error");
+		    return RLI_ERRORE;
+		}
+	    }
+	}
+    }
+    array = G_malloc(m * sizeof(AVL_tableRow));
+    if (array == NULL) {
+	G_fatal_error("malloc array failed");
+	return RLI_ERRORE;
+    }
+    tot = avl_to_array(albero, zero, array);
+    if (tot != m) {
+	G_warning("avl_to_array unaspected value. the result could be wrong");
+	return RLI_ERRORE;
+    }
+
+    char *sval;
+
+    sval = par[0];
+    double alpha_double;
+
+    alpha_double = (double)atof(sval);
+    /* claculate index summary */
+    for (i = 0; i < m; i++) {
+	t = (double)(array[i]->tot);
+	p = t / area;
+	G_debug(1,"Valore p: %g, valore pow: %g",p,pow(p,alpha_double));
+	somma = somma + pow(p, alpha_double);
+    }
+
+    indice = (1 / (1 - alpha_double)) * log(somma);
+
+    if (isnan(indice) || isinf(indice)) {
+	indice = -1;
+    }
+
+    G_debug(1, "Valore somma: %g Valore indice: %g", somma, indice);
+
+    *result = indice;
+
+    G_free(array);
+    if (masked)
+	G_free(mask_buf);
+
+    return RLI_OK;
+}
+
+
+
+double calculateF(area_des ad, int fd, char **par, double *result)
+{
+    FCELL *buf;
+    FCELL corrCell;
+    FCELL precCell;
+
+    int i, j;
+    int mask_fd = -1, *mask_buf;
+    int ris = 0;
+    int masked = FALSE;
+    int a = 0;			/* a=0 if all cells are null */
+    long m = 0;
+    long tot = 0;
+    long zero = 0;
+    long totCorr = 0;
+    double indice = 0;
+    double somma = 0;
+    double p = 0;
+    double area = 0;
+    double t;
+
+    avl_tree albero = NULL;
+    AVL_table *array;
+    generic_cell uc;
+    uc.t = FCELL_TYPE;
+
+    /* open mask if needed */
+    if (ad->mask == 1) {
+	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
+	    return RLI_ERRORE;
+	mask_buf = G_malloc(ad->cl * sizeof(int));
+	if (mask_buf == NULL) {
+	    G_fatal_error("malloc mask_buf failed");
+	    return RLI_ERRORE;
+	}
+	masked = TRUE;
+    }
+
+    G_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) {
+		G_fatal_error("mask read failed");
+		return RLI_ERRORE;
+	    }
+	}
+
+	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
+
+
+	for (i = 0; i < ad->cl; i++) {	/* for each fcell in the row */
+
+	    area++;
+	    corrCell = buf[i + ad->x];
+
+	    if (masked && mask_buf[i + ad->x] == 0) {
+		G_set_f_null_value(&corrCell, 1);
+		area--;
+	    }
+
+	    if (!(G_is_null_value(&corrCell, uc.t))) {
+		a = 1;
+		if (G_is_null_value(&precCell, uc.t)) {
+		    precCell = corrCell;
+		}
+		if (corrCell != precCell) {
+		    if (albero == NULL) {
+			uc.val.fc = precCell;
+			albero = avl_make(uc, totCorr);
+			if (albero == NULL) {
+			    G_fatal_error("avl_make error");
+			    return RLI_ERRORE;
+			}
+			m++;
+		    }
+		    else {
+			uc.val.fc = precCell;
+			ris = avl_add(&albero, uc, totCorr);
+			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;
+			    }
+			}
+		    }
+		    totCorr = 1;
+		}		/* endif not equal fcells */
+		else {		/*equal fcells */
+
+		    totCorr++;
+		}
+		precCell = corrCell;
+	    }
+
+
+	}
+    }
+
+    /*last closing */
+    if (a != 0) {
+	if (albero == NULL) {
+	    uc.val.fc = precCell;
+	    albero = avl_make(uc, totCorr);
+	    if (albero == NULL) {
+		G_fatal_error("avl_make error");
+		return RLI_ERRORE;
+	    }
+	    m++;
+	}
+	else {
+	    uc.val.fc = precCell;
+	    ris = avl_add(&albero, uc, totCorr);
+	    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_add unknown error");
+		    return RLI_ERRORE;
+		}
+	    }
+	}
+    }
+
+    array = G_malloc(m * sizeof(AVL_tableRow));
+    if (array == NULL) {
+	G_fatal_error("malloc array failed");
+	return RLI_ERRORE;
+    }
+    tot = avl_to_array(albero, zero, array);
+    if (tot != m) {
+	G_warning("avl_to_array unaspected value. the result could be wrong");
+	return RLI_ERRORE;
+    }
+
+    char *sval;
+
+    sval = par[0];
+    double alpha_double;
+
+    alpha_double = (double)atof(sval);
+    /* calculate index summary */
+    for (i = 0; i < m; i++) {
+	t = (double)(array[i]->tot);
+	p = t / area;
+	G_debug(1,"Valore p: %g, valore pow: %g",p,pow(p,alpha_double));        
+	somma = somma + pow(p, alpha_double);
+    }
+
+    indice = (1 / (1 - alpha_double)) * log(somma);
+
+    if (isnan(indice) || isinf(indice)) {
+	indice = -1;
+    }
+
+    G_debug(1, "Valore somma: %g Valore indice: %g", somma, indice);
+
+    *result = indice;
+
+    G_free(array);
+    if (masked)
+	G_free(mask_buf);
+
+    return RLI_OK;
+
+}


Property changes on: grass/branches/develbranch_6/raster/r.li/r.li.renyi/renyi.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native



More information about the grass-commit mailing list