From 3af87a83e84dcd696c80b9383091a71af6eb950a Mon Sep 17 00:00:00 2001 From: Peter Alexander <peter@blackhillock.co.uk> Date: Tue, 4 Aug 2015 13:32:27 +0100 Subject: [PATCH] Write out marker files --- src/ac/ed/lurg/ModelConfig.java | 6 ++-- src/ac/ed/lurg/ModelMain.java | 30 ++++++------------- src/ac/ed/lurg/output/LpjgOutputer.java | 39 ++++++++++++++++++++----- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java index 65c0d145..827d3bbd 100644 --- a/src/ac/ed/lurg/ModelConfig.java +++ b/src/ac/ed/lurg/ModelConfig.java @@ -80,7 +80,7 @@ public class ModelConfig { public static final boolean SUPPRESS_STD_OUTPUT = getBooleanProperty("SUPPRESS_STD_OUTPUT", Boolean.FALSE); - public static final String BASE_DIR = getProperty("BASE_DIR", "/Users/peteralexander/Documents/R_Workspace/UNPLUM"); + public static final String BASE_DIR = getProperty("BASE_DIR"); // this must to be set in config file public static final String TEMP_DIR = getProperty("TEMP_DIR", BASE_DIR + File.separator + "temp"); public static final String OUTPUT_DIR = getProperty("OUTPUT_DIR", BASE_DIR + File.separator + "output"); @@ -96,7 +96,7 @@ public class ModelConfig { public static final String COMMODITY_DATA_FILE = DATA_DIR + File.separator + "con_prod_c_and_m.csv"; // yield data - public static final String YIELD_DIR = "/Users/peteralexander/Documents/LURG/LPJ/tom-July2015"; + public static final String YIELD_DIR = getProperty("YIELD_DIR"); // Spatial (gridded) data public static final String SPATIAL_DATA_DIR = DATA_DIR; // + File.separator + "tinyTest"; @@ -107,7 +107,7 @@ public class ModelConfig { public static final int START_TIMESTEP = getIntProperty("START_TIMESTEP", 0); - public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 0); + public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 1); public static final int BASE_YEAR = getIntProperty("BASE_YEAR", 2010); public static final double MAX_IMPORT_CHANGE = getDoubleProperty("MAX_IMPORT_CHANGE", 0.2); diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java index cbd31705..0e95b38b 100644 --- a/src/ac/ed/lurg/ModelMain.java +++ b/src/ac/ed/lurg/ModelMain.java @@ -19,7 +19,6 @@ import ac.ed.lurg.demand.DemandManager; import ac.ed.lurg.landuse.AreasItem; import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IntensitiesItem; -import ac.ed.lurg.landuse.Intensity; import ac.ed.lurg.landuse.IrrigationCostItem; import ac.ed.lurg.landuse.IrrigiationCostReader; import ac.ed.lurg.landuse.LandCoverItem; @@ -73,13 +72,14 @@ public class ModelMain { } private void doTimestep(int timestep) { - YieldRaster yieldSurfaces = getYieldSurfaces(timestep); + int year = ModelConfig.BASE_YEAR + timestep; + LogWriter.println("Timestep: " + timestep + ", year:" + year); + + YieldRaster yieldSurfaces = getYieldSurfaces(year); // this will wait for the marker file from LPJ if configured to do so // YieldResponsesItem yresp = yieldSurfaces.getFromCoordinates(-50.0, -4.0); // LogWriter.printlnError("Test key: " + yresp.getYieldMax(CropType.CEREALS) + ", " + yresp.getYieldFertOnly(CropType.CEREALS) + ", " + yresp.getYieldIrrigOnly(CropType.CEREALS)); - int year = ModelConfig.BASE_YEAR + timestep; - LogWriter.println("Timestep: " + timestep + ", year:" + year); CropToDoubleMap totalQuantity = new CropToDoubleMap(); CropToDoubleMap totalWorldInputCost = new CropToDoubleMap(); @@ -246,9 +246,9 @@ public class ModelMain { // DEBUG code - // if (!(country.getCountryName().equals("United States of Americaxx") || country.getCountryName().equals("Russian Federationxx") || country.getCountryName().equals("China")) ) { //|| country.getCountryName().equals("China") - // continue; - // } + if (!(country.getCountryName().equals("United States of America") || country.getCountryName().equals("Russian Federation") || country.getCountryName().equals("China")) ) { //|| country.getCountryName().equals("China") + continue; + } if (demandManager.getPopulation(country, 2010) < 50 || countryExclusionList.contains(country.getCountryName())) { LogWriter.printlnError("Skipping " + country); @@ -300,20 +300,8 @@ public class ModelMain { return initLC; } - private YieldRaster getYieldSurfaces(int timestep) { - /* String rootDir = ModelConfig.YIELD_DIR + File.separator + (timestep + ModelConfig.BASE_YEAR) + File.separator; - YieldRaster yieldSurfaces = null; - - for (CropType cropType : CropType.values()) { - String cropDir = rootDir + cropType.getGamsName() + File.separator; - - for (YieldType yieldType : YieldType.values()) { - YieldResponseMapReader yieldReader = new YieldResponseMapReader(yieldSurfaces, yieldType, cropType); - yieldSurfaces = (YieldRaster)yieldReader.getRasterDataFromFile(cropDir + yieldType.getFileName()); - } - } */ - - LPJYieldResponseMapReader yieldReader = new LPJYieldResponseMapReader(ModelConfig.YIELD_DIR, desiredProjection); + private YieldRaster getYieldSurfaces(int year) { + LPJYieldResponseMapReader yieldReader = new LPJYieldResponseMapReader(ModelConfig.YIELD_DIR + File.separator + year, desiredProjection); for (FertiliserRate fr : FertiliserRate.values()) { yieldReader.getRasterDataFromFile(fr); diff --git a/src/ac/ed/lurg/output/LpjgOutputer.java b/src/ac/ed/lurg/output/LpjgOutputer.java index 718ca692..dddc8291 100644 --- a/src/ac/ed/lurg/output/LpjgOutputer.java +++ b/src/ac/ed/lurg/output/LpjgOutputer.java @@ -2,6 +2,7 @@ package ac.ed.lurg.output; import java.io.BufferedWriter; import java.io.File; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Map.Entry; @@ -32,24 +33,48 @@ public class LpjgOutputer { } public void writeOutput() { - writeLandCoverAndCrop(); + String outputDirName = ModelConfig.OUTPUT_DIR + File.separator + year; + File outputDir = new File(outputDirName); + outputDir.mkdirs(); + + writeLandCoverAndCrop(outputDirName); + writeIntensity(outputDirName); + writeMarkerFile(outputDirName, false); + } + + private void writeIntensity(String outputDirName) { + // TODO Auto-generated method stub + } - - public void writeLandCoverAndCrop() { + + private void writeMarkerFile(String outputDirName, boolean errorStatus) { + File markerFile = new File(outputDirName + File.separator + (errorStatus ? "error" : "done")); + + try { + if (!markerFile.exists()) + new FileOutputStream(markerFile).close(); + markerFile.setLastModified(System.currentTimeMillis()); + } + catch (IOException e) { + LogWriter.printlnError("Problem writing marker file"); + } + } + + public void writeLandCoverAndCrop(String outputDir) { BufferedWriter landCoverWriter = null; BufferedWriter cropFractWriter = null; try { - String landCoverFileName = ModelConfig.OUTPUT_DIR + File.separator + "LandCoverFract" + year + ".txt"; + String landCoverFileName = outputDir + File.separator + "LandCoverFract.txt"; landCoverWriter = new BufferedWriter(new FileWriter(landCoverFileName, false)); landCoverWriter.write("Lon Lat CROPLAND PASTURE NATURAL"); landCoverWriter.newLine(); - String cropFractionFileName = ModelConfig.OUTPUT_DIR + File.separator + "CropFract" + year + ".txt"; + String cropFractionFileName = outputDir + File.separator + "CropFract.txt"; cropFractWriter = new BufferedWriter(new FileWriter(cropFractionFileName, false)); cropFractWriter.write("Lon Lat TeWWirr TeSWirr TeCoirr TrRiirr"); cropFractWriter.newLine(); - + for (Entry<RasterKey, AreasItem> entry : cropAreaRaster.entrySet()) { RasterKey key = entry.getKey(); AreasItem item = entry.getValue(); @@ -70,7 +95,7 @@ public class LpjgOutputer { isSpringWheat = yieldSurfaces.get(key).isSpringWheat(); else LogWriter.printlnError("LpjgOutputer: Can't find YieldResponsesItem for " + key); - + double lpjWinterWheatFrac = item.getCropFraction(CropType.OILCROPS, isSpringWheat ? null : CropType.WHEAT); double lpjSpringWheatFrac = item.getCropFraction(CropType.SOYBEAN, CropType.PULSES, CropType.STARCHY_ROOTS, isSpringWheat ? CropType.WHEAT : null); double lpjCornFrac = item.getCropFraction(CropType.MAIZE, CropType.SOYBEAN); -- GitLab