[GRASS-SVN] r53177 - grass-addons/grass6/raster/r.connectivity.network

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Sep 13 02:15:33 PDT 2012


Author: sbl
Date: 2012-09-13 02:15:33 -0700 (Thu, 13 Sep 2012)
New Revision: 53177

Modified:
   grass-addons/grass6/raster/r.connectivity.network/r.connectivity.network
Log:
Further fixes for WinGRASS

Modified: grass-addons/grass6/raster/r.connectivity.network/r.connectivity.network
===================================================================
--- grass-addons/grass6/raster/r.connectivity.network/r.connectivity.network	2012-09-12 20:32:08 UTC (rev 53176)
+++ grass-addons/grass6/raster/r.connectivity.network/r.connectivity.network	2012-09-13 09:15:33 UTC (rev 53177)
@@ -72,15 +72,6 @@
 #%end 
 #
 #%option 
-#% key: cd_cutoff
-#% type: double
-#% description: Maximum (cost) distance used for plotting the negative exponential decay kernel
-#% guisection: Settings
-#% required: no
-#% answer: 0.0
-#%end
-# 
-#%option 
 #% key: connectivity_cutoff
 #% type: double
 #% description: Maximum cost distance for connectivity
@@ -182,6 +173,11 @@
 #% answer: 1
 #%end
 # 
+#%flag
+#% key: i
+#% description: Install required R packages in an interactive session if they are missing
+#%end
+#
 
 #Input
 FOLDER=$(echo "${GIS_OPT_FOLDER}" | sed 's/\/$//g')
@@ -196,6 +192,7 @@
 CONNECTIVITY_CUTOFF="${GIS_OPT_CONNECTIVITY_CUTOFF}"
 LNBH_CUTOFF="${GIS_OPT_LNBH_CUTOFF}"
 CL_THRES="${GIS_OPT_CL_THRES}"
+CD_CUTOFF=$(echo $CONNECTIVITY_CUTOFF | awk '{print $1 * 3}')
 
 #Visualise negative exponential decay kernel and exit
 X_FLAG=${GIS_FLAG_X}
@@ -203,6 +200,9 @@
 #Calculate edge betweenness community (default is do not)
 Y_FLAG=${GIS_FLAG_Y}
 
+#Install missing R packages
+I_FLAG=${GIS_FLAG_I}
+
 #
 #Check if script is started from within GRASS
 if [ -z "$GISBASE" ] ; then
@@ -231,7 +231,19 @@
 VERTICES=${PREFIX}_vertices
 NETWORK_MEASURES="${GIS_OPT_NETWORK_MEASURES}"
 
+#OS adjustment
+if [ $OS ] ; then
+	OS_type=$(echo $OS | cut -c 1-3)
+else
+	OS_type=Other
+fi
 
+#Path-settings for R on Windows
+WIN_FOLDER=$(echo "$FOLDER" | tr '\\' '/')
+WIN_KERNEL_PLOT=$(echo "$KERNEL_PLOT" | tr '\\' '/')
+WIN_OVERVIEW_PLOT=$(echo "$OVERVIEW_PLOT" | tr '\\' '/')
+
+
 g.message -v "FOLDER is ${GIS_OPT_FOLDER}"
 g.message -v "CORES is ${GIS_OPT_CORES}"
 g.message -v "CONVERGENCE_THRESHOLD is ${GIS_OPT_CONVERGENCE_THRESHOLD}"
@@ -270,20 +282,40 @@
 
 #Check if ghostscript is installed
 if [ $OVERVIEW_PLOT ] ; then
-	if [ ! -x "`which ghostscript`" ] ; then
-	    g.message -e "ghostscript is required for postscript output, please install ghostscript first" 
-	    exit 1
+	if [ "$OS_type" = "Win" ] ; then
+		g.message -v "Assuming ghostscript is installed on MS Windows" 
+	#Check for ghostscript deactivated on Windows because the path to gswin32 is not defined in the environment settings by ghostscript installation automatically
+	#	if [ ! -x "`which gswin32`" ] ; then
+	#		g.message -e "ghostscript is required for postscript output, can not find ghostscript (gswin32.exe), please install ghostscript first (or check environment path settings)" 
+	#		exit 1
+	#	fi
+	else
+		if [ ! -x "`which ghostscript`" ] ; then
+			g.message -e "ghostscript is required for postscript output, please install ghostscript first" 
+			exit 1
+		fi
+	
 	fi
 fi
 if [ $KERNEL_PLOT ]; then
