Skip to content

Commit

Permalink
CO2 checks ref #139
Browse files Browse the repository at this point in the history
  • Loading branch information
corradio committed Nov 20, 2016
1 parent f112dbf commit 2c803ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ function processDatabaseResults(countries, exchanges) {
d3.max(d3.values(country.production));
country.totalProduction =
d3.sum(d3.values(country.production));
country.totalNetExchange =
d3.sum(d3.values(country.exchange));
country.totalImport =
d3.sum(d3.values(country.exchange), function(d) {
return d >= 0 ? d : 0;
}) || 0;
country.totalExport =
d3.sum(d3.values(country.exchange), function(d) {
return d <= 0 ? -d : 0;
}) || 0;
country.totalNetExchange = country.totalImport - country.totalExport;
country.maxExport =
-Math.min(d3.min(d3.values(country.exchange)), 0) || 0;
});
Expand Down
6 changes: 5 additions & 1 deletion api/static/app/co2eq.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ function Co2eqCalculator() {

this.compute = function(countries) {
var validCountries = d3.values(countries)
.filter(function (d) { return d.countryCode && d.production; });
.filter(function (d) { return d.countryCode && d.production; })
.filter(function (d) {
// Double check that total production + import >= export
return (d.totalProduction + d.totalImport) >= d.totalExport;
});
var validCountryKeys = validCountries.map(function (d) { return d.countryCode });
// x_i: unknown co2 (consumption) footprint of i-th country
// f_ij: known co2 footprint of j-th system of i-th country
Expand Down

0 comments on commit 2c803ca

Please sign in to comment.