-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-json.php
More file actions
180 lines (163 loc) · 5.08 KB
/
generate-json.php
File metadata and controls
180 lines (163 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
require('covid.php');
$day = '2020-'.$argv[1];
$word = date('F jS',strtotime($day));
$out = '[';
FillInCountriesWorld($day);
$pop = array();
$continents = array();
$continents['USA'] = 'Northern America';
$pop['USA Country'] = '330,000,000';
$pop['World'] = '7,800,000,000';
$cpop = file_get_contents('country-pop.txt');
foreach (preg_split("/\n/",$cpop) as $line) {
$parts = preg_split("/\t/",$line);
$count = preg_replace('/,/','',$parts[4]);
$pop[trim($parts[0])." Country"] = number_format(RoundSigDigs($count,2));
$continents[trim($parts[0])] = $parts[2];
}
$citypop = file_get_contents('city-pop-dates.txt');
$today = new DateTime($day);
$schooldays = array();
$lockdays = array();
foreach (preg_split("/\n/",$citypop) as $line) {
$parts = preg_split("/\t/",$line);
$city = preg_replace("/ MSA/",'',$parts[0]);
$count = preg_replace('/,/','',$parts[1]);
if ($parts[2]) {
$closed = new DateTime($parts[2]);
$schools = date_diff($closed,$today);
$schooldays["City of ".$city] = $schools->format('%a');
}
if ($parts[4]) {
$locked = new DateTime($parts[4]);
$locks = date_diff($locked,$today);
$lockdays["City of ".$city] = $locks->format('%a');
}
$pop["City of ".$city] = number_format(RoundSigDigs($count,2));
}
$spop = file_get_contents('state-pop-dates.txt');
foreach (preg_split("/\n/",$spop) as $line) {
$parts = preg_split("/\t/",$line);
$count = preg_replace('/,/','',$parts[1]);
$state = trim($parts[0]);
if ($parts[2]) {
$closed = new DateTime($parts[2]);
$schools = date_diff($closed,$today);
$schooldays[$state. " State"] = $schools->format('%a');
}
if ($parts[4]) {
$locked = new DateTime($parts[4]);
$locks = date_diff($locked,$today);
$lockdays[$state. " State"] = $locks->format('%a');
}
$pop[$state." State"] = number_format(RoundSigDigs($count,2));
}
$firstRun = true;
foreach (AllCovidsByDay($day) as $C) {
$st = 'World';
if ($C->region_type == 'City') {
$st = 'City of '.$C->region;
} else if ($C->region_type == 'Counties') {
continue;
} else if ($C->region_type != 'World') {
$st = $C->region. ' '.$C->region_type;
}
$current_ratio = $C->ratio;
$last_total = $C->cases;
$new_cases = number_format($C->new_cases);
$new_cases_day = number_format($C->new_cases_day);
$cases_10 = number_format($C->cases_10);
$peak_cases = number_format(RoundSigDigs($C->peak,2));
$last_total = number_format($last_total);
$days_left = DaysToPeak($current_ratio);
$popu = $C->pop ? strval($C->pop) : $pop[$st];
$popuint = preg_replace("/,/","",$popu);
$C->SavePop(intval($popuint));
$peak_density = '';
$rcolor='';
$dcolor='';
// if ($popu != "") {
// continue;
// }
if ($C->cases > 20) {
$rcolor='lightgoldenrodyellow';
if ($current_ratio >= 33) {
$rcolor='palevioletred';
} else if ($current_ratio <= 10) {
$rcolor='lightgreen';
}
}
$sch = '';
$lock = '';
$est_bed_shortage = 0;
if ($popuint) {
$sch = $schooldays[$st];
$lock = $lockdays[$st];
$est_beds = preg_replace("/,/","",$popu)/10000;
$est_peak_beds = preg_replace("/,/","",$peak_cases)/10;
$est_bed_shortage = number_format($est_beds-$est_peak_beds);
$shortage_ratio = $est_peak_beds/$est_beds; # >1 means shortage.
$peak_density = RoundSigDigs(preg_replace("/,/","",$popu)/preg_replace("/,/","",$peak_cases),2);
if ($C->cases > 20) {
$dcolor='lightgoldenrodyellow';
if ($shortage_ratio <= 0.8) {
$dcolor='lightgreen';
} else if ($shortage_ratio >= 1.5) {
$dcolor='palevioletred';
}
}
$peak_density = number_format($peak_density);
}
$rtype = '';
if ($C->region_type == 'Country') {
$rtype = 'Country in '.$continents[$C->region];
} else if ($C->region_type == 'World') {
$rtype = 'All Countries';
} else {
$rtype = $C->region_type." in ".$C->country;
}
if (!$firstRun) {
$out .= ",";
}
/////// calculate last 10 days
// select 2 weeks of data cases
$hist_day_cases_ar = $C->GetDailyNewCases();
$firstLoop = true;
$hist_day_cases_st = '';
foreach ($hist_day_cases_ar as $hist_day_cases) {
if (!$firstLoop) {
$hist_day_cases_st .= ",";
}
$hist_day_cases_st .= "$hist_day_cases";
$firstLoop = false;
}
$out .= trim("{
\"region\":\"$C->region\",
\"type\":\"$rtype\",
\"pop\":\"".preg_replace("/,/","",$popu)."\",
\"cases\":\"".preg_replace("/,/","",$last_total)."\",
\"newCases\":\"".preg_replace("/,/","",$new_cases)."\",
\"newCasesAr\": [".$hist_day_cases_st."],
\"newCasesDay\":\"".preg_replace("/,/","",$new_cases_day)."\",
\"casesTenDays\":\"".preg_replace("/,/","",$cases_10)."\",
\"ratio\":\"".intval($current_ratio)."\",
\"schoolClosed\":\"$sch\",
\"physDist\":\"$lock\",
\"peak\":\"".preg_replace("/,/","",$peak_cases)."\",
\"estBedShort\":\"".preg_replace("/,/","",$est_bed_shortage)."\",
\"estDaysPeak\":\"".preg_replace("/,/","",$days_left)."\"
}");
$firstRun = false;
}
$out .= "]";
// $out = preg_replace("/\n/", '', $out);
if ($day) {
$filename = "./app/static/days/$day.json";
$fp = fopen($filename, 'w');
fwrite($fp, $out);
fclose($fp);
echo "Successfully created $filename \n";
} else {
echo "Error: please provide a --day argument in the form of '04-14' for example \n";
}