-	if [ ! -x "`which ghostscript`" ] ; then
-	    g.message -e "ghostscript is required for postscript output, please install ghostscript first" 
-	    exit 1
+	if [ "$OS_type" = "Win" ] ; then
+		g.message -v "Assuming ghostscript is installed on MS Windows" 
+	#Check for ghostscript deactivated on Windows because the path to gswin32 is not defined in the environment settings by ghostscript installation automatically
+	#	if [ ! -x "`which gswin32`" ] ; then
+	#		g.message -e "ghostscript is required for postscript output, can not find ghostscript (gswin32.exe), please install ghostscript first (or check environment path settings)" 
+	#		exit 1
+	#	fi
+	else
+		if [ ! -x "`which ghostscript`" ] ; then
+			g.message -e "ghostscript is required for postscript output, please install ghostscript first" 
+			exit 1
+		fi
+	
 	fi
 fi
 
 #Check if all necessary R-packages are installed properly
-if [ $CORES -le 1 ] ; then
+if [ $CORES -le 1 -o $OS_type = "Win" ] ; then
 missing_packages=$(echo \
 'installed_packages <- .packages(all.available = TRUE)
 missing_packages <- as.character()
@@ -316,9 +348,18 @@
 cat(missing_packages, sep=", ")
 }' | R --no-save --no-restore --slave)
 fi
+#Check for missing R-packages and install them if requested
 if [ -n "$missing_packages" ] ; then
-	g.message -e "${missing_packages}"
-	exit 1
+	if [ $I_FLAG -eq 1 ] ; then
+		if [ $OS_type = "Win" ] ; then
+			echo $missing_packages | awk '{print "install.packages(\"" $2 "\")"}' | R --no-save --no-restore --ess
+		else
+			echo $missing_packages | awk '{print "install.packages(\"" $2 "\")"}' | R --no-save --no-restore --interactive
+		fi
+	else
+		g.message -e "${missing_packages}"
+		exit 1
+	fi
 fi
 
 #Check igraph version
@@ -363,6 +404,7 @@
 
 #Start processing
 #Copy R script file to temporary folder
+#The '$' signs which should be evaluated first in R have to be masked with a '\' in the EOF-section of this shell-script
 cat << EOF > ${FOLDER}/network.r
 ########################################################################
 ###Preparation
@@ -393,6 +435,9 @@
 cl_thres <- $CL_THRES
 
 #Input files
+#Windows path for folder
+folder <- "$WIN_FOLDER"
+#Path for folder
 folder <- "$FOLDER"
 
 v_input_data_1 <- paste(folder, "vertices_part_1.csv", sep="/")
@@ -401,7 +446,14 @@
 e_input_data <- paste(folder, "edges.csv", sep="/")
 
 #Output files
+#Windows path to kernel plot file
+decay_kernel_ps <- "$WIN_KERNEL_PLOT"
+#Path to kernel plot file
 decay_kernel_ps <- "$KERNEL_PLOT"
+
+#Windows path to output overview plot file
+network_overview_ps <- "$WIN_OVERVIEW_PLOT"
+#Path to output overview plot file
 network_overview_ps <- "$OVERVIEW_PLOT"
 
 network_measures <- paste(folder, "network_measures.csv", sep="/")
@@ -1434,17 +1486,20 @@
 EOF
 
 #Suppress parallel processing if not requested
-if [ $CORES -le 1 ] ; then
-	cat ${FOLDER}/network.r | sed "s/mclapply/lapply/g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/library(codetools)//g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/library(multicore)//g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/library(iterators)//g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/library(foreach)//g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/library(doMC)//g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/registerDoMC()//g" > ${FOLDER}/network.r
-	cat ${FOLDER}/network.r | sed "s/options(cores = $CORES)//g" > ${FOLDER}/network.r
+if [ $CORES -le 1 -o "$OS_type" = "Win" ] ; then
+	cat ${FOLDER}/network.r | sed "s/mclapply/lapply/g" | sed "/options(cores = $CORES)/d" | sed "/library(doMC)/d" | sed "/library(foreach)/d" | sed "/library(iterators)/d" | sed "/library(multicore)/d" | sed "/library(codetools)/d" | sed "/registerDoMC()/d" > ${FOLDER}/network_tmp.r
+	mv ${FOLDER}/network_tmp.r ${FOLDER}/network.r
 fi
 
+#OS adjustment of path settings in R script
+if [ "$OS_type" = "Win" ] ; then
+	awk '/^#Path/{c=2}!(c&&c--)' "${FOLDER}"/network.r > ${FOLDER}/network_tmp.r
+	mv ${FOLDER}/network_tmp.r ${FOLDER}/network.r
+else
+	awk '/^#Windows/{c=2}!(c&&c--)' "${FOLDER}"/network.r > ${FOLDER}/network_tmp.r
+	mv ${FOLDER}/network_tmp.r ${FOLDER}/network.r
+fi
+
 #Run R-igraph script
 R < ${FOLDER}/network.r --no-save --no-restore --slave
 



More information about the grass-commit mailing list