[GRASS-SVN] r47694 - in grass-addons/raster: . r.maxent.lambdas r.out.maxent_swd

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Aug 17 08:20:44 EDT 2011


Author: sbl
Date: 2011-08-17 05:20:44 -0700 (Wed, 17 Aug 2011)
New Revision: 47694

Added:
   grass-addons/raster/r.maxent.lambdas/
   grass-addons/raster/r.maxent.lambdas/Makefile
   grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas
   grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas.html
   grass-addons/raster/r.out.maxent_swd/
   grass-addons/raster/r.out.maxent_swd/Makefile
   grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd
   grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd.html
Log:
Two scripts for import/export data from/to MaxEnt 3.3.3e

Added: grass-addons/raster/r.maxent.lambdas/Makefile
===================================================================
--- grass-addons/raster/r.maxent.lambdas/Makefile	                        (rev 0)
+++ grass-addons/raster/r.maxent.lambdas/Makefile	2011-08-17 12:20:44 UTC (rev 47694)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.maxent.lambdas
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas
===================================================================
--- grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas	                        (rev 0)
+++ grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas	2011-08-17 12:20:44 UTC (rev 47694)
@@ -0,0 +1,272 @@
+#!/bin/sh 
+# 
+############################################################################ 
+# 
+# MODULE:       r.maxent.lambdas 
+# AUTHOR(S):    Stefan Blumentrath <stefan dot blumentrath at nina dot no > 
+# PURPOSE:      Compute raw and/or logistic prediction maps from a lambdas
+#               file produced with MaxEnt 3.3.3e. 
+#
+#               !!!This script works only if the input data to MaxEnt 
+#               are accessible from the current region.!!!
+# 
+#               This script will parse the specified lambdas-file from 
+#               MaxEnt 3.3.3e (see http://www.cs.princeton.edu/~schapire/maxent/) 
+#               and translate it into an r.mapcalc-expression which is then stored
+#               in a temporary file and finally piped to r.mapcalc.
+#               If alias names had been used in MaxEnt, these alias names can 
+#               automatically be replaced according to a  CSV-like file provided 
+#               by the user. This file should contain alias names in the first 
+#               column and map names in the second column, seperated by comma, 
+#               without header. It should look e.g. like this:
+#               
+#               alias_1,map_1
+#               alias_2,map_2
+#               ...,...
+#               
+#               If such a CSV-file with alias names used in MaxEnt is provided, 
+#               the alias names from MaxEnt are replaced by map names. 
+#
+#               A raw output map is always computed from the MaxEnt model
+#               as a first step. If logistic output is requested, the raw output
+#               map can be deleted by the script ( using the l-flag). The 
+#               production of logistic output can be omitted. The logistic map  
+#               can be produced as an integer map. To do so the user has to specify
+#               the number of digits after comma, that should be preserved in 
+#               integer output.   
+#               Optionally the map calculator expressions can be saved in a text 
+#               file, as especially the one for the raw output is likely to exceed 
+#               the space in the map history.
+#               Due to conversion from double to floating-point in exp()-function, a 
+#               loss of precision from the 7th digit onwards is possible in the 
+#               logistic output.
+# 
+# COPYRIGHT:    (C) 2011 by the Norwegian Institute for Nature Research (NINA)
+#               http://www.nina.no
+# 
+#               This program is free software under the GNU General Public 
+#               License (>=v2). Read the file COPYING that comes with GRASS 
+#               for details. 
+# 
+############################################################################# 
+#
+# REQUIREMENTS:
+# awk
+#
+#%Module 
+#% description: Computes raw and/or logistic prediction maps from MaxEnt lambdas files 
+#%End 
+#
+#%flag
+#%  key: r
+#%  description: Produce only raw output (both are computed by default).
+#%end
+#
+#%flag
+#%  key: l
+#%  description: Produce only logistic output (both are computed by default).
+#%end
+#
+#%option 
+#% key: lambdas_file
+#% type: string
+#% description: MaxEnt lambdas-file to compute distribution-model from
+#% required : yes
+#% gisprompt: old_file,file,input
+#%end
+# 
+#%option 
+#% key: output_prefix
+#% type: string 
+#% description: Prefix for output raster maps 
+#% required : yes
+#% gisprompt: new,cell,raster
+#%end 
+#
+#%option 
+#% key: alias_file
+#% type: string
+#% description: CSV-file to replace alias names from MaxEnt by GRASS map names
+#% required : no
+#% gisprompt: old_file,file,input
+#%end
+# 
+#%option 
+#% key: integer_output
+#% type: integer
+#% description: Produce logistic integer output with this number of digits preserved
+#% required : no
+#% answer : 0
+#%end
+# 
+#%option 
+#% key: output_mapcalc
+#% type: string 
+#% description: Save r.mapcalc expression to file 
+#% required : no
+#% gisprompt: new_file,file,output
+#%end 
+#
+#
+FLAG_ONLY_RAW=${GIS_FLAG_R}
+FLAG_ONLY_LOGISTIC=${GIS_FLAG_L}
+LAMBDAS_FILE="${GIS_OPT_LAMBDAS_FILE}"
+INTEGER_OUTPUT=${GIS_OPT_INTEGER_OUTPUT}
+#
+ALIAS_FILE="${GIS_OPT_ALIAS_FILE}"
+#
+OUTPUT_PREFIX="${GIS_OPT_OUTPUT_PREFIX}"
+OUTPUT_MAPCALC="${GIS_OPT_OUTPUT_MAPCALC}"
+#
+#Check if script is started from within GRASS
+if [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+#
+#Check if awk is installed
+	if [ ! -x "`which awk`" ] ; then
+	    g.message -e "awk is required, please install awk or gawk first" 
+	    exit 1
+	fi
+#Set environment so that awk works properly in all languages
+	unset LC_ALL
+	LC_NUMERIC=C
+	export LC_NUMERIC
+#
+#Pass evtl. command line arguments to gui and start it 
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+#
+#Check if input file exists
+if [ ! -r "$LAMBDAS_FILE" ] ; then 
+	g.message -e "MaxEnt lambdas-file could not be found or is not readable."
+	exit 1
+fi 
+#
+#Check if raw output files exists 
+eval `g.findfile element=cell file=${OUTPUT_PREFIX}_raw` 
+if [ -n "$name" ] ; then 
+	g.message -e  "Raw output file already exists."
+	exit 1
+fi
+#
+#Check if logistic output files exists when requestd
+if [ "${FLAG_ONLY_RAW}" -ne 1 ] ; then
+	eval `g.findfile element=cell file=${OUTPUT_PREFIX}_logistic` 
+	if [ -n "$name" ] ; then 
+		g.message -e  "Logistic output file already exists."
+		exit 1
+	fi
+fi
+#
+#Check if output flags are set properly 
+if [ "${FLAG_ONLY_RAW}" -eq 1 -a "${FLAG_ONLY_LOGISTIC}" -eq 1 ] ; then
+	g.message -e  "The r and l flags are exclusive. Do not tick both!"
+	rm "$temp1"
+	exit 1
+fi
+#
+###Save r.mapcalc expression to file if requested, else to tmp-file
+if [ -z "$OUTPUT_MAPCALC" ] ; then
+	#
+	#Create tempfile
+	temp1=`g.tempfile pid=$$`
+	if [ $? -ne 0 ] || [ -z "$temp1" ] ; then
+			g.message -e "ERROR: unable to create temporary files"
+			exit 1
+	fi
+else
+	temp1="$OUTPUT_MAPCALC"
+fi
+#
+###Parse lambdas-file and translate it to a mapcalculator expression (safed in temporary file)
+###Get variables linearPredictorNormalizer, densityNormalizer and entropy from lambdas-file
+linearPredictorNormalizer=$(cat ${LAMBDAS_FILE} | grep linearPredictorNormalizer | sed 's/ //g' | tr -d '\r' | cut -f2 -d",")
+densityNormalizer=$(cat ${LAMBDAS_FILE} | grep densityNormalizer | sed 's/ //g' | tr -d '\r' | cut -f2 -d",")
+entropy=$(cat ${LAMBDAS_FILE} | grep entropy | sed 's/ //g' | tr -d '\r' | cut -f2 -d",")
+###Create file with initial part of mapcalc-expression
+echo "${OUTPUT_PREFIX}_raw=exp(((\\" > "$temp1"
+###Extract raw features
+cat -v "${LAMBDAS_FILE}" | awk '$0 !~ /\^2|\(|\*|\`/' | grep -vP "\'" | sed 's/\^M//g' | sed 's/,//g' | awk 'NF==4{print "if(isnull(" $1 "),0,(" $2 "*(" $1 "-" $3 ")/(" $4 "-" $3 ")))" "+" "\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Extract quadratic features
+cat -v "${LAMBDAS_FILE}" | grep '\^2' | sed 's/\^M//g' | sed 's/,//g' | sed 's/\^2//g' | awk '{print "if(isnull(" $1 "),0,((" $2 "*(" $1 "*" $1 "-" $3 "))/" "(" $4 "-" $3 ")))" "+" "\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Extract product features
+cat -v "${LAMBDAS_FILE}" | grep '*' | sed 's/\^M//g' | sed 's/,//g' | tr '*' ' ' | awk '{print "if((isnull(" $1 ")||isnull(" $2 ")),0,(" $3 "*(" $1 "*" $2 "-" $4 ")/(" $5 "-" $4 ")))+\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Extract forward hinge features
+cat -v "${LAMBDAS_FILE}" | grep -P "\'" | sed 's/\^M//g' | sed 's/,//g' | cut -b 2- | tr '<' ' ' | awk '{print "if(isnull(" $1 "),0,if(" $1 ">=" $3 ",(" $2 "*(" $1 "-" $3 ")/(" $4 "-" $3 ")),0.0))+\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Extract reverse hinge features
+cat -v "${LAMBDAS_FILE}" | grep -P "\`" | sed 's/\^M//g' | sed 's/,//g' | cut -b 2- | tr '<' ' ' | awk '{print "if(isnull(" $1 "),0,if(" $1 "<" $4 ",(" $2 "*(" $4 "-" $1 ")/(" $4 "-" $3 ")),0.0))+\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Extract threshold features
+cat -v "${LAMBDAS_FILE}" | grep '(' | grep -v '=' | sed 's/\^M//g' | sed 's/,//g' | sed 's/(//g' | sed 's/)//g' | tr '<' ' ' | awk '{print "if(isnull(" $2 "),0,if(" $2 ">=" $1 "," $3 "))" "+\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Extract categoric features
+cat -v "${LAMBDAS_FILE}" | grep '(' | grep '=' | sed 's/\^M//g' | sed 's/,//g' | sed 's/(//g' | sed 's/)//g' | tr '=' ' ' | awk '{print "if(isnull(" $1 "),0,if(" $1 "==" $2 "," $3 "))" "+\\"}' | sed 's/--/+/g' | sed 's/+-/-/g' >> $temp1
+###Replace last '\' by tail part of mapcalc-expression
+sed -i '$s/..$/\\/' "$temp1"
+echo ")-${linearPredictorNormalizer}))/${densityNormalizer}" >> "$temp1"
+###Replace possible repetitions of mathmatical signs
+sed -i 's/--/+/g' "$temp1"
+sed -i 's/+-/-/g' "$temp1"
+#
+#Check if alias file is provided and readable
+if [ -n "$ALIAS_FILE" ] ; then
+	if [ ! -r "$ALIAS_FILE" ] ; then 
+		g.message -e "Alias file could not be found or is not readable."
+		exit 1
+	else
+		#Parse alias-file to replace MaxEnt alias names by GRASS map names (including mapset specification)
+		alias_names=$(cat $ALIAS_FILE | cut -f1 -d",")
+		for a in $alias_names
+		do
+		m=$(cat $ALIAS_FILE | grep -w "${a}" | cut -f2 -d",")
+		#
+		g.message -v "Map name for alias $a is $m"
+		#
+		sed -i "s/(${a}-/(${m}-/" "$temp1"
+		sed -i "s/(${a}+/(${m}+/" "$temp1"
+		sed -i "s/(${a}>/(${m}>/" "$temp1"
+		sed -i "s/(${a}</(${m}</" "$temp1"
+		sed -i "s/(${a}=/(${m}=/" "$temp1"
+		sed -i "s/(${a}\*/(${m}\*/" "$temp1"
+		#
+		sed -i "s/-${a})/-${m})/" "$temp1"
+		sed -i "s/+${a})/+${m})/" "$temp1"
+		sed -i "s/>${a})/>${m})/" "$temp1"
+		sed -i "s/<${a})/<${m})/" "$temp1"
+		sed -i "s/=${a})/=${m})/" "$temp1"
+		sed -i "s/\*${a}-/\*${m}-/" "$temp1"
+		sed -i "s/\*${a}+/\*${m}+/" "$temp1"
+		done
+		#
+	fi 
+fi
+#
+###Compute raw output map by sending expression saved in file temporary file to r.mapcalc
+cat "$temp1" | r.mapcalc
+#
+###Compute logistic output map if not suppressed
+if [ "${FLAG_ONLY_RAW}" -eq 0 ] ; then
+	if  [ "${INTEGER_OUTPUT}" -le 0 ] ; then
+		echo "${OUTPUT_PREFIX}_logistic=(${OUTPUT_PREFIX}_raw*exp(${entropy}))/(1.0+(${OUTPUT_PREFIX}_raw*exp(${entropy})))" >> "$temp1"
+		r.mapcalc "${OUTPUT_PREFIX}_logistic=(${OUTPUT_PREFIX}_raw*exp(${entropy}))/(1.0+(${OUTPUT_PREFIX}_raw*exp(${entropy})))"
+	else
+		if  [ "${INTEGER_OUTPUT}" -lt 5 ] ; then
+			echo "${OUTPUT_PREFIX}_logistic=round(((${OUTPUT_PREFIX}_raw*exp(${entropy}))/(1.0+(${OUTPUT_PREFIX}_raw*exp(${entropy}))))*(10^${INTEGER_OUTPUT}))" >> "$temp1"
+			r.mapcalc "${OUTPUT_PREFIX}_logistic=round(((${OUTPUT_PREFIX}_raw*exp(${entropy}))/(1.0+(${OUTPUT_PREFIX}_raw*exp(${entropy}))))*(10^${INTEGER_OUTPUT}))"
+		else
+			echo "${OUTPUT_PREFIX}_logistic=round(((${OUTPUT_PREFIX}_raw*exp(${entropy}))/(1.0+(${OUTPUT_PREFIX}_raw*exp(${entropy}))))*100000.0)" >> "$temp1"
+			r.mapcalc "${OUTPUT_PREFIX}_logistic=round(((${OUTPUT_PREFIX}_raw*exp(${entropy}))/(1.0+(${OUTPUT_PREFIX}_raw*exp(${entropy}))))*100000.0)"
+		fi
+	fi
+fi
+###Remove raw output map if requested
+if [ "${FLAG_ONLY_LOGISTIC}" -eq 1 ] ; then
+	g.remove -f rast="${OUTPUT_PREFIX}"_raw
+fi
+###Remove tmp-file containing r.mapcalc expressions (if save to file not requested)
+if [ -z "$OUTPUT_MAPCALC" ] ; then
+	rm -f "$temp1"
+fi
+
+g.message "Done"


Property changes on: grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas.html
===================================================================
--- grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas.html	                        (rev 0)
+++ grass-addons/raster/r.maxent.lambdas/r.maxent.lambdas.html	2011-08-17 12:20:44 UTC (rev 47694)
@@ -0,0 +1,98 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+<head>
+<title>GRASS GIS manual: r.maxent.lambdas</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+<link rel="stylesheet" href="grassdocs.css" type="text/css">
+</head>
+<body bgcolor="white">
+
+<img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
+
+<h2>NAME</h2>
+<em><b>r.maxent.lambdas</b></em>  - Computes raw and/or logistic prediction maps from MaxEnt lambdas files
+<h2>KEYWORDS</h2>
+raster, MaxEnt, species distribution modelling
+<h2>SYNOPSIS</h2>
+<b>r.maxent.lambdas</b><br>
+<b>r.maxent.lambdas help</b><br>
+<b>r.maxent.lambdas</b> [-<b>rl</b>] <b>lambdas_file</b>=<em>string</em> <b>output_prefix</b>=<em>string</em>  [<b>alias_file</b>=<em>string</em>]   [<b>integer_output</b>=<em>integer</em>]   [<b>output_mapcalc</b>=<em>string</em>]   [--<b>overwrite</b>]  [--<b>verbose</b>]  [--<b>quiet</b>] 
+
+<h3>Flags:</h3>
+<DL>
+<DT><b>-r</b></DT>
+<DD>Produce only raw output (both are computed by default).</DD>
+
+<DT><b>-l</b></DT>
+<DD>Produce only logistic output (both are computed by default).</DD>
+
+<DT><b>--overwrite</b></DT>
+<DD>Allow output files to overwrite existing files</DD>
+<DT><b>--verbose</b></DT>
+<DD>Verbose module output</DD>
+<DT><b>--quiet</b></DT>
+<DD>Quiet module output</DD>
+</DL>
+
+<h3>Parameters:</h3>
+<DL>
+<DT><b>lambdas_file</b>=<em>string</em></DT>
+<DD>MaxEnt lambdas-file to compute distribution-model from</DD>
+
+<DT><b>output_prefix</b>=<em>string</em></DT>
+<DD>Prefix for output raster maps</DD>
+
+<DT><b>alias_file</b>=<em>string</em></DT>
+<DD>CSV-file to replace alias names from MaxEnt by GRASS map names</DD>
+
+<DT><b>integer_output</b>=<em>integer</em></DT>
+<DD>Produce logistic integer output with this number of digits preserved</DD>
+<DD>Default: <em>0</em></DD>
+
+<DT><b>output_mapcalc</b>=<em>string</em></DT>
+<DD>Save r.mapcalc expression to file</DD>
+</DL>
+<h2>DESCRIPTION</h2>
+The script is intended to compute raw and/or logistic prediction maps from a lambdas file produced with MaxEnt 3.3.3e.<br>
+It will parse the specified lambdas-file from MaxEnt 3.3.3e and translate it into an r.mapcalc-expression 
+which is then stored in a temporary file and finally piped to r.mapcalc. If alias names had been used in MaxEnt, these 
+alias names can automatically be replaced according to a CSV-like file provided by the user. 
+This file should contain alias names in the first column and map names in the second column, seperated by comma, 
+without header. It should look e.g. like this:<br>
+<br>
+<em>
+alias_1,map_1<br>
+alias_2,map_2<br>
+...,...<br>
+</em>
+<br>
+If such a CSV-file with alias names used in MaxEnt is provided, the alias names from MaxEnt are replaced by map names.<br>
+<br>
+A raw output map is always computed from the MaxEnt model as a first step. If logistic output is requested, the raw output 
+map can be deleted by the script ( using the l-flag). The logistic map can be produced as an integer map. To do so the user 
+has to specify the number of decimal places, that should be preserved in integer output.<br>
+Optionally the map calculator expressions can be saved in a text file as especially the one for the raw output is likely to exceed 
+the space in the map history.
+
+<h2>NOTES</h2>
+This script works only if the maps containing the input data to MaxEnt are accessible from the current region.<br>
+Due to conversion from double to floating-point in exp()-function, a loss of precision from the 7th decimal place onwards may occur 
+in the logistic output.
+
+<h2>SEE ALSO</h2>
+r.out.maxent_swd, r.in.xyz<br>
+MaxEnt 3.3.3e <a href="http://www.cs.princeton.edu/~schapire/maxent/">http://www.cs.princeton.edu/~schapire/maxent/</a><br>
+Jane Elith, Steven J. Phillips, Trevor Hastie, Miroslav Dudík, Yung En Chee, Colin J. Yates. 2011: A statistical explanation of MaxEnt 
+for ecologists. Diversity and Distributions, (17):43-57, 2011.<br>
+Wilson, Peter D. 2009: Guidelines for computing MaxEnt model output values from a lambdas file. (Avaliable at <a href="http://groups.google.com/group/MaxEnt">
+http://groups.google.com/group/MaxEnt</a>)
+Steven J. Phillips, Robert P. Anderson, Robert E. Schapire. 2006: Maximum entropy modeling of species geographic distributions. 
+Ecological Modelling, (190):231-259, 2006.<br>
+Steven J. Phillips, Miroslav Dudík, Robert E. Schapire. 2004: A maximum entropy approach to species distribution modeling. In: Proceedings 
+of the Twenty-First International Conference on Machine Learning, p. 655-662, 2004.
+
+<h2>AUTHOR</h2>
+Stefan Blumentrath, Norwegian Institute for Nature Research (NINA), <a href="http://www.nina.no">http://www.nina.no</a>
+
+</body>
+</html>

Added: grass-addons/raster/r.out.maxent_swd/Makefile
===================================================================
--- grass-addons/raster/r.out.maxent_swd/Makefile	                        (rev 0)
+++ grass-addons/raster/r.out.maxent_swd/Makefile	2011-08-17 12:20:44 UTC (rev 47694)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.out.maxent_swd
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd
===================================================================
--- grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd	                        (rev 0)
+++ grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd	2011-08-17 12:20:44 UTC (rev 47694)
@@ -0,0 +1,359 @@
+#!/bin/sh 
+# 
+############################################################################ 
+# 
+# MODULE:       r.maxent.lambdas 
+# AUTHOR(S):    Stefan Blumentrath <stefan dot blumentrath at nina dot no > 
+# PURPOSE:      Produce a set of SWD file as input to MaxEnt 3.3.3e using r.stats.
+#               
+#               The SWD file format is a simple CSV-like file file format as
+#               described in Elith et al. 2011. Generally it looks like:
+#               
+#               specie_name,X,Y,parameter_1,parameter_2,...
+#               your_specie,1.1,1.1,1,1,...
+#
+#               The first column always contains the name of a species, followed by
+#               two colums for the X- and Y-coordinates. Then each column 
+#               represents one environmental parameter. In contrast to r.stats
+#               only integer values are accepted to represent NO DATA.
+#
+#               A background SWD file is allways produced while specie output 
+#               can be omitted.
+# 
+#               Multiple species can be processed, but each has to be in an
+#               individual raster map. Map names of the maps containing the 
+#               environmental parameters can be replaced by short names, 
+#				that should be used in MaxEnt 3.3.3.e.
+#
+#               Results from MaxEnt can either be imported by r.in.xyz or
+#               calculated from MaxEnt lambdas file using the script 
+#               r.maxent.lambdas.
+#
+# 
+# COPYRIGHT:    (C) 2011 by the Norwegian Institute for Nature Research 
+#               http://www.nina.no
+# 
+#               This program is free software under the GNU General Public 
+#               License (>=v2). Read the file COPYING that comes with GRASS 
+#               for details. 
+# 
+############################################################################# 
+#
+# REQUIREMENTS:
+# -
+#%Module 
+#% description: Exports map data as input to MaxEnt in SWD format 
+#%End 
+#
+#%flag
+#% key: b
+#% description: Produce only background output
+#% guisection: Specie(s)
+#%end
+#
+#%flag
+#% key: z
+#% description: Zoom computational region to species data (may speed up processing)
+#% guisection: Specie(s)
+#%end
+#
+#%option
+#% key: alias_input
+#% type: string
+#% description: File containg map and alias name(s) for environmental parameter(s)
+#% required: no
+#% multiple: no
+#% gisprompt: old_file,file,input
+#%end
+#
+#%option 
+#% key: evp_maps
+#% type: string
+#% description: Environmental parameter map(s)
+#% guisection: Environmental parameter
+#% required : no
+#% multiple : yes
+#% gisprompt: old,cell,raster
+#%end
+# 
+#%option 
+#% key: alias_names
+#% type: string
+#% description: Alias names for environmental parameter
+#% guisection: Environmental parameter
+#% required : no
+#%end
+# 
+#%option 
+#% key: alias_output
+#% type: string 
+#% description: Save alias names for the environmental parameters to file
+#% guisection: Environmental parameter
+#% required : no
+#% multiple: no
+#% gisprompt: new_file,file,output
+#%end 
+#
+#%option 
+#% key: bgr_output
+#% type: string 
+#% description: Output SWD file for the environmental data of the background landscape
+#% guisection: Background
+#% required : no
+#% multiple: no
+#% gisprompt: new_file,file,output
+#%end 
+#
+#%option 
+#% key: bgr_mask
+#% type: string
+#% description: Map to be used as mask for the background landscape
+#% guisection: Background
+#% required : no
+#% multiple: no
+#% gisprompt: old,cell,raster
+#%end
+#
+#%option 
+#% key: specie_mask
+#% type: string
+#% description: Raster map(s) of specie occurence
+#% guisection: Specie(s)
+#% required : no
+#% multiple : yes
+#% gisprompt: old,cell,raster
+#%end
+# 
+#%option 
+#% key: specie_output
+#% type: string 
+#% description: Output SWD file for the specie(s) related environmental data 
+#% guisection: Specie(s)
+#% required : no
+#% multiple: no
+#% gisprompt: new_file,file,output
+#%end 
+#
+#%option 
+#% key: specie_name
+#% type: string 
+#% description: Alias-name(s) for species to be used in MaxEnt SWD file instead of map names, separated by comma (default: map names). 
+#% guisection: Specie(s)
+#% required : no
+#%end 
+#
+#%Option
+#% key: nv
+#% type: integer
+#% required: no
+#% multiple: no
+#% description: Integer representing NO DATA cell value (default: -9999)
+#% guisection: Output formating
+#% answer: -9999
+#%end
+#
+#
+ALIAS_INPUT=${GIS_OPT_ALIAS_INPUT}
+EVP_MAPS=${GIS_OPT_EVP_MAPS}
+ALIAS_NAMES=${GIS_OPT_ALIAS_NAMES}
+ALIAS_OUTPUT=${GIS_OPT_ALIAS_OUTPUT}
+#
+BGR_OUTPUT=${GIS_OPT_BGR_OUTPUT}
+BACKGROUND_MASK=${GIS_OPT_BGR_MASK}
+#
+FLAG_ONLY_BGR=${GIS_FLAG_B}
+FLAG_ZOOM_SPECIE=${GIS_FLAG_Z}
+SPECIE_MASK=${GIS_OPT_SPECIE_MASK}
+SPECIE_NAME=${GIS_OPT_SPECIE_NAME}
+SPECIE_OUTPUT=${GIS_OPT_SPECIE_OUTPUT}
+#
+NV=${GIS_OPT_NV}
+#
+#
+#Check if script is started from within GRASS
+if [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+#
+#Start gui 
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+#
+#Get Environment variables
+eval `g.gisenv`
+#
+#Check if input variables for the environmental parameter(s) are set properly
+if [ -z "$ALIAS_INPUT" -a -z "$EVP_MAPS" ] ; then
+	g.message -e "You have to provide either an alias file or environmental parameter map(s)."
+	exit 1
+fi
+#
+if [ -n "$ALIAS_INPUT" ] ; then
+	if [ -r "$ALIAS_INPUT" ] ; then
+		g.message -v "Alias and map names are beeing read from file. Other input regarding environmental parameter(s) will be ignored..."
+		parameter=$(cat "$ALIAS_INPUT" | cut -s -f2 -d',' | tr "\n" ' ' | tr '@' '#')
+	else
+		g.message -e "The file containing alias names does not exist or is not readable."
+		exit 1
+	fi
+else
+	parameter=$(echo "$EVP_MAPS" | tr ',' ' ' | tr '@' '#' | sed 's/.$//')
+fi
+#
+#Check if environmental parameter map(s) exist
+p_number=1
+for p in $parameter
+do
+	#Check if parameter map file(s) exist
+	map_name=$(echo $p | cut -f1 -d'#' | tr -d ' ')
+	mapset_name=$(echo $p | cut -f2 -d'#' | tr -d ' ')
+	#
+	eval `g.findfile element=cell file=${map_name} mapset=${mapset_name}` 
+	if [ -z "$name" ] ; then 
+		g.message -e  "Could not find environmental parameter raster map ${map_name} in ${mapset_name}."
+		exit 1
+	fi
+	#Build parameter header if necessary
+	if [ -z "$ALIAS_INPUT" ] ; then
+		if [ -z "$ALIAS_NAMES" ] ; then
+			#Use map name(s) as header
+			parameter_list="${parameter_list}",${name}
+		else
+			name=$(echo "$ALIAS_NAMES" | cut -f$p_number -d ',')
+			parameter_list="${parameter_list}",${name}
+			p_number=`expr ${p_number} + 1`
+		fi
+		if [ -n "$ALIAS_OUTPUT" ] ; then
+			if [ -r "$ALIAS_OUTPUT" ] ; then
+				rm "$ALIAS_OUTPUT"
+			fi
+			echo ${name},${p} >> "$ALIAS_OUTPUT"
+		fi
+	fi
+done
+
+if [ -n "$ALIAS_INPUT" ] ; then
+	#Check input alias file
+	if [ ! -r "$ALIAS_INPUT" ] ; then
+		g.message -e  "Could not find file with alias names ${ALIAS_INPUT} or file is not readable."			
+		exit 1
+	else
+		parameter_list_pre=$(cat "$ALIAS_INPUT" | cut -f1 -d',' | tr -d ' ' | tr "\n" ',' | sed 's/.$//')
+		parameter_list=,"${parameter_list_pre}"
+		EVP_MAPS=$(cat "$ALIAS_INPUT" | cut -f2 -d',' | tr -d ' ' | tr "\n" ',' | sed 's/.$//')
+	fi
+fi
+#
+parameter_names=$(echo "${parameter_list}" | sed 's/^,//g')
+#
+#Check if specie output is requested and produce it
+if [ "${FLAG_ONLY_BGR}" -ne 1 ] ; then
+	#Parse species input variables
+	species=$(echo "$SPECIE_MASK" | tr ',' ' ')
+	#
+	#Check input data
+	#
+	#Check specie naming (if provided)
+	if [ -n "$SPECIE_NAME" ] ; then
+		specie_names=$(echo "$SPECIE_NAME" | tr ',' ' ')
+		specie_number=$(echo "$species" | wc -w)
+		specie_names_number=$(echo "$specie_names" | wc -w)
+		#
+		#Check if number of specie names is identically with number of specie maps
+		if [ $specie_number -ne $specie_names_number ] ; then
+			g.message -e "You must provide one name for each rastermap representing a specie."
+			exit 1
+		fi 
+	fi
+	#
+	#Write header to specie output SWD file
+	echo specie,X,Y,"${parameter_names}" > "$SPECIE_OUTPUT"
+	#
+	#Check if specie map(s) exist
+	s_number=1
+	for s in "$species"
+	do
+		#Check if specie mask files exist
+		specie_map_name=$(echo $p | cut -f1 -d'#' | tr -d ' ')
+		specie_mapset_name=$(echo $p | cut -f2 -d'#' | tr -d ' ')
+		#
+		eval `g.findfile element=cell file=${specie_map_name} mapset=${specie_mapset_name}` 
+		if [ -z "$name" ] ; then 
+			g.message -e  "Could not find specie map ${specie_map_name} in mapset ${specie_mapset_name}. Skipping..."
+			s_number=`expr ${s_number} + 1`
+			continue
+		else
+			if [ -n "$SPECIE_NAME" ] ; then
+				s_name=$(echo "$SPECIE_NAME" | cut -f${s_number} -d',')
+				s_number=`expr ${s_number} + 1`
+				else
+				s_name="$specie_map_name"
+			fi
+		fi
+		#
+		#Zoom region to match specie map if requested
+		if [ "${FLAG_ZOOM_SPECIE}" -ne 0 ] ; then
+			g.message -v "Zooming region to specie ${s_name}"
+			g.region -u rast="${s}" zoom="${s}" save="${s_name}"
+			WIND_OVERRIDE="${s_name}"
+			export WIND_OVERRIDE
+		fi
+		#Check if a mask file allready exists
+		eval `g.findfile element=cell file=MASK mapset=${MAPSET}` 
+		if [ "$name" = MASK ] ; then 
+			g.message -v "A mask allready exists. Renaming existing mask to old_MASK..."
+			g.rename rast=MASK${MAPSET},old_MASK --q
+		fi
+		#
+		#Apply specie mask
+		r.mask -o input="${s}" --q
+		#
+		#Export data using r.stats
+		g.message -v "Producing output for specie ${s_name}"
+		r.stats -1 -g -N --verbose input="$EVP_MAPS" fs=',' nv=$NV | sed "s/^/${s_name},/g" >> "$SPECIE_OUTPUT"
+		#		
+		#Redo zoom region to match specie map if it had been requested
+		if [ "${FLAG_ZOOM_SPECIE}" -ne 0 ] ; then
+			unset WIND_OVERRIDE
+			g.remove region="${s_name}" --q
+		fi
+		#Remove mask
+		r.mask -r --q
+	done
+fi
+#
+#Write header to background output SWD file
+echo bgr,X,Y,"${parameter_names}"  > "$BGR_OUTPUT"
+#
+#Process map data for background
+#g.region -p --verbose region="$BACKGROUND_MASK"
+#Check if a mask file allready exists
+if [ -n "$BACKGROUND_MASK" ] ; then
+	g.message -v "Using map $BACKGROUND_MASK as mask for the background landscape..."
+	eval `g.findfile element=cell file=MASK mapset=${MAPSET}` 
+	if [ "$name" = MASK ] ; then 
+		g.message -v "A mask allready exists. Renaming existing mask to old_MASK..."
+		g.rename rast=MASK@${MAPSET},old_MASK --q
+	fi
+	#
+	#Apply mask
+	r.mask -o input="${BACKGROUND_MASK}" --q
+fi
+#
+#Export data using r.stats
+g.message -v "Producing output for background landscape"
+r.stats -1 -g -N --verbose input="$EVP_MAPS" fs=',' nv=$NV | sed 's/^/bgr,/g' >> "$BGR_OUTPUT"
+#Remove mask
+eval `g.findfile element=cell file=MASK mapset=${MAPSET}` 
+if [ -n "$name" ] ; then 
+	r.mask -r --q
+fi
+#
+#Restore old mask if it existed
+eval `g.findfile element=cell file=old_MASK mapset=${MAPSET}` 
+if [ "$name" = old_MASK ] ; then 
+	g.message -v "Restoring old mask..."
+	g.rename rast=old_MASK,MASK --q
+fi


Property changes on: grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd.html
===================================================================
--- grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd.html	                        (rev 0)
+++ grass-addons/raster/r.out.maxent_swd/r.out.maxent_swd.html	2011-08-17 12:20:44 UTC (rev 47694)
@@ -0,0 +1,124 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+<head>
+<title>GRASS GIS manual: r.out.maxent_swd</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+<link rel="stylesheet" href="grassdocs.css" type="text/css">
+</head>
+<body bgcolor="white">
+
+<img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
+
+<h2>NAME</h2>
+<em><b>r.out.maxent_swd</b></em>  - Exports map data as input to MaxEnt in SWD format
+<h2>KEYWORDS</h2>
+raster, MaxEnt, species distribution modelling
+<h2>SYNOPSIS</h2>
+<b>r.out.maxent_swd</b><br>
+<b>r.out.maxent_swd help</b><br>
+<b>r.out.maxent_swd</b> [-<b>bz</b>]  [<b>alias_input</b>=<em>string</em>]   [<b>evp_maps</b>=<em>string</em>[,<i>string</i>,...]]   [<b>alias_names</b>=<em>string</em>]   [<b>alias_output</b>=<em>string</em>]   [<b>bgr_output</b>=<em>string</em>]   [<b>bgr_mask</b>=<em>string</em>]   [<b>specie_mask</b>=<em>string</em>[,<i>string</i>,...]]   [<b>specie_output</b>=<em>string</em>]   [<b>specie_name</b>=<em>string</em>]   [<b>nv</b>=<em>integer</em>]   [--<b>verbose</b>]  [--<b>quiet</b>] 
+
+<h3>Flags:</h3>
+<DL>
+<DT><b>-b</b></DT>
+<DD>Produce only background output</DD>
+
+<DT><b>-z</b></DT>
+<DD>Zoom computational region to species data</DD>
+<br>Setting this flag may speed up processing for very large regions and locally distributed
+species. But it may decrease processing time in other cases. Use with care.
+
+<DT><b>--verbose</b></DT>
+<DD>Verbose module output</DD>
+<DT><b>--quiet</b></DT>
+<DD>Quiet module output</DD>
+</DL>
+
+<h3>Parameters:</h3>
+<DL>
+<DT><b>alias_input</b>=<em>string</em></DT>
+<DD>File containg map and alias name(s) for environmental parameter(s)</DD>
+<br>This file should contain alias names in the first column and map names in the second column, separated by comma, 
+without header. It should look e.g. like this:<br>
+<em>
+alias_1,map_1<br>
+alias_2,map_2<br>
+...,...<br>
+</em>
+
+<DT><b>evp_maps</b>=<em>string[,<i>string</i>,...]</em></DT>
+<DD>Environmental parameter map(s)</DD>
+<br>Multiple maps containing the environmental parameter(s) to be used in MaxEnt.
+
+<DT><b>alias_names</b>=<em>string</em></DT>
+<DD>Alias names for environmental parameter</DD>
+<br>A list of names to be used as alias names for the environmental parameter map names.
+
+<DT><b>alias_output</b>=<em>string</em></DT>
+<DD>Save alias names for the environmental parameters to file</DD>
+<br>When using alias names for the environmental parameter map names, it is advantageous to save the alias names to file. 
+In this case the alias file can be used to re-translate the alias names back to map names when using the script r.maxent.lambdas.
+
+<DT><b>bgr_output</b>=<em>string</em></DT>
+<DD>Output SWD file for the environmental data of the background landscape</DD>
+
+<DT><b>bgr_mask</b>=<em>string</em></DT>
+<DD>Map to be used as mask for the background landscape</DD>
+
+<DT><b>specie_mask</b>=<em>string[,<i>string</i>,...]</em></DT>
+<DD>Raster map(s) of specie occurence</DD>
+
+<DT><b>specie_output</b>=<em>string</em></DT>
+<DD>Output SWD file for the specie(s) related environmental data</DD>
+
+<DT><b>specie_name</b>=<em>string</em></DT>
+<DD>Alias-name(s) for species to be used in MaxEnt SWD file instead of map names, separated by comma (default: map names).</DD>
+
+<DT><b>nv</b>=<em>integer</em></DT>
+<DD>Integer representing NO DATA cell value</DD>
+<DD>Default: <em>-9999</em></DD>
+
+</DL>
+<h2>DESCRIPTION</h2>
+The script is intended to produce a set of SWD files as input to MaxEnt 3.3.3e using r.stats.<br>
+<br>
+The SWD file format is a simple CSV-like file file format as described in Elith et al. 2011. 
+Generally it looks like:<br>
+<em>
+specie_name,X,Y,parameter_1,parameter_2,...<br>
+your_specie,1.1,1.1,2,4.7,...<br>
+</em>
+The first column always contains the name of the species, followed by 
+two colums for the X- and Y-coordinates. Then each column 
+represents one environmental parameter. In contrast to r.stats 
+only integer values are accepted to represent NO DATA.<br>
+<br>
+A background SWD file is allways produced while specie output can be omitted.<br>
+<br>
+Multiple species can be processed, but each has to be in an 
+individual raster map. Map names of the maps containing the environmental 
+parameters can be replaced by short names, which should be used 
+in MaxEnt 3.3.3.e.<br>
+<br>
+Results from MaxEnt can either be imported using r.in.xyz or 
+calculated from MaxEnt lambdas file using the script 
+r.maxent.lambdas.
+
+<h2>NOTES</h2>
+
+<h2>SEE ALSO</h2>
+r.stats<br>
+MaxEnt 3.3.3e <a href="http://www.cs.princeton.edu/~schapire/maxent/">http://www.cs.princeton.edu/~schapire/maxent/</a><br>
+Wilson, Peter D. 2009: Guidlelines for computing MaxEnt model output values from a lambdas file. (Avaliable at <a href="http://groups.google.com/group/MaxEnt">
+http://groups.google.com/group/MaxEnt</a>)
+Steven J. Phillips, Miroslav Dudík, Robert E. Schapire. 2004: A maximum entropy approach to species distribution modeling. In Proceedings 
+of the Twenty-First International Conference on Machine Learning, pages 655-662, 2004.<br>
+Steven J. Phillips, Robert P. Anderson, Robert E. Schapire. 2006: Maximum entropy modeling of species geographic distributions. 
+Ecological Modelling, 190:231-259, 2006.<br>
+Jane Elith, Steven J. Phillips, Trevor Hastie, Miroslav Dudík, Yung En Chee, Colin J. Yates. 2011: A statistical explanation of MaxEnt 
+for ecologists. Diversity and Distributions, 17:43-57, 2011.
+
+<h2>AUTHOR</h2>
+Stefan Blumentrath, Norwegian Institute for Nature Research (NINA), <a href="http://www.nina.no">http://www.nina.no</a>
+</body>
+</html>



More information about the grass-commit mailing list