Skip to content

Commit

Permalink
generate new netCDFs for FRESC w/ updated GISSM file format
Browse files Browse the repository at this point in the history
  • Loading branch information
aestears committed Sep 9, 2024
1 parent f7d7d48 commit fda6c84
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 50 deletions.
1 change: 1 addition & 0 deletions functions/weatherFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
155 changes: 106 additions & 49 deletions main/implementation/01.2_input-values-into-ncdfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion projects/07_TestOutputForFRESC/GenerateOutputForFRESC.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 !!! ---
################ -------------------------------------------------------------
Expand Down

0 comments on commit fda6c84

Please sign in to comment.