Skip to content

Commit

Permalink
Clean up and comp function
Browse files Browse the repository at this point in the history
cleaning up

fixed set_comp function
  • Loading branch information
CaitlinA committed Sep 15, 2020
1 parent df551b6 commit a4b3e50
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.RData
.Ruserdata
*.Rproj
Tests/
8 changes: 4 additions & 4 deletions EC2_Ubuntu18_withUserData.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Resources:
R CMD INSTALL rSW2funs

# clone and install App Code
git clone -b master --single-branch https://code.chs.usgs.gov/candrews/shorttermdroughtforecaster Git/shorttermdroughtforecaster
git clone -b master --single-branch https://code.chs.usgs.gov/candrews/shorttermdroughtforecaster shorttermdroughtforecaster

# copy awslog conf file, get latest awslog agents, enable cloud watch agents, setup to start on boot
#cp /srv/shiny-server/shorttermdroughtforecaster/awslogs.conf /etc/awslogs/awslogs.conf
Expand All @@ -308,9 +308,9 @@ Resources:
#download Data
mkdir WeatherData
cd WeatherData
wget -r -l1 -np "https://www.northwestknowledge.net/metdata/data" -P /tmp -A "tmmn_*.nc"
wget -r -l1 -np "https://www.northwestknowledge.net/metdata/data" -P /tmp -A "tmmx_*.nc"
wget -r -l1 -np "https://www.northwestknowledge.net/metdata/data" -P /tmp -A "pr_*.nc"
wget -r -l1 -np "https://www.northwestknowledge.net/metdata/data" -P . -A "tmmn_*.nc"
wget -r -l1 -np "https://www.northwestknowledge.net/metdata/data" -P . -A "tmmx_*.nc"
wget -r -l1 -np "https://www.northwestknowledge.net/metdata/data" -P . -A "pr_*.nc"

#aws cli
#apt-get install -y awscli
Expand Down
24 changes: 12 additions & 12 deletions ExampleData/MonthlyAnoms.csv
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
tempAnom,pptAnom_cm,pptAnom_CF,Month
0.930147747186856,-1.02741771341792,0.648922885255099,1
1.14548814253226,-0.870647511247415,0.704537916085275,2
1.19610459786915,-0.984816996300885,0.597703220906159,3
1.46072105276534,-0.67682469956043,0.694722621873235,4
1.52342896249705,-1.04960672221113,0.52514340021219,5
0.795269935580533,-0.726461316291051,0.723118227128785,6
0.689959764330289,-0.686667456965177,0.733365915334641,7
0.706964431329182,-0.663971915575513,0.843619661541259,8
1.31760242451834,-1.7681461569356,0.429409398174906,9
1.48694188870211,-0.972341066588402,0.683397332938414,10
1.21103866328775,-0.955614437273997,0.678914712218405,11
1.27926615887471,-0.774644443118197,0.728065834861558,12
0.582996169744745,0.644660249471394,1.23075900781276,1
0.541017331916754,0.340430844900537,1.12619569561241,2
0.605373111796618,-0.300464397085692,0.896332231514933,3
0.682857908934362,-0.295469918429199,0.891611575347178,4
0.963020209986446,-0.30238478569866,0.835864232007793,5
0.614792064415242,-0.471673392341875,0.753647392492505,6
0.768035887764636,-0.869940345449937,0.6651862226512,7
0.65380752961413,-0.961587613318233,0.713101229315985,8
0.898030103154603,-0.810544449181398,0.81080232655317,9
0.659658063896513,0.55298068219911,1.17844994262266,10
0.583235868039739,0.584210779711027,1.19836891633916,11
0.650446233236396,0.883951934595386,1.30922449879114,12
19 changes: 19 additions & 0 deletions STDF_Plumber.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# web interface
library(plumber) #https://www.rplumber.io

library(rSOILWAT2)
library(rSW2funs)
library(splines)
# data formatting
library(data.table)
library(lubridate)
library(raster)
library(zoo)
library(caTools)
# app
library(rvest)

source('functions/weatherFunctions.R')
source('functions/soilsAndComp.R')
source('functions/Outputs.R')
source('functions/HelperFunctions.R')
source('functions/ecoIndicators.R')

