Skip to content

Commit

Permalink
Merged updates to generator scripts from main.
Browse files Browse the repository at this point in the history
  • Loading branch information
fershad committed Oct 23, 2024
1 parent 065b410 commit 3e3fc3e
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 95 deletions.
20 changes: 10 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of
**NB** Changes/commits that are not linked to an issue will not be accepted.

### New issues
- Our issue template provides you with a scaffold to follow when raising a new issue. There are three formats to choose from:
1. *Bugs* - clearly describe the problem you're facing including steps to reproduce it. Please also be sure to include the information about the environment your code was running in (e.g. NodeJS, Deno, Edge Worker, Browser etc).
1. *New features* - clearly describe the new feature you'd like to see added and provide a reason for why it should be added. eg what will be improved/possible as a result of making your suggested change.
1. *Request addition of carbon estimation model to CO2.js* - please provide as much information about the model as possible including links to additional documentation and information about how the model is licensed.


- Our issue template provides you with a scaffold to follow when raising a new issue. There are three formats to choose from:
1. _Bugs_ - clearly describe the problem you're facing including steps to reproduce it. Please also be sure to include the information about the environment your code was running in (e.g. NodeJS, Deno, Edge Worker, Browser etc).
1. _New features_ - clearly describe the new feature you'd like to see added and provide a reason for why it should be added. eg what will be improved/possible as a result of making your suggested change.
1. _Request addition of carbon estimation model to CO2.js_ - please provide as much information about the model as possible including links to additional documentation and information about how the model is licensed.

## Making Changes

- Fork the repository on GitHub.
- Create a topic branch from where you want to base your work. This branch should usually be based off `main`.
- Remember to add JSDoc comments to any new functions or variables that you are introducing into the codebase.
- Make commits of logical units.
- Make commit messages clear and understandable.

