Skip to content

Instantly share code, notes, and snippets.

@pjaspers
Created June 25, 2020 11:40
Show Gist options
  • Save pjaspers/82e3746022426431022991616ca87d53 to your computer and use it in GitHub Desktop.
Save pjaspers/82e3746022426431022991616ca87d53 to your computer and use it in GitHub Desktop.
require "csv"
require "time"
CITIES = [
"Leuven",
"Hamont-Achel",
"Kortrijk",
"De Panne",
"Herentals",
"Antwerpen",
"Balen",
"Heist-op-den-Berg",
"Hasselt"
].freeze
command = <<SHELL
for i in {10..25}; do
if ! [ -f "./COVID19BE_CASES_MUNI_CUM_202006${i}.csv" ]; then
curl -O "https://epistat.sciensano.be/Data/202006${i}/COVID19BE_CASES_MUNI_CUM_202006${i}.csv"
fi
done
SHELL
`#{command}`
data = {}
cities = CITIES
prev = 0
cases = 0
Dir.glob("COVID19BE_CASES_MUNI_CUM*.csv").each do |name|
total_cases = 0
CSV.foreach(name, headers: true, encoding: "ISO-8859-1:UTF-8") do |row|
city = row["TX_DESCR_NL"]
total_cases += row["CASES"].to_i
next unless cities.include? city
date_part = name.split("_").last.split(".").first
date = begin
Time.parse(date_part)
rescue ArgumentError
nil
end
data[city] ||= []
data[city] << [date, row["CASES"]]
end
cases = total_cases - prev
prev = total_cases
rescue CSV::MalformedCSVError
puts "#{name} kan het niet aan"
end
results = CITIES.inject({}) do |result, city|
if arr = data[city]
first, *rest = arr.sort_by(&:first).last(10)
prev = first.last.to_i
result[city] = rest.collect do |date, cases|
cases = cases.to_i
r = [date, cases - prev, cases]
prev = cases
r
end.sort_by(&:first)
end
result
end
dates = results[CITIES.first].collect(&:first)
fd = dates.collect {|d| "%6s" % d.strftime("%a %d")}
header = "%20s | %s" % [ "City", fd.join("|")]
puts header
puts "-" * header.length
results.each do |city, data|
fr = data.collect do |d|
delta = d[1]
if delta > 0
"%6d" % d[1]
else
"%6s" % ""
end
end.join("|")
puts "%20s | %s" % [ city, fr]
end
puts cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment