-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutMtn_cube_output_organize.R
More file actions
164 lines (137 loc) · 6.46 KB
/
FutMtn_cube_output_organize.R
File metadata and controls
164 lines (137 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# organizing rhessys outputs, from output filters, to be input to future mountain SQL
# based on variables given in FB_DB.sql
setwd("~/Documents/BigCreek7.2ForExample/out/bigcreek/")
# setwd("~/Documents/BigCreek7.2ForExample/out/bigcreek_salience_archive/randseed1/")
library(tidyverse)
library(tm)
library(RMariaDB)
# first in terminal: sudo mysql.server start
#con = dbConnect(RMariaDB::MariaDB(), user = 'root', password = '',host = 'localhost', port=3306, db="BigCreek_FutMtn")
# using historic climate as baseline, warming ID = 0
# the climate change scenarios will be ID as degrees of warming (1, 2, 4, 6)
warming = 4
################################################################
# first table is aggcube #######################################
aggc_p <- read.csv("cube_agg_p.csv")
# grouping by date only
aggc_p_grouped <- aggc_p %>%
mutate(date = as.Date(paste(year, month, day, sep="/"))) %>%
mutate(groundevap = evaporation_surf +
exfiltration_unsat_zone +
exfiltration_sat_zone,
trans = transpiration_sat_zone + transpiration_unsat_zone,
leafC = cs.leafc + cs.leafc_store,
stemC = cs.live_stemc + cs.dead_stemc,
rootC = cs.frootc + cs.live_crootc + cs.dead_crootc,
mortC = fe.canopy_target_prop_c_remain_adjusted + fe.canopy_target_prop_c_remain_adjusted_leafc) %>%
dplyr::select(-basinID, -day, -month, -year,
-evaporation_surf, -exfiltration_unsat_zone, -exfiltration_sat_zone, -transpiration_sat_zone, -transpiration_unsat_zone, -cs.leafc, -cs.leafc_store, -cs.live_stemc, -cs.dead_stemc, -cs.frootc, -cs.live_crootc, -cs.dead_crootc, -fe.canopy_target_prop_c_remain_adjusted, -fe.canopy_target_prop_c_remain_adjusted_leafc)
colnames(aggc_p_grouped) <- c("litterc",
"burn",
"soilc",
"depthToGW",
"snowpack",
"canopyevap",
"coverfract",
"streamflow",
"vegAccessWater",
"snowfall",
"rain",
"netpsn",
"height",
"consumedC",
"rootdepth",
"date",
"groundevap",
"trans",
"leafC",
"stemC",
"rootC",
"mortC")
cube_agg <- aggc_p_grouped %>%
mutate(warmingIdx = warming,
vegtype = 50)
# whats missing (to be added when passed to mysql:
# [id] int PRIMARY KEY,
# [dateIdx] [key], (Date is included, not as ID)
# check output with plots ##################################
ggplot(cube_agg) + geom_line(aes(x=date, y=burn))
ggplot(cube_agg) + geom_line(aes(x=date, y=stemC))
ggplot(cube_agg) + geom_line(aes(x=date, y=consumedC))
ggplot(cube_agg) + geom_line(aes(x=date, y=mortC))
firesize <- read.delim("../../scriptsFire/FireSizes0.txt")
colnames(firesize) <- cnames <- c("fire_size",
"year",
"month",
"wind_dir",
"wind_spd",
"num_ign")
ggplot(firesize) + geom_line(aes(x=year, y=fire_size))
############################################################
### write to .csv
write.csv(cube_agg, "aggcube_data_point.csv")
# create new table and write to it (only need to do this once)
#dbWriteTable(con, "aggcube_data_point", cube_agg)
#dbListTables(con)
# dbListFields(con, "aggcube_data_point")
# # dbSendStatement(con, "ALTER TABLE aggcube_data_point
# # ADD id int PRIMARY KEY,
# # ADD dateIdx int UNIQUE KEY,
# # ADD UNIQUE KEY (warmingIdx)")
#
# # after setting up table the first time, add rows to it
# db_insert_into(con, "aggcube_data_point", cube_agg)
# cube_data_point table ####################################
#setwd("../bigcreek_salience_archive/rs1cc6/")
cubedp_p <- read.csv("cube_data_point_p.csv")
cubedp_c1 <- read.csv("cube_data_point_c_over.csv")
cubedp_c2 <- read.csv("cube_data_point_c_under.csv")
cube_p_grouped <- cubedp_p %>%
mutate(date = as.Date(paste(year, month, day, sep="/"))) %>%
# group_by(date, patchID) %>%
# summarize_all(sum) %>%
dplyr::select(-month, -day, -year, -basinID, -hillID, -zoneID) %>%
mutate(warmingIdx = warming)
# stratums have already been separated out in the output filter file
cube_c_over <- cubedp_c1 %>%
mutate(date = as.Date(paste(year, month, day, sep="/"))) %>%
dplyr::select(-month, -day, -year,
-basinID, -hillID, -zoneID, -stratumID) %>%
group_by(date, patchID) %>%
summarize_all(sum)
cube_c_under <- cubedp_c2 %>%
mutate(date = as.Date(paste(year, month, day, sep="/"))) %>%
dplyr::select(-month, -day, -year,
-basinID, -hillID, -zoneID, - stratumID) %>%
group_by(date, patchID) %>%
summarize_all(sum)
overunder <- inner_join(cube_c_under, cube_c_over, by=c("date","patchID"))
allcube_dp <- right_join(overunder, cube_p_grouped) %>%
mutate(patchfamilyIdx = patchID) %>%
dplyr::select(-patchID)
ggplot(allcube_dp) + geom_line(aes(x=date, y=stemCOver, col=as.factor(patchfamilyIdx), linetype='over')) + geom_line(aes(x=date, y=stemCUnder, col=as.factor(patchfamilyIdx), linetype='under'))
vegids <- read.csv("~/Documents/BigCreek7.2ForExample/rhessys_fire_outputs/vegIDs_cube_data.txt")
allcube_veg = left_join(allcube_dp, vegids, by='patchfamilyIdx')
# names left over
# CREATE TABLE [cube_data_point] (
# [id] int PRIMARY KEY,
# [dateIdx] [key],
# [cubeIdx] int, ## is this same as patch??
# [patchfamilyIdx] [key],
# )
## write to csv
write.csv(allcube_veg, "cube_data_point.csv")
# convert individual cubes to sql #########################
# to write the first table
# dbWriteTable(con, "cube_data_point", allcube_veg)
# dbSendStatement(con, "ALTER TABLE cube_data_point
# ADD id int PRIMARY KEY,
# ADD dateIdx int UNIQUE KEY,
# ADD cubeIdx int UNIQUE KEy,
# ADD UNIQUE KEY (patchfamilyIdx),
# ADD UNIQUE KEY (warmingIdx)")
# dbListFields(con, "cube_data_point")
# # then to add warming scenarios to this table
# db_insert_into(con, "cube_data_point", allcube_veg)
#
# dbDisconnect(con)