r <- plumb('functions/simulationFunctions.R')

r$run(host = "0.0.0.0",port = 8080) # https://www.r-bloggers.com/hosting-a-plumber-api-on-aws/
Expand Down
88 changes: 39 additions & 49 deletions functions/simulationFunctions.R
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
library(rSOILWAT2)
library(rSW2funs)
library(splines)
#library(rSOILWAT2)
#library(rSW2funs)
#library(splines)
# data formatting
library(data.table)
library(lubridate)
library(raster)
library(zoo)
library(caTools)
#library(data.table)
#library(lubridate)
#library(raster)
#library(zoo)
#library(caTools)
# app
library(plumber)
library(rvest)
#library(plumber)
#library(rvest)

#source('functions/weatherFunctions.R')
source('functions/soilsAndComp.R')
source('functions/Outputs.R')
source('functions/HelperFunctions.R')
source('functions/ecoIndicators.R')
#source('functions/soilsAndComp.R')
#source('functions/Outputs.R')
#source('functions/HelperFunctions.R')
#source('functions/ecoIndicators.R')

#
# lat <- 35.1983
# lng <- -111.6513
# soils <- 2
# sand <- 50
# clay <- 15
# comp <- 2
# grasses <- .5
# shrubs <- .5
# trees <- 0
# forbs <- 0
# bg <- 0
lat <- 35.1983
lng <- -111.6513
soils <- 2
sand <- 50
clay <- 15


#* gather user data, execute soilwat simulations, and return outputs
#* @param lat latitude of site
#* @param long longitude of site
#* @param lng longitude of site
#* @param soils binary decision of user 1 is extract from a gridded source 2 is user input
#* @param sand user input of sand texture
#* @param clay user input of clay texture

