Skip to content
Snippets Groups Projects
Commit d8a84e8c authored by Bart Arendarczyk's avatar Bart Arendarczyk
Browse files

Changes to allow reading in CRAFTY production data (UK-SSP).

parent 8f3854a0
No related branches found
No related tags found
No related merge requests found
Country
United Kingdom
package ac.ed.lurg.country.crafty; package ac.ed.lurg.country.crafty;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import ac.ed.lurg.country.AbstractCountryAgent; import ac.ed.lurg.country.AbstractCountryAgent;
import ac.ed.lurg.country.CompositeCountry; import ac.ed.lurg.country.CompositeCountry;
import ac.ed.lurg.country.GlobalPrice; import ac.ed.lurg.country.GlobalPrice;
import ac.ed.lurg.demand.AbstractDemandManager; import ac.ed.lurg.demand.AbstractDemandManager;
import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.CropUsageData;
import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.CommodityType;
import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.CropType;
/** /**
* Country agent that is interface over data transferred to and from CRAFTY. * Country agent that is interface over data transferred to and from CRAFTY.
* Need to configure country mapping to that there is * Need to configure country mapping to that there is
* */ * */
public class CraftyCountryAgent extends AbstractCountryAgent { public class CraftyCountryAgent extends AbstractCountryAgent {
private Map<CropType, CropUsageData> cropUsageData; private Map<CropType, CropUsageData> cropUsageData;
public CraftyCountryAgent(AbstractDemandManager demandManager,CompositeCountry country, Map<CropType, Double> tradeBarriers) { public CraftyCountryAgent(AbstractDemandManager demandManager,CompositeCountry country, Map<CropType, Double> tradeBarriers) {
super(demandManager, country, tradeBarriers); super(demandManager, country, tradeBarriers);
} }
public Map<CropType, CropUsageData> getCropUsageData() { public Map<CropType, CropUsageData> getCropUsageData() {
return cropUsageData; return cropUsageData;
} }
public void updateProduction(Map<CropType, Double> cropProduction, Map<CropType, GlobalPrice> worldPrices) { public void updateProduction(Map<CropType, CropUsageData> cropUsageMap, Map<CropType, GlobalPrice> worldPrices) {
calculateCountryPricesAndDemand(worldPrices, false); this.cropUsageData = cropUsageMap;
calculateCountryPricesAndDemand(worldPrices, false);
cropUsageData = new HashMap<CropType, CropUsageData>(); updateNetImportsFromProdAndDemand(currentProjectedDemand, currentMinDemandFract, cropUsageMap);
for (Entry<CropType, Double> entry : cropProduction.entrySet()) { }
CropUsageData cropusage = new CropUsageData(entry.getValue());
cropUsageData.put(entry.getKey(), cropusage); @Override
} protected double getCommPriceFromCropPrice(CommodityType commodity) {
double commPricePlum = 0;
updateNetImportsFromProdAndDemand(currentProjectedDemand, currentMinDemandFract, cropUsageData); Map<CropType, Double> demandFract = baseDemandFact.get(commodity);
}
for (CropType crop : commodity.getCropTypes()) {
@Override commPricePlum += currentCountryPrices.get(crop).getImportPrice() * demandFract.get(crop); // weight price by base demand of each cereal crop
protected double getCommPriceFromCropPrice(CommodityType commodity) { }
double commPricePlum = 0; return commPricePlum;
Map<CropType, Double> demandFract = baseDemandFact.get(commodity); }
for (CropType crop : commodity.getCropTypes()) {
commPricePlum += currentCountryPrices.get(crop).getImportPrice() * demandFract.get(crop); // weight price by base demand of each cereal crop
}
return commPricePlum;
}
} }
\ No newline at end of file
package ac.ed.lurg.country.crafty; package ac.ed.lurg.country.crafty;
import java.io.File; 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.List; import java.util.List;
import java.util.Map; import java.util.Map;
import ac.ed.lurg.ModelConfig; import ac.ed.lurg.ModelConfig;
import ac.ed.lurg.Timestep; import ac.ed.lurg.Timestep;
import ac.ed.lurg.country.CompositeCountry; import ac.ed.lurg.country.CompositeCountry;
import ac.ed.lurg.country.GlobalPrice; import ac.ed.lurg.country.GlobalPrice;
import ac.ed.lurg.types.CropType; import ac.ed.lurg.landuse.CropUsageData;
import ac.ed.lurg.utils.LogWriter; import ac.ed.lurg.types.CropType;
import ac.ed.lurg.utils.StringTabularReader; import ac.ed.lurg.utils.LogWriter;
import ac.ed.lurg.utils.WatchForFile; import ac.ed.lurg.utils.StringTabularReader;
import ac.ed.lurg.utils.WatchForFile;
public class CraftyProdManager {
public class CraftyProdManager {
private Collection<CompositeCountry> craftyCountries;
private Collection<CompositeCountry> craftyCountries;
public synchronized Collection<CompositeCountry> getCraftyCountries() {
if (craftyCountries == null) { public synchronized Collection<CompositeCountry> getCraftyCountries() {
craftyCountries = new ArrayList<CompositeCountry>(); if (craftyCountries == null) {
StringTabularReader tabularReader = new StringTabularReader(",", new String[]{"Country"}); craftyCountries = new ArrayList<CompositeCountry>();
List<Map<String, String>> rows = tabularReader.read(ModelConfig.CRAFTY_PRODUCTION_DIR + File.separator + "craftyCountries.csv"); StringTabularReader tabularReader = new StringTabularReader(",", new String[]{"Country"});
for (Map<String, String> row : rows) { List<Map<String, String>> rows = tabularReader.read(ModelConfig.DATA_DIR + File.separator + "craftyCountries.csv");
craftyCountries.add(new CompositeCountry(row.get("Country"), "craftycountry")); for (Map<String, String> row : rows) {
} craftyCountries.add(new CompositeCountry(row.get("Country"), "craftycountry"));
} }
return craftyCountries; }
} return craftyCountries;
}
public void updateWithCraftyData(Collection<CraftyCountryAgent> craftyCountryAgents, Timestep timestep, Map<CropType, GlobalPrice> worldPrices) {
String rootDir = ModelConfig.CRAFTY_PRODUCTION_DIR + File.separator + timestep.getYear(); public void updateWithCraftyData(Collection<CraftyCountryAgent> craftyCountryAgents, Timestep timestep, Map<CropType, GlobalPrice> worldPrices) {
long startTime = System.currentTimeMillis(); String rootDir = ModelConfig.CRAFTY_PRODUCTION_DIR + File.separator + timestep.getYear();
long startTime = System.currentTimeMillis();
WatchForFile fileWatcher = new WatchForFile(new File(rootDir + File.separator + "done"));
boolean foundFile = fileWatcher.await(ModelConfig.LPJG_MONITOR_TIMEOUT_SEC); WatchForFile fileWatcher = new WatchForFile(new File(rootDir + File.separator + "done"));
if (!foundFile) { boolean foundFile = fileWatcher.await(ModelConfig.LPJG_MONITOR_TIMEOUT_SEC);
LogWriter.printlnError("Not able to find marker file. May have timed out."); if (!foundFile) {
throw new RuntimeException("Not able to find marker file. May have timed out."); LogWriter.printlnError("Not able to find marker file. May have timed out.");
} throw new RuntimeException("Not able to find marker file. May have timed out.");
LogWriter.println("Found marker file in " + (System.currentTimeMillis() - startTime) + " ms"); }
LogWriter.println("Found marker file in " + (System.currentTimeMillis() - startTime) + " ms");
StringTabularReader tabularReader = new StringTabularReader(",", new String[]{"Country","Crop","Production"});
tabularReader.read(rootDir + File.separator + "production.csv"); StringTabularReader tabularReader = new StringTabularReader(",", new String[]{"Country","Crop","Production","MonogastricFeed","RuminantFeed","NetImportsExpected"});
tabularReader.read(rootDir + File.separator + "production.csv");
for (CraftyCountryAgent cca : craftyCountryAgents) {
Map<CropType, Double> cropProduction = new HashMap<CropType, Double>(); for (CraftyCountryAgent cca : craftyCountryAgents) {
Map<CropType, CropUsageData> cropUsageMap = new HashMap<CropType, CropUsageData>();
Map<String, String> queryMap = new HashMap<String, String>();
queryMap.put("Country", cca.getCountry().getName()); Map<String, String> queryMap = new HashMap<String, String>();
queryMap.put("Country", cca.getCountry().getName());
try {
List<Map<String, String>> rows = tabularReader.query(queryMap); try {
for (Map<String, String> row : rows) { List<Map<String, String>> rows = tabularReader.query(queryMap);
String cropS = row.get("Crop"); for (Map<String, String> row : rows) {
CropType crop = CropType.getForGamsName(cropS); String cropS = row.get("Crop");
String prodS = row.get("Production"); CropType crop = CropType.getForGamsName(cropS);
Double prod = Double.valueOf(prodS); String prodS = row.get("Production");
cropProduction.put(crop, prod); Double prod = Double.valueOf(prodS);
} String monoFeedS = row.get("MonogastricFeed");
if (cropProduction.size() < CropType.getImportedTypes().size()) { // Don't need setaside or pasture, which aren't imported either Double monoFeed = Double.valueOf(monoFeedS);
LogWriter.printlnError("Not all crops present in Crafty production for country: " + cca.getCountry() + " only " + cropProduction.size()); String rumFeedS = row.get("MonogastricFeed");
} Double rumFeed = Double.valueOf(rumFeedS);
cca.updateProduction(cropProduction, worldPrices); String netImpS = row.get("NetImportsExpected");
} Double netImp = Double.valueOf(netImpS);
catch (Exception e) {
LogWriter.println("Problem getting Crafty data for: " + cca.getCountry()); CropUsageData cropUsageItem = new CropUsageData(rumFeed, monoFeed, netImp, prod);
} cropUsageMap.put(crop, cropUsageItem);
} }
} if (cropUsageMap.size() < CropType.getImportedTypes().size()) { // Don't need setaside or pasture, which aren't imported either
LogWriter.printlnError("Not all crops present in Crafty production for country: " + cca.getCountry() + " only " + cropUsageMap.size());
}
cca.updateProduction(cropUsageMap, worldPrices);
}
catch (Exception e) {
LogWriter.println("Problem getting Crafty data for: " + cca.getCountry());
}
}
}
} }
\ No newline at end of file
package ac.ed.lurg.landuse; package ac.ed.lurg.landuse;
import java.io.Serializable; import java.io.Serializable;
public class CropUsageData implements Serializable { public class CropUsageData implements Serializable {
private static final long serialVersionUID = 1200684888140690926L; private static final long serialVersionUID = 1200684888140690926L;
private double ruminantFeed; private double ruminantFeed;
private double monogastricFeed; private double monogastricFeed;
private double netImportsExpected; private double netImportsExpected;
private double prodShock; private double prodShock;
private double netImportCostExpected; private double netImportCostExpected;
private double prod; private double prod;
private double prodCost; private double prodCost;
private double area; private double area;
public CropUsageData(double prod) { public CropUsageData(double prod) {
this.prod = prod; this.prod = prod;
} }
public CropUsageData(double ruminantFeed, double monogastricFeed, double netImportsExpected, double netImportCost, double prod, double prodCost, double area, double prodShock) { public CropUsageData(double ruminantFeed, double monogastricFeed, double netImportsExpected, double netImportCost, double prod, double prodCost, double area, double prodShock) {
this.ruminantFeed = ruminantFeed; this.ruminantFeed = ruminantFeed;
this.monogastricFeed = monogastricFeed; this.monogastricFeed = monogastricFeed;
this.netImportsExpected = netImportsExpected; this.netImportsExpected = netImportsExpected;
this.netImportCostExpected = netImportCost; this.netImportCostExpected = netImportCost;
this.prod = prod; this.prod = prod;
this.prodCost = prodCost; this.prodCost = prodCost;
this.area= area; this.area= area;
this.prodShock = prodShock; this.prodShock = prodShock;
} }
public double getRuminantFeed() { public CropUsageData(double ruminantFeed, double monogastricFeed, double netImportsExpected, double prod) {
return ruminantFeed; this.ruminantFeed = ruminantFeed;
} this.monogastricFeed = monogastricFeed;
this.netImportsExpected = netImportsExpected;
public double getMonogastricFeed() { this.prod = prod;
return monogastricFeed; }
}
public double getRuminantFeed() {
public double getNetImportCostExpected() { return ruminantFeed;
return netImportCostExpected; }
}
public double getMonogastricFeed() {
public double getNetImportsExpected() { return monogastricFeed;
return netImportsExpected; }
}
public double getNetImportCostExpected() {
public double getProductionExpected() { return netImportCostExpected;
return prod; }
}
public double getNetImportsExpected() {
public double getShockedNetImports() { return netImportsExpected;
return netImportsExpected + prodShock; }
}
public double getProductionExpected() {
public double getProductionShock() { return prod;
return prodShock; }
}
public double getShockedNetImports() {
public double getTotalProdCost() { return netImportsExpected + prodShock;
return prodCost; }
}
public double getProductionShock() {
public double getProdCostRate() { return prodShock;
if (prod - prodShock <= 0.0) }
return Double.NaN;
else public double getTotalProdCost() {
return prodCost / (prod - prodShock); return prodCost;
} }
public void updateNetImports(double netImports) { public double getProdCostRate() {
this.netImportsExpected = netImports; if (prod - prodShock <= 0.0)
} return Double.NaN;
else
public double getArea(){ return prodCost / (prod - prodShock);
return area; }
}
public void updateNetImports(double netImports) {
this.netImportsExpected = netImports;
}
public double getArea(){
return area;
}
} }
\ No newline at end of file
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