Skip to content

Commit

Permalink
fixing issue with micro_terra when timeinterval = 365 and allowing 'e…
Browse files Browse the repository at this point in the history
…levation' to be downloaded in advance and passed as an argument (see example)
  • Loading branch information
mrke committed May 25, 2024
1 parent 5bb8fbb commit f2bae88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 16 additions & 5 deletions R/micro_terra.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@
#' \item 3-113 290, ..., 4000 - irradiance (W/(m2 nm)) at each of 111 wavelengths from 290 to 4000 nm
#' }
#' @examples
#'micro <- micro_terra() # run the model with default location (Madison, Wisconsin) from 2000 to 2015 and settings
#'library(geodata)
#'elevation <- geodata::worldclim_global(var = 'elev', res = 2.5, path=tempdir()) # download DEM in advance (needed for lapse rate correction - automatically downloaded otherwise)
#'micro <- micro_terra(elevation=elevation) # run the model with default location (Madison, Wisconsin) from 2000 to 2015 and settings
#'
#'metout <- as.data.frame(micro$metout) # above ground microclimatic conditions, min shade
#'shadmet <- as.data.frame(micro$shadmet) # above ground microclimatic conditions, max shade
Expand Down Expand Up @@ -329,6 +331,7 @@ micro_terra <- function(
ystart = 2000,
yfinish = 2015,
timeinterval = 12,
elevation = NA,
nyears = yfinish - ystart + 1,
REFL = 0.15,
elev = NA,
Expand Down Expand Up @@ -964,8 +967,16 @@ micro_terra <- function(
global_climate <- terra::rast(paste0(folder, "/global_climate.nc"))
CLIMATE <- t(as.numeric(terra::extract(global_climate, x)))
#ALTT <- as.numeric(CLIMATE[, 1])
library(geodata)
elevation <- geodata::worldclim_global(var = 'elev', res = 2.5, path = '.')
if(class(elevation)[1] == "SpatRaster"){
cat('using elevation DEM provided to function call \n')
}else{
if (!requireNamespace("geodata", quietly = TRUE)) {
stop("package 'geodata' is needed. Please install it.",
call. = FALSE)
}
library(geodata)
elevation <- geodata::worldclim_global(var = 'elev', res = 2.5, path=tempdir())
}
ALTT <- as.numeric(terra::extract(elevation, x))
if(is.na(ALTT)){
ALTT <- 0
Expand Down Expand Up @@ -1250,8 +1261,8 @@ micro_terra <- function(
m<-1
b<-0
for (i in 1:12){ #begin loop through 12 months of year
ndays=daymon2[i]
for (k in 1:ndays){
ndays2=daymon2[i]
for (k in 1:ndays2){
b<-b+1
sort[m,1]<-i
sort[m,2]<-b
Expand Down
4 changes: 3 additions & 1 deletion man/micro_terra.Rd

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

0 comments on commit f2bae88

Please sign in to comment.