-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase speed with foreach Point level abstaction across functions new exports
- Loading branch information
mikejohnson51
committed
May 7, 2019
1 parent
9413894
commit 53ddede
Showing
67 changed files
with
727 additions
and
2,060 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
inProgress | ||
sampleFiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.Rhistory | ||
.RData | ||
.Ruserdata | ||
sampleFiles | ||
sampleFiles | ||
inProgress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -12,8 +12,11 @@ Depends: | |
AOI | ||
Imports: | ||
areal, | ||
doParallel, | ||
foreach, | ||
httr, | ||
magrittr, | ||
ncdf4, | ||
RNetCDF, | ||
raster, | ||
sf | ||
VignetteBuilder: knitr | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.