@tgwf/co2 0.10.3 → 0.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/.eslintignore +1 -0
  2. package/CHANGELOG.md +43 -17
  3. package/LICENSE +215 -176
  4. package/README.md +45 -2
  5. package/data/.prettierignore +7 -0
  6. package/data/IFI_Default_Grid_Factors_2021_v3.1_unfccc.csv +246 -0
  7. package/data/Lean-ICT-Materials-1byte-Model-2018.xlsx +0 -0
  8. package/data/Lean-ICT-Materials-Forecast-Model-2018.xlsx +0 -0
  9. package/data/co2-intensities-ember-2020.csv +222 -0
  10. package/data/co2-intensities-ember-2021.csv +87 -0
  11. package/data/fixtures/countries.csv +247 -0
  12. package/data/fixtures/tgwf.har +6107 -0
  13. package/data/fixtures/url2green.test.db +0 -0
  14. package/data/fixtures/url2green.test.json +1 -0
  15. package/data/fixtures/url2green.test.json.gz +0 -0
  16. package/data/functions/generate_average_co2.js +74 -0
  17. package/data/functions/generate_marginal_co2.js +93 -0
  18. package/data/generate_co2_constants.py +28 -0
  19. package/data/output/average-intensities-2021.js +92 -0
  20. package/data/output/average-intensities-2021.json +776 -0
  21. package/data/output/marginal-intensities-2021.js +238 -0
  22. package/data/output/marginal-intensities-2021.json +2335 -0
  23. package/data/utils/getCountryCodes.js +32 -0
  24. package/data/utils/mapCountries.js +36 -0
  25. package/data/utils/parseCSVRow.js +29 -0
  26. package/dist/cjs/co2.js +7 -2
  27. package/dist/cjs/co2.js.map +2 -2
  28. package/dist/cjs/co2.test.js +24 -13
  29. package/dist/cjs/co2.test.js.map +2 -2
  30. package/dist/cjs/data/average-intensities-2021.min.js +30 -0
  31. package/dist/cjs/data/average-intensities-2021.min.js.map +7 -0
  32. package/dist/cjs/data/marginal-intensities-2021.min.js +30 -0
  33. package/dist/cjs/data/marginal-intensities-2021.min.js.map +7 -0
  34. package/dist/cjs/index-node.js +6 -2
  35. package/dist/cjs/index-node.js.map +2 -2
  36. package/dist/cjs/index.js +6 -2
  37. package/dist/cjs/index.js.map +2 -2
  38. package/dist/cjs/sustainable-web-design.js +0 -2
  39. package/dist/cjs/sustainable-web-design.js.map +2 -2
  40. package/dist/esm/co2.js +7 -2
  41. package/dist/esm/co2.test.js +23 -12
  42. package/dist/esm/data/average-intensities-2021.min.js +10 -0
  43. package/dist/esm/data/marginal-intensities-2021.min.js +10 -0
  44. package/dist/esm/index.js +6 -2
  45. package/dist/esm/sustainable-web-design.js +0 -2
  46. package/dist/iife/index.js +4 -0
  47. package/dist/iife/index.js.map +7 -0
  48. package/package.json +8 -2
  49. package/public/index.js +3 -1
  50. package/public/index.js.map +2 -2
  51. package/src/co2.js +12 -6
  52. package/src/data/average-intensities-2021.min.js +1 -0
  53. package/src/data/marginal-intensities-2021.min.js +1 -0
  54. package/src/index-node.js +4 -3
  55. package/src/index.js +4 -2
Binary file
@@ -0,0 +1 @@
1
+ ["google.com","maxcdn.bootstrapcdn.com","thegreenwebfoundation.org","www.thegreenwebfoundation.org","fonts.googleapis.com","ajax.googleapis.com","assets.digitalclimatestrike.net","cdnjs.cloudflare.com","graphite.thegreenwebfoundation.org","analytics.thegreenwebfoundation.org","fonts.gstatic.com","api.thegreenwebfoundation.org"]
@@ -0,0 +1,74 @@
1
+ const fs = require("fs");
2
+ const csv = fs.readFileSync("data/co2-intensities-ember-2021.csv");
3
+ const parseCSVRow = require("../utils/parseCSVRow");
4
+ const getCountryCodes = require("../utils/getCountryCodes");
5
+ const type = "average";
6
+ const source = "Ember";
7
+ const year = "2021";
8
+
9
+ const array = csv.toString().split("\n");
10
+
11
+ /* Store the converted result into an array */
12
+ const csvToJsonResult = {};
13
+ const gridIntensityResults = {};
14
+
15
+ /* Store the CSV column headers into separate variable */
16
+ const headers = parseCSVRow(array[0]);
17
+
18
+ /* Iterate over the remaining data rows */
19
+ for (let currentArrayString of array) {
20
+ // If there's an empty line, keep calm and carry on.
21
+ // Also, skip the first row since those are the headers.
22
+ if (currentArrayString.length === 0 || currentArrayString === array[0]) continue;
23
+
24
+ /* Empty object to store result in key value pair */
25
+ const jsonObject = {}
26
+
27
+ // Split the string by the pipe character
28
+ let jsonProperties = parseCSVRow(currentArrayString);
29
+
30
+ // Ember sets the country value (3-digit country code) in the first column. If data is for a region instead, it will be in the second column.
31
+ // We can assign the current country (or region) by using these fields.
32
+ let country = jsonProperties[0] || jsonProperties[1];
33
+
34
+ // Loop through the headers and assign the values to the JSON object
35
+ for (let column in headers) {
36
+ // First check if the current property is an array string. If so, then we'll split it and map the results to an array.
37
+ // We trim the values to remove any whitespace.
38
+ if (jsonProperties[column].includes(",")) {
39
+ jsonObject[headers[column]] = jsonProperties[column]
40
+ .split(",").map(item => item.trim());
41
+ } else {
42
+ // Otherwise, just assign the value to the JSON object.
43
+ // We replace \r with an empty string to remove any carriage returns.
44
+ jsonObject[headers[column].replace("\r", "")] = jsonProperties[column].replace("\r", "")
45
+ }
46
+
47
+ // This extracts only the emissions intensity data from the CSV.
48
+ // We use this to generate a smaller data file which can be later imported into CO2.js
49
+ if (headers[column].startsWith('emissions_intensity_gco2_per_kwh')) {
50
+ gridIntensityResults[country.toUpperCase()] = jsonProperties[column].replace("\r", "")
51
+ }
52
+ }
53
+
54
+ // Ember keeps the country name in the 2nd column, so we'll use that to map the ISO country codes
55
+ const countryCodes = getCountryCodes('ember_country_name', jsonProperties[1].toLowerCase());
56
+
57
+ /* Push the genearted JSON object to resultant array */
58
+ csvToJsonResult[country] = {...jsonObject, ...countryCodes};
59
+ }
60
+ /* Convert the final array to JSON */
61
+ const json = JSON.stringify(csvToJsonResult);
62
+ const gridIntensityJson = JSON.stringify(gridIntensityResults);
63
+
64
+ // This saves the country code and emissions data only, for use in the CO2.js library
65
+ fs.writeFileSync("data/output/average-intensities-2021.js", `const data = ${gridIntensityJson};
66
+ const type = "${type}";
67
+ const year = "${year}";
68
+ export { data, type, year };
69
+ export default { data, type, year };`);
70
+ // Save a minified version to the src folder so that it can be easily imported into the library
71
+ fs.writeFileSync("src/data/average-intensities-2021.min.js", `const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };`);
72
+
73
+ // This saves the full data set as a JSON file for reference.
74
+ fs.writeFileSync("data/output/average-intensities-2021.json", json);
@@ -0,0 +1,93 @@
1
+ const fs = require("fs");
2
+ const csv = fs.readFileSync("data/IFI_Default_Grid_Factors_2021_v3.1_unfccc.csv");
3
+ const parseCSVRow = require("../utils/parseCSVRow");
4
+ const getCountryCodes = require("../utils/getCountryCodes");
5
+ const type = "marginal";
6
+ const source = "UNFCCC";
7
+ const year = "2021";
8
+
9
+ const array = csv.toString().split("\n");
10
+
11
+ /* Store the converted result into an array */
12
+ const csvToJsonResult = {};
13
+ const gridIntensityResults = {};
14
+
15
+ /* Store the CSV column headers into seprate variable */
16
+ const rowHeaders = parseCSVRow(array[4]);
17
+ rowHeaders.push(...parseCSVRow(array[3]));
18
+ rowHeaders.push("Operating Margin Grid Emission");
19
+
20
+ const headers = rowHeaders.filter((header) => header !== "");
21
+
22
+ /* Iterate over the remaining data rows */
23
+ // Here we remove the first 5 items in the array, since these are the headings which we have already accounted for
24
+ for (let currentArrayString of array.slice(5)) {
25
+
26
+ // If there's an empty line, keep calm and carry on.
27
+ if (currentArrayString.length === 0) continue;
28
+
29
+ /* Empty object to store result in key value pair */
30
+ const jsonObject = {};
31
+
32
+ // Split the string by the pipe character
33
+ let jsonProperties = parseCSVRow(currentArrayString);
34
+
35
+ if (jsonProperties.length < 2) continue;
36
+
37
+ // The UNFCCC data sets the country value (full country name as string) in the first column, so we can extract that.
38
+ let country = jsonProperties[0];
39
+
40
+ // If there's no value for the country, then we can skip this row.
41
+ if (!country || country === "") continue;
42
+
43
+ // UNFCCC keeps the country name in the 1st column, so we'll use that to map the ISO country codes
44
+ const countryCodes = getCountryCodes('unfccc_country_name', country.toLowerCase());
45
+
46
+ // Loop through the headers and assign the values to the JSON object
47
+ for (let column in headers) {
48
+ if (!column || column === "") continue;
49
+
50
+ // First check if the current property is an array string. If so, then we'll split it and map the results to an array.
51
+ // We trim the values to remove any whitespace.
52
+ if (jsonProperties[column].includes(",")) {
53
+ jsonObject[headers[column]] = jsonProperties[column]
54
+ .split(",")
55
+ .map((item) => item.trim());
56
+ } else {
57
+ // Otherwise, just assign the value to the JSON object.
58
+ // We replace \r with an empty string to remove any carriage returns.
59
+ // We also remove any quotations in the strings.
60
+ jsonObject[headers[column].replace("\r", "").replaceAll('\"', "")] =
61
+ jsonProperties[column].replace("\r", "").replace('\"', "");
62
+ }
63
+
64
+ if (headers[column].startsWith("Operating Margin Grid Emission")) {
65
+ gridIntensityResults[countryCodes.country_code_iso_3.toUpperCase() || country.toUpperCase()] = jsonProperties[column]
66
+ .replace("\r", "")
67
+ .replace('\"', "");
68
+ }
69
+ }
70
+
71
+
72
+ /* Push the genearted JSON object to resultant array */
73
+ csvToJsonResult[country] = {...jsonObject, ...countryCodes};;
74
+ }
75
+ /* Convert the final array to JSON */
76
+ const json = JSON.stringify(csvToJsonResult);
77
+ const gridIntensityJson = JSON.stringify(gridIntensityResults);
78
+
79
+ // This saves the country code and emissions data only, for use in the CO2.js library
80
+ fs.writeFileSync(
81
+ "data/output/marginal-intensities-2021.js",
82
+ `const data = ${gridIntensityJson};
83
+ const type = "${type}";
84
+ const year = "${year}";
85
+ export { data, type, year };
86
+ export default { data, type, year };`);
87
+ // Save a minified version to the src folder so that it can be easily imported into the library
88
+ fs.writeFileSync(
89
+ "src/data/marginal-intensities-2021.min.js",
90
+ `const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };` );
91
+
92
+ // This saves the full data set as a JSON file for reference.
93
+ fs.writeFileSync("data/output/marginal-intensities-2021.json", json);
@@ -0,0 +1,28 @@
1
+ import csv
2
+ import json
3
+
4
+ intensity_by_country = {}
5
+ rows_2020 = None
6
+ rows_2021 = None
7
+
8
+ # build our first data structure
9
+ with open("data/co2-intensities-ember-2020.csv") as ember2020:
10
+
11
+ rows_2020 = csv.DictReader(ember2020)
12
+ for row in rows_2020:
13
+ if row["country_code"]:
14
+ intensity_by_country[row["country_code"]] = row
15
+
16
+
17
+ # now add the latest year
18
+ with open("data/co2-intensities-ember-2021.csv") as ember2021:
19
+ rows_2021 = csv.DictReader(ember2021)
20
+ for row in rows_2021:
21
+ if row["country_code"]:
22
+ intensity_by_country[row["country_code"]] = row
23
+
24
+ # TODO add the figure for world, and the EU, africa and other groupings
25
+
26
+ with open("data/intensity-by-country.json", "w+") as outfile:
27
+ outfile.write(json.dumps(intensity_by_country))
28
+
@@ -0,0 +1,92 @@
1
+ const data = {
2
+ AFRICA: "489.26",
3
+ ARG: "365.292",
4
+ ARM: "206.522",
5
+ ASIA: "543.57",
6
+ AUS: "526.876",
7
+ AUT: "145.083",
8
+ AZE: "536.585",
9
+ BGD: "559.606",
10
+ BLR: "472.727",
11
+ BEL: "156.063",
12
+ BOL: "311.475",
13
+ BIH: "470.982",
14
+ BRA: "144.677",
15
+ BGR: "364.136",
16
+ BDI: "275.862",
17
+ CAN: "123.859",
18
+ CHL: "395.565",
19
+ CHN: "549.288",
20
+ CRI: "30.903",
21
+ HRV: "212.161",
22
+ CYP: "601.19",
23
+ CZE: "401.272",
24
+ DNK: "240.419",
25
+ ECU: "132.964",
26
+ EGY: "470.879",
27
+ SLV: "180.87",
28
+ EST: "488.529",
29
+ EU: "261.43",
30
+ EUROPE: "277.64",
31
+ FIN: "152.651",
32
+ FRA: "67.781",
33
+ G20: "445.9",
34
+ G7: "338.04",
35
+ GEO: "105.685",
36
+ DEU: "363.982",
37
+ GRC: "363.388",
38
+ HUN: "236.271",
39
+ IND: "632.656",
40
+ IRL: "361.274",
41
+ ITA: "340.937",
42
+ JPN: "460.647",
43
+ KAZ: "656.097",
44
+ KEN: "104.0",
45
+ "LATIN AMERICA AND CARIBBEAN": "261.51",
46
+ LVA: "226.351",
47
+ LTU: "247.475",
48
+ LUX: "183.824",
49
+ MLT: "452.055",
50
+ MEX: "391.582",
51
+ MDA: "642.512",
52
+ MNG: "725.26",
53
+ MNE: "335.958",
54
+ NLD: "386.189",
55
+ "NORTH AMERICA": "345.38",
56
+ MKD: "444.191",
57
+ NOR: "26.131",
58
+ OCEANIA: "479.98",
59
+ OECD: "338.24",
60
+ PAK: "363.065",
61
+ PER: "241.492",
62
+ PHL: "579.689",
63
+ POL: "657.138",
64
+ PRT: "222.632",
65
+ ROU: "255.718",
66
+ RUS: "355.431",
67
+ SAU: "568.967",
68
+ SEN: "540.098",
69
+ SRB: "549.083",
70
+ SGP: "488.21",
71
+ SVK: "173.854",
72
+ SVN: "241.956",
73
+ ZAF: "706.991",
74
+ KOR: "442.389",
75
+ ESP: "193.737",
76
+ SWE: "43.9",
77
+ CHE: "58.952",
78
+ TWN: "565.629",
79
+ TJK: "72.823",
80
+ THA: "503.034",
81
+ TUN: "470.848",
82
+ TUR: "432.293",
83
+ UKR: "240.28",
84
+ GBR: "268.255",
85
+ USA: "378.625",
86
+ VNM: "491.192",
87
+ WORLD: "442.37",
88
+ };
89
+ const type = "average";
90
+ const year = "2021";
91
+ export { data, type, year };
92
+ export default { data, type, year };