diff --git a/R/check_ISO_code.R b/R/check_ISO_code.R index c035d83..ef55a01 100644 --- a/R/check_ISO_code.R +++ b/R/check_ISO_code.R @@ -11,7 +11,12 @@ check_ISO_code <- function(iso){ -codes <- c(NoAm_country,SoAm_country,Oceana_country,Africa_country,Asia_country,Eur_country) +codes <- c(NoAm_country, + SoAm_country, + Oceana_country, + Africa_country, + Asia_country, + Eur_country) if(nchar(iso) != 3 && is.character(iso)){stop("Please enter a valid 3 letter country code")} if(is.numeric(iso)){stop("Please enter a 3 letter code, not a number")} if(!toupper(iso)%in%codes){stop(paste(iso,"is an invalid 3 letter country code, please refer to http://unstats.un.org/unsd/methods/m49/m49alpha.htm for a valid list",sep=" "))} diff --git a/R/climate_map.R b/R/climate_map.R index 8d140c8..37fb8ba 100644 --- a/R/climate_map.R +++ b/R/climate_map.R @@ -21,7 +21,9 @@ #' #'@export -climate_map <- function(map_df, data_df, return_map = TRUE){ +climate_map <- function(map_df, + data_df, + return_map = TRUE){ ### You can't plot more that one piece of data on a map ### so we need to check that there's not more data than regions to plot if(length(unique(map_df$ID)) != dim(data_df)[1]){ diff --git a/R/create_map_df.R b/R/create_map_df.R index 3565ff6..66571c6 100644 --- a/R/create_map_df.R +++ b/R/create_map_df.R @@ -29,6 +29,7 @@ create_map_df <- function(locator) { df_out <- list() map_pb <- txtProgressBar(min = 0, max = length(locator), style = 3) + for(i in 1:length(locator)) { fName <- paste(my_path,to_plot[i], ".kml", sep = "") fSize <- file.info(fName)$size diff --git a/R/download_kml.R b/R/download_kml.R index 2d35ca5..3b11200 100644 --- a/R/download_kml.R +++ b/R/download_kml.R @@ -23,7 +23,7 @@ download_kml <- function(locator) { #check which kml are already downloaded to_download <- locator[!locator%in%kml_files] - base_url <- "http://api.worldbank.org/climateweb/rest/v1" + base_url <- "http://climatedataapi.worldbank.org/climateweb/rest/v1/" if(length(to_download) > 0){ download_pb <- txtProgressBar(min = 0, max = length(to_download), style = 3) diff --git a/R/get_climate_data.R b/R/get_climate_data.R index 297755e..a2f355f 100644 --- a/R/get_climate_data.R +++ b/R/get_climate_data.R @@ -13,13 +13,13 @@ #' get_climate_data <- function(locator,geo_type,type, cvar, start, end){ - base_url <- "http://api.worldbank.org/climateweb/rest/v1/" + base_url <- "http://climatedataapi.worldbank.org/climateweb/rest/v1/" ### Error handling if(geo_type == "country"){ check_ISO_code(locator) } - + if(geo_type == "basin"){ if(is.na(as.numeric(locator))){ stop("You must enter a valid Basin number between 1 and 468") @@ -27,14 +27,20 @@ get_climate_data <- function(locator,geo_type,type, cvar, start, end){ if(as.numeric(locator) < 1 || as.numeric(locator) > 468){ as.numeric(locator) < 1 || as.numeric(locator) > 468 } - + } data_url <- paste(geo_type,type,cvar,start,end,locator,sep="/") + +# print(data_url) + extension <- ".json" full_url <- paste(base_url,data_url,extension,sep="") - res <- GET(full_url) + + print(full_url) + + res <- GET(full_url) stop_for_status(res) raw_data <- try(content(res, as = "text"), silent = TRUE) if(sum(grep("unexpected",raw_data)) > 0){ @@ -55,17 +61,17 @@ get_climate_data <- function(locator,geo_type,type, cvar, start, end){ if(type == "mavg" && start > 2010){ do_list <- list() for( i in 1:length(unique(data_out$scenario))){ - ### Unpack the lists - split_do <- subset(data_out,data_out$scenario == unique(data_out$scenario)[i]) - tmp <- data.frame(sapply(split_do$monthV,unlist)) - colnames(tmp) <- split_do$gcm - tmp$fromYear <- rep(start,12) - tmp$toYear <- rep(end,12) - do_list[[i]] <- melt(tmp,id.vars =c("fromYear","toYear"), variable.name = c("gcm"), value.name = "data") - do_list[[i]]$scenario <- rep(split_do$scenario[1],dim(do_list[[i]])[1]) - do_list[[i]]$month <- rep(1:12,dim(do_list[[i]])[1]/12) - } - data_out <- do.call(rbind,do_list) + ### Unpack the lists + split_do <- subset(data_out,data_out$scenario == unique(data_out$scenario)[i]) + tmp <- data.frame(sapply(split_do$monthV,unlist)) + colnames(tmp) <- split_do$gcm + tmp$fromYear <- rep(start,12) + tmp$toYear <- rep(end,12) + do_list[[i]] <- melt(tmp,id.vars =c("fromYear","toYear"), variable.name = c("gcm"), value.name = "data") + do_list[[i]]$scenario <- rep(split_do$scenario[1],dim(do_list[[i]])[1]) + do_list[[i]]$month <- rep(1:12,dim(do_list[[i]])[1]/12) + } + data_out <- do.call(rbind,do_list) } diff --git a/R/get_data_recursive.R b/R/get_data_recursive.R index 350903b..01c2d87 100644 --- a/R/get_data_recursive.R +++ b/R/get_data_recursive.R @@ -17,13 +17,23 @@ -get_data_recursive <- function(locator,geo_type,type, cvar, start, end){ +get_data_recursive <- function(locator, + geo_type, + type, + cvar, + start, + end){ dates <- date_correct(start,end) data_out <- list() counter <- 1 for(i in 1:length(locator)){ for(j in 1:length(dates[,1])){ - data_out[[counter]] <- get_climate_data(locator[i],geo_type,type,cvar,dates[j,1],dates[j,2]) + data_out[[counter]] <- get_climate_data(locator[i], + geo_type, + type, + cvar, + dates[j,1], + dates[j,2]) counter <- counter + 1 } } diff --git a/R/get_ensemble_climate_data.R b/R/get_ensemble_climate_data.R index 30d6fac..8a3dcad 100644 --- a/R/get_ensemble_climate_data.R +++ b/R/get_ensemble_climate_data.R @@ -16,9 +16,14 @@ #' conform to dates, the fuction will automatically round them. #' @param end The ending year you want data for, can be in the past or the future. #' Similar to the start date, dates will be rounded to the nearest end dat. -get_ensemble_climate_data <- function(locator, geo_type, type, cvar, start, end){ - base_url <- "http://api.worldbank.org/climateweb/rest/v1/" - +get_ensemble_climate_data <- function(locator, + geo_type, + type, + cvar, + start, + end){ + base_url <- "http://climatedataapi.worldbank.org/climateweb/rest/v1/" + ### Error handling if(geo_type == "country"){ check_ISO_code(locator) diff --git a/R/get_historical_data.R b/R/get_historical_data.R index eb0a7ab..95dccb5 100644 --- a/R/get_historical_data.R +++ b/R/get_historical_data.R @@ -12,8 +12,8 @@ #' @return a dataframe with historical climate data #' @details The time_scale parameter returns a different number of variables depending on the input timescale. \emph{Month} will return 12 values, a historical average for that month across all years. \emph{Year} will return yearly averages for each year, and \emph{decade} will return decade averages. get_historical_data <- function(locator,cvar,time_scale){ -base_url <- "http://api.worldbank.org/climateweb/rest/v1/" - + base_url <- "http://climatedataapi.worldbank.org/climateweb/rest/v1/" + ### check cvar is valid var_vec <- c("tas","pr") if(!cvar%in%var_vec){ diff --git a/R/get_model_temp.R b/R/get_model_temp.R index 2002f66..9a5d96b 100644 --- a/R/get_model_temp.R +++ b/R/get_model_temp.R @@ -32,7 +32,10 @@ #'} #'@export -get_model_temp <- function(locator,type, start, end){ +get_model_temp <- function(locator, + type, + start, + end){ ### check type is valid typevec <- c("mavg","annualavg","manom","annualanom") if(!type%in%typevec){ diff --git a/R/kml_to_sp.R b/R/kml_to_sp.R index a39a186..9305c82 100644 --- a/R/kml_to_sp.R +++ b/R/kml_to_sp.R @@ -18,7 +18,9 @@ #'} #'@export -kml_to_sp <- function(map_df, df = NULL, crs_string = "+proj=longlat +datum=WGS84"){ +kml_to_sp <- function(map_df, + df = NULL, + crs_string = "+proj=longlat +datum=WGS84"){ diff --git a/vignettes/rWBclimate.Rmd b/vignettes/rWBclimate.Rmd index efa4057..c39b6e1 100644 --- a/vignettes/rWBclimate.Rmd +++ b/vignettes/rWBclimate.Rmd @@ -86,6 +86,8 @@ Say you want to compare temperature from two different models in the USA to see ```r +library(rWBclimate) +library(ggplot2) usa.dat <- get_model_temp("USA", "mavg", 2080, 2100) usa.dat.bcc <- usa.dat[usa.dat$gcm == "bccr_bcm2_0", ] usa.dat.had <- usa.dat[usa.dat$gcm == "ukmo_hadcm3", ] @@ -98,7 +100,7 @@ ggplot(plot.df, aes(x = as.factor(month), y = data, group = ID, colour = gcm, xlab("Month") + theme_bw() ``` -![plot of chunk getmodeldata](figure/getmodeldata.png) +![plot of chunk getmodeldata](figure/getmodeldata.png) Subsetting all the data can be a bit tedious. You could also compare all the models but just for one scenario, the A2.