From dea8c4c71b2d19f08f4f535ef275a8c32142a3f3 Mon Sep 17 00:00:00 2001
From: R0slyn <roslyn.henry.08@aberdeen.ac.uk>
Date: Fri, 20 Oct 2017 15:36:07 +0100
Subject: [PATCH] Changes to output domestic net import amounts and cost in
 domestic production output file

---
 GAMS/IntExtOpt.gms                            |  3 ++-
 src/ac/ed/lurg/ModelMain.java                 | 20 +++++++++++++-----
 src/ac/ed/lurg/country/CountryAgent.java      | 21 +++++--------------
 .../country/gams/GamsLocationOptimiser.java   | 13 +++++++-----
 .../ed/lurg/demand/AbstractDemandManager.java |  5 +++--
 src/ac/ed/lurg/landuse/CropUsageData.java     |  8 ++++++-
 src/ac/ed/lurg/landuse/CropUsageReader.java   |  4 ++--
 7 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms
index 9a011219..fd5898ca 100644
--- a/GAMS/IntExtOpt.gms
+++ b/GAMS/IntExtOpt.gms
@@ -250,7 +250,8 @@ $gdxin
  
  netImportCost(import_crop) = importAmount.l(import_crop) * importPrices(import_crop) - exportAmount.l(import_crop) * exportPrices(import_crop);
  netImportAmount(import_crop) = importAmount.l(import_crop) - exportAmount.l(import_crop);
-         
+ 
+        
  Scalar ms 'model status', ss 'solve status'; 
  ms=LAND_USE.modelstat;
  ss=LAND_USE.solvestat;
diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java
index 05206b69..123f2c0f 100644
--- a/src/ac/ed/lurg/ModelMain.java
+++ b/src/ac/ed/lurg/ModelMain.java
@@ -329,27 +329,37 @@ public class ModelMain {
 	
 	private void writeDomesticProductionFile(Timestep timestep) {
 		try {
-			StringBuffer sbHeadings = new StringBuffer("Year, Country, Crop,Production cost, export price, import price");
+			StringBuffer sbHeadings = new StringBuffer("Year, Country, Crop, Production_cost, Export_price, Import_price, Net_import_cost, Net_imports");
 			BufferedWriter outputFile = getFileWriter(timestep, ModelConfig.DOMESTIC_OUTPUT_FILE,
 					sbHeadings.toString());
 
 			for (CropType crop : CropType.getAllItems()) {
 				for (CountryAgent country : countryAgents) {
-					LogWriter.println("Country " + country.getCountry().getName() + " crop " + crop);
+					
+					GamsRasterOutput previousCountryGamsRasterOutput = country.getGamsOutput();
+					Double prodCosts = previousCountryGamsRasterOutput.getCropUsageData().get(crop).getProdCost();
+					
 					Double importPrice;
 					Double exportPrice;
-					Double prodCosts = country.getDomesticProductionCosts().get(crop);
-
+					Double netImportCost;
+					Double netImports;
+					
 					if (crop.isImportedCrop()) {
 						importPrice = country.getCurrentCountryPrices().get(crop).getImportPrice();
 						exportPrice = country.getCurrentCountryPrices().get(crop).getExportPrice();
+						netImportCost = previousCountryGamsRasterOutput.getCropUsageData().get(crop).getNetImportCost();
+						netImports = previousCountryGamsRasterOutput.getCropUsageData().get(crop).getNetImports();
+						
 					} else {
 						importPrice = null;
 						exportPrice = null;
+						netImportCost = null;
+						netImports = null;
 					}
+						
 					StringBuffer sbData = new StringBuffer();
 					sbData.append(String.format("%d,%s, %s", timestep.getYear(), country.getCountry(),crop.getGamsName()));
-					sbData.append(String.format(",%.3f,%.3f,%.3f", prodCosts, importPrice, exportPrice));
+					sbData.append(String.format(",%.3f,%.3f,%.3f,%.3f,%.3f", prodCosts, importPrice, exportPrice, netImports, netImportCost));
 
 					outputFile.write(sbData.toString());
 					outputFile.newLine();
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 12292c04..5919ca81 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -41,7 +41,6 @@ public class CountryAgent {
 	private Map<CropType, CountryPrice> currentCountryPrices;
 	private Map<CropType, Double> tradeBarriers;
 	private RasterSet<IntegerRasterItem> yieldClusters;
-	private Map<CropType, Double> domesticProductionCosts;
 	
 	public CountryAgent(AbstractDemandManager demandManager,CompositeCountry country, RasterSet<LandUseItem> cropAreaRaster,
 			Map<CropType, CropUsageData> cropUsageData, Map<CropType, Double> tradeBarriers, RasterSet<IntegerRasterItem> yieldClusters) {
@@ -127,7 +126,7 @@ public class CountryAgent {
 			LogWriter.println("Running " + country.getName() + ", currentTimestep " + currentTimestep);
 			
 			GamsRasterOutput result = opti.run();
-			domesticProductionCosts = calculateDomesticProductionCosts(result);
+			//domesticProductionCosts = calculateDomesticProductionCosts(result);
 			
 			previousGamsRasterOutput = result;
 			
@@ -201,18 +200,8 @@ public class CountryAgent {
 		return countryPrices;
 	}
 	
-	public Map<CropType, Double> getDomesticProductionCosts() {
-		return domesticProductionCosts;
-	}
-	
-	private Map<CropType, Double> calculateDomesticProductionCosts(GamsRasterOutput result) {
-		
-		Map<CropType, Double> domesticProdCosts = new HashMap<CropType, Double>();
-		Map <CropType,  CropUsageData> cropUsage = result.getCropUsageData();
-		
-		for (Entry<CropType,  CropUsageData> entry : cropUsage.entrySet()) {
-			domesticProdCosts.put(entry.getKey(),entry.getValue().getProdCost());
-		}
-		return domesticProdCosts;
-	}
+	 public GamsRasterOutput getGamsOutput(){
+		 
+		 return previousGamsRasterOutput;
+	 }
 }
diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
index 515ee338..895d9a0b 100644
--- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
@@ -295,6 +295,7 @@ public class GamsLocationOptimiser {
 		GAMSVariable varRuminantFeed = outDB.getVariable("ruminantFeed");
 		GAMSVariable varMonogastricFeed = outDB.getVariable("monogastricFeed");
 		GAMSParameter parmNetImports = outDB.getParameter("netImportAmount");
+		GAMSParameter parmNetImportCost = outDB.getParameter("netImportCost");
 		GAMSVariable varYields = outDB.getVariable("yield");
 		GAMSVariable varUnitEnergies = outDB.getVariable("unitCost");
 		GAMSParameter parmProd = outDB.getParameter("totalProd");
@@ -304,7 +305,7 @@ public class GamsLocationOptimiser {
 
 		double totalCropArea = 0;
 		double totalPastureArea = 0;
-		double area, fertIntensity, irrigIntensity, otherIntensity = Double.NaN, ruminantFeed, monogastricFeed, netImport, yield, unitCost, prod, prodCost;
+		double area, fertIntensity, irrigIntensity, otherIntensity = Double.NaN, ruminantFeed, monogastricFeed, netImport, netImportCost, yield, unitCost, prod, prodCost;
 		
 		final LazyHashMap<Integer, LandUseItem> landUses = new LazyHashMap<Integer, LandUseItem>() { 
 			protected LandUseItem createValue() { return new LandUseItem(); }
@@ -332,12 +333,13 @@ public class GamsLocationOptimiser {
 				ruminantFeed = cropType.isFeedCrop() ? varRuminantFeed.findRecord(itemName).getLevel() : 0;
 				monogastricFeed = cropType.isFeedCrop() ? varMonogastricFeed.findRecord(itemName).getLevel() : 0;
 				netImport = cropType.isImportedCrop() ? getParmValue(parmNetImports, itemName) : 0;
+				netImportCost = cropType.isImportedCrop() ? getParmValue(parmNetImportCost, itemName) : 0;
 				prod =  getParmValue(parmProd, itemName);
 				prodCost = getParmValue(parmProdCost, itemName);
 				double totalArea = getParmValue(parmTotalArea, itemName);
 
-				cropUsageData.put(cropType, new CropUsageData(ruminantFeed, monogastricFeed, netImport, prod, prodCost));
-				if (DEBUG) LogWriter.println(String.format("\n%s:\tarea= %.1f,\tfeedAmount= %.1f,\tnetImports= %.3f,\tprod= %.3f,\tprodCost= %.3f,\tprodCostRate= %.3f", itemName, totalArea, ruminantFeed+monogastricFeed, netImport, prod, prodCost, prodCost/prod)); 
+				cropUsageData.put(cropType, new CropUsageData(ruminantFeed, monogastricFeed, netImport, netImportCost, prod, prodCost));
+				if (DEBUG) LogWriter.println(String.format("\n%s:\tarea= %.1f,\tfeedAmount= %.1f,\tnetImports= %.3f,\tnetImportCost= %.3f,\tprod= %.3f,\tprodCost= %.3f,\tprodCostRate= %.3f", itemName, totalArea, ruminantFeed+monogastricFeed, netImport, netImportCost, prod, prodCost, prodCost/prod)); 
 			}
 
 			LandUseItem landUseItem = landUses.lazyGet(locId);
@@ -363,11 +365,12 @@ public class GamsLocationOptimiser {
 
 		for (CropType meatTypes : CropType.getMeatTypes()) {
 			netImport = getParmValue(parmNetImports, meatTypes.getGamsName());
+			netImportCost= getParmValue(parmNetImportCost, meatTypes.getGamsName());
 			prod = getParmValue(parmProd, meatTypes.getGamsName());
 			prodCost = getParmValue(parmProdCost, meatTypes.getGamsName()); // currently not calculated in GAMS correctly, not sure it's needed
 			
-			cropUsageData.put(meatTypes, new CropUsageData(0.0, 0.0, netImport, prod, prodCost));
-			if (DEBUG) LogWriter.println(String.format("\n%s:\t\t\t\t\tnetImports= %.3f,\tprod= %.3f,\tprodCost= %.3f", meatTypes.getGamsName(), netImport, prod, prodCost)); 
+			cropUsageData.put(meatTypes, new CropUsageData(0.0, 0.0, netImport, netImportCost, prod, prodCost));
+			if (DEBUG) LogWriter.println(String.format("\n%s:\t\t\t\t\tnetImports= %.3f,tnetImportCost= %.3f,\tprod= %.3f,\tprodCost= %.3f", meatTypes.getGamsName(), netImport, netImportCost, prod, prodCost)); 
 		}
 		LogWriter.println(String.format("\n%s %s: Total area= %.1f (crop=%.1f, pasture %.1f)", 
 				inputData.getCountryInput().getCountry(), inputData.getTimestep().getYear(), 
diff --git a/src/ac/ed/lurg/demand/AbstractDemandManager.java b/src/ac/ed/lurg/demand/AbstractDemandManager.java
index 4efe551d..7d5de330 100644
--- a/src/ac/ed/lurg/demand/AbstractDemandManager.java
+++ b/src/ac/ed/lurg/demand/AbstractDemandManager.java
@@ -106,8 +106,9 @@ public abstract class AbstractDemandManager {
 
 		}
 
-//		LogWriter.println("original map " + originalFoodDemandMap);
-//		LogWriter.println("updated map " + updatedFoodDemandMap);
+		//LogWriter.println("demand adj " + dietaryDemandAdj + " year " + year + " years of change " + yearsOfChange );
+		//LogWriter.println("original map " + originalFoodDemandMap);
+		//LogWriter.println("updated map " + updatedFoodDemandMap);
 		return updatedFoodDemandMap;
 	}
 
diff --git a/src/ac/ed/lurg/landuse/CropUsageData.java b/src/ac/ed/lurg/landuse/CropUsageData.java
index 279804d1..f9f3a449 100644
--- a/src/ac/ed/lurg/landuse/CropUsageData.java
+++ b/src/ac/ed/lurg/landuse/CropUsageData.java
@@ -5,14 +5,16 @@ public class CropUsageData {
 	private double ruminantFeed;
 	private double monogastricFeed;
 	private double netImports;
+	private double netImportCost;
 	private double prod;
 	private double prodCost;
 
-	public CropUsageData(double ruminantFeed, double monogastricFeed, double netImports, double prod, double prodCost) {
+	public CropUsageData(double ruminantFeed, double monogastricFeed, double netImports, double netImportCost, double prod, double prodCost) {
 		super();
 		this.ruminantFeed = ruminantFeed;
 		this.monogastricFeed = monogastricFeed;
 		this.netImports = netImports;
+		this.netImportCost=netImportCost;
 		this.prod = prod;
 		this.prodCost = prodCost;
 	}
@@ -28,6 +30,10 @@ public class CropUsageData {
 	public double getNetImports() {
 		return netImports;
 	}
+
+	public double getNetImportCost() {
+		return netImportCost;
+	}
 	
 	public double getProduction() {
 		return prod;
diff --git a/src/ac/ed/lurg/landuse/CropUsageReader.java b/src/ac/ed/lurg/landuse/CropUsageReader.java
index f5ec7b4b..2895f512 100644
--- a/src/ac/ed/lurg/landuse/CropUsageReader.java
+++ b/src/ac/ed/lurg/landuse/CropUsageReader.java
@@ -32,7 +32,7 @@ public class CropUsageReader {
 		LazyHashMap<CompositeCountry, Map<CropType, CropUsageData>> commodityMap = new LazyHashMap<CompositeCountry, Map<CropType, CropUsageData>>() {
 			protected Map<CropType, CropUsageData> createValue() { 
 				Map<CropType, CropUsageData> countryData = new HashMap<CropType, CropUsageData>();
-				countryData.put(CropType.ENERGY_CROPS, new CropUsageData(Double.NaN, Double.NaN, 0.0, Double.NaN, Double.NaN)); // 2nd generation energy crops not in FAO data, so adding zero values.
+				countryData.put(CropType.ENERGY_CROPS, new CropUsageData(Double.NaN, Double.NaN, 0.0, Double.NaN,Double.NaN, Double.NaN)); // 2nd generation energy crops not in FAO data, so adding zero values.
 				return countryData;
 			}
 		};
@@ -67,7 +67,7 @@ public class CropUsageReader {
 					netImports += oldData.getNetImports();
 				}
 				
-				CropUsageData newData = new CropUsageData(Double.NaN, Double.NaN, netImports, Double.NaN, Double.NaN);
+				CropUsageData newData = new CropUsageData(Double.NaN, Double.NaN, netImports, Double.NaN,Double.NaN, Double.NaN);
 				countryData.put(crop, newData);
 			} 
 			fitReader.close(); 
-- 
GitLab