diff --git a/functions/weatherFunctions.R b/functions/weatherFunctions.R index 37497a8..d344de2 100644 --- a/functions/weatherFunctions.R +++ b/functions/weatherFunctions.R @@ -152,6 +152,7 @@ runFutureSWwithAnomalies <- function(sw_in0, wdata, SoilsDF, generatedAnomData <- generateAnomalyData(monthlyWdata, TempAnoms, PPTAnoms, leads = seq_len(Nleads), Nleads = Nleads, n = n) + which(is.nan(generatedAnomData), arr.ind = TRUE) #saveRDS(generatedAnomData, 'ExampleData/generatedAnomData') ## in generatedAnomData, the rows are for each lead (12 rows), and the columns (30) are each for a different multivariate sample diff --git a/main/implementation/01.2_input-values-into-ncdfs.R b/main/implementation/01.2_input-values-into-ncdfs.R index 7c65541..488eee2 100644 --- a/main/implementation/01.2_input-values-into-ncdfs.R +++ b/main/implementation/01.2_input-values-into-ncdfs.R @@ -27,60 +27,117 @@ for(n in seq_along(netCDFnames)){ # indicating the count of values to write along each dimension, order is x-y-t) co <- c(1, 1, tdim[n]) st_n <- st - #AES get values for last 180 days? - # get the date for 180 previous from today - if (tdim[n] == 180) { - vals <- AllVarData[1:183,valueName[n]] - } else if (tdim[n] == 549){ - vals <- as.vector(AllVarData[ ,valueName[n]]) - } else if (tdim[n] == 352) { - vals <- as.vector(AllVarData[(nrow(AllVarData)-351):nrow(AllVarData),valueName[n]]) - } else if (tdim[n] == 31) { # for Oconnor variables - if (valueName_short[n] == "oconnor-swp_mean") { - vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) - } - if (valueName_short[n] == "oconnor-swp_95CI_lower") { - vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) - c(Oconnor_Stats[TP == "Forecast",]$SWP_CI95) - } - if (valueName_short[n] == "oconnor-swp_95CI_upper") { - vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) + c(Oconnor_Stats[TP == "Forecast",]$SWP_CI95) - } - if (valueName_short[n] == "oconnor-stemp_mean") { - vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) + + ## add something to deal w/ the fact that there may not be a "deep" soil layer + #value if the soil isn't deep enough in a given location write! + # if there is NOT Deep soils data... + if (sum(SoilsDF$Depth == "Deep") == 0) { + # if the layer in question (this 'n') is a "deep soils layer", then put NA values (-9999) + if (n %in% grep("Deep", valueName) ) { + vals <- rep_len(-9999, length.out = co[3]) + } else if (tdim[n] == 180) { + vals <- AllVarData[1:183,valueName[n]] + } else if (tdim[n] == 549){ + vals <- as.vector(AllVarData[ ,valueName[n]]) + } else if (tdim[n] == 352) { + vals <- as.vector(AllVarData[(nrow(AllVarData)-351):nrow(AllVarData),valueName[n]]) + } else if (tdim[n] == 31) { # for Oconnor variables + if (valueName_short[n] == "oconnor-swp_mean") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) + } + if (valueName_short[n] == "oconnor-swp_95CI_lower") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) - c(Oconnor_Stats[TP == "Forecast",]$SWP_CI95) + } + if (valueName_short[n] == "oconnor-swp_95CI_upper") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) + c(Oconnor_Stats[TP == "Forecast",]$SWP_CI95) + } + if (valueName_short[n] == "oconnor-stemp_mean") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) + } + if (valueName_short[n] == "oconnor-stemp_95CI_lower") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) - c(Oconnor_Stats[TP == "Forecast",]$sTemp_CI95) + } + if (valueName_short[n] == "oconnor-stemp_95CI_upper") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) + c(Oconnor_Stats[TP == "Forecast",]$sTemp_CI95) + } + } + if (valueName_short[n] == "shriver_historical") vals <- c(Shriver_Stats[TP == 'Historical', 'Prob'])[["Prob"]] + if (valueName_short[n] == "shriver_prediction") { + vals <- c(Shriver_Stats[TP != 'Historical', 'Prob'])$Prob + # redefine tdim to have the number of time steps for this run + # the 'count' that will be fed into nc_put, which is a "vector of integers + # indicating the count of values to write along each dimension, order is x-y-z-t) + co <- c(1, 1, 30, tdim[n]) + # ammend the 'st' vecotr, which tells the ncvar_put function where to start writing the data + st_n <- c(st, 1) } - if (valueName_short[n] == "oconnor-stemp_95CI_lower") { - vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) - c(Oconnor_Stats[TP == "Forecast",]$sTemp_CI95) + + if (valueName_short[n] == "GISSM_historical") vals <- as.numeric(c(NA, Hist_GISSM$SeedlingSurvival_1stSeason)) + if (valueName_short[n] == "GISSM_prediction") { + vals <- Future_GISSM$Prob + # redefine tdim to have the number of time steps for this run + # the 'count' that will be fed into nc_put, which is a "vector of integers + # indicating the count of values to write along each dimension, order is x-y-z-t) + co <- c(1, 1, 30, tdim[n]) + # ammend the 'st' vecotr, which tells the ncvar_put function where to start writing the data + st_n <- c(st, 1) } - if (valueName_short[n] == "oconnor-stemp_95CI_upper") { - vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) + c(Oconnor_Stats[TP == "Forecast",]$sTemp_CI95) + } else { + # if there IS Deep soils data + #AES get values for last 180 days? + # get the date for 180 previous from today + if (tdim[n] == 180) { + vals <- AllVarData[1:183,valueName[n]] + } else if (tdim[n] == 549){ + vals <- as.vector(AllVarData[ ,valueName[n]]) + } else if (tdim[n] == 352) { + vals <- as.vector(AllVarData[(nrow(AllVarData)-351):nrow(AllVarData),valueName[n]]) + } else if (tdim[n] == 31) { # for Oconnor variables + if (valueName_short[n] == "oconnor-swp_mean") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) + } + if (valueName_short[n] == "oconnor-swp_95CI_lower") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) - c(Oconnor_Stats[TP == "Forecast",]$SWP_CI95) + } + if (valueName_short[n] == "oconnor-swp_95CI_upper") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$SWP_mean) + c(Oconnor_Stats[TP == "Forecast",]$SWP_CI95) + } + if (valueName_short[n] == "oconnor-stemp_mean") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) + } + if (valueName_short[n] == "oconnor-stemp_95CI_lower") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) - c(Oconnor_Stats[TP == "Forecast",]$sTemp_CI95) + } + if (valueName_short[n] == "oconnor-stemp_95CI_upper") { + vals <- c(Oconnor_Stats[TP == "Forecast",]$sTemp_mean) + c(Oconnor_Stats[TP == "Forecast",]$sTemp_CI95) + } + } + if (valueName_short[n] == "shriver_historical") vals <- c(Shriver_Stats[TP == 'Historical', 'Prob'])[["Prob"]] + if (valueName_short[n] == "shriver_prediction") { + vals <- c(Shriver_Stats[TP != 'Historical', 'Prob'])$Prob + # redefine tdim to have the number of time steps for this run + # the 'count' that will be fed into nc_put, which is a "vector of integers + # indicating the count of values to write along each dimension, order is x-y-z-t) + co <- c(1, 1, 30, tdim[n]) + # ammend the 'st' vecotr, which tells the ncvar_put function where to start writing the data + st_n <- c(st, 1) } - } - if (valueName_short[n] == "shriver_historical") vals <- c(Shriver_Stats[TP == 'Historical', 'Prob'])[["Prob"]] - if (valueName_short[n] == "shriver_prediction") { - vals <- c(Shriver_Stats[TP != 'Historical', 'Prob'])$Prob - # redefine tdim to have the number of time steps for this run - # the 'count' that will be fed into nc_put, which is a "vector of integers - # indicating the count of values to write along each dimension, order is x-y-z-t) - co <- c(1, 1, 30, tdim[n]) - # ammend the 'st' vecotr, which tells the ncvar_put function where to start writing the data - st_n <- c(st, 1) + + if (valueName_short[n] == "GISSM_historical") vals <- as.numeric(c(NA, Hist_GISSM$SeedlingSurvival_1stSeason)) + if (valueName_short[n] == "GISSM_prediction") { + vals <- Future_GISSM$Prob + # redefine tdim to have the number of time steps for this run + # the 'count' that will be fed into nc_put, which is a "vector of integers + # indicating the count of values to write along each dimension, order is x-y-z-t) + co <- c(1, 1, 30, tdim[n]) + # ammend the 'st' vector, which tells the ncvar_put function where to start writing the data + st_n <- c(st, 1) } - - if (valueName_short[n] == "GISSM_historical") vals <- as.numeric(c(NA, Hist_GISSM$SeedlingSurvival_1stSeason)) - if (valueName_short[n] == "GISSM_prediction") { - vals <- Future_GISSM$Prob - # redefine tdim to have the number of time steps for this run - # the 'count' that will be fed into nc_put, which is a "vector of integers - # indicating the count of values to write along each dimension, order is x-y-z-t) - co <- c(1, 1, 30, tdim[n]) - # ammend the 'st' vecotr, which tells the ncvar_put function where to start writing the data - st_n <- c(st, 1) - } - ## add something to deal w/ the fact that there may not be a "deep" soil layer - #value if the soil isn't deep enough in a given location write! - if (n %in% grep("Deep", valueName)) { - vals <- rep_len(-9999, length.out = co[3]) } + + + + RNetCDF::var.put.nc(ncfile = get(netCDFnames[n]), variable = varName[n], data = vals, diff --git a/projects/07_TestOutputForFRESC/GenerateOutputForFRESC.R b/projects/07_TestOutputForFRESC/GenerateOutputForFRESC.R index 1e0d4ae..11c154d 100644 --- a/projects/07_TestOutputForFRESC/GenerateOutputForFRESC.R +++ b/projects/07_TestOutputForFRESC/GenerateOutputForFRESC.R @@ -324,12 +324,13 @@ for (j in 1:alljid){#1:alljid) { # TO DO: use "while" not "for" PPTAnoms <- PPTAnoms[1:Nleads,] # function in "weatherFunctions.R" - AnomalyData1 <- runFutureSWwithAnomalies(sw_in0 = sw_in, wdata, SoilsDF, +AnomalyData1 <- runFutureSWwithAnomalies(sw_in0 = sw_in, wdata, SoilsDF, TempAnoms, PPTAnoms, Nleads, n = nRuns, currDOY, currMonth, currYear, currDate) + ################ ------------------------------------------------------------- # FORMAT OUTPUTS --- Get 18 month median, 10, and 90 for all !!! --- ################ -------------------------------------------------------------