Skip to content

Commit

Permalink
version 0.0.3
Browse files Browse the repository at this point in the history
Increase speed with foreach
Point level abstaction across functions
new exports
  • Loading branch information
mikejohnson51 committed May 7, 2019
1 parent 9413894 commit 53ddede
Show file tree
Hide file tree
Showing 67 changed files with 727 additions and 2,060 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
^.*\.Rproj$
^\.Rproj\.user$
inProgress
sampleFiles
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.Rhistory
.RData
.Ruserdata
sampleFiles
sampleFiles
inProgress
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: climateR
Type: Package
Title: climateR
Description: A set of functions to subset climate data to an area of interest.
Version: 0.0.2
Version: 0.0.3
Authors@R: person("Mike", "Johnson", role = c("aut", "cre"), email = "[email protected]")
Maintainer: Mike Johnson <[email protected]>
BugReports: https://github.com/mikejohnson51/climateR/issues
Expand All @@ -12,8 +12,11 @@ Depends:
AOI
Imports:
areal,
doParallel,
foreach,
httr,
magrittr,
ncdf4,
RNetCDF,
raster,
sf
VignetteBuilder: knitr
Expand Down
32 changes: 16 additions & 16 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(buildWeights)
export(define.grid2)
export(getBCCA)
export(getBCSDvic)
export(getCHIRPS)
export(getDaymet)
export(getGridMET)
export(getLOCA)
export(getMACA)
export(getPRISM)
export(makeGrid)
export(zonal)
export(getTerraClim)
import(AOI)
importFrom(crayon,"%+%")
importFrom(crayon,blue)
importFrom(crayon,cyan)
importFrom(crayon,white)
importFrom(crayon,yellow)
importFrom(RNetCDF,open.nc)
importFrom(RNetCDF,var.get.nc)
importFrom(doParallel,registerDoParallel)
importFrom(foreach,"%do%")
importFrom(foreach,"%dopar%")
importFrom(foreach,foreach)
importFrom(httr,GET)
importFrom(httr,write_disk)
importFrom(magrittr,"%>%")
importFrom(ncdf4,nc_close)
importFrom(ncdf4,nc_open)
importFrom(ncdf4,ncvar_get)
importFrom(raster,brick)
importFrom(raster,crop)
importFrom(raster,crs)
importFrom(raster,extent)
importFrom(raster,intersect)
importFrom(raster,mosaic)
importFrom(raster,raster)
importFrom(raster,stack)
importFrom(raster,stackApply)
importFrom(raster,writeRaster)
Binary file modified R/.DS_Store
Binary file not shown.
44 changes: 0 additions & 44 deletions R/define_grid2.R

This file was deleted.

38 changes: 15 additions & 23 deletions R/getBCCA.R
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
#' @title Get BCCA Climate Data for an Area of Interest
#' @description Bias Corrected Constructed Analogs (BCCA) V2 Daily Climate Projections.
#' This archive contains daily BCCA CMIP3 and CMIP5 projections of precipitation, daily maximum, and daily minimum temperature over the contiguous
#' This service offeres an interface containing daily BCCA CMIP3 and CMIP5 projections of precipitation, daily maximum, and daily minimum temperature over the contiguous
#' United States. The dataset content is based on global climate projections from the World Climate Research Programme's (WCRP's) Coupled Model
#' Intercomparison Project phase 3 (CMIP3) multi-model dataset referenced in the Intergovernmental Panel on Climate Change Fourth Assessment Report,
#' and the phase 5 (CMIP5) multi-model dataset that is informing the IPCC Fifth Assessment.
#' @param AOI a spatial polygon object (sf or sp)
#' @param param a meterological parameter (see `param_meta$bcca`)
#' @param model GMC model name (see `model_meta$bcca$name`)
#' @param model GMC model name (see `model_meta$bcca`)
#' @param scenario a climate scenario pathway (rcp45 or rcp85)
#' @param startDate a start date given as "YYYY-MM-DD"
#' @param endDate an end date given as "YYYY-MM-DD"
#' @author Mike Johnson
#' @return a list of rasterStacks
#' @return if AOI is an areal extent a list of rasterStacks, if AOI is a point then a data.frame of modeled records.
#' @export


getBCCA = function(AOI, param, model = 'CCSM4', scenario = 'rcp45', startDate, endDate = NULL){

base = 'https://cida.usgs.gov/thredds/dodsC/cmip5_bcca/'
id = 'bcca'
base = 'https://cida.usgs.gov/thredds/dodsC/cmip5_bcca/'

d = define.dates(startDate, endDate)
v = define.versions(dates = d$date, scenario = scenario, future.call = "future?", historic.call = "historical?")
p = define.param(param, service = 'bcca')
k = define.config(dataset = "bcca", model = model, ensemble = NA)
p = define.param(param, service = id)
k = define.config(dataset = id, model = model, ensemble = NA)

tmp = expand.grid(min.date = v$min.date, model = k, call = p$call, stringsAsFactors = FALSE)
fin = merge(v, tmp, "min.date") %>% merge(p, "call") %>% merge(model_meta$bcca, "model")
fin = fin[fin$scenario %in% scenario,]

g = define.grid2(AOI, url = paste0(base, fin$calls[1]) )
s = define.initial(g, d, p, dataset = "bcca")

for(i in 1:NROW(fin)){

variable_call = paste0( "BCCA_0-125deg_",fin$call[i], "_day_", fin$model[i], "_", fin$ver[i], "_", fin$ensemble[i])
g = define.grid3(AOI, id)

nc = RNetCDF::open.nc(paste0(
base, fin$calls[i], variable_call,
fin$time.index[i],
g$lat.call,
g$lon.call))
variable_call = paste0( "BCCA_0-125deg_",fin$call, "_day_", fin$model, "_", fin$ver, "_", fin$ensemble)

var = RNetCDF::var.get.nc(nc, variable_call )
urls = paste0(base, fin$calls, variable_call, fin$time.index, g$lat.call, g$lon.call)

RNetCDF::close.nc(nc)
s = fast.download(urls, params = variable_call, names = paste0(fin$model, "_",fin$ensemble, "_", fin$call), g, date.names = d$date, dataset = id)

s = process.var(group = s, g = g, var, dates = seq.Date(fin$min.date[i], fin$max.date[i],1),
param = fin$common.name[i], name = paste0(fin$ver[i], "_", fin$model[i]))
}
s

return(s)

}

43 changes: 15 additions & 28 deletions R/getBCSD.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
#' @title Get BCSD VIC outputs Climate Data for an Area of Interest
#' @description Multivariate Adaptive Constructed Analogs (MACA) is a statistical method for downscaling Global Climate Models
#' (GCMs) from their native coarse resolution to a higher spatial resolution that captures
#' reflects observed patterns of daily near-surface meteorology and simulated changes in GCMs experiments.
#' @title Get BCSD Hydrologic (VIC) Outputs for an Area of Interest
#' @description CMIP5 Monthly Hydrology Projections driven with BCSD climate forcings
#' @param AOI a spatial polygon object (sf or sp)
#' @param param a meterological parameter (see `param_meta$maca`)
#' @param model GMC model name (see `model_meta$maca$name`)
#' @param param a meterological parameter (see `param_meta$bcsd`)
#' @param model GMC model name (see `model_meta$bcsd`)
#' @param scenario a climate scenario pathway (rcp45 or rcp85)
#' @param startDate a start date given as "YYYY-MM-DD"
#' @param endDate an end date given as "YYYY-MM-DD"
#' @author Mike Johnson
#' @return a list of rasterStacks
#' @return if AOI is an areal extent a list of rasterStacks, if AOI is a point then a data.frame of modeled records.
#' @export

