From eff442f55a38d56e48444f74ce7361fe6af619e4 Mon Sep 17 00:00:00 2001
From: Peter Alexander <peter@blackhillock.co.uk>
Date: Sat, 18 Jul 2015 16:59:27 +0100
Subject: [PATCH] Fix to problem getting values for crop not produced

---
 GAMS/IntExtOpt.gms                             |  4 ++--
 src/ac/ed/lurg/ModelConfig.java                |  2 +-
 .../country/gams/GamsLocationOptimiser.java    | 18 +++++++++++++++---
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms
index 96835027..a55d1d40 100644
--- a/GAMS/IntExtOpt.gms
+++ b/GAMS/IntExtOpt.gms
@@ -54,8 +54,8 @@ $gdxin
 * pasture yield is done in DM terms
    
  PARAMETER baseCost(crop)  cost per ha before intensity values just made up at the moment
-          /   wheat             0.5   
-              maize             0.5 
+          /   wheat             0.8   
+              maize             1.0 
               rice              1.0 
               tropicalCereals   0.5
               oilcrops          0.5 
diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java
index d035cda1..e0dc1948 100644
--- a/src/ac/ed/lurg/ModelConfig.java
+++ b/src/ac/ed/lurg/ModelConfig.java
@@ -106,7 +106,7 @@ public class ModelConfig {
 
 	
 	public static final int START_TIMESTEP = getIntProperty("START_TIMESTEP", 0);
-	public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 0);
+	public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 1);
 	public static final int BASE_YEAR = getIntProperty("BASE_YEAR", 2010);
 	
 	public static final double MAX_IMPORT_CHANGE = getDoubleProperty("MAX_IMPORT_CHANGE", 0.2);
diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
index ec4d7228..500f1f4a 100644
--- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
@@ -18,6 +18,7 @@ import ac.ed.lurg.utils.LogWriter;
 import ac.ed.lurg.yield.YieldResponsesItem;
 
 import com.gams.api.GAMSDatabase;
+import com.gams.api.GAMSException;
 import com.gams.api.GAMSGlobals;
 import com.gams.api.GAMSJob;
 import com.gams.api.GAMSOptions;
@@ -224,9 +225,9 @@ public class GamsLocationOptimiser {
 			if (!cropType.equals(prevCropType)) {
 				feedAmount = varFeedAmount.findRecord(itemName).getLevel();
 				netImport = cropType.isImportedCrop() ? varNetImports.findRecord(itemName).getLevel() : 0;
-				cropAdj = parmCropAdj.findRecord(itemName).getValue();
-				prod = parmProd.findRecord(itemName).getValue();
-				prodCost = parmProdCost.findRecord(itemName).getValue();
+				cropAdj = getParmValue(parmCropAdj, itemName);
+				prod =  getParmValue(parmProd, itemName);
+				prodCost = getParmValue(parmProdCost, itemName);
 				
 				cropUsageData.put(cropType, new CropUsageData(feedAmount, netImport, prod, prodCost));
 				cropAdjs.put(cropType, cropAdj);
@@ -267,6 +268,17 @@ public class GamsLocationOptimiser {
 		GamsLocationOutput results = new GamsLocationOutput(modelStatus, intensities, cropAreas, cropUsageData, cropAdjs);
 		return results;
 	}
+	
+	private double getParmValue(GAMSParameter aParm, String itemName) {
+		try {
+			double d = aParm.findRecord(itemName).getValue();
+			return d;
+		}
+		catch (GAMSException gamsEx) {
+			//LogWriter.println("GAMSException thrown for " + itemName);
+			return 0;
+		}
+	}
 
 	private void addScalar(GAMSParameter param, double val) {
 		param.addRecord().setValue(val);
-- 
GitLab