Expand All @@ -28,10 +28,10 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of
- Push the changes made in your branch to your fork of this repository.
- Submit a [pull request](https://github.com/thegreenwebfoundation/co2.js/pulls) to the CO2.js repository in the `thegreenwebfoundation` organization.
- When opening a new pull request, you'll see a template. Please follow it. It asks you to state:
- the type of change (choose from a list)
- link to the related issue
- what documentation needs updating as a result (choose from a list)
- what your changes are - be sure to clearly explain the changes you've made, any new files, dependencies, or network requests that have been added, and provide any additional context to help reviewers understand the changes made.
- the type of change (choose from a list)
- link to the related issue
- what documentation needs updating as a result (choose from a list)
- what your changes are - be sure to clearly explain the changes you've made, any new files, dependencies, or network requests that have been added, and provide any additional context to help reviewers understand the changes made.
- Your pull request will be reviewed by a maintainer from Green Web Foundation.
- After feedback has been given we expect responses within two weeks. After two weeks we may close the pull request if it isn't showing any activity.

Expand All @@ -43,4 +43,4 @@ Please note that this project is released with a [Contributor Code of Conduct](h

## Thank you

Again, thank you for your contributions. We appreciate your help improving CO2.js, and we look forward to your future contributions!
Again, thank you for your contributions. We appreciate your help improving CO2.js, and we look forward to your future contributions!
217 changes: 142 additions & 75 deletions data/functions/generate_average_co2.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
/**
* @fileoverview This script generates average CO2 emissions intensity data for countries using the Ember API.
* It processes the data and saves it in various formats for use in the CO2.js library.
* @author Chris Adams
* @version 1.0.0
*/

const fs = require("fs");

// This URL from Ember returns ALL the data for the country_overview_yearly table
/**
* The URL for the Ember API that provides country overview data on a yearly basis.
* @constant {string}
*/
const sourceURL =
"https://ember-data-api-scg3n.ondigitalocean.app/ember/country_overview_yearly.json?_sort=rowid&_shape=array";

/**
* Object to store the grid intensity results for each country.
* @type {Object.<string, number>}
*/
const gridIntensityResults = {};

/**
* Object to store general results including additional country information.
* @type {Object.<string, Object>}
*/
const generalResults = {};

/**
* The type of intensity data being processed (average or marginal).
* @constant {string}
*/
const type = "average";

/**
* This function generates the average CO2 emissions data for each country.
* It reads the data from the Ember API and saves it in the data/output folder.
* It also saves the data as a minified file in the src/data folder, so that it can be imported into the library.
* Fetches data from the Ember API, processes it to extract the latest average CO2 emissions
* intensity data for each country, and saves the results in various formats.
* @async
* @function
* @returns {Promise<void>}
*/

// Use async/await
Expand All @@ -24,79 +51,119 @@ const type = "average";
// Save a minified version of the JS file to the src/data folder

(async () => {
const response = await fetch(sourceURL);
const data = await response.json();

// Group data by country_code
const groupedData = await data.reduce((acc, item) => {
const key =
item.country_code === "" ? item.country_or_region : item.country_code;
if (!acc[key]) {
acc[key] = [];
try {
const response = await fetch(sourceURL);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
acc[key].push(item);
return acc;
}, {});

// Loop through the grouped data and find the latest year
const latestData = await Object.keys(groupedData).reduce((acc, key) => {
// Find the last year in the array with emissions intensity data
const latestYear = groupedData[key].reduce((acc, item, index) => {
const data = await response.json();

/**
* Groups the API data by country code.
* @type {Object.<string, Array>}
*/
const groupedData = await data.reduce((acc, item) => {
const key =
item.country_code === "" ? item.country_or_region : item.country_code;
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item);
return acc;
}, {});

/**
* Extracts the latest year's data for each country.
* @type {Object.<string, Object>}
*/
const latestData = await Object.keys(groupedData).reduce((acc, key) => {
// Find the last year in the array with emissions intensity data
const latestYear = groupedData[key].reduce((acc, item, index) => {
if (
item.emissions_intensity_gco2_per_kwh === null ||
item.emissions_intensity_gco2_per_kwh === ""
) {
return acc;
}
return index;
}, 0);

acc[key] = groupedData[key][latestYear];
return acc;
}, {});

// Loop through the data and extract the emissions intensity data
// Save it to the gridIntensityResults object with the country code as the key
Object.values(latestData).forEach((row) => {
if (
item.emissions_intensity_gco2_per_kwh === null ||
item.emissions_intensity_gco2_per_kwh === ""
row.emissions_intensity_gco2_per_kwh === null ||
row.emissions_intensity_gco2_per_kwh === ""
) {
return acc;
return;
}
return index;
}, 0);

acc[key] = groupedData[key][latestYear];
return acc;
}, {});

// Loop through the data and extract the emissions intensity data
// Save it to the gridIntensityResults object with the country code as the key
Object.values(latestData).forEach((row) => {
if (
row.emissions_intensity_gco2_per_kwh === null ||
row.emissions_intensity_gco2_per_kwh === ""
) {
return;
}

const country =
row.country_code === "" ? row.country_or_region : row.country_code;

gridIntensityResults[country.toUpperCase()] =
row.emissions_intensity_gco2_per_kwh;

generalResults[country] = {
country_code: row.country_code,
country_or_region: row.country_or_region,
year: row.year,
emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh,
};
});

// This saves the country code and emissions data only, for use in the CO2.js library
fs.writeFileSync(
"data/output/average-intensities.js",
`const data = ${JSON.stringify(gridIntensityResults, null, " ")};
const type = "${type}";
export { data, type };
export default { data, type };
`
);
// Save a minified version to the src folder so that it can be easily imported into the library
fs.writeFileSync(
"src/data/average-intensities.min.js",
`const data = ${JSON.stringify(gridIntensityResults)}; const type = "${type}"; export { data, type }; export default { data, type };`
);

// This saves the full data set as a JSON file for reference.
fs.writeFileSync(
"data/output/average-intensities.json",
JSON.stringify(generalResults, null, " ")
);
})();
const country =
row.country_code === "" ? row.country_or_region : row.country_code;

gridIntensityResults[country.toUpperCase()] =
row.emissions_intensity_gco2_per_kwh;

generalResults[country] = {
country_code: row.country_code,
country_or_region: row.country_or_region,
year: row.year,
emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh,
};
});

// Ensure directories exist
fs.mkdirSync("data/output", { recursive: true });
fs.mkdirSync("src/data", { recursive: true });

const jsDocComments = `/**
* @fileoverview Minified average CO2 emissions intensity data for countries.
* @generated Generated by generate_average_co2.js
* @version 1.0.0
*/
/**
* @constant {Object.<string, number>} data - Average CO2 emissions intensity data for various countries.
* @constant {string} type - Type of data being represented.
*/`;

/**
* Saves the country code and emissions data for use in the CO2.js library.
* @type {void}
*/
fs.writeFileSync(
"data/output/average-intensities.js",
`const data = ${JSON.stringify(gridIntensityResults, null, " ")};
const type = "${type}";
export { data, type };
export default { data, type };
`
);

/**
* Saves a minified version of the data for easy import into the library.
* @type {void}
*/
fs.writeFileSync(
"src/data/average-intensities.min.js",
`${jsDocComments}
const data = ${JSON.stringify(
gridIntensityResults
)}; const type = "${type}"; export { data, type }; export default { data, type };`
);

/**
* Saves the full data set as a JSON file for reference.
* @type {void}
*/
fs.writeFileSync(
"data/output/average-intensities.json",
JSON.stringify(generalResults, null, " ")
);
} catch (error) {
console.error("Error fetching or processing data:", error);
}
})();
Loading

0 comments on commit 3e3fc3e

Please sign in to comment.