diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java index c9e9c175dd7d8d7fc104f634afb9f96ee2e31af3..cbde493f792cba63cf707bf84795014fb3f4b292 100644 --- a/src/ac/ed/lurg/ModelMain.java +++ b/src/ac/ed/lurg/ModelMain.java @@ -6,10 +6,11 @@ import java.util.Collection; import ac.ed.lurg.country.CountryAgent; import ac.ed.lurg.country.CountryAgentCreator; import ac.ed.lurg.demand.DemandManager; +import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.ModelFitType; import ac.ed.lurg.types.YieldType; import ac.ed.lurg.yield.YieldManager; -import ac.ed.lurg.yield.YieldResponse; +import ac.ed.lurg.yield.YieldResponses; import ac.ed.lurg.yield.YieldResponseMapReader; import ac.sac.raster.RasterSet; @@ -42,7 +43,7 @@ public class ModelMain { } private void doTimestep(int timestep) { - RasterSet<YieldResponse> yieldSurfaces = getYieldSurfaces(timestep); + RasterSet<YieldResponses> yieldSurfaces = getYieldSurfaces(timestep); for (CountryAgent ca : countryAgents) { ca.determineProduction(timestep); @@ -51,15 +52,16 @@ public class ModelMain { // examine global trade balance } - private RasterSet<YieldResponse> getYieldSurfaces(int timestep) { + private RasterSet<YieldResponses> getYieldSurfaces(int timestep) { String rootDir = ModelConfig.YIELD_DIR + File.separator + (timestep + ModelConfig.START_TIMESTEP) + File.separator; - RasterSet<YieldResponse> yieldSurfaces = null; + RasterSet<YieldResponses> yieldSurfaces = null; - for (YieldType yieldType : YieldType.values()) { - YieldResponseMapReader yieldReader = new YieldResponseMapReader(yieldSurfaces, yieldType); - yieldSurfaces = yieldReader.getRasterDataFromFile(rootDir + yieldType.getFileName()); + for (CropType cropType : CropType.values()) { + for (YieldType yieldType : YieldType.values()) { + YieldResponseMapReader yieldReader = new YieldResponseMapReader(yieldSurfaces, yieldType, cropType); + yieldSurfaces = yieldReader.getRasterDataFromFile(rootDir + yieldType.getFileName()); + } } - return yieldSurfaces; } } diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index 7fc2bcb6db010fab943942fa86e146a502564447..8bcdf33483e7651e029655efcefd15379f314fa7 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -5,14 +5,15 @@ import java.util.Map; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.ModelContext; -import ac.ed.lurg.country.gams.GamsInputData; +import ac.ed.lurg.country.gams.GamsCountryInput; +import ac.ed.lurg.country.gams.GamsInput; import ac.ed.lurg.country.gams.GamsLandUseOptimiser; -import ac.ed.lurg.country.gams.GamsOutputData; +import ac.ed.lurg.country.gams.GamsOutput; import ac.ed.lurg.types.CropType; import ac.ed.lurg.utils.LogWriter; -import ac.ed.lurg.yield.YieldResponse; +import ac.ed.lurg.yield.YieldResponses; -public class CountryAgent implements GamsInputData { +public class CountryAgent implements GamsInput, GamsCountryInput { private ModelContext modelContext; private Country country; @@ -60,7 +61,7 @@ public class CountryAgent implements GamsInputData { GamsLandUseOptimiser opti = new GamsLandUseOptimiser(this); LogWriter.println("Running " + country.getCountryName() + ", year " + year); - GamsOutputData result = opti.run(); + GamsOutput result = opti.run(); // cropAreasTimeseries.put(timestep, result); } @@ -70,7 +71,7 @@ public class CountryAgent implements GamsInputData { } @Override - public Map<Integer, Map<CropType, YieldResponse>> getYields() { + public Map<Integer, YieldResponses> getYields() { // Map<Integer, Map<CropType, YieldResponse>> returnMap = new HashMap<Integer, Map<CropType, YieldResponse>>(); // for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) @@ -167,4 +168,9 @@ public class CountryAgent implements GamsInputData { public double getMinFeedRate() { return 0.15; } + + @Override + public GamsCountryInput getCountryInput() { + return this; + } } \ No newline at end of file diff --git a/src/ac/ed/lurg/country/LandUseData.java b/src/ac/ed/lurg/country/LandUseData.java deleted file mode 100644 index 4e4e10858da48f00f77db54d3a9c8c39b0664a47..0000000000000000000000000000000000000000 --- a/src/ac/ed/lurg/country/LandUseData.java +++ /dev/null @@ -1,33 +0,0 @@ -package ac.ed.lurg.country; - -public class LandUseData { - - private double area; - private double intensity; - public double feedAmount; - public double netImports; - - public LandUseData(double area, double intensity, double feedAmount, double netImports) { - super(); - this.area = area; - this.intensity = intensity; - this.feedAmount = feedAmount; - this.netImports = netImports; - } - - public double getArea() { - return area; - } - - public double getIntensity() { - return intensity; - } - - public double getFeedAmount() { - return feedAmount; - } - - public double getNetImports() { - return netImports; - } -} diff --git a/src/ac/ed/lurg/country/gams/GamsInputData.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java similarity index 71% rename from src/ac/ed/lurg/country/gams/GamsInputData.java rename to src/ac/ed/lurg/country/gams/GamsCountryInput.java index e1aeea0411b9203f3838bbfc25d285833f692f4c..eca14dab3db22a77ce874307b89bd80486a15303 100644 --- a/src/ac/ed/lurg/country/gams/GamsInputData.java +++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java @@ -3,13 +3,9 @@ package ac.ed.lurg.country.gams; import java.util.Map; import ac.ed.lurg.types.CropType; -import ac.ed.lurg.yield.YieldResponse; -public interface GamsInputData { - - Map<Integer, Map<CropType, YieldResponse>> getYields(); - Map<Integer, Map<CropType, Double>> getPreviousCropArea(); - +public interface GamsCountryInput { + Map<CropType, Double> getProjectedDemand(); Map<CropType, Double> getWorldInputEnergy(); Map<CropType, Double> getMaxNetImport(); @@ -23,4 +19,5 @@ public interface GamsInputData { double getMinFeedRate(); double getTradeBarrier(); double getLandChangeEnergy(); + } diff --git a/src/ac/ed/lurg/country/gams/GamsInput.java b/src/ac/ed/lurg/country/gams/GamsInput.java new file mode 100644 index 0000000000000000000000000000000000000000..c2b77650ae89db778597ab413df815b70d5127e5 --- /dev/null +++ b/src/ac/ed/lurg/country/gams/GamsInput.java @@ -0,0 +1,14 @@ +package ac.ed.lurg.country.gams; + +import java.util.Map; + +import ac.ed.lurg.types.CropType; +import ac.ed.lurg.yield.YieldResponses; + +public interface GamsInput { + + Map<Integer, YieldResponses> getYields(); + Map<Integer, Map<CropType, Double>> getPreviousCropArea(); + + GamsCountryInput getCountryInput(); +} diff --git a/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java index ae269a8c5a43a14b9684bd83ed13ff6ca72f31e2..d5a5835ca87dbfcc2ae44c7acd772ef329a9740d 100644 --- a/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java @@ -8,10 +8,10 @@ import java.util.Vector; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.landuse.LandUseDataPoint; -import ac.ed.lurg.landuse.LandUseItem; +import ac.ed.lurg.landuse.LandUse; import ac.ed.lurg.types.CropType; import ac.ed.lurg.utils.LogWriter; -import ac.ed.lurg.yield.YieldResponse; +import ac.ed.lurg.yield.YieldResponses; import com.gams.api.GAMSDatabase; import com.gams.api.GAMSGlobals; @@ -25,13 +25,13 @@ import com.gams.api.GAMSWorkspaceInfo; public class GamsLandUseOptimiser { - private GamsInputData inputData; + private GamsInput inputData; - public GamsLandUseOptimiser(GamsInputData inputData) { + public GamsLandUseOptimiser(GamsInput inputData) { this.inputData = inputData; } - public GamsOutputData run() { + public GamsOutput run() { File workingDirectory = new File(ModelConfig.TEMP_DIR, "GamsTest"); workingDirectory.mkdir(); @@ -63,7 +63,7 @@ public class GamsLandUseOptimiser { LogWriter.println("\nDemand"); param = inDB.addParameter("demand", 1, "demand for crop"); - addItemMapParm(param, inputData.getProjectedDemand()); + addItemMapParm(param, inputData.getCountryInput().getProjectedDemand()); LogWriter.println("\nYieldNone"); GAMSParameter yNoneP = inDB.addParameter("yieldNone", 2); @@ -73,45 +73,46 @@ public class GamsLandUseOptimiser { GAMSParameter fert_p = inDB.addParameter("fertParam", 2); GAMSParameter irrig_p = inDB.addParameter("irrigParam", 2); - for (Entry<Integer, Map<CropType, YieldResponse>> cropsForALocation : inputData.getYields().entrySet()) { + for (Entry<Integer, YieldResponses> cropsForALocation : inputData.getYields().entrySet()) { Integer locationId = cropsForALocation.getKey(); - Map<CropType, YieldResponse> itemMap = cropsForALocation.getValue(); + YieldResponses yresp = cropsForALocation.getValue(); - for (Map.Entry<CropType, YieldResponse> entry : itemMap.entrySet()) { - LogWriter.println(String.format(" %15s,\t %.1f", entry.getKey().getGamsName(), entry.getValue().getYieldNone())); + for (CropType crop : CropType.values()) { + LogWriter.println(String.format(" %15s,\t %.1f", crop.getGamsName(), yresp.getYieldNone(crop))); + Vector<String> v = new Vector<String>(); - v.add(entry.getKey().getGamsName()); + v.add(crop.getGamsName()); v.add(Integer.toString(locationId)); - yNoneP.addRecord(v).setValue(entry.getValue().getYieldNone()); - y_fert.addRecord(v).setValue(entry.getValue().getYieldFertOnly()); - y_irrig.addRecord(v).setValue(entry.getValue().getYieldIrrigOnly()); - y_both.addRecord(v).setValue(entry.getValue().getYieldMax()); - fert_p.addRecord(v).setValue(entry.getValue().getFertParam()); - irrig_p.addRecord(v).setValue(entry.getValue().getIrrigParam()); + yNoneP.addRecord(v).setValue(yresp.getYieldNone(crop)); + y_fert.addRecord(v).setValue(yresp.getYieldFertOnly(crop)); + y_irrig.addRecord(v).setValue(yresp.getYieldIrrigOnly(crop)); + y_both.addRecord(v).setValue(yresp.getYieldMax(crop)); + fert_p.addRecord(v).setValue(yresp.getFertParam(crop)); + irrig_p.addRecord(v).setValue(yresp.getIrrigParam(crop)); } } LogWriter.println("\nWorld input energy"); param = inDB.addParameter("world_input_energy", 1, "average input energy from world exports used to determine if we should import or export energy per t"); - addItemMapParm(param, inputData.getWorldInputEnergy()); + addItemMapParm(param, inputData.getCountryInput().getWorldInputEnergy()); LogWriter.println("\nMax Net Import"); param = inDB.addParameter("maxNetImport", 1, "max net imports for each crop based on world market"); - addItemMapParm(param, inputData.getMaxNetImport()); + addItemMapParm(param, inputData.getCountryInput().getMaxNetImport()); LogWriter.println("\nMin Net Import"); param = inDB.addParameter("minNetImport", 1, "min net imports for each crop based on world market"); - addItemMapParm(param, inputData.getMinNetImport()); + addItemMapParm(param, inputData.getCountryInput().getMinNetImport()); - addScalar(inDB.addParameter("meatEfficency", 0, "efficency of converting feed and pasture into animal products"), inputData.getMeatEfficiency()); - addScalar(inDB.addParameter("maxLandUseChange", 0, "max rate of land use change"), inputData.getMaxLandUseChange()); - addScalar(inDB.addParameter("tradeBarrier", 0, "trade barrier which adjust energy cost of imports"), inputData.getTradeBarrier()); - addScalar(inDB.addParameter("landChangeEnergy", 0, "energy required to add ha of agricultural land"), inputData.getLandChangeEnergy()); - addScalar(inDB.addParameter("minFeedRate", 0, "minimum rate of feed for producing animal products"), inputData.getMinFeedRate()); + addScalar(inDB.addParameter("meatEfficency", 0, "efficency of converting feed and pasture into animal products"), inputData.getCountryInput().getMeatEfficiency()); + addScalar(inDB.addParameter("maxLandUseChange", 0, "max rate of land use change"), inputData.getCountryInput().getMaxLandUseChange()); + addScalar(inDB.addParameter("tradeBarrier", 0, "trade barrier which adjust energy cost of imports"), inputData.getCountryInput().getTradeBarrier()); + addScalar(inDB.addParameter("landChangeEnergy", 0, "energy required to add ha of agricultural land"), inputData.getCountryInput().getLandChangeEnergy()); + addScalar(inDB.addParameter("minFeedRate", 0, "minimum rate of feed for producing animal products"), inputData.getCountryInput().getMinFeedRate()); } - private GamsOutputData handleResults(GAMSDatabase outDB) { + private GamsOutput handleResults(GAMSDatabase outDB) { int modelStatus = (int) outDB.getParameter("ms").findRecord().getValue(); System.out.println( "\nModelstatus: " + GAMSGlobals.ModelStat.lookup( modelStatus ) + @@ -127,7 +128,7 @@ public class GamsLandUseOptimiser { double totalArea = 0; double area, fertIntensity, irrigIntensity, otherIntensity, feedAmount, netImport; - Map<Integer, LandUseItem> landuses = new HashMap<Integer, LandUseItem>(); + Map<Integer, LandUse> landuses = new HashMap<Integer, LandUse>(); Map<CropType, Double> feedAmounts = new HashMap<CropType, Double>(); Map<CropType, Double> netImports = new HashMap<CropType, Double>(); @@ -153,9 +154,9 @@ public class GamsLandUseOptimiser { LogWriter.println(String.format("\t location %s:\tarea= %.1f,\tfert= %.3f,\tirrg= %.3f,\tintensity= %.3f", locationName, area, fertIntensity, irrigIntensity, otherIntensity)); - LandUseItem luItem = landuses.get(locId); + LandUse luItem = landuses.get(locId); if (luItem == null) { - luItem = new LandUseItem(); + luItem = new LandUse(); landuses.put(locId, luItem); } @@ -168,7 +169,7 @@ public class GamsLandUseOptimiser { LogWriter.println(String.format("\nTotal area= %.1f", totalArea)); //cleanup(ws.workingDirectory()); - GamsOutputData results = new GamsOutputData(modelStatus, landuses, feedAmounts, netImports); + GamsOutput results = new GamsOutput(modelStatus, landuses, feedAmounts, netImports); return results ; } diff --git a/src/ac/ed/lurg/country/gams/GamsOutputData.java b/src/ac/ed/lurg/country/gams/GamsOutput.java similarity index 61% rename from src/ac/ed/lurg/country/gams/GamsOutputData.java rename to src/ac/ed/lurg/country/gams/GamsOutput.java index 527dc6d9377190353782fb25e676232b36ed5954..ed1fe9406a3f0bd1d61def5d912ab5339cb0ccbc 100644 --- a/src/ac/ed/lurg/country/gams/GamsOutputData.java +++ b/src/ac/ed/lurg/country/gams/GamsOutput.java @@ -2,18 +2,18 @@ package ac.ed.lurg.country.gams; import java.util.Map; -import ac.ed.lurg.landuse.LandUseItem; +import ac.ed.lurg.landuse.LandUse; import ac.ed.lurg.types.CropType; -public class GamsOutputData { +public class GamsOutput { int status; - Map<Integer, LandUseItem> landuses; // data mapped from id (not raster) + Map<Integer, LandUse> landuses; // data mapped from id (not raster) Map<CropType, Double> feedAmounts; Map<CropType, Double> netImports; - public GamsOutputData(int status, Map<Integer, LandUseItem> landuses, Map<CropType, Double> feedAmounts, Map<CropType, Double> netImports) { + public GamsOutput(int status, Map<Integer, LandUse> landuses, Map<CropType, Double> feedAmounts, Map<CropType, Double> netImports) { super(); this.status = status; this.landuses = landuses; @@ -24,7 +24,7 @@ public class GamsOutputData { public int getStatus() { return status; } - public Map<Integer, LandUseItem> getLanduses() { + public Map<Integer, LandUse> getLanduses() { return landuses; } public Map<CropType, Double> getFeedAmounts() { diff --git a/src/ac/ed/lurg/country/gams/GamsRasterInput.java b/src/ac/ed/lurg/country/gams/GamsRasterInput.java new file mode 100644 index 0000000000000000000000000000000000000000..8ca8a301bf7c9f9a3578d02c02cd130215f837fb --- /dev/null +++ b/src/ac/ed/lurg/country/gams/GamsRasterInput.java @@ -0,0 +1,13 @@ +package ac.ed.lurg.country.gams; + +import ac.ed.lurg.landuse.LandUse; +import ac.ed.lurg.yield.YieldResponses; +import ac.sac.raster.RasterSet; + +public interface GamsRasterInput { + + RasterSet<YieldResponses> getYields(); + RasterSet<LandUse> getPreviousLandUse(); + + GamsCountryInput getCountryInput(); +} diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java new file mode 100644 index 0000000000000000000000000000000000000000..0d573b83b194d2b1411b80d012c88b80bc9cd7e6 --- /dev/null +++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java @@ -0,0 +1,24 @@ +package ac.ed.lurg.country.gams; + +public class GamsRasterOptimiser { + + private GamsRasterInput inputData; + + public GamsRasterOptimiser(GamsRasterInput inputData) { + this.inputData = inputData; + } + + public GamsRasterOutput run() { + // workout similar areas + + // map from raster to combined location ids + + // create GamsInput using mapping + + // run optimizer + + // map results back to raster + + return null; + } +} diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOutput.java b/src/ac/ed/lurg/country/gams/GamsRasterOutput.java new file mode 100644 index 0000000000000000000000000000000000000000..d29d37d69c5cbf510f5da702fd5682a264e5f91a --- /dev/null +++ b/src/ac/ed/lurg/country/gams/GamsRasterOutput.java @@ -0,0 +1,38 @@ +package ac.ed.lurg.country.gams; + +import java.util.Map; + +import ac.ed.lurg.landuse.LandUse; +import ac.ed.lurg.types.CropType; +import ac.sac.raster.RasterSet; + +public class GamsRasterOutput { + int status; + + RasterSet<LandUse> landuses; + + Map<CropType, Double> feedAmounts; + Map<CropType, Double> netImports; + + public GamsRasterOutput(int status, RasterSet<LandUse> landuses, Map<CropType, Double> feedAmounts, Map<CropType, Double> netImports) { + super(); + this.status = status; + this.landuses = landuses; + this.feedAmounts = feedAmounts; + this.netImports = netImports; + } + + public int getStatus() { + return status; + } + public RasterSet<LandUse> getLanduses() { + return landuses; + } + public Map<CropType, Double> getFeedAmounts() { + return feedAmounts; + } + public Map<CropType, Double> getNetImports() { + return netImports; + } + +} diff --git a/src/ac/ed/lurg/country/gams/GamsTest.java b/src/ac/ed/lurg/country/gams/GamsTest.java index 6829e56260c49793e2e068386229eb3f95041909..e5e1c8d448f447e4646e59b839a69943a2e811a3 100644 --- a/src/ac/ed/lurg/country/gams/GamsTest.java +++ b/src/ac/ed/lurg/country/gams/GamsTest.java @@ -6,9 +6,9 @@ import java.util.Map; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.YieldType; -import ac.ed.lurg.yield.YieldResponse; +import ac.ed.lurg.yield.YieldResponses; -public class GamsTest implements GamsInputData { +public class GamsTest implements GamsInput, GamsCountryInput { public static void main(String[] args) { GamsTest aGamsTest = new GamsTest(); @@ -36,25 +36,23 @@ public class GamsTest implements GamsInputData { @Override - public Map<Integer, Map<CropType, YieldResponse>> getYields() { - Map<CropType, YieldResponse> dummyMap = new HashMap<CropType, YieldResponse>(); + public Map<Integer, YieldResponses> getYields() { - YieldResponse dummyYields = new YieldResponse(); - dummyYields.setYield(YieldType.NONE, 1); - dummyYields.setYield(YieldType.FERT_MID_ONLY, 2.4); - dummyYields.setYield(YieldType.FERT_MAX_ONLY, 3); - dummyYields.setYield(YieldType.IRRIG_MID_ONLY, 1.8); - dummyYields.setYield(YieldType.IRRIG_MAX_ONLY, 2); - dummyYields.setYield(YieldType.FERT_IRRIG_MAX, 4); - + YieldResponses yresp = new YieldResponses(); - for (CropType crop : CropType.getAllItems()) - dummyMap.put(crop, dummyYields); + for (CropType crop : CropType.getAllItems()) { + yresp.setYield(YieldType.NONE, crop, 1); + yresp.setYield(YieldType.FERT_MID_ONLY, crop, 2.4); + yresp.setYield(YieldType.FERT_MAX_ONLY, crop, 3); + yresp.setYield(YieldType.IRRIG_MID_ONLY, crop, 1.8); + yresp.setYield(YieldType.IRRIG_MAX_ONLY, crop, 2); + yresp.setYield(YieldType.FERT_IRRIG_MAX, crop, 4); + } - Map<Integer, Map<CropType, YieldResponse>> returnMap = new HashMap<Integer, Map<CropType, YieldResponse>>(); + Map<Integer, YieldResponses> returnMap = new HashMap<Integer, YieldResponses>(); for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) - returnMap.put(i, dummyMap); + returnMap.put(i, yresp); return returnMap; } @@ -150,4 +148,9 @@ public class GamsTest implements GamsInputData { public double getMinFeedRate() { return 0.15; } + + @Override + public GamsCountryInput getCountryInput() { + return this; + } } diff --git a/src/ac/ed/lurg/landuse/LandUseItem.java b/src/ac/ed/lurg/landuse/LandUse.java similarity index 92% rename from src/ac/ed/lurg/landuse/LandUseItem.java rename to src/ac/ed/lurg/landuse/LandUse.java index 51db7b6209c6d5d4b2a03f63ab11ce81651f78ef..792b20bd76381562d30471c98adc647300ae6330 100644 --- a/src/ac/ed/lurg/landuse/LandUseItem.java +++ b/src/ac/ed/lurg/landuse/LandUse.java @@ -6,7 +6,7 @@ import java.util.Map; import ac.ed.lurg.types.CropType; import ac.sac.raster.RasterItem; -public class LandUseItem implements RasterItem { +public class LandUse implements RasterItem { Map<CropType, LandUseDataPoint> landUses = new HashMap<CropType, LandUseDataPoint>(); @@ -39,7 +39,7 @@ public class LandUseItem implements RasterItem { return false; if (getClass() != obj.getClass()) return false; - LandUseItem other = (LandUseItem) obj; + LandUse other = (LandUse) obj; if (landUses == null) { if (other.landUses != null) return false; diff --git a/src/ac/ed/lurg/landuse/LandUseSet.java b/src/ac/ed/lurg/landuse/LandUseSet.java index 192d0b52280e2b42ed494a78d1f7453f7c778220..7e7fa475a9a9d8dc216633496c5d9239f2388783 100644 --- a/src/ac/ed/lurg/landuse/LandUseSet.java +++ b/src/ac/ed/lurg/landuse/LandUseSet.java @@ -2,11 +2,11 @@ package ac.ed.lurg.landuse; import ac.sac.raster.RasterSet; -public class LandUseSet extends RasterSet<LandUseItem> { +public class LandUseSet extends RasterSet<LandUse> { private static final long serialVersionUID = -2682317143041974233L; - protected LandUseItem createRasterData() { - return new LandUseItem(); + protected LandUse createRasterData() { + return new LandUse(); } } diff --git a/src/ac/ed/lurg/yield/YieldResponse.java b/src/ac/ed/lurg/yield/YieldResponse.java index 88fcd9bb905602cb9db9494f5dda6aacf301c8dd..6f621e093ff3e32e6233f5f7f57b4a39aa43bd38 100644 --- a/src/ac/ed/lurg/yield/YieldResponse.java +++ b/src/ac/ed/lurg/yield/YieldResponse.java @@ -4,34 +4,28 @@ import java.util.HashMap; import java.util.Map; import ac.ed.lurg.types.YieldType; -import ac.sac.raster.RasterItem; -public class YieldResponse implements RasterItem { +public class YieldResponse { private Map<YieldType, Double> yields = new HashMap<YieldType, Double>(); public void setYield(YieldType type, double yield) { yields.put(type, yield); } - public double getYieldNone() { - return yields.get(YieldType.NONE); - } - public double getYieldFertOnly() { - return yields.get(YieldType.FERT_MAX_ONLY); - } - public double getYieldIrrigOnly() { - return yields.get(YieldType.IRRIG_MAX_ONLY); - } - public double getYieldMax() { - return yields.get(YieldType.FERT_IRRIG_MAX); - } + public double getYield(YieldType type) { + return yields.get(type); + } public double getFertParam() { double fertMin = 0, fertMid = 0.5, fertMax = 1; - return Math.log((yields.get(YieldType.FERT_MID_ONLY)-getYieldNone())/(getYieldFertOnly()-getYieldNone()))/Math.log((fertMid - fertMin)/(fertMax - fertMin))/fertMax; + + return Math.log((yields.get(YieldType.FERT_MID_ONLY)-getYield(YieldType.NONE))/(yields.get(YieldType.FERT_MAX_ONLY)-getYield(YieldType.NONE))) / + Math.log((fertMid - fertMin)/(fertMax - fertMin))/fertMax; } + public double getIrrigParam() { double irrigMin = 0, irrigMid = 0.5, irrigMax = 1; - return Math.log((yields.get(YieldType.IRRIG_MID_ONLY)-getYieldNone())/(getYieldIrrigOnly()-getYieldNone()))/Math.log((irrigMid - irrigMin)/(irrigMax - irrigMin))/irrigMax; + return Math.log((yields.get(YieldType.IRRIG_MID_ONLY)-getYield(YieldType.NONE))/(yields.get(YieldType.IRRIG_MAX_ONLY)-getYield(YieldType.NONE))) / + Math.log((irrigMid - irrigMin)/(irrigMax - irrigMin))/irrigMax; } } diff --git a/src/ac/ed/lurg/yield/YieldResponseMapReader.java b/src/ac/ed/lurg/yield/YieldResponseMapReader.java index 2fa41a4d92a5ffb078075076e620b0a1704ae675..932f3758b8fa6c4798d95b817ad4ba4fc149c542 100644 --- a/src/ac/ed/lurg/yield/YieldResponseMapReader.java +++ b/src/ac/ed/lurg/yield/YieldResponseMapReader.java @@ -1,37 +1,40 @@ package ac.ed.lurg.yield; +import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.YieldType; import ac.ed.lurg.utils.LogWriter; import ac.sac.raster.AbstractRasterReader; import ac.sac.raster.RasterHeaderDetails; import ac.sac.raster.RasterSet; -public class YieldResponseMapReader extends AbstractRasterReader<YieldResponse> { +public class YieldResponseMapReader extends AbstractRasterReader<YieldResponses> { private YieldType yieldType; + private CropType cropType; - public YieldResponseMapReader (RasterSet<YieldResponse> dataset, YieldType yieldType) { + public YieldResponseMapReader (RasterSet<YieldResponses> dataset, YieldType yieldType, CropType cropType) { super(dataset); this.yieldType = yieldType; + this.cropType = cropType; } protected void createDataSet(RasterHeaderDetails header) { if (DEBUG) LogWriter.println("Creating RasterDataset col:"+header.getNcolumns() + ", rows:" + header.getNrows()); if (dataset == null) { - dataset = new RasterSet<YieldResponse>(header) { + dataset = new RasterSet<YieldResponses>(header) { private static final long serialVersionUID = -202195565674146064L; - protected YieldResponse createRasterData() { - return new YieldResponse(); + protected YieldResponses createRasterData() { + return new YieldResponses(); } }; } } @Override - public void setData(YieldResponse item, String token) { + public void setData(YieldResponses item, String token) { double d = Double.parseDouble(token); - item.setYield(yieldType, d); + item.setYield(yieldType, cropType, d); } } \ No newline at end of file diff --git a/src/ac/ed/lurg/yield/YieldResponses.java b/src/ac/ed/lurg/yield/YieldResponses.java new file mode 100644 index 0000000000000000000000000000000000000000..9f19a38cbb321d43353c38bc0ccad6c4328b2b84 --- /dev/null +++ b/src/ac/ed/lurg/yield/YieldResponses.java @@ -0,0 +1,49 @@ +package ac.ed.lurg.yield; + +import java.util.HashMap; +import java.util.Map; + +import ac.ed.lurg.types.CropType; +import ac.ed.lurg.types.YieldType; +import ac.sac.raster.RasterItem; + +public class YieldResponses implements RasterItem { + private Map<CropType, YieldResponse> yieldResponses = new HashMap<CropType, YieldResponse>(); + + public void setYield(YieldType type, CropType crop, double yield) { + getYieldResponseForCrop(crop).setYield(type, yield); + } + + private YieldResponse getYieldResponseForCrop(CropType crop) { + YieldResponse yresp = yieldResponses.get(crop); + if (yresp == null) { + yresp = new YieldResponse(); + yieldResponses.put(crop, yresp); + } + return yresp; + } + + public double getYieldNone(CropType crop) { + return getYieldResponseForCrop(crop).getYield(YieldType.NONE); + } + + public double getYieldFertOnly(CropType crop) { + return getYieldResponseForCrop(crop).getYield(YieldType.FERT_MAX_ONLY); + } + + public double getYieldIrrigOnly(CropType crop) { + return getYieldResponseForCrop(crop).getYield(YieldType.IRRIG_MAX_ONLY); + } + + public double getYieldMax(CropType crop) { + return getYieldResponseForCrop(crop).getYield(YieldType.FERT_IRRIG_MAX); + } + + public double getFertParam(CropType crop) { + return getYieldResponseForCrop(crop).getFertParam(); + } + + public double getIrrigParam(CropType crop) { + return getYieldResponseForCrop(crop).getIrrigParam(); + } +} \ No newline at end of file