-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnrgkick_sum_csvs.php
62 lines (51 loc) · 2.08 KB
/
nrgkick_sum_csvs.php
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
<?php
# with the help of ChatGPT...
$dir = "."; // Directory containing the CSV files
$monthlyTotals = [];
$totalEnergyWh = 0;
// Process each CSV file
foreach (glob("$dir/*.csv") as $file) {
// Open the file
$handle = fopen($file, "r");
if ($handle !== false) {
// Read the header row to skip it
$header = fgetcsv($handle, 0, ";");
$header = fgetcsv($handle, 0, ";");
$header = fgetcsv($handle, 0, ";");
$header = fgetcsv($handle, 0, ";");
// Process each data row
while (($data = fgetcsv($handle, 0, ";")) !== false) {
// Skip rows that do not have the required number of columns
if (count($data) < 3 || empty($data[0]) || empty($data[2])) {
continue;
}
// Extract the month from the Ladestart column (first column, DD.MM.YYYY format)
$startDate = $data[0];
$month = substr($startDate, 3, 2); // Extract MM from DD.MM.YYYY
$year = substr($startDate, 6, 4); // Extract YYYY
$monthKey = $year . $month; // Create YYYYMM key
// Extract the Energie[Wh] value (third column) and add to the total
$energyWh = (float)str_replace(",", ".", $data[2]); // Convert to a float
$totalEnergyWh += $energyWh; // Add to the grand total
if (!isset($monthlyTotals[$monthKey])) {
$monthlyTotals[$monthKey] = 0;
}
$monthlyTotals[$monthKey] += $energyWh;
}
fclose($handle);
}
}
// Calculate the total and average
$totalEnergyKWh = $totalEnergyWh / 1000; // Convert total to kWh
$numberOfMonths = count($monthlyTotals);
$averageEnergyKWh = $numberOfMonths > 0 ? $totalEnergyKWh / $numberOfMonths : 0;
// Print the monthly totals in kWh
echo "Monthly Totals (kWh):\n";
foreach ($monthlyTotals as $month => $totalWh) {
$kwh = $totalWh / 1000; // Convert Wh to kWh
echo "$month: $kwh kWh\n";
}
// Print total and average
echo "\nOverall Totals:\n";
echo "Total Energy: $totalEnergyKWh kWh\n";
echo "Monthly Average: $averageEnergyKWh kWh\n";