Newer
Older
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ac.ed.lurg.country.CompositeCountry;
import ac.ed.lurg.country.CompositeCountryManager;
import ac.ed.lurg.country.CountryAgent;
import ac.ed.lurg.country.CountryBoundaryRaster;
import ac.ed.lurg.country.CountryBoundaryReader;
import ac.ed.lurg.country.GlobalPrice;
import ac.ed.lurg.demand.BaseConsumpManager;
import ac.ed.lurg.demand.DemandManager;
import ac.ed.lurg.landuse.CropUsageData;
import ac.ed.lurg.landuse.IrrigationConstraintReader;
import ac.ed.lurg.landuse.IrrigationItem;
import ac.ed.lurg.landuse.IrrigationMaxAmountReader;
import ac.ed.lurg.landuse.IrrigiationCostReader;
import ac.ed.lurg.landuse.LandCoverItem;
import ac.ed.lurg.landuse.LandCoverReader;
import ac.ed.lurg.landuse.LandUseItem;
import ac.ed.lurg.landuse.LandUseReader;
import ac.ed.lurg.landuse.ProtectedAreasReader;
import ac.ed.lurg.output.LandUseOutputer;
import ac.ed.lurg.output.LpjgOutputer;
import ac.ed.lurg.types.LandCoverType;
import ac.ed.lurg.types.ModelFitType;
import ac.ed.lurg.utils.LogWriter;
import ac.ed.lurg.yield.LPJYieldResponseMapReader;
import ac.ed.lurg.yield.YieldRaster;
import ac.ed.lurg.yield.YieldResponsesItem;
import ac.sac.raster.RasterKey;
import ac.sac.raster.RasterOutputer;
public class ModelMain {
private Collection<CountryAgent> countryAgents;
private CountryBoundaryRaster countryBoundaryRaster;
private CompositeCountryManager compositeCountryManager;
private Map<CropType, GlobalPrice> prevWorldPrices;
private RasterSet<LandUseItem> prevLandUseRaster;
private RasterSet<IrrigationItem> currentIrrigationData;
public static void main(String[] args) {
ModelMain theModel = new ModelMain();
theModel.setup();
theModel.run();
}
/* setup models, reading inputs, etc. */
private void setup() {
desiredProjection = RasterHeaderDetails.getGlobalHeaderFromCellSize(ModelConfig.CELL_SIZE_X, ModelConfig.CELL_SIZE_Y, "999");
BaseConsumpManager baseConsumpManager = new BaseConsumpManager();
compositeCountryManager = new CompositeCountryManager(baseConsumpManager);
demandManager = new DemandManager(ModelFitType.LOGISTIC, ModelConfig.SSP_SCENARIO, baseConsumpManager, compositeCountryManager);
tradeManager = new TradeManager(compositeCountryManager);
currentIrrigationData = getFixedIrrigationData();
countryBoundaryRaster = getCountryBoundaryRaster();
countryAgents = createCountryAgents(compositeCountryManager.getAll());
// in first timestep we don't have this info, but ok as constrained to import/export specified amount
prevWorldPrices = new HashMap<CropType, GlobalPrice>();
prevWorldPrices.put(CropType.WHEAT, GlobalPrice.createInitial(0.15));
prevWorldPrices.put(CropType.MAIZE, GlobalPrice.createInitial(0.15));
prevWorldPrices.put(CropType.RICE, GlobalPrice.createInitial(0.4));
prevWorldPrices.put(CropType.OILCROPS, GlobalPrice.createInitial(0.4));
prevWorldPrices.put(CropType.PULSES, GlobalPrice.createInitial(0.2));
prevWorldPrices.put(CropType.STARCHY_ROOTS, GlobalPrice.createInitial(0.1));
prevWorldPrices.put(CropType.MEAT, GlobalPrice.createInitial(0.2));
for (int i = ModelConfig.START_TIMESTEP; i <= ModelConfig.END_TIMESTEP; i++) {
Timestep timestep = new Timestep(i);
LpjgOutputer.writeMarkerFile(timestep.getYear(), true);
private void doTimestep(Timestep timestep) {
LogWriter.println(timestep.toString());
YieldRaster yieldSurfaces = getYieldSurfaces(timestep); // this will wait for the marker file from LPJ if configured to do so
RasterSet<IrrigationItem> allIrrigData = getUpdateIrrigationData(timestep, yieldSurfaces);
YieldResponsesItem yresp = yieldSurfaces.getFromCoordinates(-90.5, 45.5);
LogWriter.printlnError("Test key: " + yresp.getYieldMax(CropType.MAIZE) + ", " + yresp.getYieldFertOnly(CropType.MAIZE) + ", " + yresp.getYieldIrrigOnly(CropType.MAIZE) + ", " + yresp.getYieldNone(CropType.MAIZE));
CropToDoubleMap totalImportCommodities = new CropToDoubleMap();
CropToDoubleMap totalExportCommodities = new CropToDoubleMap();
RasterSet<LandUseItem> globalLandUseRaster = new RasterSet<LandUseItem>(desiredProjection);
RasterSet<IntegerRasterItem> globalLocationIdRaster = new RasterSet<IntegerRasterItem>(desiredProjection);
Collection<RasterKey> countryKeys = countryBoundaryRaster.getKeysFor(ca.getCountry());
YieldRaster countryYieldSurfaces = yieldSurfaces.createSubsetForKeys(countryKeys);
RasterSet<IrrigationItem> irrigData = allIrrigData.createSubsetForKeys(countryKeys);
result = ca.determineProduction(timestep, countryYieldSurfaces, irrigData, prevWorldPrices);
LogWriter.printlnError("Exception processing " + ca.getCountry() + " will continue with other countries");
LogWriter.print(e);
if (result == null) {
LogWriter.printlnError("No results for " + ca.getCountry());
continue;
}
// some hacky code for debug purposes that keeps each gams gdx file
if (ca.getCountry().getName().equals("Spain")) {
try {
Files.copy(
FileSystems.getDefault().getPath("/Users/peteralexander/Documents/R_Workspace/UNPLUM/temp/GamsTmp/_gams_java_gdb1.gdx"),
FileSystems.getDefault().getPath("/Users/peteralexander/Documents/R_Workspace/UNPLUM/temp/GamsTmp/" + timestep.getTimestep() + ".gdx")
, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
globalLandUseRaster.putAll(result.getLandUses());
globalLocationIdRaster.putAll(result.getLocationIdRaster());
// Get values for world input costs
Map<CropType, CropUsageData> cropUsage = result.getCropUsageData();
for (Entry<CropType, CropUsageData> entry : cropUsage.entrySet()) {
CropType c = entry.getKey();
double countryNetImports = entry.getValue().getNetImports();
if (countryNetImports > 0)
totalImportCommodities.incrementValue(c, countryNetImports);
else
totalExportCommodities.incrementValue(c, -countryNetImports);
// Look at trade balance and adjust appropriately
for (CropType crop : CropType.getImportedTypes()) {
GlobalPrice prevPrice = prevWorldPrices.get(crop);
if (!totalImportCommodities.containsKey(crop) || !totalExportCommodities.containsKey(crop)) {
LogWriter.printlnError(String.format("Price for %s not updated (still %s), as no imports or no exports for it", crop.getGamsName(), prevPrice));
double imports = totalImportCommodities.get(crop);
double exports = totalExportCommodities.get(crop) * (1.0-ModelConfig.TRANSPORT_LOSSES);
GlobalPrice adjustedPrice = prevPrice.createWithUpdatedMarketPrices(imports, exports, (ModelConfig.MARKET_ADJ_PRICE));
LogWriter.println(String.format("Price for %s updated from %s (imports amount %.0f, exports amount %.0f) to %s ", crop.getGamsName(), prevPrice, imports, exports, adjustedPrice));
prevWorldPrices.put(crop, adjustedPrice);
}
outputTimestepResults(timestep, globalLandUseRaster, globalLocationIdRaster, yieldSurfaces);
prevLandUseRaster = globalLandUseRaster;
private BufferedWriter getFileWriter(Timestep timestep, String file, String columnHeadings) throws IOException {
if (timestep.isInitialTimestep()) {
FileWriter fstream = new FileWriter(file,false);
BufferedWriter outputFile = new BufferedWriter(fstream);
outputFile.write(columnHeadings);
outputFile.newLine();
return outputFile;
}
else {
FileWriter fstream = new FileWriter(file,true);
return new BufferedWriter(fstream);
}
}
private void writeLandCoverFile(Timestep timestep, RasterSet<LandUseItem> landUseRaster) {
StringBuffer sbHeadings = new StringBuffer("Year, Cropland (Mha), Pasture (Mha), Natural (Mha), Fert crop (Mt), Fert pasture (Mt), Irrig crop (km3), Irrig pasture (km3)");
BufferedWriter outputFile = getFileWriter(timestep, ModelConfig.LAND_COVER_OUTPUT_FILE, sbHeadings.toString());
StringBuffer sbData = new StringBuffer();
sbData.append(String.format("%d, %.1f, %.1f, %.1f",
timestep.getYear(),
LandUseItem.getTotalLandCover(landUseRaster.values(), LandCoverType.CROPLAND),
LandUseItem.getTotalLandCover(landUseRaster.values(), LandCoverType.PASTURE),
LandUseItem.getTotalLandCover(landUseRaster.values(), LandCoverType.OTHER_NATURAL)));
sbData.append(String.format(", %.1f", LandUseItem.getFertiliserTotal(landUseRaster.values(), CropType.getCropsLessPasture())/1000));
sbData.append(String.format(", %.1f", LandUseItem.getFertiliserTotal(landUseRaster.values(), CropType.PASTURE)/1000));
sbData.append(String.format(", %.1f", LandUseItem.getIrrigationTotal(landUseRaster.values(), CropType.getCropsLessPasture())));
sbData.append(String.format(", %.1f", LandUseItem.getIrrigationTotal(landUseRaster.values(), CropType.PASTURE)));
outputFile.write(sbData.toString());
outputFile.newLine();
outputFile.close();
}
catch (IOException e) {
private void writeGlobalMarketFile(Timestep timestep) {
try {
Peter Alexander
committed
StringBuffer sbHeadings = new StringBuffer("Year,Crop,Imports (Mt),Exports (Mt),New export price");
BufferedWriter outputFile = getFileWriter(timestep, ModelConfig.PRICES_OUTPUT_FILE, sbHeadings.toString());
for (CropType crop : CropType.getImportedTypes() ) {
StringBuffer sbData = new StringBuffer();
sbData.append(String.format("%d,%s", timestep.getYear(), crop.getGamsName()));
sbData.append(String.format(",%.1f,%.1f", prevWorldPrices.get(crop).getImportAmount(), prevWorldPrices.get(crop).getExportAmount()));
Peter Alexander
committed
sbData.append(String.format(",%.3f", prevWorldPrices.get(crop).getExportPrice()));
outputFile.write(sbData.toString());
outputFile.newLine();
}
outputFile.close();
}
catch (IOException e) {
LogWriter.print(e);
}
}
private void writeDemandFile(Timestep timestep) {
try {
StringBuffer sbHeadings = new StringBuffer("Year,Commodity,Amount (Mt)");
BufferedWriter outputFile = getFileWriter(timestep, ModelConfig.DEMAND_OUTPUT_FILE, sbHeadings.toString());
for (CommodityType comm : CommodityType.getAllItems() ) {
double demandAmount = 0;
for (CountryAgent country : countryAgents) {
Double d = country.getCurrentProjectedDemand().get(comm);
if (d != null)
demandAmount += d.doubleValue();
}
StringBuffer sbData = new StringBuffer();
sbData.append(String.format("%d,%s", timestep.getYear(), comm.getGamsName()));
sbData.append(String.format(",%.1f", demandAmount));
outputFile.write(sbData.toString());
outputFile.newLine();
}
outputFile.close();
}
catch (IOException e) {
LogWriter.print(e);
}
}
private void outputTimestepResults(Timestep timestep, RasterSet<LandUseItem> landUseRaster, RasterSet<IntegerRasterItem> locationIdRaster, YieldRaster yieldSurfaces) {
writeLandCoverFile(timestep, landUseRaster);
writeGlobalMarketFile(timestep);
if (ModelConfig.OUTPUT_FOR_LPJG) {
for (int outputYear : timestep.getYearsFromLast()) {
LogWriter.printlnError("Outputing Year: " + outputYear);
RasterSet<LandUseItem> landUseToOutput = null;
landUseToOutput = landUseRaster;
else if (ModelConfig.INTERPOLATE_OUTPUT_YEARS) {
InterpolatingRasterSet<LandUseItem> intermediateLandUse = new InterpolatingRasterSet<LandUseItem>(landUseRaster.getHeaderDetails()) {
private static final long serialVersionUID = 1306045141011047760L;
protected LandUseItem createRasterData() {
return new LandUseItem();
intermediateLandUse.setup(prevLandUseRaster, landUseRaster, timestep.getPreviousTimestep().getYear(), timestep.getYear(), outputYear);
landUseToOutput = intermediateLandUse;
if (landUseToOutput != null) {
LpjgOutputer lpjOutputer = new LpjgOutputer(outputYear, landUseToOutput, yieldSurfaces);
lpjOutputer.writeOutput();
}
LogWriter.printlnError("Outputing land uses Year: " + timestep.getYear());
LandUseOutputer landuseOutputer = new LandUseOutputer(timestep.getYear(), landUseRaster);
landuseOutputer.writeOutput();
outputLandCover(timestep.getYear(), landUseRaster, LandCoverType.CROPLAND);
outputLandCover(timestep.getYear(), landUseRaster, LandCoverType.PASTURE);
private void outputLandCover(int year, RasterSet<LandUseItem> landUseRaster, final LandCoverType lcType) {
new RasterOutputer<LandUseItem>(landUseRaster, lcType.getName() + "Area" + year) {
@Override
public Double getValue(RasterKey location) {
LandUseItem item = results.get(location);
if (item == null)
}.writeOutput();
public CountryBoundaryRaster getCountryBoundaryRaster() {
CountryBoundaryRaster countryBoundaries = new CountryBoundaryRaster(desiredProjection, compositeCountryManager);
CountryBoundaryReader countryReader = new CountryBoundaryReader(countryBoundaries);
countryReader.getRasterDataFromFile(ModelConfig.COUNTRY_BOUNDARY_FILE);
public Collection<CountryAgent> createCountryAgents(Collection<CompositeCountry> countryGrouping) {
Collection<CountryAgent> countryAgents = new HashSet<CountryAgent>();
RasterSet<LandUseItem> initLU = null;
if (ModelConfig.IS_CALIBRATION_RUN) {
initLU = getInitialLandCover();
}
else {
initLU = getInitialLandUse(); // read in all land use data saved from calibrations run
}
Map<CompositeCountry, Map<CropType, CropUsageData>> cropUsageDataMap = new CropUsageReader(compositeCountryManager).getCommodityData();
for (CompositeCountry cc : countryGrouping) {
if (!(cc.getName().equals(ModelConfig.DEBUG_COUNTRY_NAME)))
List<RasterKey> keys = countryBoundaryRaster.getKeysFor(cc);
Map<CropType, CropUsageData> countryCommodityData = cropUsageDataMap.get(cc);
Map<CropType, Double> countryTradeBarriers = tradeManager.getTradeBarriers(cc);
LogWriter.printlnError("No commodities data for " + cc + ", so skipping");
RasterSet<LandUseItem> initCountryLandUse = initLU.createSubsetForKeys(keys);
if (initCountryLandUse.size() == 0) {
LogWriter.printlnError("No initial land use for " +cc + ", so skipping");
continue;
}
CountryAgent ca = new CountryAgent(demandManager, cc, initCountryLandUse, countryCommodityData, countryTradeBarriers);
LogWriter.println("Creating country agent for: " + cc );
/** This is if we are starting from previously saved state (for example, from calibration runs) */
private RasterSet<LandUseItem> getInitialLandUse() {
RasterSet<LandUseItem> initLU = new RasterSet<LandUseItem>(desiredProjection) {
private static final long serialVersionUID = 1317102740312961042L;
protected LandUseItem createRasterData() {
return new LandUseItem();
}
};
new LandUseReader(initLU).getRasterDataFromFile(ModelConfig.INITAL_LAND_USE_FILE);
return initLU;
}
/** this is if we are starting from Hurtt of other initial land covers (so we don't have land uses and intensity data) */
private RasterSet<LandUseItem> getInitialLandCover() {
RasterSet<LandCoverItem> initialLC = new RasterSet<LandCoverItem>(desiredProjection) {
private static final long serialVersionUID = 4642550777741425501L;
protected LandCoverItem createRasterData() {
return new LandCoverItem();
}
new ProtectedAreasReader(initialLC).getRasterDataFromFile(ModelConfig.PROTECTED_AREAS_FILE);
new LandCoverReader(initialLC).getRasterDataFromFile(ModelConfig.INITAL_LAND_COVER_FILE);
/* new RasterOutputer<LandCoverItem>(initLC, "InitialCropland") {
@Override
public Double getValue(RasterKey location) {
LandCoverItem item = results.get(location);
if (item == null)
return null;
return item.getLandCoverFract(LandCoverType.CROPLAND);
}
}.writeOutput(); */
RasterSet<LandUseItem> landUseRaster = new RasterSet<LandUseItem>(initialLC.getHeaderDetails());
for (Map.Entry<RasterKey, LandCoverItem> entry : initialLC.entrySet()) {
LandUseItem areasItem = new LandUseItem();
RasterKey key = entry.getKey();
LandCoverItem landCover = entry.getValue();
areasItem.setLandCoverAreas(landCover);
areasItem.setCropFraction(CropType.WHEAT, 0.5); // random start, better if we could get data, but free substitution between crops so not critical
areasItem.setCropFraction(CropType.MAIZE, 0.5);
areasItem.setProtectedArea(landCover.getProtectedArea());
landUseRaster.put(key, areasItem);
}
private YieldRaster getYieldSurfaces(Timestep timestep) {
LPJYieldResponseMapReader yieldReader = new LPJYieldResponseMapReader(desiredProjection);
return yieldReader.getRasterData(timestep);
/** Get irrigation data components that don't change over time */
private RasterSet<IrrigationItem> getFixedIrrigationData() {
RasterSet<IrrigationItem> irigData = new RasterSet<IrrigationItem>(desiredProjection) {
private static final long serialVersionUID = 8393130687550888654L;
protected IrrigationItem createRasterData() {
return new IrrigationItem();
new IrrigiationCostReader(irigData).getRasterDataFromFile(ModelConfig.IRRIGATION_COST_FILE);
new IrrigationConstraintReader(irigData).getRasterDataFromFile(ModelConfig.IRRIGATION_CONSTRAINT_FILE);
return irigData;
private RasterSet<IrrigationItem> getUpdateIrrigationData(Timestep timestep, YieldRaster yieldSurfaces) {
String rootDir = timestep.getYearSubDir(ModelConfig.YIELD_DIR);
new IrrigationMaxAmountReader(currentIrrigationData, yieldSurfaces).getRasterDataFromFile(rootDir + File.separator + ModelConfig.IRRIG_MAX_WATER_FILENAME);
return currentIrrigationData;
}