From ba010c010bd99fbcfc01dd4d082415aa43aa64e5 Mon Sep 17 00:00:00 2001
From: Peter Alexander <peter@blackhillock.co.uk>
Date: Mon, 6 Feb 2017 15:51:13 +0000
Subject: [PATCH] Change calibration factors, and add scripts for starting
 scenarios

---
 scripts/common.properties                |  8 ++++
 scripts/createScenarios.sh               | 48 ++++++++++++++++++++++++
 scripts/scheduleScenarios.sh             | 32 ++++++++++++++++
 scripts/testScenarios                    |  6 +++
 src/ac/ed/lurg/ModelConfig.java          | 11 +++---
 src/ac/ed/lurg/demand/DemandManager.java |  2 +-
 6 files changed, 101 insertions(+), 6 deletions(-)
 create mode 100644 scripts/common.properties
 create mode 100755 scripts/createScenarios.sh
 create mode 100755 scripts/scheduleScenarios.sh
 create mode 100644 scripts/testScenarios

diff --git a/scripts/common.properties b/scripts/common.properties
new file mode 100644
index 00000000..b772e6ba
--- /dev/null
+++ b/scripts/common.properties
@@ -0,0 +1,8 @@
+DATA_DIR=<somewhere common for the data>
+GAMS_MODEL=<where the gams code file is located>
+
+SPATIAL_DIR_NAME=halfdeg
+CELL_SIZE_X=0.5
+
+YIELD_DIR=<might actually need to be in scenario specific one, or change to have a base plus RCP specific dir or something>LPJG_PLUM_expt1.2_rcp26_forED
+
diff --git a/scripts/createScenarios.sh b/scripts/createScenarios.sh
new file mode 100755
index 00000000..3abe563f
--- /dev/null
+++ b/scripts/createScenarios.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+filename="$1"
+common_prop_file=./common.properties
+output_dir=./scenarios/
+
+if [[ $* == *-O* ]]; then overwrite=1
+else overwrite=0
+fi
+
+if [ "$#" -lt 1  ]; then
+   echo "Need to specify scenario table file"
+   exit
+fi
+
+echo "Generating scenarios into $output_dir"
+
+while read -r datarow $filename
+do
+  IFS=',' read -r -a array <<< "$datarow"
+
+  if [ -z "$header" ]; then
+     header=( "${array[@]}" )
+  else
+     scenario="${array[0]}"
+     scenario_dir=$output_dir$scenario
+     echo $scenario
+
+     if [ ! -d "$scenario_dir" ]; then
+        mkdir $scenario_dir
+     elif [ $overwrite -ne 1 ]; then
+        echo "Already have scenario $scenario, set -O to overwrite existing scenarios"
+        continue;
+     fi
+
+     config_file=$scenario_dir/config.properties
+     cp $common_prop_file $config_file
+     echo "BASE_DIR=$scenario_dir" >> $config_file
+
+     for index in "${!array[@]}"
+     do
+        echo "${header[index]}"="${array[index]}" >> $config_file
+     done
+
+     echo "$scenario generated"
+  fi
+done < $filename
+
diff --git a/scripts/scheduleScenarios.sh b/scripts/scheduleScenarios.sh
new file mode 100755
index 00000000..50a38fc3
--- /dev/null
+++ b/scripts/scheduleScenarios.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+if [ "$#" -lt 1  ]; then
+   echo "Need to specify scenario table file"
+   exit
+fi
+
+
+filename="$1"
+output_dir=/Users/peteralexander/Documents/R_Workspace/UNPLUM/dummy/scenarios/
+echo "Starting scenarios in $output_dir"
+
+while read -r datarow $filename
+do
+  IFS=',' read -r -a array <<< "$datarow"
+
+  if [ -z "$header" ]; then
+     header=( "${array[@]}" )
+  else
+     scenario="${array[0]}"
+     scenario_dir=$output_dir$scenario
+     echo $scenario
+
+     if [ ! -d "$scenario_dir" ]; then
+        echo "Scenario $scenario dir is missing, so skipping"
+        continue;
+     fi
+
+     echo "qsub runPlum.sh scenario_dir"
+  fi
+done < $filename
+
diff --git a/scripts/testScenarios b/scripts/testScenarios
new file mode 100644
index 00000000..8541a71b
--- /dev/null
+++ b/scripts/testScenarios
@@ -0,0 +1,6 @@
+scenario,SSP_SCENARIO,MAX_IMPORT_CHANGE,TECHNOLOGY_CHANGE_ANNUAL_RATE,FRED
+s1,SSP2_v9_130325,0.3,0.01,F1
+s2,SSP1_v9_130325,0.5,0.04,F2
+s3,SSP1_v9_130325,0.5,0.04,F3
+s4,SSP1_v9_130325,0.5,0.04,F3
+s5,SSP1_v9_130325,0.5,0.04,F3
diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java
index ee01ad61..3e6d352d 100644
--- a/src/ac/ed/lurg/ModelConfig.java
+++ b/src/ac/ed/lurg/ModelConfig.java
@@ -106,12 +106,12 @@ public class ModelConfig {
 	public static final String YIELD_FILENAME = getProperty("YIELD_FILENAME", "yield.out");
 	public static final boolean PASTURE_FERT_RESPONSE_FROM_LPJ = getBooleanProperty("PASTURE_FERT_RESPONSE_FROM_LPJ", true);;
 
-	public static final double CALIB_FACTOR_WHEAT = getDoubleProperty("CALIB_FACTOR_WHEAT", 0.966);
-	public static final double CALIB_FACTOR_MAIZE = getDoubleProperty("CALIB_FACTOR_MAIZE", 0.631);
-	public static final double CALIB_FACTOR_RICE = getDoubleProperty("CALIB_FACTOR_RICE", 1.232);
+	public static final double CALIB_FACTOR_WHEAT = getDoubleProperty("CALIB_FACTOR_WHEAT", 0.976);
+	public static final double CALIB_FACTOR_MAIZE = getDoubleProperty("CALIB_FACTOR_MAIZE", 0.667);
+	public static final double CALIB_FACTOR_RICE = getDoubleProperty("CALIB_FACTOR_RICE", 1.247);
 	public static final double CALIB_FACTOR_OILCROPS = getDoubleProperty("CALIB_FACTOR_OILCROPS", 0.192);
 	public static final double CALIB_FACTOR_PULSES = getDoubleProperty("CALIB_FACTOR_PULSES", 0.583);
-	public static final double CALIB_FACTOR_STARCHY_ROOTS = getDoubleProperty("CALIB_FACTOR_STARCHY_ROOTS", 6.027);
+	public static final double CALIB_FACTOR_STARCHY_ROOTS = getDoubleProperty("CALIB_FACTOR_STARCHY_ROOTS", 6.067);
 	
 	// Spatial (gridded) data
 	public static final double CELL_SIZE_X = getDoubleProperty("CELL_SIZE_X", 0.5);
@@ -167,8 +167,9 @@ public class ModelConfig {
 	public static final double TECHNOLOGY_CHANGE_ANNUAL_RATE = IS_CALIBRATION_RUN ? 0.0 : getDoubleProperty("TECHNOLOGY_CHANGE_ANNUAL_RATE", 0.0);
 	public static final int TECHNOLOGY_CHANGE_START_STEP = getIntProperty("TECHNOLOGY_CHANGE_START_STEP", 0);
 	
-	public static final double BIOENERGY_CHANGE_ANNUAL_RATE = IS_CALIBRATION_RUN ? 0.0 : getDoubleProperty("BIOENERGY_CHANGE_ANNUAL_RATE", 0.0);
+	public static final double BIOENERGY_CHANGE_ANNUAL_RATE = IS_CALIBRATION_RUN ? 0.0 : getDoubleProperty("BIOENERGY_CHANGE_ANNUAL_RATE", 0.0); // 6.2/2.31/20
 	public static final int BIOENERGY_CHANGE_START_YEAR = getIntProperty("BIOENERGY_CHANGE_START_YEAR", 2010);
+	public static final int BIOENERGY_CHANGE_END_YEAR = getIntProperty("BIOENERGY_CHANGE_END_YEAR", 2030);
 
 	public static final double MARKET_LAMBA = getDoubleProperty("MARKET_LAMBA", 0.3); // controls international market price adjustment rate
 
diff --git a/src/ac/ed/lurg/demand/DemandManager.java b/src/ac/ed/lurg/demand/DemandManager.java
index d3c15d30..88a52b82 100644
--- a/src/ac/ed/lurg/demand/DemandManager.java
+++ b/src/ac/ed/lurg/demand/DemandManager.java
@@ -76,7 +76,7 @@ public class DemandManager {
 		if (bioenergyBaseDemand != null && bioenergyBaseDemand.containsKey(country)) {
 			Double d = bioenergyBaseDemand.get(country).get(commodity);
 			
-			int yearsOfChange = year - ModelConfig.BIOENERGY_CHANGE_START_YEAR;
+			int yearsOfChange = Math.min(ModelConfig.BIOENERGY_CHANGE_END_YEAR - ModelConfig.BIOENERGY_CHANGE_START_YEAR, year - ModelConfig.BIOENERGY_CHANGE_START_YEAR);
 			double bioenergyDemandAdj = yearsOfChange > 0 ?  (1.0 +  yearsOfChange * ModelConfig.BIOENERGY_CHANGE_ANNUAL_RATE) : 1.0 ;
 
 			return d == null ? 0 : d.doubleValue() * bioenergyDemandAdj;
-- 
GitLab