blob: 1c3ae427d5dc4202034bc2bc4ed0e821c879c6f1 [file] [log] [blame]
avm9996300a40032020-05-07 21:02:32 +02001<?php
2if (php_sapi_name() != "cli")
3 exit();
4
5// Font dels nombres d'habitants: https://catsalut.gencat.cat/web/.content/minisite/catsalut/proveidors_professionals/registres_catalegs/documents/poblacio-referencia.pdf
6$HABITANTS = [
7 "Alt Pirineu i Aran" => 67277,
8 "Lleida" => 362850,
9 "Camp de Tarragona" => 607999,
10 "Terres de l'Ebre" => 176817,
11 "Girona" => 861753,
12 "Catalunya Central" => 526959,
avm999630b3b4072020-05-17 02:39:07 +020013 "Barcelona" => 5050190,
14 "Barcelona Ciutat" => 1693449,
15 "Metropolità Sud" => 1370709,
16 "Metropolità Nord" => 1986032,
avm9996300a40032020-05-07 21:02:32 +020017];
18
19$CODENAME = [
20 "Alt Pirineu i Aran" => "AltPirineuAran",
21 "Lleida" => "Lleida",
22 "Camp de Tarragona" => "CampDeTarragona",
23 "Terres de l'Ebre" => "TerresDeLEbre",
24 "Girona" => "Girona",
25 "Catalunya Central" => "CatalunyaCentral",
avm999630b3b4072020-05-17 02:39:07 +020026 "Barcelona" => "Barcelona",
27 "Barcelona Ciutat" => "BarcelonaCiutat",
28 "Metropolità Sud" => "MetropolitaSud",
29 "Metropolità Nord" => "MetropolitaNord",
avm9996300a40032020-05-07 21:02:32 +020030];
31
32function getSumDay($originalDay, $translation, &$dataRegio) {
33 if ($translation >= 0)
34 $day = (clone $originalDay)->add(new DateInterval("P".abs($translation)."D"));
35 else
36 $day = (clone $originalDay)->sub(new DateInterval("P".abs($translation)."D"));
37
38 foreach ($dataRegio as $row) {
39 $rowDay = new DateTime($row["data"]);
40 if ($day == $rowDay) return $row["sum_numcasos"];
41 }
42
43 return 0;
44}
45
46function query($soql) {
avm999634b07f162020-06-04 13:35:57 +020047 $url = "https://analisi.transparenciacatalunya.cat/resource/xuwf-dxjd.json?\$query=".urlencode($soql);
48 $raw = file_get_contents($url);
avm9996300a40032020-05-07 21:02:32 +020049 return json_decode($raw, true);
50}
51
52$data = query("SELECT data, regiosanitariadescripcio AS regio, sum(numcasos) AS sum_numcasos
53WHERE
avm999634b07f162020-06-04 13:35:57 +020054 resultatcoviddescripcio = 'Positiu PCR' AND
avm9996300a40032020-05-07 21:02:32 +020055 regiosanitariadescripcio <> 'No classificat'
56GROUP BY regiosanitariadescripcio, data
57ORDER BY data ASC, regiosanitariadescripcio");
58
59$dataPerRegio = [];
60foreach ($data as $row) {
61 if (!isset($dataPerRegio[$row["regio"]])) $dataPerRegio[$row["regio"]] = [];
62 $dataPerRegio[$row["regio"]][] = $row;
63}
64
65$summary = [];
66foreach ($dataPerRegio as $regio => $dataRegio) {
67 $summary[$regio] = [];
68
69 $oldestDay = new DateTime("today");
70 $newestDay = new DateTime();
71 $newestDay->setTimestamp(0);
72
73 foreach ($dataRegio as $row) {
74 $date = new DateTime($row["data"]);
75 if ($date < $oldestDay) $oldestDay = $date;
76 if ($date > $newestDay) $newestDay = $date;
77 }
78
avm9996300a40032020-05-07 21:02:32 +020079 $rhos = [];
80
81 for ($currentDate = (clone $oldestDay)->add(new DateInterval("P7D")); $currentDate < $newestDay; $currentDate->add(new DateInterval("P1D"))) {
82 $den = getSumDay($currentDate, -4, $dataRegio) + getSumDay($currentDate, -5, $dataRegio) + getSumDay($currentDate, -6, $dataRegio);
83 if ($den == 0) continue;
84
85 $rho = (getSumDay($currentDate, 1, $dataRegio) + getSumDay($currentDate, 0, $dataRegio) + getSumDay($currentDate, -1, $dataRegio))/($den);
86
87 $rhos[] = [
88 "data" => $currentDate->format("c"),
89 "rho" => $rho
90 ];
91 }
92
93 for ($currentDate = (clone $oldestDay)->add(new DateInterval("P14D")); $currentDate < $newestDay; $currentDate->add(new DateInterval("P1D"))) {
94 $sum = 0;
95
96 $p14Date = (clone $currentDate)->sub(new DateInterval("P14D"));
97 $p7Date = (clone $currentDate)->sub(new DateInterval("P7D"));
98
99 foreach ($dataRegio as $row) {
100 $date = new DateTime($row["data"]);
101 if ($date >= $p14Date && $date < $currentDate) {
102 $sum += $row["sum_numcasos"];
103 }
104 }
105
106 $rhoAverage = 0;
107 $rhoCount = 0;
108
109 foreach ($rhos as $row) {
110 $date = new DateTime($row["data"]);
111 if ($date >= $p7Date && $date < $currentDate) {
112 ++$rhoCount;
113 $rhoAverage += $row["rho"];
114 }
115 }
116
117 $rhoAverage /= $rhoCount;
118
119 $summary[$regio][] = [
avm999630b3b4072020-05-17 02:39:07 +0200120 "data" => $currentDate->format("d/m/y"),
avm9996300a40032020-05-07 21:02:32 +0200121 "ia14" => (isset($HABITANTS[$regio]) ? $sum*(1e5/$HABITANTS[$regio]) : null),
122 "rho7" => $rhoAverage
123 ];
124 }
125}
126
127foreach ($summary as $regio => $summaryRegio) {
128 $file = fopen("/tmp/covid19graphgenerator-".$CODENAME[$regio].".dat", "w");
129
130 foreach ($summaryRegio as $row)
131 fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]."\n");
132
133 fclose($file);
134}