From cd8857e73295868fd3794b476424a3dc90c87347 Mon Sep 17 00:00:00 2001 From: Peter Alexander <peter@blackhillock.co.uk> Date: Wed, 8 Jul 2015 09:24:57 +0100 Subject: [PATCH] Multiple crops map to cereal demand --- GAMS/IntExtOpt.gms | 42 +++++---- src/ac/ed/lurg/ModelMain.java | 6 +- src/ac/ed/lurg/country/CountryAgent.java | 28 +++--- .../lurg/country/gams/GamsCountryInput.java | 7 +- .../country/gams/GamsLocationOptimiser.java | 21 +++-- .../lurg/country/gams/GamsLocationOutput.java | 12 +-- .../lurg/country/gams/GamsLocationTest.java | 86 ++++++++++--------- .../country/gams/GamsRasterOptimiser.java | 18 ++-- .../lurg/country/gams/GamsRasterOutput.java | 16 ++-- src/ac/ed/lurg/demand/BaseConsumpManager.java | 33 ++++--- src/ac/ed/lurg/demand/DemandCurve.java | 20 ++--- src/ac/ed/lurg/demand/DemandCurveManager.java | 7 +- src/ac/ed/lurg/demand/DemandManager.java | 14 ++- ...{CommodityData.java => CropUsageData.java} | 17 ++-- src/ac/ed/lurg/types/CommodityType.java | 51 +++++++++++ src/ac/ed/lurg/types/CountryCropKey.java | 46 ---------- src/ac/ed/lurg/types/CropType.java | 21 +++-- src/ac/sac/raster/RasterSetTest.java | 1 - 18 files changed, 241 insertions(+), 205 deletions(-) rename src/ac/ed/lurg/landuse/{CommodityData.java => CropUsageData.java} (79%) create mode 100644 src/ac/ed/lurg/types/CommodityType.java delete mode 100644 src/ac/ed/lurg/types/CountryCropKey.java diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms index 42988c1a..3aa6f79b 100644 --- a/GAMS/IntExtOpt.gms +++ b/GAMS/IntExtOpt.gms @@ -1,9 +1,12 @@ + SET all_types / cereals, wheat, maize, rice, tropicalCereals, oilcrops, soybean, pulses, starchyRoots, pasture_or_meat /; + + SET crop(all_types) / wheat, maize, rice, tropicalCereals, oilcrops, soybean, pulses, starchyRoots, pasture_or_meat /; + SET crop_less_pasture(crop) / wheat, maize, rice, tropicalCereals, oilcrops, soybean, pulses, starchyRoots /; + SET cereal_crop(crop) / wheat, maize, rice, tropicalCereals /; + SET non_cereal_crop(crop) / oilcrops, soybean, pulses, starchyRoots /; + SET feed_crop(crop) / wheat, maize, oilcrops, soybean /; + SET not_feed_crops(crop) / rice, tropicalCereals, pulses, starchyRoots, pasture_or_meat /; - SET crop crop types including pasture / cereals, fruits, oilcrops, starchyRoots, treenuts, vegetables, pulses, pasture_or_meat /; - SET crop_less_pasture(crop) arable crops types includes treenuts but not pasture / cereals, fruits, oilcrops, starchyRoots, treenuts, vegetables, pulses /; - SET feed_crop(crop) / cereals, oilcrops /; - SET not_feed_crops(crop) / fruits, starchyRoots, treenuts, vegetables, pulses, pasture_or_meat /; - SET location; PARAMETER suitableLandArea(location) areas of land in Mha; PARAMETER previousArea(crop, location) areas for previous timestep in Mha; @@ -13,7 +16,7 @@ PARAMETER yieldBoth(crop, location) yield in t per ha; PARAMETER fertParam(crop, location) yield response to fertilizer parameter; PARAMETER irrigParam(crop, location) yield response to irrigation parameter; - PARAMETER demand(crop) in t; + PARAMETER demand(all_types) in t; PARAMETER worldInputEnergy(crop) average input energy from world exports used to determine if we should import or export; PARAMETER maxNetImport(crop) maximum net import for each crop based on world market; PARAMETER minNetImport(crop) minimum net import for each crop based on world market; @@ -34,16 +37,13 @@ $gdxin SCALAR delta use to smooth power function see 7.5 www.gams.com dd docs solversconopt.pdf / 0.00000000001 / display previousArea - - PARAMETER feedEnergy(crop) energy from feed in MJ per t - / cereals 13.7 - oilcrops 18.6 - pasture_or_meat 18.1 / ; - - PARAMETER feedDM(crop) energy from feed in MJ per t - / cereals 0.87 + + PARAMETER feedDM(crop) energy from feed in MJ per t (wet) (need to check these figures) + / wheat 0.87 + maize 0.87 oilcrops 0.32 - pasture_or_meat 1.0 / ; + soybean 0.87 + pasture_or_meat 1.0 / ; VARIABLES area(crop, location) total area for each crop - Mha @@ -66,8 +66,9 @@ $gdxin EQUATIONS UNIT_ENERGY_EQ(crop, location) energy per area YIELD_EQ(crop, location) yield given chosen intensity - CROP_DEMAND_CONSTRAINT(crop_less_pasture) satisfy demand for crop - MEAT_DEMAND_CONSTRAINT satisfy demand for crop + NON_CEREAL_DEMAND_CONSTRAINT(non_cereal_crop) satisfy demand for non-cereal crop + CEREAL_DEMAND_CONSTRAINT satisfy demand for cereal + MEAT_DEMAND_CONSTRAINT satisfy demand for meat MAX_FERT_INTENSITY_CONSTRAINT(crop, location) constraint on maximum fertilizer intensity MAX_IRRIG_INTENSITY_CONSTRAINT(crop, location) constraint on maximum irrigation intensity TOTAL_LAND_CHANGE_CONSTRAINT(location) constraint on the rate of land use change @@ -100,8 +101,11 @@ $gdxin * (yieldBoth(crop, location) + yieldNone(crop, location) - yieldFertOnly(crop, location) - yieldIrrigOnly(crop, location)) * (1 - exp(-fertParam(crop, location)*fertI(crop, location))) * (1 - exp(-irrigParam(crop, location)*irrigI(crop, location))) * ) * otherIntensity(crop, location); - CROP_DEMAND_CONSTRAINT(crop_less_pasture) .. sum(location, area(crop_less_pasture, location) * yield(crop_less_pasture, location)) - feedAmount(crop_less_pasture) =G= - demand(crop_less_pasture) - netImportAmount(crop_less_pasture); + NON_CEREAL_DEMAND_CONSTRAINT(non_cereal_crop) .. sum(location, area(non_cereal_crop, location) * yield(non_cereal_crop, location)) - feedAmount(non_cereal_crop) =G= + demand(non_cereal_crop) - netImportAmount(non_cereal_crop); + + CEREAL_DEMAND_CONSTRAINT .. sum(cereal_crop, sum(location, area(cereal_crop, location) * yield(cereal_crop, location)) - feedAmount(cereal_crop)) =G= + demand('cereals') - sum(cereal_crop, netImportAmount(cereal_crop)); MEAT_DEMAND_CONSTRAINT .. meatEfficency*sum(feed_crop, feedDM(feed_crop) * feedAmount(feed_crop)) + feedDM('pasture_or_meat') * sum(location, area('pasture_or_meat', location) * yield('pasture_or_meat', location)) =G= demand('pasture_or_meat') - netImportAmount('pasture_or_meat'); diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java index c937c928..49ab8c57 100644 --- a/src/ac/ed/lurg/ModelMain.java +++ b/src/ac/ed/lurg/ModelMain.java @@ -16,7 +16,7 @@ import ac.ed.lurg.country.CountryBoundaryReader; import ac.ed.lurg.country.gams.GamsRasterOutput; import ac.ed.lurg.demand.DemandManager; import ac.ed.lurg.landuse.AreasItem; -import ac.ed.lurg.landuse.CommodityData; +import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IntensitiesItem; import ac.ed.lurg.landuse.Intensity; import ac.ed.lurg.landuse.IrrigationCostItem; @@ -161,7 +161,7 @@ public class ModelMain { RasterSet<LandCoverItem> initLC = getInitialLandCover(); RasterSet<IrrigationCostItem> allIrrigationCosts = getIrrigationCosts(); - Map<Country, Map<CropType, CommodityData>> commodityDataMap = CommodityData.readCommodityData(); + Map<Country, Map<CropType, CropUsageData>> cropUsageDataMap = CropUsageData.readCommodityData(); HashSet<String> countryExclusionList = new HashSet<String>(Arrays.asList("French Polynesia", "Cabo Verde", "Samoa", "Saint Vincent and the Grenadines")); @@ -184,7 +184,7 @@ public class ModelMain { RasterSet<LandCoverItem> initCountryLC = initLC.popSubsetForKeys(new RasterSet<LandCoverItem>(), keys); RasterSet<IrrigationCostItem> irrigationCosts = allIrrigationCosts.popSubsetForKeys(new RasterSet<IrrigationCostItem>(), keys); - Map<CropType, CommodityData> countryCommodityData = commodityDataMap.get(country); + Map<CropType, CropUsageData> countryCommodityData = cropUsageDataMap.get(country); if (countryCommodityData == null) { LogWriter.printlnError("No commodities data for " +country + ", so skipping"); diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index e4a823c7..d90227cd 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -10,9 +10,10 @@ import ac.ed.lurg.country.gams.GamsRasterOptimiser; import ac.ed.lurg.country.gams.GamsRasterOutput; import ac.ed.lurg.demand.DemandManager; import ac.ed.lurg.landuse.AreasItem; -import ac.ed.lurg.landuse.CommodityData; +import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IrrigationCostItem; import ac.ed.lurg.landuse.LandCoverItem; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.LandDataType; import ac.ed.lurg.utils.LogWriter; @@ -28,19 +29,18 @@ public class CountryAgent { private Map<Integer, GamsRasterOutput> resultsTimeseries = new HashMap<Integer, GamsRasterOutput>(); private int currentTimestep; - private Map<CropType, Double> currentProjectedDemand; private YieldRaster countryYieldSurfaces; private RasterSet<IrrigationCostItem> irrigationCostRaster; public CountryAgent(DemandManager demandManager, Country country, RasterSet<LandCoverItem> initialLC, - RasterSet<IrrigationCostItem> irrigationCostRaster, Map<CropType, CommodityData> commoditiesData) { + RasterSet<IrrigationCostItem> irrigationCostRaster, Map<CropType, CropUsageData> cropUsageData) { this.demandManager = demandManager; this.country = country; RasterSet<AreasItem> cropAreaRaster = convertInitialLC(initialLC); this.irrigationCostRaster = irrigationCostRaster; - GamsRasterOutput initialData = new GamsRasterOutput(cropAreaRaster, commoditiesData); + GamsRasterOutput initialData = new GamsRasterOutput(cropAreaRaster, cropUsageData); resultsTimeseries.put(0, initialData); } @@ -55,7 +55,7 @@ public class CountryAgent { LandCoverItem landCover = entry.getValue(); if (landCover != null) { - areasItem.setCropArea(CropType.CEREALS, landCover.getLandCover(LandDataType.CROPLAND)); // we allow free substitution between crop types so this is ok-ish + areasItem.setCropArea(CropType.WHEAT, landCover.getLandCover(LandDataType.CROPLAND)); // we allow free substitution between crop types so this is ok-ish areasItem.setCropArea(CropType.MEAT_OR_PASTURE, landCover.getLandCover(LandDataType.PASTURE)); areasItem.setLandCoverArea(LandDataType.FOREST, landCover.getLandCover(LandDataType.FOREST)); areasItem.setLandCoverArea(LandDataType.OTHER_NATURAL, landCover.getLandCover(LandDataType.OTHER_NATURAL)); @@ -76,9 +76,9 @@ public class CountryAgent { // get projected demand int year = ModelConfig.BASE_YEAR + currentTimestep; - currentProjectedDemand = demandManager.getDemand(country, year); + Map<CommodityType, Double> projectedDemand = demandManager.getDemand(country, year); - if (currentProjectedDemand.size() == 0) { + if (projectedDemand.size() == 0) { LogWriter.printlnError("No demand for country " + country + " so skipping it"); } else if (countryYieldSurfaces.size() == 0 ) { @@ -86,7 +86,7 @@ public class CountryAgent { } else { // optimize areas and intensity - GamsRasterInput input = getGamsRasterInput(worldInputEnergy); + GamsRasterInput input = getGamsRasterInput(projectedDemand, worldInputEnergy); GamsRasterOptimiser opti = new GamsRasterOptimiser(input); LogWriter.println("Running " + country.getCountryName() + ", currentTimestep " + currentTimestep); @@ -98,16 +98,12 @@ public class CountryAgent { return null; } - private Map<CropType, Double> getProjectedDemand() { - return currentProjectedDemand; - } - - public GamsRasterInput getGamsRasterInput(Map<CropType, Double> worldInputEnergy) { + private GamsRasterInput getGamsRasterInput(Map<CommodityType, Double> projectedDemand, Map<CropType, Double> worldInputEnergy) { int previousTimestep = (ModelConfig.START_TIMESTEP == currentTimestep) ? currentTimestep : currentTimestep - 1; GamsRasterOutput prevOutput = resultsTimeseries.get(previousTimestep); - Map<CropType, CommodityData> prevCommoditiesData = prevOutput.getCommoditiesData(); + Map<CropType, CropUsageData> prevCropUsageData = prevOutput.getCropUsageData(); double allowedImportChange; if (currentTimestep == ModelConfig.START_TIMESTEP) @@ -117,14 +113,14 @@ public class CountryAgent { Map<CropType, Double> maxNetImport = new HashMap<CropType, Double>(); Map<CropType, Double> minNetImport = new HashMap<CropType, Double>(); - for (Map.Entry<CropType, CommodityData> entry : prevCommoditiesData.entrySet()) { + for (Map.Entry<CropType, CropUsageData> entry : prevCropUsageData.entrySet()) { double largerOfProductionOrSupply; // this is what might be better to use than the netImport amount maxNetImport.put(entry.getKey(), entry.getValue().getNetImports() * (1 + allowedImportChange)); minNetImport.put(entry.getKey(), entry.getValue().getNetImports() * (1 - allowedImportChange)); } - GamsCountryInput countryLevelInputs = new GamsCountryInput(country, getProjectedDemand(), worldInputEnergy, maxNetImport, maxNetImport); + GamsCountryInput countryLevelInputs = new GamsCountryInput(country, projectedDemand, worldInputEnergy, maxNetImport, maxNetImport); GamsRasterInput input = new GamsRasterInput(countryYieldSurfaces, prevOutput.getCropAreaRaster(), irrigationCostRaster, countryLevelInputs); return input; diff --git a/src/ac/ed/lurg/country/gams/GamsCountryInput.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java index 1de8b4ba..c5a2f321 100644 --- a/src/ac/ed/lurg/country/gams/GamsCountryInput.java +++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java @@ -3,12 +3,13 @@ package ac.ed.lurg.country.gams; import java.util.Map; import ac.ed.lurg.country.Country; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.CropType; public class GamsCountryInput { private Country country; // not really required but useful for debugging - private Map<CropType, Double> projectedDemand; + private Map<CommodityType, Double> projectedDemand; private Map<CropType, Double> worldInputEnergy; private Map<CropType, Double> maxNetImport; private Map<CropType, Double> minNetImport; @@ -22,7 +23,7 @@ public class GamsCountryInput { private double landChangeEnergy;*/ - public GamsCountryInput(Country country, Map<CropType, Double> projectedDemand, Map<CropType, Double> worldInputEnergy, + public GamsCountryInput(Country country, Map<CommodityType, Double> projectedDemand, Map<CropType, Double> worldInputEnergy, Map<CropType, Double> maxNetImport, Map<CropType, Double> minNetImport) { super(); this.country = country; @@ -36,7 +37,7 @@ public class GamsCountryInput { return country; } - public Map<CropType, Double> getProjectedDemand() { + public Map<CommodityType, Double> getProjectedDemand() { return projectedDemand; } diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java index 9c2ea6f8..39a3bf30 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java @@ -8,10 +8,11 @@ import java.util.Vector; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.landuse.AreasItem; -import ac.ed.lurg.landuse.CommodityData; +import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IntensitiesItem; import ac.ed.lurg.landuse.Intensity; import ac.ed.lurg.landuse.IrrigationCostItem; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.CropType; import ac.ed.lurg.utils.LogWriter; import ac.ed.lurg.yield.YieldResponsesItem; @@ -112,7 +113,7 @@ public class GamsLocationOptimiser { if (DEBUG) LogWriter.println("\nDemand"); GamsCountryInput countryInput = inputData.getCountryInput(); - addItemMapParm(inDB.addParameter("demand", 1), countryInput.getProjectedDemand()); + addCommodityMapParm(inDB.addParameter("demand", 1), countryInput.getProjectedDemand()); if (DEBUG) LogWriter.println("\nYield (fert/irrig) None/None, Max/None, None/Max, Max/Max, [fert p], [irrig p]"); GAMSParameter yNoneP = inDB.addParameter("yieldNone", 2); @@ -186,7 +187,7 @@ public class GamsLocationOptimiser { Map<Integer, IntensitiesItem> intensities = new HashMap<Integer, IntensitiesItem>(); Map<Integer, AreasItem> cropAreas = new HashMap<Integer, AreasItem>(); - Map<CropType, CommodityData> commoditiesData = new HashMap<CropType, CommodityData>(); + Map<CropType, CropUsageData> cropUsageData = new HashMap<CropType, CropUsageData>(); CropType prevCropType = null; for (GAMSVariableRecord rec : varAreas) { @@ -206,7 +207,7 @@ public class GamsLocationOptimiser { feedAmount = varFeedAmount.findRecord(itemName).getLevel(); netImport = varNetImports.findRecord(itemName).getLevel(); - commoditiesData.put(cropType, new CommodityData(feedAmount, netImport)); + cropUsageData.put(cropType, new CropUsageData(feedAmount, netImport)); if (DEBUG) LogWriter.println(String.format("\n%s:\tfeedAmount= %.1f,\tnetImports= %.3f", itemName, feedAmount, netImport)); } @@ -233,7 +234,7 @@ public class GamsLocationOptimiser { } if (DEBUG) LogWriter.println(String.format("\nTotal area= %.1f", totalArea)); - GamsLocationOutput results = new GamsLocationOutput(modelStatus, intensities, cropAreas, commoditiesData); + GamsLocationOutput results = new GamsLocationOutput(modelStatus, intensities, cropAreas, cropUsageData); return results ; } @@ -249,6 +250,16 @@ public class GamsLocationOptimiser { parm.addRecord(entry.getKey().getGamsName()).setValue(d); } } + + private void addCommodityMapParm(GAMSParameter parm, Map<CommodityType, Double> itemMap) { + for (Map.Entry<CommodityType, Double> entry : itemMap.entrySet()) { + double d = entry.getValue(); + if (DEBUG) LogWriter.println(String.format(" %15s,\t %.1f", entry.getKey().getFaoName(), d)); + if (!Double.isNaN(d)) + parm.addRecord(entry.getKey().getFaoName()).setValue(d); + } + } + private void cleanup(String directory) { File directoryToDelete = new File(directory); diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOutput.java b/src/ac/ed/lurg/country/gams/GamsLocationOutput.java index bbb55eb0..1a91e915 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationOutput.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationOutput.java @@ -3,7 +3,7 @@ package ac.ed.lurg.country.gams; import java.util.Map; import ac.ed.lurg.landuse.AreasItem; -import ac.ed.lurg.landuse.CommodityData; +import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IntensitiesItem; import ac.ed.lurg.types.CropType; @@ -12,17 +12,17 @@ public class GamsLocationOutput { Map<Integer, IntensitiesItem> intensities; // data mapped from id (not raster) Map<Integer, AreasItem> cropAreas; - private Map<CropType, CommodityData> commoditiesData; + private Map<CropType, CropUsageData> cropUsageData; public GamsLocationOutput(int status, Map<Integer, IntensitiesItem> intensities, Map<Integer, AreasItem> cropAreas, - Map<CropType, CommodityData> commoditiesData) { + Map<CropType, CropUsageData> cropUsageData) { super(); this.status = status; this.intensities = intensities; this.cropAreas = cropAreas; - this.commoditiesData = commoditiesData; + this.cropUsageData = cropUsageData; } public int getStatus() { @@ -34,7 +34,7 @@ public class GamsLocationOutput { public Map<Integer, AreasItem> getCropAreas() { return cropAreas; } - public Map<CropType, CommodityData> getCommoditiesData() { - return commoditiesData; + public Map<CropType, CropUsageData> getCommoditiesData() { + return cropUsageData; } } diff --git a/src/ac/ed/lurg/country/gams/GamsLocationTest.java b/src/ac/ed/lurg/country/gams/GamsLocationTest.java index b91ef301..18d5dc88 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationTest.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationTest.java @@ -6,13 +6,14 @@ import java.util.Map; import ac.ed.lurg.country.Country; import ac.ed.lurg.landuse.AreasItem; import ac.ed.lurg.landuse.IrrigationCostItem; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.LandDataType; import ac.ed.lurg.types.YieldType; import ac.ed.lurg.yield.YieldResponsesItem; public class GamsLocationTest { - public static final int NUM_LOCATIONS_PER_COUNTRY = 5; + public static final int NUM_LOCATIONS_PER_COUNTRY = 3; public static void main(String[] args) { GamsLocationTest aGamsTest = new GamsLocationTest(); @@ -27,16 +28,14 @@ public class GamsLocationTest { opti.run(); } - Map<CropType, Double> getProjectedDemand() { - Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 300.0); - dummyMap.put(CropType.FRUIT, 50.0); - dummyMap.put(CropType.OILCROPS, 50.0); - dummyMap.put(CropType.STARCHY_ROOTS, 150.0); - dummyMap.put(CropType.PULSES, 60.0); - dummyMap.put(CropType.VEGETABLES, 20.0); - dummyMap.put(CropType.MEAT_OR_PASTURE, 480.0); - dummyMap.put(CropType.TREENUTS, 10.0); + Map<CommodityType, Double> getProjectedDemand() { + Map<CommodityType, Double> dummyMap = new HashMap<CommodityType, Double>(); + dummyMap.put(CommodityType.CEREALS, 300.0); + dummyMap.put(CommodityType.SOYBEAN, 50.0); + dummyMap.put(CommodityType.OILCROPS, 50.0); + dummyMap.put(CommodityType.PULSES, 60.0); + dummyMap.put(CommodityType.STARCHY_ROOTS, 150.0); + dummyMap.put(CommodityType.MEAT_OR_PASTURE, 480.0); return dummyMap; } @@ -94,14 +93,16 @@ public class GamsLocationTest { AreasItem getAreaItem(double cereal, double pasture) { AreasItem dummyMap = new AreasItem(); - dummyMap.setCropArea(CropType.CEREALS, 5.0 + cereal); - dummyMap.setCropArea(CropType.FRUIT, 5.0); + + dummyMap.setCropArea(CropType.WHEAT, 5.0 + cereal); + dummyMap.setCropArea(CropType.MAIZE, 5.0); + dummyMap.setCropArea(CropType.RICE, 5.0); + dummyMap.setCropArea(CropType.TROPICAL_CEREALS, 5.0); + dummyMap.setCropArea(CropType.SOYBEAN, 5.0); dummyMap.setCropArea(CropType.OILCROPS, 5.0); - dummyMap.setCropArea(CropType.STARCHY_ROOTS, 5.0); dummyMap.setCropArea(CropType.PULSES, 5.0); - dummyMap.setCropArea(CropType.VEGETABLES, 5.0); + dummyMap.setCropArea(CropType.STARCHY_ROOTS, 5.0); dummyMap.setCropArea(CropType.MEAT_OR_PASTURE, 5.0 + pasture); - dummyMap.setCropArea(CropType.TREENUTS, 5.0); dummyMap.setLandCoverArea(LandDataType.FOREST, 200 - pasture - cereal); dummyMap.setLandCoverArea(LandDataType.OTHER_NATURAL, 30); @@ -110,40 +111,43 @@ public class GamsLocationTest { Map<CropType, Double> getWorldInputEnergy() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 0.4); - dummyMap.put(CropType.FRUIT, 5.0); + dummyMap.put(CropType.WHEAT, 0.4); + dummyMap.put(CropType.MAIZE, 0.4); + dummyMap.put(CropType.RICE, 1.0); + dummyMap.put(CropType.TROPICAL_CEREALS, 2.0); + dummyMap.put(CropType.SOYBEAN, 0.6); dummyMap.put(CropType.OILCROPS, 10.0); - dummyMap.put(CropType.STARCHY_ROOTS, 5.0); dummyMap.put(CropType.PULSES, 20.0); - dummyMap.put(CropType.VEGETABLES, 20.0); + dummyMap.put(CropType.STARCHY_ROOTS, 5.0); dummyMap.put(CropType.MEAT_OR_PASTURE, 80.0); - dummyMap.put(CropType.TREENUTS, 5.0); return dummyMap; } Map<CropType, Double> getMaxNetImport() { - Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); - dummyMaxExports.put(CropType.CEREALS, 24.0); - dummyMaxExports.put(CropType.FRUIT, 5.0); - dummyMaxExports.put(CropType.OILCROPS, 5.0); - dummyMaxExports.put(CropType.STARCHY_ROOTS, 5.0); - dummyMaxExports.put(CropType.PULSES, 5.0); - dummyMaxExports.put(CropType.VEGETABLES, 5.0); - dummyMaxExports.put(CropType.MEAT_OR_PASTURE, 5.0); - dummyMaxExports.put(CropType.TREENUTS, 5.0); - return dummyMaxExports; + Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); + dummyMap.put(CropType.WHEAT, 24.0); + dummyMap.put(CropType.MAIZE, 5.0); + dummyMap.put(CropType.OILCROPS, 5.0); + dummyMap.put(CropType.TROPICAL_CEREALS, 2.0); + dummyMap.put(CropType.SOYBEAN, 0.6); + dummyMap.put(CropType.OILCROPS, 10.0); + dummyMap.put(CropType.PULSES, 20.0); + dummyMap.put(CropType.STARCHY_ROOTS, 5.0); + dummyMap.put(CropType.MEAT_OR_PASTURE, 5.0); + return dummyMap; } Map<CropType, Double> getMinNetImport() { - Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); - dummyMaxExports.put(CropType.CEREALS, -24.0); - dummyMaxExports.put(CropType.FRUIT, -5.0); - dummyMaxExports.put(CropType.OILCROPS, -5.0); - dummyMaxExports.put(CropType.STARCHY_ROOTS, -5.0); - dummyMaxExports.put(CropType.PULSES, -5.0); - dummyMaxExports.put(CropType.VEGETABLES, -5.0); - dummyMaxExports.put(CropType.MEAT_OR_PASTURE, -5.0); - dummyMaxExports.put(CropType.TREENUTS, -5.0); - return dummyMaxExports; + Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); + dummyMap.put(CropType.WHEAT, -24.0); + dummyMap.put(CropType.MAIZE, -5.0); + dummyMap.put(CropType.OILCROPS, -5.0); + dummyMap.put(CropType.TROPICAL_CEREALS, 0.0); + dummyMap.put(CropType.SOYBEAN, -0.6); + dummyMap.put(CropType.OILCROPS, -10.0); + dummyMap.put(CropType.PULSES, -20.0); + dummyMap.put(CropType.STARCHY_ROOTS, -5.0); + dummyMap.put(CropType.MEAT_OR_PASTURE, -5.0); + return dummyMap; } } diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java index 55dce778..b47ff427 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java @@ -25,7 +25,7 @@ import ac.sac.raster.RasterKey; import ac.sac.raster.RasterSet; public class GamsRasterOptimiser { - public static final boolean DEBUG = true; + public static final boolean DEBUG = false; private GamsRasterInput rasterInputData; private LazyHashMap<Integer, Set<RasterKey>> mapping; @@ -181,19 +181,21 @@ public class GamsRasterOptimiser { YieldResponsesItem yresp = entry.getValue(); RasterKey key = entry.getKey(); + CropType crop = CropType.WHEAT; - if (yresp != null && (yresp.getYieldMax(CropType.CEREALS) < yresp.getYieldFertOnly(CropType.CEREALS) || yresp.getYieldMax(CropType.CEREALS) < yresp.getYieldIrrigOnly(CropType.CEREALS))) { - LogWriter.printlnError(key.getCol() + ", " + key.getRow() + ": " + yieldRaster.getXCoordin(key) + ", " + yieldRaster.getYCoordin(key) + ": " + yresp.getYieldMax(CropType.CEREALS) + ", " + yresp.getYieldFertOnly(CropType.CEREALS) + ", " + yresp.getYieldIrrigOnly(CropType.CEREALS)); + if (yresp != null && (yresp.getYieldMax(crop) < yresp.getYieldFertOnly(crop) || yresp.getYieldMax(crop) < yresp.getYieldIrrigOnly(crop))) { + LogWriter.printlnError(key.getCol() + ", " + key.getRow() + ": " + yieldRaster.getXCoordin(key) + ", " + yieldRaster.getYCoordin(key) + ": " + yresp.getYieldMax(crop) + ", " + yresp.getYieldFertOnly(crop) + ", " + yresp.getYieldIrrigOnly(crop)); } // else if (yresp != null) // LogWriter.println(key.getCol() + ", " + key.getRow() + ": " + yieldRaster.getXCoordin(key) + ", " + yieldRaster.getYCoordin(key) + ": " + yresp.getYieldMax(CropType.CEREALS) + ", " + yresp.getYieldFertOnly(CropType.CEREALS) + ", " + yresp.getYieldIrrigOnly(CropType.CEREALS)); } - int numCerealCats = 4; - int numPastureCats = 4; + int numCerealCats = 3; + int numPastureCats = 3; - List<Double> cerealDivisions = getDivisions(yieldRaster, CropType.CEREALS, numCerealCats); + int thisShouldLookAtCropsOtherThanJustWheat; // need to consider other crops, and perhaps other yieldTypes as well + List<Double> wheatlDivisions = getDivisions(yieldRaster, CropType.WHEAT, numCerealCats); List<Double> pastureDivisions = getDivisions(yieldRaster, CropType.MEAT_OR_PASTURE, numPastureCats); if (DEBUG) LogWriter.println("Making " + numCerealCats * numPastureCats + " categories"); @@ -233,7 +235,7 @@ public class GamsRasterOptimiser { IrrigationCostItem irrigCost = irrigCostRaster.get(key); - int cerealCat = findCategory(cerealDivisions, yresp.getYieldMax(CropType.CEREALS)); + int cerealCat = findCategory(wheatlDivisions, yresp.getYieldMax(CropType.WHEAT)); int pastureCat = findCategory(pastureDivisions, yresp.getYieldMax(CropType.MEAT_OR_PASTURE)); Integer id = cerealCat + pastureCat * numCerealCats; @@ -276,7 +278,7 @@ public class GamsRasterOptimiser { for (Map.Entry<Integer, Set<RasterKey>> e : mapping.entrySet()) { LogWriter.println(e.getKey() + " category has " + e.getValue().size() + " raster areas"); if (DEBUG) for (RasterKey key : e.getValue()) { - LogWriter.println(String.format("\t%s: yields pasture=%.1f, cereal=%.1f", key, yieldRaster.get(key).getYieldMax(CropType.MEAT_OR_PASTURE), yieldRaster.get(key).getYieldMax(CropType.CEREALS))); + LogWriter.println(String.format("\t%s: yields pasture=%.1f, cereal=%.1f", key, yieldRaster.get(key).getYieldMax(CropType.MEAT_OR_PASTURE), yieldRaster.get(key).getYieldMax(CropType.WHEAT))); } } diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOutput.java b/src/ac/ed/lurg/country/gams/GamsRasterOutput.java index 0f04f589..78f2cd12 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterOutput.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterOutput.java @@ -3,7 +3,7 @@ package ac.ed.lurg.country.gams; import java.util.Map; import ac.ed.lurg.landuse.AreasItem; -import ac.ed.lurg.landuse.CommodityData; +import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IntensitiesItem; import ac.ed.lurg.types.CropType; import ac.sac.raster.RasterSet; @@ -14,16 +14,16 @@ public class GamsRasterOutput { private RasterSet<IntensitiesItem> intensityRaster; private RasterSet<AreasItem> cropAreaRaster; - private Map<CropType, CommodityData> commoditiesData; + private Map<CropType, CropUsageData> cropUsageData; - public GamsRasterOutput(RasterSet<AreasItem> cropAreaRaster, Map<CropType, CommodityData> commoditiesData) { + public GamsRasterOutput(RasterSet<AreasItem> cropAreaRaster, Map<CropType, CropUsageData> cropUsageData) { super(); this.cropAreaRaster = cropAreaRaster; - this.commoditiesData = commoditiesData; + this.cropUsageData = cropUsageData; } - public GamsRasterOutput(int status, RasterSet<IntensitiesItem> intensityRaster, RasterSet<AreasItem> cropAreaRaster, Map<CropType, CommodityData> commoditiesData) { - this(cropAreaRaster, commoditiesData); + public GamsRasterOutput(int status, RasterSet<IntensitiesItem> intensityRaster, RasterSet<AreasItem> cropAreaRaster, Map<CropType, CropUsageData> cropUsageData) { + this(cropAreaRaster, cropUsageData); this.status = status; this.intensityRaster = intensityRaster; } @@ -37,7 +37,7 @@ public class GamsRasterOutput { public RasterSet<AreasItem> getCropAreaRaster() { return cropAreaRaster; } - public Map<CropType, CommodityData> getCommoditiesData() { - return commoditiesData; + public Map<CropType, CropUsageData> getCropUsageData() { + return cropUsageData; } } diff --git a/src/ac/ed/lurg/demand/BaseConsumpManager.java b/src/ac/ed/lurg/demand/BaseConsumpManager.java index e0dc1d8f..18d1303f 100644 --- a/src/ac/ed/lurg/demand/BaseConsumpManager.java +++ b/src/ac/ed/lurg/demand/BaseConsumpManager.java @@ -8,16 +8,15 @@ import java.util.Map; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.country.Country; import ac.ed.lurg.country.CountryManager; -import ac.ed.lurg.types.CountryCropKey; -import ac.ed.lurg.types.CropType; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.utils.LogWriter; public class BaseConsumpManager { private static final int COUNTRY_COL = 0; - private static final int CROP_COL = 3; + private static final int COMMODITY_COL = 3; private static final int BASE_CPC_COL = 7; - private Map<CountryCropKey, Double> baseConsumpMap = new HashMap<CountryCropKey, Double>(); + private Map<Country, Map<CommodityType, Double>> baseConsumpMap = new HashMap<Country, Map<CommodityType, Double>>(); public BaseConsumpManager() { super(); @@ -28,7 +27,7 @@ public class BaseConsumpManager { String filename = ModelConfig.BASELINE_CONSUMP_FILE; try { BufferedReader fitReader = new BufferedReader(new FileReader(filename)); - String line, countryName, cropName; + String line, countryName, commodityName; double cpc; fitReader.readLine(); // read header @@ -39,10 +38,19 @@ public class BaseConsumpManager { LogWriter.printlnError("Too few columns in " + filename + ", " + line); countryName = tokens[COUNTRY_COL]; - cropName = tokens[CROP_COL]; + commodityName = tokens[COMMODITY_COL]; cpc = Double.parseDouble(tokens[BASE_CPC_COL]); - baseConsumpMap.put(new CountryCropKey(CountryManager.getForName(countryName), CropType.getForFaoName(cropName)), cpc); + Country country = CountryManager.getForName(countryName); + CommodityType commodity = CommodityType.getForFaoName(commodityName); + + Map<CommodityType, Double> commodityMap = baseConsumpMap.get(country); + if (commodityMap == null) { + commodityMap = new HashMap<CommodityType, Double>(); + baseConsumpMap.put(country, commodityMap); + } + + commodityMap.put(commodity, cpc); } fitReader.close(); @@ -53,12 +61,15 @@ public class BaseConsumpManager { LogWriter.println("Processed " + filename + ", create " + baseConsumpMap.size() + " base consumption values"); } - public double get(Country country, CropType crop) { - Double d = baseConsumpMap.get(new CountryCropKey(country, crop)); - if (d == null) { - LogWriter.printlnError("BaseConsumpManager: can't get value for " + crop + ", " + country); + public double get(Country country, CommodityType commodity) { + Map<CommodityType, Double> commodityMap = baseConsumpMap.get(country); + + if (commodityMap == null || !commodityMap.containsKey(commodity)) { + LogWriter.printlnError("BaseConsumpManager: can't get value for " + commodity + ", " + country); return Double.NaN; } + + Double d = commodityMap.get(commodity); return d.doubleValue(); } } diff --git a/src/ac/ed/lurg/demand/DemandCurve.java b/src/ac/ed/lurg/demand/DemandCurve.java index 0af0d37a..764135db 100644 --- a/src/ac/ed/lurg/demand/DemandCurve.java +++ b/src/ac/ed/lurg/demand/DemandCurve.java @@ -1,6 +1,6 @@ package ac.ed.lurg.demand; -import ac.ed.lurg.types.CropType; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.ModelFitType; @@ -10,11 +10,11 @@ public class DemandCurve { private double b; private double c; private ModelFitType fitType; - private CropType cropType; + private CommodityType commodityType; - DemandCurve(CropType item, double a, double b, double c, ModelFitType fitType) { + DemandCurve(CommodityType commodityType, double a, double b, double c, ModelFitType fitType) { super(); - this.cropType = item; + this.commodityType = commodityType; this.a = a; this.b = b; this.c = c; @@ -37,8 +37,8 @@ public class DemandCurve { return fitType; } - public CropType getCropType() { - return cropType; + public CommodityType getCommodityType() { + return commodityType; } @Override @@ -46,7 +46,7 @@ public class DemandCurve { final int prime = 31; int result = 1; result = prime * result + ((fitType == null) ? 0 : fitType.hashCode()); - result = prime * result + ((cropType == null) ? 0 : cropType.hashCode()); + result = prime * result + ((commodityType == null) ? 0 : commodityType.hashCode()); return result; } @@ -64,10 +64,10 @@ public class DemandCurve { return false; } else if (!fitType.equals(other.fitType)) return false; - if (cropType == null) { - if (other.cropType != null) + if (commodityType == null) { + if (other.commodityType != null) return false; - } else if (!cropType.equals(other.cropType)) + } else if (!commodityType.equals(other.commodityType)) return false; return true; } diff --git a/src/ac/ed/lurg/demand/DemandCurveManager.java b/src/ac/ed/lurg/demand/DemandCurveManager.java index 44517855..8319f163 100644 --- a/src/ac/ed/lurg/demand/DemandCurveManager.java +++ b/src/ac/ed/lurg/demand/DemandCurveManager.java @@ -2,11 +2,12 @@ package ac.ed.lurg.demand; import java.io.BufferedReader; import java.io.FileReader; +import java.io.IOException; import java.util.Collection; import java.util.HashSet; import ac.ed.lurg.ModelConfig; -import ac.ed.lurg.types.CropType; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.ModelFitType; import ac.ed.lurg.utils.LogWriter; @@ -44,12 +45,12 @@ public class DemandCurveManager { c = Double.parseDouble(tokens[C_COL]); fitType = tokens[FIT_TYPE_COL]; - DemandCurve dc = new DemandCurve(CropType.getForFaoName(faoItemName), a, b, c, ModelFitType.findByName(fitType)); + DemandCurve dc = new DemandCurve(CommodityType.getForFaoName(faoItemName), a, b, c, ModelFitType.findByName(fitType)); curves.add(dc); } fitReader.close(); - } catch (Exception e) { + } catch (IOException e) { LogWriter.printlnError("Failed in reading commodity demand fits"); e.printStackTrace(); } diff --git a/src/ac/ed/lurg/demand/DemandManager.java b/src/ac/ed/lurg/demand/DemandManager.java index dd74db09..ace23a3a 100644 --- a/src/ac/ed/lurg/demand/DemandManager.java +++ b/src/ac/ed/lurg/demand/DemandManager.java @@ -5,7 +5,7 @@ import java.util.Map; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.country.Country; -import ac.ed.lurg.types.CropType; +import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.ModelFitType; import ac.ed.lurg.utils.LogWriter; @@ -26,10 +26,10 @@ public class DemandManager { baseConsumpManager = new BaseConsumpManager(); } - public Map<CropType, Double> getDemand(Country country, int year) { + public Map<CommodityType, Double> getDemand(Country country, int year) { SspData baseSspData = sspManager.get(ssp_scenario, ModelConfig.BASE_YEAR, country); - Map<CropType, Double> demandMap = new HashMap<CropType, Double>(); + Map<CommodityType, Double> demandMap = new HashMap<CommodityType, Double>(); SspData sd = sspManager.get(ssp_scenario, year, country); LogWriter.println("Got ssp data for " + country.getCountryName() + " of " + sd); @@ -40,14 +40,10 @@ public class DemandManager { } for (DemandCurve dc : demandCurveManager.get(fitType)) { - double cpc = dc.getConsumptionPc(baseSspData.getGdpPc(), baseConsumpManager.get(country, dc.getCropType()), sd.getGdpPc()); + double cpc = dc.getConsumptionPc(baseSspData.getGdpPc(), baseConsumpManager.get(country, dc.getCommodityType()), sd.getGdpPc()); double d = cpc * sd.getPopulation(); - double thisFudgeFactorShouldBeSomewhereElse; - if (dc.getCropType().equals(CropType.MEAT_OR_PASTURE)) - d *= 0.9; // DM Kg / Kg of feed (cereal) - - demandMap.put(dc.getCropType(), d); + demandMap.put(dc.getCommodityType(), d); } return demandMap; } diff --git a/src/ac/ed/lurg/landuse/CommodityData.java b/src/ac/ed/lurg/landuse/CropUsageData.java similarity index 79% rename from src/ac/ed/lurg/landuse/CommodityData.java rename to src/ac/ed/lurg/landuse/CropUsageData.java index 31b8e9ea..4bcfb340 100644 --- a/src/ac/ed/lurg/landuse/CommodityData.java +++ b/src/ac/ed/lurg/landuse/CropUsageData.java @@ -2,6 +2,7 @@ package ac.ed.lurg.landuse; import java.io.BufferedReader; import java.io.FileReader; +import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -11,7 +12,7 @@ import ac.ed.lurg.country.CountryManager; import ac.ed.lurg.types.CropType; import ac.ed.lurg.utils.LogWriter; -public class CommodityData { +public class CropUsageData { private static final int COUNTRY_COL = 0; private static final int COMMODITY_COL = 2; private static final int FEED_COL = 4; @@ -20,7 +21,7 @@ public class CommodityData { private double feedAmount; private double netImports; - public CommodityData(double feedAmount, double netImports) { + public CropUsageData(double feedAmount, double netImports) { super(); this.feedAmount = feedAmount; this.netImports = netImports; @@ -34,8 +35,8 @@ public class CommodityData { return netImports; } - public static Map<Country, Map<CropType, CommodityData>> readCommodityData() { - Map<Country, Map<CropType, CommodityData>> commodityMap = new HashMap<Country, Map<CropType, CommodityData>>(); + public static Map<Country, Map<CropType, CropUsageData>> readCommodityData() { + Map<Country, Map<CropType, CropUsageData>> commodityMap = new HashMap<Country, Map<CropType, CropUsageData>>(); String filename = ModelConfig.COMMODITY_DATA_FILE; try { BufferedReader fitReader = new BufferedReader(new FileReader(filename)); @@ -56,10 +57,10 @@ public class CommodityData { Country country = CountryManager.getForName(countryName); - CommodityData data = new CommodityData(feedAmount, netImports); - Map<CropType, CommodityData> countryData = commodityMap.get(country); + CropUsageData data = new CropUsageData(feedAmount, netImports); + Map<CropType, CropUsageData> countryData = commodityMap.get(country); if (countryData == null) { - countryData = new HashMap<CropType, CommodityData>(); + countryData = new HashMap<CropType, CropUsageData>(); commodityMap.put(country, countryData); } @@ -67,7 +68,7 @@ public class CommodityData { } fitReader.close(); - } catch (Exception e) { + } catch (IOException e) { LogWriter.printlnError("Failed in reading commodity demand fits"); e.printStackTrace(); } diff --git a/src/ac/ed/lurg/types/CommodityType.java b/src/ac/ed/lurg/types/CommodityType.java new file mode 100644 index 00000000..aa51104e --- /dev/null +++ b/src/ac/ed/lurg/types/CommodityType.java @@ -0,0 +1,51 @@ +package ac.ed.lurg.types; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public enum CommodityType { + + CEREALS("Cereals - Excluding Beer + (Total)", "cereals"), + OILCROPS("Oilcrops + (Total)", "oilcrops"), + PULSES("Pulses + (Total)", "pulses"), + SOYBEAN("Soyabeans", "soybean"), + STARCHY_ROOTS("Starchy Roots + (Total)", "starchyRoots"), + MEAT_OR_PASTURE("meatmilkeggs", "pasture_or_meat"); + + private String faoName; + private String gamsName; + + CommodityType (String faoName, String gamsName) { + this.faoName = faoName; + this.gamsName = gamsName; + } + + private static final Map<String, CommodityType> faoNameCache = new HashMap<String, CommodityType>(); + static { + for (CommodityType c : values()) { + faoNameCache.put(c.getFaoName(), c); + } + } + + public static CommodityType getForFaoName(String faoName) { + CommodityType commodity = faoNameCache.get(faoName); + + if (commodity == null) + throw new RuntimeException("Can't find CommodityType for faoName: " + faoName); + + return commodity; + } + + public static Collection<CommodityType> getAllItems() { + return faoNameCache.values(); + } + + public String getFaoName() { + return faoName; + } + + public String getGamsName() { + return gamsName; + } +} diff --git a/src/ac/ed/lurg/types/CountryCropKey.java b/src/ac/ed/lurg/types/CountryCropKey.java deleted file mode 100644 index ecb72a39..00000000 --- a/src/ac/ed/lurg/types/CountryCropKey.java +++ /dev/null @@ -1,46 +0,0 @@ -package ac.ed.lurg.types; - -import ac.ed.lurg.country.Country; - -public class CountryCropKey { - private Country country; - private CropType crop; - - public CountryCropKey(Country country, CropType crop) { - super(); - this.country = country; - this.crop = crop; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((country == null) ? 0 : country.hashCode()); - result = prime * result + ((crop == null) ? 0 : crop.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - CountryCropKey other = (CountryCropKey) obj; - if (country == null) { - if (other.country != null) - return false; - } else if (!country.equals(other.country)) - return false; - if (crop == null) { - if (other.crop != null) - return false; - } else if (!crop.equals(other.crop)) - return false; - return true; - } - -} diff --git a/src/ac/ed/lurg/types/CropType.java b/src/ac/ed/lurg/types/CropType.java index ce1630b6..33543231 100644 --- a/src/ac/ed/lurg/types/CropType.java +++ b/src/ac/ed/lurg/types/CropType.java @@ -8,14 +8,15 @@ import ac.ed.lurg.utils.LogWriter; public enum CropType { - MEAT_OR_PASTURE("meatmilkeggs", "pasture_or_meat"), - TREENUTS("Treenuts + (Total)", "treenuts"), - VEGETABLES("Vegetables + (Total)", "vegetables"), + WHEAT("WheatBarleyOats", "wheat"), + MAIZE("Maize and products", "maize"), + RICE("Rice (Paddy Equivalent)", "rice"), + TROPICAL_CEREALS("TropicalCereals", "tropicalCereals"), + OILCROPS("Oilcrops + (Total)", "oilcrops"), + SOYBEAN("Soyabeans", "soybean"), PULSES("Pulses + (Total)", "pulses"), STARCHY_ROOTS("Starchy Roots + (Total)", "starchyRoots"), - FRUIT("Fruits - Excluding Wine + (Total)", "fruits"), - OILCROPS("Oilcrops + (Total)", "oilcrops"), - CEREALS("Cereals - Excluding Beer + (Total)", "cereals"); + MEAT_OR_PASTURE("meatmilkeggs", "pasture_or_meat"); private String faoName; private String gamsName; @@ -35,7 +36,11 @@ public enum CropType { } public static CropType getForFaoName(String faoName) { - return getFromCache(faoNameCache, faoName); + CropType crop = getFromCache(faoNameCache, faoName); + if (crop == null) { + throw new RuntimeException("Can't find CropType for faoName: " + faoName); + } + return crop; } public static CropType getForGamsName(String gamsName) { @@ -52,7 +57,7 @@ public enum CropType { } public static Collection<CropType> getAllItems() { - return faoNameCache.values(); + return gamsNameCache.values(); } public String getFaoName() { diff --git a/src/ac/sac/raster/RasterSetTest.java b/src/ac/sac/raster/RasterSetTest.java index 55ba8547..857588fe 100644 --- a/src/ac/sac/raster/RasterSetTest.java +++ b/src/ac/sac/raster/RasterSetTest.java @@ -3,7 +3,6 @@ package ac.sac.raster; import org.junit.Test; import ac.ed.lurg.yield.YieldRaster; -import ac.ed.lurg.yield.YieldResponsesItem; public class RasterSetTest { -- GitLab