From 2ee6a90ac0a1c867273788e1718941c25c32b7ff Mon Sep 17 00:00:00 2001 From: s1924442 <b.arendarczyk@sms.ed.ac.uk> Date: Mon, 8 Mar 2021 21:18:18 +0000 Subject: [PATCH] Minimum land cover implemented. --- GAMS/IntExtOpt.gms | 6 +- src/ac/ed/lurg/ModelConfig.java | 1 + src/ac/ed/lurg/country/CountryAgent.java | 48 +++++++++++--- .../country/gams/GamsLocationOptimiser.java | 50 ++++++++------ .../country/gams/GamsRasterOptimiser.java | 4 +- src/ac/ed/lurg/landuse/CarbonFluxItem.java | 14 ++-- src/ac/ed/lurg/landuse/CarbonFluxReader.java | 66 +++++++++++-------- src/ac/ed/lurg/landuse/WoodYieldItem.java | 12 ++-- src/ac/ed/lurg/landuse/WoodYieldReader.java | 66 +++++++++++-------- src/ac/ed/lurg/types/LandCoverType.java | 32 ++++++--- src/ac/ed/lurg/utils/DoubleMap.java | 8 ++- 11 files changed, 195 insertions(+), 112 deletions(-) diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms index 6e72b652..19d6d68a 100644 --- a/GAMS/IntExtOpt.gms +++ b/GAMS/IntExtOpt.gms @@ -9,7 +9,7 @@ SET feed_crop_less_pasture(feed_crop) / wheat, maize, rice, oilcrops, pulses, starchyRoots, fruitveg /; SET import_crop(all_types) / monogastrics, ruminants, wheat, maize, rice, oilcrops, pulses, starchyRoots, fruitveg, sugar, energycrops/; - SET land_cover / cropland, pasture, timberForest, carbonForest, natural /; + SET land_cover / cropland, pasture, timberForest, carbonForest, unmanagedForest, otherNatural /; * SET wood_producing / timberForest, carbonForest, natural /; ALIAS (land_cover, land_cover_before); ALIAS (land_cover, land_cover_after); @@ -177,7 +177,7 @@ $gdxin PASTURE_TOTAL_CHANGE_CONSTRAINT(location) CROPLAND_LAND_COVER_CALC(location) cropland area equals sum of all crop areas PASTURE_LAND_COVER_CALC(location) pasture area (land cover) equals pasture area (land use) - MINIMUM_LAND_COVER_CONSTRAINT(location) constraint on land cover conversion + MINIMUM_LAND_COVER_CONSTRAINT(land_cover, location) constraint on land cover conversion LAND_COVER_CHANGE_CALC(land_cover, location) LAND_COVER_CHANGE_CONSTRAINT(land_cover, location) can't convert more land than was previously available LAND_COVER_SELF_CHANGE_CONSTRAINT(land_cover, location) calculate no change area (e.g. cropland to cropland) @@ -241,7 +241,7 @@ $gdxin PASTURE_LAND_COVER_CALC(location) .. landCoverArea('pasture', location) =E= cropArea('pasture', location); - MINIMUM_LAND_COVER_CONSTRAINT(location, land_cover) .. landCoverArea(land_cover, location) =G= minimumLandCover(land_cover, location); + MINIMUM_LAND_COVER_CONSTRAINT(land_cover, location) .. landCoverArea(land_cover, location) =G= minimumLandCoverArea(land_cover, location); LAND_COVER_CHANGE_CALC(land_cover, location) .. landCoverArea(land_cover, location) =E= previousLandCoverArea(land_cover, location) + sum(land_cover_before, landCoverChange(land_cover_before, land_cover, location)) - sum(land_cover_after, landCoverChange(land_cover, land_cover_after, location)); diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java index c3352508..0bf741dd 100755 --- a/src/ac/ed/lurg/ModelConfig.java +++ b/src/ac/ed/lurg/ModelConfig.java @@ -425,5 +425,6 @@ public class ModelConfig { // Forestry parameters public static final double CARBON_PRICE = getDoubleProperty("CARBON_PRICE", 10.0); // $/tC-eq public static final double WOOD_PRICE = getDoubleProperty("WOOD_PRICE", 50.0); // $/tC-eq + public static final int FOREST_LOCKIN_PERIOD = getIntProperty("FOREST_LOCKIN_PERIOD", 20); // cannot convert forest after planting for this many years } diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index bb230396..a6b0eea4 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -53,12 +53,13 @@ public class CountryAgent extends AbstractCountryAgent { this.yieldClusters = yieldClusters; this.subsidyRates = subsidyRates; - minimumLandCover = new TripleMap<Integer, Integer, LandCoverType, Double>(); // TODO how to intialise? - GamsRasterOutput initialData = new GamsRasterOutput(cropAreaRaster, cropUsageData); previousGamsRasterOutput = initialData; saveGamsGdxFiles = (ModelConfig.GAMS_COUNTRY_TO_SAVE != null && country.getName().equals(ModelConfig.GAMS_COUNTRY_TO_SAVE)); + + initialiseMinimumLandCover(yieldClusters); + } public RasterSet<IntegerRasterItem> getYieldClusters() { @@ -130,10 +131,8 @@ public class CountryAgent extends AbstractCountryAgent { if (yieldClusters==null) yieldClusters = calcYieldClusters(irrigData, countryYieldSurfaces); // this should only be on the first timestep - - // optimize areas and intensity - GamsRasterInput input = getGamsRasterInput(irrigData, countryYieldSurfaces, globalGen2EcIncrease, carbonFluxData, woodYieldData, minimumLandCover); + GamsRasterInput input = getGamsRasterInput(irrigData, countryYieldSurfaces, globalGen2EcIncrease, carbonFluxData, woodYieldData); GamsRasterOptimiser opti = new GamsRasterOptimiser(input, yieldClusters); LogWriter.println("Running " + country.getName() + ", currentTimestep " + currentTimestep); @@ -167,8 +166,7 @@ public class CountryAgent extends AbstractCountryAgent { } private GamsRasterInput getGamsRasterInput(RasterSet<IrrigationItem> irrigData, YieldRaster countryYieldSurfaces, double gen2EcIncrease, - RasterSet<CarbonFluxItem> carbonFluxData, RasterSet<WoodYieldItem> woodYieldData, - TripleMap<Integer, Integer, LandCoverType, Double> minimumLandCover) { + RasterSet<CarbonFluxItem> carbonFluxData, RasterSet<WoodYieldItem> woodYieldData) { double allowedImportChange; if (currentTimestep.isInitialTimestep() || (ModelConfig.IS_CALIBRATION_RUN && currentTimestep.getTimestep() <= ModelConfig.END_FIRST_STAGE_CALIBRATION)) { // initialisation time-step @@ -212,10 +210,10 @@ public class CountryAgent extends AbstractCountryAgent { } // Aggregate minimum land cover for current timestep - DoubleMap<Integer, LandCoverType, Double> minimumLandCoverForTimestep = new DoubleMap<Integer, LandCoverType, Double>(); + DoubleMap<Integer, LandCoverType, Double> minimumLandCoverForTimestep = new DoubleMap<Integer, LandCoverType, Double>(); //<Location, LandCoverType, Area> for (Entry<Integer, DoubleMap<Integer, LandCoverType, Double>> yearMap : minimumLandCover.getMap().entrySet()) { - if (yearMap.getKey() < currentTimestep.getYear()) { + if (yearMap.getKey() > currentTimestep.getYear()) { for (Entry<Integer, Map<LandCoverType, Double>> locMap : yearMap.getValue().getMap().entrySet()) { for (Entry<LandCoverType, Double> coverMap : locMap.getValue().entrySet()) { minimumLandCoverForTimestep.addTo(locMap.getKey(), coverMap.getKey(), coverMap.getValue()); @@ -315,7 +313,37 @@ public class CountryAgent extends AbstractCountryAgent { } + private void initialiseMinimumLandCover(RasterSet<IntegerRasterItem> yieldClusters) { + minimumLandCover = new TripleMap<Integer, Integer, LandCoverType, Double>(); + int maxYear = Integer.MAX_VALUE; + for (LandCoverType lc : LandCoverType.getConvertibleTypes()) { + + for (IntegerRasterItem iri : yieldClusters.values()) { + // inefficient, better to store somewhere the total number of clusters + if (iri != null) // TODO Why? + minimumLandCover.put(maxYear, iri.getInt(), lc, 0.0); + } + + /* + for (Entry<RasterKey, IntegerRasterItem> iri : yieldClusters.entrySet()) { + // inefficient, better to store somewhere the total number of clusters + LogWriter.println("" + iri.getKey().toString()); + LogWriter.println("" + iri.getValue().getInt()); + minimumLandCover.put(maxYear, iri.getValue().getInt(), lc, 0.0); + } + */ + } + } + private void updateMinimumLandCover() { - //TODO + DoubleMap<Integer, LandCoverType, Double> minimumLandCoverAdditions = previousGamsRasterOutput.getMinimumLandCoverAdditions(); + int expiryYear = currentTimestep.getYear() + ModelConfig.FOREST_LOCKIN_PERIOD; + for (Entry<Integer, Map<LandCoverType, Double>> locMap : minimumLandCoverAdditions.getMap().entrySet()) { + int locId = locMap.getKey(); + Map<LandCoverType, Double> changeMap = locMap.getValue(); + for (Entry<LandCoverType, Double> lcChange: changeMap.entrySet()) { + minimumLandCover.put(expiryYear, locId, lcChange.getKey(), lcChange.getValue()); + } + } } } \ No newline at end of file diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java index 2786437b..bdef567e 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java @@ -75,8 +75,10 @@ public class GamsLocationOptimiser { long startTime = System.currentTimeMillis(); gamsJob.run(opt, inDB); + /* if (inputData.getCountryInput().getCountry().getName().equals("Peru & Ecuador")) LogWriter.println("" + inputData.getPreviousLandUse().get(2).getUnprotectedLandCoverArea(LandCoverType.CROPLAND)); + */ if (ModelConfig.CLEANUP_GAMS_DIR) cleanup(ws.workingDirectory()); @@ -112,7 +114,6 @@ public class GamsLocationOptimiser { GAMSParameter seedAndWasteRateP = inDB.addParameter("seedAndWasteRate", 1); GAMSParameter prevLandCoverP = inDB.addParameter("previousLandCoverArea", 2); - //GAMSParameter minLandCoverP = inDB.addParameter("minimumLandCoverArea", 2); double totalAgriLand = 0; double totalSuitable = 0; @@ -175,9 +176,17 @@ public class GamsLocationOptimiser { } // Previous land covers + // TODO Increase number of decimal places? + for (LandCoverType lc : LandCoverType.getConvertibleTypes()) { + Vector<String> v = new Vector<String>(); + v.add(lc.getName()); + v.add(locString); + setGamsParamValueTruncate(prevLandCoverP.addRecord(v), landUseItem.getUnprotectedLandCoverArea(lc), 3); + } + + /* Vector<String> v = new Vector<String>(); - // TODO Increase number of decimal places? v.add("cropland"); v.add(locString); setGamsParamValueTruncate(prevLandCoverP.addRecord(v), landUseItem.getUnprotectedLandCoverArea(LandCoverType.CROPLAND), 3); @@ -193,7 +202,7 @@ public class GamsLocationOptimiser { v.set(0, "natural"); setGamsParamValueTruncate(prevLandCoverP.addRecord(v), landUseItem.getUnprotectedLandCoverArea(LandCoverType.OTHER_NATURAL) + landUseItem.getUnprotectedLandCoverArea(LandCoverType.UNMANAGED_FOREST), 3); - + */ } if (DEBUG) LogWriter.println(String.format(" Total agricultural %.1f,\t suitable %.1f", totalAgriLand, totalSuitable)); @@ -367,8 +376,8 @@ public class GamsLocationOptimiser { String locString = Integer.toString(locationId); CarbonFluxItem cFlux = entry.getValue(); - for (GamsLandCoverType prevLC : GamsLandCoverType.values()) { - for (GamsLandCoverType newLC : GamsLandCoverType.values()) { + for (LandCoverType prevLC : LandCoverType.getConvertibleTypes()) { + for (LandCoverType newLC : LandCoverType.getConvertibleTypes()) { Vector<String> v = new Vector<String>(); v.add(prevLC.getName()); v.add(newLC.getName()); @@ -387,8 +396,8 @@ public class GamsLocationOptimiser { String locString = Integer.toString(locationId); WoodYieldItem wYield = entry.getValue(); - for (GamsLandCoverType prevLC : GamsLandCoverType.values()) { - for (GamsLandCoverType newLC : GamsLandCoverType.values()) { + for (LandCoverType prevLC : LandCoverType.getConvertibleTypes()) { + for (LandCoverType newLC : LandCoverType.getConvertibleTypes()) { Vector<String> v = new Vector<String>(); v.add(prevLC.getName()); v.add(newLC.getName()); @@ -400,8 +409,8 @@ public class GamsLocationOptimiser { } // Minimum land covers - GAMSParameter minimumLandCover = inDB.addParameter("minimumLandCover", 2); - + GAMSParameter minimumLandCover = inDB.addParameter("minimumLandCoverArea", 2); + for (Entry<Integer, Map<LandCoverType, Double>> locMap : inputData.getMinimumLandCover().getMap().entrySet()) { Integer locationId = locMap.getKey(); String locString = Integer.toString(locationId); @@ -414,6 +423,7 @@ public class GamsLocationOptimiser { } } + } private void addScalar(GAMSDatabase gamsDb, String recordName, double val, int places) { @@ -582,12 +592,6 @@ public class GamsLocationOptimiser { TripleMap<Integer, LandCoverType, LandCoverType, Double> landCoverChanges = new TripleMap<Integer, LandCoverType, LandCoverType, Double>(); - /* - if (inputData.getCountryInput().getCountry().getName().equals("Peru & Ecuador")) { - LogWriter.println("foo"); - } - */ - for (GAMSVariableRecord rec : varLandCoverChange) { String fromLC = rec.getKeys()[0]; String toLC = rec.getKeys()[1]; @@ -596,12 +600,16 @@ public class GamsLocationOptimiser { double change = rec.getLevel(); + if (!fromLC.equals(toLC)) { + landCoverChanges.put(locId, LandCoverType.getForName(fromLC), LandCoverType.getForName(toLC), change); + } /* if (change == 0.0) { continue; } */ + /* if (fromLC.equals("natural") && toLC.equals("natural")) { continue; } else if (fromLC.equals("natural")) { @@ -615,8 +623,8 @@ public class GamsLocationOptimiser { } else { landCoverChanges.put(locId, LandCoverType.getForName(fromLC), LandCoverType.getForName(toLC), change); } - - + */ + } // Minimum land cover additions. Need to keep track of new forest areas to restrict conversion @@ -626,8 +634,12 @@ public class GamsLocationOptimiser { Integer locId = locMap.getKey(); DoubleMap<LandCoverType, LandCoverType, Double> changeMap = locMap.getValue(); for (LandCoverType fromLC : LandCoverType.getConvertibleTypes()) { - minimumLandCoverAdditions.addTo(locId, LandCoverType.TIMBER_FOREST, changeMap.get(fromLC, LandCoverType.TIMBER_FOREST)); - minimumLandCoverAdditions.addTo(locId, LandCoverType.CARBON_FOREST, changeMap.get(fromLC, LandCoverType.CARBON_FOREST)); + if (!fromLC.equals(LandCoverType.TIMBER_FOREST)) + minimumLandCoverAdditions.addTo(locId, LandCoverType.TIMBER_FOREST, changeMap.get(fromLC, LandCoverType.TIMBER_FOREST)); + if (!fromLC.equals(LandCoverType.CARBON_FOREST)) + minimumLandCoverAdditions.addTo(locId, LandCoverType.CARBON_FOREST, changeMap.get(fromLC, LandCoverType.CARBON_FOREST)); + if (!fromLC.equals(LandCoverType.UNMANAGED_FOREST)) + minimumLandCoverAdditions.addTo(locId, LandCoverType.UNMANAGED_FOREST, changeMap.get(fromLC, LandCoverType.UNMANAGED_FOREST)); } } diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java index 60b4cb5d..c961e515 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java @@ -415,8 +415,8 @@ public class GamsRasterOptimiser { } // Aggregate carbon fluxes and wood yields - for (GamsLandCoverType prevLC : GamsLandCoverType.values()) { - for (GamsLandCoverType newLC : GamsLandCoverType.values()) { + for (LandCoverType prevLC : LandCoverType.getConvertibleTypes()) { + for (LandCoverType newLC : LandCoverType.getConvertibleTypes()) { // Carbon flux if (!aggCFlux.checkForKeys(prevLC, newLC)) { // if not seen yet diff --git a/src/ac/ed/lurg/landuse/CarbonFluxItem.java b/src/ac/ed/lurg/landuse/CarbonFluxItem.java index 7c471ae3..ce7452d6 100644 --- a/src/ac/ed/lurg/landuse/CarbonFluxItem.java +++ b/src/ac/ed/lurg/landuse/CarbonFluxItem.java @@ -4,27 +4,27 @@ import ac.sac.raster.RasterItem; import java.util.HashMap; import java.util.Map; -import ac.ed.lurg.types.GamsLandCoverType; +import ac.ed.lurg.types.LandCoverType; public class CarbonFluxItem implements RasterItem { - Map<GamsLandCoverType, Map<GamsLandCoverType, Double>> carbonFluxes = new HashMap<GamsLandCoverType, Map<GamsLandCoverType, Double>>(); + Map<LandCoverType, Map<LandCoverType, Double>> carbonFluxes = new HashMap<LandCoverType, Map<LandCoverType, Double>>(); - public void setCarbonFlux(GamsLandCoverType previousLandCover, GamsLandCoverType newLandCover, double carbonFlux) { + public void setCarbonFlux(LandCoverType previousLandCover, LandCoverType newLandCover, double carbonFlux) { if (carbonFluxes.containsKey(previousLandCover)) { carbonFluxes.get(previousLandCover).put(newLandCover, carbonFlux); } else { - Map<GamsLandCoverType, Double> cfMap = new HashMap<GamsLandCoverType, Double>(); + Map<LandCoverType, Double> cfMap = new HashMap<LandCoverType, Double>(); cfMap.put(newLandCover, carbonFlux); carbonFluxes.put(previousLandCover, cfMap); } } - public double getCarbonFlux(GamsLandCoverType previousLandCover, GamsLandCoverType newLandCover) { + public double getCarbonFlux(LandCoverType previousLandCover, LandCoverType newLandCover) { return carbonFluxes.get(previousLandCover).get(newLandCover); } - public boolean checkForKeys(GamsLandCoverType previousLandCover, GamsLandCoverType newLandCover) { + public boolean checkForKeys(LandCoverType previousLandCover, LandCoverType newLandCover) { if (carbonFluxes.containsKey(previousLandCover)) { if (carbonFluxes.get(previousLandCover).containsKey(newLandCover)) { return true; @@ -36,7 +36,7 @@ public class CarbonFluxItem implements RasterItem { } } - public Map<GamsLandCoverType, Map<GamsLandCoverType, Double>> getCarbonFluxes() { + public Map<LandCoverType, Map<LandCoverType, Double>> getCarbonFluxes() { return carbonFluxes; } diff --git a/src/ac/ed/lurg/landuse/CarbonFluxReader.java b/src/ac/ed/lurg/landuse/CarbonFluxReader.java index b07f14a3..29014937 100644 --- a/src/ac/ed/lurg/landuse/CarbonFluxReader.java +++ b/src/ac/ed/lurg/landuse/CarbonFluxReader.java @@ -2,7 +2,7 @@ package ac.ed.lurg.landuse; import java.util.Map; -import ac.ed.lurg.types.GamsLandCoverType; +import ac.ed.lurg.types.LandCoverType; import ac.sac.raster.AbstractTabularRasterReader; import ac.sac.raster.RasterKey; import ac.sac.raster.RasterSet; @@ -18,35 +18,47 @@ public class CarbonFluxReader extends AbstractTabularRasterReader<CarbonFluxItem @Override protected void setData(RasterKey key, CarbonFluxItem item, Map<String, Double> rowValues) { - item.setCarbonFlux(GamsLandCoverType.CROPLAND, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "crop_to_ntrl") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CROPLAND, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "crop_to_forT") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CROPLAND, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "crop_to_forC") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CROPLAND, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "crop_to_past") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CROPLAND, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "crop_to_crop") * conversionFactor); + item.setCarbonFlux(LandCoverType.CROPLAND, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "crop_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.CROPLAND, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "crop_to_forT") * conversionFactor); + item.setCarbonFlux(LandCoverType.CROPLAND, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "crop_to_forC") * conversionFactor); + item.setCarbonFlux(LandCoverType.CROPLAND, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "crop_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.CROPLAND, LandCoverType.PASTURE, getValueForCol(rowValues, "crop_to_past") * conversionFactor); + item.setCarbonFlux(LandCoverType.CROPLAND, LandCoverType.CROPLAND, getValueForCol(rowValues, "crop_to_crop") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.PASTURE, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "past_to_ntrl") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.PASTURE, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "past_to_forT") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.PASTURE, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "past_to_forC") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.PASTURE, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "past_to_crop") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.PASTURE, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "past_to_past") * conversionFactor); + item.setCarbonFlux(LandCoverType.PASTURE, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "past_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.PASTURE, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "past_to_forT") * conversionFactor); + item.setCarbonFlux(LandCoverType.PASTURE, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "past_to_forC") * conversionFactor); + item.setCarbonFlux(LandCoverType.PASTURE, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "past_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.PASTURE, LandCoverType.CROPLAND, getValueForCol(rowValues, "past_to_crop") * conversionFactor); + item.setCarbonFlux(LandCoverType.PASTURE, LandCoverType.PASTURE, getValueForCol(rowValues, "past_to_past") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.NATURAL, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "ntrl_to_past") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.NATURAL, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "ntrl_to_forT") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.NATURAL, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "ntrl_to_forC") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.NATURAL, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "ntrl_to_crop") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.NATURAL, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.OTHER_NATURAL, LandCoverType.PASTURE, getValueForCol(rowValues, "ntrl_to_past") * conversionFactor); + item.setCarbonFlux(LandCoverType.OTHER_NATURAL, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "ntrl_to_forT") * conversionFactor); + item.setCarbonFlux(LandCoverType.OTHER_NATURAL, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "ntrl_to_forC") * conversionFactor); + item.setCarbonFlux(LandCoverType.OTHER_NATURAL, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.OTHER_NATURAL, LandCoverType.CROPLAND, getValueForCol(rowValues, "ntrl_to_crop") * conversionFactor); + item.setCarbonFlux(LandCoverType.OTHER_NATURAL, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "forT_to_past") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "forT_to_ntrl") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forT_to_forC") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "forT_to_crop") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forT_to_forT") * conversionFactor); - - item.setCarbonFlux(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "forC_to_past") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "forC_to_ntrl") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forC_to_forT") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "forC_to_crop") * conversionFactor); - item.setCarbonFlux(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forC_to_forC") * conversionFactor); + item.setCarbonFlux(LandCoverType.TIMBER_FOREST, LandCoverType.PASTURE, getValueForCol(rowValues, "forT_to_past") * conversionFactor); + item.setCarbonFlux(LandCoverType.TIMBER_FOREST, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "forT_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.TIMBER_FOREST, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forT_to_forC") * conversionFactor); + item.setCarbonFlux(LandCoverType.TIMBER_FOREST, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "forT_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.TIMBER_FOREST, LandCoverType.CROPLAND, getValueForCol(rowValues, "forT_to_crop") * conversionFactor); + item.setCarbonFlux(LandCoverType.TIMBER_FOREST, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forT_to_forT") * conversionFactor); + + item.setCarbonFlux(LandCoverType.CARBON_FOREST, LandCoverType.PASTURE, getValueForCol(rowValues, "forC_to_past") * conversionFactor); + item.setCarbonFlux(LandCoverType.CARBON_FOREST, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "forC_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.CARBON_FOREST, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forC_to_forT") * conversionFactor); + item.setCarbonFlux(LandCoverType.CARBON_FOREST, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "forC_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.CARBON_FOREST, LandCoverType.CROPLAND, getValueForCol(rowValues, "forC_to_crop") * conversionFactor); + item.setCarbonFlux(LandCoverType.CARBON_FOREST, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forC_to_forC") * conversionFactor); + + item.setCarbonFlux(LandCoverType.UNMANAGED_FOREST, LandCoverType.PASTURE, getValueForCol(rowValues, "ntrl_to_past") * conversionFactor); + item.setCarbonFlux(LandCoverType.UNMANAGED_FOREST, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "ntrl_to_forT") * conversionFactor); + item.setCarbonFlux(LandCoverType.UNMANAGED_FOREST, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "ntrl_to_forC") * conversionFactor); + item.setCarbonFlux(LandCoverType.UNMANAGED_FOREST, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); + item.setCarbonFlux(LandCoverType.UNMANAGED_FOREST, LandCoverType.CROPLAND, getValueForCol(rowValues, "ntrl_to_crop") * conversionFactor); + item.setCarbonFlux(LandCoverType.UNMANAGED_FOREST, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); } diff --git a/src/ac/ed/lurg/landuse/WoodYieldItem.java b/src/ac/ed/lurg/landuse/WoodYieldItem.java index 6c5f9473..3da28759 100644 --- a/src/ac/ed/lurg/landuse/WoodYieldItem.java +++ b/src/ac/ed/lurg/landuse/WoodYieldItem.java @@ -3,27 +3,27 @@ package ac.ed.lurg.landuse; import java.util.HashMap; import java.util.Map; -import ac.ed.lurg.types.GamsLandCoverType; +import ac.ed.lurg.types.LandCoverType; import ac.sac.raster.RasterItem; public class WoodYieldItem implements RasterItem { - Map<GamsLandCoverType, Map<GamsLandCoverType, Double>> woodYields = new HashMap<GamsLandCoverType, Map<GamsLandCoverType, Double>>(); + Map<LandCoverType, Map<LandCoverType, Double>> woodYields = new HashMap<LandCoverType, Map<LandCoverType, Double>>(); - public void setWoodYield(GamsLandCoverType previousLandCover, GamsLandCoverType newLandCover, double woodYield) { + public void setWoodYield(LandCoverType previousLandCover, LandCoverType newLandCover, double woodYield) { if (woodYields.containsKey(previousLandCover)) { woodYields.get(previousLandCover).put(newLandCover, woodYield); } else { - Map<GamsLandCoverType, Double> cfMap = new HashMap<GamsLandCoverType, Double>(); + Map<LandCoverType, Double> cfMap = new HashMap<LandCoverType, Double>(); cfMap.put(newLandCover, woodYield); woodYields.put(previousLandCover, cfMap); } } - public double getWoodYield(GamsLandCoverType previousLandCover, GamsLandCoverType newLandCover) { + public double getWoodYield(LandCoverType previousLandCover, LandCoverType newLandCover) { return woodYields.get(previousLandCover).get(newLandCover); } - public boolean checkForKeys(GamsLandCoverType previousLandCover, GamsLandCoverType newLandCover) { + public boolean checkForKeys(LandCoverType previousLandCover, LandCoverType newLandCover) { if (woodYields.containsKey(previousLandCover)) { if (woodYields.get(previousLandCover).containsKey(newLandCover)) { return true; diff --git a/src/ac/ed/lurg/landuse/WoodYieldReader.java b/src/ac/ed/lurg/landuse/WoodYieldReader.java index eb8c5aee..eca499a9 100644 --- a/src/ac/ed/lurg/landuse/WoodYieldReader.java +++ b/src/ac/ed/lurg/landuse/WoodYieldReader.java @@ -3,6 +3,7 @@ package ac.ed.lurg.landuse; import java.util.Map; import ac.ed.lurg.types.GamsLandCoverType; +import ac.ed.lurg.types.LandCoverType; import ac.sac.raster.AbstractTabularRasterReader; import ac.sac.raster.RasterKey; import ac.sac.raster.RasterSet; @@ -17,35 +18,46 @@ public class WoodYieldReader extends AbstractTabularRasterReader<WoodYieldItem> } protected void setData(RasterKey key, WoodYieldItem item, Map<String, Double> rowValues) { - item.setWoodYield(GamsLandCoverType.CROPLAND, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "crop_to_ntrl") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CROPLAND, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "crop_to_forT") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CROPLAND, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "crop_to_forC") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CROPLAND, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "crop_to_past") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CROPLAND, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "crop_to_crop") * conversionFactor); + item.setWoodYield(LandCoverType.CROPLAND, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "crop_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.CROPLAND, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "crop_to_forT") * conversionFactor); + item.setWoodYield(LandCoverType.CROPLAND, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "crop_to_forC") * conversionFactor); + item.setWoodYield(LandCoverType.CROPLAND, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "crop_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.CROPLAND, LandCoverType.PASTURE, getValueForCol(rowValues, "crop_to_past") * conversionFactor); + item.setWoodYield(LandCoverType.CROPLAND, LandCoverType.CROPLAND, getValueForCol(rowValues, "crop_to_crop") * conversionFactor); + + item.setWoodYield(LandCoverType.PASTURE, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "past_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.PASTURE, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "past_to_forT") * conversionFactor); + item.setWoodYield(LandCoverType.PASTURE, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "past_to_forC") * conversionFactor); + item.setWoodYield(LandCoverType.PASTURE, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "past_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.PASTURE, LandCoverType.CROPLAND, getValueForCol(rowValues, "past_to_crop") * conversionFactor); + item.setWoodYield(LandCoverType.PASTURE, LandCoverType.PASTURE, getValueForCol(rowValues, "past_to_past") * conversionFactor); - item.setWoodYield(GamsLandCoverType.PASTURE, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "past_to_ntrl") * conversionFactor); - item.setWoodYield(GamsLandCoverType.PASTURE, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "past_to_forT") * conversionFactor); - item.setWoodYield(GamsLandCoverType.PASTURE, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "past_to_forC") * conversionFactor); - item.setWoodYield(GamsLandCoverType.PASTURE, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "past_to_crop") * conversionFactor); - item.setWoodYield(GamsLandCoverType.PASTURE, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "past_to_past") * conversionFactor); + item.setWoodYield(LandCoverType.OTHER_NATURAL, LandCoverType.PASTURE, getValueForCol(rowValues, "ntrl_to_past") * conversionFactor); + item.setWoodYield(LandCoverType.OTHER_NATURAL, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "ntrl_to_forT") * conversionFactor); + item.setWoodYield(LandCoverType.OTHER_NATURAL, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "ntrl_to_forC") * conversionFactor); + item.setWoodYield(LandCoverType.OTHER_NATURAL, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.OTHER_NATURAL, LandCoverType.CROPLAND, getValueForCol(rowValues, "ntrl_to_crop") * conversionFactor); + item.setWoodYield(LandCoverType.OTHER_NATURAL, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); - item.setWoodYield(GamsLandCoverType.NATURAL, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "ntrl_to_past") * conversionFactor); - item.setWoodYield(GamsLandCoverType.NATURAL, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "ntrl_to_forT") * conversionFactor); - item.setWoodYield(GamsLandCoverType.NATURAL, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "ntrl_to_forC") * conversionFactor); - item.setWoodYield(GamsLandCoverType.NATURAL, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "ntrl_to_crop") * conversionFactor); - item.setWoodYield(GamsLandCoverType.NATURAL, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.TIMBER_FOREST, LandCoverType.PASTURE, getValueForCol(rowValues, "forT_to_past") * conversionFactor); + item.setWoodYield(LandCoverType.TIMBER_FOREST, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "forT_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.TIMBER_FOREST, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forT_to_forC") * conversionFactor); + item.setWoodYield(LandCoverType.TIMBER_FOREST, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "forT_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.TIMBER_FOREST, LandCoverType.CROPLAND, getValueForCol(rowValues, "forT_to_crop") * conversionFactor); + item.setWoodYield(LandCoverType.TIMBER_FOREST, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forT_to_forT") * conversionFactor); + + item.setWoodYield(LandCoverType.CARBON_FOREST, LandCoverType.PASTURE, getValueForCol(rowValues, "forC_to_past") * conversionFactor); + item.setWoodYield(LandCoverType.CARBON_FOREST, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "forC_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.CARBON_FOREST, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forC_to_forT") * conversionFactor); + item.setWoodYield(LandCoverType.CARBON_FOREST, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "forC_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.CARBON_FOREST, LandCoverType.CROPLAND, getValueForCol(rowValues, "forC_to_crop") * conversionFactor); + item.setWoodYield(LandCoverType.CARBON_FOREST, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forC_to_forC") * conversionFactor); - item.setWoodYield(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "forT_to_past") * conversionFactor); - item.setWoodYield(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "forT_to_ntrl") * conversionFactor); - item.setWoodYield(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forT_to_forC") * conversionFactor); - item.setWoodYield(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "forT_to_crop") * conversionFactor); - item.setWoodYield(GamsLandCoverType.TIMBER_FOREST, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forT_to_forT") * conversionFactor); - - item.setWoodYield(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.PASTURE, getValueForCol(rowValues, "forC_to_past") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.NATURAL, getValueForCol(rowValues, "forC_to_ntrl") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "forC_to_forT") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.CROPLAND, getValueForCol(rowValues, "forC_to_crop") * conversionFactor); - item.setWoodYield(GamsLandCoverType.CARBON_FOREST, GamsLandCoverType.CARBON_FOREST, getValueForCol(rowValues, "forC_to_forC") * conversionFactor); - + item.setWoodYield(LandCoverType.UNMANAGED_FOREST, LandCoverType.PASTURE, getValueForCol(rowValues, "ntrl_to_past") * conversionFactor); + item.setWoodYield(LandCoverType.UNMANAGED_FOREST, LandCoverType.TIMBER_FOREST, getValueForCol(rowValues, "ntrl_to_forT") * conversionFactor); + item.setWoodYield(LandCoverType.UNMANAGED_FOREST, LandCoverType.CARBON_FOREST, getValueForCol(rowValues, "ntrl_to_forC") * conversionFactor); + item.setWoodYield(LandCoverType.UNMANAGED_FOREST, LandCoverType.UNMANAGED_FOREST, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); + item.setWoodYield(LandCoverType.UNMANAGED_FOREST, LandCoverType.CROPLAND, getValueForCol(rowValues, "ntrl_to_crop") * conversionFactor); + item.setWoodYield(LandCoverType.UNMANAGED_FOREST, LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "ntrl_to_ntrl") * conversionFactor); } } diff --git a/src/ac/ed/lurg/types/LandCoverType.java b/src/ac/ed/lurg/types/LandCoverType.java index 84277884..4a54b05d 100644 --- a/src/ac/ed/lurg/types/LandCoverType.java +++ b/src/ac/ed/lurg/types/LandCoverType.java @@ -9,23 +9,25 @@ import ac.ed.lurg.utils.LogWriter; public enum LandCoverType { - TIMBER_FOREST("timberForest", true, true), - CARBON_FOREST("carbonForest", true, true), - UNMANAGED_FOREST ("unmanagedForest", true, true), - OTHER_NATURAL ("otherNatural", true, true), - CROPLAND ("cropland", false, true), - PASTURE ("pasture", false, true), - BARREN ("barren", false, false), - URBAN("urban", false, false); + TIMBER_FOREST("timberForest", true, true, true), + CARBON_FOREST("carbonForest", true, true, true), + UNMANAGED_FOREST ("unmanagedForest", true, true, true), + OTHER_NATURAL ("otherNatural", true, true, false), + CROPLAND ("cropland", false, true, false), + PASTURE ("pasture", false, true, false), + BARREN ("barren", false, false, false), + URBAN("urban", false, false, false); private String name; private boolean isProtectable; private boolean isConvertible; + private boolean isForest; - LandCoverType(String name, boolean isProtectable, boolean isConvertible) { + LandCoverType(String name, boolean isProtectable, boolean isConvertible, boolean isForest) { this.name = name; this.isProtectable = isProtectable; this.isConvertible = isConvertible; + this.isForest = isForest; } public String getName() { @@ -77,5 +79,17 @@ public enum LandCoverType { return protectibleTypes; } + + public static Collection<LandCoverType> getConvertibleNonForestTypes() { + + Collection<LandCoverType> convNonforestTypes = new HashSet<LandCoverType>(); + + for (LandCoverType c : values()) + if (!c.isForest & c.isConvertible) + convNonforestTypes.add(c); + + return convNonforestTypes; + + } } diff --git a/src/ac/ed/lurg/utils/DoubleMap.java b/src/ac/ed/lurg/utils/DoubleMap.java index c94b232b..14f7fdcf 100644 --- a/src/ac/ed/lurg/utils/DoubleMap.java +++ b/src/ac/ed/lurg/utils/DoubleMap.java @@ -41,8 +41,12 @@ public class DoubleMap<K, L, V> { public void addTo(K key1, L key2, double value) { if (theMap.containsKey(key1)) { - double current = get(key1, key2); - put(key1, key2, current + value); + if (theMap.get(key1).containsKey(key2)) { + double current = get(key1, key2); + put(key1, key2, current + value); + } else { + theMap.get(key1).put(key2, value); + } } else { put(key1, key2, value); } -- GitLab