-
Notifications
You must be signed in to change notification settings - Fork 2
/
Step1_Process_all_available_flux_sites_for_PLUMBER2.R
441 lines (314 loc) · 17.7 KB
/
Step1_Process_all_available_flux_sites_for_PLUMBER2.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#NB. needs these to work on storm servers (because of package lutz) !!!!!!!!!
# module add proj/4.9.3
# module add python/2.7.13
# module add perl/5.24.1
# module add gdal/2.1.3
# module add gcc/6.3.0
#
#devtools::install_github("aukkola/FluxnetLSM", ref="master") #Package broken for some reason, must be installed locally
setwd("/srv/ccrc/data04/z3509830/Fluxnet_data//All_flux_sites_processed_PLUMBER2/FluxnetLSM")
install.packages(".", repos=NULL, type='source', INSTALL_opts="--no-staged-install") #not sure why have to add install opt
library(FluxnetLSM) # convert_fluxnet_to_netcdf
library(parallel)
library(ncdf4)
#clear R environment
rm(list=ls(all=TRUE))
#Thresholds for missing and gap-filled time steps
missing_met <- 100 #max. percent missing (must be set)
missing_flux <- 100
gapfill_met_tier1 <- 100 #max. gapfilled percentage
gapfill_met_tier2 <- 100
gapfill_flux <- 100
min_yrs <- 1 #min. number of consecutive years
#Set main path
path <- "/srv/ccrc/data04/z3509830/Fluxnet_data/"
#Set output path for all data
out_path <- paste0(path, "/All_flux_sites_processed_PLUMBER2")
#Number of cluster
ncl <- 12
### Process all datasets separately and then find non-duplicate sites ###
####################
### FLUXNET 2015 ###
####################
#Outputs will be saved to this directory
out_path_flx <- paste0(out_path, "/FLUXNET2015_sites/")
#Remove path
unlink(out_path_flx, recursive = TRUE)
### Hourly and Halfhourly ###
tstep <- c("Hourly", "Halfhourly")
#Loop through time steps
for(k in 1:length(tstep)){
in_path <- paste(path, "/FLUXNET2016/Original_data/",
tstep[k], "_qc_fixed", sep="")
era_path <- paste(path, "/FLUXNET2016/Original_data/EraInterim/",
tstep[k], sep="")
# Input Fluxnet data files (using FULLSET in this example, se R/Helpers.R for details)
infiles <- get_fluxnet_files(in_path)
#Retrieve site codes
site_codes <- sapply(infiles, get_path_site_code)
#Retrieve dataset versions
datasetversions <- sapply(infiles, get_fluxnet_version_no)
# Find ERA-files corresponding to site codes
ERA_files <- sapply(site_codes, function(x) get_fluxnet_erai_files(era_path, site_code=x))
#Stop if didn't find ERA files
if(any(sapply(ERA_files, length)==0)){
stop("No ERA files found, amend input path")
}
### Process files ###
#Initialise clusters (using 2 cores here)
cl <- makeCluster(getOption('cl.cores', ncl))
#Import variables to cluster
clusterExport(cl, "out_path_flx")
clusterExport(cl, "convert_fluxnet_to_netcdf")
if(exists("missing_met")) {clusterExport(cl, "missing_met")}
if(exists("missing_flux")) {clusterExport(cl, "missing_flux")}
if(exists("gapfill_met_tier1")) {clusterExport(cl, "gapfill_met_tier1")}
if(exists("gapfill_met_tier2")) {clusterExport(cl, "gapfill_met_tier2")}
if(exists("gapfill_flux")) {clusterExport(cl, "gapfill_flux")}
if(exists("min_yrs")) {clusterExport(cl, "min_yrs")}
#Loops through sites
clusterMap(cl=cl, function(w,x,y,z) tryCatch(convert_fluxnet_to_netcdf(infile=w, site_code=x, out_path=out_path_flx,
datasetversion=z, met_gapfill="ERAinterim",
flux_gapfill="statistical", era_file=y,
missing_met=missing_met, missing_flux=missing_flux,
gapfill_met_tier1=gapfill_met_tier1,
gapfill_met_tier2=gapfill_met_tier2,
gapfill_flux=gapfill_flux, min_yrs=min_yrs,
#model="CABLE",
check_range_action="warn",
include_all_eval=TRUE),
error = function(e) NULL),
w=infiles, x=site_codes, y=ERA_files, z=datasetversions)
stopCluster(cl)
}
#
# #Loops through sites
# w=infiles[[1]]
# x=site_codes[[1]]
# y=ERA_files[[1]]
# z=datasetversions[[1]]
# convert_fluxnet_to_netcdf(infile=w, site_code=x, out_path=out_path_flx,
# datasetversion=z, met_gapfill="ERAinterim",
# flux_gapfill="statistical", era_file=y,
# missing_met=missing_met, missing_flux=missing_flux,
# gapfill_met_tier1=gapfill_met_tier1,
# gapfill_met_tier2=gapfill_met_tier2,
# gapfill_flux=gapfill_flux, min_yrs=min_yrs,
# #model="CABLE",
# check_range_action="warn",
# include_all_eval=TRUE)
#
#
#
# #Loops through sites
# mapply( function(w,x,y,z) convert_fluxnet_to_netcdf(infile=w, site_code=x, out_path=out_path_flx,
# datasetversion=z, met_gapfill="ERAinterim",
# flux_gapfill="statistical", era_file=y,
# missing_met=missing_met, missing_flux=missing_flux,
# gapfill_met_tier1=gapfill_met_tier1,
# gapfill_met_tier2=gapfill_met_tier2,
# gapfill_flux=gapfill_flux, min_yrs=min_yrs,
# #model="CABLE",
# check_range_action="warn",
# include_all_eval=TRUE),
# # error = function(e) NULL),
# w=infiles, x=site_codes, y=ERA_files, z=datasetversions)
#
#
#
#################
### La Thuile ###
#################
#Input path
in_path <- paste(path, "/LaThuile/Original_data/raw_data", sep="/")
#Output path
out_path_lt <- paste0(out_path, "/LaThuile_sites/")
#Remove path
unlink(out_path_lt, recursive = TRUE)
#Input Fluxnet data files (using FULLSET in this example, se R/Helpers.R for details)
infiles <- get_fluxnet_files(in_path, datasetname="LaThuile")
#Retrieve site codes
site_codes <- unique(sapply(infiles, get_path_site_code))
#Reorganise input files by site
infiles <- lapply(site_codes, function(site) get_fluxnet_files(in_path, site_code=site,
datasetname="LaThuile"))
### Process files ###
#Initialise clusters (using 2 cores here)
cl <- makeCluster(getOption('cl.cores', ncl))
#Import variables to cluster
clusterExport(cl, "out_path_lt")
clusterExport(cl, "convert_fluxnet_to_netcdf")
if(exists("missing_met")) {clusterExport(cl, "missing_met")}
if(exists("missing_flux")) {clusterExport(cl, "missing_flux")}
if(exists("gapfill_met_tier1")) {clusterExport(cl, "gapfill_met_tier1")}
if(exists("gapfill_met_tier2")) {clusterExport(cl, "gapfill_met_tier2")}
if(exists("gapfill_flux")) {clusterExport(cl, "gapfill_flux")}
if(exists("min_yrs")) {clusterExport(cl, "min_yrs")}
#Loops through sites
clusterMap(cl=cl, function(w,x) tryCatch(convert_fluxnet_to_netcdf(infile=w, site_code=x, out_path=out_path_lt,
datasetname="LaThuile",
met_gapfill="statistical",
flux_gapfill="statistical",
missing_met=missing_met, missing_flux=missing_flux,
gapfill_met_tier1=gapfill_met_tier1,
gapfill_met_tier2=gapfill_met_tier2,
gapfill_flux=gapfill_flux, min_yrs=min_yrs,
#model="CABLE",
include_all_eval=TRUE,
check_range_action="warn",
copyfill=365, regfill=365),
error = function(e) NULL),
w=infiles, x=site_codes)
stopCluster(cl)
#
# mapply( function(w,x) convert_fluxnet_to_netcdf(infile=w, site_code=x, out_path=out_path_lt,
# datasetname="LaThuile",
# met_gapfill="statistical",
# flux_gapfill="statistical",
# missing=missing, gapfill_all=gapfill_all,
# min_yrs=min_yrs, model="CABLE",
# include_all_eval=TRUE,
# check_range_action="truncate",
# copyfill=365, regfill=365),
#
# w=infiles, x=site_codes)
##############
### OzFlux ###
##############
#Input path
in_path <- paste0(path, '/OzFlux/Original_data/')
#Outputs will be saved to this directory
out_path_pre <- paste(path, "/OzFlux/Pre-processed_OzFlux_data/", sep="/")
out_path_oz <- paste(out_path, "/OzFlux_sites/", sep="/")
#Remove path
unlink(out_path_pre, recursive = TRUE)
unlink(out_path_oz, recursive = TRUE)
#Find sitefiles
site_files <- list.files(in_path, full.names=TRUE)
### Pre-process files ###
lapply(site_files, preprocess_OzFlux, outpath=out_path_pre)
#Fluxnet site codes (having to set these manually for now, should add to pre-processing)
site_codes <- list(AdelaideRiver = "AU-Ade",
AliceSpringsMulga = "AU-ASM", #NEW
Calperum = "AU-Cpr",
CapeTribulation = "AU-Ctr",
CowBay = "AU-Cow",
CumberlandPlain = "AU-Cum",
DalyPasture = "AU-DaP", #Site code only DaP on ozflux website
DalyUncleared = "AU-DaS",
DryRiver = "AU-Dry",
Emerald = "AU-Emr", #Not provided on ozflux website, maybe Arcturus? Taking site code from site metadata file
FoggDam = "AU-Fog", #NEW
Gingin = "AU-Gin",
GreatWesternWoodlands = "AU-GWW",
HowardSprings = "AU-How",
Litchfield = "AU-Lit", #NEW
#Loxton = "AU-Lox", #Less than one year of data
Otway = "AU-Otw",
RedDirtMelonFarm = "AU-RDF", #Not provided on ozflux website, taking site code from site metadata file
Ridgefield = "AU-Rgf", #NEW
RiggsCreek = "AU-Rig",
RobsonCreek = "AU-Rob", #NEW
Samford = "AU-Sam",
SturtPlains = "AU-Stp",
TiTreeEast = "AU-TTE", #NEW
Tumbarumba = "AU-Tum",
WallabyCreek = "AU-Wac", #NEW
Warra = "AU-Wrr", #NEW
Whroo = "AU-Whr",
WombatStateForest = "AU-Wom",
Yanco = "AU-Ync"
)
#Get sites (listed manually above for now, otherwise can't get site code)
sites <- names(site_codes)
#Find input files
in_files_oz <- unlist(sapply(sites, function(x) list.files(path=out_path_pre, pattern=x, full.names=TRUE)))
#Check that have as many original and pre-processed sites
if (length(in_files_oz) != length(site_files)) stop("Check why sites don't match")
### Process data ###
#Initialise clusters (using 2 cores here)
cl <- makeCluster(getOption('cl.cores', ncl))
#Import variables to cluster
clusterExport(cl, "out_path_oz")
clusterExport(cl, "convert_fluxnet_to_netcdf")
if(exists("missing_met")) {clusterExport(cl, "missing_met")}
if(exists("missing_flux")) {clusterExport(cl, "missing_flux")}
if(exists("gapfill_met_tier1")) {clusterExport(cl, "gapfill_met_tier1")}
if(exists("gapfill_met_tier2")) {clusterExport(cl, "gapfill_met_tier2")}
if(exists("gapfill_flux")) {clusterExport(cl, "gapfill_flux")}
if(exists("min_yrs")) {clusterExport(cl, "min_yrs")}
#Loops through sites
clusterMap(cl=cl, function(w,x) tryCatch(convert_fluxnet_to_netcdf(infile=w, site_code=x, out_path=out_path_oz,
datasetname="OzFlux", met_gapfill="statistical",
flux_gapfill="statistical",
missing_met=missing_met, missing_flux=missing_flux,
gapfill_met_tier1=gapfill_met_tier1,
gapfill_met_tier2=gapfill_met_tier2,
gapfill_flux=gapfill_flux, min_yrs=min_yrs,
include_all_eval=TRUE, check_range_action="warn"),
#model="CABLE"),
error = function(e) NULL),
w=in_files_oz, x=site_codes)
stopCluster(cl)
###################################
### Combine non-duplicate sites ###
###################################
### Find sites and files ###
#List all datasets
datasets <- list.files(out_path, full.names=TRUE)
#Find all site files
all_files <- unlist(sapply(datasets, function(x) list.files(paste0(x, "/Nc_files/Met/"))))
#Find dataset for each file
all_files_dataset <- sapply(all_files, function(x) strsplit(x, "_")[[1]][3])
#Get site codes
all_sites <- sapply(all_files, substr, start=1, stop=6)
#Find unique sites
unique_sites <- unique(all_sites)
### Create output directory for non-duplicate sites ###
out_path_all <- paste0(out_path, "/all_sites_no_duplicates/")
#Remove old directory if it exists
unlink(out_path_all, recursive=TRUE)
#Create output paths for met and flux
dir.create(paste0(out_path_all, "/Nc_files/Met/"), recursive=TRUE)
dir.create(paste0(out_path_all, "/Nc_files/Flux/"), recursive=TRUE)
dir.create(paste0(out_path_all, "/Nc_files/Figures/"), recursive=TRUE)
### Loop through unique sites ###
for (s in 1:length(unique_sites)) {
#Find number of site replicates
ind <- which(all_sites == unique_sites[s])
#If only one instance, copy that file
if (length(ind) == 1) {
#Met
file.copy(from=list.files(paste0(out_path, "/", all_files_dataset[ind],"_sites/Nc_files/Met"),
pattern=all_sites[ind], full.names=TRUE), to=paste0(out_path_all, "/Nc_files/Met"))
#Flux
file.copy(from=list.files(paste0(out_path, "/", all_files_dataset[ind],"_sites/Nc_files/Flux"),
pattern=all_sites[ind], full.names=TRUE), to=paste0(out_path_all, "/Nc_files/Flux"))
#If available in multiple datasets
} else {
#Prioritise OzFlux
if(any(all_files_dataset[ind] == "OzFlux")) {
dataset_to_use <- "OzFlux"
#Then use FLUXNET2015
} else if (any(all_files_dataset[ind] == "FLUXNET2015")) {
dataset_to_use <- "FLUXNET2015"
#And finally La Thuile
} else if (any(all_files_dataset[ind] == "LaThuile")) {
dataset_to_use <- "LaThuile"
#Dataset not recognised (stop)
} else {
stop ("Dataset not recognised")
}
#Copy files
#Met
file.copy(from=list.files(paste0(out_path, "/", dataset_to_use,"_sites/Nc_files/Met"),
pattern=all_sites[ind[1]], full.names=TRUE), to=paste0(out_path_all, "/Nc_files/Met"))
#Flux
file.copy(from=list.files(paste0(out_path, "/", dataset_to_use,"_sites/Nc_files/Flux"),
pattern=all_sites[ind[1]], full.names=TRUE), to=paste0(out_path_all, "/Nc_files/Flux"))
#Figures
file.copy(from=list.files(paste0(out_path, "/", dataset_to_use,"_sites/Nc_files/Figures"),
pattern=all_sites[ind[1]], full.names=TRUE), to=paste0(out_path_all, "/Nc_files/Figures"),
recursive = TRUE)
}
}