Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision a93e442dfb67053f3de2e74ee06ceb99e53a4524 authored by Pierre Masselot on 07 November 2024, 17:08:33 UTC, committed by Pierre Masselot on 07 November 2024, 17:08:33 UTC
Zenodo repo pub
1 parent a4f5dec
  • Files
  • Changes
  • 58173d1
  • /
  • 02_prep_data.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:a93e442dfb67053f3de2e74ee06ceb99e53a4524
directory badge
swh:1:dir:58173d1bde519d4f834511fd10e457d9220f5825
content badge
swh:1:cnt:ba4ddd478267c8b10f28ad7fe1d0bceb72688e52

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
02_prep_data.R
################################################################################
# 
# Contrasting future heat and cold-related mortality under climate change, 
# demographic and adaptation scenarios in 854 European cities
#
# R Code Part 2: Load and prepare data
#
# Pierre Masselot & Antonio Gasparrini
#
################################################################################
#------------------------
# LOAD EUcityTRM info
#------------------------

# read coefficients 
coefs <- read.csv("data/coefs.csv") |>
  mutate(agegroup = factor(agegroup, levels = agelabs))

# Read city and age values
cityage <- read.csv("data/city_results.csv")

# Reduce to cities only
cities <- unique(subset(cityage, select = c("URAU_CODE", "LABEL", "CNTR_CODE", 
  "cntr_name", "region", "lon", "lat", "pop")))

# Countries
countries <- summarise(cities, 
    ncities = length(URAU_CODE), lat = mean(lat), region = unique(region), 
    .by = CNTR_CODE) |>
  mutate(region = factor(region, levels = ordreg)) |>
  arrange(region, desc(lat))

# Reorder cities
cities <- mutate(cities, region = factor(region, levels = ordreg),
    CNTR_CODE = factor(CNTR_CODE, levels = countries$CNTR_CODE)) |>
  arrange(region, CNTR_CODE, URAU_CODE)

#------------------------
# LOAD DEMOGRAPHIC DATA
#------------------------

#----- Load Wittgenstein projections

# Read Age-specific survival ratios
projdeath <- fread("data/wittgenstein_assr.csv")

# Attach the first year to each period to later merge with population
projdeath[, year5 := as.numeric(substr(period, 1, 4))]

# Read Population
projpop <- fread("data/wittgenstein_pop.csv") |> rename(year5 = "year")

# Select sex and age
projdeath <- projdeath[age != "Newborn"]
projpop <- projpop[age != "All" & sex != "Both",]

# Merge population and deaths
proj <- merge(projpop, projdeath)

# Select only relevant years
proj <- proj[year5 %between% histrange | year5 %between% range(projrange),]

# Rescale population and compute deaths
proj[, ":="(pop = 1000 * pop, death = 1000 * pop * (1 - assr))]

# Aggregate sex
proj <- proj[, .(pop = sum(pop), death = sum(death)), 
  by = .(CNTR_CODE, age, ssp, year5)]

#----- Aggregate by age group

# Create age groups
proj[, agegroup := cut(as.numeric(sapply(strsplit(age, "[-+]"), "[", 1)), 
  agebreaks, include.lowest = T, right = F, labels = agelabs)]

# Sum population and death by age group
proj <- proj[!is.na(agegroup), .(wittpop = sum(pop), wittdeath = sum(death)), 
  by = .(CNTR_CODE, agegroup, ssp, year5)]

# Rescale number of deaths as annual average
proj[, ":="(wittdeath = wittdeath / 5)]

#----- Calibrate using Urban Audit

# Average pop and deaths of Wittgenstein over historical period
histo_witt <- proj[year5 %between% histrange, 
  .(histowpop = mean(wittpop), histowdeaths = mean(wittdeath)), 
  by = .(CNTR_CODE, agegroup, ssp)]

# Extract pop and deaths from EUcityTRM
citycal <- rename(cityage, uraupop = agepop, uraudeath = death) |>
  subset(select = c(URAU_CODE, CNTR_CODE, agegroup, uraupop, uraudeath))

# Merge Eurostat and projection data
citycal <- merge(histo_witt, citycal, all = T, allow.cartesian = T)

# Multiplicative factor for each city from national
citycal[, ":="(popfac = uraupop / histowpop, dfac = uraudeath / histowdeaths)]

# Add correction to population projection
projdata <- merge(proj, citycal, allow.cartesian = T, all = T)
projdata[, ":="(pop = wittpop * popfac, death = wittdeath * dfac)]

#----- Some further data cleaning

# Select SSP. Discard the duplicates of historical period
projdata <- projdata[ssp %in% ssplist,]
projdata[, ssp := as.character(ssp)]
projdata[year5 %between% histrange, ssp := "hist"]
projdata <- unique(projdata)

# Select cities with full data
cities <- subset(cities, URAU_CODE %in% projdata$URAU_CODE)
projdata <- subset(projdata, URAU_CODE %in% cities$URAU_CODE)

# Define factors with nice names
cities <- mutate(cities, 
  across(URAU_CODE:cntr_name, ~ factor(.x, levels = unique(.x))),
  region = factor(region, levels = ordreg))

# Order demographic data
projdata[, ":="(URAU_CODE = factor(URAU_CODE, levels = levels(cities$URAU_CODE)),
  agegroup = factor(agegroup, levels = agelabs))]
setkey(projdata, ssp, URAU_CODE, agegroup)


#------------------------
# Load warming target data
#------------------------

# Read data
warming_years <- read.csv("data/warming_years.csv", check.names = F)

# Reshape into long data.frame
warming_years <- pivot_longer(warming_years, !model_run, names_to = "level", 
  values_to = "year")

# Rename and select targets
warming_years <- mutate(warming_years, 
    model_run = str_split_i(model_run, "_", 1),
    gcm = gsub("-", "_", model_run),
    model_run = NULL,
    ssp = str_split_i(level, "_", 2),
    ssp = substr(str_extract(ssp, ".*ssp[[:digit:]]"), 4, 4),
    level = str_split_i(level, "_", 1)) |>
  subset(level %in% targets & ssp %in% ssplist)

# Expand for all years within the window and for all cities/ssp/agegroup
warming_win <- group_by(warming_years, gcm, level, ssp) |>
  reframe(year = if (is.na(year)) NA else (year - win_len):(year + win_len)) |>
  ungroup() |>
  mutate(year5 = floor(year / 5) * 5) |> 
  data.table()

#------------------------
# Other data
#------------------------

# Load European map data
euromap <- gisco_get_countries(year = "2020", cache_dir = "data")
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API