Skip to content
Snippets Groups Projects
Commit 30e5ddd9 authored by Peter Alexander's avatar Peter Alexander
Browse files

no message

parent 48609f7b
No related branches found
No related tags found
No related merge requests found
...@@ -93,7 +93,8 @@ public class ModelConfig { ...@@ -93,7 +93,8 @@ public class ModelConfig {
public static final String SSP_FILE = DATA_DIR + File.separator + "ssp.csv"; public static final String SSP_FILE = DATA_DIR + File.separator + "ssp.csv";
public static final String YIELD_DIR = DATA_DIR + File.separator + "yields"; public static final String YIELD_DIR = DATA_DIR + File.separator + "yields";
public static final String BASELINE_CONSUMP_FILE = DATA_DIR + File.separator + "base_consump.csv"; public static final String BASELINE_CONSUMP_FILE = DATA_DIR + File.separator + "base_consump.csv";
public static final String INITAL_LAND_COVER_DIR = DATA_DIR + File.separator + "initLandUse";
public static final String COUNTRY_BOUNDARY_FILE = DATA_DIR + File.separator + "country_boundaries.test.asc"; public static final String COUNTRY_BOUNDARY_FILE = DATA_DIR + File.separator + "country_boundaries.test.asc";
public static final String COUNTRY_CODES_FILE = DATA_DIR + File.separator + "country_codes3.csv"; public static final String COUNTRY_CODES_FILE = DATA_DIR + File.separator + "country_codes3.csv";
......
...@@ -4,7 +4,7 @@ import ac.ed.lurg.demand.DemandManager; ...@@ -4,7 +4,7 @@ import ac.ed.lurg.demand.DemandManager;
public class ModelContext { public class ModelContext {
private DemandManager demandManager; private DemandManager demandManager;
public ModelContext(DemandManager demandManager) { public ModelContext(DemandManager demandManager) {
super(); super();
this.demandManager = demandManager; this.demandManager = demandManager;
......
...@@ -4,16 +4,19 @@ import java.io.File; ...@@ -4,16 +4,19 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import ac.ed.lurg.country.Country; import ac.ed.lurg.country.Country;
import ac.ed.lurg.country.CountryAgent; import ac.ed.lurg.country.CountryAgent;
import ac.ed.lurg.country.CountryAgentCreator;
import ac.ed.lurg.country.CountryBoundaryItem; import ac.ed.lurg.country.CountryBoundaryItem;
import ac.ed.lurg.country.CountryBoundaryReader; import ac.ed.lurg.country.CountryBoundaryReader;
import ac.ed.lurg.demand.DemandManager; import ac.ed.lurg.demand.DemandManager;
import ac.ed.lurg.landuse.LandCoverItem;
import ac.ed.lurg.landuse.LandCoverReader;
import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.CropType;
import ac.ed.lurg.types.LandDataType;
import ac.ed.lurg.types.ModelFitType; import ac.ed.lurg.types.ModelFitType;
import ac.ed.lurg.types.YieldType; import ac.ed.lurg.types.YieldType;
import ac.ed.lurg.utils.LogWriter; import ac.ed.lurg.utils.LogWriter;
...@@ -27,77 +30,106 @@ public class ModelMain { ...@@ -27,77 +30,106 @@ public class ModelMain {
private Collection<CountryAgent> countryAgents; private Collection<CountryAgent> countryAgents;
private ModelContext modelContext; private ModelContext modelContext;
private Map<Country, List<RasterKey>> countryToKeysMap; private Map<Country, List<RasterKey>> countryToKeysMap;
public static void main(String[] args) { public static void main(String[] args) {
ModelMain theModel = new ModelMain(); ModelMain theModel = new ModelMain();
theModel.setup(); theModel.setup();
theModel.run(); theModel.run();
} }
/* setup models, reading inputs, etc. */ /* setup models, reading inputs, etc. */
private void setup() { private void setup() {
DemandManager demandManager = new DemandManager(ModelFitType.LOGISTIC, "SSP2_v9_130325"); DemandManager demandManager = new DemandManager(ModelFitType.LOGISTIC, "SSP2_v9_130325");
modelContext = new ModelContext(demandManager); modelContext = new ModelContext(demandManager);
countryToKeysMap = getCountryToKeysMap(); countryToKeysMap = getCountryToKeysMap();
countryAgents = getAgents();
}
countryAgents = new CountryAgentCreator().getAgents(modelContext); /* run the model */
private void run() {
for (int i = ModelConfig.START_TIMESTEP; i <= ModelConfig.END_TIMESTEP; i++)
doTimestep(i);
} }
private Map<Country, List<RasterKey>> getCountryToKeysMap() { private void doTimestep(int timestep) {
YieldRaster yieldSurfaces = getYieldSurfaces(timestep);
LogWriter.println("Timestep " + timestep);
for (CountryAgent ca : countryAgents) {
LogWriter.println("Country " + ca.getCountry());
Collection<RasterKey> countryKeys = countryToKeysMap.get(ca.getCountry());
YieldRaster countryYieldSurfaces = yieldSurfaces.getSubsetRasterForKeys(countryKeys);
ca.determineProduction(timestep, countryYieldSurfaces);
}
// examine global trade balance
}
public Map<Country, List<RasterKey>> getCountryToKeysMap() {
CountryBoundaryReader countryReader = new CountryBoundaryReader(); CountryBoundaryReader countryReader = new CountryBoundaryReader();
RasterSet<CountryBoundaryItem> countryBoundaries = countryReader.getRasterDataFromFile(ModelConfig.COUNTRY_BOUNDARY_FILE); RasterSet<CountryBoundaryItem> countryBoundaries = countryReader.getRasterDataFromFile(ModelConfig.COUNTRY_BOUNDARY_FILE);
Map<Country, List<RasterKey>> countryToKeysMap = new HashMap<Country, List<RasterKey>>(); Map<Country, List<RasterKey>> countryToKeysMap = new HashMap<Country, List<RasterKey>>();
for (Map.Entry<RasterKey, CountryBoundaryItem> entry : countryBoundaries.entrySet()) { for (Map.Entry<RasterKey, CountryBoundaryItem> entry : countryBoundaries.entrySet()) {
List<RasterKey> keys = countryToKeysMap.get(entry.getValue().getCountry()); List<RasterKey> keys = countryToKeysMap.get(entry.getValue().getCountry());
if (keys == null) { if (keys == null) {
keys = new ArrayList<RasterKey>(); keys = new ArrayList<RasterKey>();
countryToKeysMap.put(entry.getValue().getCountry(), keys); countryToKeysMap.put(entry.getValue().getCountry(), keys);
} }
keys.add(entry.getKey()); keys.add(entry.getKey());
} }
return countryToKeysMap; return countryToKeysMap;
} }
/* run the model */
private void run() { public Collection<CountryAgent> getAgents() {
for (int i = ModelConfig.START_TIMESTEP; i <= ModelConfig.END_TIMESTEP; i++) Collection<CountryAgent> countryAgents = new HashSet<CountryAgent>();
doTimestep(i);
RasterSet<LandCoverItem> initLC = getInitialLandCover();
for (Map.Entry<Country, List<RasterKey>> entry : countryToKeysMap.entrySet()) {
Country country = entry.getKey();
List<RasterKey> keys = entry.getValue();
RasterSet<LandCoverItem> initCountryLC = initLC.popSubsetForKeys(new RasterSet<LandCoverItem>(), keys);
CountryAgent ca = new CountryAgent(modelContext, country, initCountryLC);
countryAgents.add(ca);
}
return countryAgents;
} }
private void doTimestep(int timestep) {
YieldRaster yieldSurfaces = getYieldSurfaces(timestep);
LogWriter.println("Timestep " + timestep);
for (CountryAgent ca : countryAgents) {
LogWriter.println("Country " + ca.getCountry()); private RasterSet<LandCoverItem> getInitialLandCover() {
Collection<RasterKey> countryKeys = countryToKeysMap.get(ca.getCountry()); String rootDir = ModelConfig.INITAL_LAND_COVER_DIR + File.separator;
YieldRaster countryYieldSurfaces = yieldSurfaces.getSubsetRasterForKeys(countryKeys); RasterSet<LandCoverItem> initLC = null;
ca.determineProduction(timestep, countryYieldSurfaces);
for (LandDataType landType : LandDataType.values()) {
LandCoverReader lcReader = new LandCoverReader(initLC, landType);
initLC = lcReader.getRasterDataFromFile(rootDir + landType.getFileName());
} }
// examine global trade balance return initLC;
} }
private YieldRaster getYieldSurfaces(int timestep) { private YieldRaster getYieldSurfaces(int timestep) {
String rootDir = ModelConfig.YIELD_DIR + File.separator + (timestep + ModelConfig.BASE_YEAR) + File.separator; String rootDir = ModelConfig.YIELD_DIR + File.separator + (timestep + ModelConfig.BASE_YEAR) + File.separator;
YieldRaster yieldSurfaces = null; YieldRaster yieldSurfaces = null;
for (CropType cropType : CropType.values()) { for (CropType cropType : CropType.values()) {
String cropDir = rootDir + cropType.getGamsName() + File.separator; String cropDir = rootDir + cropType.getGamsName() + File.separator;
for (YieldType yieldType : YieldType.values()) { for (YieldType yieldType : YieldType.values()) {
YieldResponseMapReader yieldReader = new YieldResponseMapReader(yieldSurfaces, yieldType, cropType); YieldResponseMapReader yieldReader = new YieldResponseMapReader(yieldSurfaces, yieldType, cropType);
yieldSurfaces = (YieldRaster)yieldReader.getRasterDataFromFile(cropDir + yieldType.getFileName()); yieldSurfaces = (YieldRaster)yieldReader.getRasterDataFromFile(cropDir + yieldType.getFileName());
} }
} }
return yieldSurfaces; return yieldSurfaces;
} }
} }
...@@ -10,9 +10,13 @@ import ac.ed.lurg.country.gams.GamsRasterInput; ...@@ -10,9 +10,13 @@ import ac.ed.lurg.country.gams.GamsRasterInput;
import ac.ed.lurg.country.gams.GamsRasterOptimiser; import ac.ed.lurg.country.gams.GamsRasterOptimiser;
import ac.ed.lurg.country.gams.GamsRasterOutput; import ac.ed.lurg.country.gams.GamsRasterOutput;
import ac.ed.lurg.landuse.AreasItem; import ac.ed.lurg.landuse.AreasItem;
import ac.ed.lurg.landuse.LandCoverItem;
import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.CropType;
import ac.ed.lurg.types.LandDataType;
import ac.ed.lurg.utils.LogWriter; import ac.ed.lurg.utils.LogWriter;
import ac.ed.lurg.yield.YieldRaster; import ac.ed.lurg.yield.YieldRaster;
import ac.sac.raster.RasterKey;
import ac.sac.raster.RasterSet;
public class CountryAgent { public class CountryAgent {
...@@ -25,11 +29,38 @@ public class CountryAgent { ...@@ -25,11 +29,38 @@ public class CountryAgent {
private Map<CropType, Double> currentProjectedDemand; private Map<CropType, Double> currentProjectedDemand;
private YieldRaster countryYieldSurfaces; private YieldRaster countryYieldSurfaces;
public CountryAgent(ModelContext modelContext, Country country, AreasItem initialCropAreas) { public CountryAgent(ModelContext modelContext, Country country, RasterSet<LandCoverItem> initialLC) {
this.modelContext = modelContext; this.modelContext = modelContext;
this.country = country; this.country = country;
// cropAreasTimeseries.put(0, initialCropAreas);
// landUseAreasTimeseries.put(0, initialLandCover); RasterSet<AreasItem> cropAreaRaster = convertInitialLC(initialLC);
Map<CropType, Double> feedAmounts = null;
Map<CropType, Double> netImports = null;
GamsRasterOutput initialData = new GamsRasterOutput(cropAreaRaster, feedAmounts, netImports);
resultsTimeseries.put(-1, initialData);
}
private RasterSet<AreasItem> convertInitialLC(RasterSet<LandCoverItem> initialLC) {
RasterSet<AreasItem> cropAreaRaster = new RasterSet<AreasItem>();
for (Map.Entry<RasterKey, LandCoverItem> entry : initialLC.entrySet()) {
AreasItem areasItem = new AreasItem();
LandCoverItem landCover = entry.getValue();
areasItem.setCropArea(CropType.CEREALS, 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.LAND, landCover.getLandCover(LandDataType.LAND));
areasItem.setLandCoverArea(LandDataType.FOREST, landCover.getLandCover(LandDataType.FOREST));
areasItem.setLandCoverArea(LandDataType.OTHER_NATURAL, landCover.getLandCover(LandDataType.OTHER_NATURAL));
cropAreaRaster.put(entry.getKey(), areasItem);
}
return cropAreaRaster;
} }
public Country getCountry() { public Country getCountry() {
...@@ -59,7 +90,7 @@ public class CountryAgent { ...@@ -59,7 +90,7 @@ public class CountryAgent {
public GamsRasterInput getGamsRasterInput() { public GamsRasterInput getGamsRasterInput() {
int previousTimestep = currentTimestep==0 ? 0 : currentTimestep-1; int previousTimestep = currentTimestep-1;
GamsRasterOutput prevOutput = resultsTimeseries.get(previousTimestep); GamsRasterOutput prevOutput = resultsTimeseries.get(previousTimestep);
GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport());
......
package ac.ed.lurg.country;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collection;
import java.util.HashSet;
import ac.ed.lurg.ModelConfig;
import ac.ed.lurg.ModelContext;
import ac.ed.lurg.landuse.AreasItem;
import ac.ed.lurg.types.CropType;
import ac.ed.lurg.utils.LogWriter;
public class CountryAgentCreator {
private static final int COUNTRY_COL = 0;
private static final int YEAR_COL = 1;
private static final int LAND_COL = 2;
private static final int ARABLE_COL = 3;
private static final int FOREST_COL = 5;
private static final int PERM_CROP_COL = 6;
private static final int PASTURE_COL = 7;
public Collection<CountryAgent> getAgents(ModelContext modelContext) {
Collection<CountryAgent> countryAgents = new HashSet<CountryAgent>();
String filename = ModelConfig.COUNTRY_DATA_FILE;
try {
BufferedReader fitReader = new BufferedReader(new FileReader(filename));
String line, countryName;
int year;
fitReader.readLine(); // read header
while ((line=fitReader.readLine()) != null) {
String[] tokens = line.split(",");
if (tokens.length < 11)
LogWriter.printlnError("Too few columns in " + filename + ", " + line);
countryName = tokens[COUNTRY_COL];
year = Integer.parseInt(tokens[YEAR_COL]);
if (year != ModelConfig.BASE_YEAR) {
LogWriter.printlnError("Baseline country data year (" + year + ") does not match configured year");
}
double arableArea = Double.parseDouble(tokens[ARABLE_COL])/1000;
double pastureArea = Double.parseDouble(tokens[PASTURE_COL])/1000;
if (pastureArea == 0)
pastureArea = arableArea * 0.05;
// double intensity = Math.sqrt(Double.parseDouble(tokens[FERT_N_COL])/arableArea);
/* LandCoverAreas initialLandCover = new LandCoverAreas();
initialLandCover.setLandCoverArea(LandDataType.LAND, Double.parseDouble(tokens[LAND_COL])/1000);
initialLandCover.setLandCoverArea(LandDataType.ARABLE, arableArea);
initialLandCover.setLandCoverArea(LandDataType.FOREST, Double.parseDouble(tokens[FOREST_COL])/1000);
initialLandCover.setLandCoverArea(LandDataType.PERM_CROP, Double.parseDouble(tokens[PERM_CROP_COL])/1000);
initialLandCover.setLandCoverArea(LandDataType.PASTURE, pastureArea);
*/
AreasItem initialCropData = new AreasItem();
initialCropData.setCropArea(CropType.CEREALS, arableArea); // at the moment can substitute freely between arable crops so this is ok-ish
initialCropData.setCropArea(CropType.MEAT_OR_PASTURE, pastureArea);
CountryAgent ca = new CountryAgent(modelContext, CountryManager.getForName(countryName), initialCropData);
countryAgents.add(ca);
}
fitReader.close();
} catch (Exception e) {
LogWriter.printlnError("Failed in reading commodity demand fits");
e.printStackTrace();
}
LogWriter.println("Processed " + filename + ", create " + countryAgents.size() + " country agents");
return countryAgents;
}
}
...@@ -16,15 +16,19 @@ public class GamsRasterOutput { ...@@ -16,15 +16,19 @@ public class GamsRasterOutput {
private Map<CropType, Double> feedAmounts; private Map<CropType, Double> feedAmounts;
private Map<CropType, Double> netImports; private Map<CropType, Double> netImports;
public GamsRasterOutput(int status, RasterSet<IntensitiesItem> intensityRaster, RasterSet<AreasItem> cropAreaRaster, public GamsRasterOutput(RasterSet<AreasItem> cropAreaRaster, Map<CropType, Double> feedAmounts, Map<CropType, Double> netImports) {
Map<CropType, Double> feedAmounts, Map<CropType, Double> netImports) {
super(); super();
this.status = status;
this.intensityRaster = intensityRaster;
this.cropAreaRaster = cropAreaRaster; this.cropAreaRaster = cropAreaRaster;
this.feedAmounts = feedAmounts; this.feedAmounts = feedAmounts;
this.netImports = netImports; this.netImports = netImports;
} }
public GamsRasterOutput(int status, RasterSet<IntensitiesItem> intensityRaster, RasterSet<AreasItem> cropAreaRaster,
Map<CropType, Double> feedAmounts, Map<CropType, Double> netImports) {
this(cropAreaRaster, feedAmounts, netImports);
this.status = status;
this.intensityRaster = intensityRaster;
}
public int getStatus() { public int getStatus() {
return status; return status;
......
package ac.ed.lurg.landuse;
import java.util.HashMap;
import java.util.Map;
import ac.ed.lurg.types.LandDataType;
import ac.sac.raster.RasterItem;
/** Used to hold less detailed land-cover information
* This is used in the initalisation phase, after that land-use is used */
public class LandCoverItem implements RasterItem {
Map<LandDataType, Double> landcover = new HashMap<LandDataType, Double>();
public Double getLandCover(LandDataType landType) {
return landcover.get(landType);
}
public void setLandCover(LandDataType landType, double d) {
landcover.put(landType, d);
}
/*public double getTotal() {
double total = 0;
for (double d : landcover.values()) {
total += d;
}
return total;
}*/
}
package ac.ed.lurg.landuse;
import ac.ed.lurg.types.LandDataType;
import ac.ed.lurg.utils.LogWriter;
import ac.sac.raster.AbstractRasterReader;
import ac.sac.raster.RasterHeaderDetails;
import ac.sac.raster.RasterSet;
public class LandCoverReader extends AbstractRasterReader<LandCoverItem> {
private LandDataType landType;
public LandCoverReader (RasterSet<LandCoverItem> dataset, LandDataType landType) {
super(dataset);
this.landType = landType;
}
protected void createDataSet(RasterHeaderDetails header) {
if (DEBUG) LogWriter.println("Creating RasterDataset col:"+header.getNcolumns() + ", rows:" + header.getNrows());
if (dataset == null) {
dataset = new RasterSet<LandCoverItem>(header) {
private static final long serialVersionUID = 8005262111782090805L;
protected LandCoverItem createRasterData() {
return new LandCoverItem();
}
};
}
}
@Override
public void setData(LandCoverItem item, String token) {
double d = Double.parseDouble(token);
item.setLandCover(landType, d);
}
}
...@@ -4,8 +4,10 @@ public enum LandDataType { ...@@ -4,8 +4,10 @@ public enum LandDataType {
LAND ("land"), LAND ("land"),
FOREST ("forest"), FOREST ("forest"),
OTHER_NATURAL ("perm_crop"); OTHER_NATURAL ("otherNatural"),
CROPLAND ("cropland"),
PASTURE ("pasture");
private String name; private String name;
LandDataType(String name) { LandDataType(String name) {
...@@ -15,5 +17,9 @@ public enum LandDataType { ...@@ -15,5 +17,9 @@ public enum LandDataType {
public String getName() { public String getName() {
return name; return name;
} }
public String getFileName() {
return name + ".asc";
}
} }
package ac.ed.lurg.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CollectionHelper {
public static <K, V> Map<V, List<K>> invertMap(Map<K, V> map) {
Map<V, List<K>> result = new HashMap<V, List<K>>();
for (Map.Entry<K, V> entry : map.entrySet()) {
if (!result.containsKey(entry.getValue())) {
result.put(entry.getValue(), new ArrayList<K>());
}
result.get(entry.getValue()).add(entry.getKey());
}
return result;
}
}
...@@ -21,11 +21,7 @@ public class YieldRaster extends RasterSet<YieldResponsesItem> { ...@@ -21,11 +21,7 @@ public class YieldRaster extends RasterSet<YieldResponsesItem> {
// not very efficient, we could keep the mapping of country to area somewhere. // not very efficient, we could keep the mapping of country to area somewhere.
public YieldRaster getSubsetRasterForKeys(Collection<RasterKey> keys) { public YieldRaster getSubsetRasterForKeys(Collection<RasterKey> keys) {
YieldRaster subsetYieldRaster = new YieldRaster(this.getHeaderDetails()); YieldRaster subsetYieldRaster = new YieldRaster(this.getHeaderDetails());
popSubsetForKeys(subsetYieldRaster, keys);
for (RasterKey key : keys) {
subsetYieldRaster.put(key, get(key));
}
return subsetYieldRaster; return subsetYieldRaster;
} }
} }
...@@ -110,4 +110,12 @@ public class RasterSet<D extends RasterItem> extends HashMap<RasterKey, D> { ...@@ -110,4 +110,12 @@ public class RasterSet<D extends RasterItem> extends HashMap<RasterKey, D> {
public boolean isConstistentWithHeader(RasterHeaderDetails h) { public boolean isConstistentWithHeader(RasterHeaderDetails h) {
return h.getCellSize() == header.getCellSize(); return h.getCellSize() == header.getCellSize();
} }
public RasterSet<D> popSubsetForKeys(RasterSet<D> subset, Collection<RasterKey> keys) {
for (RasterKey key : keys) {
subset.put(key, get(key));
}
return subset;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment