-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (258 loc) · 12.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CO2 Emission Calculator</title>
<style>
body {
margin: 0px;
}
</style>
</head>
<body>
<co2calc>
<style>
co2calc .bckground {
height: 100%;
width: 100%;
top: 0px;
position: fixed;
background-image: url('SMLBck.jpg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
margin: 0;
padding: 0;
z-index: -5;
}
co2calc .Calculator {
font-family: Arial, sans-serif;
width: 100%;
background-color: #f5f5f5;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
margin: 20px auto;
max-width: 1000px;
}
co2calc .Calculator h2 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
co2calc .Calculator label {
font-size: 14px;
color: #666;
display: block;
margin-bottom: 8px;
}
co2calc .Calculator select,
co2calc .Calculator input {
width: 100%;
padding: 8px 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
color: #333;
}
/* Hide the default checkbox */
co2calc .Calculator input[type="checkbox"] {
display: none;
}
/* Style the label for the checkbox to create a custom checkbox design */
co2calc .Calculator label[for="roundtrip"] {
padding-bottom: 20px;
}
co2calc .Calculator label[for="roundtrip"]:before {
content: "";
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #666;
border-radius: 4px;
margin-right: 10px;
vertical-align: middle;
transition: background-color 0.2s;
text-align: center;
line-height: 20px;
/* Same as the height of the box */
}
/* Style the checkbox when checked */
co2calc .Calculator input[type="checkbox"]:checked+label[for="roundtrip"]:before {
background-color: #007BFF;
border-color: #007BFF;
content: "\2713";
/* Unicode checkmark */
color: white;
font-size: 18px;
vertical-align: middle;
}
co2calc .Calculator button {
width: 100%;
padding: 10px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
co2calc .Calculator button:hover {
background-color: #0056b3;
}
co2calc #result {
font-size: 16px;
color: #333;
margin-top: 20px;
text-align: center;
}
co2calc .notice {
font-size: 12px;
color: #666;
margin-top: 10px;
text-align: center;
}
</style>
<div class="bckground"></div>
<div class="Calculator">
<h2>MV Norröna Co2 Emission Calculator</h2>
<label for="month">Select Month:</label>
<select id="month" class="emissionInput">
</select>
<label for="route">Select Route:</label>
<select id="route" class="emissionInput">
</select>
<input type="checkbox" id="roundtrip" value="roundtrip" class="emissionInput">
<label for="roundtrip">Round Trip:</label>
<label for="vehicle">Select Vehicle:</label>
<select id="vehicle" class="emissionInput">
<option value="none">No Vehicle</option>
</select>
<label for="passengers">Number of Passengers:</label>
<input type="number" id="passengers" value="1" min="1" class="emissionInput" style="width: 93%;">
<div id="result"></div>
<div id="extra_info" class="notice"></div>
<div class="notice">More information can be found here: <a href="https://smyrilline.github.io/Co2Calc/README.html" target="_blank">ReadMe</a>
</div>
</div>
<script>
let co2Data = {};
function generateSelectOptions(data, selectElementId) {
const selectElement = document.getElementById(selectElementId);
for (const key in data) {
const option = document.createElement('option');
option.value = key;
option.text = key.replace(/_/g, ' ').replace(/-/g, ' to ');
selectElement.appendChild(option);
}
}
function generateVehicleSelectOptions(data, selectElementId) {
const selectElement = document.getElementById(selectElementId);
for (const key in data) {
const vehicle = data[key]; // This is now an object with name, low, high
const option = document.createElement('option');
option.value = key; // You may want to keep this as the key (BIKE, BUS, etc.)
option.text = vehicle.name; // Use the name property for the option text
selectElement.appendChild(option);
}
}
function calculateTotalVehicleWeight(vehicleTypes, monthlyData) {
let totalWeight = 0;
for (const vehicleType in monthlyData.vehicles) {
if (vehicleType !== 'Total') {
const count = monthlyData.vehicles[vehicleType];
const avgWeight = (vehicleTypes[vehicleType].low + vehicleTypes[vehicleType].high) / 2;
totalWeight += count * avgWeight;
}
}
return totalWeight;
}
function calculatePercentages(totalVehicleWeight, totalPaxWeight) {
const totalWeight = totalVehicleWeight + totalPaxWeight;
const vehicleWeightPercent = (totalVehicleWeight / totalWeight);
const paxWeightPercent = (totalPaxWeight / totalWeight);
return {
vehicleWeightPercent: vehicleWeightPercent.toFixed(2), // rounding to 2 decimal places
paxWeightPercent: paxWeightPercent.toFixed(2) // rounding to 2 decimal places
};
}
function getVehicleWeight(data, vehicle) {
return (data.vehicleTypes[vehicle].low + data.vehicleTypes[vehicle].high) / 2;
}
function gToKg(g) {
return (g / 1000).toFixed(2);
}
function calculateEmission() {
const month = document.getElementById("month").value;
const route = document.getElementById("route").value;
const passengers = parseInt(document.getElementById("passengers").value);
const vehicle = document.getElementById("vehicle").value;
const roundtrip = document.getElementById("roundtrip").checked ? 2 : 1;
const passengersPerMonth = Object.values(co2Data.months).map(month => month.passengers);
const totalVehicleWeightMonthly = calculateTotalVehicleWeight(co2Data.vehicleTypes, co2Data.months[month]);
const totalPaxWeightMonthly = co2Data.months[month].passengers * co2Data.paxAvgWeight;
const percentages = calculatePercentages(totalVehicleWeightMonthly, totalPaxWeightMonthly);
const paxCo2 = co2Data.co2PerNauticalMilePerPax * percentages.paxWeightPercent;
const vehicleCo2 = co2Data.co2PerNauticalMilePerPax * percentages.vehicleWeightPercent;
const MonthData = co2Data.months[month];
const routeDistance = co2Data.routes[route];
const vehicleWeight = (vehicle == 'none' ? 0 : getVehicleWeight(co2Data, vehicle));
const vehicleEmission = ((vehicleWeight / 1000) * (vehicleCo2 * routeDistance));
const baseCo2PerPax = (paxCo2 * routeDistance); // ?? 1000
const scalarFactor = (co2Data.co2PassengerScalarFactor);
const minPassengers = Math.min(...passengersPerMonth);
const maxPassengers = Math.max(...passengersPerMonth);
const inverseNormalizedPassengers = passengersPerMonth.map(passengers =>
(1 + scalarFactor) - ((passengers - minPassengers) / (maxPassengers - minPassengers)) * ((1 + scalarFactor) - (1 - scalarFactor))
);
const adjustedCo2PerPax = inverseNormalizedPassengers.map(scalar =>
(baseCo2PerPax * scalar).toFixed(2) // rounding to 2 decimal places for clarity
);
const adjustedCo2PerMonth = {};
Object.keys(co2Data.months).forEach((month, index) => {
adjustedCo2PerMonth[month] = parseFloat(adjustedCo2PerPax[index]);
});
const paxEmission = (adjustedCo2PerMonth[month] * passengers);
// Calculate total emission in grams
const totalEmission = (paxEmission + vehicleEmission) * roundtrip;
// Display the result
const totalGrams = totalEmission.toFixed(2);
const totalKg = (totalEmission / 1000).toFixed(2);
document.getElementById("result").innerHTML = "Total CO2 Emission for " + passengers + " passengers: " + totalKg + " kg CO2";
document.getElementById("extra_info").innerHTML = "With an estimated " + MonthData.passengers + " passengers at " + month + ", with an average of " + gToKg(paxEmission) + " kg CO2 per pax";
if (vehicle != 'none') {
document.getElementById("extra_info").innerHTML += " with the additional " + vehicleWeight.toFixed(2) + " kg of the " + vehicle.replace(/_/g, ' ') + " taking on " + gToKg(vehicleEmission) + " kg CO2 of the emission.";
}
if (roundtrip == 2) {
document.getElementById("extra_info").innerHTML += " and the roundtrip distance of " + (routeDistance * 2).toFixed(2) + " nautical miles.";
} else {
document.getElementById("extra_info").innerHTML += " and the distance of " + routeDistance.toFixed(2) + " nautical miles.";
}
}
// Fetch the data from the JSON file
fetch('data.json')
.then(response => response.json())
.then(data => {
co2Data = data;
// Generate the select options
generateSelectOptions(co2Data.months, "month");
generateSelectOptions(co2Data.routes, "route");
generateVehicleSelectOptions(co2Data.vehicleTypes, "vehicle");
// Initial calculation
calculateEmission();
// Add a change event listener to each of them
let inputs = document.querySelectorAll('.emissionInput');
inputs.forEach(input => {
input.addEventListener('change', calculateEmission);
});
})
.catch(error => {
console.error("Error fetching the JSON data:", error);
});
</script>
</co2calc>
</body>
</html>