From b3ebcd05bfbbfbe5df2fb86048bf267ebf4c700a Mon Sep 17 00:00:00 2001
From: Peter Alexander <p.m.w.alexander@gmail.com>
Date: Wed, 11 Oct 2017 14:00:27 +0100
Subject: [PATCH] Cluster scripts updated

---
 scripts/concatOutputLc.sh        |  9 ++++
 scripts/runPlum.sh               | 12 +++++-
 scripts/summariseLandUseOneSim.R | 71 ++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 scripts/summariseLandUseOneSim.R

diff --git a/scripts/concatOutputLc.sh b/scripts/concatOutputLc.sh
index c40ea7a7..3d352bbd 100755
--- a/scripts/concatOutputLc.sh
+++ b/scripts/concatOutputLc.sh
@@ -7,8 +7,10 @@ fi
 cd $1
 concatLcFile=lc_concat.txt
 concatPriceFile=price_concat.txt
+concatLUFile=LandUseSummary.csv
 echo "Scenario,Year,Cropland,Pasture,ManForest,UnmanForest,Natural,EnergyCrop,FertCrop,IrrigCrop" > $concatLcFile
 echo "Scenario,Year,Crop,Imports,Exports,Price,Stocks" > $concatPriceFile
+echo "Scenario,cropType,yield,irrig,fert,otherint,area,Year" > $concatLUFile
 
 find . -name "*lc.txt"|while read fname; do
         scenarioDir="$(dirname $fname)"
@@ -29,4 +31,11 @@ find . -name "*lc.txt"|while read fname; do
 		test $j -eq 1 && ((j=j+1)) && continue
 		echo "${scenario},$line" >> $concatPriceFile
 	done < $scenarioDir/prices.txt
+
+	k=1
+	while read -r line
+	do
+		test $k -eq 1 && ((k=k+1)) && continue
+		echo "${scenario},$line" >> $concatLUFile
+	done < $scenarioDir/LandUseSummary.csv
 done
diff --git a/scripts/runPlum.sh b/scripts/runPlum.sh
index 0fd8847f..9ecafedc 100755
--- a/scripts/runPlum.sh
+++ b/scripts/runPlum.sh
@@ -4,7 +4,7 @@
 #$ -l h_vmem=8G 
 #$ -R y
 
-if [ $# -ne 1 ]; then
+if [ $# -lt 1 ]; then
 	echo need 1 argument to specify config/output directory
 	exit 0
 fi
@@ -21,3 +21,13 @@ echo "starting"
 java -Xmx1G -XX:+PrintGC -classpath /exports/csce/eddie/geos/groups/LURG/models/gams/gams24.7_linux_x64_64_sfx/apifiles/Java/api/GAMSJavaAPI.jar:/exports/csce/eddie/geos/groups/LURG/models/PLUM/classes -DBUILDVER=$buildver -DCONFIG_FILE=/exports/csce/eddie/geos/groups/LURG/models/PLUM/output/$1/config.properties ac.ed.lurg.ModelMain
 echo "finished"
 
+module load R
+R < /exports/csce/eddie/geos/groups/LURG/models/PLUM/plumv2/scripts/summariseLandUseOneSim.R  --no-save --args $1
+
+if [[ $* == *-NoP* ]]; then
+	echo "Not pruning outputs"
+else
+	echo "Pruning outputs, to save disk space"
+	/exports/csce/eddie/geos/groups/LURG/models/PLUM/plumv2/scripts/pruneOutputs.sh /exports/csce/eddie/geos/groups/LURG/models/PLUM/output/$1
+fi
+
diff --git a/scripts/summariseLandUseOneSim.R b/scripts/summariseLandUseOneSim.R
new file mode 100644
index 00000000..ed4c8298
--- /dev/null
+++ b/scripts/summariseLandUseOneSim.R
@@ -0,0 +1,71 @@
+require(data.table)
+
+cropIntensitySummary = function (luDt, cropType) {
+	
+	yDt = luDt[cropland > 0, 
+			list(	irrig = get(paste0(cropType, '_IQ')), 
+					fert = get(paste0(cropType, '_FQ')),
+					otherint = get(paste0(cropType, '_OI')),
+					thisCropFact = get(paste0(cropType, '_A')), 
+					allCropFact = cropland/area, 
+					area = get(paste0(cropType, '_A'))*cropland, 
+					yield = get(paste0(cropType, '_Y'))) ]
+	
+#	yDt[thisCropFact>0.05 & allCropFact > 0.03, list(cropType = cropType, yield=sum(area * yield)/sum(area), 
+	yDt[thisCropFact>0, list(cropType = cropType, yield=sum(area * yield)/sum(area), 
+					irrig=sum(area * irrig)/sum(area), 
+					fert=sum(area * fert)/sum(area),
+					otherint=sum(area * otherint)/sum(area),
+					area = sum(area)) ]
+}
+
+summariseLandUseOutput = function(simDir, writeFile=TRUE) {
+	
+	years = list.files(simDir, pattern="[0-9]+")
+	cropSumDt = NULL
+	
+	for (year in years) {
+		fileName = file.path(simDir, year, 'LandUse.txt')
+		if (file.exists(fileName)) {
+			print (paste("Processing", year, "from", fileName))
+			luDt = data.table(read.table(fileName, header=TRUE))
+		
+			for (cropType in c('maize', 'rice', 'wheat', 'energycrops', 'oilcrops', 'pulses', 'starchyRoots')) {
+				cropSummary = cropIntensitySummary(luDt, cropType)
+				if (nrow(cropSummary) >0)
+					cropSumDt = rbind(cropSumDt, cbind(cropSummary, year=year))
+			}
+		} else {
+			print(paste("Can't find file, so skipping:", fileName))
+		}
+	}
+
+	resDt = rbind(cropSumDt, cropSumDt[cropType != 'energycrops', list(cropType='avgExcEC', 
+					yield=sum(yield*area)/sum(area), 
+					irrig=sum(irrig*area)/sum(area),
+					fert=sum(fert*area)/sum(area),
+					otherint=sum(otherint*area)/sum(area),
+					area=sum(area)), by=year])
+
+	resDt = rbind(resDt, cropSumDt[, list(cropType='avgIncEC', 
+						yield=sum(yield*area)/sum(area), 
+						irrig=sum(irrig*area)/sum(area),
+						fert=sum(fert*area)/sum(area),
+						otherint=sum(otherint*area)/sum(area),
+						area=sum(area)), by=year])
+
+	if (writeFile) write.table(resDt, file.path(simDir, "LandUseSummary.csv"), sep=",", row.names=FALSE, quote=FALSE)
+	resDt
+}
+
+baseOutputDir="/exports/csce/eddie/geos/groups/LURG/models/PLUM/output" # "~/Downloads"
+
+ensemble = commandArgs(trailingOnly = TRUE)[1]
+ensDir=file.path(baseOutputDir, ensemble)
+if (dir.exists(ensDir)) {
+	print(paste(ensDir, "exists.  Processing"))
+	summariseLandUseOutput(simDir = ensDir)
+} else {
+	print(paste(ensDir, "does not exist.  Stopping"))
+}
+
-- 
GitLab