diff --git a/Main.R b/Main.R index fcd6362ddd33652947d5aab25f8e27ad0e92e5db..a5f9614c4e86533614c50b30139a4e1254dcdf54 100644 --- a/Main.R +++ b/Main.R @@ -24,12 +24,6 @@ inputFolder <- paste(path, "input_data", sep = "/") outputFolder <- paste(path, "output", sep = "/") -### load some parameter -# Management regimes and their abbreviation, they are merged to the data by the branching group (script loadDB.R) -regime <- read.csv(paste0(path, "/params/regimes.csv"), - sep = ",", - stringsAsFactors = FALSE) - ### Simulation variant that will be load ### The corresponding db files need to be stored in folder "input_data" @@ -65,36 +59,24 @@ regime <- read.csv(paste0(path, "/params/regimes.csv"), # Select the variation +# !!! no needed if query based on the file name #sim_variant <- "CC0_p_canesm" -sim_variant <- "without" +#sim_variant <- "without" # Vector to keep all variants for future use: -all.variants <- c("CC45", - "CC85", - "without", - "CC45_p", - "CC85_p", - "CC26_p", - "CC0_p", - "CC45_p_canesm", - "CC85_p_canesm", - "CC26_p_canesm", - "CC0_p_canesm") - - -### Define the names of the databases (SIMO-output for 10 watersheds) that will be imported -# It is used in the skripts "structure_SIMO_rslDB_FBE.R" and "loadDB.R" -#db_names <- c(#"MV_Hartola", - #"MV_Kitee", - # "MV_Korsnas" #, - #"MV_Parikkala", - #"MV_Pori", - #"MV_Pyhtaa", - #"MV_Raasepori", - #"MV_Simo", - #"MV_Vaala", - #"MV_Voyri" - # ) +#all.variants <- c("CC45", + # "CC85", + # "without", + # "CC45_p", + # "CC85_p", + # "CC26_p", + # "CC0_p", + # "CC45_p_canesm", + # "CC85_p_canesm", + # "CC26_p_canesm", + # "CC0_p_canesm") + + # List all .db in 'input' db_input <- list.files(inputFolder, @@ -105,46 +87,12 @@ db_input <- list.files(inputFolder, db_names <- gsub(".db", "", db_input) -### Restructure the SQL database. -# The query creates a table called UNIT, which contains indicators over time and under management regimes -# -# !!! Only needed if the DB is loaded for the first time (this may take some time): "first_load = TRUE" -# !!! For the downloaded watershed data this is already done: "first_load = FALSE" - - -first_load = FALSE -if (sim_variant %in% all.variants ){ - print("YES") -} else { - print("NO") -} - -### the following lines do not need any changes ### - - -# If one of the follwing simulation variants is read ... -if(sim_variant %in% all.variants ) { - print("sim_varian is in all variants") - - # Run the script with the SQL query for all management regimes - source(paste0(path, "/structure_SIMO_rslDB_FBE.R")) - - } else { - print("!!!else") - # Otherwise, run the script witht the SQL query for Set aside without deadwood extraction (...SA) - # This needs a different SQL query, since the database does not contain harvest information - source(paste0(path, "/structure_SIMO_rslDB_SA.R")) # Query - -} - - -### - - -### Import the restructured SIMO data (from UNIT table) in the R-environment -# It gives a single dataframe for each database named by "rsl_db_names.csv" AND an overall dataframe called "rslt_all.csv" -# Select columns that should be importat from the SQL table UNIT (created before by script structure_SIMO_rslDB) +# Identify columns from UNIT table to export to .cvs +# ----------------------------------------- +# different columns are stored in stratum or in comp_unit +# if more variables are required, need to update the SQL +# created script 'structure_SIMO_rslDB.R' # An overview on all available SIMO outcomes can be found under: params/Overview_outcomes_SIMO.xlsx columns <- paste0("id, @@ -158,21 +106,91 @@ columns <- paste0("id, V_total_deadwood, BA, V, - N, SC, + N, H_dom, D_gm, Harvested_V, Biomass, income_biomass, - CARBON_STORAGE") + CARBON_STORAGE, + MAIN_SP") + + + +# If the _SA is included in the .db name +# use specific query -# Create the final CSV -# ------------------------- +# CHeck for matching character string +# !!!!!! this needs to be fixed! +# what is the correct order of teh queries? +# need to include OPER2 & OPER3 in SA scenario??? +# error: op_res.income - in SA and NO SA scenarios + +# TRy to add THIN value to SA_scenario??? +# NEED to fix final export of csv tables: simplify the output name!!! +# remove the 'simulated_' + +# +for (name in db_names) { + print("Back in main!!") + # CHeck if the name contains "_SA" regime + # if YES - run SA query + # if NO - run normal query + + + if(grepl("_SA", name)){ + + print(name) + print("run SA query") + # This needs specific SQL, since the database does not contain harvest information + source(paste0(path, "/structure_SIMO_rslDB_SA.R")) + + } else { + print(name) + print("run FBE query") + + # Run the script with the SQL query for all management regimes besides SA + source(paste0(path, "/structure_SIMO_rslDB_FBE.R")) # Query + + } + } + + + + +### +# Create the final CSV for each database +# ---------------------------------- source(paste(path, "loadDB.R", sep = "/")) + + + +# If one of the follwing simulation variants is read ... +# if(sim_variant %in% all.variants ) { +# print("sim_varian is in all variants") +# +# # Run the script with the SQL query for all management regimes +# source(paste0(path, "/structure_SIMO_rslDB_FBE.R")) +# +# +# } else { + +# # Otherwise, run the script witht the SQL query for Set aside without deadwood extraction (...SA) +# # This needs a different SQL query, since the database does not contain harvest information +# source(paste0(path, "/structure_SIMO_rslDB_SA.R")) # Query +# } + + + + + + + + ### !!! CSV files are stored unter output # # If data/columns have already been loaded and saved as csv file under ../output: csv_exist = TRUE