blob: 2489afaa2e02ea70dc72d4c0e0810413d38bd6ec [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) {
47 $raw = file_get_contents("https://analisi.transparenciacatalunya.cat/resource/xuwf-dxjd.json?\$query=".urlencode($soql));
48 return json_decode($raw, true);
49}
50
51$data = query("SELECT data, regiosanitariadescripcio AS regio, sum(numcasos) AS sum_numcasos
52WHERE
53 resultatcoviddescripcio = 'Positiu' AND
54 regiosanitariadescripcio <> 'No classificat'
55GROUP BY regiosanitariadescripcio, data
56ORDER BY data ASC, regiosanitariadescripcio");
57
58$dataPerRegio = [];
59foreach ($data as $row) {
60 if (!isset($dataPerRegio[$row["regio"]])) $dataPerRegio[$row["regio"]] = [];
61 $dataPerRegio[$row["regio"]][] = $row;
62}
63
64$summary = [];
65foreach ($dataPerRegio as $regio => $dataRegio) {
66 $summary[$regio] = [];
67
68 $oldestDay = new DateTime("today");
69 $newestDay = new DateTime();
70 $newestDay->setTimestamp(0);
71
72 foreach ($dataRegio as $row) {
73 $date = new DateTime($row["data"]);
74 if ($date < $oldestDay) $oldestDay = $date;
75 if ($date > $newestDay) $newestDay = $date;
76 }
77
avm9996300a40032020-05-07 21:02:32 +020078 $rhos = [];
79
80 for ($currentDate = (clone $oldestDay)->add(new DateInterval("P7D")); $currentDate < $newestDay; $currentDate->add(new DateInterval("P1D"))) {
81 $den = getSumDay($currentDate, -4, $dataRegio) + getSumDay($currentDate, -5, $dataRegio) + getSumDay($currentDate, -6, $dataRegio);
82 if ($den == 0) continue;
83
84 $rho = (getSumDay($currentDate, 1, $dataRegio) + getSumDay($currentDate, 0, $dataRegio) + getSumDay($currentDate, -1, $dataRegio))/($den);
85
86 $rhos[] = [
87 "data" => $currentDate->format("c"),
88 "rho" => $rho
89 ];
90 }
91
92 for ($currentDate = (clone $oldestDay)->add(new DateInterval("P14D")); $currentDate < $newestDay; $currentDate->add(new DateInterval("P1D"))) {
93 $sum = 0;
94
95 $p14Date = (clone $currentDate)->sub(new DateInterval("P14D"));
96 $p7Date = (clone $currentDate)->sub(new DateInterval("P7D"));
97
98 foreach ($dataRegio as $row) {
99 $date = new DateTime($row["data"]);
100 if ($date >= $p14Date && $date < $currentDate) {
101 $sum += $row["sum_numcasos"];
102 }
103 }
104
105 $rhoAverage = 0;
106 $rhoCount = 0;
107
108 foreach ($rhos as $row) {
109 $date = new DateTime($row["data"]);
110 if ($date >= $p7Date && $date < $currentDate) {
111 ++$rhoCount;
112 $rhoAverage += $row["rho"];
113 }
114 }
115
116 $rhoAverage /= $rhoCount;
117
118 $summary[$regio][] = [
avm999630b3b4072020-05-17 02:39:07 +0200119 "data" => $currentDate->format("d/m/y"),
avm9996300a40032020-05-07 21:02:32 +0200120 "ia14" => (isset($HABITANTS[$regio]) ? $sum*(1e5/$HABITANTS[$regio]) : null),
121 "rho7" => $rhoAverage
122 ];
123 }
124}
125
126foreach ($summary as $regio => $summaryRegio) {
127 $file = fopen("/tmp/covid19graphgenerator-".$CODENAME[$regio].".dat", "w");
128
129 foreach ($summaryRegio as $row)
130 fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]."\n");
131
132 fclose($file);
133}