avm99963 | c3b457a | 2020-07-21 14:23:15 +0200 | [diff] [blame] | 1 | <?php |
| 2 | if (php_sapi_name() != "cli") |
| 3 | exit(); |
| 4 | |
| 5 | // Funció per obtenir el nombre de casos nous al dia originalDay+translation |
| 6 | // a partir de les dades de la regió (dataRegio). |
| 7 | function getSumDay($originalDay, $translation, &$dataRegio) { |
| 8 | if ($translation >= 0) |
| 9 | $day = (clone $originalDay)->add(new DateInterval("P".abs($translation)."D")); |
| 10 | else |
| 11 | $day = (clone $originalDay)->sub(new DateInterval("P".abs($translation)."D")); |
| 12 | |
| 13 | foreach ($dataRegio as $row) { |
| 14 | $rowDay = new DateTime($row["data"]); |
| 15 | if ($day == $rowDay) return $row["sum_numcasos"]; |
| 16 | } |
| 17 | |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | // Funció per fer una consulta a la taula de dades |
| 22 | function query($soql, $resource = "xuwf-dxjd") { |
| 23 | $url = "https://analisi.transparenciacatalunya.cat/resource/".$resource.".json?\$query=".urlencode($soql); |
| 24 | $raw = file_get_contents($url); |
| 25 | if ($raw === false) return null; |
| 26 | return json_decode($raw, true); |
| 27 | } |
| 28 | |
| 29 | // Funció per generar les dades de cada regió |
| 30 | function generateSummary(&$dataRegio, $habitants){ |
| 31 | $summary = []; |
| 32 | |
| 33 | // Veiem quin és el primer i l'últim dia de la sèrie |
| 34 | $oldestDay = new DateTime("today"); |
| 35 | $newestDay = new DateTime(); |
| 36 | $newestDay->setTimestamp(0); |
| 37 | |
| 38 | foreach ($dataRegio as $row) { |
| 39 | $date = new DateTime($row["data"]); |
| 40 | if ($date < $oldestDay) $oldestDay = $date; |
| 41 | if ($date > $newestDay) $newestDay = $date; |
| 42 | } |
| 43 | |
| 44 | // Si l'últim dia és avui, posem que sigui ahir, perquè no volem informació |
| 45 | // incompleta sobre avui. |
| 46 | if ($oldestDay == (new DateTime("today"))) |
| 47 | $oldestDay = new DateTime("yesterday"); |
| 48 | |
| 49 | // Ara calculem les rhos. |
| 50 | $rhos = []; |
| 51 | |
| 52 | // Considerem cada dia a partir de 6 dies després del primer dia, i fins al |
| 53 | // dia anterior a l'últim dia (extrems inclosos) |
| 54 | for ($currentDate = (clone $oldestDay)->add(new DateInterval("P6D")); |
| 55 | $currentDate < $newestDay; |
| 56 | $currentDate->add(new DateInterval("P1D"))) { |
| 57 | // Calculem la rho (velocitat reproductiva efectiva) per aquell dia. |
| 58 | // Fórmula: https://biocomsc.upc.edu/en/shared/avaluacio_risc.pdf |
| 59 | $num = getSumDay($currentDate, 1, $dataRegio) + |
| 60 | getSumDay($currentDate, 0, $dataRegio) + |
| 61 | getSumDay($currentDate, -1, $dataRegio); |
| 62 | |
| 63 | $den = getSumDay($currentDate, -4, $dataRegio) + |
| 64 | getSumDay($currentDate, -5, $dataRegio) + |
| 65 | getSumDay($currentDate, -6, $dataRegio); |
| 66 | |
| 67 | if ($num != 0 && $den == 0) continue; |
| 68 | |
| 69 | $rho = ($num == 0 ? 0 : $num/$den); |
| 70 | |
| 71 | $rhos[] = [ |
| 72 | "data" => $currentDate->format("c"), |
| 73 | "rho" => $rho |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | // Considerem cada dia a partir de 13 dies després del primer dia, i fins el |
| 78 | // dia anterior a l'últim dia (extrems inclosos) |
| 79 | for ($currentDate = (clone $oldestDay)->add(new DateInterval("P13D")); |
| 80 | $currentDate < $newestDay; |
| 81 | $currentDate->add(new DateInterval("P1D"))) { |
| 82 | // Calculem Rho_7 i IA_14 |
| 83 | // Rho_7(t) := \sum_{i=0}^{7} Rho(t - i) |
| 84 | // IA_14(t) := \sum_{i=0}^{14} N(t - i), |
| 85 | // on N(j) és el nombre de casos nous confirmats per PCR el dia j. |
| 86 | $sum = 0; |
| 87 | |
| 88 | $p13Date = (clone $currentDate)->sub(new DateInterval("P13D")); |
| 89 | $p6Date = (clone $currentDate)->sub(new DateInterval("P6D")); |
| 90 | |
| 91 | foreach ($dataRegio as $row) { |
| 92 | $date = new DateTime($row["data"]); |
| 93 | if ($date >= $p13Date && $date <= $currentDate) { |
| 94 | $sum += $row["sum_numcasos"]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $rhoAverage = 0; |
| 99 | $rhoCount = 0; |
| 100 | |
| 101 | foreach ($rhos as $row) { |
| 102 | $date = new DateTime($row["data"]); |
| 103 | if ($date >= $p6Date && $date <= $currentDate) { |
| 104 | ++$rhoCount; |
| 105 | $rhoAverage += $row["rho"]; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Si no hem trobat rhos (rhoCount == 0) és perquè el numerador no era 0 |
| 110 | // però el denominador era sempre 0 al calcular les rhos. Aleshores, tot i |
| 111 | // que no poguem calcular la rho_7 a causa de no poder calcular les rho_t |
| 112 | // individuals, aquest fet ens indica que el creixement ha sigut altíssim, |
| 113 | // i per tant posem una rho_7 de 1000000000, que se surt de la gràfica. |
| 114 | $rhoAverage = ($rhoCount == 0 ? 1000000000 : $rhoAverage/$rhoCount); |
| 115 | |
| 116 | $summary[] = [ |
| 117 | "data" => $currentDate->format("d/m/y"), |
| 118 | "ia14" => $sum*(1e5/$habitants), |
| 119 | "rho7" => $rhoAverage |
| 120 | ]; |
| 121 | } |
| 122 | |
| 123 | return $summary; |
| 124 | } |