-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCCLMcalc.R
63 lines (48 loc) · 1.83 KB
/
CCLMcalc.R
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
library(dplyr)
library(reshape2)
calcderived <- function(sim) {
## calculate 2m potential temperature with lowest pressure,
## other hights use correct heights
lowestap <- sim$ap %>%
arrange(time, site, type, height) %>%
group_by(time, site, type) %>%
summarise(ap=first(ap)) %>%
mutate(height=2)
names(sim$at2)[names(sim$at2)=="at2"] <- "at"
sim$pt <- rbind(
merge(sim$at, lowestap),
merge(sim$at2, sim$ap)
) %>%
mutate(pt = at / ((1.0e-5 * ap)^(287.05/1005.0)) - 273.15) %>%
select(-c(ap, at))
## use degree celsius
sim$at <- mutate(sim$at, at=at-273.15)
sim$tg <- mutate(sim$tg, tg=tg-273.15)
## change convention of fluxes
sim$fh <- mutate(sim$fh, fh=-fh)
sim$fl <- mutate(sim$fl, fl=-fl)
## combine different wind velocity
names(sim$wvu2)[names(sim$wvu2)=="wvu2"] <- "wvu"
names(sim$wvv2)[names(sim$wvv2)=="wvv2"] <- "wvv"
sim$wvu <- rbind(sim$wvu, sim$wvu2)
sim$wvv <- rbind(sim$wvv, sim$wvv2)
## calculate total wind velocity
sim$wv <- merge(sim$wvu, sim$wvv) %>%
mutate(wv = sqrt(wvu^2 + wvv^2)) %>%
select(-c(wvu, wvv))
## calculate total incoming solar radiation
sim$sd <- merge(sim$sd_dir, sim$sd_diff) %>%
mutate(sd = sd_dir + sd_diff) %>%
select(-c(sd_dir, sd_diff))
## calculate albedo
sim$al <- merge(sim$sd, sim$su) %>%
mutate(al = su/sd) %>%
select(-c(sd, su))
## remove these temporary field
for (toremove in c("sd_dir", "sd_diff", "ap", "at2")) {
sim[[toremove]] <- NULL
}
return(lapply(sim,
function(x) melt(x, id.vars=c("time", "site", "type", "height"))) %>%
do.call(what="rbind"))
}