-
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.
- Loading branch information
mikejohnson51
committed
Aug 22, 2018
0 parents
commit 7d7edae
Showing
66 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Package: climateR | ||
Type: Package | ||
Title: What the Package Does (Title Case) | ||
Version: 0.1.0 | ||
Author: Who wrote it | ||
Maintainer: The package maintainer <[email protected]> | ||
Description: More about what it does (maybe more than one line) | ||
Use four spaces when indenting paragraphs within the Description. | ||
License: What license is it under? | ||
Encoding: UTF-8 | ||
LazyData: true |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 mikejohnson51 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
exportPattern("^[[:alpha:]]+") |
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
climate = list( | ||
|
||
macaV2metdata = list( | ||
PARAM = data.frame( | ||
parameter = c('tasmax','tasmin','rhsmax', | ||
'rhsmin','pr','rsds', | ||
'uas','vas','huss'), | ||
|
||
longname = c( 'Maximum daily temperature near surface', | ||
'Minimum daily temperature near surface', | ||
'Maximum daily relative humidity near surface', | ||
'Minimum daily relative humdity near surface', | ||
'Average daily precipitation amount at surface', | ||
'Average daily downward shortwave radiation at surface', | ||
'Average daily eastward component of wind near surface', | ||
'Average daily northward component of wind near surface', | ||
'Average daily specific humidity near surface'), | ||
|
||
units = c("K", "K", "%", "%", "mm", "W m-2", "m s-1", "m s-1", "kg kg-1"), | ||
|
||
scale_factor = c(0.1, 0.1, 1, 1, 0.1, 1, 0.1, 0.1, 1.0E-5), | ||
stringsAsFactors = F | ||
), | ||
|
||
models = data.frame( | ||
name = c("bcc-csm1-1", "bcc-csm1-1-m", "BNU-ESM", "CanESM2", | ||
"CCSM4", "CNRM-CM5", "CSIRO-Mk3-6-0", "GFDL-ESM2M", | ||
"GFDL-ESM2G", "HadGEM2-ES", "HadGEM2-CC", "inmcm4", | ||
"IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", "MIROC5", | ||
"MIROC-ESM", "MIROC-ESM-CHEM", "MRI-CGCM3", "NorESM1-M" ), | ||
|
||
country = c("China", "China", "China", "Canada", | ||
"USA", "France", "Australia", "USA", | ||
"USA", "United Kingdom", "United Kingdom", "Russia", | ||
"France", "France", "France", "Japan", | ||
"Japan", "Japan", "Japan", "Norway" ), | ||
|
||
agency = c("Beijing Climate Center, China Meteorological Administration", | ||
"Beijing Climate Center, China Meteorological Administration", | ||
"College of Global Change and Earth System Science, Beijing Normal University, China", | ||
"Canadian Centre for Climate Modeling and Analysis", | ||
"National Center of Atmospheric Research, USA", | ||
"National Centre of Meteorological Research, France", | ||
"Commonwealth Scientific and Industrial Research Organization/Queensland Climate Change Centre of Excellence, Australia", | ||
"NOAA Geophysical Fluid Dynamics Laboratory, USA", | ||
"NOAA Geophysical Fluid Dynamics Laboratory, USA", | ||
"Met Office Hadley Center, UK", | ||
"Met Office Hadley Center, UK", | ||
"Institute for Numerical Mathematics, Russia", | ||
"Institut Pierre Simon Laplace, France", | ||
"Institut Pierre Simon Laplace, France", | ||
"Institut Pierre Simon Laplace, France", | ||
"Atmosphere and Ocean Research Institute (The University of Tokyo),\n\t\tNational Institute for Environmental Studies,and Japan Agency for Marine-Earth Science and Technology", | ||
"Japan Agency for Marine-Earth Science and Technology, Atmosphere and Ocean Research Institute (The University of Tokyo), and National Institute for Environmental Studies", | ||
"Japan Agency for Marine-Earth Science and Technology, Atmosphere and Ocean Research Institute (The University of Tokyo), and National Institute for Environmental Studies", | ||
"Meteorological Research Institute, Japan", | ||
"Norwegian Climate Center, Norway"), | ||
|
||
AtmoRes_lonlat = c("2.8 deg x 2.8 deg", "1.12 deg x 1.12 deg", | ||
"2.8 deg x 2.8 deg", "2.8 deg x 2.8 deg", | ||
"1.25 deg x 0.94 deg", "1.4 deg x 1.4 deg", | ||
"1.8 deg x 1.8 deg", "2.5 deg x 2.0 deg", | ||
"2.5 deg x 2.0 deg", "1.88 deg x 1.25 deg", | ||
"1.88 deg x 1.25 deg", "2.0 deg x 1.5 deg", | ||
"3.75 deg x 1.8 deg", "2.5 deg x 1.25 deg", | ||
"2.75 deg x 1.8 deg", "1.4 deg x 1.4 deg", | ||
"2.8 deg x 2.8 deg", "2.8 deg x 2.8 deg", | ||
"1.1 deg x 1.1 deg", "2.5 deg x 1.9 deg"), | ||
|
||
ensemble = c( "r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1", "r6i1p1", | ||
"r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1", | ||
"r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1", | ||
"r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1", "r1i1p1"), | ||
stringsAsFactors = FALSE), | ||
|
||
secenarios = c('rcp45', 'rcp85'), | ||
|
||
metadata = data.frame(res = '4km', | ||
spatialExtent = "Contiguous USA", | ||
dataum = "WGS 84", | ||
startDate = "2006-01-01", | ||
endData = "2099-12-31", | ||
leapDays = 'Yes', | ||
stringsAsFactors = F) | ||
|
||
|
||
), | ||
|
||
|
||
# UofI Data --------------------------------------------------------------- | ||
|
||
|
||
UofIMETDATA = list( | ||
PARAM = data.frame( | ||
param = c('precipitation_amount', | ||
'max_air_temperature', | ||
'min_air_temperature', | ||
'surface_downwelling_shortwave_flux_in_air', | ||
'specific_humidity', | ||
'max_relative_humidity', | ||
'min_relative_humidity', | ||
'wind_speed'), | ||
|
||
description = c('precipitation', | ||
'maximum temperature', | ||
'minimum temperature', | ||
'downward shortwave solar radiation', | ||
'specific humidity', | ||
'maximum relative humidity', | ||
'minimum relative humidity', | ||
'wind speed'), | ||
|
||
unit = c('mm', 'K', 'K', 'W m-2', 'kg/kg', '%', '%', 'm/s'), | ||
|
||
scale_factor = c(0.1, 0.1, 0.1, 0.1, 0.001, 0.1, 0.1, 0.1), stringsAsFactors = TRUE | ||
) | ||
) | ||
) | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#' Get NWM gridded data output | ||
#' | ||
#' Download land output from the HydroShare Thredds server for a list of input files. | ||
#' Parallized version of \code{get_land_data} using \code{doParallel} and \code{foreach}. | ||
#' | ||
#' @param AOI a AOI to subset data (generated with AOI::getAOI()) | ||
#' @param filelist a list of filepaths (generated with getFiles) | ||
#' @param param a channel paramater | ||
#' @param layer for soil and snow land parameters a layer must be declared (defaults to 1) | ||
#' | ||
#' @return a raster stack | ||
#' @export | ||
#' | ||
|
||
getGridded = function(AOI, filelist, param, layer = NULL) { | ||
|
||
i = NULL | ||
|
||
types = c("channel", "land", "forcing", "terrain", "reservoir") | ||
|
||
type = types[sapply(types, grepl, filelist[1])] | ||
|
||
if(type == 'terrain') { param = tolower(param)} else { param = toupper(param) } | ||
|
||
`%dopar%` <- foreach::`%dopar%` | ||
|
||
combine_lists <- function(LL1, LL2) { | ||
r <- raster::stack(LL1$r, LL2$r) | ||
time <- c(LL1$time, LL2$time) | ||
return(list(r = r, time = time)) | ||
} | ||
|
||
layer = defineLayers(param, layer) | ||
|
||
grid = define_AOI_grid(AOI) | ||
|
||
no_cores <- parallel::detectCores() - 1 | ||
doParallel::registerDoParallel(no_cores) | ||
|
||
res <- | ||
foreach::foreach(i = 1:length(filelist) , .combine = combine_lists) %dopar% { | ||
|
||
file = paste0(filelist[i], | ||
"?time[0:1:0],", | ||
param, | ||
"[0:1:0][", | ||
grid$y.min, | ||
":1:", | ||
grid$y.max , | ||
"]", | ||
layer$layer.p, | ||
"[", | ||
grid$x.min, | ||
":1:", | ||
grid$x.max, | ||
"]" | ||
) | ||
|
||
nc = ncdf4::nc_open(file) | ||
vals = ncdf4::ncvar_get(nc, param) | ||
time = ncdf4::ncvar_get(nc, "time") | ||
ncdf4::nc_close(nc) | ||
|
||
mat <- apply(t(vals),2,rev) | ||
r <-raster::raster(mat) | ||
raster::projection(r) = sp::CRS("+proj=lcc +lat_1=30 +lat_2=60 +lat_0=40 +lon_0=-97 +x_0=0 +y_0=0 +a=6370000 +b=6370000 +units=m +no_defs") | ||
raster::extent(r) = c(grid$long.min, grid$long.max, grid$lat.min, grid$lat.max) | ||
raster::res(r) = 1000 | ||
|
||
return(list(r = r, time = time)) | ||
|
||
} | ||
|
||
names(res$r) <- paste0("X.", res$time) | ||
|
||
return(res$r) | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
getMACA = function(AOI, param, model, scenario, sdate, edate = NULL, write.path = "/Users/mikejohnson/Documents/WRF_Hydro/output/"){ | ||
|
||
base = "https://cida.usgs.gov/thredds/dodsC/macav2metdata_daily_future?" | ||
|
||
if(is.null(edate)){edate = sdate} | ||
dates = seq.Date(as.Date(sdate, format = "%Y-%m-%d", tz = 'GMT' ), as.Date(edate, format = "%Y-%m-%d", tz = 'GMT' ) , by = 1) | ||
dates = dates - as.Date("2006-01-01", format = "%Y-%m-%d", tz = 'GMT') | ||
|
||
nc = ncdf4::nc_open(paste0(base, "lon,lat")) | ||
lat = ncdf4::ncvar_get(nc, "lat") | ||
lon = ncdf4::ncvar_get(nc, "lon") | ||
ncdf4::nc_close(nc) | ||
|
||
min.lat = which.min(abs(lat-min(AOI@bbox[2,]))) | ||
max.lat = which.min(abs(lat-max(AOI@bbox[2,]))) | ||
|
||
min.lon = which.min(abs(lon-min(AOI@bbox[1,]))) | ||
max.lon = which.min(abs(lon-max(AOI@bbox[1,]))) | ||
|
||
l = list(base = paste0(param, "_", | ||
model, "_r1i1p1_", | ||
scenario), | ||
|
||
call = paste0("[", dates[1] , ":1:", tail(dates, 1), "]", | ||
"[", min.lat , ":1:", max.lat, "]", | ||
"[", min.lon , ":1:", max.lon, "]") | ||
) | ||
|
||
xx = paste(do.call(paste0,l), collapse = ",") | ||
|
||
xx = paste0( xx , ",time[",dates[1], ":1:", tail(dates, 1), "],lat[", min.lat , ":1:", max.lat, "],", "lon[", min.lon , ":1:", max.lon, "]") | ||
|
||
r = ncdf4::nc_open(paste0(base, xx)) | ||
|
||
return(r) | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
getRaster = function(r){ | ||
xx = names(r$var) | ||
nc = r | ||
|
||
no_cores <- parallel::detectCores() - 1 | ||
doParallel::registerDoParallel(no_cores) | ||
|
||
res <- | ||
foreach::foreach(i = 1:length(xx) , .combine = raster::stack) %dopar% { | ||
|
||
vals = ncdf4::ncvar_get(nc, xx[i]) | ||
|
||
mat <- apply(t(vals),2,rev) | ||
r <-raster::raster(mat) | ||
raster::projection(r) = sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") | ||
raster::extent(r) = c(lon[min.lon], lon[max.lon], lat[min.lat], lat[max.lat]) | ||
names(r) = xx[i] | ||
|
||
return(r) | ||
|
||
} | ||
|
||
} | ||
|
Oops, something went wrong.