Skip to content

Commit

Permalink
adding fourier series func, and starting seasonal dynamics move from …
Browse files Browse the repository at this point in the history
…docs
  • Loading branch information
gilesjohnr committed Sep 25, 2024
1 parent fbb8ec5 commit 52bd34d
Show file tree
Hide file tree
Showing 21 changed files with 567 additions and 147 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
arrow,
cowplot,
countrycode,
dplyr,
elevatr,
FNN,
ggplot2,
ggraph,
ggrepel,
Expand All @@ -35,6 +37,7 @@ Imports:
jsonlite,
lubridate,
magrittr,
minpack.lm,
mobility,
propvacc,
raster,
Expand All @@ -51,8 +54,7 @@ Imports:
remotes,
scales,
shiny,
arrow,
minpack.lm
shinyWidgets
Depends:
R (>= 4.1.1),
stats,
Expand Down
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ importFrom("lubridate", "ceiling_date")
importFrom("glue", "glue")
importFrom("propvacc", "get_beta_params")
importFrom("tidyr", "pivot_longer")
importFrom("stats", "aggregate", "cor", "median", "optim", "quantile")
importFrom("stats", "aggregate", "cor", "median", "optim", "quantile", "vcov")
importFrom("stats", "dbeta", "density", "dgamma", "dlnorm", "qgamma", "rbeta", "runif")
importFrom("utils", "install.packages", "installed.packages")
importFrom("arrow", "write_parquet", "read_parquet")
importFrom("minpack.lm", "nls.lm", "nls.lm.control")
importFrom("grDevices", "colorRampPalette")
importFrom("graphics", "legend", "lines", "points", "text")
importFrom("stats", "dbeta", "density", "dgamma", "dlnorm", "qgamma",
"rbeta", "runif")