#* @get /gatherDataAndExecuteSW
gatherDataAndExecuteSW <- function(lat, lng,
soils, sand = 33, clay = 33, comp = 1) {
soils, sand = 33, clay = 33) {

################### ----------------------------------------------------------------
# Part 0 - Setup - format data from HTTP request, get dates
Expand All @@ -59,16 +54,14 @@ source('functions/ecoIndicators.R')
################### ----------------------------------------------------------------
# Part 1 - Getting and formatting weather data for the historical and future runs
################### ----------------------------------------------------------------

# get historical weather data -> using geoknife ... this takes very long and needs to be changed

print(Sys.time())

print(paste('Formatting Weather Data', Sys.time()))
wdata <- getWeatherData(lat, lng, currYear,
dir = '~/Desktop/www.northwestknowledge.net/metdata/data/')
print(Sys.time())
# write.csv(wdata2, 'ExampleData/wdata.csv', row.names = FALSE)
#wdata <- fread('ExampleData/wdata.csv')


weath <- rSOILWAT2::dbW_dataframe_to_weatherData(wdata[wdata$Year %in% c(1979:(currYear - 1)),
c('Year', 'DOY', 'Tmax_C', 'Tmin_C', 'PPT_cm')], round = 4)
################### ----------------------------------------------------------------
# Part 2 - Sets soils and veg and lat
################### ----------------------------------------------------------------
Expand All @@ -77,8 +70,8 @@ source('functions/ecoIndicators.R')
# set whether soils should be extracted from 250m data or chosen by user
sw_in0 <- set_soils(sw_in0, soils, sand, clay)

#3 set whether composition should be predicted from climate or chosen by user
sw_in0 <- set_comp(sw_in0, comp, trees, shrubs, grasses, forbs, bg)
#3 predict composition from climate
sw_in0 <- set_comp(sw_in0, weath)

# 4 set latitude. Used in GISSM calculations
swSite_IntrinsicSiteParams(sw_in0)[["Latitude"]] <- lat * pi/180
Expand All @@ -96,23 +89,20 @@ source('functions/ecoIndicators.R')
Soils$width <- diff(c(0, Soils$depth_cm))
SoilsDF <- merge(Soils, SoilsDF, by = 'depth_cm')
SoilsDF$variable <- paste0('Lyr_',1:dim(SoilsDF)[1])
# write.csv(SoilsDF, 'Tests/TestExample/SoilsDF.csv')

# --------------------------------------------------------------------------
# Run 1 - with observed historical data ------------------------------------
# --------------------------------------------------------------------------
print('Running Historical')
print(paste('Running Historical', Sys.time()))
swCarbon_Use_Bio(sw_in0) <- FALSE
swCarbon_Use_WUE(sw_in0) <- FALSE
swYears_EndYear(sw_in0) <- currYear - 1

weath <- dbW_dataframe_to_weatherData(wdata[wdata$Year %in% c(1979:(currYear - 1)),
c('Year', 'DOY', 'Tmax_C', 'Tmin_C', 'PPT_cm')], round = 4)
sw_out0 <- sw_exec(inputData = sw_in0, weatherList = weath, quiet = FALSE)
sw_out0 <- sw_exec(inputData = sw_in0, weatherList = weath, quiet = TRUE)
HistDataAll <- getOutputs(sw_out0, sw_in0)

# format outputs
HistDataAll1 <- setorder(HistDataAll[[1]], Year, Day)
#fwrite(HistDataAll1, 'Tests/TestExample/HistData.csv')

HistDataAll1 <- getRolling(HistDataAll1)

Expand All @@ -136,15 +126,14 @@ source('functions/ecoIndicators.R')
# --------------------------------------------------------------------------
# Run 2 - with future anomaly data
# --------------------------------------------------------------------------
print('Running Future')
print(Sys.time())
AnomalyData1 <- runFutureSWwithAnomalies(lat, lng, sw_in0, wdata, res2, n = 1, SoilsDF,
print(paste('Running Future', Sys.time()))
AnomalyData1 <- runFutureSWwithAnomalies(lat, lng, sw_in0, wdata, res2, n = 30, SoilsDF,
currDOY, currMonth, currYear)

AllOut <- AnomalyData1[[1]]
#fwrite(AllOut, 'Tests/TestExample/FutureData.csv')
MonthlyAnoms <- AnomalyData1[[3]]

MonthlyAnoms <- AnomalyData1[[4]]

print(paste('Formatting Outputs', Sys.time()))
AnomRunStats <- formatOutputsFuture(AllOut, SoilsDF)

# eco vars ------------------------------------------------------------------
Expand All @@ -168,6 +157,7 @@ source('functions/ecoIndicators.R')
# fwrite(Future_Shriver2018, 'ExampleData/Future_Shriver2018.csv')
# #fwrite(Hist_GISSM, 'ExampleData/Hist_GISSM.csv')
# #fwrite(Future_GISSM, 'ExampleData/Future_GISSM.csv')
print(paste('Done', Sys.time()))
return(list(HistData_Norm_Stats, AnomRunStats, Shriver_Stats, GISSM_Stats))#, HistDataAll1))

}
43 changes: 14 additions & 29 deletions functions/soilsAndComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,21 @@ set_soils <- function(sw_in, soils, sand, clay){

}

set_comp <- function(sw_in, comp, trees, shrubs, grasses, forbs, bg){

if(comp == 2){
sw_in@prod@Composition[1] <- grasses
sw_in@prod@Composition[2] <- shrubs
sw_in@prod@Composition[3] <- trees
sw_in@prod@Composition[4] <- forbs
sw_in@prod@Composition[5] <- bg
}
if(comp == 1){
# MAP_mm <- SiteClimate_Scenario$MAP_cm*10
# MAT_C <- SiteClimate_Scenario$MAT_C
# monthly.ppt <- SiteClimate_Scenario$meanMonthlyPPTcm*10
# monthly.temp <- SiteClimate_Scenario$meanMonthlyTempC
# dailyC4vars <- SiteClimate_Scenario$dailyC4vars
# rSFSW2::estimate_PotNatVeg_composition(MAP_mm, MAT_C,
# mean_monthly_ppt_mm = monthly.ppt, dailyC4vars)
#
# rSFSW2::estimate_PotNatVeg_biomass( tr_VegBiom = tr_VegetationComposition,
# do_adjBiom_by_temp = any(create_treatments == "AdjMonthlyBioMass_Temperature") && i_sw_input_treatments$AdjMonthlyBioMass_Temperature,
# do_adjBiom_by_ppt = any(create_treatments == "AdjMonthlyBioMass_Precipitation") & i_sw_input_treatments$AdjMonthlyBioMass_Precipitation,
# fgrass_c3c4ann = grasses.c3c4ann.fractions[[sc]],
# growing_limit_C = opt_sim[["growseason_Tlimit_C"]],
# isNorth = isNorth, MAP_mm = MAP_mm, mean_monthly_temp_C = monthly.temp)
#
# rSOILWAT2::swProd_MonProd_grass(swRunScenariosData[[sc]])[, 1:3] <- temp$grass[, 1:3]
# rSOILWAT2::swProd_MonProd_shrub(swRunScenariosData[[sc]])[, 1:3] <- temp$shrub[, 1:3]

}
set_comp <- function(sw_in, weath){

clim1 <- calc_SiteClimate(weatherList = weath, do_C4vars = TRUE)

comp <- rSOILWAT2::estimate_PotNatVeg_composition(
MAP_mm = 10 * clim1[["MAP_cm"]], MAT_C = clim1[["MAT_C"]],
mean_monthly_ppt_mm = 10 * clim1[["meanMonthlyPPTcm"]],
mean_monthly_Temp_C = clim1[["meanMonthlyTempC"]]
)

swProd_Composition(sw_in) <- c(comp$Rel_Abundance_L1[4],
comp$Rel_Abundance_L1[2],
comp$Rel_Abundance_L1[1],
comp$Rel_Abundance_L1[3],
comp$Rel_Abundance_L1[5])
return(sw_in)

}
Expand Down
11 changes: 5 additions & 6 deletions functions/weatherFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ runFutureSWwithAnomalies <- function(lat, lng, sw_in0, wdata, res2, n, SoilsDF,
wdata$Date <- NULL

# Aggregate to monthly values
monthlyWdata <- wdata[,.(Tmean_C = mean(Tmean_C), PPT_cm = sum(PPT_cm)), .(Month, Year)]
monthlyWdata <- setDT(wdata)[,.(Tmean_C = mean(Tmean_C), PPT_cm = sum(PPT_cm)), .(Month, Year)]

# Convert monthly PPT to inches
monthlyWdata$PPT_in <- monthlyWdata$PPT_cm / 2.54
Expand Down Expand Up @@ -220,8 +220,7 @@ runFutureSWwithAnomalies <- function(lat, lng, sw_in0, wdata, res2, n, SoilsDF,

MonthlyAnoms <- rbind(MonthlyAnoms, yearlydat)
#}
fwrite(MonthlyAnoms, 'ExampleData/MonthlyAnoms.csv')

#fwrite(MonthlyAnoms, 'ExampleData/MonthlyAnoms.csv')

# Step 4 ----------------------------------------------------------------------------------------------
# Create future weather / integrate anomaly data into historical weather ----------------------------------------------------------
Expand Down Expand Up @@ -255,16 +254,16 @@ runFutureSWwithAnomalies <- function(lat, lng, sw_in0, wdata, res2, n, SoilsDF,
sw_out <- sw_exec(inputData = sw_in0, weatherList = weathAnomOneSim, quiet = TRUE)

# Grab Data I want for this run ----------------------------------------------------------
Out1 <- getOutputs(sw_out, sw_in0, calc_GISSM = FALSE)
Out1 <- getOutputs(sw_out, sw_in0, calc_GISSM = TRUE)

AllOut1 <- rbind(AllOut1, cbind(Out1[[1]], run = paste(nn, y, sep = '_')))
Shriver_Out <- rbind(Shriver_Out, cbind(Out1[[2]], run = paste(nn, y, sep = '_')))
#GISSM_Out <- rbind(GISSM_Out, cbind(Out1[[3]], run = paste(nn, y, sep = '_')))
GISSM_Out <- rbind(GISSM_Out, cbind(Out1[[3]], run = paste(nn, y, sep = '_')))

}
}

return(list(AllOut1, Shriver_Out, #GISSM_Out,
return(list(AllOut1, Shriver_Out, GISSM_Out,
MonthlyAnoms))

}
Expand Down

0 comments on commit a4b3e50

Please sign in to comment.