Skip to content

Commit

Permalink
function 'sw_dailyC4_TempVar' gains argument 'return_yearly'
Browse files Browse the repository at this point in the history
- if argument ‘return_yearly’ is TRUE, then a matrix is returned where
rows correspond to the simTime$useyrs (with one row having potentially
NAs because of North-South hemisphere adjustment)
- if ‘return_yearly’ is FALSE (default), then the old behavior is
maintained and a vector with the mean and SD of the three variables is
returned


Former-commit-id: c3fb0fd
  • Loading branch information
dschlaep committed Sep 15, 2016
1 parent 8392cd7 commit fa2c40d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
13 changes: 7 additions & 6 deletions 2_SWSF_p4of5_Code_v51.R
Original file line number Diff line number Diff line change
Expand Up @@ -2146,7 +2146,7 @@ do_OneSite <- function(i_sim, i_labels, i_SWRunInformation, i_sw_input_soillayer
if(print.debug) print("Start of get SiteClimate")
do.C4vars <- any(create_treatments == "PotentialNaturalVegetation_CompositionShrubsC3C4_Paruelo1996") || aon$dailyC4_TempVar
#redo SiteClimate_Ambient
SiteClimate_Ambient <- sw_SiteClimate_Ambient(weatherList=i_sw_weatherList[[1]], year.start=min(simTime$useyrs), year.end=max(simTime$useyrs), do.C4vars=do.C4vars, simTime2=simTime2)
SiteClimate_Ambient <- sw_SiteClimate_Ambient(weatherList=i_sw_weatherList[[1]], year.start=min(simTime$useyrs), year.end=max(simTime$useyrs), do.C4vars=do.C4vars, simTime = simTime, simTime2=simTime2)
}
}
if(!getScenarioWeatherDataFromDatabase) {
Expand Down Expand Up @@ -2248,11 +2248,11 @@ do_OneSite <- function(i_sim, i_labels, i_SWRunInformation, i_sw_input_soillayer
if(do.C4vars){
SiteClimate_Scenario$dailyTempMin <- SiteClimate_Ambient$dailyTempMin + t_min_f[simTime2$month_ForEachUsedDay]
SiteClimate_Scenario$dailyTempMean <- SiteClimate_Ambient$dailyTempMean + tmean_f[simTime2$month_ForEachUsedDay]
SiteClimate_Scenario$dailyC4vars <- sw_dailyC4_TempVar(SiteClimate_Scenario$dailyTempMin, SiteClimate_Scenario$dailyTempMean, simTime2)
SiteClimate_Scenario$dailyC4vars <- sw_dailyC4_TempVar(SiteClimate_Scenario$dailyTempMin, SiteClimate_Scenario$dailyTempMean, simTime, simTime2)
}
}
} else {
SiteClimate_Scenario <- sw_SiteClimate_Ambient(weatherList=i_sw_weatherList[[sc]], year.start=min(simTime$useyrs), year.end=max(simTime$useyrs), do.C4vars=do.C4vars, simTime2=simTime2)
SiteClimate_Scenario <- sw_SiteClimate_Ambient(weatherList=i_sw_weatherList[[sc]], year.start=min(simTime$useyrs), year.end=max(simTime$useyrs), do.C4vars=do.C4vars, simTime = simTime, simTime2=simTime2)
if(sc > 1){
ppt_sc <- (temp <- swWeather_MonScalingParams(swRunScenariosData[[sc]]))[,1]
t_max <- temp[,2]
Expand Down Expand Up @@ -3388,14 +3388,15 @@ do_OneSite <- function(i_sim, i_labels, i_SWRunInformation, i_sw_input_soillayer
}
#TODO(drs): progress state
#23
if(any(simulation_timescales=="daily") & aon$dailyC4_TempVar){#Variables to estimate percent C4 species in North America: Teeri JA, Stowe LG (1976) Climatic patterns and the distribution of C4 grasses in North America. Oecologia, 23, 1-12.
if(any(simulation_timescales=="daily") & aon$dailyC4_TempVar){
# Variables to estimate percent C4 species in North America: Teeri JA, Stowe LG (1976) Climatic patterns and the distribution of C4 grasses in North America. Oecologia, 23, 1-12.
if(print.debug) print("Aggregation of dailyC4_TempVar")
if(!exists("temp.dy")) temp.dy <- get_Temp_dy(sc, runData, simTime)

temp <- sw_dailyC4_TempVar(dailyTempMin = temp.dy$min,
dailyTempMean = temp.dy$mean,
simTime2) #accountNSHemispheres_agg
temp <- as.numeric(temp)
simTime, simTime2,
return_yearly = TRUE)

resMeans[nv:(nv+2)] <- temp[1:3]
resSDs[nv:(nv+2)] <- temp[4:6]
Expand Down
36 changes: 27 additions & 9 deletions 2_SWSF_p5of5_Functions_v51.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ adjustLayersDepth <- compiler::cmpfun(function(layers_depth, d) round(layers_dep
getLayersWidth <- compiler::cmpfun(function(layers_depth) diff(c(0, layers_depth)))
setLayerSequence <- compiler::cmpfun(function(d) seq_len(d))

sw_dailyC4_TempVar <- compiler::cmpfun(function(dailyTempMin, dailyTempMean, simTime2) {
sw_dailyC4_TempVar <- compiler::cmpfun(function(dailyTempMin, dailyTempMean, simTime, simTime2, return_yearly = FALSE) {
#Variables to estimate percent C4 species in North America: Teeri JA, Stowe LG (1976) Climatic patterns and the distribution of C4 grasses in North America. Oecologia, 23, 1-12.

temp7 <- simTime2$month_ForEachUsedDay_NSadj == 7
Expand All @@ -420,19 +420,37 @@ sw_dailyC4_TempVar <- compiler::cmpfun(function(dailyTempMin, dailyTempMean, sim
DegreeDaysAbove65F_DaysC <- tapply(temp_base65F, simTime2$year_ForEachUsedDay_NSadj, sum)

nyrs <- seq_along(Month7th_MinTemp_C) #if southern Hemisphere, then 7th month of last year is not included
temp <- cbind(Month7th_MinTemp_C[nyrs],
ydat <- cbind(Month7th_MinTemp_C[nyrs],
LengthFreezeFreeGrowingPeriod_Days[nyrs],
DegreeDaysAbove65F_DaysC[nyrs])
res <- c(apply(temp, 2, mean), apply(temp, 2, sd))
temp <- c("Month7th_NSadj_MinTemp_C",
"LengthFreezeFreeGrowingPeriod_NSadj_Days",
"DegreeDaysAbove65F_NSadj_DaysC")
names(res) <- c(temp, paste0(temp, ".sd"))
dimnames(ydat) <- list(NULL,
c("Month7th_NSadj_MinTemp_C",
"LengthFreezeFreeGrowingPeriod_NSadj_Days",
"DegreeDaysAbove65F_NSadj_DaysC")
)

if (return_yearly) {
yrs_have <- as.integer(names(Month7th_MinTemp_C[nyrs]))

if (all(yrs_have == simTime$useyrs)) {
res <- cbind(Years = yrs_have, ydat)

} else {
res <- matrix(NA, nrow = simTime$no.useyr, ncol = 4,
dimnames = list(NULL, c("Years", colnames(ydat))))
res[, "Years"] <- simTime$useyrs
res[, -1] <- ydat[match(simTime$useyrs, yrs_have), ]
}

} else {
res <- c(apply(ydat, 2, mean), apply(ydat, 2, sd))
names(res) <- c(colnames(ydat), paste0(colnames(ydat), ".sd"))
}

res
})

sw_SiteClimate_Ambient <- compiler::cmpfun(function(weatherList, year.start, year.end, do.C4vars = FALSE, simTime2 = NULL) {
sw_SiteClimate_Ambient <- compiler::cmpfun(function(weatherList, year.start, year.end, do.C4vars = FALSE, simTime = NULL, simTime2 = NULL) {
sw.weather.suffix <- as.numeric(names(weatherList))
itemp <- year.start <= sw.weather.suffix & year.end >= sw.weather.suffix
years <- sw.weather.suffix[itemp]
Expand Down Expand Up @@ -479,7 +497,7 @@ sw_SiteClimate_Ambient <- compiler::cmpfun(function(weatherList, year.start, yea
if (do.C4vars) {
res[["dailyTempMin"]] <- dailyTempMin
res[["dailyTempMean"]] <- dailyTempMean
res[["dailyC4vars"]] <- sw_dailyC4_TempVar(dailyTempMin, dailyTempMean, simTime2)
res[["dailyC4vars"]] <- sw_dailyC4_TempVar(dailyTempMin, dailyTempMean, simTime, simTime2)

} else {
res[["dailyTempMin"]] <- res[["dailyTempMean"]] <- res[["dailyC4vars"]] <- NA
Expand Down

0 comments on commit fa2c40d

Please sign in to comment.