Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from ibarraespinosa/master
Browse files Browse the repository at this point in the history
Update base_url
  • Loading branch information
Sergio Ibarra Espinosa authored Mar 10, 2020
2 parents 473870e + 6eeb83f commit 409629f
Show file tree
Hide file tree
Showing 49 changed files with 737 additions and 495 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Imports:
jsonlite,
reshape2,
sp
RoxygenNote: 7.0.2
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.0): do not edit by hand
# Generated by roxygen2: do not edit by hand

export(climate_map)
export(create_map_df)
Expand Down
7 changes: 6 additions & 1 deletion R/check_ISO_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -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=" "))}
Expand Down
4 changes: 3 additions & 1 deletion R/climate_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]){
Expand Down
1 change: 1 addition & 0 deletions R/create_map_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/download_kml.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 21 additions & 15 deletions R/get_climate_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,34 @@
#'

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")
}
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){
Expand All @@ -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)
}


Expand Down
14 changes: 12 additions & 2 deletions R/get_data_recursive.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
11 changes: 8 additions & 3 deletions R/get_ensemble_climate_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions R/get_historical_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
5 changes: 4 additions & 1 deletion R/get_model_temp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
4 changes: 3 additions & 1 deletion R/kml_to_sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"){



Expand Down
3 changes: 1 addition & 2 deletions man/Africa_basin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Africa_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Asia_basin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Asia_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Eur_basin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Eur_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/NoAm_basin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/NoAm_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Oceana_basin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/Oceana_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/SoAm_basin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/SoAm_country.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/check_ISO_code.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/check_locator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/climate_map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/codes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 409629f

Please sign in to comment.