blob: fab3093c01b435b93878985e2bb37f541e980eec [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,
13 "Barcelona" => 5050190
14];
15
16$CODENAME = [
17 "Alt Pirineu i Aran" => "AltPirineuAran",
18 "Lleida" => "Lleida",
19 "Camp de Tarragona" => "CampDeTarragona",
20 "Terres de l'Ebre" => "TerresDeLEbre",
21 "Girona" => "Girona",
22 "Catalunya Central" => "CatalunyaCentral",
23 "Barcelona" => "Barcelona"
24];
25
26function getSumDay($originalDay, $translation, &$dataRegio) {
27 if ($translation >= 0)
28 $day = (clone $originalDay)->add(new DateInterval("P".abs($translation)."D"));
29 else
30 $day = (clone $originalDay)->sub(new DateInterval("P".abs($translation)."D"));
31
32 foreach ($dataRegio as $row) {
33 $rowDay = new DateTime($row["data"]);
34 if ($day == $rowDay) return $row["sum_numcasos"];
35 }
36
37 return 0;
38}
39
40function query($soql) {
41 $raw = file_get_contents("https://analisi.transparenciacatalunya.cat/resource/xuwf-dxjd.json?\$query=".urlencode($soql));
42 return json_decode($raw, true);
43}
44
45$data = query("SELECT data, regiosanitariadescripcio AS regio, sum(numcasos) AS sum_numcasos
46WHERE
47 resultatcoviddescripcio = 'Positiu' AND
48 regiosanitariadescripcio <> 'No classificat'
49GROUP BY regiosanitariadescripcio, data
50ORDER BY data ASC, regiosanitariadescripcio");
51
52$dataPerRegio = [];
53foreach ($data as $row) {
54 if (!isset($dataPerRegio[$row["regio"]])) $dataPerRegio[$row["regio"]] = [];
55 $dataPerRegio[$row["regio"]][] = $row;
56}
57
58$summary = [];
59foreach ($dataPerRegio as $regio => $dataRegio) {
60 $summary[$regio] = [];
61
62 $oldestDay = new DateTime("today");
63 $newestDay = new DateTime();
64 $newestDay->setTimestamp(0);
65
66 foreach ($dataRegio as $row) {
67 $date = new DateTime($row["data"]);
68 if ($date < $oldestDay) $oldestDay = $date;
69 if ($date > $newestDay) $newestDay = $date;
70 }
71
72 $newestDay->sub(new DateInterval("P1D"));
73
74 $rhos = [];
75
76 for ($currentDate = (clone $oldestDay)->add(new DateInterval("P7D")); $currentDate < $newestDay; $currentDate->add(new DateInterval("P1D"))) {
77 $den = getSumDay($currentDate, -4, $dataRegio) + getSumDay($currentDate, -5, $dataRegio) + getSumDay($currentDate, -6, $dataRegio);
78 if ($den == 0) continue;
79
80 $rho = (getSumDay($currentDate, 1, $dataRegio) + getSumDay($currentDate, 0, $dataRegio) + getSumDay($currentDate, -1, $dataRegio))/($den);
81
82 $rhos[] = [
83 "data" => $currentDate->format("c"),
84 "rho" => $rho
85 ];
86 }
87
88 for ($currentDate = (clone $oldestDay)->add(new DateInterval("P14D")); $currentDate < $newestDay; $currentDate->add(new DateInterval("P1D"))) {
89 $sum = 0;
90
91 $p14Date = (clone $currentDate)->sub(new DateInterval("P14D"));
92 $p7Date = (clone $currentDate)->sub(new DateInterval("P7D"));
93
94 foreach ($dataRegio as $row) {
95 $date = new DateTime($row["data"]);
96 if ($date >= $p14Date && $date < $currentDate) {
97 $sum += $row["sum_numcasos"];
98 }
99 }
100
101 $rhoAverage = 0;
102 $rhoCount = 0;
103
104 foreach ($rhos as $row) {
105 $date = new DateTime($row["data"]);
106 if ($date >= $p7Date && $date < $currentDate) {
107 ++$rhoCount;
108 $rhoAverage += $row["rho"];
109 }
110 }
111
112 $rhoAverage /= $rhoCount;
113
114 $summary[$regio][] = [
115 "data" => $currentDate->format("c"),
116 "ia14" => (isset($HABITANTS[$regio]) ? $sum*(1e5/$HABITANTS[$regio]) : null),
117 "rho7" => $rhoAverage
118 ];
119 }
120}
121
122foreach ($summary as $regio => $summaryRegio) {
123 $file = fopen("/tmp/covid19graphgenerator-".$CODENAME[$regio].".dat", "w");
124
125 foreach ($summaryRegio as $row)
126 fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]."\n");
127
128 fclose($file);
129}