diff --git a/structure_SIMO_rslDB_FBE.R b/structure_SIMO_rslDB_FBE.R index 013ceabee3f5c69e386b29eba047be0b3323c5c3..1168d31029c24b04daf004993b0ee30f71481724 100644 --- a/structure_SIMO_rslDB_FBE.R +++ b/structure_SIMO_rslDB_FBE.R @@ -41,7 +41,7 @@ create_table_OPERS2 <- 'CREATE TABLE OPERS2 AS SELECT t.id, t.branch, t.op_date, ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =1 and (op_res.sp = 3 or op_res.sp = 4)),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_log_birch, ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =2 and (op_res.sp = 3 or op_res.sp = 4)),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_pulp_birch, ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and (op_res.assortment =2 or op_res.assortment =1) and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_others, -case when t.op_name = "thinning" OR t.op_name = "first_thinning" then t.op_date else 0 end as THIN, + case when t.op_name = "thinning" OR t.op_name = "first_thinning" then t.op_date else 0 end as THIN, ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =1 and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_log_others, ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =2 and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_pulp_others, @@ -74,7 +74,7 @@ create_table_OPERS3 <- 'CREATE TABLE OPERS3 AS SELECT distinct id, branch, op_da sum(income_biomass) as income_biomass, sum(Biomass) as Biomass, sum(Harvested_V_log_pine* P_PINE_LOG + Harvested_V_log_spruce* P_SPRUCE_LOG+ Harvested_V_log_birch* P_BIRCH_LOG) as income_log_change, sum(Harvested_V_pulp_pine* P_PINE_PULP+ Harvested_V_pulp_spruce* P_SPRUCE_PULP+ Harvested_V_pulp_birch* P_BIRCH_PULP+ Harvested_v_others* P_OTHER)as income_pulp_change, - sum(cash_flow) as cash_flow, thin + sum(cash_flow) as cash_flow, thin, SCAR, PLANTING,SEEDING, sum(Harvested_V_log_pine*0.88 +Harvested_V_log_spruce*0.898 +Harvested_V_log_birch*0.885+Harvested_V_log_others*0.874) as Harvested_V_log_under_bark, @@ -100,7 +100,7 @@ create_table_UNIT <- 'Create Table UNIT AS SELECT u.*, (select sum(stratum.V) From stratum where stratum.data_id = u.data_id and SP = 9) as V_o_decidious, l.data_date, b.branch, b.branch_desc, b.branching_group, o.income/u.AREA as income, o.cash_flow/u.AREA as cash_flow, o.clearcut, o.Harvested_V_log/u.AREA as Harvested_V_log, o.Harvested_V_pulp/u.AREA as Harvested_V_pulp, o.Harvested_V/u.AREA as Harvested_V, m.max_v, o.income_log/u.AREA as income_log, o.income_pulp/u.AREA as income_pulp, - o.income_log_change/u.AREA as income_log_change,o.income_pulp_change/u.AREA as income_pulp_change, o.income_biomass, o.Biomass/u.AREA as Biomass, o.thin + o.income_log_change/u.AREA as income_log_change,o.income_pulp_change/u.AREA as income_pulp_change, o.income_biomass, o.Biomass/u.AREA as Biomass, o.thin, o.Harvested_biomass_Ton/u.AREA as Biomass_ton, o.Harvested_V_pulp_under_bark/u.AREA as Harvested_V_pulp_under_bark, @@ -129,27 +129,36 @@ create_table_UNIT <- 'Create Table UNIT AS SELECT u.*, for (name in db_names){ + name = "MV_Korsnas" # Connect to the database - con <- dbConnect(dbDriver("SQLite"), dbname = paste0(path,"input_data/simulated_", sim_variant,"_" ,name , "_rsu.db")) + #con <- dbConnect(dbDriver("SQLite"), + # dbname = paste0(path,"input_data/simulated_", sim_variant,"_" ,name , "_rsu.db")) + + db <- dbConnect(dbDriver("SQLite"), + dbname = paste0(path, "input_data/simulated_without_MV_Korsnas_Wind_NO_1-15.db")) # If the following tables already exist, for which the query is defined, remove them tab_to_delete <- c("OPERS2", "OPERS3", "max_v", "UNIT") for(i in tab_to_delete){ - if(dbExistsTable(con, i)) {dbRemoveTable(con, i)} + if(dbExistsTable(db, i)) {dbRemoveTable(db, i)} } # Run the Queries and create the final table "UNIT", # which contains the development of all stands and indicators under the simulated management regimes - query_to_run <- c(create_table_max_v, create_table_OPERS2, create_table_OPERS3, create_table_UNIT) + query_to_run <- c(create_table_max_v, + create_table_OPERS2, + create_table_OPERS3, + create_table_UNIT) for(i in query_to_run){ - dbExecute(con, i) + #i = create_table_UNIT + dbExecute(db, i) } - dbDisconnect(con) + dbDisconnect(db) - rm(query_to_run, tab_to_delete, con) + rm(query_to_run, tab_to_delete, db) } rm(create_table_max_v, create_table_OPERS2, create_table_OPERS3, create_table_UNIT) @@ -173,13 +182,13 @@ rm(create_table_max_v, create_table_OPERS2, create_table_OPERS3, create_table_UN # for (name in db_names){ # # Connect to the database # -# con <- dbConnect(dbDriver("SQLite"), dbname = paste0(path,"input_data/withoutCC_wind/simulated_", sim_variant,"_" ,name , ".db")) +# db <- dbConnect(dbDriver("SQLite"), dbname = paste0(path,"input_data/withoutCC_wind/simulated_", sim_variant,"_" ,name , ".db")) # # # If the following tables already exist, for which the query is defined, remove them # tab_to_delete <- c("OPERS2", "OPERS3", "max_v", "UNIT") # # for(i in tab_to_delete){ -# if(dbExistsTable(con, i)) {dbRemoveTable(con, i)} +# if(dbExistsTable(db, i)) {dbRemoveTable(db, i)} # } # # # Run the Queries and create the final table "UNIT", @@ -187,12 +196,12 @@ rm(create_table_max_v, create_table_OPERS2, create_table_OPERS3, create_table_UN # query_to_run <- c(create_table_max_v, create_table_OPERS2, create_table_OPERS3, create_table_UNIT) # # for(i in query_to_run){ -# dbExecute(con, i) +# dbExecute(db, i) # } # -# dbDisconnect(con) +# dbDisconnect(db) # -# rm(query_to_run, tab_to_delete, con) +# rm(query_to_run, tab_to_delete, db) # } # # rm(create_table_max_v, create_table_OPERS2, create_table_OPERS3, create_table_UNIT)