diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java index c1eb057ba5e6db7867ee95574e685775e54f128f..7d1e4592001b49dc0fb0ed0955ead8855ebfb39a 100644 --- a/src/ac/ed/lurg/ModelConfig.java +++ b/src/ac/ed/lurg/ModelConfig.java @@ -78,8 +78,6 @@ public class ModelConfig { return v==null ? defaultBoolean : Boolean.valueOf(v); } - public static final boolean DEBUG = true; - public static final boolean SUPPRESS_STD_OUTPUT = getBooleanProperty("SUPPRESS_STD_OUTPUT", Boolean.FALSE); public static final String BASE_DIR = getProperty("OUTPUT_DIR", "/Users/peteralexander/Documents/R_Workspace"); @@ -92,6 +90,8 @@ public class ModelConfig { public static final String DEMAND_CURVES_FILE = DATA_DIR + File.separator + "com_curves.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 LPJG_YIELD_FILE = DATA_DIR + File.separator + "yield_gfdl.test.out"; + 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"; @@ -99,7 +99,9 @@ public class ModelConfig { public static final String COUNTRY_CODES_FILE = DATA_DIR + File.separator + "country_codes3.csv"; public static final String COUNTRY_DATA_FILE = DATA_DIR + File.separator + "country_data.csv"; public static final String COMMODITY_DATA_FILE = DATA_DIR + File.separator + "con_prod_c.csv"; + public static final String IRRIGATION_COST_FILE = DATA_DIR + File.separator + "irrigation_cost.test.asc";; + public static final int START_TIMESTEP = getIntProperty("START_TIMESTEP", 0); public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 1); public static final int BASE_YEAR = getIntProperty("BASE_YEAR", 2010); diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java index 6ee7710fb55a0dbb16b1d0bd1994503f6120c18b..71b969558eae8e06cecefdae075fd574995d7b85 100644 --- a/src/ac/ed/lurg/ModelMain.java +++ b/src/ac/ed/lurg/ModelMain.java @@ -29,6 +29,7 @@ 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.sac.raster.RasterHeaderDetails; import ac.sac.raster.RasterKey; import ac.sac.raster.RasterSet; @@ -38,7 +39,7 @@ public class ModelMain { private Map<Country, List<RasterKey>> countryToKeysMap; private DemandManager demandManager; private Map<CropType, Double> prevWorldInputEnergy; - + private RasterHeaderDetails desiredProjection; public static void main(String[] args) { ModelMain theModel = new ModelMain(); @@ -48,6 +49,7 @@ public class ModelMain { /* setup models, reading inputs, etc. */ private void setup() { + desiredProjection = new RasterHeaderDetails(720, 360, -180, -90, 0.5, ""); countryToKeysMap = createCountryToKeysMap(); demandManager = new DemandManager(ModelFitType.LOGISTIC, ModelConfig.SSP_SCENARIO); countryAgents = createCountryAgents(); @@ -110,17 +112,29 @@ public class ModelMain { } public Map<Country, List<RasterKey>> createCountryToKeysMap() { - CountryBoundaryReader countryReader = new CountryBoundaryReader(); - RasterSet<CountryBoundaryItem> countryBoundaries = countryReader.getRasterDataFromFile(ModelConfig.COUNTRY_BOUNDARY_FILE); + + RasterSet<CountryBoundaryItem> countryBoundaries = new RasterSet<CountryBoundaryItem>(desiredProjection) { + private static final long serialVersionUID = -8449000692429399253L; + protected CountryBoundaryItem createRasterData() { + return new CountryBoundaryItem(); + } + }; + + CountryBoundaryReader countryReader = new CountryBoundaryReader(countryBoundaries); + countryReader.getRasterDataFromFile(ModelConfig.COUNTRY_BOUNDARY_FILE); Map<Country, List<RasterKey>> countryMap = new HashMap<Country, List<RasterKey>>(); for (Map.Entry<RasterKey, CountryBoundaryItem> entry : countryBoundaries.entrySet()) { - List<RasterKey> keys = countryMap.get(entry.getValue().getCountry()); + Country c = entry.getValue().getCountry(); + if (c == null) + continue; + + List<RasterKey> keys = countryMap.get(c); if (keys == null) { keys = new ArrayList<RasterKey>(); - countryMap.put(entry.getValue().getCountry(), keys); + countryMap.put(c, keys); } keys.add(entry.getKey()); } @@ -149,8 +163,16 @@ public class ModelMain { } private RasterSet<LandCoverItem> getInitialLandCover() { + + RasterSet<LandCoverItem> initLC = new RasterSet<LandCoverItem> (desiredProjection) { + private static final long serialVersionUID = 4642550777741425501L; + @Override + protected LandCoverItem createRasterData() { + return new LandCoverItem(); + } + }; + String rootDir = ModelConfig.INITAL_LAND_COVER_DIR + File.separator; - RasterSet<LandCoverItem> initLC = null; for (LandDataType landType : LandDataType.values()) { LandCoverReader lcReader = new LandCoverReader(initLC, landType); @@ -171,20 +193,24 @@ public class ModelMain { YieldResponseMapReader yieldReader = new YieldResponseMapReader(yieldSurfaces, yieldType, cropType); yieldSurfaces = (YieldRaster)yieldReader.getRasterDataFromFile(cropDir + yieldType.getFileName()); } - } - */ + } */ - String lpjFile = "/Users/peteralexander/Documents/LURG/LPJ/tom-Jan2015/yield_gfdl_rcp85_2000.out"; LPJYieldResponseMapReader yieldReader = new LPJYieldResponseMapReader(null); - YieldRaster yieldSurfaces = (YieldRaster)yieldReader.getRasterDataFromFile(lpjFile); - + YieldRaster yieldSurfaces = (YieldRaster)yieldReader.getRasterDataFromFile(ModelConfig.LPJG_YIELD_FILE); return yieldSurfaces; } private RasterSet<IrrigationCostItem> getIrrigationCosts() { - String irigCostFile = "/Users/peteralexander/Documents/LURG/Data/CGIAR/IrrigationCost.asc"; - IrrigiationCostReader irigCostReader = new IrrigiationCostReader(); - RasterSet<IrrigationCostItem> irigCosts = irigCostReader.getRasterDataFromFile(irigCostFile); + RasterSet<IrrigationCostItem> irigCosts = new RasterSet<IrrigationCostItem>(desiredProjection) { + private static final long serialVersionUID = 8393130687550888654L; + + protected IrrigationCostItem createRasterData() { + return new IrrigationCostItem(); + } + }; + + IrrigiationCostReader irigCostReader = new IrrigiationCostReader(irigCosts); + irigCostReader.getRasterDataFromFile(ModelConfig.IRRIGATION_COST_FILE); return irigCosts; } } \ No newline at end of file diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index c82bfa6f959e9cc317b12006270a2c9de9fd783b..8c63de10a1d2036f631d4bfc4058b429176aeaaf 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -56,13 +56,15 @@ public class CountryAgent { double cellArea = key.getHalfDegreeArea(); - areasItem.setCropArea(CropType.CEREALS, landCover.getLandCover(LandDataType.CROPLAND) * cellArea); // we allow free substitution between crop types so this is ok-ish - areasItem.setCropArea(CropType.MEAT_OR_PASTURE, landCover.getLandCover(LandDataType.PASTURE) * cellArea); + if (landCover != null) { + areasItem.setCropArea(CropType.CEREALS, landCover.getLandCover(LandDataType.CROPLAND) * cellArea); // we allow free substitution between crop types so this is ok-ish + areasItem.setCropArea(CropType.MEAT_OR_PASTURE, landCover.getLandCover(LandDataType.PASTURE) * cellArea); + + areasItem.setLandCoverArea(LandDataType.LAND, landCover.getLandCover(LandDataType.LAND) * cellArea); + areasItem.setLandCoverArea(LandDataType.FOREST, landCover.getLandCover(LandDataType.FOREST) * cellArea); + areasItem.setLandCoverArea(LandDataType.OTHER_NATURAL, landCover.getLandCover(LandDataType.OTHER_NATURAL) * cellArea); + } - areasItem.setLandCoverArea(LandDataType.LAND, landCover.getLandCover(LandDataType.LAND) * cellArea); - areasItem.setLandCoverArea(LandDataType.FOREST, landCover.getLandCover(LandDataType.FOREST) * cellArea); - areasItem.setLandCoverArea(LandDataType.OTHER_NATURAL, landCover.getLandCover(LandDataType.OTHER_NATURAL) * cellArea); - cropAreaRaster.put(key, areasItem); } return cropAreaRaster; @@ -116,7 +118,7 @@ public class CountryAgent { minNetImport.put(entry.getKey(), entry.getValue().getNetImports() * (1 - allowedImportChange)); } - GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), worldInputEnergy, maxNetImport, maxNetImport); + GamsCountryInput countryLevelInputs = new GamsCountryInput(country, getProjectedDemand(), worldInputEnergy, maxNetImport, maxNetImport); GamsRasterInput input = new GamsRasterInput(countryYieldSurfaces, prevOutput.getCropAreaRaster(), irrigationCostRaster, countryLevelInputs); return input; diff --git a/src/ac/ed/lurg/country/CountryBoundaryReader.java b/src/ac/ed/lurg/country/CountryBoundaryReader.java index 747a6e681d39baf3d222a8e44caabde3759ee381..74371d235a2dfe0d6528ef838a37f7de4f8f3796 100644 --- a/src/ac/ed/lurg/country/CountryBoundaryReader.java +++ b/src/ac/ed/lurg/country/CountryBoundaryReader.java @@ -1,24 +1,12 @@ package ac.ed.lurg.country; -import ac.ed.lurg.utils.LogWriter; import ac.sac.raster.AbstractRasterReader; -import ac.sac.raster.RasterHeaderDetails; import ac.sac.raster.RasterSet; public class CountryBoundaryReader extends AbstractRasterReader<CountryBoundaryItem> { - protected void createDataSet(RasterHeaderDetails header) { - if (DEBUG) LogWriter.println("Creating RasterDataset col:"+header.getNcolumns() + ", rows:" + header.getNrows()); - - if (dataset == null) { - dataset = new RasterSet<CountryBoundaryItem>(header) { - private static final long serialVersionUID = 8813968413893792926L; - - protected CountryBoundaryItem createRasterData() { - return new CountryBoundaryItem(); - } - }; - } + public CountryBoundaryReader (RasterSet<CountryBoundaryItem> dataset) { + super(dataset); } @Override diff --git a/src/ac/ed/lurg/country/gams/GamsCountryInput.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java index 3f729a83d4fce13c405aba2afcd6861002770e30..1de8b4ba342c0d72fcd49f432e3df4b07f813124 100644 --- a/src/ac/ed/lurg/country/gams/GamsCountryInput.java +++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java @@ -2,10 +2,12 @@ package ac.ed.lurg.country.gams; import java.util.Map; +import ac.ed.lurg.country.Country; import ac.ed.lurg.types.CropType; public class GamsCountryInput { + private Country country; // not really required but useful for debugging private Map<CropType, Double> projectedDemand; private Map<CropType, Double> worldInputEnergy; private Map<CropType, Double> maxNetImport; @@ -20,15 +22,20 @@ public class GamsCountryInput { private double landChangeEnergy;*/ - public GamsCountryInput(Map<CropType, Double> projectedDemand, Map<CropType, Double> worldInputEnergy, + public GamsCountryInput(Country country, Map<CropType, Double> projectedDemand, Map<CropType, Double> worldInputEnergy, Map<CropType, Double> maxNetImport, Map<CropType, Double> minNetImport) { super(); + this.country = country; this.projectedDemand = projectedDemand; this.worldInputEnergy = worldInputEnergy; this.maxNetImport = maxNetImport; this.minNetImport = minNetImport; } + public Country getCountry() { + return country; + } + public Map<CropType, Double> getProjectedDemand() { return projectedDemand; } diff --git a/src/ac/ed/lurg/country/gams/GamsLocationTest.java b/src/ac/ed/lurg/country/gams/GamsLocationTest.java index 83ea4c70ae78a758b41e6b3039386af1027624d7..5d3579f7d3c396f7f60047c6bea86b284c527fe3 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationTest.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationTest.java @@ -3,6 +3,7 @@ package ac.ed.lurg.country.gams; import java.util.HashMap; import java.util.Map; +import ac.ed.lurg.country.Country; import ac.ed.lurg.landuse.AreasItem; import ac.ed.lurg.landuse.IrrigationCostItem; import ac.ed.lurg.types.CropType; @@ -19,7 +20,7 @@ public class GamsLocationTest { } private void run() { - GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); + GamsCountryInput countryLevelInputs = new GamsCountryInput(new Country("Test", "TES", 123), getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); GamsLocationInput gamsInput = new GamsLocationInput(getYields(), getPreviousArea(), getIrrigationCosts(), countryLevelInputs); GamsLocationOptimiser opti = new GamsLocationOptimiser(gamsInput); diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java index 534f5263584e1fedac14b331f5be8617cdcf0cf8..8ab5c52ec72d657c5bacd1574d8193344ae9a5a2 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java @@ -8,7 +8,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import ac.ed.lurg.ModelConfig; import ac.ed.lurg.landuse.AreasItem; import ac.ed.lurg.landuse.IntensitiesItem; import ac.ed.lurg.landuse.IrrigationCostItem; @@ -25,6 +24,7 @@ import ac.sac.raster.RasterKey; import ac.sac.raster.RasterSet; public class GamsRasterOptimiser { + public static final boolean DEBUG = true; private GamsRasterInput rasterInputData; private LazyHashMap<Integer, Set<RasterKey>> mapping; @@ -49,7 +49,6 @@ public class GamsRasterOptimiser { RasterSet<AreasItem> newAreaRaster = allocAreas(gamsInput.getPreviousAreas(), gamsOutput); RasterSet<IntensitiesItem> newIntensityRaster = allocIntensities(gamsOutput); - return new GamsRasterOutput(gamsOutput.getStatus(), newIntensityRaster, newAreaRaster, gamsOutput.getCommoditiesData()); } @@ -193,8 +192,9 @@ public class GamsRasterOptimiser { for (Entry<RasterKey, YieldResponsesItem> entry : yieldRaster.entrySet()) { YieldResponsesItem yresp = entry.getValue(); - AreasItem cropAreas = cropAreaRaster.get(entry.getKey()); - IrrigationCostItem irrigCost = irrigCostRaster.get(entry.getKey()); + RasterKey key = entry.getKey(); + AreasItem cropAreas = cropAreaRaster.get(key); + IrrigationCostItem irrigCost = irrigCostRaster.get(key); int cerealCat = findCategory(cerealDivisions, yresp.getYieldMax(CropType.CEREALS)); int pastureCat = findCategory(pastureDivisions, yresp.getYieldMax(CropType.MEAT_OR_PASTURE)); @@ -220,7 +220,7 @@ public class GamsRasterOptimiser { aggAreas.setLandCoverArea(LandDataType.LAND, areaSoFar + areaThisTime); } - if (ModelConfig.DEBUG) { + if (DEBUG) { for (Map.Entry<Integer, Set<RasterKey>> e : mapping.entrySet()) { LogWriter.println(e.getKey() + " category has " + e.getValue().size() + " raster areas"); for (RasterKey key : e.getValue()) { @@ -248,9 +248,13 @@ public class GamsRasterOptimiser { List<Double> yieldValues = new ArrayList<Double>(); for (YieldResponsesItem yresp : yieldRaster.values()) { - yieldValues.add(yresp.getYieldMax(crop)); + if (yresp != null) yieldValues.add(yresp.getYieldMax(crop)); } + if (yieldValues.size() == 0) { + throw new RuntimeException("No yield values for country"); + } + Collections.sort(yieldValues); List<Double> divisions = new ArrayList<Double>(); diff --git a/src/ac/ed/lurg/country/gams/GamsRasterTest.java b/src/ac/ed/lurg/country/gams/GamsRasterTest.java index e9cfe8d751e82a1ee4400a17c89388e05bce6949..6ce825fd9f7760f2af83d5e1575b10812afcdde5 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterTest.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterTest.java @@ -1,5 +1,6 @@ package ac.ed.lurg.country.gams; +import ac.ed.lurg.country.Country; import ac.ed.lurg.landuse.AreasItem; import ac.ed.lurg.landuse.IrrigationCostItem; import ac.ed.lurg.utils.LogWriter; @@ -16,7 +17,7 @@ public class GamsRasterTest extends GamsLocationTest { } private void run() { - GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); + GamsCountryInput countryLevelInputs = new GamsCountryInput(new Country("Test", "TES", 123), getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); GamsRasterInput input = new GamsRasterInput(getYieldRaster(), getPreviousAreaRaster(), getIrrigationCost(), countryLevelInputs); GamsRasterOptimiser opti = new GamsRasterOptimiser(input); diff --git a/src/ac/ed/lurg/landuse/IrrigiationCostReader.java b/src/ac/ed/lurg/landuse/IrrigiationCostReader.java index d8ce4380f0f2adfab214c92a12794eb05416d200..74f609eba77081192deb2dfa06d78af29bebba9c 100644 --- a/src/ac/ed/lurg/landuse/IrrigiationCostReader.java +++ b/src/ac/ed/lurg/landuse/IrrigiationCostReader.java @@ -1,24 +1,12 @@ package ac.ed.lurg.landuse; -import ac.ed.lurg.utils.LogWriter; import ac.sac.raster.AbstractRasterReader; -import ac.sac.raster.RasterHeaderDetails; import ac.sac.raster.RasterSet; public class IrrigiationCostReader extends AbstractRasterReader<IrrigationCostItem> { - protected void createDataSet(RasterHeaderDetails header) { - if (DEBUG) LogWriter.println("Creating RasterDataset col:"+header.getNcolumns() + ", rows:" + header.getNrows()); - - if (dataset == null) { - dataset = new RasterSet<IrrigationCostItem>(header) { - private static final long serialVersionUID = 8393130687550888654L; - - protected IrrigationCostItem createRasterData() { - return new IrrigationCostItem(); - } - }; - } + public IrrigiationCostReader (RasterSet<IrrigationCostItem> dataset) { + super(dataset); } @Override diff --git a/src/ac/ed/lurg/landuse/LandCoverReader.java b/src/ac/ed/lurg/landuse/LandCoverReader.java index ab7725b593ca7e24b4247e2ec92a10a2a4d7aa0f..8fbd6133502fc10c4c6713cd63a1b5aaaaf8a734 100644 --- a/src/ac/ed/lurg/landuse/LandCoverReader.java +++ b/src/ac/ed/lurg/landuse/LandCoverReader.java @@ -1,9 +1,7 @@ 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> { @@ -15,20 +13,6 @@ public class LandCoverReader extends AbstractRasterReader<LandCoverItem> { 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); diff --git a/src/ac/ed/lurg/yield/LPJYieldResponseMapReader.java b/src/ac/ed/lurg/yield/LPJYieldResponseMapReader.java index d7f7046f193c488a758732c63f0faba15f5f3af6..70845c40cfbedb53fe54f8d14005fc02912791bc 100644 --- a/src/ac/ed/lurg/yield/LPJYieldResponseMapReader.java +++ b/src/ac/ed/lurg/yield/LPJYieldResponseMapReader.java @@ -35,7 +35,7 @@ public class LPJYieldResponseMapReader { int col=0; if (dataset == null) - createDataSet(new RasterHeaderDetails(720,360,-180,-90,0.5,"-9999")); + createDataSet(new RasterHeaderDetails(720,360,-180,-90,0.5,"")); try { BufferedReader in = new BufferedReader(new FileReader(filename)); diff --git a/src/ac/sac/raster/AbstractRasterReader.java b/src/ac/sac/raster/AbstractRasterReader.java index c9d5bea169519ac760726cab57f2bc3911446221..b0f71a2953ec551ddf85d151cba8390318de181e 100755 --- a/src/ac/sac/raster/AbstractRasterReader.java +++ b/src/ac/sac/raster/AbstractRasterReader.java @@ -76,11 +76,13 @@ public abstract class AbstractRasterReader<D extends RasterItem> { if (DEBUG) LogWriter.println("Creating RasterDataset col:"+ncolumns + ", rows:" + nrows); RasterHeaderDetails headerDetails = new RasterHeaderDetails(ncolumns, nrows, xllCorner, yllCorner, cellSize, nodataString); - createDataSet(headerDetails); + if (dataset == null) createDataSet(headerDetails); return headerDetails; } - protected abstract void createDataSet(RasterHeaderDetails headerDetails); + protected void createDataSet(RasterHeaderDetails headerDetails) { + throw new RuntimeException("AbstractRasterReader.createDataSet: This should be overridden if dataset not passed in"); + } public RasterSet<D> getRasterDataFromFile(String filename) { diff --git a/src/ac/sac/raster/RasterHeaderDetails.java b/src/ac/sac/raster/RasterHeaderDetails.java index 31cd4c1a61d41d8bffcb3aaecfcd581cd55e086c..1878cab389b501d87a6d547156c54e83720c0ec9 100755 --- a/src/ac/sac/raster/RasterHeaderDetails.java +++ b/src/ac/sac/raster/RasterHeaderDetails.java @@ -25,6 +25,14 @@ public class RasterHeaderDetails { return new RasterHeaderDetails(ncolumns, nrows, xllCorner, yllCorner, cellSize, nodataString); } + public double getOriginX() { + return xllCorner; + } + + public double getOriginY() { + return yllCorner + cellSize * nrows; + } + public void incrementNcolumns() { ncolumns++; } diff --git a/src/ac/sac/raster/RasterSet.java b/src/ac/sac/raster/RasterSet.java index 7df844aa89aaedf93a33942561ae5fd7a4c4258a..91e359801fd076b7b4806f366c8663e687258480 100755 --- a/src/ac/sac/raster/RasterSet.java +++ b/src/ac/sac/raster/RasterSet.java @@ -29,7 +29,7 @@ public class RasterSet<D extends RasterItem> extends HashMap<RasterKey, D> { if (source!=null && !source.equals(header)) { int xOffset = (int) Math.round((source.getXllCorner() - header.getXllCorner())/header.getCellSize()); - int yOffset = (int) Math.round((-source.getYllCorner() + header.getYllCorner())/header.getCellSize()); + int yOffset = (int) Math.round((-source.getOriginY() + header.getOriginY())/header.getCellSize()); // down from top if (source.getCellSize() % header.getCellSize() != 0) throw new RuntimeException("Cell sizes must be the same or souce an integer multiple of destination"); @@ -60,9 +60,9 @@ public class RasterSet<D extends RasterItem> extends HashMap<RasterKey, D> { public D getFromCoordinates(double source_x, double source_y) { int col = (int) Math.round((source_x - header.getXllCorner())/header.getCellSize()); - // no idea why you need to turn this upside down by taking header.getNrows() and then subtracting, but it works - int row = header.getNrows() - (int) Math.round((source_y - header.getYllCorner())/header.getCellSize()); - + // header.getNrows() - 1, lower left basis. Minus 1 as nrows is number, but indexed from zero + int row = header.getNrows() - 1 - (int) Math.round((source_y - header.getYllCorner())/header.getCellSize()); + if (row < 0 || col < 0) { LogWriter.println("Got negative values"); }