-
Notifications
You must be signed in to change notification settings - Fork 0
/
cov_load_timeseries_csv.php
executable file
·289 lines (224 loc) · 10.2 KB
/
cov_load_timeseries_csv.php
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/php
<?php
/*
COVtrack
Copyright (C) 2020 by Arjen Lentz <[email protected]>
Licensed under GPLv3
Tracking data from https://github.com/CSSEGISandData/COVID-19
*/
define ('TIMESERIES_CONFIRMED' , 'time_series_covid19_confirmed_global.csv');
define ('TIMESERIES_DEATHS' , 'time_series_covid19_deaths_global.csv');
define ('TIMESERIES_RECOVERED' , 'time_series_covid19_recovered_global.csv');
define ('DB_NAME', 'covtrack');
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
debug_print_backtrace();
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
function read_timeseries_csv($fname)
{
$data = array();
$fp = @fopen($fname, 'r');
if (!$fp)
die("Can't open '$fname'\n");
$header = fgetcsv($fp);
foreach ($header as $key => $value) {
if (is_numeric($value[0])) {
// convert mm/dd/yy to standard ISO yyyy-mm-dd
$d = strptime($value, '%m/%d/%y');
$header[$key] = sprintf('%04u-%02u-%02u', $d['tm_year']+1900, $d['tm_mon']+1, $d['tm_mday']);
}
}
//print_r($header);
$data['header'] = $header;
while (!feof($fp)) {
$arr = fgetcsv($fp);
if (is_null($arr) || !is_array($arr) || count($arr) < 5)
continue;
// Dirty data: contains a stateprov "Recovered" for confirmed/deaths files for Canada
if ($arr[0] == 'Recovered')
continue;
// We have data from County (US only), State/Province (Australia,Canada,China,US), and Country.
// If we tally all that in the same table, we'll be double-counting people!
// To fix this, we will:
// - Not import the County-level data at all.
if ($arr[1] == 'US' && ($arr[0] == 'US' || strchr($arr[0],',')))
continue;
// - Import State/Province-level data in a separate table.
// We handle (later) that during processing.
// Some countries also have external territories, but they're not like a province or county so we keep them separate.
// In our input data for the main country of those situations, the stateprov is same as country, so we clear that.
// That way all countries will look the same
if ($arr[0] == $arr[1])
$arr[0] = '';
$data[trim($arr[0] . ' ' . $arr[1])] = $arr;
}
fclose($fp);
return ($data);
}
// -------------------------------------
// get cmdline options
$shortopts = 'f:h:u:p:'; // filepath, dbhost, user, pwd
$options = getopt($shortopts);
$filepath = isset($options['f']) ? $options['f'] : './'; // default filepath to current dir
$db_host = isset($options['h']) ? $options['h'] : 'localhost'; // default dbhost to localhost
if (!isset($options['u']))
die("Missing -u (db user) option\n");
$db_user = $options['u'];
$db_pass = isset($options['p']) ? $options['p'] : NULL; // default user to no pwd
// -------------------------------------
// Read latest timeseries CSVs.
$confirmed = read_timeseries_csv($filepath.TIMESERIES_CONFIRMED);
$deaths = read_timeseries_csv($filepath.TIMESERIES_DEATHS);
$recovered = read_timeseries_csv($filepath.TIMESERIES_RECOVERED);
// Grab header row, then remove it from dataset arrays
$header = $confirmed['header'];
unset($confirmed['header']);
unset($deaths['header']);
unset($recovered['header']);
function aggregate_stateprov_to_country ($country, &$dataset)
{
$dataset[$country][0] = ''; // stateprov
$dataset[$country][1] = $country;
$dataset[$country][2] = 0; // lat (yep so we lose that info on aggregation)
$dataset[$country][3] = 0; // lon
foreach ($dataset as $key => $row) {
if ($row[1] != $country || empty($row[0]))
continue;
for ($i = 4; $i < count($row); $i++) {
if (!isset($dataset[$country][$i]))
$dataset[$country][$i] = $row[$i];
else
$dataset[$country][$i] += $row[$i];
}
unset($dataset[$key]);
}
}
aggregate_stateprov_to_country('Canada',$confirmed);
aggregate_stateprov_to_country('Canada',$deaths);
// Because we're processing all these three datasets in parallel,
// we have to ensure they're same # cols, as it should be!
// Not an ideal input format really, but if we check it should be ok.
// They don't have the same country on the same row, so we do that differently already.
/* No need to check this, we just run on any country that has confirmed cases
$rows = count($confirmed);
if ($rows != count($deaths) || $rows != count($recovered))
die("The timeseries CSV files have different number of rows\n");
*/
$cols = count($header);
foreach ($confirmed as $key => $row) {
/*
Don't need to catch these, we'll deal with them on the fly
if (!in_array($key, $deaths))
print("Location '$key' from confirmed not in deaths\n");
if (!in_array($key, $recovered))
print("Location '$key' from confirmed not in recovered\n");
*/
if ($cols != count($confirmed[$key]) || $cols != count($deaths[$key]) || $cols != count($recovered[$key])) {
die("Different number of columns on timeseries row '$key'\n");
}
}
// -------------------------------------
// Open db
$db = new mysqli($db_host, $db_user, $db_pass, DB_NAME);
if ($db->connect_error) {
die('Connect Error (' . $db->connect_errno . ') ' . $db->connect_error);
}
$create_locations_table_query = '
CREATE TABLE IF NOT EXISTS locations (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
stateprov VARCHAR(50) NOT NULL,
country VARCHAR(50) NOT NULL,
lat DECIMAL(7,4),
lon DECIMAL(7,4),
UNIQUE KEY (stateprov,country),
INDEX (country)
) ENGINE=InnoDB
';
$db->query($create_locations_table_query);
$db->begin_transaction();
$put_location_query = 'INSERT IGNORE INTO locations (stateprov,country,lat,lon) VALUES (?,?,?,?)';
$put_location_stmt = $db->prepare($put_location_query);
// step through countries
foreach ($confirmed as $key => $row) {
$location_stateprov = $confirmed[$key][0];
$location_country = $confirmed[$key][1];
$location_lat = $confirmed[$key][2];
$location_lon = $confirmed[$key][3];
$put_location_stmt->bind_param('ssss', $location_stateprov, $location_country, $location_lat, $location_lon);
$put_location_stmt->execute();
}
$put_location_stmt->close();
$db->commit();
// creating a lookup array for stateprov/country -> id
$get_location_query = 'SELECT id,stateprov,country FROM locations';
$get_location_stmt = $db->prepare($get_location_query);
$get_location_stmt->execute();
$get_location_stmt->bind_result($location_id, $location_stateprov, $location_country);
$location_lookup = array();
while ($get_location_stmt->fetch()) {
$key = trim($location_stateprov . ' ' . $location_country);
$location_lookup[$key] = $location_id;
}
$get_location_stmt->close();
ksort($location_lookup); // sort array by key, predictable processing order
$create_items_table_query = '
CREATE TABLE items (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
location_id INT UNSIGNED NOT NULL,
recdate DATE,
confirmed_total INT UNSIGNED NOT NULL,
deaths_total INT UNSIGNED NOT NULL,
recovered_total INT UNSIGNED NOT NULL,
confirmed_new INT UNSIGNED NOT NULL,
deaths_new INT UNSIGNED NOT NULL,
recovered_new INT UNSIGNED NOT NULL,
confirmed_active INT UNSIGNED NOT NULL,
UNIQUE KEY (location_id,recdate),
INDEX (recdate)
) ENGINE=InnoDB
';
$db->query($create_items_table_query);
$db->begin_transaction();
// try and insert, or update the data within the key (location_id,recdate)
$put_item_query = 'INSERT INTO items (location_id,recdate,confirmed_total,deaths_total,recovered_total,confirmed_new,deaths_new,recovered_new,confirmed_active)'
. ' VALUES (?,?,?,?,?,?,?,?,?)'
. ' ON DUPLICATE KEY UPDATE'
. ' confirmed_total=?, deaths_total=?, recovered_total=?,'
. ' confirmed_new=?, deaths_new=?, recovered_new=?,'
. ' confirmed_active=?';
$put_item_stmt = $db->prepare($put_item_query);
// step through countries
foreach ($confirmed as $key => $row) {
if (!array_key_exists($key, $location_lookup))
die("Location key '$key' not find in location lookup array\n");
$location_id = $location_lookup[$key];
$last_confirmed_total = $last_deaths_total = $last_recovered_total = 0;
// step through dates within this country
for ($col = 4; $col < $cols; $col++) {
// grab date of this column
$recdate = $header[$col];
// the numbers from the timeseries CSVs
// dirty dataset from 2020-03-23: some columns empty rather than 0
$confirmed_total = is_numeric($row[$col]) ? $row[$col] : 0;
$deaths_total = array_key_exists($key, $deaths) && is_numeric($deaths[$key][$col]) ? $deaths[$key][$col] : 0;
$recovered_total = array_key_exists($key, $recovered) && is_numeric($recovered[$key][$col]) ? $recovered[$key][$col] : 0;
// calculate some extra data while we're here
$confirmed_new = $confirmed_total - $last_confirmed_total;
$deaths_new = $deaths_total - $last_deaths_total;
$recovered_new = $recovered_total - $last_recovered_total;
$confirmed_active = $confirmed_total - ($deaths_total + $recovered_total);
$put_item_stmt->bind_param('isiiiiiiiiiiiiii', $location_id, $recdate,
$confirmed_total, $deaths_total, $recovered_total, $confirmed_new, $deaths_new, $recovered_new, $confirmed_active,
$confirmed_total, $deaths_total, $recovered_total, $confirmed_new, $deaths_new, $recovered_new, $confirmed_active
);
$put_item_stmt->execute();
$last_confirmed_total = $confirmed_total;
$last_deaths_total = $deaths_total;
$last_recovered_total = $recovered_total;
}
}
$put_item_stmt->close();
$db->commit();
$db->close();
// end of file