forked from JW-spec-tech/S_land_bio_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Surface_characterization.R
154 lines (121 loc) · 4.03 KB
/
Surface_characterization.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
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
library(parallel)
library(parallelly)
library(doParallel)
library(mgcv)
library(dplyr)
library(purrr)
library(raster)
library(geodiv)
library(arrow)
print(paste("Start of Surface analysis @",Sys.time()))
reps= as.numeric(Sys.getenv('REPS')) # Number of Replicates
sims= as.numeric(Sys.getenv('SIMS')) # Number of Sims
sizes= as.numeric(Sys.getenv('SIZE')) # Size of the Landscape
seed= as.numeric(Sys.getenv('SEED')) # Starting seed
var = as.numeric(Sys.getenv('VAR')) # Variation in biomass field --> higher variation = increased biomass variation
percent = as.numeric(Sys.getenv('PERCENT')) # Sets sampling percentage of the sampling of the entire dataset
# #### create the cluster ####
#
# n.cores <- as.numeric(Sys.getenv('OMP_NUM_THREADS'))
# my.cluster <- parallel::makeCluster(
# n.cores,
# type = "PSOCK"
# )
#
# #check cluster definition (optional)
# print(my.cluster)
#
#
# #register it to be used by %dopar%
# doParallel::registerDoParallel(cl = my.cluster)
#
# #check if it is registered (optional)
# foreach::getDoParRegistered()
#
# #how many workers are available? (optional)
# foreach::getDoParWorkers()
Sys.time()
Surface_analysis <- function() {
for (cwd in list.dirs(full.names = T,recursive = F)) {
print(cwd)
setwd(cwd)
cwd=getwd()
setwd(cwd)
#### 1. Find all sims and files ####
dir.create(paste0(cwd,"/Result_Surface"), recursive = T)
raster_sim <- arrow::read_parquet(paste0(cwd,"/sim/sim1"))
raster_sim <- data.frame(raster_sim$coord.x,raster_sim$coord.y,raster_sim$biomass)
raster_sim <- rasterFromXYZ(raster_sim)
#### 2. Surface analysis ####
# Roughness Calculation
RC <- geodiv::sa(raster_sim)
# Surface bearing index (peaks)
SBI <- geodiv::sbi(raster_sim)
#### 3. Write files ####
surface_C <- data.frame(RC=RC,SBI=SBI)
write.table(surface_C, paste0(cwd,"/Result_Surface/Surface.charaterization"),row.names=F)
#### 4. GC ####
gc()
#
# #### 1. Find all sims and files ####
# start_year =S_year
# files <- list.dirs(recursive = F, full.names = TRUE)
# years = length(files)
# size = 499
#
#
# #### Loop to run Surface analysis on each sim individually ####
#
# Surface <- foreach(
# d = files,
# .packages = c('mgcv','dplyr','purrr','raster','geodiv','arrow')
# ) %dopar% {
#
# #### 1. Find all sims and files ####
# dir.create(paste0(d,"/Result_Surface"), recursive = T)
# raster_sim <- arrow::read_parquet(paste0(d,"/sim/sim1"))
# raster_sim <- data.frame(raster_sim$coord.x,raster_sim$coord.y,raster_sim$biomass)
# raster_sim <- rasterFromXYZ(raster_sim)
#
# #### 2. Surface analysis ####
# # Roughness Calculation
#
# RC <- geodiv::sa(raster_sim)
#
# # Surface bearing index (peaks)
#
# SBI <- geodiv::sbi(raster_sim)
#
# #### 3. Write files ####
# surface_C <- data.frame(RC=RC,SBI=SBI)
# write.table(surface_C, paste0(d,"/Result_Surface/Surface.charaterization"),row.names=F)
#
# #### 4. GC ####
# gc()
setwd("../")
}
Surface_roughness_list <- list()
counter = 1
files <- list.dirs(full.names = T,recursive = F)
for (file in files) {
file <- substring(file, 2)
file = paste0(getwd(),file)
print(file)
Surface_roughness_list[[counter]] <- readr::read_table(paste0(file,"/Result_Surface/Surface.charaterization"))
counter = counter + 1
}
Surface_roughness_df <- do.call(rbind.data.frame, Surface_roughness_list)
Surface_roughness_df_mean <- colMeans(Surface_roughness_df)
write.table(Surface_roughness_df_mean, "Surface_roughness_mean")
setwd("../")
}
# Get files names
f_list <- paste0(getwd(),"/",list.dirs(path = "exp", full.names = TRUE, recursive = F))
for (i in f_list) {
print(i)
setwd(i)
Surface_analysis()
setwd("~/Git projects/S_land_bio_sim")
}
parallel::stopCluster(cl = my.cluster)
print(paste("End of Surface analysis @",Sys.time()))