1+ /**
2+ * @fileoverview This script generates average CO2 emissions intensity data for countries using the Ember API.
3+ * It processes the data and saves it in various formats for use in the CO2.js library.
4+ * @author Chris Adams
5+ * @version 1.0.0
6+ */
7+
18const fs = require ( "fs" ) ;
29
3- // This URL from Ember returns ALL the data for the country_overview_yearly table
10+ /**
11+ * The URL for the Ember API that provides country overview data on a yearly basis.
12+ * @constant {string}
13+ */
414const sourceURL =
515 "https://ember-data-api-scg3n.ondigitalocean.app/ember/country_overview_yearly.json?_sort=rowid&_shape=array" ;
16+
17+
18+ /**
19+ * Object to store the grid intensity results for each country.
20+ * @type {Object.<string, number> }
21+ */
622const gridIntensityResults = { } ;
23+
24+
25+ /**
26+ * Object to store general results including additional country information.
27+ * @type {Object.<string, Object> }
28+ */
729const generalResults = { } ;
30+
31+
32+ /**
33+ * The type of intensity data being processed (average or marginal).
34+ * @constant {string}
35+ */
836const type = "average" ;
937
1038/**
11- * This function generates the average CO2 emissions data for each country.
12- * It reads the data from the Ember API and saves it in the data/output folder.
13- * It also saves the data as a minified file in the src/data folder, so that it can be imported into the library.
39+ * Fetches data from the Ember API, processes it to extract the latest average CO2 emissions
40+ * intensity data for each country, and saves the results in various formats.
41+ * @async
42+ * @function
43+ * @returns {Promise<void> }
1444 */
1545
1646// Use async/await
@@ -27,7 +57,10 @@ const type = "average";
2757 const response = await fetch ( sourceURL ) ;
2858 const data = await response . json ( ) ;
2959
30- // Group data by country_code
60+ /**
61+ * Groups the API data by country code.
62+ * @type {Object.<string, Array> }
63+ */
3164 const groupedData = await data . reduce ( ( acc , item ) => {
3265 const key =
3366 item . country_code === "" ? item . country_or_region : item . country_code ;
@@ -38,7 +71,10 @@ const type = "average";
3871 return acc ;
3972 } , { } ) ;
4073
41- // Loop through the grouped data and find the latest year
74+ /**
75+ * Extracts the latest year's data for each country.
76+ * @type {Object.<string, Object> }
77+ */
4278 const latestData = await Object . keys ( groupedData ) . reduce ( ( acc , key ) => {
4379 // Find the last year in the array with emissions intensity data
4480 const latestYear = groupedData [ key ] . reduce ( ( acc , item , index ) => {
@@ -79,22 +115,32 @@ const type = "average";
79115 } ;
80116 } ) ;
81117
82- // This saves the country code and emissions data only, for use in the CO2.js library
118+ /**
119+ * Saves the country code and emissions data for use in the CO2.js library.
120+ * @type {void }
121+ */
83122 fs . writeFileSync (
84123 "data/output/average-intensities.js" ,
85- `const data = ${ JSON . stringify ( gridIntensityResults , null , " " ) } ;
86- const type = "${ type } ";
87- export { data, type };
88- export default { data, type };
89- `
124+ `
125+ const data = ${ JSON . stringify ( gridIntensityResults , null , " " ) } ;
126+ const type = "${ type } ";
127+ export { data, type };
128+ export default { data, type };
129+ `
90130 ) ;
91- // Save a minified version to the src folder so that it can be easily imported into the library
131+ /**
132+ * Saves a minified version of the data for easy import into the library.
133+ * @type {void }
134+ */
92135 fs . writeFileSync (
93136 "src/data/average-intensities.min.js" ,
94137 `const data = ${ JSON . stringify ( gridIntensityResults ) } ; const type = "${ type } "; export { data, type }; export default { data, type };`
95138 ) ;
96139
97- // This saves the full data set as a JSON file for reference.
140+ /**
141+ * Saves the full data set as a JSON file for reference.
142+ * @type {void }
143+ */
98144 fs . writeFileSync (
99145 "data/output/average-intensities.json" ,
100146 JSON . stringify ( generalResults , null , " " )
0 commit comments