-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_run_migclim.R
228 lines (197 loc) · 11.1 KB
/
03_run_migclim.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/software/R/3.5.1/bin/R
####################################################################################################
### Run migclim simulations on for the "Bryophyte" data (Florian, Flavien and Alain's data).
### ***************************************************************************************
###
###
####################################################################################################
####################################################################################################
### Input and environment setup.
### ***************************
# Input data location and range of parameters to test.
root_dir = '/scratch/temporary/rengler/migclim_run'
input_dir = file.path(root_dir, 'data_input')
if(startsWith(system('hostname', intern=T), 'rserv')) input_dir = '/scratch/local/monthly/rengler/data_input'
if(startsWith(system('hostname', intern=T), 'dee-serv')) input_dir = '/scratch/local/monthly/rengler/data_input'
output_dir = file.path(root_dir, 'data_output')
output_dir_rasters = file.path(output_dir, '01_raster_files')
species_file = file.path(input_dir, 'species_table.txt')
simulation_file = file.path(input_dir, 'simulation_table.txt')
test_mode = TRUE
# migclim parameters.
env_change_steps = 50 # number of environmental change steps.
dispersal_steps = 10 # number of dispersal steps.
dispersal_distance = 10 # maximum dispersal distance, in cell units.
ldd_min_dist = 11 # minimum long distance dispersal distance, in cell units.
ldd_max_dist = 3000 # maximum long distance dispersal distance, in cell units.
replicate_nb = 30 # number of replicates to perform for each migclim simulation.
# Load species list from species table.
# ************************************
if(!dir.exists(input_dir)) stop('root dir does not exist:', input_dir, '\n')
if(!dir.exists(output_dir)) stop('directory does not exist:', output_dir, '\n')
if(!dir.exists(output_dir_rasters)) stop('directory does not exist:', output_dir_rasters, '\n')
if(!file.exists(species_file)) stop('input file does not exist:', species_file, '\n')
if(!file.exists(simulation_file)) stop('input file does not exist:', species_file, '\n')
species_table = read.table(species_file, h=T, sep='\t', as.is=T, stringsAsFactors=F)
stopifnot(nrow(species_table) == 40)
simulation_table = read.table(simulation_file, h=T, sep='\t',
as.is=T, stringsAsFactors=F, colClasses='character')
simulation_list = 1:nrow(simulation_table)
stopifnot(length(simulation_list) == 4800)
# Command line agruments (optional).
# *********************************
# The index of the simulation(s) to run (one or more numbers in the range 1 - 2400) and the test
# mode value (TRUE/FALSE) can be passed to the script as command line arguments. If nothing is
# passed, then the script defaults to running all simulations in test mode.
args = commandArgs(trailingOnly=TRUE) # commandArgs() retrieves all input arguments that were
# passed to the script with 'Rscript'.
if(length(args) > 0){
simulation_index = args[[1]] # e.g. '1,2,5'
test_mode = as.logical(args[[2]]) # either 'TRUE' or 'FALSE'.
# Convert species to process input to numeric values.
if(simulation_index != 'all'){
simulation_index = as.numeric(unlist(strsplit(simulation_index, split=',')))
if(any(is.na(simulation_index))) stop('invalid input species number passed.\n')
if(any(simulation_index < 1)) stop('input species nb out of range.\n')
if(any(simulation_index > nrow(simulation_table))) stop('input species nb out of range.\n')
simulation_list = simulation_list[simulation_index]
}
# Verify test mode value is TRUE/FALSE.
if(is.na(test_mode)) stop('invalid test mode value passed.\n')
}
# Progress report.
# ***************
cat('################################################################################ \n')
cat('### Setup input data script. \n')
cat('### *********************** \n')
cat('### Number of simulations:', length(simulation_list), '\n')
cat('### input data dir :', input_dir, '\n')
cat('### Test mode :', test_mode, '\n')
cat('### \n')
Sys.sleep(3)
# Load required libraries.
require(raster, quietly=TRUE)
require(SDMTools, quietly=TRUE, warn.conflicts=FALSE)
require(MigClim, lib.loc = '/Home/rengler/R/x86_64-koji-linux-gnu-library/3.5/', quietly=TRUE)
####################################################################################################
####################################################################################################
### Verify all input data for the current run are present.
### *****************************************************
cat('### Input data check: \n')
for(simulation_index in simulation_list){
# Retrieve simulation parameter values.
species = simulation_table[simulation_index, 'species']
cc_scenario = simulation_table[simulation_index, 'cc_scenario']
wind_speed = simulation_table[simulation_index, 'wind_speed']
release_height = simulation_table[simulation_index, 'release_height']
cat('### ->', species, cc_scenario, wind_speed, release_height, '\n')
# Set working directory for current simulation.
species_dir = file.path(input_dir, species)
if(!dir.exists(species_dir)) stop('missing input directory:', species_dir, '\n')
setwd(species_dir)
# Check that species initial distribution is present.
input_file = paste0('hsmap_', species, '_present.asc')
if(!file.exists(input_file)) stop('missing input file:', input_file, '\n')
# Check that habitat suitability maps are present.
# Note that there are only 4 actual habitat suitability files, because for the other
# environmental change steps we just keep re-using the data for 2050.
for(i in 1:4){
input_file = paste0('hsmap_', species, '_', cc_scenario, '_', i, '.asc')
if(!file.exists(input_file)) stop('missing input file:', input_file, '\n')
}
# Check that disersal kernel rasters are present.
# Note that there are only 4 actual kernel files, because for the other environmental change
# steps we just keep re-using the data for 2050.
for(i in 1:4){
year = (i + 201) * 10
for(j in 1:dispersal_distance){
input_file = paste0('kernel_', species, '_', cc_scenario, '_',
wind_speed, '_', release_height, '_', year, '_', j, '.asc')
if(!file.exists(input_file)) stop('missing input file:', input_file, '\n')
}
}
rm(species, cc_scenario, wind_speed, release_height, species_dir, input_file, year, i, j)
}
cat('### -> completed. \n')
cat('### \n')
####################################################################################################
####################################################################################################
### Run migclim.
### ***********
for(simulation_index in simulation_list){
# Retrieve simulation parameter values.
species = simulation_table[simulation_index, 'species']
cc_scenario = simulation_table[simulation_index, 'cc_scenario']
wind_speed = simulation_table[simulation_index, 'wind_speed']
release_height = simulation_table[simulation_index, 'release_height']
ldd_frequency = simulation_table[simulation_index, 'ldd_freq']
simulation_name = simulation_table[simulation_index, 'simulation_name']
tss_threshold = species_table[which(species_table$species==species),'max_TSS_threshold']
# Set working directory for current simulation, and if needed, delete existing output
# directory for the current run.
setwd(file.path(input_dir, species))
if(dir.exists(simulation_name)) unlink(simulation_name, recursive=TRUE)
# Run migclim simulation.
# **********************
cat('### Starting migclim simulation:', simulation_name, '\n')
cat('### Test mode:', test_mode, '\n')
N = MigClim.migrate(iniDist = paste0('hsmap_', species, '_present.asc'),
hsMap = paste0('hsmap_', species, '_', cc_scenario, '_'),
rcThreshold = tss_threshold,
envChgSteps = env_change_steps,
dispSteps = dispersal_steps,
dispKernel = paste0('kernel_', species, '_', cc_scenario, '_',
wind_speed, '_', release_height),
dispKernelMapNb = dispersal_distance,
barrier = NULL,
barrierType = 'strong',
iniMatAge = 1,
propaguleProd = 1,
lddFreq = as.numeric(ldd_frequency),
lddMinDist = ldd_min_dist,
lddMaxDist = ldd_max_dist,
simulName = simulation_name,
replicateNb = replicate_nb,
overWrite = TRUE,
testMode = test_mode,
fullOutput = FALSE,
keepTempFiles = FALSE,
randomGeneratorSeed = 10 + simulation_index)
# Re-organize output files.
# ************************
if(!test_mode){
# Delete some of the raster outputs to save space.
cat('### -> delete some of replicated rasters. \n')
if(replicate_nb > 5){
for(i in 6:replicate_nb) unlink(file.path(simulation_name,
paste0(simulation_name, '_', i, '_raster.asc')))
}
# Move outputs to output directory. Text and raster files go into separate directories.
cat('### -> move data to output directory. \n')
output_dir_text = file.path(output_dir, species, simulation_name)
output_dir_raster = file.path(output_dir_rasters, species, simulation_name)
if(!dir.exists(output_dir_text)) dir.create(output_dir_text, recursive=T)
if(!dir.exists(output_dir_raster)) dir.create(output_dir_raster, recursive=T)
for(f in list.files(simulation_name, full.names=F)){
if(endsWith(f,'.asc')){
file.copy(from=file.path(simulation_name, f), to=file.path(output_dir_raster, f))
} else{
file.copy(from=file.path(simulation_name, f), to=file.path(output_dir_text, f))
}
file.remove(file.path(simulation_name, f))
}
# Delete now empty directory.
file.remove(simulation_name)
rm(output_dir_text, output_dir_raster, f)
}
# Clean environment.
rm(species, cc_scenario, wind_speed,
release_height, ldd_frequency, tss_threshold, simulation_name, N)
}
# Progress report.
# ***************
cat('### -> completed. \n')
cat('### \n')
cat('### migclim run completed successfully. \n')
cat('################################################################################ \n')
####################################################################################################