126 changes: 73 additions & 53 deletions R/download_climate_data.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Download and Save Climate Data for Multiple Countries (Parquet Format, Single Model)
#' Download and Save Climate Data for Multiple Countries (Parquet Format, Multiple Models and Variables)
#'
#' This function downloads daily climate data for a list of specified countries, saving the data as Parquet files. The data includes both historical and future climate variables at grid points within each country for a specified climate model.
#' This function downloads daily climate data for a list of specified countries, saving the data as Parquet files. The data includes both historical and future climate variables at grid points within each country for a specified set of climate models and variables.
#'
#' @param PATHS A list containing paths where raw and processed data are stored.
#' PATHS is typically the output of the `get_paths()` function and should include:
Expand All @@ -13,7 +13,7 @@
#' @param n_points An integer specifying the number of grid points to generate within each country for which climate data will be downloaded.
#' @param date_start A character string representing the start date for the climate data (in "YYYY-MM-DD" format).
#' @param date_stop A character string representing the end date for the climate data (in "YYYY-MM-DD" format).
#' @param climate_model A single character string representing the climate model to use. Available models include:
#' @param climate_models A character vector of climate models to use. Available models include:
#' \itemize{
#' \item \strong{CMCC_CM2_VHR4}
#' \item \strong{FGOALS_f3_H}
Expand All @@ -23,12 +23,25 @@
#' \item \strong{MPI_ESM1_2_XR}
#' \item \strong{NICAM16_8S}
#' }
#' @param climate_variables A character vector of climate variables to retrieve. See details for available variables.
#'
#' @return The function does not return a value. It downloads the climate data for each country and saves the results as Parquet files in the specified directory.
#' @return The function does not return a value. It downloads the climate data for each country, climate model, and climate variable, saving the results as Parquet files in the specified directory.
#'
#' @details This function uses country shapefiles to generate a grid of points within each country, at which climate data is downloaded. The function retrieves climate data for the specified date range (`date_start` to `date_stop`) and the specified climate model. The data is saved for each country in a Parquet file named `climate_data_{climate_model}_{date_start}_{date_stop}_{ISO3}.parquet`.
#' @details This function uses country shapefiles to generate a grid of points within each country, at which climate data is downloaded. The function retrieves climate data for the specified date range (`date_start` to `date_stop`) and the specified climate models and variables. The data is saved for each country, climate model, and climate variable in a Parquet file named `climate_data_{climate_model}_{climate_variable}_{date_start}_{date_stop}_{ISO3}.parquet`.
#'
#' The climate data variables include temperature, wind speed, cloud cover, precipitation, and more. The function retrieves data from a single climate model.
#' The available climate variables include:
#' \itemize{
#' \item \strong{temperature_2m_mean}, \strong{temperature_2m_max}, \strong{temperature_2m_min}
#' \item \strong{wind_speed_10m_mean}, \strong{wind_speed_10m_max}
#' \item \strong{cloud_cover_mean}
#' \item \strong{shortwave_radiation_sum}
#' \item \strong{relative_humidity_2m_mean}, \strong{relative_humidity_2m_max}, \strong{relative_humidity_2m_min}
#' \item \strong{dew_point_2m_mean}, \strong{dew_point_2m_min}, \strong{dew_point_2m_max}
#' \item \strong{precipitation_sum}, \strong{rain_sum}, \strong{snowfall_sum}
#' \item \strong{pressure_msl_mean}
#' \item \strong{soil_moisture_0_to_10cm_mean}
#' \item \strong{et0_fao_evapotranspiration_sum}
#' }
#'
#' @importFrom dplyr select mutate
#' @importFrom lubridate year month week yday
Expand All @@ -46,10 +59,11 @@
#' # API key for climate data API
#' api_key <- "your-api-key-here"
#'
#' # Download climate data for a specified model and save it for the specified countries
#' # Download climate data for multiple models and variables and save it for the specified countries
#' download_climate_data(PATHS, iso_codes, n_points = 5,
#' date_start = "1970-01-01", date_stop = "2030-12-31",
#' climate_model = "MRI_AGCM3_2_S", api_key)
#' climate_models = c("MRI_AGCM3_2_S", "EC_Earth3P_HR"),
#' climate_variables = c("temperature_2m_mean", "precipitation_sum"), api_key)
#'}
#'
#' @export
Expand All @@ -59,68 +73,74 @@ download_climate_data <- function(PATHS,
n_points,
date_start,
date_stop,
climate_model,
climate_models,
climate_variables,
api_key) {

if (length(climate_model) > 1) stop("One climate model at a time")

# Ensure output directory exists, if not, create it
if (!dir.exists(PATHS$DATA_CLIMATE)) {
dir.create(PATHS$DATA_CLIMATE, recursive = TRUE)
}

# List of climate variables for both historical and future data
climate_variables_historical_and_future <- c(
climate_variables <- c(
"temperature_2m_mean", "temperature_2m_max", "temperature_2m_min",
"wind_speed_10m_mean", "wind_speed_10m_max", "cloud_cover_mean",
"shortwave_radiation_sum", "relative_humidity_2m_mean",
"relative_humidity_2m_max", "relative_humidity_2m_min",
"dew_point_2m_mean", "dew_point_2m_min", "dew_point_2m_max",
"precipitation_sum", "rain_sum", "snowfall_sum",
"precipitation_sum", "rain_sum",
"pressure_msl_mean", "soil_moisture_0_to_10cm_mean",
"et0_fao_evapotranspiration_sum"
)

# Loop through each ISO3 country code
for (country_iso_code in iso_codes) {
# Ensure output directory exists, if not, create it
if (!dir.exists(PATHS$DATA_CLIMATE)) {
dir.create(PATHS$DATA_CLIMATE, recursive = TRUE)
}

# Loop through each climate model
for (climate_model in climate_models) {

# Loop through each ISO3 country code
for (country_iso_code in iso_codes) {

# Loop through each climate variable
for (climate_variable in climate_variables) {

message(glue::glue("Downloading daily climate data for {country_iso_code} using {climate_model} at {n_points} points"))
message(glue::glue("Downloading daily {climate_variable} data for {country_iso_code} using {climate_model} at {n_points} points"))

# Convert ISO3 code to country name and read country shapefile
country_name <- MOSAIC::convert_iso_to_country(country_iso_code)
country_shp <- sf::st_read(dsn = file.path(PATHS$DATA_SHAPEFILES, paste0(country_iso_code, "_ADM0.shp")), quiet = TRUE)
# Convert ISO3 code to country name and read country shapefile
country_name <- MOSAIC::convert_iso_to_country(country_iso_code)
country_shp <- sf::st_read(dsn = file.path(PATHS$DATA_SHAPEFILES, paste0(country_iso_code, "_ADM0.shp")), quiet = TRUE)

# Generate grid points within the country
grid_points <- MOSAIC::generate_country_grid_n(country_shp, n_points = n_points)
coords <- sf::st_coordinates(grid_points)
coords <- as.data.frame(coords)
rm(grid_points)
rm(country_shp)
# Generate grid points within the country
grid_points <- MOSAIC::generate_country_grid_n(country_shp, n_points = n_points)
coords <- sf::st_coordinates(grid_points)
coords <- as.data.frame(coords)
rm(grid_points)
rm(country_shp)

# Download climate data for the generated grid points
climate_data <- MOSAIC::get_climate_future(lat = coords$Y,
lon = coords$X,
date_start = date_start,
date_stop = date_stop,
climate_variables = climate_variables_historical_and_future,
climate_model = climate_model,
api_key = api_key)
# Download climate data for the generated grid points for the current climate variable
climate_data <- MOSAIC::get_climate_future(lat = coords$Y,
lon = coords$X,
date_start = date_start,
date_stop = date_stop,
climate_variables = climate_variable, # Single variable per loop
climate_model = climate_model,
api_key = api_key)

# Add additional metadata columns
climate_data <- data.frame(
country_name = country_name,
iso_code = country_iso_code,
year = lubridate::year(climate_data$date),
month = lubridate::month(climate_data$date),
week = lubridate::week(climate_data$date),
doy = lubridate::yday(climate_data$date),
climate_data
)
# Add additional metadata columns
climate_data <- data.frame(
country_name = country_name,
iso_code = country_iso_code,
year = lubridate::year(climate_data$date),
month = lubridate::month(climate_data$date),
week = lubridate::week(climate_data$date),
doy = lubridate::yday(climate_data$date),
climate_data
)

# Save climate data as Parquet, including the climate model in the file name
arrow::write_parquet(climate_data,
sink = file.path(PATHS$DATA_CLIMATE, paste0("climate_data_", climate_model, "_", date_start, "_", date_stop, "_", country_iso_code, ".parquet")))
# Save climate data as Parquet, including the climate model and variable in the file name
arrow::write_parquet(climate_data,
sink = file.path(PATHS$DATA_RAW, paste0("climate/climate_data_", climate_model, "_", climate_variable, "_", date_start, "_", date_stop, "_", country_iso_code, ".parquet")))
}
}
}

message(glue::glue("Climate data saved for all countries using {climate_model} here: {PATHS$DATA_CLIMATE}"))
message(glue::glue("Raw climate data saved for all countries, variables, and models here: {PATHS$DATA_RAW}/climate"))
}
2 changes: 1 addition & 1 deletion R/est_mobility.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#'
#' @importFrom ggplot2 ggplot aes geom_tile xlab ylab theme_bw element_text margin scale_fill_gradient guides guide_colorbar geom_segment geom_sf geom_point geom_text scale_size_continuous theme_void
#' @importFrom reshape2 melt
#' @importFrom mobility fit_prob_travel fit_mobility mobility summary predict
#' @importFrom mobility fit_prob_travel mobility summary predict
#' @importFrom sf st_read st_coordinates st_centroid
#' @importFrom dplyr left_join rename
#' @importFrom glue glue
Expand Down
Loading

0 comments on commit 52bd34d

Please sign in to comment.