getBCSDvic = function(AOI, param, model = 'CCSM4', scenario = 'rcp45', startDate, endDate = NULL){

id = "bcsd"
base = "https://cida.usgs.gov/thredds/dodsC/BCSD_mon_VIC?"

d = define.dates(startDate, endDate, baseDate = '1950-01-15')
p = define.param(param, service = 'bcsd')
k = define.config(dataset = 'bcca', model = model)
g = define.grid3(AOI, id)

fin = expand.grid(model = k, scenario = scenario, call = p$call)
fin = merge(fin, p, "call")

base = "https://cida.usgs.gov/thredds/dodsC/BCSD_mon_VIC?"

g = define.grid2(AOI, url = base)
s = define.initial(g, d)

var.names = paste0(tolower(fin$model), "_", fin$scenario, "_r1i1p1_",fin$call)

for(i in 1:NROW(fin)){
urls = paste0(base, var.names,
"[", min(d$month.index), ":1:" , max(d$month.index), "]",
g$lat.call,
g$lon.call )

nc = ncdf4::nc_open(paste0("https://cida.usgs.gov/thredds/dodsC/BCSD_mon_VIC?",
tolower(fin$model[i]),
"_",
fin$scenario[i],
"_r1i1p1_",
fin$call[i],
"[", min(d$month.index), ":1:" , max(d$month.index), "]",
g$lat.call,
g$lon.call))

var = ncdf4::ncvar_get(nc)
ncdf4::nc_close(nc)
fast.download(urls, params = var.names, names = paste0(fin$model, "_", fin$common.name), g, date.names = unique(format(d$date, "%Y-%m")), dataset = id)

s = process.var(group = s, g = g, var, dates = unique(format(d$date, "%Y-%m")),
param = fin$common.name[i], name = paste0(fin$model[i], "_", fin$scenario[i]))
}

return(s)
}

50 changes: 38 additions & 12 deletions R/getCHIRPS.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
#' @title Get CHIRPS Precipitation Data for an Area of Interest
#' @description Climate Hazards Group InfraRed Precipitation with Station data (CHIRPS) is a 30+ year quasi-global rainfall dataset. Spanning 50°S-50°N (and all longitudes), starting in 1981 to near-present,
#' CHIRPS incorporates 0.05° resolution satellite imagery with in-situ station data to create gridded rainfall time series for trend analysis and seasonal drought monitoring.
#' @param AOI a spatial polygon object (sf or sp)
#' @param startDate a start date given as "YYYY-MM-DD"
#' @param endDate an end date given as "YYYY-MM-DD"
#' @author Mike Johnson
#' @return a list of rasterStacks or dataframe of point values
#' @export

getCHIRPS = function(AOI, startDate, endDate = NULL ){

d = define.dates (startDate, endDate)

bb = AOI::bbox_st(AOI)

dates = paste0("%28", as.numeric(format(d$date, "%d")), "%20", month.abb[as.numeric(format(d$date, "%m"))], "%20", format(d$date, "%Y"))
g = define.grid3(AOI, 'chirps')

bb = g$AOI %>% AOI::bbox_st()

urls = paste0('https://iridl.ldeo.columbia.edu/SOURCES/.UCSB/.CHIRPS/.v2p0/.daily-improved/.global/.0p05/.prcp/',
'Y/', bb$ymin, '/', bb$ymax,'/RANGEEDGES/',
'X/',bb$xmin, '/', bb$xmax,'/RANGEEDGES/',
'T/',dates,'%29VALUES/',
'%5BX/Y/%5D/palettecolor.tiff?filename=tmp.tiff')
'Y/', bb$ymin, '/', bb$ymax,'/RANGEEDGES/',
'X/',bb$xmin, '/', bb$xmax,'/RANGEEDGES/',
'T/',dates,'%29VALUES/%5BX/Y/%5D/palettecolor.tiff?filename=tmp.tiff')

`%dopar%` <- foreach::`%dopar%`
no_cores <- parallel::detectCores() - 1
doParallel::registerDoParallel(no_cores)

if(any(sf::st_geometry_type(g$AOI) == 'POINT')) {

s = raster::stack()
var = foreach::foreach(i = 1:length(urls), .combine = 'c', .packages = 'raster') %dopar% { raster::raster(urls[i]) %>% as.matrix()}

for(i in 1:length(urls)){
r = raster::raster(urls[i])
names(r) = paste0("prcp_", d$string[i])
s = raster::addLayer(s, r )
l = length(d$date)
df = data.frame(source = rep('chirps', l), lat = rep(bb$ymax , l), lon = rep(bb$xmax, l), date = d$date, stringsAsFactors = FALSE)

mat = matrix(var, nrow = l, byrow = F)
s = cbind(df, mat)
colnames(s) = c('source', 'lat', 'lon', 'date', 'prcp')

} else {

s = foreach::foreach(i = 1:length(urls), .combine = 'stack', .packages = 'raster') %dopar% { raster::raster(urls[i])}
names(s) = paste0("prcp_", d$string)
raster::crs(s) = sf::st_crs(g$AOI)[[2]]
}

return(s)
}


