-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrscript_rp_diag.R
50 lines (41 loc) · 1.54 KB
/
rscript_rp_diag.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
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
# args <- c(5, 30)
library(tidyverse)
source("R/calc_return_period.R")
# load("data/df_corr_fet.RData") # must use df_corr_fet, and cwd_lue0_fet below
load("data/df_corr_nSIF.RData") # must use df_corr_nSIF, and cwd_lue0_nSIF below
df_corr <- df_corr_nSIF %>%
arrange(lon) %>%
mutate(idx = 1:n()) %>%
mutate(chunk = rep(1:as.integer(args[2]), each = (nrow(.)/as.integer(args[2])), len = nrow(.)))
## split sites data frame into (almost) equal chunks
list_df_split <- df_corr %>%
group_by(chunk) %>%
group_split()
## retain only the one required for this chunk
df_corr_sub <- list_df_split[[as.integer(args[1])]]
##------------------------------------------------------------------------
## asdf
##------------------------------------------------------------------------
filn <- paste0("data/df_rp_diag/df_rp_diag_nSIF_ichunk_", args[1], "_", args[2], ".RData")
df_rp_diag <- df_corr_sub %>%
dplyr::select(lon, lat, s0 = cwd_lue0_nSIF) %>% ## select which one to consider here!
drop_na() %>%
group_by(lon) %>%
nest() %>%
mutate(ilon = as.integer((lon + 179.975)/0.05 + 1)) %>%
ungroup()
if (nrow(df_rp_diag)>0){
if (!file.exists(filn)){
df_rp_diag <- df_rp_diag %>%
mutate(data = purrr::map2(ilon, data, ~calc_return_period(.x, .y))) %>%
unnest(data) %>%
dplyr::select(lon, lat, loc, scale, rp_diag)
save(df_rp_diag, file = filn)
} else {
print(paste("File exists already: ", filn))
}
} else {
print("No data available for this chunk.")
}