@@ -24,79 +24,90 @@ const type = "average";
2424// Save a minified version of the JS file to the src/data folder
2525
2626( async ( ) => {
27- const response = await fetch ( sourceURL ) ;
28- const data = await response . json ( ) ;
29-
30- // Group data by country_code
31- const groupedData = await data . reduce ( ( acc , item ) => {
32- const key =
33- item . country_code === "" ? item . country_or_region : item . country_code ;
34- if ( ! acc [ key ] ) {
35- acc [ key ] = [ ] ;
27+ try {
28+ const response = await fetch ( sourceURL ) ;
29+ if ( ! response . ok ) {
30+ throw new Error ( `Network response was not ok: ${ response . statusText } ` ) ;
3631 }
37- acc [ key ] . push ( item ) ;
38- return acc ;
39- } , { } ) ;
32+ const data = await response . json ( ) ;
33+
34+ // Group data by country_code
35+ const groupedData = await data . reduce ( ( acc , item ) => {
36+ const key =
37+ item . country_code === "" ? item . country_or_region : item . country_code ;
38+ if ( ! acc [ key ] ) {
39+ acc [ key ] = [ ] ;
40+ }
41+ acc [ key ] . push ( item ) ;
42+ return acc ;
43+ } , { } ) ;
44+
45+ // Loop through the grouped data and find the latest year
46+ const latestData = await Object . keys ( groupedData ) . reduce ( ( acc , key ) => {
47+ // Find the last year in the array with emissions intensity data
48+ const latestYear = groupedData [ key ] . reduce ( ( acc , item , index ) => {
49+ if (
50+ item . emissions_intensity_gco2_per_kwh === null ||
51+ item . emissions_intensity_gco2_per_kwh === ""
52+ ) {
53+ return acc ;
54+ }
55+ return index ;
56+ } , 0 ) ;
4057
41- // Loop through the grouped data and find the latest year
42- const latestData = await Object . keys ( groupedData ) . reduce ( ( acc , key ) => {
43- // Find the last year in the array with emissions intensity data
44- const latestYear = groupedData [ key ] . reduce ( ( acc , item , index ) => {
58+ acc [ key ] = groupedData [ key ] [ latestYear ] ;
59+ return acc ;
60+ } , { } ) ;
61+
62+ // Loop through the data and extract the emissions intensity data
63+ // Save it to the gridIntensityResults object with the country code as the key
64+ Object . values ( latestData ) . forEach ( ( row ) => {
4565 if (
46- item . emissions_intensity_gco2_per_kwh === null ||
47- item . emissions_intensity_gco2_per_kwh === ""
66+ row . emissions_intensity_gco2_per_kwh === null ||
67+ row . emissions_intensity_gco2_per_kwh === ""
4868 ) {
49- return acc ;
69+ return ;
5070 }
51- return index ;
52- } , 0 ) ;
53-
54- acc [ key ] = groupedData [ key ] [ latestYear ] ;
55- return acc ;
56- } , { } ) ;
5771
58- // Loop through the data and extract the emissions intensity data
59- // Save it to the gridIntensityResults object with the country code as the key
60- Object . values ( latestData ) . forEach ( ( row ) => {
61- if (
62- row . emissions_intensity_gco2_per_kwh === null ||
63- row . emissions_intensity_gco2_per_kwh === ""
64- ) {
65- return ;
66- }
72+ const country =
73+ row . country_code === "" ? row . country_or_region : row . country_code ;
6774
68- const country =
69- row . country_code === "" ? row . country_or_region : row . country_code ;
75+ gridIntensityResults [ country . toUpperCase ( ) ] =
76+ row . emissions_intensity_gco2_per_kwh ;
7077
71- gridIntensityResults [ country . toUpperCase ( ) ] =
72- row . emissions_intensity_gco2_per_kwh ;
78+ generalResults [ country ] = {
79+ country_code : row . country_code ,
80+ country_or_region : row . country_or_region ,
81+ year : row . year ,
82+ emissions_intensity_gco2_per_kwh : row . emissions_intensity_gco2_per_kwh ,
83+ } ;
84+ } ) ;
7385
74- generalResults [ country ] = {
75- country_code : row . country_code ,
76- country_or_region : row . country_or_region ,
77- year : row . year ,
78- emissions_intensity_gco2_per_kwh : row . emissions_intensity_gco2_per_kwh ,
79- } ;
80- } ) ;
86+ // Ensure directories exist
87+ fs . mkdirSync ( "data/output" , { recursive : true } ) ;
88+ fs . mkdirSync ( "src/data" , { recursive : true } ) ;
8189
82- // This saves the country code and emissions data only, for use in the CO2.js library
83- fs . writeFileSync (
84- "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- `
90- ) ;
91- // Save a minified version to the src folder so that it can be easily imported into the library
92- fs . writeFileSync (
93- "src/data/average-intensities.min.js" ,
94- `const data = ${ JSON . stringify ( gridIntensityResults ) } ; const type = "${ type } "; export { data, type }; export default { data, type };`
95- ) ;
90+ // This saves the country code and emissions data only, for use in the CO2.js library
91+ fs . writeFileSync (
92+ "data/output/average-intensities.js" ,
93+ `const data = ${ JSON . stringify ( gridIntensityResults , null , " " ) } ;
94+ const type = "${ type } ";
95+ export { data, type };
96+ export default { data, type };
97+ `
98+ ) ;
99+ // Save a minified version to the src folder so that it can be easily imported into the library
100+ fs . writeFileSync (
101+ "src/data/average-intensities.min.js" ,
102+ `const data = ${ JSON . stringify ( gridIntensityResults ) } ; const type = "${ type } "; export { data, type }; export default { data, type };`
103+ ) ;
96104
97- // This saves the full data set as a JSON file for reference.
98- fs . writeFileSync (
99- "data/output/average-intensities.json" ,
100- JSON . stringify ( generalResults , null , " " )
101- ) ;
105+ // This saves the full data set as a JSON file for reference.
106+ fs . writeFileSync (
107+ "data/output/average-intensities.json" ,
108+ JSON . stringify ( generalResults , null , " " )
109+ ) ;
110+ } catch ( error ) {
111+ console . error ( "Error fetching or processing data:" , error ) ;
112+ }
102113} ) ( ) ;
0 commit comments