diff --git a/data/functions/generate_average_co2.js b/data/functions/generate_average_co2.js index 8161c139..a5238fa1 100644 --- a/data/functions/generate_average_co2.js +++ b/data/functions/generate_average_co2.js @@ -24,79 +24,90 @@ 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; - }, {}); + 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] = []; + } + 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) => { + if ( + item.emissions_intensity_gco2_per_kwh === null || + item.emissions_intensity_gco2_per_kwh === "" + ) { + return acc; + } + return index; + }, 0); - // 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) => { + 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; - const country = - row.country_code === "" ? row.country_or_region : row.country_code; + gridIntensityResults[country.toUpperCase()] = + row.emissions_intensity_gco2_per_kwh; - 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, + }; + }); - 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 }); - // 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 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, " ") - ); + // This saves the full data set as a JSON file for reference. + fs.writeFileSync( + "data/output/average-intensities.json", + JSON.stringify(generalResults, null, " ") + ); + } catch (error) { + console.error("Error fetching or processing data:", error); + } })(); \ No newline at end of file