def retrieve_pop_data(): countries = parse_countries(2) countries1 = parse_countries(1) countries['XKX'] = countries['RKS'] countries1['BLM'] = countries1['BL'] popData = {} file_name, headers = urlretrieve(URL_Pop) workbook = xlrd.open_workbook(file_name) worksheet = workbook.sheet_by_index(0) i = 0 Ix = {} for c in worksheet.row_values(0): Ix[c] = i i += 1 for row_index in range(1, worksheet.nrows): row = worksheet.row_values(row_index) popData2018 = stoi(row[Ix['popData2018']]) # replace country name if we have the "official" one in country_codes.csv country = row[Ix['countryterritoryCode']] if country == [] or not country: country = countries1[row[Ix['geoId']]] elif country in countries: country = countries[country] else: #print(f'could not find country {country}') continue popData[country] = {'populationServed': popData2018, 'srcPopulation': 'ECDC'} return popData
def update_hosp_data(hospData, popData): countries = parse_countries(2) newData = {} # OECD, only in percentages of population, up to around 2016 with open('hospital-data/hospital_capacity_oecd.csv', 'r') as fd: rdr = csv.reader(fd, delimiter=',') hdr = next(rdr) for row in rdr: country = row[0].strip() if not country in countries: #print(f'{country} not found in countries') continue else: country = countries[country] if not country in hospData: if not country in newData: newData[country] = {} newData[country][row[2]] = float(row[6]) # newData contains latest=newest entries only now, for countries we need to add for country in newData: if country in popData: if 'ACUTE' in newData[country]: if not country in hospData: hospData[country] = {} hospData[country]['hospitalBeds'] = int(popData[country]['populationServed']/1000*newData[country]['ACUTE']) hospData[country]['srcHospitalBeds'] = "Computed from acute care OECD numbers and population size" elif 'TOT' in newData[country]: if not country in hospData: hospData[country] = {} hospData[country]['hospitalBeds'] = int(popData[country]['populationServed']/1000*newData[country]['TOT']) hospData[country]['srcHospitalBeds'] = "Computed from total bed OECD numbers and population size" return hospData
def update_cia_facts(hospData, popData): r = get(URL_CIA) if not r.ok: print(f"Failed to fetch {URL}", file=sys.stderr) exit(1) db = json.loads(r.text) countries = parse_countries(2) for country in db["countries"]: country2 = None matches = [r for s,r in zip([x.lower() for x in countries.values()],[x for x in countries.values()]) if country.replace("_"," ") in s] if len(matches) == 0: # manual map to compensate if country in ciaMap: country2 = ciaMap[country] else: #print(f'no match found for {country}') continue elif len(matches) == 1: country2 = matches[0] else: # only few cases, handle manually for n in matches: if country.replace("_"," ") == n.lower(): country2 = n if country == 'united_states': country2 = 'United States of America' elif country == 'virgin_islands': country2 = 'Virgin Islands (U.S.)' if not country2: print(f'{country} matches {matches}') continue if not 'people' in db['countries'][country]['data']: continue population = stoi(db["countries"][country]["data"]["people"]["population"]["total"]) source = db["countries"][country]["metadata"]["source"] if not country2 in hospData and 'hospital_bed_density' in db['countries'][country]['data']['people']: hospData[country2] = {} hospData[country2]['hospitalBeds'] = int(population/1000*float(db['countries'][country]['data']['people']['hospital_bed_density']['beds_per_1000_population'])) if 'date' in db['countries'][country]['data']['people']['hospital_bed_density']: hospData[country2]['srcHospitalBeds'] = f"Computed from {db['countries'][country]['data']['people']['hospital_bed_density']['date']} data in CIA fact book {source}" else: hospData[country2]['srcHospitalBeds'] = f"Computed from data in CIA fact book {source}" if not country2 in popData and 'population' in db['countries'][country]['data']['people']: popData[country2] = {} popData[country2]['populationServed'] = population if 'date' in db['countries'][country]['data']['people']['population']: popData[country2]['srcPopulation'] = f"{db['countries'][country]['data']['people']['population']['date']} data in CIA fact book {source}" else: popData[country2]['srcPopulation'] = f"Data in CIA fact book {source}" return hospData, popData
def retrieve_hosp_data(): countries = parse_countries(2) hospData = {} # For now, always get the data from yesterday. We could make fancier check if today's data is already available file_name, headers = urlretrieve(URL_Hosp) workbook = xlrd.open_workbook(file_name) worksheet = workbook.sheet_by_name('Data (table)') i = 0 Ix = {} for c in worksheet.row_values(0): Ix[c] = i i += 1 for row_index in range(1, worksheet.nrows): row = worksheet.row_values(row_index) country = row[Ix['COUNTRY_REGION']] if country in countries: country = countries[country] else: #print(f'could not find country {country}') continue hospData[country] = {'hospitalBeds': stoi(row[Ix['VALUE']]), 'srcHospitalBeds': 'WHO Hospital data from https://gateway.euro.who.int/en/indicators/hfa_479-5061-number-of-acute-care-hospital-beds/'} # second data set, for Asia with open('hospital-data/ICU_asia.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) for row in rdr: country = row[0].strip() if stoi(row[1]): hospData[country] = {'hospitalBeds': stoi(row[1]), 'srcHospitalBeds': URL_ICU_ASIA} # Eurostat, newer than from WHO (2017 vs 2015) with open('hospital-data/hospital_eurostat.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) for row in rdr: country = row[0].strip() if not country in hospData: hospData[country] = {'hospitalBeds': stoi(row[1]), 'srcHospitalBeds': URL_Hosp_Eurostat} else: #print(f'found new data for {country}: old: {hospData[country]["hospitalBeds"]}, new: {stoi(row[1])}') hospData[country] = {'hospitalBeds': stoi(row[1]), 'srcHospitalBeds': URL_Hosp_Eurostat} return hospData