blob: 06b5caadf732b007ba4a065435ae54043735a4ca [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 = [
avm9996389b0a422021-01-05 12:02:57 +01007 "AltPirineuAran" => 67277,
avm9996300a40032020-05-07 21:02:32 +02008 "Lleida" => 362850,
avm9996389b0a422021-01-05 12:02:57 +01009 "CampDeTarragona" => 607999,
10 "TerresDeLEbre" => 176817,
avm9996300a40032020-05-07 21:02:32 +020011 "Girona" => 861753,
avm9996389b0a422021-01-05 12:02:57 +010012 "CatalunyaCentral" => 526959,
avm999630b3b4072020-05-17 02:39:07 +020013 "Barcelona" => 5050190,
avm9996389b0a422021-01-05 12:02:57 +010014 "BarcelonaCiutat" => 1693449,
15 "MetropolitaSud" => 1370709,
16 "MetropolitaNord" => 1986032,
avm9996300a40032020-05-07 21:02:32 +020017];
18
19$CODENAME = [
avm9996389b0a422021-01-05 12:02:57 +010020 "7100" => "AltPirineuAran",
21 "6100" => "Lleida",
22 "6200" => "CampDeTarragona",
23 "6300" => "TerresDeLEbre",
24 "6400" => "Girona",
25 "6700" => "CatalunyaCentral",
26 "7803" => "BarcelonaCiutat",
27 "7801" => "MetropolitaSud",
28 "7802" => "MetropolitaNord",
avm9996300a40032020-05-07 21:02:32 +020029];
30
avm99963c3b457a2020-07-21 14:23:15 +020031require_once(__DIR__."/includes/generation.php");
avm9996300a40032020-05-07 21:02:32 +020032
avm99963227cbb02020-06-09 13:51:25 +020033// Demanem una llista del nombre de casos cada dia a cada regió sanitària
avm9996389b0a422021-01-05 12:02:57 +010034$data = query("SELECT data, regiosanitariacodi AS regio, sum(numcasos) AS sum_numcasos
avm9996300a40032020-05-07 21:02:32 +020035WHERE
avm9996389b0a422021-01-05 12:02:57 +010036 (
37 resultatcoviddescripcio = 'Positiu PCR' OR
38 resultatcoviddescripcio = 'Positiu TAR'
39 ) AND
40 regiosanitariacodi <> '0000'
41GROUP BY regiosanitariacodi, data
42ORDER BY data ASC, regiosanitariacodi
avm999637e562d42020-07-15 23:27:38 +020043LIMIT 50000");
avm9996300a40032020-05-07 21:02:32 +020044
avm99963227cbb02020-06-09 13:51:25 +020045// Fem un array que tindrà com a elements un array per cada regió amb el
46// contingut de totes les files d'aquella regió
avm9996300a40032020-05-07 21:02:32 +020047$dataPerRegio = [];
48foreach ($data as $row) {
49 if (!isset($dataPerRegio[$row["regio"]])) $dataPerRegio[$row["regio"]] = [];
50 $dataPerRegio[$row["regio"]][] = $row;
51}
52
53$summary = [];
avm9996389b0a422021-01-05 12:02:57 +010054foreach ($dataPerRegio as $regioRaw => $dataRegio) { // Per a cada regió
55 $regio = mb_strtolower($regioRaw);
56
avm99963227cbb02020-06-09 13:51:25 +020057 if (!in_array($regio, array_keys($CODENAME)))
58 die("[fatal error] No tenim contemplada la regió '".$regio."'.\n");
59
avm99963c3b457a2020-07-21 14:23:15 +020060 // Generem les dades
avm9996389b0a422021-01-05 12:02:57 +010061 $summary[$regio] = generateSummary($dataRegio, $HABITANTS[$CODENAME[$regio]]);
avm9996300a40032020-05-07 21:02:32 +020062}
63
avm99963227cbb02020-06-09 13:51:25 +020064// Posem les dades a diversos fitxers per tal que les pugui llegir el gnuplot
avm9996300a40032020-05-07 21:02:32 +020065foreach ($summary as $regio => $summaryRegio) {
66 $file = fopen("/tmp/covid19graphgenerator-".$CODENAME[$regio].".dat", "w");
67
avm9996373e13142020-08-31 01:59:30 +020068 $i = 0;
69 foreach ($summaryRegio as $row) {
70 fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]." ".$i."\n");
71 ++$i;
72 }
avm9996300a40032020-05-07 21:02:32 +020073
74 fclose($file);
75}