Skip to content
Snippets Groups Projects
Main.R 5.24 KiB
#####
#
# Load simulated Watershed data of SIMO and create CSV.files for further analysis
# 2020-02-21
#
#####



library(dplyr)


### Set the working path to this R-project
path <- paste0(getwd(),"/")


### 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"
### specify which one should be read:


# FOR MSC STUDENTS - total watershed have been already simulated:
# variant simulated with WIN version of NON-climate sensitive SIMO version
#                    "without"     no climate change
#                    "without_SA"  no climate change and set aside without deadwood extraction (no other management regimes!)
# 
# variants simulated with WIN version of climate sensitive SIMO version, climate model HadGEM2
#                    "CC45"        climate change with RCP scenraio 4.5 
#                    "CC45_SA"     RCP 4.5 and set aside without deadwood extraction (no other management regimes!)
#                    "CC85"        climate change with RCP scenario 8.5 
#                    "CC85_SA"     RCP 8.5 and set aside without deadwood extraction (no other management regimes!)


# FOR COMPARISON OF CLIMATE MODEL AND SIMO VERSIONS - ONLY 5 stands per watershed so far simulated
# variants simulated with SIMO Linux version on cPouta, indicated by "p"
# with climate model HadGEM2
#                   "CC45_p"      RCP scenario 4.5
#                   "CC85_p"      RCP scenario 8.5
#                   "CC26_p"      NEW: RCP scenario 2.6
#                   "CC0_p        NEW: RCP scenario 0, represents NO CLIMATE CHANGE ?? 
#
# with climate model CanESM2 (like in FutureBioEcon), indicated further by "canesm"
#                   "CC45_p_canesm"      RCP sceanrio 4.5
#                   "CC85_p_canesm"      RCP scenario 8.5
#                   "CC26_p_canesm"      NEW: RCP scenario 2.6
#                   "CC0_p_canesm"       NEW: RCP scenario 0


sim_variant <- "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")

### 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


### the following lines do not need any changes ###
if(first_load == TRUE){

  # If one of the follwing simulation variants is read ...
  if(sim_variant %in% c("CC45", "CC85", "without", "CC45_p", "CC85_p", "CC26_p", "CC0_p", "CC45_p_canesm", "CC85_p_canesm", "CC26_p_canesm", "CC0_p_canesm")) {
    
    # 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 
      
      }

}
###


### 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)
# An overview on all available SIMO outcomes can be found under: params/Overview_outcomes_SIMO.xlsx

columns <-  paste0("id,
                   year,
                   branch,
                   branch_desc,
                   branching_group,
                   Age,
                   area,
                   cash_flow,
                   V_total_deadwood,
                   BA,
                   V,
                   N,
                   H_dom,
                   D_gm,
                   Harvested_V,
                   Biomass,
                   income_biomass,
                   CARBON_STORAGE")


### !!! CSV files are stored unter output
#
# If data/columns have already been loaded and saved as csv file  under ../output:                csv_exist = TRUE
# If data is loaded for the firest time OR "new columns have to be loaded" (takes some time):     csv_exits = FALSE 


csv_exist = TRUE


### the following lines do not need any changes ###
if(csv_exist == FALSE){
  
  # If FALSE run the following script 
  source(paste0(path, "loadDB.R")) 
 
  } else {
  
    # If TRUE read the CSV file that contains all watersheds (GPKGs)
    rslt <- read.csv(paste0(path, "output/rslt_", sim_variant, "_all.csv" ), sep = ";", header = TRUE, stringsAsFactors = FALSE)

    }
###