57 changes: 27 additions & 30 deletions R/getDaymet.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
#' @title Get Daymet Climate Data for an Area of Interest
#' @description This dataset provides Daymet Version 3 model output data as gridded estimates of daily weather parameters for North America.
#' Daymet output variables include the following parameters: minimum temperature, maximum temperature, precipitation, shortwave radiation,
#' vapor pressure, snow water equivalent, and day length. The dataset covers the period from January 1, 1980 to December 31 of the most recent full calendar year. Each subsequent year is processed individually at the close of a calendar year after
#' allowing adequate time for input weather station data to be of archive quality. Daymet variables are continuous surfaces provided as individual files, by year, at a 1-km x 1-km spatial resolution and a daily temporal resolution. Data are in a Lambert Conformal
#' Conic projection for North America and are in a netCDF file format compliant with Climate and Forecast (CF) metadata conventions.
#' @param AOI a spatial polygon object (sf or sp)
#' @param param a meterological parameter (see `param_meta$daymet`)
#' @param startDate a start date given as "YYYY-MM-DD"
#' @param endDate an end date given as "YYYY-MM-DD"
#' @author Mike Johnson
#' @return if AOI is an areal extent a list of rasterStacks, if AOI is a point then a data.frame of modeled records.
#' @export
#'
getDaymet = function(AOI, param, startDate, endDate = NULL){

daymet.proj = "+proj=lcc +lon_0=-100 +lat_0=42.5 +x_0=0 +y_0=0 +a=6378137 +rf=298.257223563 +lat_1=25 +lat_2=60 +datum=WGS84"
id = 'daymet'
base = 'https://thredds.daac.ornl.gov/thredds/dodsC/ornldaac/1328/'

d = define.dates(startDate, endDate)
g = define.grid(AOI, service = 'daymet', proj = daymet.proj)
p = define.param(param, service = 'daymet')
s = define.initial(grid = g, date = d)
d = define.dates(startDate, endDate, baseDate = "1980-01-01")
d = d[d$julien < 366, ]
p = define.param(param, id)
g = define.grid3(AOI, id)

y = data.frame(year = unique(d$year), minJul = NA, maxJul = NA, min.date = as.Date(NA), max.date = as.Date(NA), stringsAsFactors = FALSE)

Expand All @@ -20,33 +35,15 @@ getDaymet = function(AOI, param, startDate, endDate = NULL){
tmp = expand.grid(year = y$year, call = p$call, stringsAsFactors = FALSE)
fin = merge(tmp, p, "call") %>% merge(y, "year")

for(i in 1:NROW(fin)){
urls = paste0(base, fin$year, '/daymet_v3_', fin$call, '_', fin$year, '_na.nc4?', fin$call,
'[', fin$minJul - 1 , ':1:', fin$maxJul - 1, ']',
g$lat.call,
g$lon.call)

nc = ncdf4::nc_open(paste0('https://thredds.daac.ornl.gov/thredds/dodsC/ornldaac/1328/',
fin$year[i],
'/daymet_v3_',
fin$call[i],
'_',
fin$year[i],
'_na.nc4?',
fin$call[i],
'[', fin$minJul[i] -1 , ':1:', fin$maxJul[i] -1, ']',
g$lat.call,
g$lon.call ))
s = fast.download(urls, params = fin$call, names = fin$call, g, date.names = d$date, dataset = id, fun = 'r')

var = ncdf4::ncvar_get(nc)
s

ncdf4::nc_close(nc)

s = process.var(group = s, g = g, var, dates = seq.Date(as.Date(fin$min.date[i]), as.Date(fin$max.date[i]), 1),
param = fin$common.name[i], name = fin$year[i], proj = daymet.proj, fun = 't')
}

ss = define.initial(grid = g, date = d)

for(i in 1:NROW(p)){ ss[[p$common.name[i]]] = raster::stack(s[grepl(p$common.name[i], names(s))]) }

return(ss)
return(s)
}


Loading

0 comments on commit 53ddede

Please sign in to comment.